03: Enhanced Context Restart
このコンテンツはまだ日本語訳がありません。
handleEnhancedContextRestart intercepts context_restart_* payloads from successful tool results.
Instead of recording a standard tool-history entry, it mutates the turn’s live context items and
streaming flags in place, signaling the loop to restart generation with an enriched context window.
The restart protocol and payload transports
Section titled “The restart protocol and payload transports”When a tool completes successfully, executeToolCall passes toolResult.data to
handleEnhancedContextRestart. If data.type begins with "context_restart_", the stage applies
the requested mutations and returns true.
Tools deliver enrichment payloads through one of two transports:
-
Inline payload (
enhanced_context_item): Lightweight items such as YouTube video links, web page summaries, or directive notes travel directly inside the tool result object. -
Stashed payload (
pending_context_key): Bulk media items such as profile picture image buffers (peek_profile_picture) or GIF keyframes (process_gif) use the side-channel stash insrc/utils/chat/pendingEnhancedContext.ts. The tool stashes the item viastashEnhancedContextItemand returns only a UUID key. During restart,resolveEnhancedContextItemdrains the item usingtakeEnhancedContextItem. The stash bounds memory:ToolRegistryretains the last 1000 tool execution results for diagnostic inspection, so placing multi-megabyte media payloads directly inToolResult.datawould hold them in memory indefinitely. The stash enforces a 5-minute TTL and a 16-entry ceiling.
Context mutations and disable flags
Section titled “Context mutations and disable flags”Mutations depend on the restart type suffix:
- Message metadata reveal (
message_metadata): The stage fetches reactor names for the history messages that carry reactions (fetchReactionUserSummaries), then callsannotateRecentMessageMetadataInContextto enrich recent dialogue messages with author identity, timestamps, message reference IDs, and those reactors. Ordinary turns show reaction counts only, because each reactor lookup is a sequential Discord REST call. It appends a tail directive message viabuildTailDirectiveMessageinstructing the model not to callreveal_message_metadataagain during this turn. It then setsstreamingContext.disableMessageMetadataContext = true. - Media enrichment (
youtube,image,gif): The resolvedenhanced_context_itemis appended toparams.context.contextItems. The stage sets the corresponding disable flag onstreamingContext:disableYouTubeProcessing = truedisableProfilePictureProcessing = truedisableGifProcessing = trueTool availability and execution checks consume these flags to refuse repeated enrichment in the current turn. A new turn initializes fresh flags.
Flow control and loop restart
Section titled “Flow control and loop restart”When handleEnhancedContextRestart returns true:
executeToolCallreturns{ kind: "restart" }.runToolLoopresetsconsecutiveToolErrorsandnaiConsecutiveToolFailuresto 0.- With Response Drafting Off, the restart call is omitted from
functionHistory. With it On, the review state retains the call and its successful result plus continuation message. The raw control payload still selects the enrichment, and restart outcomes are excluded from successful-call replay because a replacement attempt rebuilds context without the injected media. - The loop issues a
continuestatement, advancing the iteration counter and callingstreamOncewith the updated context.
Constraints and invariants
Section titled “Constraints and invariants”- Single drain: Calling
takeEnhancedContextItemdeletes the stashed item upon retrieval, so stale media cannot be replayed on subsequent restarts. - Resilient resolution: If a
pending_context_keycannot be resolved (for example, if expired by TTL), a warning is logged and the turn proceeds without the media item rather than failing the turn. - Prompt integrity: Because system prompts are compiled at context-build time, tool prompt macros cannot dynamically rewrite the compiled prompt after execution. The metadata tail directive discourages repeat calls; live tool availability and execution guards enforce the disable flags.
Source pointers
Section titled “Source pointers”src/utils/chat/toolLoop.ts:handleEnhancedContextRestart,resolveEnhancedContextItem.src/utils/chat/pendingEnhancedContext.ts:stashEnhancedContextItem,takeEnhancedContextItem.src/utils/chat/contextAnnotations.ts:annotateRecentMessageMetadataInContext,buildRevealedMessageMetadataTailDirective,buildTailDirectiveMessage.src/types/misc/context.ts:StructuredContextItem.