@devries-cameron20 / t3code Mirror · read-only / commits / 861211f215
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 09:45 UTC

fix(server): skip settlement cleanup for deleted threads (#37833)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

안동민 committed Nov 17, 2026 at 11:02 UTC 861211f215848f017167c9624ae262ef8a7acc01
2 files changed +54 -42
apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+51 -39
@@ -2090,24 +2090,31 @@ describe("ThreadSettlementServiceV2 worker", () => {
2090 });
2091
2092 describe("ThreadSettlementServiceV2 terminals", () => {
2093 - it.effect("keeps the terminals of a thread re-engaged before the event ran", () =>
2094 - Effect.gen(function* () {
2095 - const settled = appThread("reengaged-thread", "settled");
2096 - const reengaged = appThread("reengaged-thread", "active");
2097 - const marker = appThread("marker-thread", "settled");
2098 - const fixture = yield* makeHarness({
2099 - snapshot: makeSnapshot([]),
2100 - currentThreads: [reengaged, marker],
2101 - });
2093 + it.effect.each(["re-engaged", "deleted"] as const)(
2094 + "skips settlement actions for a thread %s before the event ran",
2095 + (state) =>
2096 + Effect.gen(function* () {
2097 + const settled = appThread("stale-thread", "settled", "/worktrees/stale");
2098 + const current =
2099 + state === "re-engaged"
2100 + ? { ...settled, settledOverride: "active" as const }
2101 + : { ...settled, deletedAt: DateTime.makeUnsafe(NOW) };
2102 + const marker = appThread("marker-thread", "settled", "/worktrees/marker");
2103 + const fixture = yield* makeHarness({
2104 + snapshot: makeSnapshot([]),
2105 + currentThreads: [current, marker],
2106 + existingWorktreePaths: ["/worktrees/stale", "/worktrees/marker"],
2107 + });
2108
2103 - yield* Effect.gen(function* () {
2104 - const service = yield* ThreadSettlementService.ThreadSettlementServiceV2;
2105 - yield* startHarness(service, fixture.activation, fixture.snapshotReads);
2106 - yield* fixture.publishEvent(settledEvent(settled));
2107 - yield* fixture.publishEvent(settledEvent(marker));
2108 - assert.deepStrictEqual(yield* Queue.take(fixture.closedIdle), { threadId: marker.id });
2109 - }).pipe(Effect.provide(fixture.layer));
2110 - }),
2109 + yield* Effect.gen(function* () {
2110 + const service = yield* ThreadSettlementService.ThreadSettlementServiceV2;
2111 + yield* startHarness(service, fixture.activation, fixture.snapshotReads);
2112 + yield* fixture.publishEvent(settledEvent(settled));
2113 + yield* fixture.publishEvent(settledEvent(marker));
2114 + assert.deepStrictEqual(yield* Queue.take(fixture.closedIdle), { threadId: marker.id });
2115 + assert.strictEqual((yield* Queue.take(fixture.scriptRuns)).threadId, marker.id);
2116 + }).pipe(Effect.provide(fixture.layer));
2117 + }),
2118 );
2119
2120 it.effect("runs the settle script in the thread's own worktree", () =>
@@ -2135,29 +2142,34 @@ describe("ThreadSettlementServiceV2 terminals", () => {
2142 }),
2143 );
2144
2138 - it.effect("skips the settle script for a thread re-engaged while its shells close", () =>
2139 - Effect.gen(function* () {
2140 - const settled = appThread("reengaging-thread", "settled", "/worktrees/reengaging");
2141 - const marker = appThread("marker-thread", "settled", "/worktrees/marker");
2142 - const currentThreads = [settled, marker];
2143 - const fixture = yield* makeHarness({
2144 - snapshot: makeSnapshot([]),
2145 - currentThreads,
2146 - existingWorktreePaths: ["/worktrees/reengaging", "/worktrees/marker"],
2147 - onCloseIdle: () =>
2148 - Effect.sync(() => {
2149 - currentThreads[0] = { ...settled, settledOverride: "active" };
2150 - }),
2151 - });
2145 + it.effect.each(["re-engaged", "deleted"] as const)(
2146 + "skips the settle script for a thread %s while its shells close",
2147 + (state) =>
2148 + Effect.gen(function* () {
2149 + const settled = appThread("stale-thread", "settled", "/worktrees/stale");
2150 + const marker = appThread("marker-thread", "settled", "/worktrees/marker");
2151 + const currentThreads = [settled, marker];
2152 + const fixture = yield* makeHarness({
2153 + snapshot: makeSnapshot([]),
2154 + currentThreads,
2155 + existingWorktreePaths: ["/worktrees/stale", "/worktrees/marker"],
2156 + onCloseIdle: () =>
2157 + Effect.sync(() => {
2158 + currentThreads[0] =
2159 + state === "re-engaged"
2160 + ? { ...settled, settledOverride: "active" }
2161 + : { ...settled, deletedAt: DateTime.makeUnsafe(NOW) };
2162 + }),
2163 + });
2164
2153 - yield* Effect.gen(function* () {
2154 - const service = yield* ThreadSettlementService.ThreadSettlementServiceV2;
2155 - yield* startHarness(service, fixture.activation, fixture.snapshotReads);
2156 - yield* fixture.publishEvent(settledEvent(settled));
2157 - yield* fixture.publishEvent(settledEvent(marker));
2158 - assert.strictEqual((yield* Queue.take(fixture.scriptRuns)).threadId, marker.id);
2159 - }).pipe(Effect.provide(fixture.layer));
2160 - }),
2165 + yield* Effect.gen(function* () {
2166 + const service = yield* ThreadSettlementService.ThreadSettlementServiceV2;
2167 + yield* startHarness(service, fixture.activation, fixture.snapshotReads);
2168 + yield* fixture.publishEvent(settledEvent(settled));
2169 + yield* fixture.publishEvent(settledEvent(marker));
2170 + assert.strictEqual((yield* Queue.take(fixture.scriptRuns)).threadId, marker.id);
2171 + }).pipe(Effect.provide(fixture.layer));
2172 + }),
2173 );
2174
2175 it.effect("skips the settle script for a subagent in its parent's worktree", () =>
apps/server/src/orchestration-v2/ThreadSettlementService.ts
+3 -3
@@ -700,13 +700,13 @@ export const make = Effect.gen(function* () {
700 const cleanUpSettledThread = Effect.fn("ThreadSettlementServiceV2.cleanUpSettledThread")(
701 function* (threadId: ThreadId) {
702 const settled = yield* projections.getThread(threadId);
703 - if (settled.settledOverride !== "settled") return;
703 + if (settled.deletedAt !== null || settled.settledOverride !== "settled") return;
704 yield* terminals.closeOnSettle({ threadId });
705 const worktreePath = settled.worktreePath;
706 if (worktreePath === null || !(yield* fileSystem.exists(worktreePath))) return;
707 - // Recheck after closing and worktree I/O; re-engaged threads must not be cleaned up.
707 + // Recheck after closing and worktree I/O; only live, settled threads may run scripts.
708 const thread = yield* projections.getThread(threadId);
709 - if (thread.settledOverride !== "settled") return;
709 + if (thread.deletedAt !== null || thread.settledOverride !== "settled") return;
710 const settledAtMs = toMillis(thread.settledAt);
711 if (settledAtMs === null || settleActionRunAt.get(threadId) === settledAtMs) return;
712 // The parent's worktree belongs to the parent; shared-worktree children