Skip to content

fix(gateway): stabilize inter-session completion wake prompts#2155

Open
BingqingLyu wants to merge 4 commits into
mainfrom
fork-pr-63096-af-63030-completion-wake-context
Open

fix(gateway): stabilize inter-session completion wake prompts#2155
BingqingLyu wants to merge 4 commits into
mainfrom
fork-pr-63096-af-63030-completion-wake-context

Conversation

@BingqingLyu

@BingqingLyu BingqingLyu commented Apr 28, 2026

Copy link
Copy Markdown
Owner

Summary

Describe the problem and fix in 2–5 bullets:

  • Problem: inter-session task_completion wakes sent through the gateway agent path did not rebuild the session-backed Inbound Context / Group Chat Context suffix that normal chat turns include.
  • Why it matters: the volatile system-prompt suffix changed on ACP/subagent completion notifications, which busted Anthropic prompt cache reuse and caused expensive cache rewrites.
  • What changed: src/gateway/server-methods/agent.ts now synthesizes persisted session context for inter-session completion wakes when no explicit extraSystemPrompt is provided, and preserves explicit caller-provided prompt context when it is present.
  • What did NOT change (scope boundary): no gateway protocol/schema changes, no changes to the normal inbound chat prompt path, and no changes to internal event formatting beyond using the existing session metadata to rebuild prompt context.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

Root Cause (if applicable)

  • Root cause: the completion-wake path entered through gateway agent with inputProvenance.kind="inter_session" and task_completion internal events, but unlike the normal chat path it did not reconstruct the session-derived extraSystemPrompt sections that contain Inbound Context and Group Chat Context.
  • Missing detection / guardrail: there was no gateway-seam regression test asserting that inter-session completion wakes preserve the same session-derived prompt context as ordinary chat turns.
  • Contributing context (if known): these sections live below the cache boundary and above ## Runtime, so missing them changes the system prompt digest even when session state and transcript history are otherwise unchanged.

Regression Test Plan (if applicable)

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: src/gateway/server-methods/agent.test.ts
  • Scenario the test should lock in: an inter-session task_completion wake for an existing session should rebuild persisted inbound/group prompt context when extraSystemPrompt is omitted, and should preserve explicit extraSystemPrompt when one is provided.
  • Why this is the smallest reliable guardrail: the bug lives at the gateway agent ingress seam, where session metadata, provenance, and internal events are combined before the run is dispatched.
  • Existing test that already covers this (if any): does not create task rows for inter-session completion wakes covers adjacent routing behavior, but not prompt-context reconstruction.
  • If no new test is added, why not: N/A

User-visible / Behavior Changes

  • ACP/subagent completion notifications routed through the gateway now reuse the same persisted session context shaping as normal chat turns.
  • For affected Anthropic sessions, completion wakes should stop causing unnecessary prompt-cache invalidation from missing session-derived suffix sections.
  • No config, CLI, or protocol behavior changed.

Diagram (if applicable)

Before:
[child task completes]
  -> [gateway agent wake with inter_session provenance]
  -> [internal events only]
  -> [system prompt misses inbound/group context]
  -> [systemDigest changes] -> [prompt cache bust]

After:
[child task completes]
  -> [gateway agent wake with inter_session provenance]
  -> [rebuild persisted inbound/group context from session entry]
  -> [system prompt matches normal session shaping]
  -> [stable systemDigest] -> [prompt cache reused]

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.

System prompt assembled differently across code paths (chat/heartbeat/announce), causing continuous Anthropic cache invalidation

2 participants