Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Keep sessions working while their subagents run (#1689)

Regan Bell7c1a333febab
diff --git a/src/api/app-sessions.ts b/src/api/app-sessions.ts
index 95a3bab40..80582be6a 100644
--- a/src/api/app-sessions.ts
+++ b/src/api/app-sessions.ts
@@ -1,6 +1,11 @@
import { notifyDeploymentShared } from "../deploy/share-notice.ts";
import { deploymentShareScope } from "../deploy/email-access.ts";
-import { sessionTreeRoot, sessionTreeRunCount, SUBAGENT_TREE_RUN_CAP } from "../sessions/session-syscalls.ts";
+import {
+ sessionTreeRoot,
+ sessionTreeRunCount,
+ SUBAGENT_TREE_RUN_CAP,
+ workingSessionThreadRefs,
+} from "../sessions/session-syscalls.ts";
import { isSessionStatus } from "../sessions/session-status.ts";
import type { PendingApprovalRecord } from "../types.ts";
import { orgId as orgIdOf } from "../config.ts";
@@ -396,7 +401,6 @@ export function createSessionMethods(
},
async listSessions(principalId) {
- const workingThreadRefs = new Set(await deps.runs.activeSessionIds());
const all = await sessionsForViewer(principalId);
const visibleById = new Map(all.map((session) => [session.id, session]));
const approvalRows: PendingApprovalRecord[] = [];
@@ -405,6 +409,7 @@ export function createSessionMethods(
if (session && (await approvalRecordIsCurrent(record, session))) approvalRows.push(record);
}
const waiting = new Set(approvalRows.filter((r) => r.blocksInput !== false).map((r) => r.sessionId));
+ const workingThreadRefs = await workingSessionThreadRefs(deps.sessions, deps.runs, waiting);
const sessions = all.filter(
(s) =>
s.hasEntries !== false || Boolean(s.title?.trim()) || workingThreadRefs.has(s.threadRef) || waiting.has(s.id),
diff --git a/src/sessions/session-syscalls.ts b/src/sessions/session-syscalls.ts
index e9e4b54a7..220cad3a3 100644
--- a/src/sessions/session-syscalls.ts
+++ b/src/sessions/session-syscalls.ts
@@ -6,7 +6,7 @@ import { conversationScope } from "../resolution/resolution-service.ts";
import { sleep } from "../util/async.ts";
import { pgTextSafe } from "../util/text.ts";
import { createMemoryAdvisoryLock, type AdvisoryLock } from "../persistence/advisory-lock.ts";
-import { errMessage } from "../util/errors.ts";
+import { errMessage, swallowAs } from "../util/errors.ts";
import { filterHistoryForAudience } from "../resolution/context-filter.ts";
import { randomUUID } from "node:crypto";
import { hashId } from "../util/crypto.ts";
@@ -337,7 +337,8 @@ function assertAudienceCompatible(
throw new Error("the target audience includes people outside the sender's authorized audience");
}
-export async function sessionTreeRoot(sessions: Pick<SessionStore, "get">, session: Session): Promise<Session> {
+async function sessionAncestors(sessions: Pick<SessionStore, "get">, session: Session): Promise<Session[]> {
+ const ancestors: Session[] = [];
let current = session;
const seen = new Set<string>();
while (current.parentSessionId) {
@@ -345,9 +346,49 @@ export async function sessionTreeRoot(sessions: Pick<SessionStore, "get">, sessi
seen.add(current.id);
const parent = await sessions.get(current.parentSessionId);
if (!parent) break;
+ ancestors.push(parent);
current = parent;
}
- return current;
+ return ancestors;
+}
+
+export async function sessionTreeRoot(sessions: Pick<SessionStore, "get">, session: Session): Promise<Session> {
+ return (await sessionAncestors(sessions, session)).at(-1) ?? session;
+}
+
+export async function stoppableAncestors(sessions: Pick<SessionStore, "get">, session: Session): Promise<Session[]> {
+ return (await sessionAncestors(sessions, session)).filter((ancestor) => ancestor.scopeId === session.scopeId);
+}
+
+export async function sessionTreeWorking(
+ sessions: Pick<SessionStore, "childrenOf">,
+ runs: Pick<RunStore, "activeForThread">,
+ root: Session,
+): Promise<boolean> {
+ for (const session of await treeSessions(sessions, root)) {
+ if (session.scopeId === root.scopeId && (await runs.activeForThread(session.threadRef))) return true;
+ }
+ return false;
+}
+
+export async function workingSessionThreadRefs(
+ sessions: Pick<SessionStore, "get" | "getByThread">,
+ runs: Pick<RunStore, "activeSessionIds">,
+ awaitingSessionIds: ReadonlySet<string>,
+): Promise<Set<string>> {
+ const active = await runs.activeSessionIds();
+ const working = new Set(active);
+ await Promise.all(
+ active
+ .map(async (threadRef) => {
+ const session = await sessions.getByThread(threadRef);
+ if (!session?.parentSessionId) return;
+ for (const ancestor of await stoppableAncestors(sessions, session))
+ if (!awaitingSessionIds.has(ancestor.id)) working.add(ancestor.threadRef);
+ })
+ .map((lookup) => lookup.catch(swallowAs("session list: working ancestors", undefined))),
+ );
+ return working;
}
async function treeSessions(sessions: Pick<SessionStore, "childrenOf">, root: Session): Promise<Session[]> {
diff --git a/src/wiring.ts b/src/wiring.ts
index d2ec9d711..d431f88f8 100644
--- a/src/wiring.ts
+++ b/src/wiring.ts
@@ -31,6 +31,8 @@ import {
deliverSubagentMail,
sessionTreeRoot,
sessionTreeRunCount,
+ sessionTreeWorking,
+ stoppableAncestors,
SUBAGENT_TREE_RUN_CAP,
} from "./sessions/session-syscalls.ts";
import { createPostgresBrokerSessions, type BrokerSessionStore } from "./auth/broker-sessions.ts";
@@ -129,6 +131,7 @@ import type {
ShipGrant,
PendingApprovalRecord,
ScopeId,
+ Session,
SurfaceContextRequest,
Webhook,
} from "./types.ts";
@@ -1458,6 +1461,22 @@ export function buildApp(
runStoreKind === "postgres"
? createPostgresRunStore(requireDbUrl("RUN_STORE"), { maxClaims: config.maxClaims })
: createMemoryRunStore({ maxClaims: config.maxClaims });
+ const blockingApprovalSessions = async (): Promise<Set<string>> =>
+ new Set(
+ (await approvals.entries())
+ .filter(([, r]) => r.blocksInput !== false && actorAssertionActive(identity, r.request?.actor))
+ .map(([, r]) => r.sessionId),
+ );
+ const workingAncestors = async (session: Session) => {
+ const awaiting = await blockingApprovalSessions();
+ const ancestors = (await stoppableAncestors(sessions, session)).filter((ancestor) => !awaiting.has(ancestor.id));
+ return Promise.all(
+ ancestors.map(async (ancestor) => ({
+ session: ancestor,
+ participants: await sessions.participantsOf(ancestor.id),
+ })),
+ );
+ };
const runs: RunStore = {
...runStore.runs,
async enqueue(input) {
@@ -1472,14 +1491,23 @@ export function buildApp(
)
throw new Error(`all ${SUBAGENT_TREE_RUN_CAP} session run slots are in use`);
const participants = known ? await sessions.participantsOf(known.id) : [];
+ const ancestors = known?.parentSessionId ? await workingAncestors(known) : [];
const result = await runStore.runs.enqueue(input);
- if (!result.deduped)
+ if (result.deduped) return result;
+ sessionStateBus.emit({
+ threadRef: input.sessionId,
+ ...(known ? { sessionId: known.id } : {}),
+ state: "working",
+ at: result.run.createdAt,
+ participants: participants.length ? participants : [input.request.actor.id],
+ });
+ for (const ancestor of ancestors)
sessionStateBus.emit({
- threadRef: input.sessionId,
- ...(known ? { sessionId: known.id } : {}),
+ threadRef: ancestor.session.threadRef,
+ sessionId: ancestor.session.id,
state: "working",
at: result.run.createdAt,
- participants: participants.length ? participants : [input.request.actor.id],
+ participants: ancestor.participants,
});
return result;
};
@@ -2199,20 +2227,30 @@ export function buildApp(
});
runs.onTerminal((run) => {
void (async () => {
- const uuid = (await sessions.getByThread(run.sessionId))?.id;
- const rows = uuid ? await approvals.entries() : [];
- const awaiting = rows.some(
- ([, r]) => r.sessionId === uuid && r.blocksInput !== false && actorAssertionActive(identity, r.request?.actor),
- );
- const participants = uuid ? await sessions.participantsOf(uuid) : [];
- if (await runs.activeForThread(run.sessionId)) return;
- sessionStateBus.emit({
- threadRef: run.sessionId,
- ...(uuid ? { sessionId: uuid } : {}),
- state: awaiting ? "awaiting_approval" : "idle",
- at: run.finishedAt ?? Date.now(),
- ...(participants.length ? { participants } : {}),
- });
+ const session = await sessions.getByThread(run.sessionId);
+ const at = run.finishedAt ?? Date.now();
+ if (!session) {
+ if (!(await runs.activeForThread(run.sessionId)))
+ sessionStateBus.emit({ threadRef: run.sessionId, state: "idle", at });
+ return;
+ }
+ const awaiting = await blockingApprovalSessions();
+ const settle = async (target: Session): Promise<boolean> => {
+ if (await runs.activeForThread(target.threadRef)) return false;
+ const waiting = awaiting.has(target.id);
+ if (!waiting && (await sessionTreeWorking(sessions, runs, target))) return false;
+ const participants = await sessions.participantsOf(target.id);
+ sessionStateBus.emit({
+ threadRef: target.threadRef,
+ sessionId: target.id,
+ state: waiting ? "awaiting_approval" : "idle",
+ at,
+ ...(participants.length ? { participants } : {}),
+ });
+ return true;
+ };
+ if (!(await settle(session))) return;
+ for (const ancestor of await stoppableAncestors(sessions, session)) if (!(await settle(ancestor))) return;
})().catch(swallowAs("session-state: terminal emit", undefined));
});
let lastSignalPrune = 0;
diff --git a/test/session-state-events.test.ts b/test/session-state-events.test.ts
index c91d3ceaf..28d48e394 100644
--- a/test/session-state-events.test.ts
+++ b/test/session-state-events.test.ts
@@ -279,3 +279,83 @@ test("a settle frame is stamped with the run's durable finishedAt — a later en
const row = await built.runs.get(run.id);
assert.equal(second!.at, row!.finishedAt, "the settle frame carries the run's durable finishedAt");
});
+
+test("a parent stays working while a subagent runs after its own turn ends — push and snapshot agree", async () => {
+ const built = freshApp();
+ const parentThread = "web:U1:parent";
+ const childThread = "web:U1:parent-child";
+ const parent = await built.app.turn(dm("hello", parentThread));
+ const child = await built.app.turn(dm("hello", childThread));
+ await built.sessions.setParentSession(child.sessionId!, parent.sessionId!);
+ const got = record(built.sessionStateBus);
+ const { run: parentRun } = await built.runs.enqueue({
+ sessionId: parentThread,
+ request: resolvedDm("delegate", parentThread),
+ maxAttempts: 3,
+ });
+ const { run: childRun } = await built.runs.enqueue({
+ sessionId: childThread,
+ request: resolvedDm("work", childThread),
+ maxAttempts: 3,
+ });
+ const leasedParent = await built.runs.claimById(parentRun.id, "w1", 30_000);
+ await built.runs.complete(parentRun.id, leasedParent!.leaseToken!, { status: "ok", reply: "delegated" });
+ await new Promise((r) => setTimeout(r, 150));
+ assert.deepEqual(statesFor(got, parentThread), ["working", "working"], "the subagent keeps its parent working");
+ const working = (await built.app.listSessions("U1")).find((s) => s.id === parent.sessionId);
+ assert.equal(working?.working, true, "the sidebar snapshot shows the parent working too");
+
+ const leasedChild = await built.runs.claimById(childRun.id, "w1", 30_000);
+ await built.runs.complete(childRun.id, leasedChild!.leaseToken!, { status: "ok", reply: "done" });
+ assert.ok(
+ await waitFor(() => statesFor(got, parentThread).includes("idle")),
+ "the last subagent run settles the parent",
+ );
+ assert.deepEqual(statesFor(got, childThread), ["working", "idle"]);
+ const settled = (await built.app.listSessions("U1")).find((s) => s.id === parent.sessionId);
+ assert.ok(!settled?.working, "the snapshot settles with the push");
+});
+
+test("a parent parked on a blocking approval keeps awaiting while its subagent runs", async () => {
+ const built = freshApp();
+ const parentThread = "web:U1:parked-parent";
+ const childThread = "web:U1:parked-child";
+ const child = await built.app.turn(dm("hello", childThread));
+ const got = record(built.sessionStateBus);
+ const parent = await built.app.turn(dm(`!run ${BLOCKED_CMD}`, parentThread));
+ assert.equal(parent.status, "pending_approval");
+ await built.sessions.setParentSession(child.sessionId!, parent.sessionId!);
+ const { run } = await built.runs.enqueue({
+ sessionId: childThread,
+ request: resolvedDm("work", childThread),
+ maxAttempts: 3,
+ });
+ const leased = await built.runs.claimById(run.id, "w1", 30_000);
+ await built.runs.complete(run.id, leased!.leaseToken!, { status: "ok", reply: "done" });
+ assert.ok(await waitFor(() => statesFor(got, childThread).includes("idle")));
+ await new Promise((r) => setTimeout(r, 100));
+ assert.deepEqual(statesFor(got, parentThread), ["working", "awaiting_approval", "awaiting_approval"]);
+});
+
+test("a parent whose own turn parks on an approval while its subagent runs settles awaiting at once", async () => {
+ const built = freshApp();
+ const parentThread = "web:U1:parks-mid-tree";
+ const childThread = "web:U1:parks-mid-tree-child";
+ const parent = await built.app.turn(dm("hello", parentThread));
+ const child = await built.app.turn(dm("hello", childThread));
+ await built.sessions.setParentSession(child.sessionId!, parent.sessionId!);
+ const { run } = await built.runs.enqueue({
+ sessionId: childThread,
+ request: resolvedDm("work", childThread),
+ maxAttempts: 3,
+ });
+ const got = record(built.sessionStateBus);
+ const parked = await built.app.turn(dm(`!run ${BLOCKED_CMD}`, parentThread));
+ assert.equal(parked.status, "pending_approval");
+ assert.ok(await waitFor(() => statesFor(got, parentThread).includes("awaiting_approval")));
+ const row = (await built.app.listSessions("U1")).find((s) => s.id === parent.sessionId);
+ assert.equal(row?.awaitingInput, true);
+ assert.ok(!row?.working, "the snapshot ranks the pending approval above subagent work, like the push");
+ const leased = await built.runs.claimById(run.id, "w1", 30_000);
+ await built.runs.complete(run.id, leased!.leaseToken!, { status: "ok", reply: "done" });
+});