- MCP
- blame on this site's /mcp
- CLI
- abso blame worker/project/express-moves.ts --json
- JSON
- /christopher/abso-core/blame/worker/project/express-moves.ts?format=json
261 linesFirst import 74% · codex-capacity-chair 17% · capacity 10%Last change capacity in version 311,
261 lines from 6 changes in 5 versions.
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare1import { diffLineSymbols } from "../../src/engine/text.ts";
2import { recordFileChanges } from "./record-file.ts";
init0Start the project3// Moves (#28): within one agent's unpublished draft, a line that leaves one place and the same
4// text that arrives somewhere else is the same line. Before a publish, those matches become move
5// atoms so the lines keep their ids. Writes go through ExpressMovesHost; today the host is Project.
codex-capacity-chair143Add inactive disk projection provider and verified transactional readers (#16)Compare7import type { AtomId, AttrSet, EditInput, Graph, LineId, MoveInput, ReadView } from "../../src/engine/index.ts";
codex-capacity-chair133Keep persistent publish membership indexed through conflict checksCompare8import { differenceSet, StableOverlaySet } from "../../src/engine/state-source.ts";
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare9import { MoveLineText } from "./move-line-text.ts";
12/** The most moved blocks one publish expresses: the longest ones, when there are more. */
13export const MOVE_LIMIT = 64;
14/** About how many lines one publish may lay out to express moves: each move lays out its files again. */
15export const MOVE_BUDGET = 200_000;
16/** The most places one deleted text is tried as the start of a moved stretch. */
17export const MATCH_STARTS = 256;
19/** What expressing moves needs from a project. */
20export interface ExpressMovesHost {
21 readonly graph: Graph;
22 draft(agent: AgentState): AtomId[];
codex-capacity-chair143Add inactive disk projection provider and verified transactional readers (#16)Compare23 agentView(agent: AgentState): ReadView;
24 beforeDraft?(agent: AgentState, draft: readonly AtomId[]): ReadView;
25 view(set: ReadonlySet<AtomId>): ReadView;
27 addDraft(
28 agent: AgentState,
codex-capacity-chair143Add inactive disk projection provider and verified transactional readers (#16)Compare29 view: ReadView,
init0Start the project30 input: { edits: readonly EditInput[]; attrs?: readonly AttrSet[]; move?: MoveInput },
31 time: number,
32 meta: Meta,
33 entries: Entry[],
34 ): void;
35 recordFile(
36 agent: AgentState,
codex-capacity-chair143Add inactive disk projection provider and verified transactional readers (#16)Compare37 view: ReadView,
39 text: string | null | undefined,
40 time: number,
41 meta: Meta,
42 entries: Entry[],
43 mode?: Mode,
44 ): boolean;
45}
47/**
48 * Moves (#28). Within one agent's unpublished draft, a line that leaves one
49 * place and the same text that arrives somewhere else is the same line, in
50 * either order (deleted then written, or written then deleted). Before a
51 * publish, the draft's changes to the files involved are dropped (a discard
52 * entry) and made again: one move per matched block, which keeps the
53 * lines' ids, then a change per file to the draft's final text. The files
54 * end up exactly as the draft had them. A matched block needs a line with
55 * real content: blank lines and lone brackets only travel next to one.
56 * The agent's publishes waiting in the queue keep their changes: they count
57 * as already there, never dropped (abso issue 135, item 4).
58 */
59export function expressMoves(host: ExpressMovesHost, agent: AgentState, time: number, entries: Entry[]): void {
60 const draft = host.draft(agent);
61 if (draft.length === 0) return;
62 const g = host.graph;
63 const mine = new Set(draft);
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare64 const text = new MoveLineText(g);
capacity311Move detection reads the draft's own lines first and lays out no file for an edit with no move (abso 714, piece 3 of Christopher's base fix)
- expressMoves, before a publish, used to lay out every file the draft touched twice: before the draft and after it. It then read each line's deleters, so its cost grew with the files' length, not the change. For a 1470-shaped publish that was worker/src/index.ts's 12k lines, twice.
- Now it reads the draft's own deleted lines (deletedLines) and written lines (eachInsertedLine) first. A move needs a written line with real content whose text the draft also deleted. With none, as in any edit that only changes lines, it returns before laying out anything.
- When there may be a move, the files are laid out as before. But a line counts as deleted by the draft from that set of the draft's deleted lines, not from a lookup of each line's deleters. Moves come out the same.
- Test: worker/project/test/express-moves-early.test.ts, with a host whose views throw if laid out. A line edit and a brackets-only match lay out nothing; a real line written again goes on to the files.
Checks: tsc clean; Biome clean on both files; core tests (worker/project and src): 762 passed, none failed; the 17 move tests pass.
Lab, on abso's Oct 14 tables with abso 1476's disk route, 1470-shaped publish, 38 changed lines: 9,408 statements and 1,438 writes in one transaction of 1.5 s, inside the budget test's 10,600 and 3,280. It was 24,209 statements after piece 2 (6.1 s) and 36,038 before it. Record is unchanged (piece 4).Compare65 // 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)) {
72 goneIds.add(line);
73 goneTexts.add(text.intern(line));
74 }
75 if (goneTexts.size === 0) return;
76 let moved = false;
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)) {
81 moved = true;
82 break;
83 }
84 }
85 if (moved) break;
86 }
87 if (!moved) return;
codex-capacity-chair143Add inactive disk projection provider and verified transactional readers (#16)Compare89 const before = host.beforeDraft
90 ? host.beforeDraft(agent, draft)
91 : host.view(differenceSet(after.set, new StableOverlaySet(draft)));
93 for (const id of draft) for (const file of g.filesOf(id)) touched.add(file);
95 // Blocks of lines the draft deleted, and runs of lines it wrote, each consecutive.
96 interface Block {
97 readonly file: string;
98 readonly start: number;
99 readonly ids: LineId[];
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare100 readonly texts: number[];
102 const gone: Block[] = [];
103 const written: Block[] = [];
104 for (const file of [...touched].toSorted()) {
105 let cur: Block | undefined;
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare106 before.buildLines(file).forEach((l, i) => {
capacity311Move detection reads the draft's own lines first and lays out no file for an edit with no move (abso 714, piece 3 of Christopher's base fix)
- expressMoves, before a publish, used to lay out every file the draft touched twice: before the draft and after it. It then read each line's deleters, so its cost grew with the files' length, not the change. For a 1470-shaped publish that was worker/src/index.ts's 12k lines, twice.
- Now it reads the draft's own deleted lines (deletedLines) and written lines (eachInsertedLine) first. A move needs a written line with real content whose text the draft also deleted. With none, as in any edit that only changes lines, it returns before laying out anything.
- When there may be a move, the files are laid out as before. But a line counts as deleted by the draft from that set of the draft's deleted lines, not from a lookup of each line's deleters. Moves come out the same.
- Test: worker/project/test/express-moves-early.test.ts, with a host whose views throw if laid out. A line edit and a brackets-only match lay out nothing; a real line written again goes on to the files.
Checks: tsc clean; Biome clean on both files; core tests (worker/project and src): 762 passed, none failed; the 17 move tests pass.
Lab, on abso's Oct 14 tables with abso 1476's disk route, 1470-shaped publish, 38 changed lines: 9,408 statements and 1,438 writes in one transaction of 1.5 s, inside the budget test's 10,600 and 3,280. It was 24,209 statements after piece 2 (6.1 s) and 36,038 before it. Record is unchanged (piece 4).Compare107 // Deleted by the draft: one of its atoms' deleted lines, with no lookup of each line's deleters.
108 if (goneIds.has(l.id)) {
110 cur.ids.push(l.id);
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare111 cur.texts.push(text.intern(l.id));
113 });
114 cur = undefined;
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare115 after.buildLines(file).forEach((l, i) => {
117 if (!cur) written.push((cur = { file, start: i, ids: [], texts: [] }));
118 cur.ids.push(l.id);
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare119 cur.texts.push(text.intern(l.id));
121 });
122 }
123 if (gone.length === 0 || written.length === 0) return;
125 // Match: for each written run, the longest stretches that some deleted block has too.
126 interface Match {
127 readonly from: string;
128 readonly ids: LineId[];
129 readonly to: string;
130 /** Where the stretch starts in the draft's final text of `to`. */
131 readonly at: number;
132 }
133 const used = new Set<LineId>();
134 const matches: Match[] = [];
135 // Where each deleted text starts a candidate, in block and line order (abso issue 158): a stretch
136 // can only start where the texts are equal, so each written line looks only there, not at
137 // every deleted line. A text deleted more than MATCH_STARTS times keeps its first ones.
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare138 const starts = new Map<number, { block: Block; q: number }[]>();
140 block.texts.forEach((t, q) => {
141 const list = starts.get(t);
142 if (!list) starts.set(t, [{ block, q }]);
143 else if (list.length < MATCH_STARTS) list.push({ block, q });
144 });
145 for (const run of written) {
146 let p = 0;
147 while (p < run.texts.length) {
148 let best: { block: Block; q: number; len: number } | undefined;
149 for (const { block, q } of starts.get(run.texts[p]!) ?? []) {
150 let len = 0;
151 while (
152 p + len < run.texts.length &&
153 q + len < block.texts.length &&
154 run.texts[p + len] === block.texts[q + len] &&
155 !used.has(block.ids[q + len]!)
156 ) {
157 len++;
158 }
159 if (len > 0 && (!best || len > best.len)) best = { block, q, len };
160 }
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare161 if (best?.block.texts.slice(best.q, best.q + best.len).some((t) => !text.plain[t])) {
163 for (const id of ids) used.add(id);
164 matches.push({ from: best.block.file, ids, to: run.file, at: run.start + p });
165 p += best.len;
166 } else p++;
167 }
168 }
169 if (matches.length === 0) return;
170 // Each match lays its files out again, so the cost is matches times file length (abso issues 137,
171 // 142): a rewritten 23,572-line JSON file made 438 matches, 399 of them one line, and the publish
172 // passed Workers' CPU limit. So the longest blocks move first, up to MOVE_LIMIT of them and
173 // MOVE_BUDGET lines laid out in all; the rest are a delete and a fresh write, as an unmatched block
174 // always is.
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare175 const length = (path: string) => (after.hasFile(path) ? after.buildLines(path).length : 0);
init0Start the project176 const cost = (m: Match) => length(m.from) + (m.to === m.from ? 0 : length(m.to));
177 const keep = new Set<Match>();
178 let spent = 0;
179 for (const { m } of matches
180 .map((m, i) => ({ m, i }))
181 .toSorted((a, b) => b.m.ids.length - a.m.ids.length || a.i - b.i)) {
182 if (keep.size >= MOVE_LIMIT) break;
183 if (keep.size > 0 && spent + cost(m) > MOVE_BUDGET) continue;
184 spent += cost(m);
185 keep.add(m);
186 }
187 for (let i = matches.length - 1; i >= 0; i--) if (!keep.has(matches[i]!)) matches.splice(i, 1);
189 // Drop the draft's changes to the files involved, and whatever builds on them.
190 const involved = new Set<string>(matches.flatMap((m) => [m.from, m.to]));
191 const drop = new Set<AtomId>();
192 for (let grew = true; grew; ) {
193 grew = false;
194 for (const id of draft) {
195 if (drop.has(id)) continue;
196 const atom = g.atom(id);
197 if (g.filesOf(id).some((f) => involved.has(f)) || atom.deps.some((d) => drop.has(d))) {
198 drop.add(id);
199 for (const f of g.filesOf(id)) involved.add(f);
200 grew = true;
201 }
202 }
203 }
codex-capacity-chair143Add inactive disk projection provider and verified transactional readers (#16)Compare204 // Keep the intended membership, but reacquire its reader after each graph mutation.
205 const finalAgent = { ...agent, mine: [...agent.mine] };
init0Start the project206 const discard: Entry = { type: "discard", agent: agent.name, atoms: draft.filter((id) => drop.has(id)) };
207 entries.push(discard);
208 host.apply(discard);
210 for (const m of matches) {
211 const view = host.agentView(agent);
212 const start = view.lineNumber(m.from, m.ids[0]!);
213 if (start < 0 || m.ids.some((id, k) => view.lineNumber(m.from, id) !== start + k)) continue;
214 // Land after the nearest line above it in the final text that is already there.
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare215 const finalLines = host.agentView(finalAgent).buildLines(m.to);
217 for (let i = m.at - 1; i >= 0; i--) {
218 const n = view.lineNumber(m.to, finalLines[i]!.id);
219 if (n >= 0) {
220 at = n + 1;
221 break;
222 }
223 }
224 if (m.to === m.from && at >= start && at <= start + m.ids.length) continue;
225 host.addDraft(
226 agent,
227 view,
228 { edits: [], move: { file: m.from, start, end: start + m.ids.length, to: m.to, at } },
229 time,
230 {},
231 entries,
232 );
233 }
234 for (const path of [...involved].toSorted()) {
codex-capacity-chair143Add inactive disk projection provider and verified transactional readers (#16)Compare235 const finalView = host.agentView(finalAgent);
236 const was = finalView.attrs(path);
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare237 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) => ({
243 file: path,
244 start: hunk.start,
245 end: hunk.end,
246 lines: hunk.lines.map((symbol) => symbols.text(symbol)),
247 }));
248 recordFileChanges(
249 host,
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare251 view,
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare253 !finalView.hasFile(path) ? "missing" : target.length === 0 ? "empty" : "present",
254 edits,
256 {},
257 entries,
codex-capacity-chair175Reduce disk-backed record and publish allocations; reviewed in core ask 52 with parsed-code budget and exact aligned-line follow-ups. Keep disk-first activation off.Compare258 { exec: was.exec, link: was.link },
260 }
261}