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 @@ -40,6 +40,8 @@ import { readAntigravityClientTextFile } from "../../provider/acp/AntigravityCli
import { watchAntigravityPromptOutcome } from "../../provider/acp/AntigravityTurnOutcome.ts";
import {
antigravityApprovalOptions,
antigravitySwarmAddChildSessionId,
antigravitySwarmAddModel,
antigravityToolOutlivesPrompt,
antigravityCommandOutlivesPrompt,
antigravitySubagentOutput,
Expand Down Expand Up @@ -130,9 +132,9 @@ const extractAntigravitySubagentUpdate: NonNullable<AcpAdapterV2Flavor["extractS
toolCall.title ??
ANTIGRAVITY_SUBAGENT_BATCH_TITLE,
title: launch?.title ?? ANTIGRAVITY_SUBAGENT_BATCH_TITLE,
model: null,
model: antigravitySwarmAddModel(toolCall),
status,
childSessionId: null,
childSessionId: antigravitySwarmAddChildSessionId(toolCall),
result: status === "failed" ? (antigravitySubagentOutput(toolCall) ?? null) : null,
};
};
Expand Down
105 changes: 105 additions & 0 deletions apps/server/src/provider/acp/AntigravityChildWork.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,111 @@ it.layer(NodeServices.layer)("readAntigravityChildWork", (it) => {
}),
);

it.effect(
"reads legacy disk-backed swarm worker transcripts and tracks working vs parked or killed members",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const profile = yield* fs.makeTempDirectoryScoped({ prefix: "t3-agy-swarm-disk-" });
const workerOne = "11111111-1111-4000-8000-111111111111";
const workerTwo = "22222222-2222-4000-8000-222222222222";
const workerKilled = "33333333-3333-4000-8000-333333333333";
const writeFile = (segments: ReadonlyArray<string>, body: string) => {
const file = path.join(profile, "antigravity-acp", "brain", ...segments);
return fs
.makeDirectory(path.dirname(file), { recursive: true })
.pipe(Effect.andThen(fs.writeFileString(file, body)));
};
const writeWorker = (
convId: string,
name: string,
callId: string,
args: Record<string, unknown>,
reply: string,
) =>
Effect.all([
writeFile(
[ROOT, ".system_generated", "subagents", `${convId}.json`],
JSON.stringify({
conversationId: convId,
subagentDescriptor: { typeName: "self", role: `Swarm Worker (${name})` },
state: "SUBAGENT_STATE_ALIVE",
}),
),
writeFile(
[convId, ".system_generated", "messages", "launch.json"],
JSON.stringify({
sender: ROOT,
content: `Task for ${name}`,
sourceMetadata: {
tool: {
conversationId: ROOT,
toolCall: { id: callId, name: "swarm", argumentsJson: JSON.stringify(args) },
},
},
}),
),
writeFile(
[convId, ".system_generated", "logs", "transcript.jsonl"],
`${JSON.stringify({ step_index: 0, type: "PLANNER_RESPONSE", status: "DONE", content: reply })}\n`,
),
]);

const batchArgs = {
action: "add",
name: "shard",
brief: "Inspect shard",
tasks: ["First shard", "Second shard"],
};
yield* writeWorker(workerOne, "shard-1", "call_batch", batchArgs, "shard-1 finished");
yield* writeWorker(workerTwo, "shard-2", "call_batch", batchArgs, "shard-2 working");
yield* writeWorker(
workerKilled,
"retired",
"call_retired",
{ action: "add", name: "retired", brief: "Old task" },
"stopped",
);
yield* writeFile(
[ROOT, ".system_generated", "swarm", "members.json"],
JSON.stringify({
title: "Shard audit",
members: {
coordinator: { conversation_id: ROOT, role: "coordinator", wake_mode: "mentions" },
"shard-1": { conversation_id: workerOne, role: "worker", wake_mode: "mentions" },
"shard-2": { conversation_id: workerTwo, role: "worker", wake_mode: "working" },
retired: {
conversation_id: workerKilled,
role: "worker",
desired_state: "KILLED",
phase: "KILLED",
wake_mode: "working",
},
},
}),
);

// shard-1 is parked on mentions and retired is killed; only shard-2 keeps call_batch active.
expect(
yield* readAntigravityChildrenAtWork({
profileDirectory: profile,
rootSessionId: ROOT,
toolCallIds: ["call_batch", "call_retired"],
}),
).toEqual(["call_batch"]);

const batchChildren = yield* read(profile, "call_batch");
expect(batchChildren.map((child) => [child.role, child.prompt])).toEqual([
["@shard-1", "Inspect shard\n\nFirst shard"],
["@shard-2", "Inspect shard\n\nSecond shard"],
]);
expect(batchChildren[0]?.steps?.[0]?.text).toBe("shard-1 finished");

expect(antigravitySubagentLaunches({ action: "add", name: "w", n: 1e9 })).toHaveLength(64);
}),
);

