Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d587056
feat(j5): one Inbox approval for the worktrees a Crew archive keeps w…
bryantderosier Oct 28, 2026
9eb1404
feat(client-runtime): read and answer the worktree cleanup approval
bryantderosier Oct 28, 2026
295d76e
docs(j5): the worktree cleanup approval in the Inbox, archive flow an…
bryantderosier Oct 28, 2026
70a09b8
feat(web): answer the worktree cleanup approval from the Inbox
bryantderosier Oct 28, 2026
79ec5a5
docs(j5): the worktree cleanup item as the Inbox shows it
bryantderosier Oct 28, 2026
16cb782
fix(j5): the removal recheck compares file contents, and a Crew files…
bryantderosier Oct 28, 2026
8b35ad6
merge: bring the updated pinned-worktree layer into the cleanup-appro…
bryantderosier Nov 2, 2026
2a7be74
fix(j5): a Crew archived again after it came back files its own clean…
bryantderosier Nov 2, 2026
cabe715
fix(j5): a worktree whose commits its branch does not hold is never o…
bryantderosier Nov 2, 2026
d700a5c
fix(j5): the removal recheck notices newly staged content under uncha…
bryantderosier Nov 2, 2026
a2c7585
fix(j5): only an Owner is offered, counted and listed a worktree cleanup
bryantderosier Nov 2, 2026
5e5e385
docs(j5): worktree cleanup items per archive, withdrawn on restore, O…
bryantderosier Nov 2, 2026
bfdd4e3
merge: bring the updated pinned-worktree layer into the cleanup-appro…
bryantderosier Nov 2, 2026
da0c53d
merge: worktree cleanup fixes from review
bryantderosier Nov 2, 2026
17c8d1b
fix(j5): keep the cleanup approval lint-clean on j5/main's rules
bryantderosier Nov 2, 2026
50b7ece
merge: pick up the settings seam test fix
bryantderosier Nov 2, 2026
9795ece
merge: pick up the Crew introduction record
bryantderosier Nov 2, 2026
4ea2469
fix(j5): the cleanup section compiles, and symbols only their own fil…
bryantderosier Nov 2, 2026
55221de
merge: pick up the CI fixes from 752
bryantderosier Nov 2, 2026
7183196
style(j5): format the launch service after its error class stopped be…
bryantderosier Nov 2, 2026
a846436
Merge remote-tracking branch 'origin/j5/752-pin-worktree-base-commit'…
bryantderosier Nov 3, 2026
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
18 changes: 15 additions & 3 deletions apps/server/src/j5/a2a/ArchiveCrewService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const fixture = Effect.gen(function* () {
// What cleanup was asked about, and what it reports back.
const cleanedSeats = yield* Ref.make<ReadonlyArray<ReadonlyArray<ThreadId>>>([]);
const cleanupResult = yield* Ref.make<ReadonlyArray<CrewArchiveWorktree>>([]);
const approvalStamps = yield* Ref.make<ReadonlyArray<string | undefined>>([]);
// The retired stamps and playbook cancels that had already happened when cleanup was asked.
const lifecycleAtCleanup = yield* Ref.make<ReadonlyArray<string>>([]);
// The order of session reads, seat archives, session closes and cleanup passes.
Expand All @@ -119,9 +120,10 @@ const fixture = Effect.gen(function* () {
),
closeSessions: (sessions) =>
Effect.forEach(sessions, (session) => step(`close:${session.providerSessionId}`)),
cleanup: ({ seats }) =>
cleanup: ({ seats, crew }) =>
Effect.all([
step("cleanup"),
Ref.update(approvalStamps, (items) => [...items, crew?.archivedAt]),
Ref.update(cleanedSeats, (items) => [...items, seats.map((seat) => seat.threadId)]),
Ref.get(lifecycle).pipe(Effect.flatMap((seen) => Ref.set(lifecycleAtCleanup, seen))),
]).pipe(Effect.andThen(Ref.get(cleanupResult))),
Expand Down Expand Up @@ -217,6 +219,7 @@ const fixture = Effect.gen(function* () {
crewArchivedAt,
cleanedSeats,
cleanupResult,
approvalStamps,
lifecycleAtCleanup,
sessionSteps,
};
Expand Down Expand Up @@ -552,17 +555,26 @@ it.effect("closes each seat's session after its archive and before the worktree

it.effect("a replay of an archived Crew checks its worktrees again", () =>
Effect.gen(function* () {
const { layer, input, cleanedSeats, cleanupResult } = yield* fixture;
const { layer, input, cleanedSeats, cleanupResult, approvalStamps } = yield* fixture;
yield* Effect.gen(function* () {
const service = yield* ArchiveCrewService;
yield* service.archive(input({ callerParticipantId: null, confirmationSatisfied: true }));
yield* Ref.set(cleanupResult, [
{ path: "/wt/critic", branch: "j5/critic", outcome: "removed", reason: null },
]);
const replay = yield* service.archive(
input({ callerParticipantId: null, confirmationSatisfied: true }),
input({
callerParticipantId: null,
confirmationSatisfied: true,
archivedAt: "2026-09-10T08:00:00.000Z",
}),
);
assert.equal(replay.status, "already_archived");
// The replay is the same archive, so an approval it would file carries the original stamp.
assert.deepStrictEqual(yield* Ref.get(approvalStamps), [
"2026-09-09T17:00:00.000Z",
"2026-09-09T17:00:00.000Z",
]);
assert.deepStrictEqual(
replay.worktrees.map((entry) => entry.outcome),
["removed"],
Expand Down
15 changes: 13 additions & 2 deletions apps/server/src/j5/a2a/ArchiveCrewService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ const createdSeats = (facts: ArchiveCrewConsequenceFacts) =>
.filter((member) => !member.neverCreated)
.map((member) => ({ seatName: member.seatName, threadId: member.threadId }));

/** Who the worktree approval is filed for. */
const approvalCrew = (instance: AgentCrewInstance, archivedAt: string) => ({
crewInstanceId: instance.id,
archivedAt: instance.archivedAt ?? archivedAt,
crewName: instance.displayName,
projectId: instance.projectId,
captainThreadId: instance.captainThreadId,
});

const hasConsequences = (facts: ArchiveCrewConsequenceFacts) =>
facts.members.some(
(member) => member.facts.openExchanges.length > 0 || member.facts.runningTurn !== null,
Expand Down Expand Up @@ -407,6 +416,7 @@ export const layer = Layer.effect(
: ("already_archived" as const),
})),
seats: createdSeats(facts),
crew: approvalCrew(instance, input.archivedAt),
sessions: [] as ReadonlyArray<CrewSeatSession>,
};
}
Expand Down Expand Up @@ -493,6 +503,7 @@ export const layer = Layer.effect(
status: "archived" as const,
members: results,
seats: createdSeats(facts),
crew: approvalCrew(instance, input.archivedAt),
sessions,
};
},
Expand All @@ -504,10 +515,10 @@ export const layer = Layer.effect(
// open, or because a check failed, gets another look when the archive is asked for again.
const archive: ArchiveCrewServiceShape["archive"] = Effect.fn("j5.a2a.archiveCrew.archive")(
function* (input) {
const { seats, sessions, ...outcome } = yield* archiveUnit(input);
const { seats, sessions, crew, ...outcome } = yield* archiveUnit(input);
// Each seat's session is closed before the cleanup looks for one open in its worktree.
yield* worktrees.closeSessions(sessions);
const removed = yield* worktrees.cleanup({ seats });
const removed = yield* worktrees.cleanup({ seats, crew });
return { ...outcome, worktrees: removed };
},
);
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/j5/a2a/ClientActionsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
type RespondToCrewRuntimeRequestInput,
} from "./CrewRuntimeRequestService.ts";
import { CrewStopNotFoundError, CrewStopService, type StopCrewInput } from "./CrewStopService.ts";
import { CrewWorktrees } from "./CrewWorktrees.ts";
import { A2ADeliveryWorker } from "./DeliveryWorker.ts";
import { A2AHumanInbox } from "./HumanInboxService.ts";

Expand Down Expand Up @@ -169,6 +170,7 @@ const serviceWith = (seen: ReturnType<typeof calls>) =>
seen.notified += 1;
}),
}),
Layer.mock(CrewWorktrees)({}),
),
),
Layer.provide(NodeServices.layer),
Expand Down
16 changes: 16 additions & 0 deletions apps/server/src/j5/a2a/ClientActionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
AnswerHumanExchangeResponse,
CrewArchiveRequest,
CrewArchiveResponse,
CrewWorktreeCleanupAnswerRequest,
CrewWorktreeCleanupAnswerResponse,
CrewProposalPreviewRequest,
CrewProposalPreviewResponse,
CrewProposalResolveRequest,
Expand All @@ -27,6 +29,7 @@ import { projectCrewProposal } from "./crewProposalProjection.ts";
import { CrewProposalService } from "./CrewProposalService.ts";
import { CrewRuntimeRequestService } from "./CrewRuntimeRequestService.ts";
import { CrewStopService } from "./CrewStopService.ts";
import { CrewWorktrees } from "./CrewWorktrees.ts";
import { A2ADeliveryWorker } from "./DeliveryWorker.ts";
import { A2AHumanInbox } from "./HumanInboxService.ts";
import { crewSeatRequestKey, lifecycleCommandId } from "./spawnIds.ts";
Expand Down Expand Up @@ -98,6 +101,13 @@ export class ClientActionsService extends Context.Service<
typeof AnswerHumanExchangeResponse.Type,
Failure<A2AHumanInbox["Service"]["answer"]>
>;
/** Decide the worktree cleanup an archive filed in the Inbox; refused once decided. */
readonly answerCrewWorktreeCleanup: (
input: CrewWorktreeCleanupAnswerRequest,
) => Effect.Effect<
typeof CrewWorktreeCleanupAnswerResponse.Type,
Failure<CrewWorktrees["Service"]["answerCleanup"]>
>;
}
>()("t3/j5/a2a/ClientActionsService") {}

