/** * The cards a session that ended left behind. * * A crashed worker's card otherwise holds a WIP slot until a person notices and reclaims it, and * the fact is already trustworthy: `#396` made `gone` pid-checked, and the reclaim gate reads it. * So the sweep at a turn start does what that gate already allows a peer to do, through the same * operation — the store decides, and a card whose owner turns out to be running is refused and left * where it is. * * One roster read for the whole board: the fact is one directory of records, so it is read once and * asked per owner rather than a directory read per card. `quiet` is deliberately not swept — a long * turn looks exactly like it — and that decision stays a person's, as `#396` left it. * * One verb, because one fact is what a sweep has: the owner's record reads `gone`. A name is not an * identity — two sessions wear one in a row — so nothing here reads a label to decide whose a card * is; the owner id `holds` compares is the whole question (#918). */ import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; import { cardRef, holds } from "../../lib/workflow/card.ts"; import { LANE_RULES } from "../../lib/workflow/lanes.ts"; import type { BoardSession } from "../../lib/workflow/session.ts"; import { listOf } from "../../lib/format.ts"; import { presenceOf } from "../../lib/session-roster.ts"; /** What a sweep needs of the session, so a suite drives it without a bus. */ export type SweepSession = Pick; /** `a and b`, never `a, and b`: a bullet in the first turn is one sentence, and two is the common case. */ function listed(items: readonly string[]): string { return items.length === 2 ? items.join(" and ") : listOf(items, "and"); } /** * Hand a stopped session's cards back to the pool, and say which. * * The owner's record is the fact, and it is the bus's: a card whose owner stopped is one nobody is * holding, so it goes back to the pool — which is what the reclaim gate already lets a peer do. * A card this session holds is not its to release, and the id `holds` compares is the one that * answers that: a `/resume` keeps the id it carries, and a `/new` keeps the record (#915), so the * session that just came back already owns its own cards. * * Refusals are skipped rather than reported: a refusal is a race (another session swept the same * card) or a record that changed between the read and the write, and the store's locked * read-check-write has already decided. Only a write that changed something is counted, so the * clause names exactly what this session wrote. */ export async function releaseEnded(session: SweepSession, ctx: ExtensionContext): Promise { try { const me = await session.who(ctx, ctx.cwd); const store = await session.store(ctx.cwd); const roster = await session.roster(ctx.cwd); // No bus is the single-session board: nobody else owns a card, so there is nothing to sweep. if (roster === undefined) return undefined; const { cards } = await store.scan(); const now = session.now(); const released: string[] = []; for (const card of cards) { if (card.owner === undefined || LANE_RULES[card.lane].terminal) continue; // A card this session holds is not its to release. A `/resume` that kept the id is already the // holder by it, and the record the crashed process left behind is gone by pid — so this sweep, // which runs right after the re-claim of those same paths, would take back what it just resumed. if (holds(card, me)) continue; const state = presenceOf(roster, card.owner, now); if (state !== "gone") continue; const holder = { owner: card.owner, state }; const result = await store .mutate(card.id, { kind: "reclaim", holder, swept: true }, me, now) .catch(() => undefined); if (result?.ok !== true || !result.changed) continue; released.push(cardRef(card.id)); } if (released.length === 0) return undefined; // `a and b`, never `a, and b`: a bullet in the first turn is one sentence, and two cards is // the common case. The clause is the same shape `stray-fields` uses for the same reason. const from = released.length === 1 ? "a session that ended" : "sessions that ended"; return `released ${listed(released)} from ${from}`; } catch { // A board that cannot be read is not a reason to fail the turn it was read for. return undefined; } }