{"file":"client/src/drafts.ts","version":2,"draft":"login-timeout","total":169,"from":1,"to":169,"lines":[{"n":1,"text":"// abso switch, land and draft-sync: named drafts (#10, #22).","atom":"a0","author":"init","version":0},{"n":2,"text":"//","atom":"a0","author":"init","version":0},{"n":3,"text":"// Each agent has its own unpublished draft in each space (shared, or a named","atom":"a0","author":"init","version":0},{"n":4,"text":"// draft). Switching parks the draft on the server in the space it leaves and","atom":"a0","author":"init","version":0},{"n":5,"text":"// brings back what was parked in the space it moves to, so nothing needs","atom":"a0","author":"init","version":0},{"n":6,"text":"// publishing first. `switch -c <name>` instead takes the current unpublished","atom":"a0","author":"init","version":0},{"n":7,"text":"// changes into a new named draft.","atom":"a0","author":"init","version":0},{"n":8,"text":"","atom":"a0","author":"init","version":0},{"n":9,"text":"import { readFileSync } from \"node:fs\";","atom":"a0","author":"init","version":0},{"n":10,"text":"import { bool, type Parsed, str, UsageError } from \"./args.ts\";","atom":"a0","author":"init","version":0},{"n":11,"text":"import {","atom":"a0","author":"init","version":0},{"n":12,"text":"\tapplyAnswer,","atom":"a0","author":"init","version":0},{"n":13,"text":"\tchangedFilesText,","atom":"a0","author":"init","version":0},{"n":14,"text":"\tcollisionsHeading,","atom":"a0","author":"init","version":0},{"n":15,"text":"\tcollisionText,","atom":"a0","author":"init","version":0},{"n":16,"text":"\ttype Env,","atom":"a0","author":"init","version":0},{"n":17,"text":"\tEXIT,","atom":"a0","author":"init","version":0},{"n":18,"text":"\tmergedLinesText,","atom":"a0","author":"init","version":0},{"n":19,"text":"\tnewsText,","atom":"a0","author":"init","version":0},{"n":20,"text":"\ttype Output,","atom":"a0","author":"init","version":0},{"n":21,"text":"\topen,","atom":"a0","author":"init","version":0},{"n":22,"text":"\tplural,","atom":"a0","author":"init","version":0},{"n":23,"text":"\trecordedFirst,","atom":"a0","author":"init","version":0},{"n":24,"text":"\trecordFolder,","atom":"a0","author":"init","version":0},{"n":25,"text":"} from \"./commands.ts\";","atom":"a0","author":"init","version":0},{"n":26,"text":"import { isLocalOnly } from \"./core.ts\";","atom":"a0","author":"init","version":0},{"n":27,"text":"import { absolute } from \"./folder.ts\";","atom":"a0","author":"init","version":0},{"n":28,"text":"import { refusedText } from \"./safety.ts\";","atom":"a0","author":"init","version":0},{"n":29,"text":"","atom":"a0","author":"init","version":0},{"n":30,"text":"/** The name that means shared, not a named draft. */","atom":"a0","author":"init","version":0},{"n":31,"text":"const SHARED = \"shared\";","atom":"a0","author":"init","version":0},{"n":32,"text":"","atom":"a0","author":"init","version":0},{"n":33,"text":"function draftName(raw: string): string {","atom":"a0","author":"init","version":0},{"n":34,"text":"\tif (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(raw))","atom":"a0","author":"init","version":0},{"n":35,"text":"\t\tthrow new UsageError(`draft names are letters, digits, dot, dash and underscore (got ${JSON.stringify(raw)})`);","atom":"a0","author":"init","version":0},{"n":36,"text":"\treturn raw;","atom":"a0","author":"init","version":0},{"n":37,"text":"}","atom":"a0","author":"init","version":0},{"n":38,"text":"","atom":"a0","author":"init","version":0},{"n":39,"text":"export async function runSwitch(args: Parsed, env: Env): Promise<Output> {","atom":"a0","author":"init","version":0},{"n":40,"text":"\tif (args.positionals.length !== 1)","atom":"a0","author":"init","version":0},{"n":41,"text":"\t\tthrow new UsageError(","atom":"a0","author":"init","version":0},{"n":42,"text":"\t\t\t\"switch takes one name: abso switch <draft>, abso switch -c <new draft>, or abso switch shared\",","atom":"a0","author":"init","version":0},{"n":43,"text":"\t\t);","atom":"a0","author":"init","version":0},{"n":44,"text":"\tconst raw = args.positionals[0]!;","atom":"a0","author":"init","version":0},{"n":45,"text":"\tconst carry = bool(args, \"carry\");","atom":"a0","author":"init","version":0},{"n":46,"text":"\tconst name = raw === SHARED ? null : draftName(raw);","atom":"a0","author":"init","version":0},{"n":47,"text":"\tif (carry && name === null)","atom":"a0","author":"init","version":0},{"n":48,"text":"\t\tthrow new UsageError(\"-c starts a new named draft; shared already exists (use: abso switch shared)\");","atom":"a0","author":"init","version":0},{"n":49,"text":"\tconst { root, api } = open(env);","atom":"a0","author":"init","version":0},{"n":50,"text":"\t// Folder-only edits must be on the server first, or the new view would overwrite them instead of parking them.","atom":"a0","author":"init","version":0},{"n":51,"text":"\tconst recorded = await recordFolder(root, api);","atom":"a0","author":"init","version":0},{"n":52,"text":"\t// What the folder held before, for each file the switch or the sync touches, so only the net change is reported.","atom":"a0","author":"init","version":0},{"n":53,"text":"\tconst before = new Map<string, string | null>();","atom":"a0","author":"init","version":0},{"n":54,"text":"\tconst note = (files: Readonly<Record<string, unknown>>): void => {","atom":"a0","author":"init","version":0},{"n":55,"text":"\t\tfor (const rel of Object.keys(files)) if (!before.has(rel)) before.set(rel, onDisk(root, rel));","atom":"a0","author":"init","version":0},{"n":56,"text":"\t};","atom":"a0","author":"init","version":0},{"n":57,"text":"\tconst answer = await api.switchDraft(name, carry);","atom":"a0","author":"init","version":0},{"n":58,"text":"\tnote(answer.files);","atom":"a0","author":"init","version":0},{"n":59,"text":"\tconst switched = await applyAnswer(root, api, answer);","atom":"a0","author":"init","version":0},{"n":60,"text":"\t// The switch brings back the space as it was when parked; sync straight away so the folder shows it as it is now (#22).","atom":"a0","author":"init","version":0},{"n":61,"text":"\tconst syncAnswer = await api.sync();","atom":"a0","author":"init","version":0},{"n":62,"text":"\tnote(syncAnswer.files);","atom":"a0","author":"init","version":0},{"n":63,"text":"\tconst synced = await applyAnswer(root, api, syncAnswer);","atom":"a0","author":"init","version":0},{"n":64,"text":"\tconst touched = new Set([...switched.written, ...switched.deleted, ...synced.written, ...synced.deleted]);","atom":"a0","author":"init","version":0},{"n":65,"text":"\tconst written: string[] = [];","atom":"a0","author":"init","version":0},{"n":66,"text":"\tconst deleted: string[] = [];","atom":"a0","author":"init","version":0},{"n":67,"text":"\tfor (const rel of [...touched].sort()) {","atom":"a0","author":"init","version":0},{"n":68,"text":"\t\tconst now = onDisk(root, rel);","atom":"a0","author":"init","version":0},{"n":69,"text":"\t\tif (now === before.get(rel)) continue;","atom":"a0","author":"init","version":0},{"n":70,"text":"\t\t(now === null ? deleted : written).push(rel);","atom":"a0","author":"init","version":0},{"n":71,"text":"\t}","atom":"a0","author":"init","version":0},{"n":72,"text":"\tconst where = answer.draft === null ? \"shared\" : `draft ${answer.draft}`;","atom":"a0","author":"init","version":0},{"n":73,"text":"\tconst kept = [...new Set([...switched.kept, ...synced.kept])];","atom":"a0","author":"init","version":0},{"n":74,"text":"\tconst dropped = [...new Set([...switched.dropped, ...synced.dropped])];","atom":"a0","author":"init","version":0},{"n":75,"text":"\tconst collisions = [...switched.collisions, ...synced.collisions];","atom":"a0","author":"init","version":0},{"n":76,"text":"\tconst lines = [","atom":"a0","author":"init","version":0},{"n":77,"text":"\t\t...recordedFirst(recorded),","atom":"a0","author":"init","version":0},{"n":78,"text":"\t\tcarry","atom":"a0","author":"init","version":0},{"n":79,"text":"\t\t\t? `Started ${where}, carrying your unpublished changes into it.`","atom":"a0","author":"init","version":0},{"n":80,"text":"\t\t\t: `Switched to ${where}. Your unpublished changes where you were are parked there, and come back when you switch back.`,","atom":"a0","author":"init","version":0},{"n":81,"text":"\t\t`It is at version ${synced.version}. ${written.length + deleted.length === 0 ? \"No files changed in this folder.\" : `${plural(written.length, \"file\")} updated, ${deleted.length} deleted:`}`,","atom":"a0","author":"init","version":0},{"n":82,"text":"\t\t...changedFilesText(\"switch\", written, deleted, [], bool(args, \"verbose\")),","atom":"a0","author":"init","version":0},{"n":83,"text":"\t\t...refusedText([...switched.refused, ...synced.refused]),","atom":"a0","author":"init","version":0},{"n":84,"text":"\t\t...mergedLinesText([...switched.merged, ...synced.merged]),","atom":"a0","author":"init","version":0},{"n":85,"text":"\t\t...(kept.length > 0 ? [`Left alone because they changed while switching (run abso sync): ${kept.join(\", \")}`] : []),","atom":"a0","author":"init","version":0},{"n":86,"text":"\t\t...(dropped.length > 0 ? [`Dropped from your draft, kept on disk (these stay local): ${dropped.join(\", \")}`] : []),","atom":"a0","author":"init","version":0},{"n":87,"text":"\t\t...(collisions.length > 0 ? [collisionsHeading(collisions), ...collisions.map(collisionText)] : []),","atom":"a0","author":"init","version":0},{"n":88,"text":"\t\t...(answer.draft !== null","atom":"a0","author":"init","version":0},{"n":89,"text":"\t\t\t? ['Publish into this draft with abso publish; land it in shared with abso land -m \"why\".']","atom":"a0","author":"init","version":0},{"n":90,"text":"\t\t\t: []),","atom":"a0","author":"init","version":0},{"n":91,"text":"\t];","atom":"a0","author":"init","version":0},{"n":92,"text":"\treturn {","atom":"a0","author":"init","version":0},{"n":93,"text":"\t\ttext: lines.join(\"\\n\"),","atom":"a0","author":"init","version":0},{"n":94,"text":"\t\tjson: {","atom":"a0","author":"init","version":0},{"n":95,"text":"\t\t\tdraft: answer.draft,","atom":"a0","author":"init","version":0},{"n":96,"text":"\t\t\tversion: synced.version,","atom":"a0","author":"init","version":0},{"n":97,"text":"\t\t\tcarried: carry,","atom":"a0","author":"init","version":0},{"n":98,"text":"\t\t\trecorded: recorded.sent,","atom":"a0","author":"init","version":0},{"n":99,"text":"\t\t\twritten,","atom":"a0","author":"init","version":0},{"n":100,"text":"\t\t\tdeleted,","atom":"a0","author":"init","version":0},{"n":101,"text":"\t\t\tkept,","atom":"a0","author":"init","version":0},{"n":102,"text":"\t\t\tdropped,","atom":"a0","author":"init","version":0},{"n":103,"text":"\t\t\tcollisions,","atom":"a0","author":"init","version":0},{"n":104,"text":"\t\t},","atom":"a0","author":"init","version":0},{"n":105,"text":"\t};","atom":"a0","author":"init","version":0},{"n":106,"text":"}","atom":"a0","author":"init","version":0},{"n":107,"text":"","atom":"a0","author":"init","version":0},{"n":108,"text":"/** A file's bytes as text, or null when there is no file. */","atom":"a0","author":"init","version":0},{"n":109,"text":"function onDisk(root: string, rel: string): string | null {","atom":"a0","author":"init","version":0},{"n":110,"text":"\ttry {","atom":"a0","author":"init","version":0},{"n":111,"text":"\t\treturn readFileSync(absolute(root, rel), \"latin1\");","atom":"a0","author":"init","version":0},{"n":112,"text":"\t} catch {","atom":"a0","author":"init","version":0},{"n":113,"text":"\t\treturn null;","atom":"a0","author":"init","version":0},{"n":114,"text":"\t}","atom":"a0","author":"init","version":0},{"n":115,"text":"}","atom":"a0","author":"init","version":0},{"n":116,"text":"","atom":"a0","author":"init","version":0},{"n":117,"text":"export async function runLand(args: Parsed, env: Env): Promise<Output> {","atom":"a0","author":"init","version":0},{"n":118,"text":"\tconst message = str(args, \"message\");","atom":"a0","author":"init","version":0},{"n":119,"text":"\tif (!message) throw new UsageError('say why with -m \"<message>\"');","atom":"a0","author":"init","version":0},{"n":120,"text":"\tconst { root, api } = open(env);","atom":"a0","author":"init","version":0},{"n":121,"text":"\tconst recorded = await recordFolder(root, api);","atom":"a0","author":"init","version":0},{"n":122,"text":"\tconst answer = await api.land(message);","atom":"a0","author":"init","version":0},{"n":123,"text":"\tconst pre = recordedFirst(recorded);","atom":"a0","author":"init","version":0},{"n":124,"text":"\tif (answer.ok) {","atom":"a0","author":"init","version":0},{"n":125,"text":"\t\tconst what =","atom":"a0","author":"init","version":0},{"n":126,"text":"\t\t\tanswer.changes === 0","atom":"a0","author":"init","version":0},{"n":127,"text":"\t\t\t\t? `Nothing to land: the draft has nothing shared doesn't. Shared is at version ${answer.version}.`","atom":"a0","author":"init","version":0},{"n":128,"text":"\t\t\t\t: `Landed in shared as version ${answer.version}.`;","atom":"a0","author":"init","version":0},{"n":129,"text":"\t\treturn {","atom":"a0","author":"init","version":0},{"n":130,"text":"\t\t\ttext: [...pre, what].join(\"\\n\"),","atom":"a0","author":"init","version":0},{"n":131,"text":"\t\t\tjson: { version: answer.version, changes: answer.changes ?? null, recorded: recorded.sent },","atom":"a0","author":"init","version":0},{"n":132,"text":"\t\t};","atom":"a0","author":"init","version":0},{"n":133,"text":"\t}","atom":"a0","author":"init","version":0},{"n":134,"text":"\tconst collisions = (answer.collisions ?? []).filter((c) => !isLocalOnly(c.file));","atom":"a0","author":"init","version":0},{"n":135,"text":"\treturn {","atom":"a0","author":"init","version":0},{"n":136,"text":"\t\texit: EXIT.refused,","atom":"a0","author":"init","version":0},{"n":137,"text":"\t\ttext: [","atom":"a0","author":"init","version":0},{"n":138,"text":"\t\t\t...pre,","atom":"a0","author":"init","version":0},{"n":139,"text":"\t\t\t`Not landed: ${plural(collisions.length, \"collision\")} with work that landed in shared since the draft's base.`,","atom":"a0","author":"init","version":0},{"n":140,"text":"\t\t\t...collisions.map(collisionText),","atom":"a0","author":"init","version":0},{"n":141,"text":"\t\t\t\"Run abso draft-sync to move the draft onto shared, settle those lines in the draft, publish, then land again.\",","atom":"a0","author":"init","version":0},{"n":142,"text":"\t\t].join(\"\\n\"),","atom":"a0","author":"init","version":0},{"n":143,"text":"\t\tjson: { collisions, recorded: recorded.sent },","atom":"a0","author":"init","version":0},{"n":144,"text":"\t};","atom":"a0","author":"init","version":0},{"n":145,"text":"}","atom":"a0","author":"init","version":0},{"n":146,"text":"","atom":"a0","author":"init","version":0},{"n":147,"text":"export async function runDraftSync(_args: Parsed, env: Env): Promise<Output> {","atom":"a0","author":"init","version":0},{"n":148,"text":"\tconst { root, api } = open(env);","atom":"a0","author":"init","version":0},{"n":149,"text":"\tconst moved = await api.draftSync();","atom":"a0","author":"init","version":0},{"n":150,"text":"\t// Bring the moved draft's new view into this folder straight away.","atom":"a0","author":"init","version":0},{"n":151,"text":"\tconst recorded = await recordFolder(root, api);","atom":"a0","author":"init","version":0},{"n":152,"text":"\tconst applied = await applyAnswer(root, api, await api.sync());","atom":"a0","author":"init","version":0},{"n":153,"text":"\tconst lines = [","atom":"a0","author":"init","version":0},{"n":154,"text":"\t\t`Moved draft ${moved.draft} onto shared version ${moved.sharedBase} (the draft is at version ${moved.version}).`,","atom":"a0","author":"init","version":0},{"n":155,"text":"\t\t...recordedFirst(recorded),","atom":"a0","author":"init","version":0},{"n":156,"text":"\t\tapplied.written.length + applied.deleted.length === 0","atom":"a0","author":"init","version":0},{"n":157,"text":"\t\t\t? \"No files changed in this folder.\"","atom":"a0","author":"init","version":0},{"n":158,"text":"\t\t\t: `${plural(applied.written.length, \"file\")} updated, ${applied.deleted.length} deleted.`,","atom":"a0","author":"init","version":0},{"n":159,"text":"\t\t...refusedText(applied.refused),","atom":"a0","author":"init","version":0},{"n":160,"text":"\t\t...mergedLinesText(applied.merged),","atom":"a0","author":"init","version":0},{"n":161,"text":"\t\t...(applied.collisions.length > 0","atom":"a0","author":"init","version":0},{"n":162,"text":"\t\t\t? [","atom":"a0","author":"init","version":0},{"n":163,"text":"\t\t\t\t\t`${plural(applied.collisions.length, \"collision\")} to settle in the draft before it lands:`,","atom":"a0","author":"init","version":0},{"n":164,"text":"\t\t\t\t\t...applied.collisions.map(collisionText),","atom":"a0","author":"init","version":0},{"n":165,"text":"\t\t\t\t]","atom":"a0","author":"init","version":0},{"n":166,"text":"\t\t\t: []),","atom":"a0","author":"init","version":0},{"n":167,"text":"\t];","atom":"a0","author":"init","version":0},{"n":168,"text":"\treturn { text: lines.join(\"\\n\"), json: { ...applied, ...moved } };","atom":"a0","author":"init","version":0},{"n":169,"text":"}","atom":"a0","author":"init","version":0}],"atoms":[{"atom":"a0","author":"init","message":"Start the project","time":1791506895507,"timeIso":"2026-10-09T00:48:15.507Z","publish":{"draft":null,"version":0,"agent":"init","message":"Start the project","time":1791506895507,"timeIso":"2026-10-09T00:48:15.507Z"}}]}