Expand All @@ -122,6 +132,7 @@ const make = Effect.gen(function* () {
const runtimeRequests = yield* CrewRuntimeRequestService;
const inbox = yield* A2AHumanInbox;
const worker = yield* A2ADeliveryWorker;
const worktrees = yield* CrewWorktrees;
// A random source that fails is the host's fault, not a refusal a client can act on.
const freshId = (yield* Crypto.Crypto).randomUUIDv4().pipe(Effect.orDie);
const projectProposal = projectCrewProposal(yield* PlaybookStore);
Expand Down Expand Up @@ -231,6 +242,11 @@ const make = Effect.gen(function* () {
return { result };
},
),
answerCrewWorktreeCleanup: Effect.fn("j5.a2a.clientActions.answerCrewWorktreeCleanup")(
function* ({ itemId, removePaths }) {
return { item: yield* worktrees.answerCleanup({ itemId, removePaths }) };
},
),
});
});

Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/j5/a2a/ClientReadsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ export interface ClientReadsShape {
ReadonlyArray<CrewArchiveWorktreeRead>,
Read<typeof makeCrewArchiveWorktreesRead>["error"]
>;
/** The Inbox's worktree cleanup approvals, open or decided. */
readonly crewWorktreeCleanups: CrewWorktrees["Service"]["listCleanups"];
}

