Review the prerequisites
Suitable for: Backend developers learning Agent applications.
- Reconnecting clients and durable task state
- The connection carries progress notifications, and task status is persisted by the backend. Reconnection only restores the observation position. Repeated submission, cancellation and business completion require independent and clear semantics.
How does the mechanism work?
Step-by-step explanation
- What should I do if the last completed event is lost?
After separating the task from the stream, handle lost terminal-state notifications.
- How to deal with the backlog logs of slow clients?
Progress persistence also requires resource and retention boundaries.
- Should closing the page automatically cancel it?
A user's connection status does not automatically express an intent to cancel the business operation.
Implementation · Build it
Implement and check the complete contract
Objectives of this level: Implement scoped cursor consumption and duplicate conflict detection.
Visual guide · Advance only through continuity
A display cursor resumes observation, not business execution. Bind tenant and trace; a gap must not be hidden by jumping to the largest sequence. 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 reconnect can redeliver text that was already displayed. The display cursor and business checkpoint solve different problems: concatenating every received chunk repeats output, while skipping a missing sequence fabricates continuity.
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/agent-streaming-resume.json
- https://ai.lwkjob.com/downloads/extended-agent-practice-v1.zip
python3 lab_runner.py scaffold --course agent-streaming-resume --out resume_stream_student.py
python3 lab_runner.py grade --course agent-streaming-resume --module resume_stream_student.py --out agent-streaming-resume-report.json
def resume_stream(payload):
"""Implement the public contract; disclose assistance used."""
raise NotImplementedError("agent-streaming-resume")
Complete function contract
resume_stream(payload) accepts exactly checkpoint and chunks. checkpoint has exactly tenant/trace (nonempty strings), last_seq (strict nonnegative int), text (string), complete (strict bool). Each chunk has exactly tenant/trace (nonempty strings), seq (strict positive int), kind (delta/done), text (string; done requires empty text). Validate all shapes first: ValueError("input_shape"). Every chunk must match checkpoint tenant and trace, otherwise ValueError("stream_identity"). Within this input, same seq/same full record deduplicates; same seq/different record raises ValueError("chunk_conflict").
Discard sequences <= checkpoint.last_seq as already displayed. These historical chunks are not checked against a retained per-chunk hash, because checkpoint has none. Of new chunks, a complete checkpoint cannot accept any; no sequence may follow the earliest done chunk, even across a gap. Those cases raise ValueError("after_done"). Consume only contiguous last_seq+1 events: append delta text, advance even for empty delta, and include done in last_seq/applied. Never jump over a gap.
Return exactly text, last_seq, status, need_seq, applied. status is complete if checkpoint or consumed done marks completion; otherwise waiting if unapplied new chunks remain; otherwise streaming. need_seq is null for complete or last_seq+1; applied lists consumed sequence numbers in ascending order. Out-of-order arrival is sorted logically, not proof of wall-clock chronology. Output does not contain the buffered gap suffix: the caller must retain it or refetch it. This is display-event assembly, not restarting an Agent, tool-result dedup, an SSE server, or an outbox implementation.
Three public inputs and exact expectations
contiguous-complete
{
"input": {
"checkpoint": {
"tenant": "A",
"trace": "stream-1",
"last_seq": 1,
"text": "Refund ",
"complete": false
},
"chunks": [
{
"tenant": "A",
"trace": "stream-1",
"seq": 3,
"kind": "done",
"text": ""
},
{
"tenant": "A",
"trace": "stream-1",
"seq": 2,
"kind": "delta",
"text": "queued"
}
]
},
"expected": {
"text": "Refund queued",
"last_seq": 3,
"status": "complete",
"need_seq": null,
"applied": [
2,
3
]
},
"error": null
}
gap-does-not-advance
{
"input": {
"checkpoint": {
"tenant": "A",
"trace": "stream-1",
"last_seq": 1,
"text": "Refund ",
"complete": false
},
"chunks": [
{
"tenant": "A",
"trace": "stream-1",
"seq": 3,
"kind": "delta",
"text": "late"
}
]
},
"expected": {
"text": "Refund ",
"last_seq": 1,
"status": "waiting",
"need_seq": 2,
"applied": []
},
"error": null
}
history-ignored
{
"input": {
"checkpoint": {
"tenant": "A",
"trace": "stream-1",
"last_seq": 1,
"text": "Refund ",
"complete": false
},
"chunks": [
{
"tenant": "A",
"trace": "stream-1",
"seq": 1,
"kind": "delta",
"text": "old replay"
},
{
"tenant": "A",
"trace": "stream-1",
"seq": 2,
"kind": "delta",
"text": "queued"
}
]
},
"expected": {
"text": "Refund queued",
"last_seq": 2,
"status": "streaming",
"need_seq": 3,
"applied": [
2
]
},
"error": null
}
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 · gap-does-not-advance
A reconnect can redeliver text that was already displayed. The display cursor and business checkpoint solve different problems: concatenating every received chunk repeats output, while skipping a missing sequence fabricates continuity.
{
"input": {
"checkpoint": {
"tenant": "A",
"trace": "stream-1",
"last_seq": 1,
"text": "Refund ",
"complete": false
},
"chunks": [
{
"tenant": "A",
"trace": "stream-1",
"seq": 3,
"kind": "delta",
"text": "late"
}
]
},
"expected": {
"text": "Refund ",
"last_seq": 1,
"status": "waiting",
"need_seq": 2,
"applied": []
},
"error": null
}
Acceptance task for this level
Implement resume_stream in resume_stream_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
May seq3 be consumed when the last displayed sequence is1 and seq2 is absent?
Expand reference derivation
No. Keep last_seq1, report waiting/need_seq2, and retain or refetch the unconsumed suffix.
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.