diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index fda061b665d4f..ea84018d1bc64 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -496,25 +496,38 @@ describe("ClaudeAdapterV2 runtime query policy", () => { cwd: "/workspace", settings: { ...DEFAULT_CLAUDE_SETTINGS, autoCompactWindow: "400000" }, environment: { CLAUDE_CODE_PROMPT_CACHE_TTL: "5m" }, - threadType: { type: "standing", contextWindow: 272_000, compactAt: 100_000, cacheTtl: "1h" }, + threadType: { + type: "standing", + claudeContextWindow: 200_000, + codexContextWindow: 272_000, + compactAt: 165_000, + cacheTtl: "1h", + summaryCap: 8_000, + }, }); const settings = options.settings as { autoCompactWindow?: number; promptCacheTtl?: string; env?: Record; }; - assert.equal(settings.autoCompactWindow, 272_000); + assert.equal(settings.autoCompactWindow, 200_000); assert.equal(settings.promptCacheTtl, "1h"); - assert.equal(settings.env?.CLAUDE_CODE_AUTO_COMPACT_WINDOW, "272000"); - assert.equal(settings.env?.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE, "39.69"); + assert.equal(settings.env?.CLAUDE_CODE_AUTO_COMPACT_WINDOW, "200000"); + assert.equal(settings.env?.CLAUDE_AUTOCOMPACT_PCT_OVERRIDE, "91.66"); assert.equal(settings.env?.CLAUDE_CODE_PROMPT_CACHE_TTL, "1h"); assert.equal(options.env?.CLAUDE_CODE_PROMPT_CACHE_TTL, "1h"); + const append = (options.systemPrompt as { append?: string }).append ?? ""; + assert.match(append, /within 8000 tokens/); + assert.equal( + ClaudeAdapterV2.claudeAppliedCompactAt({ contextWindow: 200_000, compactAt: 165_000 }), + 164_988, + ); assert.equal( - ClaudeAdapterV2.claudeAutoCompactPercent( - { contextWindow: 272_000, compactAt: 100_000 }, + ClaudeAdapterV2.claudeAppliedCompactAt( + { contextWindow: 200_000, compactAt: 168_000 }, 200_000, ), - "55.56", + 166_986, ); }); @@ -527,7 +540,7 @@ describe("ClaudeAdapterV2 runtime query policy", () => { undefined, 120_000, ); - assert.equal(usage.autoCompactThreshold, 120_002); + assert.equal(usage.autoCompactThreshold, 119_977); assert.equal( ClaudeAdapterV2.claudeProviderTurnTokenUsage( { input_tokens: 10, output_tokens: 5 }, diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 7829d81349ae8..fb4b0aa1d827c 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -162,6 +162,7 @@ import * as Stream from "effect/Stream"; import * as ChildProcess from "effect/process/ChildProcess"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; +import * as ThreadTypePolicy from "../ThreadTypePolicy.ts"; import { resolveAttachmentPath } from "../../attachmentStore.ts"; import { snapshotMcpApp } from "../../mcpApps/McpAppSnapshot.ts"; import { resolveClaudeSdkExecutablePath } from "../../provider/Drivers/ClaudeExecutable.ts"; @@ -292,7 +293,9 @@ export function claudeAutoCompactPercent( ): string { const effective = claudeCompactEffectiveWindow(threadType.contextWindow, modelMaxTokens); if (effective <= 0) return "0.00"; - const hundredths = Math.ceil((threadType.compactAt * 10_000) / effective); + const reachable = effective - CLAUDE_COMPACT_FLOOR_GAP; + const target = Math.min(threadType.compactAt, reachable); + const hundredths = Math.floor((target * 10_000) / effective); return (hundredths / 100).toFixed(2); } @@ -302,7 +305,7 @@ export function claudeAppliedCompactAt( ): number { const effective = claudeCompactEffectiveWindow(threadType.contextWindow, modelMaxTokens); if (effective <= 0) return threadType.compactAt; - const hundredths = Math.ceil((threadType.compactAt * 10_000) / effective); + const hundredths = Number(claudeAutoCompactPercent(threadType, modelMaxTokens)) * 100; const applied = Math.floor((effective * hundredths) / 10_000); return Math.min(applied, effective - CLAUDE_COMPACT_FLOOR_GAP); } @@ -2290,7 +2293,7 @@ export function makeClaudeQueryOptions(input: { : (resolveClaudeCatalogContextWindowTokens(input.modelCatalog, input.modelSelection) ?? null); const autoCompactWindow = threadType !== undefined - ? String(threadType.contextWindow) + ? String(threadType.claudeContextWindow) : input.settings?.autoCompactWindow === undefined || input.settings.autoCompactWindow.length === 0 ? undefined @@ -2332,7 +2335,7 @@ export function makeClaudeQueryOptions(input: { ? {} : { CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: claudeAutoCompactPercent( - threadType, + { contextWindow: threadType.claudeContextWindow, compactAt: threadType.compactAt }, modelMaxTokens, ), CLAUDE_CODE_PROMPT_CACHE_TTL: threadType.cacheTtl, @@ -2405,7 +2408,10 @@ export function makeClaudeQueryOptions(input: { type: "preset" as const, preset: "claude_code" as const, append: [ - buildRuntimeInstructions({ harness: "Claude Code" }) + + (threadType?.summaryCap === undefined + ? "" + : ThreadTypePolicy.carriedSummaryInstruction(threadType.summaryCap) + "\n\n") + + buildRuntimeInstructions({ harness: "Claude Code" }) + (input.mcpServers === undefined ? "" : T3_CODE_ORCHESTRATION_INSTRUCTIONS + @@ -13232,7 +13238,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( const autoCompactWindow = turnInput.runtimePolicy.threadType === undefined || resolvedAutoCompactWindow === "" ? resolvedAutoCompactWindow - : String(turnInput.runtimePolicy.threadType.contextWindow); + : String(turnInput.runtimePolicy.threadType.claudeContextWindow); const context: ActiveClaudeTurnContext = { input: turnInput, autoCompactWindow, diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index aa337f1d5ced9..c5b2ae8c9fabb 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -99,11 +99,19 @@ describe("CodexAdapterV2 thread type policy", () => { runtimeMode: "full-access", interactionMode: "default", cwd: "/workspace", - threadType: { type: "review", contextWindow: 272_000, compactAt: 120_000, cacheTtl: "5m" }, + threadType: { + type: "standing", + claudeContextWindow: 200_000, + codexContextWindow: 272_000, + compactAt: 165_000, + cacheTtl: "1h", + summaryCap: 8_000, + }, }, }); - assert.equal(params.config.model_auto_compact_token_limit, 120_000); + assert.equal(params.config.model_auto_compact_token_limit, 165_000); assert.isUndefined(params.config.model_context_window); + assert.match(String(params.config.compact_prompt), /within 8000 tokens/); }); it("leaves Codex defaults alone without a thread type", () => { diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index 798503395a1ee..41d37ff92dade 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -106,6 +106,7 @@ import * as Semaphore from "effect/Semaphore"; import * as Stream from "effect/Stream"; import { ChildProcess, ChildProcessSpawner } from "effect/process"; +import * as ThreadTypePolicy from "../ThreadTypePolicy.ts"; import { resolveAttachmentPath, resolveAttachmentPathById } from "../../attachmentStore.ts"; import * as PortScanner from "../../preview/PortScanner.ts"; import { getCodexServiceTierOptionValue } from "../../codexModelOptions.ts"; @@ -1510,6 +1511,9 @@ export function codexThreadTypeConfig( ? {} : { model_auto_compact_token_limit: threadType.compactAt, + ...(threadType.summaryCap === undefined + ? {} + : { compact_prompt: ThreadTypePolicy.carriedSummaryInstruction(threadType.summaryCap) }), }; } diff --git a/apps/server/src/orchestration-v2/RuntimePolicy.test.ts b/apps/server/src/orchestration-v2/RuntimePolicy.test.ts index e1b90d64de5b6..2847cbae220c2 100644 --- a/apps/server/src/orchestration-v2/RuntimePolicy.test.ts +++ b/apps/server/src/orchestration-v2/RuntimePolicy.test.ts @@ -213,15 +213,18 @@ it.effect("applies the installed thread type policy when a turn starts", () => const standing = yield* resolve("Lead", "gpt-6.1-sol"); assert.deepEqual(standing.threadType, { type: "standing", - contextWindow: 272_000, - compactAt: 100_000, + claudeContextWindow: 200_000, + codexContextWindow: 272_000, + compactAt: 165_000, cacheTtl: "1h", + summaryCap: 8_000, }); const review = yield* resolve("Review PR 953", "gpt-6-astra"); assert.deepEqual(review.threadType, { type: "review", - contextWindow: 272_000, + claudeContextWindow: 200_000, + codexContextWindow: 272_000, compactAt: 120_000, cacheTtl: "5m", }); diff --git a/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts b/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts index 1fe730d2c3ba0..4165a145a9ce7 100644 --- a/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts @@ -14,6 +14,8 @@ describe("ThreadTypePolicy", () => { ["JEFFDESK:DESK — Jeff Omarchy operations desktop", false, "standing"], ["CoS", true, "coding"], ["[standing] PR landing manager", true, "standing"], + ["[Standing] PR landing manager", true, "standing"], + ["[CODING] a standing role thread", true, "coding"], ["[research] CoS notes", false, "research"], ["KSS ----> analyses with Kameron", false, "conversational"], ["Independent code-review of CLO-924", true, "review"], @@ -74,7 +76,8 @@ describe("ThreadTypePolicy", () => { ); assert.deepEqual(review, { type: "review", - contextWindow: 272_000, + claudeContextWindow: 200_000, + codexContextWindow: 272_000, compactAt: 120_000, cacheTtl: "5m", }); @@ -85,9 +88,11 @@ describe("ThreadTypePolicy", () => { ); assert.deepEqual(standing, { type: "standing", - contextWindow: 272_000, - compactAt: 100_000, + claudeContextWindow: 200_000, + codexContextWindow: 272_000, + compactAt: 165_000, cacheTtl: "1h", + summaryCap: 8_000, }); }), ); @@ -118,6 +123,12 @@ describe("ThreadTypePolicy", () => { }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); + it("caps a carried summary at one token per two characters", () => { + assert.equal(ThreadTypePolicy.carriedSummaryWithinCap(8_000, "a".repeat(16_000)), true); + assert.equal(ThreadTypePolicy.carriedSummaryWithinCap(8_000, "a".repeat(16_001)), false); + assert.match(ThreadTypePolicy.carriedSummaryInstruction(8_000), /8000 tokens/); + }); + it("reads the policy from the Closura config path unless the environment overrides it", () => { assert.match( ThreadTypePolicy.threadTypePolicyPath({}), diff --git a/apps/server/src/orchestration-v2/ThreadTypePolicy.testFixtures.ts b/apps/server/src/orchestration-v2/ThreadTypePolicy.testFixtures.ts index 4d98ac1521c2c..cbfe294291fa1 100644 --- a/apps/server/src/orchestration-v2/ThreadTypePolicy.testFixtures.ts +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.testFixtures.ts @@ -6,7 +6,7 @@ export const THREAD_TYPE_POLICY_FIXTURE: ThreadTypePolicyFile = { schema: "thread-type-policy-v1", source: "closura-agent-config config/thread-type-policy.json", decision: "CLO-924", - context_window: 272_000, + context_window: { claude: 200_000, codex: 272_000 }, default_type: "coding", classify: [ { @@ -39,7 +39,12 @@ export const THREAD_TYPE_POLICY_FIXTURE: ThreadTypePolicyFile = { }, ], types: { - standing: { compact_at: 100_000, cache_ttl: "1h", refused_models: ["gpt-6-astra"] }, + standing: { + compact_at: 165_000, + cache_ttl: "1h", + refused_models: ["gpt-6-astra"], + summary_cap: 8_000, + }, coding: { compact_at: 168_000, cache_ttl: "5m", refused_models: [] }, review: { compact_at: 120_000, cache_ttl: "5m", refused_models: [] }, research: { compact_at: 168_000, cache_ttl: "1h", refused_models: [] }, diff --git a/apps/server/src/orchestration-v2/ThreadTypePolicy.ts b/apps/server/src/orchestration-v2/ThreadTypePolicy.ts index bb3560002629a..de3d4015d0069 100644 --- a/apps/server/src/orchestration-v2/ThreadTypePolicy.ts +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.ts @@ -24,13 +24,19 @@ const ThreadTypeEntry = Schema.Struct({ compact_at: TokenCount, cache_ttl: CacheTtl, refused_models: Schema.Array(Schema.String), + summary_cap: Schema.optional(TokenCount), +}); + +const ContextWindows = Schema.Struct({ + claude: TokenCount, + codex: TokenCount, }); export const ThreadTypePolicyFile = Schema.Struct({ schema: Schema.Literal("thread-type-policy-v1"), source: Schema.String, decision: Schema.String, - context_window: TokenCount, + context_window: ContextWindows, default_type: ThreadTypeName, classify: Schema.Array( Schema.Struct({ type: ThreadTypeName, top_level_only: Schema.Boolean, title: Schema.String }), @@ -48,9 +54,11 @@ export type ThreadTypePolicyFile = typeof ThreadTypePolicyFile.Type; export interface ThreadTypeLaunch { readonly type: ThreadType; - readonly contextWindow: number; + readonly claudeContextWindow: number; + readonly codexContextWindow: number; readonly compactAt: number; readonly cacheTtl: "5m" | "1h"; + readonly summaryCap?: number; } export class ThreadTypeModelRefusedError extends Schema.TaggedError()( @@ -68,7 +76,7 @@ export class ThreadTypeModelRefusedError extends Schema.TaggedError 0 diff --git a/packages/provider-core/src/server/ProviderAdapter.ts b/packages/provider-core/src/server/ProviderAdapter.ts index 061e79815d725..7e7e4a9ce0607 100644 --- a/packages/provider-core/src/server/ProviderAdapter.ts +++ b/packages/provider-core/src/server/ProviderAdapter.ts @@ -59,9 +59,11 @@ export const ProviderAdapterV2RuntimePolicy = Schema.Struct({ threadType: Schema.optional( Schema.Struct({ type: Schema.String, - contextWindow: Schema.Number, + claudeContextWindow: Schema.Number, + codexContextWindow: Schema.Number, compactAt: Schema.Number, cacheTtl: Schema.Literals(["5m", "1h"]), + summaryCap: Schema.optional(Schema.Number), }), ), });