Implement #4: Context and Conversation Management#35
Merged
Conversation
Chapter 4 plus its examples/04-context/ samples: multi-turn history, system prompts, token counting + rolling-window trim, history summarization, prompt caching, and persisted per-user Telegram sessions. Wires the chapter into the VitePress sidebar and README. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
14 tasks
The 35-line sample cap, with no readability counter-rule and no formatter, pushed the implement/review agents to golf samples that did not fit - collapsing fetch helpers into one chained expression, using the comma operator to sequence side effects - purely to hit the count. - Appendix B: readability outranks line count (one statement per line, no comma operators, no statement stacking); soft target 35, hard cap 45 to leave room for the standalone-file boilerplate readable code needs. - Appendix A review agent: a correct-but-dense one-liner is now a defect to re-expand, not a pass. - Step 7 verify: spot-check readability, aim <=35 / hard cap 45. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Expand the one-line arrow (chained `(await fetch(...)).json()`) into a normal multi-line async function: one statement per line, 41 lines. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Apply the no-golf convention to the two remaining compressed samples,
harmonizing both on the telegram-bot.ts helper style (a multi-line,
de-chained `async function`):
- telegram-stream.ts: expand the `stream.on('text')` body (drop the
`;`-stacked statements and the `(last = now), edit(...)` comma operator
into one statement per line), split `let text='', last=0`, de-chain the
one-line `tg` arrow, hoist `const model`. 44 lines.
- telegram-sessions.ts: collapse the over-expanded fetch options back to
the shared inline helper shape and hoist `const model`. 40 lines.
Both still run (boot-smoked), tsc is clean, and the docs build renders
the de-golfed snippets.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Increase the example code budget in the write-chapter skill (aim <=70, hard cap 100 lines, comment:code <=0.40) and note that verbose, explicit code is welcome. Regenerate examples/03-repl-telegram/telegram-stream.ts from scratch in that style: expanded fetch/stream option objects, multi-line type definitions, named intermediates, braced guards, and section blank lines. 89 lines, behavior unchanged. Runs (boot-smoked), tsc clean, docs build renders the verbose snippet. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Rewrite from scratch in the verbose style now allowed by the raised code budget: expanded fetch/create option objects, multi-line Update type, a named persist() function, an explicit load-from-disk loop, and braced guards. 76 lines, behavior unchanged. Comments kept minimal (one helper note) so the chapter-4 prose stays the single home for the session concept (one-home rule). Boot-smoked (loads a seeded sessions.json into the Map and polls), tsc clean, docs build renders the verbose snippet. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
… for(;;) Add a loop-idiom convention to the write-chapter skill: for unbounded produce-then-consume loops prefer an async generator (async function* + for await...of), else while (true), never for (;;). Refactor all three Telegram samples to a shared pollUpdates() generator that long-polls getUpdates and yields one update at a time, consumed with a top-level for await loop. This separates transport (poll + offset) from handling and matches the for await...of style the REPLs already use. Rename the per-file helper to tg() across all three for consistency. All three boot-smoked, tsc clean, docs build renders the generator. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4
Chapter 4 teaches the stateful layer over the stateless Messages API: conversation history, the
systemprompt, token counting + rolling-window trimming, history summarization, prompt caching, and persisted per-user Telegram sessions. Authored by thewrite-chapterultracode Workflow (1 doc agent + 6 implement/review pipeline pairs), then verified end-to-end.Definition of done
chapters/04-context.md,<=120lines,<=4main-line H2s plus an optional closerbun run, imported via<<< @/examples/04-context/file.ts,<=35lines, comment:code<=0.30@anthropic-ai/sdksurface; ASCII punctuation only:::warning, a:::tip, and one comparison table); the main line reads complete without themREADME.mdand the.vitepress/config.tssidebar;bun x vitepress buildpassescache_read_input_tokensVerification (run live against an Anthropic-compatible gateway)
Chapter budget:
Sample budgets (all
<=35lines, comment:code<=0.30):bunx tsc --noEmitclean;bun x vitepress@2.0.0-alpha.17 buildpasses (chapter 4 renders, all six snippet imports resolve). Each sample was run, not just typechecked:history holds 6 messages across 3 turns.systemthen block-arraysystem.counting against GLM-4.6; with a small demo threshold the rolling window trims in pairs and holds atwindow=3 msgsinstead of growing.history is 78 tokens; threshold 30->compacted 4 turns into 2, replaced by one injected user/assistant summary pair.request 1 created=0 read=0,request 2 created=0 read=6272. The non-zerocache_read_input_tokens=6272on the second request is the cache hit. (Note: the gateway used for this run does not surfacecache_creation_input_tokens, so it reads as 0 here via a?? 0guard; on Anthropic direct, request 1 reports the creation count. The DoD field,cache_read_input_tokens, is non-zero as required.)sessions.jsoninto its per-chat.idMapat startup and long-pollsgetUpdatescleanly; type-checked and snippet resolves in the build. The transientsessions.jsonartifact was removed soexamples/04-context/ships exactly the six.tsfiles.Review flags
None - the Workflow returned all six samples as fully compliant (no flagged files).