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
51 changes: 51 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,57 @@ describe("ClaudeAdapterV2 runtime query policy", () => {
);
});

it("applies the thread type cache TTL and compaction point over the instance window", () => {
const options = ClaudeAdapterV2.makeClaudeQueryOptions({
modelSelection: CLAUDE_TEST_MODEL_SELECTION,
nativeThreadId: "native-thread-type",
resume: false,
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" },
});
const settings = options.settings as {
autoCompactWindow?: number;
promptCacheTtl?: string;
env?: Record<string, string>;
};
assert.equal(settings.autoCompactWindow, 272_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_PROMPT_CACHE_TTL, "1h");
assert.equal(options.env?.CLAUDE_CODE_PROMPT_CACHE_TTL, "1h");
assert.equal(
ClaudeAdapterV2.claudeAutoCompactPercent(
{ contextWindow: 272_000, compactAt: 100_000 },
200_000,
),
"55.56",
);
});

it("reports the thread type compaction point as the auto-compact threshold", () => {
const usage = ClaudeAdapterV2.claudeProviderTurnTokenUsage(
{ input_tokens: 10, output_tokens: 5 },
1_000_000,
"2026-11-20T00:00:00.000Z",
"272000",
undefined,
120_000,
);
assert.equal(usage.autoCompactThreshold, 120_002);
assert.equal(
ClaudeAdapterV2.claudeProviderTurnTokenUsage(
{ input_tokens: 10, output_tokens: 5 },
1_000_000,
"2026-11-20T00:00:00.000Z",
"272000",
).autoCompactThreshold,
239_000,
);
});

