Skip to content

feat(handoff): transactional notebook discard during context handoff - #19

Open
ofriw wants to merge 16 commits into
mainfrom
notebook-deletion
Open

feat(handoff): transactional notebook discard during context handoff#19
ofriw wants to merge 16 commits into
mainfrom
notebook-deletion

Conversation

@ofriw

@ofriw ofriw commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Note: This PR was generated by an AI agent. If you'd like to talk with other humans, drop by our Discord!


Why this PR exists

The notebook is the agent's working memory — facts, decisions, architecture notes that survive context restarts. Three problems were compounding:

  1. No way to clean up stale notebook pages without nuking the whole session (/new). Agents accumulated noise.
  2. Children and successor agents had no coordination contract. The notebook was framed as passive reference material ("grounding"), so agents didn't think to write intermediate results for sibling/successor agents to consume.
  3. CI audit tests were flaky — npm's CLI HTTP client (minipass-fetch) chokes on the registry's gzip encoding. False positives erode trust.

What this PR changes for users and agents

Agents can now prune stale notebook pages during handoffhandoff("next task", discardPages=["stale-page"]). Deletion is transactional with the context restart: if the delete fails, all pages survive and the fresh context gets a warning instead of silently losing data. This means cleaner context resets without /new.

The notebook is now "shared memory" — not "grounding". Spawned children store results for the parent. Handoff successors pick up where the last context left off. The system prompt explicitly tells agents: "Use the notebook as a shared memory between spawned agents and across handoff contexts." This unblocks multi-agent pipelines where children and handoff chains coordinate through a shared namespace.

Handoff tool descriptions got simpler — agents were seeing internal implementation detail (~30% heuristic, primer ordering) that they don't need to make good decisions. Now the description says what handoff does and when to use it, not how it works internally.

Spawn tests dropped mocks because mocks weren't catching real bugs. The import ordering issue in handoff/tool.ts (static import of notebook/store broke createAgentSession) passed all mock-based tests — they never exercised the real SDK. Tests now run against real agent sessions in sandboxed temp dirs. The sessionFactory seam is gone. Cleanup failures surface via UI notifications or error cause instead of a WeakMap nobody checked.

CI audit is no longer at the mercy of npm's broken fetch library — native Node.fetch() with manual gzip detection. Three new advisory overrides documented for known transitive protobufjs/brace-expansion paths.

The one thing to watch

The two-phase commit protocol for discardPages (prepare → compact → commit) means Pi's compaction callback must fire for pages to actually be deleted. If compaction is somehow bypassed, pages stay — which is the safe fallback, but worth noting if you see unexpected page retention.


Attached is an agent optimized description of the changes in this PR - AGENT_REVIEW.md

ofriw added 16 commits July 26, 2026 10:19
…ages

Epoch is now a predictable sequential integer (1) instead of Date.now(),
enabling the generation-based discard mechanism. Rehydration scans
generation markers to ignore staged survivors from interrupted discards.

DiscardPages lets agents prune stale notebook pages during handoff:
prepareNotebookDiscard stages survivors, commitNotebookDiscard advances
the epoch only after compaction succeeds. Interrupted discards leave
the branch on the prior generation.
…ions

Removes the sessionFactory parameter from executeSpawn and
registerSpawnTool, replacing mock-based lifecycle tests with pure
function tests and real child invocations via a deterministic provider.
Simplifies cleanup error handling from WeakMap to UI notify + cause chain.
Moves concurrent and abortion tests to real invocations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant