Skip to content

fix(chat): keep active skill instructions after compaction - #5667

Draft
kojiwakayama wants to merge 6 commits into
mainfrom
fix/retain-active-skill-context
Draft

kojiwakayama wants to merge 6 commits into
mainfrom
fix/retain-active-skill-context

Conversation

@kojiwakayama

@kojiwakayama kojiwakayama commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Problem

After a new user message, the model could lose the instructions of the skill it was using. A long load_skill result was replaced with [load_skill output omitted (N chars)].

Cause

maskOldToolOutputs shortens tool results from earlier turns. It had no rule for the active skill. Only small results (up to 4,096 characters) from the previous turn, and messages in the preserved list, were kept. Turn compression could also remove the skill when the history was over the token budget.

Change

  • Keep the active skill. The full instructions of the latest successfully loaded skill stay in history, for load_skill and veryfront__load_skill.
  • Handle repeated loads. Loading the same skill again returns a short "already loaded" notice. The notice counts as a repeat only when an earlier full result for the same skill exists; it is then not treated as the instructions. The most recent full result for that skill is kept.
  • Same rules for everything else. Older skills, failed loads, reference files and similar results from other tools are still masked by the existing rules.
  • Token budget. Only the matching call and result are kept, and only if they fit. If not, the old behavior applies and the drop is recorded. This covers prepareProviderModelMessagesFromUiMessages and compactForStep.
  • Hosted compaction. After the context budget summarizes history, the skill's call and result are put back when they fit the usable budget. The event stores the ids of the kept call and result, so later requests can rebuild the pair. Restored copies are never trusted.
  • Anthropic requests. The request builder dropped finished tool rounds before the last historical text answer. It now keeps the text, skill call and skill result of the round that loaded the active skill. Parallel calls and thinking beside it are dropped; signed thinking stays only when the skill call is the only tool. If other messages separate the call from its result, neither is sent. Input validation checks both possible user message groupings.
  • Tool search. A masked tool search result now tells the model to search again before it calls a tool.
  • Diagnostics. The new HistoricalToolOutputCompactionDiagnostic type lists masked outputs, and an active skill dropped for budget (active_skill_over_token_budget) or for a reused tool call ID (active_skill_tool_call_conflict). The hosted budget result has a new droppedActiveSkill field and a debug log. Hosted execution returns it as historicalToolOutputCompactions, and logs it when a context-budget logger is set.
  • No new permissions. Kept text is history only. It does not activate a skill or become trusted.

Code moves: the activation check moved unchanged to src/skill/activation-result.ts, and the policy file still exports it. The "already loaded" text now lives there too, so the writer and the reader share it. enforceTokenBudgetWithTurnCompression has a new optional fifth parameter, a map. New files: src/skill/active-skill.ts, src/skill/active-skill-context-id.ts and src/agent/hosted/active-skill-context.ts. findActiveSkillPromptCall and the type ActiveSkillCall are new exports of veryfront/provider/shared. The chat API reference was regenerated.

Tests

  • Before any fix: 2 tests failed (11 steps) in message-prep.test.ts and chat-preparation.test.ts. Some steps fail because the new diagnostics or search marker did not exist yet.
  • Before the repeated-load fix: 1 test failed (6 steps), all for repeated loads.
  • With the latest source changes reversed: 4 tests failed (7 steps), covering hosted compaction, matching pairs, the repeat notice and Anthropic requests.
  • Anthropic pair filtering: with the previous source, 5 new test steps failed (parallel calls, missing result, thinking and server tools, validation, message list).
  • Stored skill reference: with the source changes reversed, 8 tests (4 steps) failed. With the latest change, 2,146 tests (4,095 steps) passed, plus format, lint, typecheck and test typecheck.
  • Before that change: 1,320 tests (2,331 steps) passed, including all of src/agent/hosted and the Anthropic extension. Format, lint, typecheck, test typecheck, module boundary, client bundle, size limit and API docs checks passed.

