Skip to content

Commit 7ad8fdf

Browse files
t3dotggclaude
andcommitted
fix(server): delegated Claude children follow subagentPromptCacheTtl
Keep Anthropic's default when the setting is unset. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 62a7ac2 commit 7ad8fdf

2 files changed

Lines changed: 59 additions & 18 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ describe("ClaudeAdapterV2 runtime query policy", () => {
249249
assert.isUndefined(options.extraArgs?.["dangerously-skip-permissions"]);
250250
});
251251

252-
it("gives delegated children a 5m prompt cache unless the instance sets a TTL", () => {
252+
it("gives delegated children Claude's subagent prompt cache TTL", () => {
253253
const lineage = (relationshipToParent: "fork" | "subagent" | null) => ({
254254
lineage: {
255255
parentThreadId: null,
@@ -258,18 +258,16 @@ describe("ClaudeAdapterV2 runtime query policy", () => {
258258
},
259259
});
260260
const env = { PATH: "/bin" };
261-
assert.equal(
262-
ClaudeAdapterV2.claudeQueryEnvironment(env, lineage("subagent")).CLAUDE_CODE_PROMPT_CACHE_TTL,
263-
"5m",
264-
);
265-
assert.strictEqual(ClaudeAdapterV2.claudeQueryEnvironment(env, lineage(null)), env);
266-
assert.strictEqual(ClaudeAdapterV2.claudeQueryEnvironment(env, lineage("fork")), env);
267-
assert.equal(
268-
ClaudeAdapterV2.claudeQueryEnvironment(
269-
{ ...env, CLAUDE_CODE_PROMPT_CACHE_TTL: "1h" },
270-
lineage("subagent"),
271-
).CLAUDE_CODE_PROMPT_CACHE_TTL,
272-
"1h",
261+
const ttl = (environment: NodeJS.ProcessEnv, ...args: Parameters<typeof lineage>) =>
262+
ClaudeAdapterV2.claudeQueryEnvironment(environment, lineage(...args), "5m")
263+
.CLAUDE_CODE_PROMPT_CACHE_TTL;
264+
assert.equal(ttl(env, "subagent"), "5m");
265+
assert.isUndefined(ttl(env, null));
266+
assert.isUndefined(ttl(env, "fork"));
267+
assert.equal(ttl({ ...env, CLAUDE_CODE_PROMPT_CACHE_TTL: "1h" }, "subagent"), "1h");
268+
assert.strictEqual(
269+
ClaudeAdapterV2.claudeQueryEnvironment(env, lineage("subagent"), undefined),
270+
env,
273271
);
274272
});
275273

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,20 +1244,22 @@ function claudeLiveSelectionFlagSettings(
12441244
};
12451245
}
12461246

1247-
// Claude Code gives SDK sessions a 1h prompt cache on subscriptions, but a delegated child runs a
1248-
// tight tool loop like a native subagent, which gets 5m. A 1h write costs 2x base input and a 5m
1249-
// write 1.25x, so children use 5m unless the provider instance sets its own TTL.
1247+
// A delegated child is its own SDK session, so Claude Code gives it the main-conversation cache
1248+
// TTL. When the user sets Claude's subagentPromptCacheTtl, the child follows it like a native
1249+
// subagent would. An explicit CLAUDE_CODE_PROMPT_CACHE_TTL on the instance still wins.
12501250
export function claudeQueryEnvironment(
12511251
environment: NodeJS.ProcessEnv,
12521252
appThread: Pick<OrchestrationV2AppThread, "lineage">,
1253+
subagentPromptCacheTtl: "5m" | "1h" | undefined,
12531254
): NodeJS.ProcessEnv {
12541255
if (
12551256
appThread.lineage.relationshipToParent !== "subagent" ||
1257+
subagentPromptCacheTtl === undefined ||
12561258
environment.CLAUDE_CODE_PROMPT_CACHE_TTL !== undefined
12571259
) {
12581260
return environment;
12591261
}
1260-
return { ...environment, CLAUDE_CODE_PROMPT_CACHE_TTL: "5m" };
1262+
return { ...environment, CLAUDE_CODE_PROMPT_CACHE_TTL: subagentPromptCacheTtl };
12611263
}
12621264

12631265
export function makeClaudeQueryOptions(input: {
@@ -2144,6 +2146,17 @@ const decodeClaudeBypassSettings = Schema.decodeUnknownEffect(
21442146
fromLenientJson(ClaudeBypassSettings),
21452147
);
21462148

2149+
const ClaudePromptCacheTtl = Schema.Literals(["5m", "1h"]);
2150+
const decodeClaudePromptCacheTtl = Schema.decodeUnknownOption(ClaudePromptCacheTtl);
2151+
const decodeClaudeSubagentCacheSettings = Schema.decodeUnknownEffect(
2152+
fromLenientJson(
2153+
Schema.StructWithRest(
2154+
Schema.Struct({ subagentPromptCacheTtl: Schema.optionalKey(ClaudePromptCacheTtl) }),
2155+
[Schema.Record(Schema.String, Schema.Unknown)],
2156+
),
2157+
),
2158+
);
2159+
21472160
function claudeBypassAvailability(
21482161
settings: typeof ClaudeBypassSettings.Type,
21492162
): ClaudeBypassAvailability {
@@ -3878,6 +3891,30 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
38783891
},
38793892
);
38803893

3894+
// Claude Code's own subagent TTL setting, in its precedence order. Unset or unreadable means
3895+
// Anthropic's default applies.
3896+
const resolveSubagentPromptCacheTtl = Effect.fn("ClaudeAdapterV2.resolveSubagentPromptCacheTtl")(
3897+
function* (cwd: string | null) {
3898+
const fromEnvironment = decodeClaudePromptCacheTtl(
3899+
adapterOptions.environment.CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL,
3900+
);
3901+
if (Option.isSome(fromEnvironment)) return fromEnvironment.value;
3902+
const resolved = yield* Effect.tryPromise(() =>
3903+
resolveClaudeSettings({
3904+
...(cwd === null ? {} : { cwd }),
3905+
settingSources: customHome ? ["project", "local"] : ["user", "project", "local"],
3906+
}),
3907+
).pipe(Effect.orElseSucceed(() => undefined));
3908+
const home = customHome
3909+
? yield* fileSystem.readFileString(path.join(homePath, "settings.json")).pipe(
3910+
Effect.flatMap(decodeClaudeSubagentCacheSettings),
3911+
Effect.orElseSucceed(() => ({ subagentPromptCacheTtl: undefined })),
3912+
)
3913+
: undefined;
3914+
return resolved?.effective.subagentPromptCacheTtl ?? home?.subagentPromptCacheTtl;
3915+
},
3916+
);
3917+
38813918
const userInvocableSkills = (cwd: string | null) =>
38823919
discoverClaudeSkills(
38833920
adapterOptions.settings,
@@ -9170,7 +9207,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
91709207
cwd: turnInput.runtimePolicy.cwd,
91719208
attachmentsDir,
91729209
settings: adapterOptions.settings,
9173-
environment: claudeQueryEnvironment(adapterOptions.environment, turnInput.appThread),
9210+
environment: claudeQueryEnvironment(
9211+
adapterOptions.environment,
9212+
turnInput.appThread,
9213+
turnInput.appThread.lineage.relationshipToParent === "subagent"
9214+
? yield* resolveSubagentPromptCacheTtl(turnInput.runtimePolicy.cwd)
9215+
: undefined,
9216+
),
91749217
tools: queryPolicy.tools ?? CLAUDE_CODE_PRESET_TOOLS,
91759218
...(mcpOverrides.allowedTools === undefined
91769219
? {}

0 commit comments

Comments
 (0)