Skip to content

Commit 066c8fc

Browse files
t3dotggclaude
andauthored
fix(server): delegated Claude children follow your subagent cache TTL setting (#22841)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 96f09c6 commit 066c8fc

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

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

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

252+
it("gives delegated children Claude's subagent prompt cache TTL", () => {
253+
const lineage = (relationshipToParent: "fork" | "subagent" | null) => ({
254+
lineage: {
255+
parentThreadId: null,
256+
relationshipToParent,
257+
rootThreadId: ThreadId.make("thread-root"),
258+
},
259+
});
260+
const env = { PATH: "/bin" };
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,
271+
);
272+
});
273+
252274
it("projects AskUserQuestion input with question text as the answer key", () => {
253275
const preview = "\n **Proposed change**\n\n```ts\n\tconst enabled = true;\n```\n ";
254276
assert.deepEqual(

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,24 @@ function claudeLiveSelectionFlagSettings(
12441244
};
12451245
}
12461246

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.
1250+
export function claudeQueryEnvironment(
1251+
environment: NodeJS.ProcessEnv,
1252+
appThread: Pick<OrchestrationV2AppThread, "lineage">,
1253+
subagentPromptCacheTtl: "5m" | "1h" | undefined,
1254+
): NodeJS.ProcessEnv {
1255+
if (
1256+
appThread.lineage.relationshipToParent !== "subagent" ||
1257+
subagentPromptCacheTtl === undefined ||
1258+
environment.CLAUDE_CODE_PROMPT_CACHE_TTL !== undefined
1259+
) {
1260+
return environment;
1261+
}
1262+
return { ...environment, CLAUDE_CODE_PROMPT_CACHE_TTL: subagentPromptCacheTtl };
1263+
}
1264+
12471265
export function makeClaudeQueryOptions(input: {
12481266
readonly modelSelection: ModelSelection;
12491267
readonly nativeThreadId: string;
@@ -2128,6 +2146,17 @@ const decodeClaudeBypassSettings = Schema.decodeUnknownEffect(
21282146
fromLenientJson(ClaudeBypassSettings),
21292147
);
21302148

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+
21312160
function claudeBypassAvailability(
21322161
settings: typeof ClaudeBypassSettings.Type,
21332162
): ClaudeBypassAvailability {
@@ -3862,6 +3891,30 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
38623891
},
38633892
);
38643893

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+
38653918
const userInvocableSkills = (cwd: string | null) =>
38663919
discoverClaudeSkills(
38673920
adapterOptions.settings,
@@ -9154,7 +9207,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
91549207
cwd: turnInput.runtimePolicy.cwd,
91559208
attachmentsDir,
91569209
settings: adapterOptions.settings,
9157-
environment: adapterOptions.environment,
9210+
environment: claudeQueryEnvironment(
9211+
adapterOptions.environment,
9212+
turnInput.appThread,
9213+
turnInput.appThread.lineage.relationshipToParent === "subagent"
9214+
? yield* resolveSubagentPromptCacheTtl(turnInput.runtimePolicy.cwd)
9215+
: undefined,
9216+
),
91589217
tools: queryPolicy.tools ?? CLAUDE_CODE_PRESET_TOOLS,
91599218
...(mcpOverrides.allowedTools === undefined
91609219
? {}

0 commit comments

Comments
 (0)