Lines 1–12 at 318 (were 1–9 at 92)
1+import { diffLineSymbols } from "../../src/engine/text.ts"; 2+import { recordFileChanges } from "./record-file.ts"; 13// Moves (#28): within one agent's unpublished draft, a line that leaves one place and the same 24// text that arrives somewhere else is the same line. Before a publish, those matches become move 35// atoms so the lines keep their ids. Writes go through ExpressMovesHost; today the host is Project. 5−import type { AtomId, AttrSet, EditInput, Graph, LineId, MoveInput, View } from "../../src/engine/index.ts";
6−import { PLAIN_LINE } from "./collisions.ts";
7+import type { AtomId, AttrSet, EditInput, Graph, LineId, MoveInput, ReadView } from "../../src/engine/index.ts"; 8+import { differenceSet, StableOverlaySet } from "../../src/engine/state-source.ts"; 9+import { MoveLineText } from "./move-line-text.ts"; 710import type { AgentState, Entry, Meta, Mode } from "./project-types.ts"; 912/** The most moved blocks one publish expresses: the longest ones, when there are more. */ Lines 20–32 at 318 (were 17–28 at 92)
1720export interface ExpressMovesHost { 1821 readonly graph: Graph; 1922 draft(agent: AgentState): AtomId[]; 20− agentView(agent: AgentState): View;
21− view(set: ReadonlySet<AtomId>): View;
23+ agentView(agent: AgentState): ReadView; 24+ beforeDraft?(agent: AgentState, draft: readonly AtomId[]): ReadView; 25+ view(set: ReadonlySet<AtomId>): ReadView; 2226 apply(entry: Entry): void; 25− view: View,
2630 input: { edits: readonly EditInput[]; attrs?: readonly AttrSet[]; move?: MoveInput }, Lines 34–40 at 318 (were 30–36 at 92)
33− view: View,
3539 text: string | null | undefined, Lines 61–94 at 318 (were 57–64 at 92)
5761 if (draft.length === 0) return; 5862 const g = host.graph; 5963 const mine = new Set(draft); 64+ const text = new MoveLineText(g); 65+ // The draft's own lines first, before laying out any file (abso 714, piece 3): a move needs a written line with 66+ // real content whose text the draft also deleted. An edit that only changes lines has none, and laying out its 67+ // files (12k lines twice for one changed line of worker/src/index.ts) cost the whole publish. 68+ const goneIds = new Set<LineId>(); 69+ const goneTexts = new Set<number>(); 70+ for (const id of draft) 71+ for (const line of g.deletedLines(id)) { 73+ goneTexts.add(text.intern(line)); 75+ if (goneTexts.size === 0) return; 77+ for (const id of draft) { 78+ for (const line of g.eachInsertedLine(id)) { 79+ const t = text.intern(line); 80+ if (!text.plain[t] && goneTexts.has(t)) { 6088 const after = host.agentView(agent); 61− const before = host.view(new Set([...after.set].filter((id) => !mine.has(id))));
89+ const before = host.beforeDraft 90+ ? host.beforeDraft(agent, draft) 91+ : host.view(differenceSet(after.set, new StableOverlaySet(draft))); 6292 const touched = new Set<string>(); 6393 for (const id of draft) for (const file of g.filesOf(id)) touched.add(file); Lines 97–122 at 318 (were 67–91 at 92)
6797 readonly file: string; 6898 readonly start: number; 6999 readonly ids: LineId[]; 70− readonly texts: string[];
100+ readonly texts: number[]; 72102 const gone: Block[] = []; 73103 const written: Block[] = []; 74104 for (const file of [...touched].toSorted()) { 75105 let cur: Block | undefined; 76− before.lines(file).forEach((l, i) => {
77− if (g.deletersOf(l.id).some((d) => mine.has(d))) {
106+ before.buildLines(file).forEach((l, i) => { 107+ // Deleted by the draft: one of its atoms' deleted lines, with no lookup of each line's deleters. 108+ if (goneIds.has(l.id)) { 78109 if (!cur) gone.push((cur = { file, start: i, ids: [], texts: [] })); 79110 cur.ids.push(l.id); 80− cur.texts.push(l.text);
111+ cur.texts.push(text.intern(l.id)); 81112 } else cur = undefined; 84− after.lines(file).forEach((l, i) => {
115+ after.buildLines(file).forEach((l, i) => { 85116 if (mine.has(l.atom)) { 86117 if (!cur) written.push((cur = { file, start: i, ids: [], texts: [] })); 87118 cur.ids.push(l.id); 88− cur.texts.push(l.text);
119+ cur.texts.push(text.intern(l.id)); 89120 } else cur = undefined; Lines 135–141 at 318 (were 104–110 at 92)
104135 // Where each deleted text starts a candidate, in block and line order (abso issue 158): a stretch 105136 // can only start where the texts are equal, so each written line looks only there, not at 106137 // every deleted line. A text deleted more than MATCH_STARTS times keeps its first ones. 107− const starts = new Map<string, { block: Block; q: number }[]>();
138+ const starts = new Map<number, { block: Block; q: number }[]>(); 108139 for (const block of gone) 109140 block.texts.forEach((t, q) => { 110141 const list = starts.get(t); Lines 158–164 at 318 (were 127–133 at 92)
128159 if (len > 0 && (!best || len > best.len)) best = { block, q, len }; 130− if (best?.block.texts.slice(best.q, best.q + best.len).some((t) => !PLAIN_LINE.test(t))) {
161+ if (best?.block.texts.slice(best.q, best.q + best.len).some((t) => !text.plain[t])) { 131162 const ids = best.block.ids.slice(best.q, best.q + best.len); 132163 for (const id of ids) used.add(id); 133164 matches.push({ from: best.block.file, ids, to: run.file, at: run.start + p }); Lines 172–178 at 318 (were 141–147 at 92)
141172 // passed Workers' CPU limit. So the longest blocks move first, up to MOVE_LIMIT of them and 142173 // MOVE_BUDGET lines laid out in all; the rest are a delete and a fresh write, as an unmatched block 144− const length = (path: string) => (after.hasFile(path) ? after.lines(path).length : 0);
175+ const length = (path: string) => (after.hasFile(path) ? after.buildLines(path).length : 0); 145176 const cost = (m: Match) => length(m.from) + (m.to === m.from ? 0 : length(m.to)); 146177 const keep = new Set<Match>(); Lines 201–208 at 318 (were 170–175 at 92)
204+ // Keep the intended membership, but reacquire its reader after each graph mutation. 205+ const finalAgent = { ...agent, mine: [...agent.mine] }; 173206 const discard: Entry = { type: "discard", agent: agent.name, atoms: draft.filter((id) => drop.has(id)) }; 174207 entries.push(discard); 175208 host.apply(discard); Lines 212–218 at 318 (were 179–185 at 92)
179212 const start = view.lineNumber(m.from, m.ids[0]!); 180213 if (start < 0 || m.ids.some((id, k) => view.lineNumber(m.from, id) !== start + k)) continue; 181214 // Land after the nearest line above it in the final text that is already there. 182− const finalLines = after.lines(m.to);
215+ const finalLines = host.agentView(finalAgent).buildLines(m.to); 184217 for (let i = m.at - 1; i >= 0; i--) { 185218 const n = view.lineNumber(m.to, finalLines[i]!.id); Lines 232–261 at 318 (were 199–217 at 92)
201234 for (const path of [...involved].toSorted()) { 202− const was = after.attrs(path);
203− host.recordFile(
235+ const finalView = host.agentView(finalAgent); 236+ const was = finalView.attrs(path); 237+ const view = host.agentView(agent); 238+ const target = finalView.buildLines(path); 239+ const symbols = new MoveLineText(g); 240+ const previous = view.buildLines(path).map((line) => symbols.intern(line.id)); 241+ const next = target.map((line) => symbols.intern(line.id)); 242+ const edits = diffLineSymbols(previous, next).map((hunk) => ({ 246+ lines: hunk.lines.map((symbol) => symbols.text(symbol)), 205− host.agentView(agent),
207− after.hasFile(path) ? after.text(path) : null,
253+ !finalView.hasFile(path) ? "missing" : target.length === 0 ? "empty" : "present", 211− {
212− exec: was.exec,
213− link: was.link,
214− },
258+ { exec: was.exec, link: was.link },