Repository navigation
fix(chat): keep active skill instructions after compaction #5667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
veryfront-code-auto-merge
merged 9 commits into
main
from
fix/retain-active-skill-context
Dec 5, 2026
+2,018
−110
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
447dd49
fix(chat): keep active skill instructions after compaction
kojiwakayama 34eeed0
fix(chat): keep full skill instructions after a repeated load
kojiwakayama 39e2c34
fix(chat): keep the active skill through every compaction step
kojiwakayama ab65376
test(agent): use the shared fetch mock in skill retention tests
kojiwakayama f4e70f3
fix(provider): keep only the active skill pair in Anthropic history
kojiwakayama 42fe8f2
fix(agent): store the kept skill in the compaction event
kojiwakayama bfd790c
fix(agent): keep the skill reference when call and result share a mes…
kojiwakayama 6fc7087
refactor(agent): leave the stored skill reference to a follow-up
kojiwakayama bd931ef
fix(skill): decode restored-copy ids with the captured slice
kojiwakayama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { findActiveSkill } from "#veryfront/skill/active-skill.ts"; | ||
| import { stringifyChatJson } from "#veryfront/chat/json-value.ts"; | ||
| import type { HistoricalToolOutputCompactionDiagnostic } from "#veryfront/chat/message-prep.ts"; | ||
| import { estimateTokens } from "#veryfront/chat/message-prep-budget.ts"; | ||
| import type { AgentRuntimeMessage, AgentRuntimeMessagePart } from "../runtime/message-adapter.ts"; | ||
| import { isToolResultPart } from "../runtime/tool-result-part.ts"; | ||
|
|
||
| type ActiveSkillPair = { | ||
| callIndex: number; | ||
| toolCallId: string; | ||
| toolName: string; | ||
| result: unknown; | ||
| messages: AgentRuntimeMessage[]; | ||
| }; | ||
|
|
||
| function hasToolCallId(part: AgentRuntimeMessagePart, toolCallId: string): boolean { | ||
| return "toolCallId" in part && part.toolCallId === toolCallId; | ||
| } | ||
|
|
||
| /** | ||
| * Copy a message with only one part. The part object is reused, so its trust mark | ||
| * is neither added nor lost. The new id keeps a partial copy from matching a | ||
| * provider replay checkpoint, which expects the complete message. | ||
| */ | ||
| function keepOnlyPart( | ||
| message: AgentRuntimeMessage, | ||
| part: AgentRuntimeMessagePart, | ||
| ): AgentRuntimeMessage { | ||
| return { ...message, id: `active_skill_context:${message.id}`, parts: [part] }; | ||
| } | ||
|
|
||
| /** Find the active skill call and result in messages that are about to be summarized. */ | ||
| function findSummarizedSkillPair( | ||
| messages: readonly AgentRuntimeMessage[], | ||
| tailStartIndex: number, | ||
| ): ActiveSkillPair | undefined { | ||
| const active = findActiveSkill( | ||
| messages, | ||
| (part) => isToolResultPart(part) ? { toolName: part.toolName, value: part.result } : undefined, | ||
| ); | ||
| if (!active || active.messageIndex >= tailStartIndex || !isToolResultPart(active.part)) { | ||
| return undefined; | ||
| } | ||
| const { toolCallId, toolName, result } = active.part; | ||
| for (let index = active.messageIndex - 1; index >= 0; index--) { | ||
| const message = messages[index]!; | ||
| // A tool call and its result always belong to the same turn. | ||
| if (message.role === "user") return undefined; | ||
| if (message.role !== "assistant") continue; | ||
| const call = message.parts.find((part) => | ||
| part.type !== "tool-result" && hasToolCallId(part, toolCallId) | ||
| ); | ||
| if (!call) continue; | ||
| return { | ||
| callIndex: index, | ||
| toolCallId, | ||
| toolName, | ||
| result, | ||
| messages: [ | ||
| keepOnlyPart(message, call), | ||
| keepOnlyPart(messages[active.messageIndex]!, active.part), | ||
| ], | ||
| }; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** @internal Put a summarized active skill back into compacted context when it fits. */ | ||
| export function restoreActiveSkillPair(input: { | ||
| messages: readonly AgentRuntimeMessage[]; | ||
| tailStartIndex: number; | ||
| summaryMessage: AgentRuntimeMessage; | ||
| retainedMessages: readonly AgentRuntimeMessage[]; | ||
| preservedRequest?: AgentRuntimeMessage; | ||
| usableBudget: number; | ||
| }): { | ||
| messages: AgentRuntimeMessage[]; | ||
| droppedActiveSkill?: HistoricalToolOutputCompactionDiagnostic; | ||
| } { | ||
| const { messages, summaryMessage, retainedMessages, preservedRequest } = input; | ||
| const compacted = [summaryMessage, ...retainedMessages]; | ||
| const pair = findSummarizedSkillPair(messages, input.tailStartIndex); | ||
| if (!pair) return { messages: compacted }; | ||
|
|
||
| const drop = (reason: HistoricalToolOutputCompactionDiagnostic["reason"]) => ({ | ||
| messages: compacted, | ||
| droppedActiveSkill: { | ||
| source: "provider" as const, | ||
| toolName: pair.toolName, | ||
| toolCallId: pair.toolCallId, | ||
| originalOutputChars: stringifyChatJson(pair.result).length, | ||
| retainedOutputChars: 0, | ||
| reason, | ||
| }, | ||
| }); | ||
| // Two calls with one id are invalid for a provider once the turns between them are gone. | ||
| if ( | ||
| retainedMessages.some((message) => | ||
| message.parts.some((part) => hasToolCallId(part, pair.toolCallId)) | ||
|
kojiwakayama marked this conversation as resolved.
Resolved
|
||
| ) | ||
| ) { | ||
| return drop("active_skill_tool_call_conflict"); | ||
| } | ||
|
|
||
| // Keep the original order: a preserved request can come before or after the skill call. | ||
| const requestIsFirst = preservedRequest !== undefined && | ||
| messages.indexOf(preservedRequest) < pair.callIndex; | ||
| const insertAt = requestIsFirst ? 2 : 1; | ||
| const restored = [ | ||
| ...compacted.slice(0, insertAt), | ||
| ...pair.messages, | ||
| ...compacted.slice(insertAt), | ||
| ]; | ||
| return estimateTokens(restored) <= input.usableBudget | ||
| ? { messages: restored } | ||
| : drop("active_skill_over_token_budget"); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.