Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
5ad53e9
feat(j5): archiving a Crew removes its clean, merged worktrees (#748)
bryantderosier Oct 27, 2026
ea89483
feat(web): Crew archive dialogs list which worktrees go and which sta…
bryantderosier Oct 27, 2026
48f8023
docs(j5): archiving a Crew removes clean, merged worktrees (#748)
bryantderosier Oct 27, 2026
7a9a485
test(j5): provide CrewWorktrees test layers in one call (#748)
bryantderosier Oct 27, 2026
ad19dc7
fix(j5): archiving a Crew keeps a worktree that is a project root or …
bryantderosier Oct 28, 2026
76beb42
fix(j5): archiving a Crew keeps a worktree a seat was unarchived into…
bryantderosier Oct 28, 2026
0cf98aa
test(j5): provide TerminalManager where the Crew worktree check now r…
bryantderosier Oct 28, 2026
2f54f53
merge: bring the updated deferred-assignment layer into the archive-c…
bryantderosier Nov 2, 2026
1f44f7c
fix(j5): only the worktree a seat's own preparation creates is record…
bryantderosier Nov 2, 2026
463a4d1
fix(j5): archiving a Crew closes its seats' provider sessions before …
bryantderosier Nov 2, 2026
bd99df2
merge: bring the updated deferred-assignment layer into the archive-c…
bryantderosier Nov 2, 2026
1624423
merge: archive cleanup fixes from review
bryantderosier Nov 2, 2026
e24dd3b
fix(j5): keep the archive worktree cleanup lint-clean on j5/main's rules
bryantderosier Nov 2, 2026
e48e0e8
test(j5): the archive cleanup mock covers closing sessions
bryantderosier Nov 2, 2026
8212444
merge: pick up the settings seam test fix
bryantderosier Nov 2, 2026
2a66775
merge: pick up the Crew introduction record
bryantderosier Nov 2, 2026
dbaa09f
merge: pick up the CI fixes from 745
bryantderosier Nov 2, 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
72 changes: 69 additions & 3 deletions apps/mobile/src/j5/archive/ArchiveWarningSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import {
ARCHIVE_WARNING_WORKSPACE_NOTE,
archiveWarningAskHeader,
archiveWarningSeatConsequences,
groupWorktreeEstimates,
sentenceEnd,
worktreeReasonLabel,
worktreeShortPath,
type ArchiveWarning,
type ArchiveWarningPayload,
type ArchiveWarningRow,
type ArchiveWarningWorktrees,
} from "@t3tools/client-runtime/j5/archive-flow";
import { useSyncExternalStore } from "react";
import { Modal, Platform, ScrollView, View } from "react-native";
Expand Down Expand Up @@ -84,6 +88,64 @@ function AskRow({ row }: { readonly row: ArchiveWarningRow }) {
);
}

function WorktreeList({
heading,
worktrees,
}: {
readonly heading: string;
readonly worktrees: NonNullable<ArchiveWarningWorktrees>;
}) {
if (worktrees.length === 0) return null;
return (
<View className="gap-1">
<AppText textRole="body" className="font-t3-medium text-foreground">
{heading}
</AppText>
{worktrees.map((worktree) => (
<View key={`${worktree.crewInstanceId}:${worktree.seat}`} className="flex-row gap-2">
<AppText textRole="caption" className="uppercase text-foreground-muted">
{worktree.seat}
</AppText>
<AppText textRole="footnote" className="min-w-0 shrink text-foreground">
{worktreeShortPath(worktree.path)}
</AppText>
{worktree.reason === null ? null : (
<AppText textRole="label" className="ml-auto text-foreground-muted">
{worktreeReasonLabel[worktree.reason]}
</AppText>
)}
</View>
))}
</View>
);
}

/** Which worktrees J5 made for the Crews' seats archiving would remove, and which stay. */
function WorktreeSection({ worktrees }: { readonly worktrees: ArchiveWarningWorktrees }) {
if (worktrees === null)
return (
<AppText textRole="footnote" className="text-foreground-muted">
Worktrees: couldn't check. Branches and pull requests remain.
</AppText>
);
const { removed, kept } = groupWorktreeEstimates(worktrees);
return (
<View className="gap-2">
<WorktreeList
heading="Worktrees removed, if still clean and merged at archive time:"
worktrees={removed}
/>
<WorktreeList heading="Worktrees kept:" worktrees={kept} />
<AppText textRole="footnote" className="text-foreground-muted">
{worktrees.length === 0
? "No worktrees were created for these seats. "
: "This is an estimate; each is checked again when you archive. "}
Branches and pull requests remain.
</AppText>
</View>
);
}

function ArchiveWarningFacts({ payload }: { readonly payload: ArchiveWarningPayload }) {
const crews = payload.crews?.state === "known" ? payload.crews.crews : [];
const asks = asksByUrgency(payload.openAsks);
Expand Down Expand Up @@ -160,9 +222,13 @@ function ArchiveWarningFacts({ payload }: { readonly payload: ArchiveWarningPayl
))}
</View>
) : null}
<AppText textRole="footnote" className="text-foreground-muted">
{ARCHIVE_WARNING_WORKSPACE_NOTE}
</AppText>
{payload.worktrees === undefined ? (
<AppText textRole="footnote" className="text-foreground-muted">
{ARCHIVE_WARNING_WORKSPACE_NOTE}
</AppText>
) : (
<WorktreeSection worktrees={payload.worktrees} />
)}
</View>
);
}
Expand Down
137 changes: 136 additions & 1 deletion apps/server/src/j5/a2a/ArchiveCrewService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert, it } from "@effect/vitest";
import { CommandId, RunId, ThreadId } from "@t3tools/contracts";
import { CommandId, ProviderSessionId, RunId, ThreadId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Ref from "effect/Ref";
Expand All @@ -23,6 +23,8 @@ import {
} from "./ArchiveCrewService.ts";
import { PlaybookStore } from "../playbooks/PlaybookStore.ts";
import { ExchangeId, ParticipantId, LedgerProjectId } from "./contracts.ts";
import { CrewWorktrees } from "./CrewWorktrees.ts";
import type { CrewArchiveWorktree } from "@t3tools/contracts/j5";

const projectId = LedgerProjectId.make("project:j5:archive-crew");
const captain = ParticipantId.make("agent:j5:a2a:captain");
Expand Down Expand Up @@ -89,9 +91,41 @@ const fixture = Effect.gen(function* () {
const crewArchivedAt = yield* Ref.make<string | null>(null);
// The order of cancels and retired stamps, so a test can see the run ends before the stamp.
const lifecycle = yield* Ref.make<ReadonlyArray<string>>([]);
// 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>>([]);
// 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.
const sessionSteps = yield* Ref.make<ReadonlyArray<string>>([]);
const openSessions = new Map<ThreadId, ProviderSessionId>([
[ThreadId.make("t:b"), ProviderSessionId.make("session:b")],
]);
const step = (name: string) => Ref.update(sessionSteps, (items) => [...items, name]);
const layer = archiveCrewLayer.pipe(
Layer.provide(
Layer.mergeAll(
Layer.mock(CrewWorktrees)({
liveSessions: (seats) =>
Effect.forEach(seats, (seat) => step(`read:${seat.threadId}`)).pipe(
Effect.as(
seats.flatMap((seat) => {
const providerSessionId = openSessions.get(seat.threadId);
return providerSessionId === undefined
? []
: [{ threadId: seat.threadId, providerSessionId }];
}),
),
),
closeSessions: (sessions) =>
Effect.forEach(sessions, (session) => step(`close:${session.providerSessionId}`)),
cleanup: ({ seats }) =>
Effect.all([
step("cleanup"),
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))),
}),
Layer.mock(ArchiveAgentService)({
readFacts: (target) =>
Effect.gen(function* () {
Expand All @@ -105,6 +139,7 @@ const fixture = Effect.gen(function* () {
}),
archive: (input) =>
Effect.gen(function* () {
yield* step(`archive:${input.target.threadId}`);
// Mirrors the real service: a retired member replays as already archived.
if ((yield* Ref.get(facts))[input.target.participantId]?.retired)
return "already_archived" as const;
Expand Down Expand Up @@ -180,6 +215,10 @@ const fixture = Effect.gen(function* () {
facts,
lifecycle,
crewArchivedAt,
cleanedSeats,
cleanupResult,
lifecycleAtCleanup,
sessionSteps,
};
});

Expand Down Expand Up @@ -225,6 +264,7 @@ it.effect("refuses until the captain confirms every member's facts, then retires
{ seatName: "builder", participantId: builder, result: "archived" },
{ seatName: "critic", participantId: critic, result: "archived" },
],
worktrees: [],
});
const calls = yield* Ref.get(archived);
assert.deepStrictEqual(
Expand Down Expand Up @@ -422,6 +462,7 @@ it.effect("retires the unit past a seat whose thread was never created, and says
{ seatName: "builder", participantId: builder, result: "archived" },
{ seatName: "critic", participantId: critic, result: "never_created" },
],
worktrees: [],
});
assert.deepStrictEqual(
(yield* Ref.get(archived)).map((call) => call.target.participantId),
Expand Down Expand Up @@ -463,3 +504,97 @@ it.effect(
}).pipe(Effect.provide(layer));
}),
);

it.effect(
"cleans up worktrees once every seat is archived and the Crew is stamped, and reports the result",
() =>
Effect.gen(function* () {
const { layer, input, lifecycleAtCleanup, cleanedSeats, cleanupResult } = yield* fixture;
const reported: ReadonlyArray<CrewArchiveWorktree> = [
{ path: "/wt/builder", branch: "j5/builder", outcome: "removed", reason: null },
{ path: "/wt/critic", branch: "j5/critic", outcome: "kept", reason: "unmerged_commits" },
];
yield* Ref.set(cleanupResult, reported);
yield* Effect.gen(function* () {
const service = yield* ArchiveCrewService;
const outcome = yield* service.archive(
input({ callerParticipantId: null, confirmationSatisfied: true }),
);
assert.deepStrictEqual(outcome.worktrees, reported);
assert.deepStrictEqual(yield* Ref.get(cleanedSeats), [
[ThreadId.make("t:b"), ThreadId.make("t:c")],
]);
// The archive is committed, and the Crew stamped, before anything is removed.
assert.include(yield* Ref.get(lifecycleAtCleanup), `marked:${instance.id}`);
}).pipe(Effect.provide(layer));
}),
);

it.effect("closes each seat's session after its archive and before the worktree cleanup", () =>
Effect.gen(function* () {
const { layer, input, sessionSteps } = yield* fixture;
yield* Effect.gen(function* () {
const service = yield* ArchiveCrewService;
yield* service.archive(input({ callerParticipantId: null, confirmationSatisfied: true }));
// The session is read before the seat's archive removes its binding, closed once every
// seat is archived, and the cleanup comes last.
assert.deepStrictEqual(yield* Ref.get(sessionSteps), [
"read:t:b",
"archive:t:b",
"read:t:c",
"archive:t:c",
"close:session:b",
"cleanup",
]);
}).pipe(Effect.provide(layer));
}),
);

it.effect("a replay of an archived Crew checks its worktrees again", () =>
Effect.gen(function* () {
const { layer, input, cleanedSeats, cleanupResult } = 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 }),
);
assert.equal(replay.status, "already_archived");
assert.deepStrictEqual(
replay.worktrees.map((entry) => entry.outcome),
["removed"],
);
assert.lengthOf(yield* Ref.get(cleanedSeats), 2);
}).pipe(Effect.provide(layer));
}),
);

it.effect("a seat that never came to exist is not looked at for a worktree", () =>
Effect.gen(function* () {
const { layer, input, facts, cleanedSeats } = yield* fixture;
yield* Ref.update(facts, (current) => ({ ...current, [critic]: null }));
yield* Effect.gen(function* () {
const service = yield* ArchiveCrewService;
yield* service.archive(input({ callerParticipantId: null, confirmationSatisfied: true }));
assert.deepStrictEqual(yield* Ref.get(cleanedSeats), [[ThreadId.make("t:b")]]);
}).pipe(Effect.provide(layer));
}),
);

it.effect("a failed archive removes nothing", () =>
Effect.gen(function* () {
const { layer, input, failSeat, cleanedSeats } = yield* fixture;
yield* Ref.set(failSeat, critic);
yield* Effect.gen(function* () {
const service = yield* ArchiveCrewService;
const failure = yield* service
.archive(input({ callerParticipantId: null, confirmationSatisfied: true }))
.pipe(Effect.flip);
assert.instanceOf(failure, ArchiveCrewPartialFailureError);
assert.lengthOf(yield* Ref.get(cleanedSeats), 0);
}).pipe(Effect.provide(layer));
}),
);
Loading
Loading