diff --git a/apps/server/src/links/LinkMoves.test.ts b/apps/server/src/links/LinkMoves.test.ts index 53bbe56dc..f5901dce0 100644 --- a/apps/server/src/links/LinkMoves.test.ts +++ b/apps/server/src/links/LinkMoves.test.ts @@ -400,7 +400,7 @@ const pack = ( state?: PeerMoveThreadState, room?: Pick< MoveExportInput, - "room" | "outsideWork" | "family" | "linkLineage" | "startedThere" + "room" | "outsideWork" | "family" | "linkLineage" | "startedThere" | "startedElsewhere" > & { /** What the move says the computer it leaves calls itself. */ readonly sourceEnvironmentId?: EnvironmentId | undefined; @@ -424,6 +424,7 @@ const pack = ( family: room?.family, linkLineage: room?.linkLineage, startedThere: room?.startedThere, + startedElsewhere: room?.startedElsewhere, attachmentsDir: path.join(repos.root, "attachments"), thread: room?.thread ?? movingThread(), room: room?.room, @@ -2076,6 +2077,40 @@ describe("a thread moving across its link (Part 3f, (h))", () => { ), ); + it.live( + "a parent moving without its family names the threads it started on other computers", + () => { + const repos = makeRepos(); + const dispatched: OrchestrationCommand[] = []; + return Effect.gen(function* () { + const packed = yield* pack(repos, null, undefined, { + startedElsewhere: [ + { label: "Linux box", title: "Fix tests" }, + { label: "Linux box", title: "Docs" }, + { label: "Office PC", title: "Review" }, + ], + }); + const state = yield* deliver(packed.manifest, packed.payload); + assert.equal(state.status, "committed"); + assert.includeMembers( + [...(importOf(dispatched).arrival?.left ?? [])], + [ + "2 threads it started on Linux box stayed there: Fix tests, Docs. It can't reach them from here.", + "1 thread it started on Office PC stayed there: Review. It can't reach it from here.", + ], + ); + }).pipe( + Effect.provide( + testLayer(repos, dispatched, "allowed", () => false, [], { + home: os.tmpdir(), + adopted: [], + linkLabel: "Windows PC", + }), + ), + ); + }, + ); + it.live("a child alone stays its parent's child, its link taken from the link itself", () => { const repos = makeRepos(); const dispatched: OrchestrationCommand[] = []; diff --git a/apps/server/src/links/LinkMoves.ts b/apps/server/src/links/LinkMoves.ts index 25a19ba9f..487e98b58 100644 --- a/apps/server/src/links/LinkMoves.ts +++ b/apps/server/src/links/LinkMoves.ts @@ -303,6 +303,29 @@ const OWNER_DIDNT_MOVE = "The thread whose folder it works in didn't move."; * in another's checkout (docs/design/fleet.md, Part 3f). */ const OTHER_PROJECTS_FOLDER = "The folder it works in belongs to another project."; + +/** + * The "Moved here" words for the threads a parent's agent started on other + * computers, which stay there (docs/design/fleet.md, Part 3f, (g)): one line + * per computer. From a family's `remoteStayed` or a lone parent's + * `startedElsewhere`. Ones this computer names as already here + * (`alreadyHere`, by title) are left to that line: an older computer still + * lists them. + */ +export const startedElsewhereWords = ( + list: ReadonlyArray<{ readonly label: string; readonly title: string }>, + alreadyHere: ReadonlySet, +): string[] => { + const byComputer = new Map(); + for (const entry of list) { + if (alreadyHere.has(entry.title)) continue; + byComputer.set(entry.label, [...(byComputer.get(entry.label) ?? []), entry.title]); + } + return [...byComputer].map( + ([label, titles]) => + `${titles.length === 1 ? "1 thread" : `${titles.length} threads`} it started on ${label} stayed there: ${titles.join(", ")}. It can't reach ${titles.length === 1 ? "it" : "them"} from here.`, + ); +}; /** A repository move's project; a General Chats move names none to share. */ const movedProjectOf = (manifest: PeerMoveManifest) => "canonicalKey" in manifest.project ? manifest.project : null; @@ -377,6 +400,9 @@ export function moveManifestRefusal(manifest: PeerMoveManifest): string | null { if ((manifest.startedThere ?? []).length > PEER_MOVE_MAX_STARTED_THERE) { return "it names too many threads it started here"; } + if ((manifest.startedElsewhere ?? []).length > PEER_MOVE_FAMILY_MAX_STAYED) { + return "it names too many threads it started elsewhere"; + } return ( moveRoomRefusal(manifest) ?? moveFamilyRefusal(manifest) ?? @@ -2300,17 +2326,7 @@ const make = Effect.gen(function* () { `${plural(stayed.length)} it started stayed on ${fromLabel}: ${titles(stayed)}.`, ); } - const byComputer = new Map>(); - for (const entry of (family.remoteStayed ?? []).filter( - (stayed) => !alreadyHere.has(stayed.title), - )) { - byComputer.set(entry.label, [...(byComputer.get(entry.label) ?? []), entry]); - } - for (const [label, list] of byComputer) { - words.push( - `${plural(list.length)} it started on ${label} stayed there: ${titles(list)}. It can't reach ${list.length === 1 ? "it" : "them"} from here.`, - ); - } + words.push(...startedElsewhereWords(family.remoteStayed ?? [], alreadyHere)); const stopped = family.childRequestsStopped ?? 0; if (stopped > 0) { words.push( @@ -2738,7 +2754,12 @@ const make = Effect.gen(function* () { const liveHere = startedHere.filter((thread) => thread.archivedAt === null); const familyLeft = family === null - ? [] + ? // A parent moving without its family names the threads its + // agent started on other computers (`startedElsewhere`). + startedElsewhereWords( + row.manifest.startedElsewhere ?? [], + new Set(liveHere.map((entry) => entry.title)), + ) : family.left( fromLabel, lineage?.left ?? null, diff --git a/apps/server/src/moves/ThreadMoves.test.ts b/apps/server/src/moves/ThreadMoves.test.ts index ead3087dd..d60405ef8 100644 --- a/apps/server/src/moves/ThreadMoves.test.ts +++ b/apps/server/src/moves/ThreadMoves.test.ts @@ -33,7 +33,7 @@ import { sessionsThatFit, } from "./moveExport.ts"; import { carryBringsWords, threadMoveCarry } from "../orchestration/threadMoveCarry.ts"; -import { linkLineageOf, startedThereOf } from "./familyMove.ts"; +import { linkLineageOf, startedElsewhereOf, startedThereOf } from "./familyMove.ts"; import { seenThroughOf } from "./roomMove.ts"; import { NATIVE_SESSION_MAX_BYTES, type NativeSessionExport } from "../provider/nativeSession.ts"; @@ -587,6 +587,10 @@ describe("what a thread carries, as the dialog says it (Part 3e)", () => { { threadId: "thread-wrapped", override: wrapped }, { threadId: "thread-plain" }, ]); + // The rest stay where they are, named for the arrival line. + expect(startedElsewhereOf(thread, MAC_MINI)).toEqual([ + { label: "Mac mini", title: "thread-on-linux" }, + ]); }); it("warns per agent picked, in the server's words", () => { diff --git a/apps/server/src/moves/ThreadMoves.ts b/apps/server/src/moves/ThreadMoves.ts index 4af4f60c0..34e407358 100644 --- a/apps/server/src/moves/ThreadMoves.ts +++ b/apps/server/src/moves/ThreadMoves.ts @@ -89,6 +89,7 @@ import { familyManifestOf, familyThreadIdsOf, linkLineageOf, + startedElsewhereOf, startedThereOf, } from "./familyMove.ts"; import { @@ -1647,6 +1648,12 @@ const make = Effect.gen(function* () { takes?.adoptsChildren === true ? startedThereOf(detail.value, targetEnvironmentId) : undefined; + // The ones on other computers, for a parent moving without its family + // (a family's carry them as `remoteStayed`): words for the arrival. + const startedElsewhere = + moveFamily === undefined + ? startedElsewhereOf(detail.value, targetEnvironmentId) + : undefined; const source = { environmentId: descriptor.environmentId, environmentLabel: environment.currentLabel(), @@ -1668,6 +1675,7 @@ const make = Effect.gen(function* () { familyThreadIds, linkLineage, startedThere, + startedElsewhere, }); } const { place, checkout } = yield* whereFrom(detail.value, project.workspaceRoot); @@ -1679,6 +1687,7 @@ const make = Effect.gen(function* () { familyThreadIds, linkLineage, startedThere, + startedElsewhere, git, checkout, transferId, diff --git a/apps/server/src/moves/familyMove.ts b/apps/server/src/moves/familyMove.ts index 2ca0a0b1b..b16662c6b 100644 --- a/apps/server/src/moves/familyMove.ts +++ b/apps/server/src/moves/familyMove.ts @@ -10,6 +10,7 @@ import type { OrchestrationThread, PeerMoveFamily, PeerMoveLinkLineage, + PeerMoveStartedElsewhere, PeerMoveStartedThere, ThreadMoveFamily, ThreadMoveFamilyId, @@ -83,12 +84,7 @@ export function familyManifestOf( .slice(0, PEER_MOVE_FAMILY_MAX_STAYED) .map((child) => ({ title: child.title })) : []; - const remoteStayed = isParent - ? (thread.childRequests.remoteChildren ?? []) - .filter((child) => child.environmentId !== targetEnvironmentId) - .slice(0, PEER_MOVE_FAMILY_MAX_STAYED) - .map((child) => ({ label: child.label, title: child.title })) - : []; + const remoteStayed = isParent ? startedElsewhereOf(thread, targetEnvironmentId) : []; const stopped = thread.move?.interrupted?.childRequestsStopped; return { id: family.id, @@ -154,6 +150,22 @@ export function linkLineageOf( return { remoteParent: { threadId: parent.id, title: parent.title } }; } +/** + * The threads `thread`'s agent started on computers other than + * `targetEnvironmentId`, which stay there when it moves (docs/design/fleet.md, + * Part 3f, (g)): a family's `remoteStayed`, or a lone parent's + * `startedElsewhere`, for the "Moved here" line there. + */ +export function startedElsewhereOf( + thread: Pick, + targetEnvironmentId: EnvironmentId, +): Array { + return (thread.childRequests.remoteChildren ?? []) + .filter((child) => child.environmentId !== targetEnvironmentId) + .slice(0, PEER_MOVE_FAMILY_MAX_STAYED) + .map((child) => ({ label: child.label, title: child.title })); +} + /** * The threads `thread`'s agent started on `targetEnvironmentId`, as its * records here hold them, for a move there (docs/design/fleet.md, Part 3f, diff --git a/apps/server/src/moves/moveExport.ts b/apps/server/src/moves/moveExport.ts index 9be554f8a..dc7076234 100644 --- a/apps/server/src/moves/moveExport.ts +++ b/apps/server/src/moves/moveExport.ts @@ -28,6 +28,7 @@ import type { PeerMoveRoom, PeerMoveRoomParticipant, PeerMoveScratchManifest, + PeerMoveStartedElsewhere, PeerMoveStartedThere, PeerMoveThreadState, ThreadId, @@ -505,6 +506,8 @@ export interface MoveExportInput { readonly linkLineage?: PeerMoveLinkLineage | undefined; /** The threads its agent started on the target (Part 3f, (g)): see PeerMoveStartedThere. */ readonly startedThere?: ReadonlyArray | undefined; + /** A lone parent's threads on other computers (Part 3f, (g)): see PeerMoveStartedElsewhere. */ + readonly startedElsewhere?: ReadonlyArray | undefined; } /** @@ -748,6 +751,9 @@ export function writeMoveExport( ...(input.startedThere !== undefined && input.startedThere.length > 0 ? { startedThere: [...input.startedThere] } : {}), + ...(input.startedElsewhere !== undefined && input.startedElsewhere.length > 0 + ? { startedElsewhere: [...input.startedElsewhere] } + : {}), }; yield* writeManifest(dir, manifest); return manifest; @@ -834,6 +840,9 @@ function writeSharerExport( ...(input.startedThere !== undefined && input.startedThere.length > 0 ? { startedThere: [...input.startedThere] } : {}), + ...(input.startedElsewhere !== undefined && input.startedElsewhere.length > 0 + ? { startedElsewhere: [...input.startedElsewhere] } + : {}), }; yield* writeManifest(input.dir, manifest); return manifest; @@ -1157,6 +1166,7 @@ export interface ScratchExportInput { readonly familyThreadIds?: ReadonlySet | undefined; readonly linkLineage?: PeerMoveLinkLineage | undefined; readonly startedThere?: ReadonlyArray | undefined; + readonly startedElsewhere?: ReadonlyArray | undefined; } /** @@ -1275,6 +1285,9 @@ export function writeScratchExport( ...(input.startedThere !== undefined && input.startedThere.length > 0 ? { startedThere: [...input.startedThere] } : {}), + ...(input.startedElsewhere !== undefined && input.startedElsewhere.length > 0 + ? { startedElsewhere: [...input.startedElsewhere] } + : {}), }; yield* writeManifest(input.dir, manifest); return manifest; diff --git a/docs/design/fleet.md b/docs/design/fleet.md index 91d897ad9..61063a635 100644 --- a/docs/design/fleet.md +++ b/docs/design/fleet.md @@ -1384,6 +1384,7 @@ A child that isn't in the set, or whose own move failed after the parent's commi - **Open remote requests:** "when done" waits for them as above. "Stop and move now" cancels them through the path Stop already uses (`cancelRequested`, `peer.request.cancel`, `peer.thread.stop`) and settles them here at once. - **`RemoteChildReactor` learns about moves.** It reads only `id`, `title`, `participants`, `childRequests`, `archivedAt` and `deletedAt` today. It now skips a parent with `move` set for watches and for anything that writes to the parent. One exception: `thread.remote-cancels.delivered` joins `FROZEN_THREAD_COMMANDS`, so cancels owed at the freeze still drain. - **A hole this closes.** On a frozen parent the reactor's four commands are refused. It reads a refused settle as "settled meanwhile" and acks the report as not delivered (RemoteChildReactor.ts ~609-619), so the answer is lost. No frozen parent can have remote children today, so it can't happen yet. Skipping frozen parents makes sure it never does. +- **A parent moving without its family** (no children on its own computer) still names the threads its agent started on other computers. The manifest's `startedElsewhere` (label and title, at most `PEER_MOVE_FAMILY_MAX_STAYED`) gives the "Moved here" line the same words a family's `remoteStayed` does. It is words only, and an older receiver ignores the field. - **Also when the child's computer is the target (Nov 18: taken back on arrival).** Kept apart, such a thread was stranded: fenced for good, pointing at a read-only copy, its reports retried forever, and nobody could direct it. So the parent takes it back as its child there. - **Who.** A thread both lists name: the sender's `remoteChildren` on the target (`PeerMoveManifest.startedThere`, at most 50, each with the user's pending Wrap up or Reopen), and the target's own records (threads started over this very link by the thread the move came from, not deleted or moved away). Of those, only threads that pass the decider's checks below (`adoptionRefusal`, asked once before the release with the modes the import will write, and again in the decision) are released and taken back; one that fails, archived ones included, keeps what its link asked of it, and the "Moved here" line says why. The sender sends the list only to a computer that says `PeerDescriptor.moveAdoptsChildren`; an older one keeps them apart, with the line below. Only a thread that lands on its own (no `lineage`, `remoteOrigin` or `adopt`) takes any back; a family's parent does. - **Checks, per thread, in the decider** (`thread.import`'s `adoptsChildren`, with the link from the transfer's row, never the manifest). Its `remoteOrigin` is this link, names the thread the move came from, and is pinned to the computer the move came from; it is in the same project; it isn't archived, moving or queued to move; it has no attached children; and it asks no more access than the arriving thread has there (its modes as the import writes them, after the link's cap). One that fails stays as it is, never with its access lowered, and the "Moved here" line says why. The import never fails for it. diff --git a/packages/contracts/src/peerRpc.ts b/packages/contracts/src/peerRpc.ts index dc39b7d6d..eaed6b21f 100644 --- a/packages/contracts/src/peerRpc.ts +++ b/packages/contracts/src/peerRpc.ts @@ -746,6 +746,19 @@ export const PeerMoveFamily = Schema.Struct({ }); export type PeerMoveFamily = typeof PeerMoveFamily.Type; +/** + * The threads a parent moving without its family (docs/design/fleet.md, Part + * 3f, (g)) had its agent start on computers other than the receiving one: they + * stay there, and the "Moved here" line says so. A family move carries them + * in `PeerMoveFamily.remoteStayed` instead. Words only; the receiving computer + * acts on none of it. At most PEER_MOVE_FAMILY_MAX_STAYED. + */ +export const PeerMoveStartedElsewhere = Schema.Struct({ + label: TrimmedNonEmptyString, + title: TrimmedNonEmptyString, +}); +export type PeerMoveStartedElsewhere = typeof PeerMoveStartedElsewhere.Type; + /** At most this many threads a manifest names as started on the receiving computer. */ export const PEER_MOVE_MAX_STARTED_THERE = 50; @@ -823,6 +836,8 @@ export const PeerMoveScratchManifest = Schema.Struct({ linkLineage: Schema.optional(PeerMoveLinkLineage), /** The threads its agent started there: see PeerMoveStartedThere. */ startedThere: Schema.optional(Schema.Array(PeerMoveStartedThere)), + /** The threads its agent started on other computers: see PeerMoveStartedElsewhere. */ + startedElsewhere: Schema.optional(Schema.Array(PeerMoveStartedElsewhere)), }); export type PeerMoveScratchManifest = typeof PeerMoveScratchManifest.Type; @@ -876,6 +891,8 @@ export const PeerMoveGitManifest = Schema.Struct({ linkLineage: Schema.optional(PeerMoveLinkLineage), /** The threads its agent started there: see PeerMoveStartedThere. */ startedThere: Schema.optional(Schema.Array(PeerMoveStartedThere)), + /** The threads its agent started on other computers: see PeerMoveStartedElsewhere. */ + startedElsewhere: Schema.optional(Schema.Array(PeerMoveStartedElsewhere)), }); export type PeerMoveGitManifest = typeof PeerMoveGitManifest.Type;