Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions apps/server/src/orchestration-v2/SubagentProjection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ it("resets a subagent child's snooze, title job and remote delegation but keeps
threadId: ThreadId.make("linked-parent"),
title: "Linked parent",
},
lastSnoozeWakeAt: snoozedAt,
activeOrderKey: "parent-active",
opens: { openedAt: 1_753_348_800_000, score: 4, picks: [] },
group: {
status: "active",
primary: { model: "gpt-5.4", label: "GPT", handle: "gpt" },
seats: [
{
threadId: ThreadId.make("thread:parent-seat"),
handle: "claude",
label: "Claude",
status: "seated",
joinedAfterRun: 1,
},
],
discussion: { mode: "everyone", turnsLeft: 2, delivering: ["primary"] },
closedBecause: null,
closedAt: null,
},
};
const childProviderThreadId = ProviderThreadId.make("provider-thread:subagent-awake-child");
const parentNodeId = NodeId.make("node:subagent-parent");
Expand All @@ -110,6 +129,10 @@ it("resets a subagent child's snooze, title job and remote delegation but keeps

assert.isNull(childThread.snoozedUntil);
assert.isNull(childThread.snoozedAt);
assert.isNull(childThread.lastSnoozeWakeAt);
assert.isNull(childThread.activeOrderKey);
assert.isNull(childThread.opens);
assert.isNull(childThread.group);
assert.isNull(childThread.titleRegeneration);
assert.isUndefined(childThread.delegatedFrom);
assert.equal(childThread.projectId, parentThread.projectId);
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/persistence/Migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ import Migration0109 from "./Migrations/109_QueuedTurnInputIndexJsonValid.ts";
import Migration0110 from "./Migrations/110_ProviderTurnAttemptIndex.ts";
import Migration0111 from "./Migrations/111_TurnItemMessageOrdinalIndex.ts";
import Migration0112 from "./Migrations/112_TurnItemInlineWorkOutput.ts";
import Migration0113 from "./Migrations/113_RepairSubagentGroups.ts";

const migrationEntries = [
[1, "OrchestrationEvents", Migration0001],
Expand Down Expand Up @@ -238,6 +239,7 @@ const migrationEntries = [
[110, "ProviderTurnAttemptIndex", Migration0110],
[111, "TurnItemMessageOrdinalIndex", Migration0111],
[112, "TurnItemInlineWorkOutput", Migration0112],
[113, "RepairSubagentGroups", Migration0113],
] as const;

export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { assert, it } from "@effect/vitest";
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

import { runMigrations } from "../Migrations.ts";

const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" }));
const createdAt = "2026-11-15T00:00:00.000Z";
const group = {
status: "active",
primary: { model: "gpt-5.4", label: "GPT", handle: "gpt" },
seats: [
{
threadId: "thread:seat",
handle: "claude",
label: "Claude",
status: "seated",
joinedAfterRun: 1,
},
],
discussion: { mode: "everyone", turnsLeft: 2, delivering: ["primary"] },
closedBecause: null,
closedAt: null,
};

function threadPayload(id: string, lineage: object, extras: object = {}) {
return {
id,
projectId: "project-1",
title: id,
providerInstanceId: "codex",
modelSelection: { instanceId: "codex", model: "gpt-5.4" },
runtimeMode: "full-access",
interactionMode: "default",
createdBy: "user",
creationSource: "web",
branch: null,
worktreePath: null,
activeProviderThreadId: null,
lineage,
forkedFrom: null,
createdAt,
updatedAt: createdAt,
archivedAt: null,
deletedAt: null,
settledOverride: null,
settledAt: null,
group,
...extras,
};
}

layer("113_RepairSubagentGroups", (it) => {
it.effect("removes a copied group from a subagent and leaves the owner and the seat", () =>
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* runMigrations({ toMigrationInclusive: 112 });
const rows: ReadonlyArray<readonly [string, object]> = [
[
"owner",
threadPayload("owner", {
parentThreadId: null,
relationshipToParent: null,
rootThreadId: "owner",
}),
],
[
"seat",
threadPayload("seat", {
parentThreadId: "owner",
relationshipToParent: "fork",
rootThreadId: "owner",
sideChat: true,
}),
],
[
"subagent",
threadPayload(
"subagent",
{
parentThreadId: "owner",
relationshipToParent: "subagent",
rootThreadId: "owner",
},
{
lastSnoozeWakeAt: createdAt,
activeOrderKey: "parent-active",
opens: { openedAt: 1_753_348_800_000, score: 4, picks: [] },
},
),
],
[
"subagent-null",
threadPayload(
"subagent-null",
{
parentThreadId: "owner",
relationshipToParent: "subagent",
rootThreadId: "owner",
},
{ group: null },
),
],
];
for (const [id, payload] of rows) {
yield* sql`
INSERT INTO orchestration_v2_projection_threads (
thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
created_at, updated_at, payload_json
) VALUES (
${id}, 'project-1', ${id}, 'codex', 'full-access', 'default',
${createdAt}, ${createdAt}, ${JSON.stringify(payload)}
)
`;
}

const read = sql<{
readonly thread_id: string;
readonly payload_json: string;
readonly group_type: string | null;
}>`
SELECT thread_id, payload_json, json_type(payload_json, '$.group') AS group_type
FROM orchestration_v2_projection_threads
ORDER BY thread_id
`;
const before = yield* read;
assert.deepStrictEqual(
before.map((row) => [row.thread_id, row.group_type]),
[
["owner", "object"],
["seat", "object"],
["subagent", "object"],
["subagent-null", "null"],
],
);

yield* runMigrations({ toMigrationInclusive: 113 });
const after = new Map((yield* read).map((row) => [row.thread_id, row]));
for (const id of ["owner", "seat", "subagent-null"]) {
const earlier = before.find((row) => row.thread_id === id);
assert.strictEqual(after.get(id)?.payload_json, earlier?.payload_json);
}
const subagent = after.get("subagent");
assert.strictEqual(subagent?.group_type, null);
const kept = yield* sql<{
readonly wake: string | null;
readonly order_key: string | null;
readonly opens_score: number | null;
}>`
SELECT json_extract(payload_json, '$.lastSnoozeWakeAt') AS wake,
json_extract(payload_json, '$.activeOrderKey') AS order_key,
json_extract(payload_json, '$.opens.score') AS opens_score
FROM orchestration_v2_projection_threads
WHERE thread_id = 'subagent'
`;
assert.deepStrictEqual(kept, [
{ wake: createdAt, order_key: "parent-active", opens_score: 4 },
]);
}),
);
});
13 changes: 13 additions & 0 deletions apps/server/src/persistence/Migrations/113_RepairSubagentGroups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* sql`
UPDATE orchestration_v2_projection_threads
SET payload_json = json_remove(payload_json, '$.group')
WHERE json_valid(payload_json)
AND json_extract(payload_json, '$.lineage.relationshipToParent') = 'subagent'
AND json_type(payload_json, '$.group') = 'object'
`;
});
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe("V2 preview upgrade", () => {
[110, "ProviderTurnAttemptIndex"],
[111, "TurnItemMessageOrdinalIndex"],
[112, "TurnItemInlineWorkOutput"],
[113, "RepairSubagentGroups"],
]);
assert.deepStrictEqual(yield* runMigrations(), []);
assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
Expand Down Expand Up @@ -227,6 +228,7 @@ describe("V2 preview upgrade", () => {
[110, "ProviderTurnAttemptIndex"],
[111, "TurnItemMessageOrdinalIndex"],
[112, "TurnItemInlineWorkOutput"],
[113, "RepairSubagentGroups"],
]);
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
);
Expand Down
5 changes: 5 additions & 0 deletions packages/provider-core/src/server/subagentProjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ export function makeSubagentChildThread(input: {
settledAt: null,
snoozedUntil: null,
snoozedAt: null,
lastSnoozeWakeAt: null,
pinnedAt: null,
pinOrderKey: null,
activeOrderKey: null,
autoSettleDisabledAt: null,
// A guest link belongs to the thread it was made for.
guestShare: null,
// Seats belong to the thread that invited them.
group: null,
lastVisitedAt: null,
opens: null,
deletedAt: null,
};
}
Expand Down
Loading