Skip to content

Commit 659ff80

Browse files
fix(server): clear groups copied into existing subagent threads
Existing subagent rows still carry the parent's group, so Stop on those children reaches the parent's seat. Migration 113 removes that group from subagent thread payloads. Owner threads and fork seats keep theirs. grok-4.7 via Cursor Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 52632bb commit 659ff80

3 files changed

Lines changed: 165 additions & 0 deletions

File tree

‎apps/server/src/persistence/Migrations.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ import Migration0109 from "./Migrations/109_QueuedTurnInputIndexJsonValid.ts";
123123
import Migration0110 from "./Migrations/110_ProviderTurnAttemptIndex.ts";
124124
import Migration0111 from "./Migrations/111_TurnItemMessageOrdinalIndex.ts";
125125
import Migration0112 from "./Migrations/112_TurnItemInlineWorkOutput.ts";
126+
import Migration0113 from "./Migrations/113_RepairSubagentGroups.ts";
126127

127128
const migrationEntries = [
128129
[1, "OrchestrationEvents", Migration0001],
@@ -238,6 +239,7 @@ const migrationEntries = [
238239
[110, "ProviderTurnAttemptIndex", Migration0110],
239240
[111, "TurnItemMessageOrdinalIndex", Migration0111],
240241
[112, "TurnItemInlineWorkOutput", Migration0112],
242+
[113, "RepairSubagentGroups", Migration0113],
241243
] as const;
242244

243245
export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import { assert, it } from "@effect/vitest";
2+
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3+
import * as Effect from "effect/Effect";
4+
import * as SqlClient from "effect/sql/SqlClient";
5+
6+
import { runMigrations } from "../Migrations.ts";
7+
8+
const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" }));
9+
const createdAt = "2026-11-15T00:00:00.000Z";
10+
const group = {
11+
status: "active",
12+
primary: { model: "gpt-5.4", label: "GPT", handle: "gpt" },
13+
seats: [
14+
{
15+
threadId: "thread:seat",
16+
handle: "claude",
17+
label: "Claude",
18+
status: "seated",
19+
joinedAfterRun: 1,
20+
},
21+
],
22+
discussion: { mode: "everyone", turnsLeft: 2, delivering: ["primary"] },
23+
closedBecause: null,
24+
closedAt: null,
25+
};
26+
27+
function threadPayload(id: string, lineage: object, extras: object = {}) {
28+
return {
29+
id,
30+
projectId: "project-1",
31+
title: id,
32+
providerInstanceId: "codex",
33+
modelSelection: { instanceId: "codex", model: "gpt-5.4" },
34+
runtimeMode: "full-access",
35+
interactionMode: "default",
36+
createdBy: "user",
37+
creationSource: "web",
38+
branch: null,
39+
worktreePath: null,
40+
activeProviderThreadId: null,
41+
lineage,
42+
forkedFrom: null,
43+
createdAt,
44+
updatedAt: createdAt,
45+
archivedAt: null,
46+
deletedAt: null,
47+
settledOverride: null,
48+
settledAt: null,
49+
group,
50+
...extras,
51+
};
52+
}
53+
54+
layer("113_RepairSubagentGroups", (it) => {
55+
it.effect("removes a copied group from a subagent and leaves the owner and the seat", () =>
56+
Effect.gen(function* () {
57+
const sql = yield* SqlClient.SqlClient;
58+
yield* runMigrations({ toMigrationInclusive: 112 });
59+
const rows: ReadonlyArray<readonly [string, object]> = [
60+
[
61+
"owner",
62+
threadPayload("owner", {
63+
parentThreadId: null,
64+
relationshipToParent: null,
65+
rootThreadId: "owner",
66+
}),
67+
],
68+
[
69+
"seat",
70+
threadPayload("seat", {
71+
parentThreadId: "owner",
72+
relationshipToParent: "fork",
73+
rootThreadId: "owner",
74+
sideChat: true,
75+
}),
76+
],
77+
[
78+
"subagent",
79+
threadPayload(
80+
"subagent",
81+
{
82+
parentThreadId: "owner",
83+
relationshipToParent: "subagent",
84+
rootThreadId: "owner",
85+
},
86+
{
87+
lastSnoozeWakeAt: createdAt,
88+
activeOrderKey: "parent-active",
89+
opens: { openedAt: 1_753_348_800_000, score: 4, picks: [] },
90+
},
91+
),
92+
],
93+
];
94+
for (const [id, payload] of rows) {
95+
yield* sql`
96+
INSERT INTO orchestration_v2_projection_threads (
97+
thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
98+
created_at, updated_at, payload_json
99+
) VALUES (
100+
${id}, 'project-1', ${id}, 'codex', 'full-access', 'default',
101+
${createdAt}, ${createdAt}, ${JSON.stringify(payload)}
102+
)
103+
`;
104+
}
105+
106+
const read = sql<{
107+
readonly thread_id: string;
108+
readonly payload_json: string;
109+
readonly group_type: string | null;
110+
}>`
111+
SELECT thread_id, payload_json, json_type(payload_json, '$.group') AS group_type
112+
FROM orchestration_v2_projection_threads
113+
ORDER BY thread_id
114+
`;
115+
const before = yield* read;
116+
assert.deepStrictEqual(
117+
before.map((row) => [row.thread_id, row.group_type]),
118+
[
119+
["owner", "object"],
120+
["seat", "object"],
121+
["subagent", "object"],
122+
],
123+
);
124+
125+
yield* runMigrations({ toMigrationInclusive: 113 });
126+
const after = new Map((yield* read).map((row) => [row.thread_id, row]));
127+
for (const id of ["owner", "seat"]) {
128+
const earlier = before.find((row) => row.thread_id === id);
129+
assert.strictEqual(after.get(id)?.payload_json, earlier?.payload_json);
130+
assert.strictEqual(after.get(id)?.group_type, "object");
131+
}
132+
const subagent = after.get("subagent");
133+
assert.strictEqual(subagent?.group_type, null);
134+
const kept = yield* sql<{
135+
readonly wake: string | null;
136+
readonly order_key: string | null;
137+
readonly opens_score: number | null;
138+
}>`
139+
SELECT json_extract(payload_json, '$.lastSnoozeWakeAt') AS wake,
140+
json_extract(payload_json, '$.activeOrderKey') AS order_key,
141+
json_extract(payload_json, '$.opens.score') AS opens_score
142+
FROM orchestration_v2_projection_threads
143+
WHERE thread_id = 'subagent'
144+
`;
145+
assert.deepStrictEqual(kept, [
146+
{ wake: createdAt, order_key: "parent-active", opens_score: 4 },
147+
]);
148+
}),
149+
);
150+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Effect from "effect/Effect";
2+
import * as SqlClient from "effect/sql/SqlClient";
3+
4+
export default Effect.gen(function* () {
5+
const sql = yield* SqlClient.SqlClient;
6+
yield* sql`
7+
UPDATE orchestration_v2_projection_threads
8+
SET payload_json = json_remove(payload_json, '$.group')
9+
WHERE json_valid(payload_json)
10+
AND json_extract(payload_json, '$.lineage.relationshipToParent') = 'subagent'
11+
AND json_type(payload_json, '$.group') IS NOT NULL
12+
`;
13+
});

0 commit comments

Comments
 (0)