Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
};
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,
);
});

Expand All @@ -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 },
Expand Down
18 changes: 12 additions & 6 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 +
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 10 additions & 2 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -1510,6 +1511,9 @@ export function codexThreadTypeConfig(
? {}
: {
model_auto_compact_token_limit: threadType.compactAt,
...(threadType.summaryCap === undefined
? {}
: { compact_prompt: ThreadTypePolicy.carriedSummaryInstruction(threadType.summaryCap) }),
};
}

Expand Down
9 changes: 6 additions & 3 deletions apps/server/src/orchestration-v2/RuntimePolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
Expand Down
17 changes: 14 additions & 3 deletions apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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",
});
Expand All @@ -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,
});
}),
);
Expand Down Expand Up @@ -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({}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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: [] },
Expand Down
34 changes: 29 additions & 5 deletions apps/server/src/orchestration-v2/ThreadTypePolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand All @@ -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<ThreadTypeModelRefusedError>()(
Expand All @@ -68,7 +76,7 @@ export class ThreadTypeModelRefusedError extends Schema.TaggedError<ThreadTypeMo

export const isThreadTypeModelRefusedError = Schema.is(ThreadTypeModelRefusedError);

const TITLE_TAG = /^\s*\[([a-z]+)\]/;
const TITLE_TAG = /^\s*\[([a-z]+)\]/i;

export function normalizeModel(model: string): string {
let value = model.trim().toLowerCase();
Expand All @@ -89,7 +97,7 @@ export function classifyThread(
): ThreadType {
if (thread.standing != null) return "standing";
const title = thread.title ?? "";
const tag = TITLE_TAG.exec(title)?.[1];
const tag = TITLE_TAG.exec(title)?.[1]?.toLowerCase();
if (tag !== undefined && isThreadType(tag)) return tag;
const child = isDelegatedAppThread(thread);
for (const rule of policy.classify) {
Expand Down Expand Up @@ -118,12 +126,28 @@ export function resolveThreadTypeLaunch(
}
return Effect.succeed({
type,
contextWindow: policy.context_window,
claudeContextWindow: policy.context_window.claude,
codexContextWindow: policy.context_window.codex,
compactAt: entry.compact_at,
cacheTtl: entry.cache_ttl,
...(entry.summary_cap === undefined ? {} : { summaryCap: entry.summary_cap }),
});
}

export const SUMMARY_CAP_CHARS_PER_TOKEN = 2;

export function carriedSummaryWithinCap(summaryCap: number, text: string): boolean {
return Math.ceil(text.length / SUMMARY_CAP_CHARS_PER_TOKEN) <= summaryCap;
}

export function carriedSummaryInstruction(summaryCap: number): string {
return [
`The carried summary of this compaction must stay within ${summaryCap} tokens.`,
`Count one token per ${SUMMARY_CAP_CHARS_PER_TOKEN} characters and stop at ${summaryCap * SUMMARY_CAP_CHARS_PER_TOKEN} characters.`,
"Keep decisions, open work and exact identifiers; drop narration.",
].join(" ");
}

export function threadTypePolicyPath(environment: NodeJS.ProcessEnv = process.env): string {
const override = environment[THREAD_TYPE_POLICY_PATH_ENV];
return override !== undefined && override.length > 0
Expand Down
4 changes: 3 additions & 1 deletion packages/provider-core/src/server/ProviderAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}),
),
});
Expand Down
Loading