Review the prerequisites
Suitable for: Backend developers learning Agent applications.
- Computation, references, and consistent snapshots for large results
- The working input of the model can be small, and the computational basis for the conclusions cannot be silently reduced. The complete data is saved in the controlled artifact, the value calculated by the program is determined, and the model is read and interpreted on demand; citations, pagination and abstracts must indicate the version, scope and completeness.
How does the mechanism work?
Step-by-step explanation
- There are 100 rows on the previous page. Can you give the total amount directly?
Context truncation changes what is visible; it must not silently change the scope of conclusions.
- What should I do if the artifact reference is obtained by another tenant?
Externalizing data reduces context load but introduces a separate read boundary.
- How to handle underlying data updates during paging?
Pagination requires consistency across pages as well as splitting results into chunks.
Implementation · Build it
Implement and check the complete contract
Objectives of this level: Implement exact page aggregation, snapshot/epoch binding and bounded inline context.
Visual guide · Small context, complete computation
The packer separates observed sums from full totals. Incomplete pages keep total=null; artifact references still require current authorization on later reads. Select the figure to open it at full size.
Read the full contract before coding
The following preserves the existing local function, exact shape rules, output precedence and fixed inputs. Implement it in your own module, retain the first failed report, then explain what the adapter must additionally prove in a real deployment.
Derive the invariant
A short sample is useful for reading but cannot establish a financial total. Separate exact aggregation over a declared complete snapshot, bounded inline text and currently authorized artifact metadata. Preserve the query and snapshot identity across pages; observed partial cents and full total are different facts.
Executable entry and responsibility
Same after-sales project: ticket-1 belongs to tenant A. Junior responsibility derives input/failure boundaries; intermediate responsibility implements and repairs the function; senior responsibility owns integration facts and recovery tradeoffs. These are responsibilities, not the four tracks or a calibrated hiring ladder. Python3.9+ standard library; extract the package and keep lab_runner.py beside cases/. Scaffold refuses overwrites.
- https://ai.lwkjob.com/lab/extended-practice/README.md
- https://ai.lwkjob.com/lab/extended-practice/lab_runner.py
- https://ai.lwkjob.com/lab/extended-practice/cases/tool-result-artifacts.json
- https://ai.lwkjob.com/downloads/extended-agent-practice-v1.zip
python3 lab_runner.py scaffold --course tool-result-artifacts --out pack_artifacts_student.py
python3 lab_runner.py grade --course tool-result-artifacts --module pack_artifacts_student.py --out tool-result-artifacts-report.json
def pack_artifacts(payload):
"""Implement the public contract; disclose assistance used."""
raise NotImplementedError("tool-result-artifacts")
Complete function contract
pack_artifacts(payload) accepts exactly tenant/task_id (nonempty strings), budget_chars (strict integer >=0), results (list), dataset and principal. Every object below has exactly the stated keys. principal={tenant,epoch,enabled}: tenant is nonempty, epoch is a strict integer >=1, enabled is strictly boolean. dataset={snapshot,query,expected_rows,pages}: snapshot is nonempty; query={currency,status}, both nonempty strings; expected_rows is a strict integer >=0. pages is an ordered list of {snapshot,query,cursor,next_cursor,rows}; cursor/next_cursor are null or nonempty strings; rows is a list of {id,bank,cents}, id/bank nonempty and cents a strict signed integer. These are a trusted server adapter's snapshot/query/count/permission fixtures; model text cannot grant access or establish a complete dataset. No floating currency arithmetic.
A result has exactly tool (nonempty string), kind (text/artifact), content, artifact. text requires string content and artifact=null. artifact requires content=null and exactly {tenant,task_id,uri,sha256,bytes,snapshot,query,permission_epoch}: string fields are nonempty, sha256 is64 lowercase hex characters, bytes is a strict integer >=0, permission_epoch a strict integer >=1, query as above. Shape/type validation for the whole payload runs first: ValueError("input_shape"). Then principal.tenant must equal tenant and enabled must be True, otherwise current_permission. Artifact tenant/task_id mismatch is artifact_identity; artifact permission_epoch differing from current principal.epoch is stale_reference. Any page or artifact snapshot/query differing from dataset is snapshot_binding. These gates precede aggregation/URI checks; migration or regranting permissions cannot reuse an old-epoch reference.
Page order is the supplied order. The first cursor must be null, each later cursor equals the preceding next_cursor, and a terminal next_cursor=null cannot have a following page: page_chain. Repeated row id anywhere is row_duplicate. Provided row count larger than expected_rows is coverage_overflow. Calculate exact observed_sum_cents and observed_by_bank_cents across every supplied row; group banks in lexical order. Completeness means row_count==expected_rows AND the last next_cursor is null. No pages with expected_rows=0 is a complete empty dataset; no pages with expected_rows>0 is incomplete. A nonnull tail remains incomplete even when the declared count is reached. summary has exactly {snapshot,query,row_count,expected_rows,complete,observed_sum_cents,observed_by_bank_cents,total_cents,by_bank_cents}; total_cents/by_bank_cents equal observed values only if complete, otherwise both are null. Partial observed values never claim the full total. The adapter must independently substantiate expected_rows and snapshot consistency in a real database; equal metadata alone does not create database isolation.
After these gates, artifact URI must parse as absolute HTTPS with a netloc and without credentials or fragment: artifact_uri. This is local URI syntax, not URL reachability. Process text in input order, take the remaining prefix in Python Unicode code points and deduct its length; context rows are {tool,text,truncated}, with strict boolean truncated=True only when some original text was omitted. Keep a context row even when the remaining budget is0. Metadata is separate and does not consume this inline character budget; it is not a model token limit or complete serialized-output limit. Merge artifact metadata by exact URI, reject differing sha256 or bytes as artifact_conflict, deduplicate/sort tools and sort artifacts by URI. Each manifest row has exactly {uri,sha256,bytes,tools,snapshot,query,permission_epoch}. Return exactly {context,artifacts,remaining_chars,summary}, with new nested containers and no input mutation.
The function performs independent integer aggregation, local completeness/identity/epoch decisions and context packing. It does not execute SQL pagination, read storage, verify file bytes/hash, authenticate callers, atomically check changing permissions or validate real receipts. example.invalid URLs and capacity/count values are teaching fixtures. The next real read must check current authorization again; passing this snapshot contract does not grant future access.
Three public inputs and exact expectations
text-capped-artifact-kept
{
"input": {
"tenant": "A",
"task_id": "ticket-1",
"budget_chars": 5,
"results": [
{
"tool": "search",
"kind": "text",
"content": "Refund policy",
"artifact": null
},
{
"tool": "read_policy",
"kind": "artifact",
"content": null,
"artifact": {
"tenant": "A",
"task_id": "ticket-1",
"uri": "https://example.invalid/artifacts/A/ticket-1/policy.txt",
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bytes": 4096,
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"permission_epoch": 1
}
}
],
"principal": {
"tenant": "A",
"epoch": 1,
"enabled": true
},
"dataset": {
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"expected_rows": 3,
"pages": [
{
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"cursor": null,
"next_cursor": null,
"rows": [
{
"id": "row-1",
"bank": "bank-A",
"cents": 10
},
{
"id": "row-2",
"bank": "bank-B",
"cents": 20
},
{
"id": "row-3",
"bank": "bank-A",
"cents": 30
}
]
}
]
}
},
"expected": {
"context": [
{
"tool": "search",
"text": "Refun",
"truncated": true
}
],
"artifacts": [
{
"uri": "https://example.invalid/artifacts/A/ticket-1/policy.txt",
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bytes": 4096,
"tools": [
"read_policy"
],
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"permission_epoch": 1
}
],
"remaining_chars": 0,
"summary": {
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"row_count": 3,
"expected_rows": 3,
"complete": true,
"observed_sum_cents": 60,
"observed_by_bank_cents": {
"bank-A": 40,
"bank-B": 20
},
"total_cents": 60,
"by_bank_cents": {
"bank-A": 40,
"bank-B": 20
}
}
},
"error": null
}
incomplete-page-never-total
{
"input": {
"tenant": "A",
"task_id": "ticket-1",
"budget_chars": 5,
"results": [
{
"tool": "search",
"kind": "text",
"content": "Refund policy",
"artifact": null
},
{
"tool": "read_policy",
"kind": "artifact",
"content": null,
"artifact": {
"tenant": "A",
"task_id": "ticket-1",
"uri": "https://example.invalid/artifacts/A/ticket-1/policy.txt",
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bytes": 4096,
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"permission_epoch": 1
}
}
],
"principal": {
"tenant": "A",
"epoch": 1,
"enabled": true
},
"dataset": {
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"expected_rows": 3,
"pages": [
{
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"cursor": null,
"next_cursor": "cursor-2",
"rows": [
{
"id": "row-1",
"bank": "bank-A",
"cents": 10
},
{
"id": "row-2",
"bank": "bank-B",
"cents": 20
}
]
}
]
}
},
"expected": {
"context": [
{
"tool": "search",
"text": "Refun",
"truncated": true
}
],
"artifacts": [
{
"uri": "https://example.invalid/artifacts/A/ticket-1/policy.txt",
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bytes": 4096,
"tools": [
"read_policy"
],
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"permission_epoch": 1
}
],
"remaining_chars": 0,
"summary": {
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"row_count": 2,
"expected_rows": 3,
"complete": false,
"observed_sum_cents": 30,
"observed_by_bank_cents": {
"bank-A": 10,
"bank-B": 20
},
"total_cents": null,
"by_bank_cents": null
}
},
"error": null
}
authority-revoked
{
"input": {
"tenant": "A",
"task_id": "ticket-1",
"budget_chars": 5,
"results": [
{
"tool": "search",
"kind": "text",
"content": "Refund policy",
"artifact": null
},
{
"tool": "read_policy",
"kind": "artifact",
"content": null,
"artifact": {
"tenant": "A",
"task_id": "ticket-1",
"uri": "https://example.invalid/artifacts/A/ticket-1/policy.txt",
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bytes": 4096,
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"permission_epoch": 1
}
}
],
"principal": {
"tenant": "A",
"epoch": 1,
"enabled": false
},
"dataset": {
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"expected_rows": 3,
"pages": [
{
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"cursor": null,
"next_cursor": null,
"rows": [
{
"id": "row-1",
"bank": "bank-A",
"cents": 10
},
{
"id": "row-2",
"bank": "bank-B",
"cents": 20
},
{
"id": "row-3",
"bank": "bank-A",
"cents": 30
}
]
}
]
}
},
"expected": null,
"error": "ValueError:current_permission"
}
Scope and evidence
All cases are public fixed local simulations. The grader imports and calls your function, checks exact JSON types (bool is not int), immutable input and no dict/list aliases in the return. Return0 only when all cases pass; return1 for contract failures; malformed CLI/overwrite returns2. It records input, expected/actual/error, source/module/case hashes and Python version. Five seconds is a hang guard, not performance evidence. No model, remote tool, production load, cryptographic receipt validation or real learner outcomes were measured. Submit your source, initial red report, personal diff, rerun report and a short tradeoff note; published expected values and author calibration do not prove independent mastery.
code example
Implement the function contract · incomplete-page-never-total
A short sample is useful for reading but cannot establish a financial total. Separate exact aggregation over a declared complete snapshot, bounded inline text and currently authorized artifact metadata. Preserve the query and snapshot identity across pages; observed partial cents and full total are different facts.
{
"input": {
"tenant": "A",
"task_id": "ticket-1",
"budget_chars": 5,
"results": [
{
"tool": "search",
"kind": "text",
"content": "Refund policy",
"artifact": null
},
{
"tool": "read_policy",
"kind": "artifact",
"content": null,
"artifact": {
"tenant": "A",
"task_id": "ticket-1",
"uri": "https://example.invalid/artifacts/A/ticket-1/policy.txt",
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bytes": 4096,
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"permission_epoch": 1
}
}
],
"principal": {
"tenant": "A",
"epoch": 1,
"enabled": true
},
"dataset": {
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"expected_rows": 3,
"pages": [
{
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"cursor": null,
"next_cursor": "cursor-2",
"rows": [
{
"id": "row-1",
"bank": "bank-A",
"cents": 10
},
{
"id": "row-2",
"bank": "bank-B",
"cents": 20
}
]
}
]
}
},
"expected": {
"context": [
{
"tool": "search",
"text": "Refun",
"truncated": true
}
],
"artifacts": [
{
"uri": "https://example.invalid/artifacts/A/ticket-1/policy.txt",
"sha256": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"bytes": 4096,
"tools": [
"read_policy"
],
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"permission_epoch": 1
}
],
"remaining_chars": 0,
"summary": {
"snapshot": "snapshot-1",
"query": {
"currency": "CNY",
"status": "posted"
},
"row_count": 2,
"expected_rows": 3,
"complete": false,
"observed_sum_cents": 30,
"observed_by_bank_cents": {
"bank-A": 10,
"bank-B": 20
},
"total_cents": null,
"by_bank_cents": null
}
},
"error": null
}
Acceptance task for this level
Implement pack_artifacts in pack_artifacts_student.py without copying a public solver; run every fixed case and retain the first failed report before repair.
Reading this browser’s record…
Hide the answer and check your understanding
What are the complete three-row total and the partial two-row total?
Expand reference derivation
Complete rows10/20/30 cents produce total60 and bank A40/B20. A two-row prefix has observed30 but total_cents=null and by_bank_cents=null while its tail is nonterminal.
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.