Skip to content

Commit e7fba1e

Browse files
committed
fix(broker): roll back nested descendants inherited through a failed claim
A grandchild spawned while a provisional claim was open has an inherited child as its source, not the claimed root, so it was owned but omitted from the rollback. Record descendants whose source is either a claimed root or an already inherited thread.
1 parent fd45841 commit e7fba1e

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎plugins/codex/scripts/app-server-broker.mjs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,10 @@ async function main() {
308308
for (const socket of sourceOwners) {
309309
if (addThreadOwner(socket, subscribedThreadId)) {
310310
// Ownership inherited through a claim that has not succeeded yet is
311-
// rolled back with that claim.
311+
// rolled back with that claim. The source may itself be an inherited
312+
// child, so nested descendants are recorded too.
312313
const claim = provisionalClaims.get(socket);
313-
if (claim?.threadIds.has(sourceThreadId)) {
314+
if (claim && (claim.threadIds.has(sourceThreadId) || claim.inheritedThreadIds.has(sourceThreadId))) {
314315
claim.inheritedThreadIds.add(subscribedThreadId);
315316
}
316317
}

‎tests/broker-subscriptions.test.mjs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,17 @@ test("broker rolls back child threads inherited through a failed provisional cla
751751
(thread) => thread.name === "delayed-design-challenger"
752752
);
753753
assert.ok(childThread, "delayed child thread was not created");
754+
const grandchildCreated = await waitFor(() =>
755+
readState(broker.statePath)?.threads.some((thread) => thread.name === "delayed-design-grandchild")
756+
);
757+
assert.equal(grandchildCreated, true, "delayed grandchild thread was not created");
758+
const grandchildThread = readState(broker.statePath).threads.find(
759+
(thread) => thread.name === "delayed-design-grandchild"
760+
);
754761

755-
// Both the parent and the inherited child are released while the client stays connected.
756-
await waitForUnsubscribes(broker.statePath, [threadId, threadId, childThread.id]);
762+
// The parent, the inherited child, and the nested grandchild are all released
763+
// while the client stays connected.
764+
await waitForUnsubscribes(broker.statePath, [threadId, threadId, childThread.id, grandchildThread.id]);
757765
assert.deepEqual(readState(broker.statePath).subscriptions, []);
758766
await secondClient.end();
759767
});

‎tests/fake-codex-fixture.mjs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ rl.on("line", (line) => {
348348
349349
case "thread/resume": {
350350
if (BEHAVIOR === "with-delayed-subagent" && message.params.persistFullHistory === true) {
351+
state.nestedSubagentRequested = true;
352+
saveState(state);
351353
setTimeout(() => {
352354
send({ id: message.id, error: { code: -32000, message: "forced resume failure after child arrival" } });
353355
}, 250);
@@ -556,6 +558,17 @@ rl.on("line", (line) => {
556558
send({ method: "thread/started", params: { thread: { ...buildThread(subThreadRecord), name: subThreadRecord.name, agentNickname: subThreadRecord.name } } });
557559
send({ method: "turn/started", params: { threadId: subThread.id, turn: buildTurn(subTurnId) } });
558560
send({ method: "turn/completed", params: { threadId: subThread.id, turn: buildTurn(subTurnId, "completed") } });
561+
if (delayedState.nestedSubagentRequested) {
562+
setTimeout(() => {
563+
const nestedState = loadState();
564+
const grandchild = nextThread(nestedState, thread.cwd, true, { parentThreadId: subThread.id });
565+
const grandchildRecord = ensureThread(nestedState, grandchild.id);
566+
grandchildRecord.name = "delayed-design-grandchild";
567+
nestedState.subscriptions = [...new Set([...(nestedState.subscriptions || []), grandchild.id])];
568+
saveState(nestedState);
569+
send({ method: "thread/started", params: { thread: { ...buildThread(grandchildRecord), name: grandchildRecord.name, agentNickname: grandchildRecord.name } } });
570+
}, 100);
571+
}
559572
}, 100);
560573
break;
561574
}

0 commit comments

Comments
 (0)