export class ClientReadsService extends Context.Service<ClientReadsService, ClientReadsShape>()(
Expand Down Expand Up @@ -403,6 +405,7 @@ export const layer: Layer.Layer<
fleet: yield* makeFleetRead,
preArchiveFacts: yield* makePreArchiveFactsRead,
crewArchiveWorktrees: yield* makeCrewArchiveWorktreesRead,
crewWorktreeCleanups: (yield* CrewWorktrees).listCleanups,
});
}),
);
15 changes: 15 additions & 0 deletions apps/server/src/j5/a2a/CrewCaptainArchiveCascade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { OrchestratorProjectionError } from "../../orchestration-v2/Orchestrator
import { ProjectionStoreThreadNotFoundError } from "../../orchestration-v2/ProjectionStore.ts";
import { ThreadManagementService } from "../../orchestration-v2/ThreadManagementService.ts";
import { AgentCrewInstanceService, type AgentCrewInstance } from "./AgentCrewInstanceService.ts";
import { CrewWorktrees } from "./CrewWorktrees.ts";
import { ArchiveCrewService, type ArchiveCrewInput } from "./ArchiveCrewService.ts";
import { CrewCaptainArchiveCascade, layer as cascadeLayer } from "./CrewCaptainArchiveCascade.ts";
import { participantIdForThread } from "./HomeRegistrar.ts";
Expand Down Expand Up @@ -59,13 +60,15 @@ const captainProjection = (archivedAt: string | null, deletedAt: string | null =
},
}) as unknown as OrchestrationV2ThreadProjection;
const noThreads = Layer.mock(ThreadManagementService)({});
const noWorktrees = Layer.mock(CrewWorktrees)({ withdrawCleanup: () => Effect.void });

