From 8fb1ae58f68ba249ddc2f2e078231e668f2e503c Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Fri, 20 Nov 2026 01:21:57 +0000 Subject: [PATCH 1/3] Apply the Closura thread type policy at turn start RuntimePolicy reads ~/.config/closura/thread-type-policy.json (installed by closura-agent-config machine_sync) and classifies each thread as standing, coding, review, research, ops or conversational. A model the type refuses, such as gpt-6-astra on a standing thread, fails the turn start with a clear message. Claude turns get the type's prompt cache TTL and compaction point; Codex threads get model_context_window and model_auto_compact_token_limit. A missing or invalid policy file keeps today's behavior. Model: claude-opus-5-5 Task: CLO-924 Agent: claude-code Lane: magpie Harness: t3/claude-code Model-family: Claude Model-generation: 5.5 Skills-SHA: 709beb59 Co-Authored-By: Claude Opus 5.5 --- .../Adapters/ClaudeAdapterV2.test.ts | 41 +++++ .../Adapters/ClaudeAdapterV2.ts | 60 +++++-- .../Adapters/CodexAdapterV2.test.ts | 26 +++ .../Adapters/CodexAdapterV2.ts | 14 +- .../Adapters/GrokAdapterV2.test.ts | 5 +- .../ProviderTurnStartService.ts | 9 ++ .../orchestration-v2/RuntimePolicy.test.ts | 73 ++++++++- .../src/orchestration-v2/RuntimePolicy.ts | 130 +++++++++------ .../orchestration-v2/ThreadTypePolicy.test.ts | 112 +++++++++++++ .../ThreadTypePolicy.testFixtures.ts | 67 ++++++++ .../src/orchestration-v2/ThreadTypePolicy.ts | 152 ++++++++++++++++++ .../src/server/ProviderAdapter.ts | 8 + 12 files changed, 630 insertions(+), 67 deletions(-) create mode 100644 apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts create mode 100644 apps/server/src/orchestration-v2/ThreadTypePolicy.testFixtures.ts create mode 100644 apps/server/src/orchestration-v2/ThreadTypePolicy.ts diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index c07ab96746eb5..095fe8fec73eb 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -488,6 +488,47 @@ 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" }, + threadType: { type: "standing", contextWindow: 272_000, compactAt: 100_000, cacheTtl: "1h" }, + }); + const settings = options.settings as { + autoCompactWindow?: number; + promptCacheTtl?: string; + env?: Record; + }; + 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, "36.76"); + }); + + 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_000); + 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" }, diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 25d9d616cb939..d44b9b5eaba92 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -278,13 +278,28 @@ function isClaudeEnvTruthy(value: string | undefined): boolean { return value !== undefined && ["1", "true", "yes", "on"].includes(value.trim().toLowerCase()); } -function claudeAutoCompactThreshold(autoCompactWindow: string, maxTokens: number | null) { +function claudeAutoCompactThreshold( + autoCompactWindow: string, + maxTokens: number | null, + compactAt?: number, +) { const windowTokens = Number(autoCompactWindow); return windowTokens > 0 && (maxTokens === null || windowTokens < maxTokens) - ? { autoCompactThreshold: windowTokens - CLAUDE_AUTO_COMPACT_RESERVE_TOKENS } + ? { + autoCompactThreshold: Math.min( + windowTokens - CLAUDE_AUTO_COMPACT_RESERVE_TOKENS, + compactAt ?? Number.POSITIVE_INFINITY, + ), + } : {}; } +export function claudeAutoCompactPercent( + threadType: NonNullable, +): string { + return String(Math.round((threadType.compactAt * 10_000) / threadType.contextWindow) / 100); +} + function claudeMessageInputTokens(usage: { readonly input_tokens: number; readonly cache_creation_input_tokens?: number | null; @@ -309,6 +324,7 @@ export function claudeProviderTurnTokenUsage( updatedAt: string, autoCompactWindow = "", requestedAt?: DateTime.Utc, + compactAt?: number, ) { const inputTokens = claudeMessageInputTokens(usage); const outputTokens = usage.output_tokens; @@ -326,7 +342,7 @@ export function claudeProviderTurnTokenUsage( outputTokens, reasoningOutputTokens: 0, ...(promptCacheTtlMs === undefined ? {} : { promptCacheTtlMs }), - ...claudeAutoCompactThreshold(autoCompactWindow, maxTokens), + ...claudeAutoCompactThreshold(autoCompactWindow, maxTokens, compactAt), ...(promptCacheExpiresAt === undefined ? {} : { promptCacheExpiresAt }), updatedAt, }; @@ -2197,6 +2213,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 { @@ -2239,16 +2256,21 @@ export function makeClaudeQueryOptions(input: { : typeof input.sdkSettings === "object" && input.sdkSettings !== null ? ({ ...input.sdkSettings, ...selectionSettings } as ClaudeSdkSettings) : selectionSettings; + const threadType = input.threadType; 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. @@ -2275,6 +2297,9 @@ export function makeClaudeQueryOptions(input: { ...(autoCompactWindow === undefined ? {} : { CLAUDE_CODE_AUTO_COMPACT_WINDOW: autoCompactWindow }), + ...(threadType === undefined + ? {} + : { CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: claudeAutoCompactPercent(threadType) }), ...(input.toolSearchOverride === undefined ? {} : { ENABLE_TOOL_SEARCH: input.toolSearchOverride }), @@ -6310,7 +6335,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( @@ -6319,6 +6348,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; @@ -12804,6 +12834,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 ?? ".") @@ -13114,11 +13147,14 @@ 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( - querySession.settingsCwd, - turnInput.modelSelection, - querySession.launchArgSettings, - ); + const autoCompactWindow = + turnInput.runtimePolicy.threadType === undefined + ? yield* resolveAutoCompactWindow( + querySession.settingsCwd, + turnInput.modelSelection, + querySession.launchArgSettings, + ) + : String(turnInput.runtimePolicy.threadType.contextWindow); 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 019dcf4ec0edb..95eb2f4b0c8f2 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -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 and the context cap on thread start", () => { + 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.equal(params.config.model_context_window, 272_000); + }); + + 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( diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index d1ddd569e60d5..984bfd127ce77 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -1503,7 +1503,18 @@ 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> { + return threadType === undefined + ? {} + : { + model_context_window: threadType.contextWindow, + 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 }; @@ -1527,6 +1538,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 ? {} diff --git a/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts index dec55480af300..045e2b08a9826 100644 --- a/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts @@ -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)({ diff --git a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts index 5996a090bf4bd..5e37e8c46324a 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnStartService.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnStartService.ts @@ -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", diff --git a/apps/server/src/orchestration-v2/RuntimePolicy.test.ts b/apps/server/src/orchestration-v2/RuntimePolicy.test.ts index 6f9fbe53d911a..b73d38db1142e 100644 --- a/apps/server/src/orchestration-v2/RuntimePolicy.test.ts +++ b/apps/server/src/orchestration-v2/RuntimePolicy.test.ts @@ -1,4 +1,5 @@ import { assert, it } from "@effect/vitest"; +import * as NodeServices from "@effect/platform-node/NodeServices"; import { type ManagedPolicySummary, type ModelSelection, @@ -11,6 +12,7 @@ import { } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Stream from "effect/Stream"; @@ -20,6 +22,7 @@ import * as ServerSettings from "../serverSettings.ts"; import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; import * as ProjectStore from "./ProjectStore.ts"; import * as RuntimePolicy from "./RuntimePolicy.ts"; +import { THREAD_TYPE_POLICY_FIXTURE } from "./ThreadTypePolicy.testFixtures.ts"; const projectId = ProjectId.make("project:runtime-policy"); const providerInstanceId = ProviderInstanceId.make("codex"); @@ -32,6 +35,7 @@ function makeThread(input: { readonly now: DateTime.Utc; readonly worktreePath: string | null; readonly runtimeMode?: RuntimeMode; + readonly title?: string; }): OrchestrationV2AppThread { const threadId = ThreadId.make("thread:runtime-policy"); return { @@ -39,7 +43,7 @@ function makeThread(input: { creationSource: "web", id: threadId, projectId, - title: "Runtime policy", + title: input.title ?? "Runtime policy", providerInstanceId, modelSelection, runtimeMode: input.runtimeMode ?? "full-access", @@ -78,8 +82,14 @@ const providerInstanceFor = (instanceId: ProviderInstanceId) => }, }) as ProviderInstance; -const layerWithPolicy = (policy?: ManagedPolicySummary) => - RuntimePolicy.layerFromProjectStore.pipe( +const missingThreadTypePolicyPath = "/nonexistent/closura/thread-type-policy.json"; + +const layerWithPolicy = ( + policy?: ManagedPolicySummary, + threadTypePolicyPath: string = missingThreadTypePolicyPath, +) => + RuntimePolicy.makeLayerFromProjectStore(threadTypePolicyPath).pipe( + Layer.provide(NodeServices.layer), Layer.provide(ServerSettings.layerTest({}, policy)), Layer.provide( Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, { @@ -157,8 +167,65 @@ it.layer(layerTest)("RuntimePolicyV2", (it) => { assert.equal(yield* modeFor(providerInstanceId, "auto-accept-edits"), "auto-accept-edits"); }), ); + + it.effect("starts a turn with no thread type when no policy file is installed", () => + Effect.gen(function* () { + const policy = yield* RuntimePolicy.RuntimePolicyV2; + const now = yield* DateTime.now; + const resolved = yield* policy.resolve({ + thread: makeThread({ now, worktreePath: "/work", title: "Lead" }), + modelSelection: { instanceId: providerInstanceId, model: "gpt-6-astra" }, + }); + assert.isUndefined(resolved.threadType); + }), + ); }); +it.effect("applies the installed thread type policy when a turn starts", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const dir = yield* fileSystem.makeTempDirectoryScoped(); + const path = `${dir}/thread-type-policy.json`; + yield* fileSystem.writeFileString(path, JSON.stringify(THREAD_TYPE_POLICY_FIXTURE)); + const now = yield* DateTime.now; + const resolve = (title: string, model: string) => + Effect.gen(function* () { + const policy = yield* RuntimePolicy.RuntimePolicyV2; + return yield* policy.resolve({ + thread: makeThread({ now, worktreePath: "/work", title }), + modelSelection: { instanceId: providerInstanceId, model }, + }); + }).pipe(Effect.provide(layerWithPolicy(undefined, path))); + + const refused = yield* resolve("Lead", "gpt-6-astra").pipe(Effect.flip); + assert.equal(refused._tag, "ThreadTypeModelRefusedError"); + assert.equal( + refused.message, + "gpt-6-astra is refused on standing threads by the Closura thread type policy. Pick another model to continue.", + ); + + const standing = yield* resolve("Lead", "gpt-6.1-sol"); + assert.deepEqual(standing.threadType, { + type: "standing", + contextWindow: 272_000, + compactAt: 100_000, + cacheTtl: "1h", + }); + + const review = yield* resolve("Review PR 953", "gpt-6-astra"); + assert.deepEqual(review.threadType, { + type: "review", + contextWindow: 272_000, + compactAt: 120_000, + cacheTtl: "5m", + }); + + const coding = yield* resolve("CLO-924 per-thread-type policy", "gpt-6-astra"); + assert.equal(coding.threadType?.type, "coding"); + assert.equal(coding.threadType?.compactAt, 168_000); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + it.layer( layerWithPolicy({ organization: "Example Corp", diff --git a/apps/server/src/orchestration-v2/RuntimePolicy.ts b/apps/server/src/orchestration-v2/RuntimePolicy.ts index f0d0ae38577d1..e5e0494696f1f 100644 --- a/apps/server/src/orchestration-v2/RuntimePolicy.ts +++ b/apps/server/src/orchestration-v2/RuntimePolicy.ts @@ -10,6 +10,7 @@ import { } from "@t3tools/contracts"; import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; @@ -18,6 +19,7 @@ import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry. import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; import * as ServerSettings from "../serverSettings.ts"; import * as ProjectStore from "./ProjectStore.ts"; +import * as ThreadTypePolicy from "./ThreadTypePolicy.ts"; export class RuntimePolicyResolveError extends Schema.TaggedError()( "RuntimePolicyResolveError", @@ -56,6 +58,7 @@ export const isRuntimePolicyAboveCeilingError = Schema.is(RuntimePolicyAboveCeil export const RuntimePolicyV2Error = Schema.Union([ RuntimePolicyResolveError, RuntimePolicyAboveCeilingError, + ThreadTypePolicy.ThreadTypeModelRefusedError, ]); export type RuntimePolicyV2Error = typeof RuntimePolicyV2Error.Type; @@ -137,62 +140,89 @@ export const layerEnforcingCeiling: Layer.Layer< }), ); -export const layerFromProjectStore: Layer.Layer< +export const applyThreadTypePolicy = Effect.fn("RuntimePolicyV2.applyThreadTypePolicy")(function* ( + policyPath: string, + input: { readonly thread: OrchestrationV2AppThread; readonly modelSelection: ModelSelection }, +) { + const policy = yield* ThreadTypePolicy.readThreadTypePolicy(policyPath); + return policy === undefined + ? undefined + : yield* ThreadTypePolicy.resolveThreadTypeLaunch( + policy, + input.thread, + input.modelSelection.model, + ); +}); + +export const makeLayerFromProjectStore = ( + threadTypePolicyPath: string, +): Layer.Layer< RuntimePolicyV2, never, | ProjectStore.ProjectStoreV2 | ProviderInstanceRegistry.ProviderInstanceRegistry | ServerSettings.ServerSettingsService -> = Layer.effect( - RuntimePolicyV2, - Effect.gen(function* () { - const projects = yield* ProjectStore.ProjectStoreV2; - const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry; - // The same ceiling the orchestrator enforces, fixed for the server's life. - const { managedPolicy } = yield* ServerSettings.ServerSettingsService; - const maxRuntimeMode = managedPolicy?.limits?.maxRuntimeMode; - return RuntimePolicyV2.of({ - resolve: Effect.fn("RuntimePolicyV2.resolve")(function* (input) { - yield* refuseAboveCeiling(managedPolicy, input.thread); - const instance = yield* providerInstances.getInstance(input.modelSelection.instanceId); - const supportedRuntimeModes = - instance === undefined - ? undefined - : (yield* instance.snapshot.getSnapshot).supportedRuntimeModes; - const cwd = - input.thread.worktreePath ?? - (yield* projects.get(input.thread.projectId).pipe( - Effect.mapError( - (cause) => - new RuntimePolicyResolveError({ - projectId: input.thread.projectId, - providerInstanceId: input.modelSelection.instanceId, - cause, + | FileSystem.FileSystem +> => + Layer.effect( + RuntimePolicyV2, + Effect.gen(function* () { + const projects = yield* ProjectStore.ProjectStoreV2; + const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry; + // The same ceiling the orchestrator enforces, fixed for the server's life. + const { managedPolicy } = yield* ServerSettings.ServerSettingsService; + const maxRuntimeMode = managedPolicy?.limits?.maxRuntimeMode; + const fileSystem = yield* FileSystem.FileSystem; + return RuntimePolicyV2.of({ + resolve: Effect.fn("RuntimePolicyV2.resolve")(function* (input) { + yield* refuseAboveCeiling(managedPolicy, input.thread); + const threadType = yield* applyThreadTypePolicy(threadTypePolicyPath, input).pipe( + Effect.provideService(FileSystem.FileSystem, fileSystem), + ); + const instance = yield* providerInstances.getInstance(input.modelSelection.instanceId); + const supportedRuntimeModes = + instance === undefined + ? undefined + : (yield* instance.snapshot.getSnapshot).supportedRuntimeModes; + const cwd = + input.thread.worktreePath ?? + (yield* projects.get(input.thread.projectId).pipe( + Effect.mapError( + (cause) => + new RuntimePolicyResolveError({ + projectId: input.thread.projectId, + providerInstanceId: input.modelSelection.instanceId, + cause, + }), + ), + Effect.flatMap( + Option.match({ + onNone: () => + Effect.fail( + new RuntimePolicyResolveError({ + projectId: input.thread.projectId, + providerInstanceId: input.modelSelection.instanceId, + cause: "Project not found.", + }), + ), + onSome: (project) => Effect.succeed(project.workspaceRoot), }), - ), - Effect.flatMap( - Option.match({ - onNone: () => - Effect.fail( - new RuntimePolicyResolveError({ - projectId: input.thread.projectId, - providerInstanceId: input.modelSelection.instanceId, - cause: "Project not found.", - }), - ), - onSome: (project) => Effect.succeed(project.workspaceRoot), - }), - ), - )); - return ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ - runtimeMode: providerRuntimeMode(input.thread.runtimeMode, supportedRuntimeModes), - interactionMode: input.thread.interactionMode, - cwd, - ...(maxRuntimeMode === undefined ? {} : { maxRuntimeMode }), - }); - }), - }); - }), + ), + )); + return ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ + runtimeMode: providerRuntimeMode(input.thread.runtimeMode, supportedRuntimeModes), + interactionMode: input.thread.interactionMode, + cwd, + ...(maxRuntimeMode === undefined ? {} : { maxRuntimeMode }), + ...(threadType === undefined ? {} : { threadType }), + }); + }), + }); + }), + ); + +export const layerFromProjectStore = makeLayerFromProjectStore( + ThreadTypePolicy.threadTypePolicyPath(), ); export function layerWithOverride( diff --git a/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts b/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts new file mode 100644 index 0000000000000..f005b9831d1e5 --- /dev/null +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts @@ -0,0 +1,112 @@ +import { assert, describe, it } from "@effect/vitest"; +import * as NodeServices from "@effect/platform-node/NodeServices"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; + +import * as ThreadTypePolicy from "./ThreadTypePolicy.ts"; +import { THREAD_TYPE_POLICY_FIXTURE, threadTypeThread } from "./ThreadTypePolicy.testFixtures.ts"; + +describe("ThreadTypePolicy", () => { + it.each([ + ["CoS", false, "standing"], + ["CTO daily", false, "standing"], + ["Product Designer", false, "standing"], + ["JEFFDESK:DESK — Jeff Omarchy operations desktop", false, "standing"], + ["CoS", true, "coding"], + ["[standing] PR landing manager", true, "standing"], + ["[research] CoS notes", false, "research"], + ["KSS ----> analyses with Kameron", false, "conversational"], + ["Independent code-review of CLO-924", true, "review"], + ["Token burn analysis and plan", false, "research"], + ["OPS-1:ASTRA — R720 CI & Grafana alert delivery", false, "ops"], + ["Standing bot threads look different in T3", false, "coding"], + ] as const)("classifies %s (child %s) as %s", (title, child, expected) => { + assert.equal( + ThreadTypePolicy.classifyThread( + THREAD_TYPE_POLICY_FIXTURE, + threadTypeThread(title, { child }), + ), + expected, + ); + }); + + it("treats a thread with a standing persona as standing whatever its title", () => { + assert.equal( + ThreadTypePolicy.classifyThread( + THREAD_TYPE_POLICY_FIXTURE, + threadTypeThread("[coding] CLO-924 per-thread-type policy", { standing: true }), + ), + "standing", + ); + }); + + it.effect("refuses gpt-6-astra on standing threads in any provider spelling", () => + Effect.gen(function* () { + for (const model of ["gpt-6-astra", "codex/gpt-6-astra", " OpenAI/GPT-6-Astra "]) { + const refused = yield* ThreadTypePolicy.resolveThreadTypeLaunch( + THREAD_TYPE_POLICY_FIXTURE, + threadTypeThread("Lead"), + model, + ).pipe(Effect.flip); + assert.equal(refused._tag, "ThreadTypeModelRefusedError"); + assert.equal(refused.threadType, "standing"); + assert.equal(refused.model, "gpt-6-astra"); + } + }), + ); + + it.effect("allows gpt-6-astra on named short task threads and returns the type values", () => + Effect.gen(function* () { + const review = yield* ThreadTypePolicy.resolveThreadTypeLaunch( + THREAD_TYPE_POLICY_FIXTURE, + threadTypeThread("Review PR 953"), + "gpt-6-astra", + ); + assert.deepEqual(review, { + type: "review", + contextWindow: 272_000, + compactAt: 120_000, + cacheTtl: "5m", + }); + const standing = yield* ThreadTypePolicy.resolveThreadTypeLaunch( + THREAD_TYPE_POLICY_FIXTURE, + threadTypeThread("CoS"), + "gpt-6.1-sol", + ); + assert.deepEqual(standing, { + type: "standing", + contextWindow: 272_000, + compactAt: 100_000, + cacheTtl: "1h", + }); + }), + ); + + it.effect("reads a policy file and treats a missing or invalid one as no policy", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const dir = yield* fileSystem.makeTempDirectoryScoped(); + const valid = `${dir}/policy.json`; + const invalid = `${dir}/invalid.json`; + yield* fileSystem.writeFileString(valid, JSON.stringify(THREAD_TYPE_POLICY_FIXTURE)); + yield* fileSystem.writeFileString(invalid, JSON.stringify({ schema: "other" })); + assert.deepEqual( + yield* ThreadTypePolicy.readThreadTypePolicy(valid), + THREAD_TYPE_POLICY_FIXTURE, + ); + assert.isUndefined(yield* ThreadTypePolicy.readThreadTypePolicy(invalid)); + assert.isUndefined(yield* ThreadTypePolicy.readThreadTypePolicy(`${dir}/missing.json`)); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), + ); + + it("reads the policy from the Closura config path unless the environment overrides it", () => { + assert.match( + ThreadTypePolicy.threadTypePolicyPath({}), + /\/\.config\/closura\/thread-type-policy\.json$/, + ); + assert.equal( + ThreadTypePolicy.threadTypePolicyPath({ T3_THREAD_TYPE_POLICY_PATH: "/etc/policy.json" }), + "/etc/policy.json", + ); + }); +}); diff --git a/apps/server/src/orchestration-v2/ThreadTypePolicy.testFixtures.ts b/apps/server/src/orchestration-v2/ThreadTypePolicy.testFixtures.ts new file mode 100644 index 0000000000000..4d98ac1521c2c --- /dev/null +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.testFixtures.ts @@ -0,0 +1,67 @@ +import { type OrchestrationV2AppThread, ThreadId } from "@t3tools/contracts"; + +import type { ThreadTypePolicyFile } from "./ThreadTypePolicy.ts"; + +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, + default_type: "coding", + classify: [ + { + type: "standing", + top_level_only: true, + title: + "^\\s*(cos|ceo|cto|cfo|coo|chief of staff|meta[- ]?thread|lead|eng lead|jeff lead|builder|arc ops|product designer|grok ?bot|pool dispatcher|dispatcher)\\b|\\bstanding (role|session|lane|thread)\\b|\\bdesk\\b", + }, + { + type: "conversational", + top_level_only: true, + title: "\\bwith kameron\\b|\\bworking session\\b|\\bbrainstorm|\\bkss\\b", + }, + { + type: "review", + top_level_only: false, + title: "\\b(review|reviewer|re-?review|code-review|second opinion|audit)\\b", + }, + { + type: "research", + top_level_only: false, + title: + "\\b(research|analysis|analy[sz]e|investigat\\w*|survey|eval|evals|benchmark|forensic|root cause)\\b", + }, + { + type: "ops", + top_level_only: false, + title: + "^\\s*ops\\b|\\b(runner|runners|fleet|failover|tailnet|backups?|alerts?|grafana|ssh)\\b", + }, + ], + types: { + standing: { compact_at: 100_000, cache_ttl: "1h", refused_models: ["gpt-6-astra"] }, + 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: [] }, + ops: { compact_at: 168_000, cache_ttl: "5m", refused_models: [] }, + conversational: { compact_at: 168_000, cache_ttl: "5m", refused_models: [] }, + }, +}; + +const threadId = ThreadId.make("thread:thread-type-policy"); + +export const threadTypeThread = ( + title: string, + options: { readonly child?: boolean; readonly standing?: boolean } = {}, +): Pick => ({ + id: threadId, + title, + lineage: { + parentThreadId: options.child === true ? ThreadId.make("thread:parent") : null, + relationshipToParent: options.child === true ? ("subagent" as const) : null, + rootThreadId: threadId, + }, + ...(options.standing === true + ? { standing: { name: "CoS", role: "Chief of staff", avatarId: "owl", accent: "blue" } } + : {}), +}); diff --git a/apps/server/src/orchestration-v2/ThreadTypePolicy.ts b/apps/server/src/orchestration-v2/ThreadTypePolicy.ts new file mode 100644 index 0000000000000..1ba875b6acef1 --- /dev/null +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.ts @@ -0,0 +1,152 @@ +import * as NodeOs from "node:os"; + +import { isDelegatedAppThread, OrchestrationV2AppThread, ThreadId } from "@t3tools/contracts"; +import * as Effect from "effect/Effect"; +import * as FileSystem from "effect/FileSystem"; +import * as Schema from "effect/Schema"; + +export const THREAD_TYPE_POLICY_PATH_ENV = "T3_THREAD_TYPE_POLICY_PATH"; +export const THREAD_TYPES = [ + "standing", + "coding", + "review", + "research", + "ops", + "conversational", +] as const; +export type ThreadType = (typeof THREAD_TYPES)[number]; + +const ThreadTypeName = Schema.Literals(THREAD_TYPES); +const CacheTtl = Schema.Literals(["5m", "1h"]); +const TokenCount = Schema.Int.check(Schema.isGreaterThan(0)); + +const ThreadTypeEntry = Schema.Struct({ + compact_at: TokenCount, + cache_ttl: CacheTtl, + refused_models: Schema.Array(Schema.String), +}); + +export const ThreadTypePolicyFile = Schema.Struct({ + schema: Schema.Literal("thread-type-policy-v1"), + source: Schema.String, + decision: Schema.String, + context_window: TokenCount, + default_type: ThreadTypeName, + classify: Schema.Array( + Schema.Struct({ type: ThreadTypeName, top_level_only: Schema.Boolean, title: Schema.String }), + ), + types: Schema.Struct({ + standing: ThreadTypeEntry, + coding: ThreadTypeEntry, + review: ThreadTypeEntry, + research: ThreadTypeEntry, + ops: ThreadTypeEntry, + conversational: ThreadTypeEntry, + }), +}); +export type ThreadTypePolicyFile = typeof ThreadTypePolicyFile.Type; + +export interface ThreadTypeLaunch { + readonly type: ThreadType; + readonly contextWindow: number; + readonly compactAt: number; + readonly cacheTtl: "5m" | "1h"; +} + +export class ThreadTypeModelRefusedError extends Schema.TaggedError()( + "ThreadTypeModelRefusedError", + { + threadId: ThreadId, + threadType: Schema.String, + model: Schema.String, + }, +) { + override get message(): string { + return `${this.model} is refused on ${this.threadType} threads by the Closura thread type policy. Pick another model to continue.`; + } +} + +export const isThreadTypeModelRefusedError = Schema.is(ThreadTypeModelRefusedError); + +const MODEL_PREFIXES = ["codex/", "claude/", "openai/", "anthropic/", "magpie-go/"]; +const TITLE_TAG = /^\s*\[([a-z]+)\]/; + +export function normalizeModel(model: string): string { + let value = model.trim().toLowerCase(); + for (const prefix of MODEL_PREFIXES) { + if (value.startsWith(prefix)) value = value.slice(prefix.length); + } + return value.replaceAll("[1m]", ""); +} + +const isThreadType = (value: string): value is ThreadType => + (THREAD_TYPES as ReadonlyArray).includes(value); + +export function classifyThread( + policy: ThreadTypePolicyFile, + thread: Pick, +): ThreadType { + if (thread.standing != null) return "standing"; + const title = thread.title ?? ""; + const tag = TITLE_TAG.exec(title)?.[1]; + if (tag !== undefined && isThreadType(tag)) return tag; + const child = isDelegatedAppThread(thread); + for (const rule of policy.classify) { + if (rule.top_level_only && child) continue; + if (new RegExp(rule.title, "i").test(title)) return rule.type; + } + return policy.default_type; +} + +export function resolveThreadTypeLaunch( + policy: ThreadTypePolicyFile, + thread: Pick, + model: string, +): Effect.Effect { + const type = classifyThread(policy, thread); + const entry = policy.types[type]; + const bare = normalizeModel(model); + if (entry.refused_models.map(normalizeModel).includes(bare)) { + return Effect.fail( + new ThreadTypeModelRefusedError({ threadId: thread.id, threadType: type, model: bare }), + ); + } + return Effect.succeed({ + type, + contextWindow: policy.context_window, + compactAt: entry.compact_at, + cacheTtl: entry.cache_ttl, + }); +} + +export function threadTypePolicyPath(environment: NodeJS.ProcessEnv = process.env): string { + const override = environment[THREAD_TYPE_POLICY_PATH_ENV]; + return override !== undefined && override.length > 0 + ? override + : `${NodeOs.homedir()}/.config/closura/thread-type-policy.json`; +} + +const decodePolicy = Schema.decodeUnknownEffect(Schema.fromJsonString(ThreadTypePolicyFile)); + +export const readThreadTypePolicy = Effect.fn("ThreadTypePolicy.read")(function* (path: string) { + const fileSystem = yield* FileSystem.FileSystem; + const text = yield* fileSystem.readFileString(path).pipe( + Effect.map((value): string | undefined => value), + Effect.catch((error) => + error.reason._tag === "NotFound" + ? Effect.undefined + : Effect.logWarning("Could not read the thread type policy", { path, error }).pipe( + Effect.as(undefined), + ), + ), + ); + if (text === undefined) return undefined; + return yield* decodePolicy(text).pipe( + Effect.map((value): ThreadTypePolicyFile | undefined => value), + Effect.catch((error) => + Effect.logWarning("Ignoring an invalid thread type policy", { path, error }).pipe( + Effect.as(undefined), + ), + ), + ); +}); diff --git a/packages/provider-core/src/server/ProviderAdapter.ts b/packages/provider-core/src/server/ProviderAdapter.ts index ff879ef0f723f..061e79815d725 100644 --- a/packages/provider-core/src/server/ProviderAdapter.ts +++ b/packages/provider-core/src/server/ProviderAdapter.ts @@ -56,6 +56,14 @@ export const ProviderAdapterV2RuntimePolicy = Schema.Struct({ * it; adapters use it to refuse native mode choices a user makes beside T3 Code's own. */ maxRuntimeMode: Schema.optional(RuntimeMode), + threadType: Schema.optional( + Schema.Struct({ + type: Schema.String, + contextWindow: Schema.Number, + compactAt: Schema.Number, + cacheTtl: Schema.Literals(["5m", "1h"]), + }), + ), }); export type ProviderAdapterV2RuntimePolicy = typeof ProviderAdapterV2RuntimePolicy.Type; From a889e4882fbef821134809ce443c0b5c7f3113a1 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Fri, 20 Nov 2026 03:58:33 +0000 Subject: [PATCH 2/3] Repair thread-type refusal and Claude compaction Claude's percent override now targets the effective window, the model's context minus the 20k output reserve, so the compaction point matches the table. The cache TTL is set on the env var Claude ranks above the setting. A disabled auto-compact still reports no threshold. Codex receives only the compaction limit. Refusal covers stacked prefixes, openai. slugs, a trailing -fast, and [1m]. A title pattern JavaScript cannot compile fails open with one warning, and a missing policy warns once. Dispatch and rollback surface the refusal message. Model: grok/grok-4.7 Task: CLO-924 Agent: grok Lane: magpie Harness: t3/codex Model-family: Grok Model-generation: 4.7 Skills-SHA: e6cf8938; skills/github-repository-ops/SKILL.md, docs/delivery/provenance-trailers.md Co-Authored-By: Grok 4.7 --- .../Adapters/ClaudeAdapterV2.test.ts | 14 ++- .../Adapters/ClaudeAdapterV2.ts | 89 ++++++++++++++----- .../Adapters/CodexAdapterV2.test.ts | 4 +- .../Adapters/CodexAdapterV2.ts | 1 - .../CheckpointRollbackService.ts | 2 + .../orchestration-v2/RuntimePolicy.test.ts | 17 +++- .../orchestration-v2/ThreadTypePolicy.test.ts | 21 ++++- .../src/orchestration-v2/ThreadTypePolicy.ts | 60 ++++++++++--- .../orchestration-v2/UserFacingErrors.test.ts | 14 +++ 9 files changed, 183 insertions(+), 39 deletions(-) diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index 095fe8fec73eb..8a1de0342e94c 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -495,6 +495,7 @@ describe("ClaudeAdapterV2 runtime query policy", () => { 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 { @@ -505,7 +506,16 @@ describe("ClaudeAdapterV2 runtime query policy", () => { 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, "36.76"); + 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", () => { @@ -517,7 +527,7 @@ describe("ClaudeAdapterV2 runtime query policy", () => { undefined, 120_000, ); - assert.equal(usage.autoCompactThreshold, 120_000); + assert.equal(usage.autoCompactThreshold, 120_002); 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 d44b9b5eaba92..4e1e2aec8eb49 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -278,28 +278,55 @@ function isClaudeEnvTruthy(value: string | undefined): boolean { return value !== undefined && ["1", "true", "yes", "on"].includes(value.trim().toLowerCase()); } +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) - ? { - autoCompactThreshold: Math.min( - windowTokens - CLAUDE_AUTO_COMPACT_RESERVE_TOKENS, - compactAt ?? Number.POSITIVE_INFINITY, - ), - } + 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 } : {}; } -export function claudeAutoCompactPercent( - threadType: NonNullable, -): string { - return String(Math.round((threadType.compactAt * 10_000) / threadType.contextWindow) / 100); -} - function claudeMessageInputTokens(usage: { readonly input_tokens: number; readonly cache_creation_input_tokens?: number | null; @@ -2257,6 +2284,10 @@ export function makeClaudeQueryOptions(input: { ? ({ ...input.sdkSettings, ...selectionSettings } as ClaudeSdkSettings) : selectionSettings; const threadType = input.threadType; + const modelMaxTokens = + input.modelCatalog === undefined + ? null + : (resolveClaudeCatalogContextWindowTokens(input.modelCatalog, input.modelSelection) ?? null); const autoCompactWindow = threadType !== undefined ? String(threadType.contextWindow) @@ -2299,7 +2330,13 @@ export function makeClaudeQueryOptions(input: { : { CLAUDE_CODE_AUTO_COMPACT_WINDOW: autoCompactWindow }), ...(threadType === undefined ? {} - : { CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: claudeAutoCompactPercent(threadType) }), + : { + CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: claudeAutoCompactPercent( + threadType, + modelMaxTokens, + ), + CLAUDE_CODE_PROMPT_CACHE_TTL: threadType.cacheTtl, + }), ...(input.toolSearchOverride === undefined ? {} : { ENABLE_TOOL_SEARCH: input.toolSearchOverride }), @@ -2353,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, @@ -13147,13 +13193,14 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( ); const querySession = yield* openQuery(turnInput, nativeThreadId, queryPolicy); yield* applySessionTitle(querySession.query, nativeThreadId, turnInput.appThread.title); + const resolvedAutoCompactWindow = yield* resolveAutoCompactWindow( + querySession.settingsCwd, + turnInput.modelSelection, + querySession.launchArgSettings, + ); const autoCompactWindow = - turnInput.runtimePolicy.threadType === undefined - ? yield* resolveAutoCompactWindow( - querySession.settingsCwd, - turnInput.modelSelection, - querySession.launchArgSettings, - ) + turnInput.runtimePolicy.threadType === undefined || resolvedAutoCompactWindow === "" + ? resolvedAutoCompactWindow : String(turnInput.runtimePolicy.threadType.contextWindow); const context: ActiveClaudeTurnContext = { input: turnInput, diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 95eb2f4b0c8f2..b424d2db66123 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts @@ -91,7 +91,7 @@ const isProviderAdapterEventStreamError = Schema.is( const encodeUnknownJson = Schema.encodeUnknownSync(Schema.fromJsonString(Schema.Unknown)); describe("CodexAdapterV2 thread type policy", () => { - it("sets the thread type compaction limit and the context cap on thread start", () => { + it("sets the thread type compaction limit without raising the context window", () => { const params = CodexAdapterV2.codexThreadRuntimeParams({ threadId: null, mcpSession: undefined, @@ -103,7 +103,7 @@ describe("CodexAdapterV2 thread type policy", () => { }, }); assert.equal(params.config.model_auto_compact_token_limit, 120_000); - assert.equal(params.config.model_context_window, 272_000); + assert.isUndefined(params.config.model_context_window); }); 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 984bfd127ce77..1d9af396b5b48 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -1509,7 +1509,6 @@ export function codexThreadTypeConfig( return threadType === undefined ? {} : { - model_context_window: threadType.contextWindow, model_auto_compact_token_limit: threadType.compactAt, }; } diff --git a/apps/server/src/orchestration-v2/CheckpointRollbackService.ts b/apps/server/src/orchestration-v2/CheckpointRollbackService.ts index 8924a3eb9a78b..c058b557090c8 100644 --- a/apps/server/src/orchestration-v2/CheckpointRollbackService.ts +++ b/apps/server/src/orchestration-v2/CheckpointRollbackService.ts @@ -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 = @@ -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 diff --git a/apps/server/src/orchestration-v2/RuntimePolicy.test.ts b/apps/server/src/orchestration-v2/RuntimePolicy.test.ts index b73d38db1142e..bbd8df7940f39 100644 --- a/apps/server/src/orchestration-v2/RuntimePolicy.test.ts +++ b/apps/server/src/orchestration-v2/RuntimePolicy.test.ts @@ -197,11 +197,16 @@ it.effect("applies the installed thread type policy when a turn starts", () => }); }).pipe(Effect.provide(layerWithPolicy(undefined, path))); + for (const model of ["gpt-6-astra", "openai.gpt-6-astra", "openai/gpt-6-astra-fast", "openai/codex/gpt-6-astra"]) { + const spelled = yield* resolve("Lead", model).pipe(Effect.flip); + assert.equal(spelled._tag, "ThreadTypeModelRefusedError"); + assert.equal(spelled.model, "gpt-6-astra"); + } const refused = yield* resolve("Lead", "gpt-6-astra").pipe(Effect.flip); assert.equal(refused._tag, "ThreadTypeModelRefusedError"); assert.equal( refused.message, - "gpt-6-astra is refused on standing threads by the Closura thread type policy. Pick another model to continue.", + "gpt-6-astra is refused on standing threads by the Closura thread type policy. Pick another model, or tag the title with a type that allows it, such as [coding].", ); const standing = yield* resolve("Lead", "gpt-6.1-sol"); @@ -223,6 +228,16 @@ it.effect("applies the installed thread type policy when a turn starts", () => const coding = yield* resolve("CLO-924 per-thread-type policy", "gpt-6-astra"); assert.equal(coding.threadType?.type, "coding"); assert.equal(coding.threadType?.compactAt, 168_000); + const research = yield* resolve("Token burn analysis and plan", "gpt-6.1-sol"); + assert.equal(research.threadType?.type, "research"); + assert.equal(research.threadType?.compactAt, 168_000); + assert.equal(research.threadType?.cacheTtl, "1h"); + const ops = yield* resolve("OPS-1:ASTRA — R720 CI & Grafana alert delivery", "gpt-6.1-sol"); + assert.equal(ops.threadType?.type, "ops"); + assert.equal(ops.threadType?.cacheTtl, "5m"); + const conversational = yield* resolve("KSS ----> analyses with Kameron", "gpt-6.1-sol"); + assert.equal(conversational.threadType?.type, "conversational"); + assert.equal(conversational.threadType?.compactAt, 168_000); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); diff --git a/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts b/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts index f005b9831d1e5..1fe730d2c3ba0 100644 --- a/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts @@ -42,7 +42,17 @@ describe("ThreadTypePolicy", () => { it.effect("refuses gpt-6-astra on standing threads in any provider spelling", () => Effect.gen(function* () { - for (const model of ["gpt-6-astra", "codex/gpt-6-astra", " OpenAI/GPT-6-Astra "]) { + for (const model of [ + "gpt-6-astra", + "codex/gpt-6-astra", + " OpenAI/GPT-6-Astra ", + "openai.gpt-6-astra", + "openai/gpt-6-astra-fast", + "openrouter/openai/gpt-6-astra", + "magpie/gpt-6-astra", + "openai/codex/gpt-6-astra", + "gpt-6-astra[1m]", + ]) { const refused = yield* ThreadTypePolicy.resolveThreadTypeLaunch( THREAD_TYPE_POLICY_FIXTURE, threadTypeThread("Lead"), @@ -96,6 +106,15 @@ describe("ThreadTypePolicy", () => { ); assert.isUndefined(yield* ThreadTypePolicy.readThreadTypePolicy(invalid)); assert.isUndefined(yield* ThreadTypePolicy.readThreadTypePolicy(`${dir}/missing.json`)); + const possessive = `${dir}/possessive.json`; + yield* fileSystem.writeFileString( + possessive, + JSON.stringify({ + ...THREAD_TYPE_POLICY_FIXTURE, + classify: [{ type: "standing", top_level_only: true, title: "a*+" }], + }), + ); + assert.isUndefined(yield* ThreadTypePolicy.readThreadTypePolicy(possessive)); }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); diff --git a/apps/server/src/orchestration-v2/ThreadTypePolicy.ts b/apps/server/src/orchestration-v2/ThreadTypePolicy.ts index 1ba875b6acef1..bb3560002629a 100644 --- a/apps/server/src/orchestration-v2/ThreadTypePolicy.ts +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.ts @@ -62,21 +62,22 @@ export class ThreadTypeModelRefusedError extends Schema.TaggedError= 0) value = value.slice(slash + 1); + if (value.startsWith("openai.")) value = value.slice("openai.".length); + value = value.replaceAll("[1m]", ""); + if (value.endsWith("-fast")) value = value.slice(0, -"-fast".length); + return value; } const isThreadType = (value: string): value is ThreadType => @@ -93,7 +94,11 @@ export function classifyThread( const child = isDelegatedAppThread(thread); for (const rule of policy.classify) { if (rule.top_level_only && child) continue; - if (new RegExp(rule.title, "i").test(title)) return rule.type; + try { + if (new RegExp(rule.title, "i").test(title)) return rule.type; + } catch { + continue; + } } return policy.default_type; } @@ -127,6 +132,25 @@ export function threadTypePolicyPath(environment: NodeJS.ProcessEnv = process.en } const decodePolicy = Schema.decodeUnknownEffect(Schema.fromJsonString(ThreadTypePolicyFile)); +const policyWarningKeys = new Set(); + +const warnPolicyOnce = (path: string, state: string, message: string, detail?: unknown) => { + const key = `${state}|${path}`; + if (policyWarningKeys.has(key)) return Effect.void; + policyWarningKeys.add(key); + return Effect.logWarning(message, detail === undefined ? { path, state } : { path, state, detail }); +}; + +const classifyRulesCompile = (policy: ThreadTypePolicyFile): boolean => { + for (const rule of policy.classify) { + try { + new RegExp(rule.title, "i"); + } catch { + return false; + } + } + return true; +}; export const readThreadTypePolicy = Effect.fn("ThreadTypePolicy.read")(function* (path: string) { const fileSystem = yield* FileSystem.FileSystem; @@ -134,19 +158,33 @@ export const readThreadTypePolicy = Effect.fn("ThreadTypePolicy.read")(function* Effect.map((value): string | undefined => value), Effect.catch((error) => error.reason._tag === "NotFound" - ? Effect.undefined - : Effect.logWarning("Could not read the thread type policy", { path, error }).pipe( + ? warnPolicyOnce( + path, + "missing", + "No thread type policy is installed; turns keep their current behavior", + ).pipe(Effect.as(undefined)) + : warnPolicyOnce(path, "unreadable", "Could not read the thread type policy", error).pipe( Effect.as(undefined), ), ), ); if (text === undefined) return undefined; - return yield* decodePolicy(text).pipe( + const decoded = yield* decodePolicy(text).pipe( Effect.map((value): ThreadTypePolicyFile | undefined => value), Effect.catch((error) => - Effect.logWarning("Ignoring an invalid thread type policy", { path, error }).pipe( + warnPolicyOnce(path, "invalid", "Ignoring an invalid thread type policy", error).pipe( Effect.as(undefined), ), ), ); + if (decoded === undefined) return undefined; + if (!classifyRulesCompile(decoded)) { + yield* warnPolicyOnce( + path, + "uncompilable", + "Ignoring a thread type policy whose title pattern JavaScript cannot compile", + ); + return undefined; + } + return decoded; }); diff --git a/apps/server/src/orchestration-v2/UserFacingErrors.test.ts b/apps/server/src/orchestration-v2/UserFacingErrors.test.ts index 1d69d1c88a3ae..fe69decd6abc2 100644 --- a/apps/server/src/orchestration-v2/UserFacingErrors.test.ts +++ b/apps/server/src/orchestration-v2/UserFacingErrors.test.ts @@ -80,4 +80,18 @@ describe("userFacingDispatchErrorMessage", () => { assert.include(message, "server logs"); assert.notInclude(message, "private-query-data"); }); + + it("shows the thread type refusal when dispatch wraps it", () => { + const message = userFacingDispatchErrorMessage({ + message: "Failed to dispatch orchestration command message.dispatch (command:clo-924).", + cause: { + message: + "gpt-6-astra is refused on standing threads by the Closura thread type policy. Pick another model, or tag the title with a type that allows it, such as [coding].", + }, + }); + assert.equal( + message, + "gpt-6-astra is refused on standing threads by the Closura thread type policy. Pick another model, or tag the title with a type that allows it, such as [coding].", + ); + }); }); From 9a2413c66e1ce9e8b23085ca42fc1cf6e094c3c1 Mon Sep 17 00:00:00 2001 From: nullStack65 <112618179+nullStack65@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:05:03 +0000 Subject: [PATCH 3/3] fix(server): narrow the thread type refusal before reading its model The refusal test read model off the whole resolve error union, so the server typecheck exited 2 and the Linux ARC check failed. Co-Authored-By: Claude Fable 5.2 --- apps/server/src/orchestration-v2/RuntimePolicy.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/server/src/orchestration-v2/RuntimePolicy.test.ts b/apps/server/src/orchestration-v2/RuntimePolicy.test.ts index bbd8df7940f39..e1b90d64de5b6 100644 --- a/apps/server/src/orchestration-v2/RuntimePolicy.test.ts +++ b/apps/server/src/orchestration-v2/RuntimePolicy.test.ts @@ -200,6 +200,7 @@ it.effect("applies the installed thread type policy when a turn starts", () => for (const model of ["gpt-6-astra", "openai.gpt-6-astra", "openai/gpt-6-astra-fast", "openai/codex/gpt-6-astra"]) { const spelled = yield* resolve("Lead", model).pipe(Effect.flip); assert.equal(spelled._tag, "ThreadTypeModelRefusedError"); + if (spelled._tag !== "ThreadTypeModelRefusedError") continue; assert.equal(spelled.model, "gpt-6-astra"); } const refused = yield* resolve("Lead", "gpt-6-astra").pipe(Effect.flip);