it.effect("shares one transcript budget across the batch", () =>
Effect.gen(function* () {
const filler = JSON.stringify({
Expand Down
170 changes: 157 additions & 13 deletions apps/server/src/provider/acp/AntigravityChildWork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,72 @@ interface AntigravityChildLaunch {
}

const LaunchInput = Schema.Struct({ Role: Schema.String, Prompt: Schema.String });
const SwarmAddInput = Schema.Struct({
action: Schema.String,
name: Schema.optional(Schema.String),
role: Schema.optional(Schema.String),
brief: Schema.optional(Schema.String),
n: Schema.optional(Schema.Number),
tasks: Schema.optional(Schema.Array(Schema.String)),
});
// agy 1.5 launches one child per call with a flat input; earlier releases send a batch.
const decodeLaunchInput = Schema.decodeUnknownOption(
Schema.Union([Schema.Struct({ Subagents: Schema.NonEmptyArray(LaunchInput) }), LaunchInput]),
);
const decodeSwarmAddInput = Schema.decodeUnknownOption(SwarmAddInput);

// The tool input is untrusted; a huge `n` must not allocate a launch per requested worker.
const MAX_SWARM_ADD_LAUNCHES = 64;

function swarmAddLaunches(input: unknown): ReadonlyArray<AntigravityChildLaunch> | undefined {
const decoded = Option.getOrUndefined(decodeSwarmAddInput(input));
if (decoded === undefined || decoded.action.trim().toLowerCase() !== "add") return undefined;
const baseName = decoded.name?.trim().replace(/^@/, "") ?? "";
const brief = decoded.brief?.trim() ?? "";
const tasks = decoded.tasks ?? [];
const requestedCount =
decoded.n !== undefined && Number.isFinite(decoded.n) ? Math.trunc(decoded.n) : 0;
const count = Math.min(Math.max(tasks.length, requestedCount, 1), MAX_SWARM_ADD_LAUNCHES);
const roleSuffix =
decoded.role !== undefined && decoded.role.trim().length > 0 && decoded.role !== "worker"
? ` (${decoded.role.trim()})`
: "";

if (count === 1) {
const task = tasks[0]?.trim() ?? "";
const prompt = brief.length > 0 && task.length > 0 ? `${brief}\n\n${task}` : brief || task;
return [
{
role: baseName.length > 0 ? `@${baseName}${roleSuffix}` : `Swarm worker${roleSuffix}`,
prompt,
},
];
}

return Array.from({ length: count }, (_, index) => {
const task = tasks[index]?.trim() ?? "";
const prompt = brief.length > 0 && task.length > 0 ? `${brief}\n\n${task}` : task || brief;
const role =
baseName.length > 0
? `@${baseName}-${index + 1}${roleSuffix}`
: `Swarm worker ${index + 1}${roleSuffix}`;
return { role, prompt };
});
}

/** `invoke_subagent` arguments, from the permission prompt or the persisted record. */
/** `invoke_subagent` or native `swarm` (`action: "add"`) arguments. */
export function antigravitySubagentLaunches(
input: unknown,
): ReadonlyArray<AntigravityChildLaunch> | undefined {
return Option.getOrUndefined(
const classic = Option.getOrUndefined(
Option.map(decodeLaunchInput(input), (decoded) =>
("Subagents" in decoded ? decoded.Subagents : [decoded]).map((entry) => ({
role: entry.Role,
prompt: entry.Prompt,
})),
),
);
return classic ?? swarmAddLaunches(input);
}

/** Batch title and prompt; reads match permission-prompt tool call ids against this exact text. */
Expand Down Expand Up @@ -580,11 +629,71 @@ function detachedLaunchCall(message: ChildMessage, rootSessionId: string) {
const call = tool?.toolCall;
return message.sender === rootSessionId &&
(tool?.conversationId === undefined || tool.conversationId === rootSessionId) &&
call?.name === "invoke_subagent"
(call?.name === "invoke_subagent" || call?.name === "swarm")
? call
: undefined;
}

function swarmRoleLabel(descriptorRole: string | undefined): string | undefined {
const trimmed = descriptorRole?.trim() ?? "";
const match = /^Swarm Worker \((.+)\)$/.exec(trimmed);
return match?.[1] !== undefined ? `@${match[1].trim()}` : undefined;
}

const decodeSwarmRoster = Schema.decodeUnknownOption(
Schema.fromJsonString(
Schema.Struct({
members: Schema.optional(
Schema.Record(
Schema.String,
Schema.Struct({
conversation_id: Schema.optional(Schema.String),
role: Schema.optional(Schema.String),
desired_state: Schema.optional(Schema.String),
phase: Schema.optional(Schema.String),
wake_mode: Schema.optional(Schema.String),
}),
),
),
}),
),
);

interface DiskSwarmMember {
readonly name: string;
readonly role: string;
readonly killed: boolean;
readonly parked: boolean;
}

async function readDiskSwarmMembersByConversationId(
profileRoot: string,
rootSessionId: string,
): Promise<ReadonlyMap<string, DiskSwarmMember>> {
const file = await antigravityManagedPath(
profileRoot,
["antigravity-acp", "brain", rootSessionId, ".system_generated", "swarm", "members.json"],
"file",
);
if (file === undefined || file.info.size > MAX_STEP_BYTES) return new Map();
const raw = await NodeFSP.readFile(file.path, "utf8").catch(() => undefined);
if (raw === undefined) return new Map();
const decoded = Option.getOrUndefined(decodeSwarmRoster(raw));
const members = decoded?.members;
if (members === undefined) return new Map();
const byConversation = new Map<string, DiskSwarmMember>();
for (const [name, member] of Object.entries(members)) {
const convId = member.conversation_id;
if (convId === undefined || !NATIVE_ID.test(convId)) continue;
const role = member.role ?? "worker";
if (role === "coordinator") continue;
const killed = member.desired_state === "KILLED" || member.phase === "KILLED";
const parked = member.wake_mode === "mentions" || member.wake_mode === "anything";
byConversation.set(convId, { name, role, killed, parked });
}
return byConversation;
}

async function readDetachedSubagent(profileRoot: string, rootSessionId: string, childId: string) {
const file = await antigravityManagedPath(
profileRoot,
Expand Down Expand Up @@ -619,7 +728,7 @@ async function findDetachedInvokeRecord(
): Promise<InvokeRecord | undefined> {
const childIds = await listDetachedChildIds(profileRoot, rootSessionId);
if (childIds === undefined || childIds.length === 0) return undefined;
const matches: Array<InvokeRecord> = [];
const matches: Array<InvokeRecord & { readonly isSwarm: boolean }> = [];
for (const childId of childIds) {
const messages = await readInbox(profileRoot, childId).catch(() => undefined);
// An unreadable or oversized sibling inbox could also claim this id, so uniqueness is unproven.
Expand All @@ -630,25 +739,50 @@ async function findDetachedInvokeRecord(
if (launchMessage === undefined) continue;
const subagent = await readDetachedSubagent(profileRoot, rootSessionId, childId);
if (subagent === undefined) return undefined;
const argumentsJson = launchMessage.sourceMetadata?.tool?.toolCall?.argumentsJson;
const call = launchMessage.sourceMetadata?.tool?.toolCall;
const isSwarm = call?.name === "swarm";
const argumentsJson = call?.argumentsJson;
const parsedArgs =
argumentsJson === undefined
? undefined
: Option.getOrUndefined(decodeArgument(argumentsJson));
const [fromArgs] = antigravitySubagentLaunches(parsedArgs) ?? [];
const argLaunches = antigravitySubagentLaunches(parsedArgs);
const descriptorLabel = swarmRoleLabel(subagent.subagentDescriptor?.role);
const matchedLaunch =
isSwarm && descriptorLabel !== undefined
? (argLaunches?.find((entry) => entry.role.startsWith(descriptorLabel)) ?? {
role: descriptorLabel,
prompt: launchMessage.content ?? argLaunches?.[0]?.prompt ?? "",
})
: argLaunches?.[0];
matches.push({
isSwarm,
toolCallId: nativeTaskId,
launches: [
fromArgs ?? {
role: subagent.subagentDescriptor?.role ?? "",
matchedLaunch ?? {
role: descriptorLabel ?? subagent.subagentDescriptor?.role ?? "",
prompt: launchMessage.content ?? "",
},
],
children: [{ conversationId: childId }],
});
}
const [match] = matches;
return matches.length === 1 ? match : undefined;
if (matches.length === 1) {
return matches[0];
}
if (matches.length > 1 && matches.every((entry) => entry.isSwarm)) {
const ordered = [...matches].sort((left, right) =>
(left.launches[0]?.role ?? "").localeCompare(right.launches[0]?.role ?? "", undefined, {
numeric: true,
}),
);
return {
toolCallId: nativeTaskId,
launches: ordered.flatMap((entry) => entry.launches),
children: ordered.flatMap((entry) => entry.children),
};
}
return undefined;
}

const PENDING_REPLY_TAIL_BYTES = 64 * 1024;
Expand Down Expand Up @@ -799,20 +933,30 @@ export const readAntigravityChildrenAtWork = Effect.fn("readAntigravityChildrenA
const reported = new Set(
(await readInbox(profileRoot, input.rootSessionId)).map((message) => message.sender),
);
const atWork: Array<string> = [];
const swarmMembers = await readDiskSwarmMembersByConversationId(
profileRoot,
input.rootSessionId,
);
const atWork = new Set<string>();
for (const childId of childIds) {
if (reported.has(childId)) continue;
const swarmMember = swarmMembers.get(childId);
if (swarmMember?.killed === true || swarmMember?.parked === true) continue;
// `manage_subagents` kill only marks the record; a killed child never reports.
const subagent = await readDetachedSubagent(profileRoot, input.rootSessionId, childId);
if (subagent?.state === "SUBAGENT_STATE_KILLED") continue;
const directTaskId = `swarm:${childId}`;
if (input.toolCallIds.includes(directTaskId)) {
atWork.add(directTaskId);
}
for (const message of await readInbox(profileRoot, childId)) {
const launch = detachedLaunchCall(message, input.rootSessionId);
if (launch !== undefined && input.toolCallIds.includes(launch.id)) {
atWork.push(launch.id);
atWork.add(launch.id);
}
}
}
return atWork;
return [...atWork];
};
return yield* Effect.tryPromise({
try: read,
Expand Down
Loading
Loading