diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts index c07ab96746eb5..8a1de0342e94c 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts @@ -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; + }; + 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" }, diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 25d9d616cb939..4e1e2aec8eb49 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -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 } : {}; } @@ -309,6 +351,7 @@ export function claudeProviderTurnTokenUsage( updatedAt: string, autoCompactWindow = "", requestedAt?: DateTime.Utc, + compactAt?: number, ) { const inputTokens = claudeMessageInputTokens(usage); const outputTokens = usage.output_tokens; @@ -326,7 +369,7 @@ export function claudeProviderTurnTokenUsage( outputTokens, reasoningOutputTokens: 0, ...(promptCacheTtlMs === undefined ? {} : { promptCacheTtlMs }), - ...claudeAutoCompactThreshold(autoCompactWindow, maxTokens), + ...claudeAutoCompactThreshold(autoCompactWindow, maxTokens, compactAt), ...(promptCacheExpiresAt === undefined ? {} : { promptCacheExpiresAt }), updatedAt, }; @@ -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 { @@ -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. @@ -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 }), @@ -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, @@ -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( @@ -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; @@ -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 ?? ".") @@ -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, diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts index 019dcf4ec0edb..b424d2db66123 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 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( diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts index d1ddd569e60d5..1d9af396b5b48 100644 --- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts @@ -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> { + 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 }; @@ -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 ? {} 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/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/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..e1b90d64de5b6 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,81 @@ 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))); + + 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); + 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, or tag the title with a type that allows it, such as [coding].", + ); + + 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); + 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)), +); + 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..1fe730d2c3ba0 --- /dev/null +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.test.ts @@ -0,0 +1,131 @@ +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 ", + "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"), + 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`)); + 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)), + ); + + 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..bb3560002629a --- /dev/null +++ b/apps/server/src/orchestration-v2/ThreadTypePolicy.ts @@ -0,0 +1,190 @@ +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, or tag the title with a type that allows it, such as [coding].`; + } +} + +export const isThreadTypeModelRefusedError = Schema.is(ThreadTypeModelRefusedError); + +const TITLE_TAG = /^\s*\[([a-z]+)\]/; + +export function normalizeModel(model: string): string { + let value = model.trim().toLowerCase(); + const slash = value.lastIndexOf("/"); + if (slash >= 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 => + (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; + try { + if (new RegExp(rule.title, "i").test(title)) return rule.type; + } catch { + continue; + } + } + 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)); +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; + const text = yield* fileSystem.readFileString(path).pipe( + Effect.map((value): string | undefined => value), + Effect.catch((error) => + error.reason._tag === "NotFound" + ? 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; + const decoded = yield* decodePolicy(text).pipe( + Effect.map((value): ThreadTypePolicyFile | undefined => value), + Effect.catch((error) => + 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].", + ); + }); }); 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;