Skip to content

Commit 361eace

Browse files
committed
fix(claude): isolate thread title generation
Run Claude thread title generation in a neutral temporary directory with all tools disabled. Keep permission bypass only for text-generation operations that still require their existing tool-capable behavior. Verify with focused Claude text-generation tests and server typecheck.
1 parent 9b2d043 commit 361eace

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎apps/server/src/textGeneration/ClaudeTextGeneration.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ function makeFakeClaudeBinary(dir: string) {
7171
' fail("CLAUDE_CONFIG_DIR was " + (process.env.CLAUDE_CONFIG_DIR ?? ""), 5);',
7272
"}",
7373
"",
74+
"const cwdMustNotBe = process.env.T3_FAKE_CLAUDE_CWD_MUST_NOT_BE;",
75+
"if (cwdMustNotBe && process.cwd() === cwdMustNotBe) {",
76+
' fail("Claude ran from a forbidden cwd", 6);',
77+
"}",
78+
"",
7479
"const stderrText = process.env.T3_FAKE_CLAUDE_STDERR;",
7580
"if (stderrText) {",
7681
' process.stderr.write(stderrText + "\\n");',
@@ -111,6 +116,7 @@ function withFakeClaudeEnv<A, E, R>(
111116
argsMustNotContain?: string;
112117
stdinMustContain?: string;
113118
configDirMustBe?: string;
119+
cwdMustNotBe?: string;
114120
claudeConfig?: Partial<ClaudeSettings>;
115121
},
116122
effectFn: (textGeneration: TextGeneration.TextGeneration["Service"]) => Effect.Effect<A, E, R>,
@@ -128,6 +134,7 @@ function withFakeClaudeEnv<A, E, R>(
128134
const previousArgsMustNotContain = process.env.T3_FAKE_CLAUDE_ARGS_MUST_NOT_CONTAIN;
129135
const previousStdinMustContain = process.env.T3_FAKE_CLAUDE_STDIN_MUST_CONTAIN;
130136
const previousConfigDirMustBe = process.env.T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE;
137+
const previousCwdMustNotBe = process.env.T3_FAKE_CLAUDE_CWD_MUST_NOT_BE;
131138

132139
yield* Effect.acquireRelease(
133140
Effect.sync(() => {
@@ -169,6 +176,12 @@ function withFakeClaudeEnv<A, E, R>(
169176
} else {
170177
delete process.env.T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE;
171178
}
179+
180+
if (input.cwdMustNotBe !== undefined) {
181+
process.env.T3_FAKE_CLAUDE_CWD_MUST_NOT_BE = input.cwdMustNotBe;
182+
} else {
183+
delete process.env.T3_FAKE_CLAUDE_CWD_MUST_NOT_BE;
184+
}
172185
}),
173186
() =>
174187
Effect.sync(() => {
@@ -215,6 +228,12 @@ function withFakeClaudeEnv<A, E, R>(
215228
} else {
216229
process.env.T3_FAKE_CLAUDE_CONFIG_DIR_MUST_BE = previousConfigDirMustBe;
217230
}
231+
232+
if (previousCwdMustNotBe === undefined) {
233+
delete process.env.T3_FAKE_CLAUDE_CWD_MUST_NOT_BE;
234+
} else {
235+
process.env.T3_FAKE_CLAUDE_CWD_MUST_NOT_BE = previousCwdMustNotBe;
236+
}
218237
}),
219238
);
220239

@@ -300,6 +319,9 @@ it.layer(ClaudeTextGenerationTestLayer)("ClaudeTextGeneration", (it) => {
300319
},
301320
}),
302321
stdinMustContain: "Please investigate reconnect failures after restarting the session.",
322+
argsMustContain: "--tools",
323+
argsMustNotContain: "--dangerously-skip-permissions",
324+
cwdMustNotBe: process.cwd(),
303325
},
304326
(textGeneration) =>
305327
Effect.gen(function* () {

‎apps/server/src/textGeneration/ClaudeTextGeneration.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*
88
* @module ClaudeTextGeneration
99
*/
10+
import { tmpdir } from "node:os";
11+
1012
import * as Effect from "effect/Effect";
1113
import * as Option from "effect/Option";
1214
import * as Schema from "effect/Schema";
@@ -110,6 +112,7 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
110112
prompt,
111113
outputSchemaJson,
112114
modelSelection,
115+
toolAccess = "default",
113116
}: {
114117
operation:
115118
| "generateCommitMessage"
@@ -120,6 +123,7 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
120123
prompt: string;
121124
outputSchemaJson: S;
122125
modelSelection: ModelSelection;
126+
toolAccess?: "default" | "none";
123127
}): Effect.fn.Return<S["Type"], TextGenerationError, S["DecodingServices"]> {
124128
const jsonSchemaStr = yield* encodeJsonForOperation(
125129
operation,
@@ -169,7 +173,7 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
169173
resolveClaudeApiModelId(modelSelection),
170174
...(cliEffort ? ["--effort", cliEffort] : []),
171175
...(settingsJson ? ["--settings", settingsJson] : []),
172-
"--dangerously-skip-permissions",
176+
...(toolAccess === "none" ? ["--tools", ""] : ["--dangerously-skip-permissions"]),
173177
],
174178
{ env: claudeEnvironment },
175179
);
@@ -348,10 +352,13 @@ export const makeClaudeTextGeneration = Effect.fn("makeClaudeTextGeneration")(fu
348352

349353
const generated = yield* runClaudeJson({
350354
operation: "generateThreadTitle",
351-
cwd: input.cwd,
355+
// A title is text-only. Keep the process outside the user's checkout
356+
// and remove every tool so a model cannot act on the embedded prompt.
357+
cwd: tmpdir(),
352358
prompt,
353359
outputSchemaJson: outputSchema,
354360
modelSelection: input.modelSelection,
361+
toolAccess: "none",
355362
});
356363

357364
return {

0 commit comments

Comments
 (0)