@devries-cameron20 / t3code-1 / commits / 67861fd9fb
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): delegated threads no longer share one provider event log (#29770)

Co-authored-by: Taylor Dolezal <117500+onlydole@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Taylor Dolezal committed Nov 3, 2026 at 15:36 UTC 67861fd9fbc8d65d755ee94f5a23c92f4dab7baa
3 files changed +48 -5
apps/server/src/attachmentStore.ts
+1 -1
@@ -13,7 +13,7 @@ import {
13 import { inferImageExtension, SAFE_IMAGE_FILE_EXTENSIONS } from "./imageMime.ts";
14
15 const ATTACHMENT_FILENAME_EXTENSIONS = [...SAFE_IMAGE_FILE_EXTENSIONS, ".bin"];
16 -const ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS = 80;
16 +export const ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS = 80;
17 const ATTACHMENT_ID_THREAD_SEGMENT_PATTERN = "[a-z0-9_]+(?:-[a-z0-9_]+)*";
18 const ATTACHMENT_ID_UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
19 const ATTACHMENT_ID_FILE_EXTENSION_PATTERN = "[a-z0-9]{1,10}";
apps/server/src/provider/EventNdjsonLogger.test.ts
+33
@@ -156,6 +156,39 @@ it.layer(NodeServices.layer)("EventNdjsonLogger", (it) => {
156 }),
157 );
158
159 + it.effect("keeps long thread ids that share a prefix in separate files", () =>
160 + Effect.gen(function* () {
161 + const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-provider-log-"));
162 + const basePath = NodePath.join(tempDir, "events.log");
163 + const prefix =
164 + "thread:delegated-task:command%3Amcp%3A00000000-0000-4000-8000-000000000000%3Adelegate-task%3A";
165 +
166 + try {
167 + const store = yield* makeEventNdjsonLogStore(basePath, { batchWindowMs: 0 });
168 + const logger = store.logger("native");
169 + yield* logger.write({ id: "first" }, ThreadId.make(`${prefix}review-the-diff`));
170 + yield* logger.write({ id: "second" }, ThreadId.make(`${prefix}write-the-tests`));
171 + yield* store.close();
172 +
173 + const files = NodeFS.readdirSync(tempDir);
174 + assert.equal(files.length, 2);
175 + assert.deepEqual(
176 + files
177 + .map((name) =>
178 + NodeFS.readFileSync(NodePath.join(tempDir, name), "utf8")
179 + .trim()
180 + .split("\n")
181 + .map((line) => parseLogLine(line).payload),
182 + )
183 + .toSorted(),
184 + [['{"id":"first"}'], ['{"id":"second"}']],
185 + );
186 + } finally {
187 + NodeFS.rmSync(tempDir, { recursive: true, force: true });
188 + }
189 + }),
190 + );
191 +
192 it.effect("keeps shared store views non-owning when one adapter closes", () =>
193 Effect.gen(function* () {
194 const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-provider-log-"));
apps/server/src/provider/EventNdjsonLogger.ts
+14 -4
@@ -1,5 +1,6 @@
1 // @effect-diagnostics nodeBuiltinImport:off
2 /** Native and canonical views share write state to avoid races on the same thread file. */
3 +import * as NodeCrypto from "node:crypto";
4 import * as NodeFS from "node:fs";
5 import * as NodePath from "node:path";
6
@@ -18,7 +19,10 @@ import * as Schema from "effect/Schema";
19 import * as Scope from "effect/Scope";
20 import * as SynchronizedRef from "effect/SynchronizedRef";
21
21 -import { toSafeThreadAttachmentSegment } from "../attachmentStore.ts";
22 +import {
23 + ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS,
24 + toSafeThreadAttachmentSegment,
25 +} from "../attachmentStore.ts";
26 import type * as ResourceAttribution from "../resourceTelemetry/ResourceAttribution.ts";
27
28 const MEBIBYTE = 1024 * 1024;
@@ -145,8 +149,13 @@ function logWarning(message: string, context: Record<string, unknown>): Effect.E
149 }
150
151 function resolveThreadSegment(raw: string | null | undefined): string {
148 - const normalized = typeof raw === "string" ? toSafeThreadAttachmentSegment(raw) : null;
149 - return normalized ?? GLOBAL_THREAD_SEGMENT;
152 + if (typeof raw !== "string") return GLOBAL_THREAD_SEGMENT;
153 + const normalized = toSafeThreadAttachmentSegment(raw);
154 + if (normalized === null) return GLOBAL_THREAD_SEGMENT;
155 + // Delegated thread ids share a prefix longer than the segment, so ids that may be cut get a hash.
156 + if (raw.length <= ATTACHMENT_ID_THREAD_SEGMENT_MAX_CHARS) return normalized;
157 + const hash = NodeCrypto.createHash("sha256").update(raw).digest("hex").slice(0, 16);
158 + return `${normalized}-${hash}`;
159 }
160
161 function resolveStreamLabel(stream: EventNdjsonStream): string {
@@ -725,12 +734,13 @@ export const makeEventNdjsonLogStore = Effect.fnUntraced(function* (
734 const observedAt = yield* DateTime.now.pipe(Effect.map(DateTime.formatIso));
735 const line = `[${observedAt}] ${resolveStreamLabel(stream)}: ${payload}\n`;
736 const bytes = Buffer.byteLength(line);
737 + const threadSegment = resolveThreadSegment(threadId);
738 const action = yield* SynchronizedRef.modifyEffect(stateRef, (state) => {
739 if (state.closed) {
740 return Effect.succeed([{ flush: false }, state] as const);
741 }
742 const pending = state.pending;
733 - pending.push({ stream, threadSegment: resolveThreadSegment(threadId), line, bytes });
743 + pending.push({ stream, threadSegment, line, bytes });
744 const pendingBytes = state.pendingBytes + bytes;
745 const flush =
746 resolved.batchWindowMs === 0 ||