Limits

  • A message with server-side tool calls or results in its content is still replayed exactly, so it keeps its parallel calls, as on main.
  • If a later tail starts at a message before the restored pair, replay brings back the full stored messages from there, so tokensAfter is too low.
  • If the call and result share one message id, the pair is not stored and tokensAfter still counts it.
  • Limits the user sets on agent memory (maxMessages, maxTokens) can still drop the skill.
  • The per-step compactForStep callers do not collect the new diagnostics.
  • The obsolete-tool test is a guard, not a failing-first test.

Not verified

  • A real hosted agent run, and which messages the API marks as trusted in a deployed environment.
  • The full unit suite, left to CI.
  • An end-to-end replay: the test copies the replay rules and reuses the summary message as is.
  • The vendored run contract is not re-pinned yet.

Keep the latest successful load_skill result in prepared history, within the token budget. Masked tool search results now tell the model to search again. Report compacted tool outputs in preparation diagnostics.
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

📦 Client bundle boundary

Entrypoint Modules Source size Server leaks
src/index.client.ts 66 371 KiB ✅ 0
src/react/components/Head.tsx 5 74 KiB ✅ 0
src/react/router/index.tsx 5 74 KiB ✅ 0
src/react/context/index.tsx 5 74 KiB ✅ 0
src/react/fonts/index.ts 6 81 KiB ✅ 0
src/react/components/ui/index.ts 111 720 KiB ✅ 0
src/react/components/ui/adapter/contract.ts 1 18 KiB ✅ 0
src/react/components/ui/icons/index.ts 3 17 KiB ✅ 0
src/chat/index.ts 305 2055 KiB ✅ 0
src/chat/ag-ui.ts 61 464 KiB ✅ 0
src/chat/protocol.ts 1 11 KiB ✅ 0
src/chat/types.ts 46 248 KiB ✅ 0
src/chat/message-prep.ts 80 588 KiB ✅ 0
src/markdown/index.ts 6 30 KiB ✅ 0
src/mdx/index.ts 2 2 KiB ✅ 0
src/agent/identity-contracts.ts 1 3 KiB ✅ 0
src/schemas/index.ts 43 222 KiB ✅ 0
src/platform/env.ts 5 55 KiB ✅ 0
src/workflow/claude-code/react/index.ts 9 91 KiB ✅ 0
src/events/index.ts 37 631 KiB ✅ 0
src/events/ag-ui/public.ts 36 624 KiB ✅ 0
src/run-events/index.ts 54 323 KiB ✅ 0

A server module in a client graph aborts hydration in the browser. New leaks fail CI; known leaks are tracked in scripts/lint/client-bundle-baseline.json to burn down.

@kojiwakayama
kojiwakayama marked this pull request as ready for review December 3, 2026 23:20
@kojiwakayama

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T23:59:07.444148Z f4e70f3 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 34eeed0705

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agent/hosted/chat-preparation.ts Resolved
Comment thread src/chat/active-skill-retention.ts Resolved
Comment thread src/chat/active-skill-retention.ts Resolved
Keep only the matching load_skill call and result when a turn is compressed. Put the pair back after hosted context compaction when it fits, and report it when it does not. Keep the active skill round in Anthropic requests. Treat repeat-notice text as a repeat only when an earlier full result exists. Fix a type error in the retention tests.
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

@codex review

@kojiwakayama

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 39e2c34778

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: ab6537644b

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Compact the round that loaded the active skill, but keep its call and result. Drop thinking and parallel calls beside a partial round. Require the result to be next to the call. Check both possible user message groupings in input validation.
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

@codex review

@kojiwakayama

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f4e70f3f27

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

@kojiwakayama
kojiwakayama marked this pull request as draft December 4, 2026 00:35
Later requests can now rebuild the same context. The event names the
stored call and result messages, and the kept copies are never trusted.

This branch has not been deployed

No deployments
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