Review the prerequisites
Suitable for: Backend developers learning Agent applications.
- Hard filters, value ranking, and causal evaluation in memory retrieval
- First use authorization, revocation and validity period to determine the memory that can be used, and then select a small amount of content based on task benefits. Hit rates describe recall behavior, and only task comparisons can prove that memory creates value.
How does the mechanism work?
Step-by-step explanation
- What should I do if the revocation record still remains in the vector index?
As scale grows, index consistency becomes a practical limit on validity filtering.
- How to use high similarity and low credibility?
Candidates that pass hard filters can still differ in trustworthiness.
- How to prove that the memory module is really useful?
Move evaluation from internal retrieval metrics back to user-task outcomes.
Debugging · Diagnose failures
Public repair exercise: same inputs
Objectives of this level: current memory eligibility, key precedence and context fitting — Locate the supplied fault and repair a copy on unchanged input.
Visual guide · Eligibility before memory ranking
Greedy score ordering is this lab’s policy, not a global optimum. Retrieval hits do not prove task benefit, and remembered preferences grant no approval. Select the figure to open it at full size.
Public repair exercise: same inputs
The score-first bad function includes m-revoked in revoked-high-score. It also injects style in current-explicit-key, despite the current task owning that key. Repair current metadata eligibility before sorting and dedup, then budget accumulation without negative remainder. Preserve exact-budget and small-budget-skip-costly as positive controls. This is guided_repair: the faulty category is supplied. Save the function below in a separate memory_retrieval_budget_debug.py. Before consulting a repair, submit the first divergent input/field, two hypotheses and a discriminating check. Repair this copy, save your diff and rerun the identical cases; keep the original cases file unchanged.
def select_memory(payload):
# Known fault: a score and available room do not prove current eligibility.
rows = sorted(payload["records"], key=lambda row: (-row["score"], row["id"]))
ids, used = [], 0
for row in rows:
if used + row["tokens"] <= payload["budget"]:
ids.append(row["id"])
used += row["tokens"]
return {"ids": ids, "used": used, "remaining": payload["budget"] - used}
python3 lab_runner.py grade --course memory-retrieval-budget --module memory_retrieval_budget_debug.py --out memory_retrieval_budget_debug_before.json
python3 lab_runner.py grade --course memory-retrieval-budget --module memory_retrieval_budget_debug.py --out memory_retrieval_budget_debug_after.json
Starting point and evidence
Download the source-only ZIP, extract it, and work in extended-agent-practice-v1 with lab_runner.py beside cases/. Python3.9+ standard library; no model, cloud, network or business request is made. Scaffold once in a fresh directory; it refuses overwrite. Reuse your module in later tracks. The empty template must exit1 on grade; scaffold exits0, overwrite exits2. Grade exits0 only when every fixed case passes and otherwise exits1.
- https://ai.lwkjob.com/downloads/extended-agent-practice-v1.zip
- https://ai.lwkjob.com/lab/extended-practice/lab_runner.py
- https://ai.lwkjob.com/lab/extended-practice/cases/memory-retrieval-budget.json
python3 lab_runner.py scaffold --course memory-retrieval-budget --out memory_retrieval_budget_student.py
python3 lab_runner.py grade --course memory-retrieval-budget --module memory_retrieval_budget_student.py --out memory_retrieval_budget_before.json
This course has12 public fixed cases. Each calls your select_memory(payload), records input/expected/actual/error, strict JSON types, input immutability and no output-container aliases, plus module/runner/cases SHA and Python version. Inputs are complete JSON fixtures. Unknown or missing keys, null outside explicitly allowed fields, wrong types, duplicate IDs/list members where uniqueness is required raise ValueError:input_shape; booleans are not integers. Exceptions match exact class:message. Return fresh JSON values without mutating input.
Trusted identity, approvals, receipts and policy/index metadata are fixtures assembled by the exercise. In a real service the server must obtain them from authenticated/current authority sources; accepting client JSON with these labels does not authenticate it. The function is a decision over that trusted snapshot, not the adapter or race-proof enforcement.
Local function contract
payload exactly principal/now/budget/current_keys/records. principal exactly tenant/user/project(nonempty str),epoch(strict positive int),enabled(strict bool). now/budget strict nonnegative int; current_keys is a unique nonempty str list, possibly empty. records may be empty; each exactly id/tenant/user/project/key(nonempty str),epoch/tokens(strict positive int),score(strict nonnegative int),confirmed/revoked(strict bool),expires_at(None or strict nonnegative int); IDs unique globally. Shape errors first. Keep only enabled principal,same tenant/user/project/epoch,confirmed=True,revoked=False,expires_at>now or None,key absent from current_keys. Sort score descending then id ascending. Keep only the first eligible row per key BEFORE fitting budget; if it is too large, skip that key, not fall back to a weaker duplicate. Continue to other keys that fit. Return exactly ids(in selection order),used(sum teaching token units),remaining(budget-used). Empty/disabled/zero-budget returns empty IDs,used0,unchanged remaining. No tokenization or knapsack optimization is performed.
def select_memory(payload):
raise NotImplementedError("Implement the public local contract")
Three reviewable inputs
exact-budget
{
"principal": {
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"enabled": true
},
"now": 10,
"budget": 5,
"current_keys": [],
"records": [
{
"id": "m-style",
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"key": "style",
"tokens": 3,
"score": 80,
"confirmed": true,
"revoked": false,
"expires_at": null
},
{
"id": "m-locale",
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"key": "locale",
"tokens": 2,
"score": 70,
"confirmed": true,
"revoked": false,
"expires_at": null
}
]
}
Expected / 预期: {"ids": ["m-style", "m-locale"], "remaining": 0, "used": 5}
revoked-high-score
{
"principal": {
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"enabled": true
},
"now": 10,
"budget": 5,
"current_keys": [],
"records": [
{
"id": "m-style",
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"key": "style",
"tokens": 3,
"score": 80,
"confirmed": true,
"revoked": false,
"expires_at": null
},
{
"id": "m-locale",
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"key": "locale",
"tokens": 2,
"score": 70,
"confirmed": true,
"revoked": false,
"expires_at": null
},
{
"id": "m-revoked",
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"key": "style",
"tokens": 3,
"score": 100,
"confirmed": true,
"revoked": true,
"expires_at": null
}
]
}
Expected / 预期: {"ids": ["m-style", "m-locale"], "remaining": 0, "used": 5}
small-budget-skip-costly
{
"principal": {
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"enabled": true
},
"now": 10,
"budget": 2,
"current_keys": [],
"records": [
{
"id": "m-style",
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"key": "style",
"tokens": 3,
"score": 80,
"confirmed": true,
"revoked": false,
"expires_at": null
},
{
"id": "m-locale",
"tenant": "acme",
"user": "alice",
"project": "after-sales",
"epoch": 2,
"key": "locale",
"tokens": 2,
"score": 70,
"confirmed": true,
"revoked": false,
"expires_at": null
}
]
}
Expected / 预期: {"ids": ["m-locale"], "remaining": 0, "used": 2}
Responsibility and limits
Junior implements authorized selection with exact budget boundaries. Intermediate would recheck metadata at body release and cache reads and measure ablation on unchanged tasks. Senior owns current-source dependencies, index lag/copy invalidation and workload/retention policies. These integer costs are authored units, not provider token usage, model relevance or production effectiveness. The four tracks are learning activities, not job ranks. Fixed authored examples and known repairs cannot prove independent diagnosis, real-world security or hiring readiness. Keep source, initial failure, personal diff, same-input rerun and a declared changed-condition result. A reviewer-new complete contract and observed personal work are separate; disclose assistance. The five-second worker guard is not a performance SLO or hostile-code sandbox.
code example
Public repair exercise: same inputs — small-budget-skip-costly
Additional review: explain why this case follows the contract and which single-field change would alter the branch. Public case: small-budget-skip-costly Expected / 预期: {"ids": ["m-locale"], "remaining": 0, "used": 2} https://ai.lwkjob.com/lab/extended-practice/cases/memory-retrieval-budget.json
Acceptance task for this level
current memory eligibility, key precedence and context fitting — Run the supplied faulty copy, record a first divergence and two hypotheses, then repair it with a personal diff and the unchanged-case rerun.
Reading this browser’s record…
Hide the answer and check your understanding
Which observation locates the score-first bug?
Expand reference derivation
m-revoked enters selection before authoritative revocation/current-key checks.
Further explanations and practice
When encountering unfamiliar principles, first read the implementation, continuous questioning and migration cases, and then independently explain the premise and boundaries. Answers and notes are saved to the original account record.
All linked explanations and exercises (1 )
Sources and verification scope
The principles are based on public information; the numbers, cases and tasks are the teaching design of this website. Offline experiments verify the range noted on this page, and the learning effect still needs to be judged through independent tasks and feedback.