it("sends a custom model named like a Claude Code alias to the API unchanged", () => {
const options = ClaudeAdapterV2.makeClaudeQueryOptions({
modelSelection: { instanceId: CLAUDE_TEST_MODEL_SELECTION.instanceId, model: "opus" },
Expand Down
101 changes: 92 additions & 9 deletions apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,51 @@ function isClaudeEnvTruthy(value: string | undefined): boolean {
return value !== undefined && ["1", "true", "yes", "on"].includes(value.trim().toLowerCase());
}

function claudeAutoCompactThreshold(autoCompactWindow: string, maxTokens: number | null) {
const CLAUDE_COMPACT_OUTPUT_RESERVE = 20_000;
const CLAUDE_COMPACT_FLOOR_GAP = 13_000;

function claudeCompactEffectiveWindow(contextWindow: number, modelMaxTokens: number | null): number {
const window = modelMaxTokens === null ? contextWindow : Math.min(modelMaxTokens, contextWindow);
return window - CLAUDE_COMPACT_OUTPUT_RESERVE;
}

export function claudeAutoCompactPercent(
threadType: { readonly contextWindow: number; readonly compactAt: number },
modelMaxTokens: number | null = null,
): string {
const effective = claudeCompactEffectiveWindow(threadType.contextWindow, modelMaxTokens);
if (effective <= 0) return "0.00";
const hundredths = Math.ceil((threadType.compactAt * 10_000) / effective);
return (hundredths / 100).toFixed(2);
}

export function claudeAppliedCompactAt(
threadType: { readonly contextWindow: number; readonly compactAt: number },
modelMaxTokens: number | null = null,
): number {
const effective = claudeCompactEffectiveWindow(threadType.contextWindow, modelMaxTokens);
if (effective <= 0) return threadType.compactAt;
const hundredths = Math.ceil((threadType.compactAt * 10_000) / effective);
const applied = Math.floor((effective * hundredths) / 10_000);
return Math.min(applied, effective - CLAUDE_COMPACT_FLOOR_GAP);
}

function claudeAutoCompactThreshold(
autoCompactWindow: string,
maxTokens: number | null,
compactAt?: number,
) {
const windowTokens = Number(autoCompactWindow);
return windowTokens > 0 && (maxTokens === null || windowTokens < maxTokens)
if (!(windowTokens > 0)) return {};
if (compactAt !== undefined) {
return {
autoCompactThreshold: claudeAppliedCompactAt(
{ contextWindow: windowTokens, compactAt },
maxTokens,
),
};
}
return maxTokens === null || windowTokens < maxTokens
? { autoCompactThreshold: windowTokens - CLAUDE_AUTO_COMPACT_RESERVE_TOKENS }
: {};
}
Expand Down Expand Up @@ -309,6 +351,7 @@ export function claudeProviderTurnTokenUsage(
updatedAt: string,
autoCompactWindow = "",
requestedAt?: DateTime.Utc,
compactAt?: number,
) {
const inputTokens = claudeMessageInputTokens(usage);
const outputTokens = usage.output_tokens;
Expand All @@ -326,7 +369,7 @@ export function claudeProviderTurnTokenUsage(
outputTokens,
reasoningOutputTokens: 0,
...(promptCacheTtlMs === undefined ? {} : { promptCacheTtlMs }),
...claudeAutoCompactThreshold(autoCompactWindow, maxTokens),
...claudeAutoCompactThreshold(autoCompactWindow, maxTokens, compactAt),
...(promptCacheExpiresAt === undefined ? {} : { promptCacheExpiresAt }),
updatedAt,
};
Expand Down Expand Up @@ -2197,6 +2240,7 @@ export function makeClaudeQueryOptions(input: {
readonly appendSystemPromptFileText?: string;
// Flag settings, because a user's settings.json env beats the process env in Claude Code.
readonly toolSearchOverride?: "true";
readonly threadType?: ProviderAdapter.ProviderAdapterV2RuntimePolicy["threadType"];
}): ClaudeAgentSdkQueryOptions {
const compiledSelection = compileClaudeModelSelection(input.modelSelection, input.modelCatalog);
const {
Expand Down Expand Up @@ -2239,16 +2283,25 @@ export function makeClaudeQueryOptions(input: {
: typeof input.sdkSettings === "object" && input.sdkSettings !== null
? ({ ...input.sdkSettings, ...selectionSettings } as ClaudeSdkSettings)
: selectionSettings;
const threadType = input.threadType;
const modelMaxTokens =
input.modelCatalog === undefined
? null
: (resolveClaudeCatalogContextWindowTokens(input.modelCatalog, input.modelSelection) ?? null);
const autoCompactWindow =
input.settings?.autoCompactWindow === undefined || input.settings.autoCompactWindow.length === 0
? undefined
: input.settings.autoCompactWindow;
threadType !== undefined
? String(threadType.contextWindow)
: input.settings?.autoCompactWindow === undefined ||
input.settings.autoCompactWindow.length === 0
? undefined
: input.settings.autoCompactWindow;
const compactQuerySettings =
autoCompactWindow === undefined
? querySettings
: ({
...(typeof querySettings === "object" && querySettings !== null ? querySettings : {}),
autoCompactWindow: Number(autoCompactWindow),
...(threadType === undefined ? {} : { promptCacheTtl: threadType.cacheTtl }),
} as ClaudeSdkSettings);
// Claude Code expands a bare alias to its own model, so a custom model with an alias's name
// pins the alias to itself.
Expand All @@ -2275,6 +2328,15 @@ export function makeClaudeQueryOptions(input: {
...(autoCompactWindow === undefined
? {}
: { CLAUDE_CODE_AUTO_COMPACT_WINDOW: autoCompactWindow }),
...(threadType === undefined
? {}
: {
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: claudeAutoCompactPercent(
threadType,
modelMaxTokens,
),
CLAUDE_CODE_PROMPT_CACHE_TTL: threadType.cacheTtl,
}),
...(input.toolSearchOverride === undefined
? {}
: { ENABLE_TOOL_SEARCH: input.toolSearchOverride }),
Expand Down Expand Up @@ -2328,7 +2390,16 @@ export function makeClaudeQueryOptions(input: {
...(input.settings?.binaryPath
? { pathToClaudeCodeExecutable: input.settings.binaryPath }
: {}),
...(input.environment === undefined ? {} : { env: input.environment }),
...(input.environment === undefined && threadType === undefined
? {}
: {
env: {
...input.environment,
...(threadType === undefined
? {}
: { CLAUDE_CODE_PROMPT_CACHE_TTL: threadType.cacheTtl }),
},
}),
...(input.mcpServers === undefined ? {} : { mcpServers: input.mcpServers }),
systemPrompt: {
type: "preset" as const,
Expand Down Expand Up @@ -6310,7 +6381,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
? {
usedTokens: usage,
maxTokens,
...claudeAutoCompactThreshold(context.autoCompactWindow, maxTokens),
...claudeAutoCompactThreshold(
context.autoCompactWindow,
maxTokens,
context.input.runtimePolicy.threadType?.compactAt,
),
updatedAt: DateTime.formatIso(now),
}
: claudeProviderTurnTokenUsage(
Expand All @@ -6319,6 +6394,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
DateTime.formatIso(now),
context.autoCompactWindow,
context.streamUsage?.requestedAt,
context.input.runtimePolicy.threadType?.compactAt,
);
if (typeof usage !== "number") {
if (claudeMessageInputTokens(usage) <= 0) return;
Expand Down Expand Up @@ -12804,6 +12880,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
supportedDialogKinds: ["resume_return"],
...(appendSystemPromptFileText === undefined ? {} : { appendSystemPromptFileText }),
...(toolSearchOverride === undefined ? {} : { toolSearchOverride }),
...(turnInput.runtimePolicy.threadType === undefined
? {}
: { threadType: turnInput.runtimePolicy.threadType }),
});
const resolvedTurnCwd = yield* fileSystem
.realPath(turnInput.runtimePolicy.cwd ?? ".")
Expand Down Expand Up @@ -13114,11 +13193,15 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
);
const querySession = yield* openQuery(turnInput, nativeThreadId, queryPolicy);
yield* applySessionTitle(querySession.query, nativeThreadId, turnInput.appThread.title);
const autoCompactWindow = yield* resolveAutoCompactWindow(
const resolvedAutoCompactWindow = yield* resolveAutoCompactWindow(
querySession.settingsCwd,
turnInput.modelSelection,
querySession.launchArgSettings,
);
const autoCompactWindow =
turnInput.runtimePolicy.threadType === undefined || resolvedAutoCompactWindow === ""
? resolvedAutoCompactWindow
: String(turnInput.runtimePolicy.threadType.contextWindow);
const context: ActiveClaudeTurnContext = {
input: turnInput,
autoCompactWindow,
Expand Down
26 changes: 26 additions & 0 deletions apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ const isProviderAdapterEventStreamError = Schema.is(
);
const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown));

describe("CodexAdapterV2 thread type policy", () => {
it("sets the thread type compaction limit without raising the context window", () => {
const params = CodexAdapterV2.codexThreadRuntimeParams({
threadId: null,
mcpSession: undefined,
runtimePolicy: {
runtimeMode: "full-access",
interactionMode: "default",
cwd: "/workspace",
threadType: { type: "review", contextWindow: 272_000, compactAt: 120_000, cacheTtl: "5m" },
},
});
assert.equal(params.config.model_auto_compact_token_limit, 120_000);
assert.isUndefined(params.config.model_context_window);
});

it("leaves Codex defaults alone without a thread type", () => {
const params = CodexAdapterV2.codexThreadRuntimeParams({
threadId: null,
mcpSession: undefined,
});
assert.isUndefined(params.config.model_auto_compact_token_limit);
assert.isUndefined(params.config.model_context_window);
});
});

describe("CodexAdapterV2 context usage", () => {
it("uses the current context rather than cumulative processed tokens", () => {
const usage = CodexAdapterV2.codexProviderTurnTokenUsage(
Expand Down
13 changes: 12 additions & 1 deletion apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,17 @@ const CODEX_T3_MCP_TOOL_TIMEOUT_SEC = 65 * 60;
const CODEX_T3_DIRECT_MCP_SERVER = "t3-code-direct";
const CODEX_DIRECT_ONLY_T3_TOOLS = ["t3_thread_wait", "delegate_task"];

function codexThreadRuntimeParams(input: {
export function codexThreadTypeConfig(
threadType: ProviderAdapter.ProviderAdapterV2RuntimePolicy["threadType"],
): Readonly<Record<string, Schema.Json>> {
return threadType === undefined
? {}
: {
model_auto_compact_token_limit: threadType.compactAt,
};
}

export function codexThreadRuntimeParams(input: {
readonly threadId: ThreadId | null;
readonly mcpSession: McpProviderSession.McpProviderSessionConfig | undefined;
readonly modelSelection?: { readonly model: string };
Expand All @@ -1527,6 +1537,7 @@ function codexThreadRuntimeParams(input: {
...(input.modelSelection === undefined ? {} : { model: input.modelSelection.model }),
config: {
...CODEX_THREAD_CONFIG,
...codexThreadTypeConfig(input.runtimePolicy?.threadType),
// One app-server serves many threads, so the id goes in per-thread shell config, not its env.
...(input.threadId === null
? {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ describe("Grok launch permission mode", () => {
});
}).pipe(
Effect.provide(
RuntimePolicy.layerFromProjectStore.pipe(
RuntimePolicy.makeLayerFromProjectStore(
"/nonexistent/closura/thread-type-policy.json",
).pipe(
Layer.provide(NodeServices.layer),
Layer.provide(ServerSettings.layerTest()),
Layer.provide(
Layer.mock(ProjectStore.ProjectStoreV2)({
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/orchestration-v2/CheckpointRollbackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as ProjectStore from "./ProjectStore.ts";
import type * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
import { RuntimePolicyAboveCeilingError, RuntimePolicyV2 } from "./RuntimePolicy.ts";
import { isThreadTypeModelRefusedError } from "./ThreadTypePolicy.ts";
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";

export const ROLLBACK_FAILED_MESSAGE =
Expand Down Expand Up @@ -87,6 +88,7 @@ const isRuntimePolicyAboveCeilingError = Schema.is(RuntimePolicyAboveCeilingErro
*/
export function rollbackFailureClientMessage(error: CheckpointRollbackExecutionError): string {
if (isRuntimePolicyAboveCeilingError(error.cause)) return error.cause.message;
if (isThreadTypeModelRefusedError(error.cause)) return error.cause.message;
const restoreCause = isCheckpointRestoreError(error.cause) ? error.cause.cause : undefined;
return isVcsProcessExitError(restoreCause) && restoreCause.detail === GIT_INDEX_LOCKED_DETAIL
? GIT_INDEX_LOCKED_DETAIL
Expand Down
9 changes: 9 additions & 0 deletions apps/server/src/orchestration-v2/ProviderTurnStartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,15 @@ export const layer: Layer.Layer<
.pipe(Effect.result);
if (resolvedPolicy._tag === "Failure") {
const error = resolvedPolicy.failure;
if (error._tag === "ThreadTypeModelRefusedError") {
yield* settleStartFailure({
signal: "thread-type-model-refused",
title: "Model refused on this thread type",
error,
message: error.message,
});
return;
}
if (error._tag !== "RuntimePolicyAboveCeilingError") return yield* error;
yield* settleStartFailure({
signal: "runtime-mode-above-ceiling",
Expand Down
Loading
Loading