-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add pinning mode for context management #231
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import type { SessionState, WithParts } from "./state" | |
| import type { Logger } from "./logger" | ||
| import type { PluginConfig } from "./config" | ||
| import { syncToolCache } from "./state/tool-cache" | ||
| import { deduplicate, supersedeWrites, purgeErrors } from "./strategies" | ||
| import { deduplicate, supersedeWrites, purgeErrors, autoPrune } from "./strategies" | ||
| import { prune, insertPruneToolContext } from "./messages" | ||
| import { checkSession } from "./state" | ||
|
|
||
|
|
@@ -11,6 +11,7 @@ export function createChatMessageTransformHandler( | |
| state: SessionState, | ||
| logger: Logger, | ||
| config: PluginConfig, | ||
| workingDirectory: string, | ||
| ) { | ||
| return async (input: {}, output: { messages: WithParts[] }) => { | ||
| await checkSession(client, state, logger, output.messages) | ||
|
|
@@ -25,6 +26,11 @@ export function createChatMessageTransformHandler( | |
| supersedeWrites(state, logger, config, output.messages) | ||
| purgeErrors(state, logger, config, output.messages) | ||
|
|
||
| // Run auto-prune if pinning mode is enabled | ||
| if (config.tools.pinningMode.enabled) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this "if" should be handled within autoPrune |
||
| await autoPrune(client, state, logger, config, output.messages, workingDirectory) | ||
| } | ||
|
|
||
| prune(state, logger, config, output.messages) | ||
|
|
||
| insertPruneToolContext(state, config, logger, output.messages) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import type { UserMessage } from "@opencode-ai/sdk/v2" | |
| import { loadPrompt } from "../prompts" | ||
| import { extractParameterKey, buildToolIdList, createSyntheticUserMessage } from "./utils" | ||
| import { getLastUserMessage } from "../shared-utils" | ||
| import { turnsUntilAutoPrune, shouldShowAutoPruneWarning } from "../strategies/auto-prune" | ||
|
|
||
| const getNudgeString = (config: PluginConfig): string => { | ||
| const discardEnabled = config.tools.discard.enabled | ||
|
|
@@ -28,9 +29,12 @@ ${content} | |
| const getCooldownMessage = (config: PluginConfig): string => { | ||
| const discardEnabled = config.tools.discard.enabled | ||
| const extractEnabled = config.tools.extract.enabled | ||
| const pinEnabled = config.tools.pin.enabled | ||
|
|
||
| let toolName: string | ||
| if (discardEnabled && extractEnabled) { | ||
| if (pinEnabled) { | ||
| toolName = extractEnabled ? "pin or extract tools" : "pin tool" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be something like "pin, discard or extract tools" and should also handle if only one of discard / extract is enabled, so if pin and extract are enabled but discard disabled, "pin or extract tools". |
||
| } else if (discardEnabled && extractEnabled) { | ||
| toolName = "discard or extract tools" | ||
| } else if (discardEnabled) { | ||
| toolName = "discard tool" | ||
|
|
@@ -43,6 +47,16 @@ Context management was just performed. Do not use the ${toolName} again. A fresh | |
| </prunable-tools>` | ||
| } | ||
|
|
||
| const getAutoPruneWarningMessage = (state: SessionState, config: PluginConfig): string => { | ||
| const turns = turnsUntilAutoPrune(state, config) | ||
| const pinnedCount = state.pins.size | ||
|
|
||
| return `<auto-prune-warning> | ||
| Auto-prune in ${turns} turn(s). All unpinned tool outputs will be discarded. | ||
| Currently ${pinnedCount} tool(s) pinned. Use the \`pin\` tool NOW to preserve any context you need. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might need to tell the model what is pinned or what isn't, I don't think telling the model how many tools are pinned is useful information |
||
| </auto-prune-warning>` | ||
| } | ||
|
|
||
| const buildPrunableToolsList = ( | ||
| state: SessionState, | ||
| config: PluginConfig, | ||
|
|
@@ -51,6 +65,7 @@ const buildPrunableToolsList = ( | |
| ): string => { | ||
| const lines: string[] = [] | ||
| const toolIdList: string[] = buildToolIdList(state, messages, logger) | ||
| const pinningModeEnabled = config.tools.pinningMode.enabled | ||
|
|
||
| state.toolParameters.forEach((toolParameterEntry, toolCallId) => { | ||
| if (state.prune.toolIds.includes(toolCallId)) { | ||
|
|
@@ -74,7 +89,18 @@ const buildPrunableToolsList = ( | |
| const description = paramKey | ||
| ? `${toolParameterEntry.tool}, ${paramKey}` | ||
| : toolParameterEntry.tool | ||
| lines.push(`${numericId}: ${description}`) | ||
|
|
||
| // Show pin status in pinning mode | ||
| let line = `${numericId}: ${description}` | ||
| if (pinningModeEnabled) { | ||
| const pin = state.pins.get(toolCallId) | ||
| if (pin) { | ||
| const turnsRemaining = pin.expiresAtTurn - state.currentTurn | ||
| line += ` [PINNED, expires in ${turnsRemaining} turn(s)]` | ||
| } | ||
| } | ||
|
|
||
| lines.push(line) | ||
| logger.debug( | ||
| `Prunable tool found - ID: ${numericId}, Tool: ${toolParameterEntry.tool}, Call ID: ${toolCallId}`, | ||
| ) | ||
|
|
@@ -93,7 +119,12 @@ export const insertPruneToolContext = ( | |
| logger: Logger, | ||
| messages: WithParts[], | ||
| ): void => { | ||
| if (!config.tools.discard.enabled && !config.tools.extract.enabled) { | ||
| const pinEnabled = config.tools.pin.enabled | ||
| const discardEnabled = config.tools.discard.enabled | ||
| const extractEnabled = config.tools.extract.enabled | ||
|
|
||
| // Need at least one pruning tool enabled | ||
| if (!pinEnabled && !discardEnabled && !extractEnabled) { | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -111,15 +142,24 @@ export const insertPruneToolContext = ( | |
| logger.debug("prunable-tools: \n" + prunableToolsList) | ||
|
|
||
| let nudgeString = "" | ||
| // Only show nudge in non-pinning mode | ||
| if ( | ||
| !config.tools.pinningMode.enabled && | ||
| config.tools.settings.nudgeEnabled && | ||
| state.nudgeCounter >= config.tools.settings.nudgeFrequency | ||
| ) { | ||
| logger.info("Inserting prune nudge message") | ||
| nudgeString = "\n" + getNudgeString(config) | ||
| } | ||
|
|
||
| prunableToolsContent = prunableToolsList + nudgeString | ||
| // Add auto-prune warning if approaching prune cycle | ||
| let warningString = "" | ||
| if (shouldShowAutoPruneWarning(state, config)) { | ||
| logger.info("Inserting auto-prune warning message") | ||
| warningString = "\n" + getAutoPruneWarningMessage(state, config) | ||
| } | ||
|
|
||
| prunableToolsContent = prunableToolsList + nudgeString + warningString | ||
| } | ||
|
|
||
| const lastUserMessage = getLastUserMessage(messages) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should match the pattern set in tools.settings.nudgeFrequency, something like
auto-prune everything not pinned every <pruneFrequency> turns
same for other occurances below