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
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ it.effect.each([false, true])("rejects thread.create for an existing id (deleted
}).pipe(Effect.provide(layerTest)),
);

it.effect("starts and finishes a delegated task without reading the parent's history", () =>
it.effect("starts, finishes and follows up a task without reading the parent's history", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const projections = yield* ProjectionStore.ProjectionStoreV2;
Expand Down Expand Up @@ -2019,10 +2019,11 @@ it.effect("starts and finishes a delegated task without reading the parent's his
});
}

// Keep the statements of the task start and finish operations, not of the recovery around them.
// Keep the statements of the task start, finish and follow-up, not of the recovery around them.
const taskSpans = [
"orchestrationV2.dispatch.delegatedTaskRequest",
"Orchestrator.finalizeAppOwnedSubagent",
"test.followUp",
];
const statements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
const recordStatements = Effect.provideService(
Expand Down Expand Up @@ -2103,9 +2104,38 @@ it.effect("starts and finishes a delegated task without reading the parent's his
["completed", "claimed", "completed", "completed", [task.id]],
);

// Start and finish read the task's own records, so their VM steps, which are deterministic,
// must stay flat on a copy with a long parent history. Left out: the counts that number the
// task and its wake, which walk one index.
// A person's follow-up to the finished child binds a new task to the parent's running run.
yield* orchestrator
.dispatch({
type: "message.dispatch",
commandId: CommandId.make("follow-up-long-parent"),
threadId: childThreadId,
messageId: MessageId.make("message:long-parent:follow-up"),
text: "One more thing",
attachments: [],
dispatchMode: { type: "start_immediately" },
createdBy: "user",
creationSource: "web",
})
.pipe(Effect.withSpan("test.followUp"), recordStatements);
const followedUp = yield* projections.getThreadRecords(parentThreadId, [
"subagents",
"turnItems",
]);
const followUp = followedUp.subagents.find((candidate) => candidate.id !== task.id);
assert.deepEqual(
[
followUp?.runId,
followedUp.turnItems.find(
(item) => item.type === "subagent" && item.subagentId === followUp?.id,
)?.providerTurnId,
],
[parentRun.id, providerTurnId],
);

// Start, finish and follow-up read the task's own records and the run it binds to, so their VM
// steps, which are deterministic, must stay flat on a copy with a long parent history. Left out:
// the counts that number the task and its wake, which walk one index.
const copyPath = "file:/delegated-task-reads?vfs=memdb";
using copy = new NodeSqlite.DatabaseSync(copyPath);
yield* sql`VACUUM INTO ${copyPath}`;
Expand Down Expand Up @@ -2146,6 +2176,9 @@ it.effect("starts and finishes a delegated task without reading the parent's his
"target_thread_id",
]);
addHistory("runs", "run_id");
// The history's runs have ended; only the parent's own run is still going.
copy.exec(`UPDATE orchestration_v2_projection_runs
SET status = 'completed', completed_at = requested_at WHERE run_id LIKE '%:history:%'`);
addHistory("run_attempts", "attempt_id", ["run_id"]);
addHistory("messages", "message_id");
addHistory("provider_turns", "provider_turn_id", ["run_attempt_id"]);
Expand Down
10 changes: 8 additions & 2 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9828,7 +9828,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
.filter((row) => row.id !== run.id && row.delegatedTaskId !== undefined)
.toSorted((a, b) => b.ordinal - a.ordinal)[0]?.delegatedTaskId;
const parent = yield* projectionStore
.getThreadRecords(parentThreadId, ["runs", "subagents", "providerTurns", "attempts"], {
.getThreadRecords(parentThreadId, ["runs", "subagents"], {
// Every run followUpOwnerRun can pick, so a long parent's history stays unread.
runStatuses: ["preparing", "starting", "running", "waiting"],
includeLatestExecutedRun: true,
subagentIds: [
...(child.forkedFrom?.type === "node" ? [child.forkedFrom.nodeId] : []),
...(latestFollowupId === undefined ? [] : [latestFollowupId]),
Expand Down Expand Up @@ -9865,6 +9868,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}
const owner = followUpOwnerRun(parent.runs);
if (owner === null || owner.rootNodeId === null) return;
const ownerRecords = yield* projectionStore
.getRunProviderRecords(parentThreadId, owner.id, owner.rootNodeId)
.pipe(mapDispatchError(command));
const now = message.createdAt;
const adapter = yield* providerAdapters.get(run.providerInstanceId);
const taskId = idAllocator.derive.delegatedTaskNode({ commandId: command.commandId });
Expand Down Expand Up @@ -9914,7 +9920,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
runId: owner.id,
nodeId: taskId,
providerThreadId: owner.providerThreadId,
providerTurnId: providerTurnForRun(parent, owner)?.id ?? null,
providerTurnId: providerTurnForRun(ownerRecords, owner)?.id ?? null,
nativeItemRef: null,
parentItemId: null,
ordinal: yield* nextTurnItemOrdinal(parent),
Expand Down
4 changes: 3 additions & 1 deletion apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,12 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
.join("\n");
assert.notInclude(runPlan, "orchestration_v2_projection_runs_thread_ordinal_idx");
assert.match(runPlan, /SEARCH orchestration_v2_projection_runs USING INDEX \w*status\w*/);
// The newest runs by ordinal, whatever their status, plus older runs the statuses match.
// The newest runs by ordinal, whatever their status, or the run that ended last, plus older runs
// the statuses match.
for (const [filter, expected] of [
[{ latestRunLimit: 2 }, [4, 5]],
[{ latestRunLimit: 2, runStatuses: ["rolled_back"] as const }, [1, 4, 5]],
[{ includeLatestExecutedRun: true, runStatuses: ["rolled_back"] as const }, [1, 4]],
] as const) {
const traced = traceSqlStatements();
const recent = yield* store
Expand Down
34 changes: 31 additions & 3 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ export interface ProjectionRecordFilter {
readonly includeLatestRun?: true;
/** The thread's N highest-ordinal runs, plus any run `runStatuses` matches. Ignores `includeLatestRun`. */
readonly latestRunLimit?: number;
/** Keeps the run `latestExecutedRun` picks even when `runStatuses` excludes it. Ignores `includeLatestRun`. */
readonly includeLatestExecutedRun?: true;
readonly providerTurnThreadIds?: ReadonlyArray<ProviderThreadId>;
/** Only the provider turn whose token usage was updated last. */
readonly latestProviderTurnUsageOnly?: true;
Expand Down Expand Up @@ -3988,13 +3990,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
(matchesNothing(filter?.runIds) ||
(matchesNothing(filter?.runStatuses) &&
filter?.includeLatestRun !== true &&
filter?.includeLatestExecutedRun !== true &&
filter?.latestRunLimit === undefined)))
? Effect.succeed([])
: window === undefined
? sql<PayloadRow>`
SELECT payload_json
FROM orchestration_v2_projection_runs
WHERE ${threadColumn(filter?.latestRunLimit === undefined ? filter?.runIds : [])} = ${threadId}
WHERE ${threadColumn(
filter?.latestRunLimit === undefined && filter?.includeLatestExecutedRun !== true
? filter?.runIds
: [],
)} = ${threadId}
${filter?.runIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.runIds)}))`}
${
filter?.latestRunLimit !== undefined
Expand All @@ -4017,12 +4024,33 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
ORDER BY ordinal ASC`
: filter?.runStatuses === undefined
? sql`ORDER BY ordinal ASC`
: filter.includeLatestRun !== true
: filter.includeLatestRun !== true && filter.includeLatestExecutedRun !== true
? // `+ordinal` stops SQLite from walking every run in the (thread_id, ordinal)
// index to skip a sort; a status index reads only matching runs.
sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
ORDER BY +ordinal ASC`
: sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
: filter.includeLatestExecutedRun === true
? sql`AND run_id IN (
SELECT run_id FROM orchestration_v2_projection_runs
WHERE thread_id = ${threadId}
AND status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
UNION
SELECT run_id FROM (
SELECT run_id FROM orchestration_v2_projection_runs
WHERE thread_id = ${threadId}
AND status <> 'queued'
AND NOT (
status = 'cancelled'
AND json_extract(payload_json, '$.startedAt') IS NULL
)
-- latestExecutedRun: the run that ended last (runRanAfter).
ORDER BY completed_at IS NULL DESC, completed_at DESC, ordinal DESC,
run_id DESC
LIMIT 1
)
)
ORDER BY ordinal ASC`
: sql`AND (status IN (SELECT value FROM json_each(${encodeIdList(filter.runStatuses)}))
OR run_id = (
SELECT run_id FROM orchestration_v2_projection_runs
WHERE thread_id = ${threadId}
Expand Down
Loading