it.effect("retires the live Crews a Captain commanded when the person archives its thread", () =>
Effect.gen(function* () {
const calls = yield* Ref.make<ReadonlyArray<ArchiveCrewInput>>([]);
const layer = cascadeLayer.pipe(
Layer.provide(
Layer.mergeAll(
noWorktrees,
Layer.mock(AgentCrewInstanceService)({
listInvolving: (input) =>
Effect.succeed(
Expand Down Expand Up @@ -126,6 +129,7 @@ it.effect(
const layer = cascadeLayer.pipe(
Layer.provide(
Layer.mergeAll(
noWorktrees,
Layer.mock(AgentCrewInstanceService)({
listLive: () =>
Effect.succeed([
Expand Down Expand Up @@ -179,6 +183,7 @@ it.effect("a Crew whose retirement fails is tried once, and the next Crew still
const testLayer = cascadeLayer.pipe(
Layer.provide(
Layer.mergeAll(
noWorktrees,
Layer.mock(AgentCrewInstanceService)({
listInvolving: () => Effect.succeed([crew("crew:broken", null), crew("crew:ok", null)]),
}),
Expand Down Expand Up @@ -315,6 +320,7 @@ it.effect("settling a Captain settles its Crew's seats, and unsettling it unsett
const layer = cascadeLayer.pipe(
Layer.provide(
Layer.mergeAll(
noWorktrees,
Layer.mock(AgentCrewInstanceService)({
listInvolving: () =>
Effect.succeed([
Expand Down Expand Up @@ -381,9 +387,13 @@ it.effect("unarchiving a Captain brings back the Crews that retired with it", ()
Effect.gen(function* () {
const dispatched = yield* Ref.make<ReadonlyArray<OrchestrationV2Command>>([]);
const restored = yield* Ref.make<ReadonlyArray<string>>([]);
const withdrawn = yield* Ref.make<ReadonlyArray<string>>([]);
const layer = cascadeLayer.pipe(
Layer.provide(
Layer.mergeAll(
Layer.mock(CrewWorktrees)({
withdrawCleanup: (id) => Ref.update(withdrawn, (items) => [...items, id]),
}),
Layer.mock(AgentCrewInstanceService)({
// The store lists only Crews that retired with this Captain, never one retired alone.
listRetiredWithCaptain: (threadId) =>
Expand Down Expand Up @@ -437,6 +447,8 @@ it.effect("unarchiving a Captain brings back the Crews that retired with it", ()
[["thread.unarchive", seatThread]],
);
assert.deepStrictEqual(yield* Ref.get(restored), ["crew:with-captain"]);
// The Crew is live again, so an open worktree approval for its archive is withdrawn.
assert.deepStrictEqual(yield* Ref.get(withdrawn), ["crew:with-captain"]);
}).pipe(Effect.provide(layer));
}),
);
Expand All @@ -452,6 +464,7 @@ it.effect("a Captain archived again after an unarchive retires its Crew again",
const layer = cascadeLayer.pipe(
Layer.provide(
Layer.mergeAll(
noWorktrees,
Layer.mock(AgentCrewInstanceService)({
listInvolving: () => Effect.map(current, (instance) => [instance]),
listRetiredWithCaptain: () =>
Expand Down Expand Up @@ -529,6 +542,7 @@ it.effect("settling a Captain leaves a seat that is waiting on the person or sti
const layer = cascadeLayer.pipe(
Layer.provide(
Layer.mergeAll(
noWorktrees,
Layer.mock(AgentCrewInstanceService)({ listInvolving: () => Effect.succeed([trio]) }),
Layer.mock(ArchiveCrewService)({}),
Layer.mock(ThreadManagementService)({
Expand Down Expand Up @@ -589,6 +603,7 @@ it.effect(
const layer = cascadeLayer.pipe(
Layer.provide(
Layer.mergeAll(
noWorktrees,
Layer.mock(AgentCrewInstanceService)({
listInvolving: () => Effect.succeed([instance]),
}),
Expand Down
6 changes: 5 additions & 1 deletion apps/server/src/j5/a2a/CrewCaptainArchiveCascade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { ThreadManagementService } from "../../orchestration-v2/ThreadManagement
import { isAutoSettlementCandidate } from "../../orchestration-v2/ThreadSettlementService.ts";
import { AgentCrewInstanceService, type AgentCrewInstance } from "./AgentCrewInstanceService.ts";
import { ArchiveCrewService } from "./ArchiveCrewService.ts";
import { CrewWorktrees } from "./CrewWorktrees.ts";
import { participantIdForThread } from "./HomeRegistrar.ts";
import { crewSeatRequestKey, lifecycleCommandId } from "./spawnIds.ts";
import {
Expand Down Expand Up @@ -75,6 +76,7 @@ export const layer = Layer.effect(
Effect.gen(function* () {
const crews = yield* AgentCrewInstanceService;
const archives = yield* ArchiveCrewService;
const worktrees = yield* CrewWorktrees;
const threads = yield* ThreadManagementService;

const retire = Effect.fn("j5.a2a.captainArchiveCascade.retire")(function* (
Expand Down Expand Up @@ -211,7 +213,9 @@ export const layer = Layer.effect(
threadId: member.threadId,
});
}
yield* crews.restoreWithCaptain(instance.id);
// Its worktrees are in use again: an open approval to remove them is moot.
if (yield* crews.restoreWithCaptain(instance.id))
yield* worktrees.withdrawCleanup(instance.id);
}),
);
yield* Effect.logInfo("J5 Captain unarchived; its Crew came back with it", {
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/j5/a2a/CrewWorktrees.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { CrewWorktrees, manualLayer } from "./CrewWorktrees.ts";
import { LedgerProjectId } from "./contracts.ts";
import { A2ALedger, layer as ledgerLayer } from "./LedgerService.ts";
import { participantIdForThread } from "./HomeRegistrar.ts";
import { ReadChanges } from "./ReadChanges.ts";

it.effect(
"records the worktree upstream's launch creates for a seat's first assignment, from the event it commits",
Expand Down Expand Up @@ -51,6 +52,7 @@ it.effect(
Layer.mock(ProviderSessionManagerV2)({}),
Layer.mock(GitVcsDriver)({}),
Layer.mock(GitManager)({}),
Layer.mock(ReadChanges)({}),
NodeServices.layer,
),
),
Expand Down
Loading
Loading