@parall/cli 1.58.2 → 1.60.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,240 @@
1
+ /**
2
+ * Reconciling a local wiki workspace with a server that moved on:
3
+ *
4
+ * - a line-level three-way text merge, used by `wiki sync` when both the
5
+ * server and the workspace changed the same file: non-overlapping edits
6
+ * merge silently, overlapping ones are written into the working file as
7
+ * `<<<<<<< mine (parall-merge)` / `======= (parall-merge)` /
8
+ * `>>>>>>> latest (parall-merge)` blocks — git's shape, the one every
9
+ * agent already knows how to edit, plus a tag so a quoted example of the
10
+ * block in the document is never mistaken for a delimiter;
11
+ * - the propose-time backstop that refuses a file still carrying such a
12
+ * block;
13
+ * - the stale-base recovery policy `changeset create` follows before it
14
+ * hands the situation back to the agent.
15
+ *
16
+ * Everything here is pure (no filesystem, no network) so the policy is
17
+ * unit-testable without a mock wiki-service.
18
+ *
19
+ * Format twin: `ts/app/src/lib/wikiThreeWayMerge.ts` (the Web editor's draft
20
+ * merge, landing with PR #2724) is written to generate and recognise the exact
21
+ * same delimiter sets under the same rules. The two packages cannot share code
22
+ * (the CLI does not depend on the app), so each carries its own "format
23
+ * constants" test over one shared fixture; nothing here can enforce the other
24
+ * side. Keep them in lockstep: a human resolving in the browser and an agent
25
+ * resolving in a workspace must see, and be able to hand each other, the same
26
+ * bytes.
27
+ */
28
+ import { ApiError } from '@parall/sdk';
29
+ import type { SyncConflict, WikiSyncOutcome } from './wiki-shared.js';
30
+ /** Server error code: a file_change's base_sha no longer matches the default branch. */
31
+ export declare const STALE_BASE_CODE = "STALE_BASE";
32
+ /**
33
+ * How many times one propose call may recover from a stale base on its own.
34
+ * One covers "the server moved since my last sync". A second stale hit right
35
+ * after a fresh re-sync means someone is publishing to the same files right
36
+ * now — that is a decision for the caller, not a retry loop.
37
+ */
38
+ export declare const MAX_STALE_RECOVERIES = 1;
39
+ export type ThreeWayMergeResult = {
40
+ kind: 'clean';
41
+ merged: Buffer;
42
+ } | {
43
+ kind: 'conflict';
44
+ conflictRegions: number;
45
+ /** Delimiter set (1-based) the blocks use — recorded by sync as the path's live set. */
46
+ set: number;
47
+ delimiters: ConflictDelimiters;
48
+ /**
49
+ * The document with every overlapping region wrapped in the document's
50
+ * own delimiter set (`<<<<<<< mine (parall-merge)` / `======= (parall-merge)`
51
+ * / `>>>>>>> latest (parall-merge)`, or a higher-numbered set when the
52
+ * content quotes that one) — non-overlapping hunks from both sides
53
+ * already merged around them. This is what `sync` writes into the
54
+ * working file.
55
+ */
56
+ marked: Buffer;
57
+ } | {
58
+ kind: 'not-mergeable';
59
+ reason: 'binary' | 'lfs-pointer' | 'too-complex' | 'delimiter-sets-exhausted';
60
+ };
61
+ /** Marker labels are agent-facing (they land in the working file). */
62
+ export declare const MERGE_LABEL_MINE = "mine";
63
+ export declare const MERGE_LABEL_THEIRS = "latest";
64
+ /** The three delimiter lines of one marker set (without line terminators). */
65
+ export interface ConflictDelimiters {
66
+ open: string;
67
+ sep: string;
68
+ close: string;
69
+ }
70
+ /**
71
+ * Delimiter set `n` (1-based). Set 1 is `<<<<<<< mine (parall-merge)` /
72
+ * `======= (parall-merge)` / `>>>>>>> latest (parall-merge)`; higher sets add
73
+ * `-n` to the tag, MIME-boundary style, and are only used when a document
74
+ * happens to contain set 1 verbatim (say, a wiki page documenting this very
75
+ * feature).
76
+ */
77
+ export declare function conflictDelimiters(n?: number): ConflictDelimiters;
78
+ /**
79
+ * Highest delimiter set this CLI generates or recognises. The set number is
80
+ * parsed with `Number()`, so an unbounded digit run in content could yield
81
+ * `Infinity` (which JSON-serialises to `null` and would blank the manifest's
82
+ * `conflict_set`) or lose integer precision (so `max + 1` would not move) —
83
+ * either way the guards would stop recognising the block sync itself wrote.
84
+ * Tags with more digits than this are content, and a document that already
85
+ * quotes set {@link MAX_CONFLICT_SET} cannot get an in-file merge at all: the
86
+ * merge reports `delimiter-sets-exhausted` and sync falls back to the
87
+ * `.remote` copy, exactly like binary content.
88
+ */
89
+ export declare const MAX_CONFLICT_SET = 999999;
90
+ /**
91
+ * Delimiter set for a document: one past the highest set any content line
92
+ * belongs to (any of the three shapes). Content can therefore only ever carry
93
+ * LOWER sets than the delimiters around it. Sync records the chosen set on the
94
+ * manifest entry (`conflict_set`) and every guard asks for exactly that set
95
+ * (`hasDelimiterLine`), so quoted marker text anywhere in the document stays
96
+ * content.
97
+ */
98
+ export declare function pickSet(content: Iterable<string>): number;
99
+ export declare function pickDelimiters(content: Iterable<string>): ConflictDelimiters;
100
+ /**
101
+ * Upper bound on the line-match pairs one diff may process. node-diff3's LCS
102
+ * walks every (line in A, identical line in B) pair, so a file with many
103
+ * repeated lines (blank lines, table separators) costs occurrences² — about
104
+ * 1 s per million pairs on a laptop, 7–8 s at four million. Above the budget
105
+ * the merge is skipped in favour of the conflict marker: slow but correct is
106
+ * still a hang from the agent's point of view.
107
+ */
108
+ export declare const MERGE_MATCH_PAIR_BUDGET = 1000000;
109
+ /**
110
+ * Upper bound on lines per side. Independently of repeated lines, node-diff3
111
+ * scans a growing candidate array once per matched line, so even all-unique
112
+ * files cost roughly lines²: ≈90 ms at 10,000 lines and ≈200 ms at 15,000
113
+ * on a laptop (both sides editing distant regions), ≈4.7 s at 50,000 in
114
+ * review. 15,000 lines is far above any wiki document that is still a
115
+ * document; beyond it the merge is skipped for the conflict marker.
116
+ */
117
+ export declare const MERGE_MAX_LINES = 15000;
118
+ /**
119
+ * Number of (i, j) pairs with a[i] === b[j] — the work an LCS over the two
120
+ * line arrays performs — computed in O(|a| + |b|) from line multiplicities.
121
+ */
122
+ export declare function countMatchPairs(a: readonly string[], b: readonly string[]): number;
123
+ /**
124
+ * Merge `local` and `remote`, both derived from `base`, at line granularity.
125
+ * Clean when every changed region was touched by one side only, or both
126
+ * sides made the identical change; overlapping hunks are never auto-resolved.
127
+ * diff3 semantics: two changed regions overlap when no unchanged line
128
+ * separates them, so adjacent edits conflict even if they touch different
129
+ * lines — that is deliberate (git behaves the same) and the skill text says
130
+ * so.
131
+ * Binary content and Git LFS pointers are refused — a line merge of either
132
+ * would corrupt the file — so the caller keeps its conflict-marker path.
133
+ */
134
+ export declare function mergeTextThreeWay(input: {
135
+ base: Buffer;
136
+ local: Buffer;
137
+ remote: Buffer;
138
+ isText: (content: Buffer) => boolean;
139
+ }): ThreeWayMergeResult;
140
+ /**
141
+ * True when `text` carries ANY delimiter line of set `set` — its opener,
142
+ * separator or closer, byte-exact (terminator stripped), complete block or
143
+ * not.
144
+ *
145
+ * This is THE predicate behind every decision about a conflicted path: the
146
+ * propose-time refusal (early scan and the encode-time re-check), sync's
147
+ * "markers still unresolved → don't merge again" guard, and dropping the
148
+ * recorded set once the agent has edited the block away. Callers pass the set
149
+ * sync recorded on the manifest entry (`conflict_set`) when it wrote the
150
+ * block, so:
151
+ *
152
+ * - a half-finished hand merge that deleted only the closer still blocks
153
+ * propose — a lone `<<<<<<< mine (parall-merge)` must never land in the
154
+ * wiki (same rule as the Web editor's publish guard, its round 5);
155
+ * - every OTHER set is content: a page that quotes a complete set-1 block
156
+ * verbatim conflicts with set 2 and, once resolved, proposes — only the
157
+ * recorded set is live. The Web draft's `merge_conflict` flag plays the
158
+ * role the manifest's `conflict_set` plays here;
159
+ * - a Setext `=======`, a git-style `<<<<<<< HEAD … >>>>>>> latest` example
160
+ * and the bare `<<<<<<< mine` this CLI used to emit never match: they carry
161
+ * no `(parall-merge…)` tag.
162
+ *
163
+ * Paths without a recorded set are not scanned at all.
164
+ */
165
+ export declare function hasDelimiterLine(text: string, set: number): boolean;
166
+ export type StalePath = {
167
+ path: string;
168
+ /** What the server did to this path since the workspace last synced it. */
169
+ reason: 'modified' | 'deleted' | 'created';
170
+ };
171
+ /**
172
+ * Compare each locally changed path's sync baseline against the server's
173
+ * current blob SHA. A locally created file is stale when the server now has
174
+ * one at that path; an update/delete is stale when the server's copy changed
175
+ * or vanished.
176
+ */
177
+ export declare function findStalePaths(input: {
178
+ changedPaths: Iterable<string>;
179
+ /** Sync-baseline blob SHA; undefined for a locally created file. */
180
+ baseSha: (path: string) => string | undefined;
181
+ /** Current server blob SHA; undefined when the server has no such path. */
182
+ serverSha: (path: string) => string | undefined;
183
+ }): StalePath[];
184
+ export declare function describeStalePath(stale: StalePath): string;
185
+ export type StaleRecoveryDecision = {
186
+ kind: 'retry';
187
+ } | {
188
+ kind: 'stop';
189
+ reason: 'failures' | 'conflicts' | 'retries-exhausted';
190
+ };
191
+ /**
192
+ * After a stale-base hit and a re-sync, decide whether propose may run again.
193
+ * Failures and conflicts always win over the retry budget: an exhausted
194
+ * budget with a clean sync means "the server keeps moving", but leftover
195
+ * conflicts are the more useful thing to tell the agent about.
196
+ */
197
+ export declare function decideStaleRecovery(input: {
198
+ /** Number of automatic recoveries already spent in this propose call. */
199
+ attempt: number;
200
+ outcome: WikiSyncOutcome;
201
+ }): StaleRecoveryDecision;
202
+ /** What an automatic stale-base recovery did, reported alongside the propose result. */
203
+ export type StaleRecoveryReport = {
204
+ /** Locally changed paths whose server copy had moved since the last sync. */
205
+ stale_paths: string[];
206
+ /** Who noticed: the CLI precheck against the server manifest, or the server's 409. */
207
+ detected_by: 'precheck' | 'server';
208
+ /** What the automatic re-sync did before re-proposing. */
209
+ sync: {
210
+ applied: number;
211
+ fast_forwarded: number;
212
+ merged: string[];
213
+ };
214
+ };
215
+ export declare function isStaleBaseError(err: unknown): err is ApiError;
216
+ /**
217
+ * Agent-facing text for a recovery that stopped on conflicts.
218
+ *
219
+ * There is no resolution recipe any more. `sync` has already moved each
220
+ * conflicted path's baseline to the server's version and left the working tree
221
+ * saying what the agent meant (marker blocks to edit, a kept file, a kept
222
+ * delete), so the only step left is "make the file say what you want, then
223
+ * propose". Every artifact the agent might need is named with its absolute
224
+ * path.
225
+ */
226
+ export declare function formatStaleRecoveryError(input: {
227
+ stale: StalePath[];
228
+ decision: Extract<StaleRecoveryDecision, {
229
+ kind: 'stop';
230
+ }>;
231
+ outcome: WikiSyncOutcome;
232
+ }): string;
233
+ /**
234
+ * Text for the propose-time backstop: files that still carry a marker block.
235
+ * Fail-closed and local — nothing was sent to the server.
236
+ */
237
+ export declare function formatUnresolvedMarkersError(paths: string[]): string;
238
+ /** One line per conflict: what happened, what the working tree now holds, where the artifacts are. */
239
+ export declare function describeConflict(conflict: SyncConflict): string;
240
+ //# sourceMappingURL=wiki-merge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wiki-merge.d.ts","sourceRoot":"","sources":["../../src/lib/wiki-merge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAEvC,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEtE,wFAAwF;AACxF,eAAO,MAAM,eAAe,eAAe,CAAC;AAE5C;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,IAAI,CAAC;AAMtC,MAAM,MAAM,mBAAmB,GAC3B;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACjC;IACE,IAAI,EAAE,UAAU,CAAC;IACjB,eAAe,EAAE,MAAM,CAAC;IACxB,wFAAwF;IACxF,GAAG,EAAE,MAAM,CAAC;IACZ,UAAU,EAAE,kBAAkB,CAAC;IAC/B;;;;;;;OAOG;IACH,MAAM,EAAE,MAAM,CAAC;CAChB,GACD;IACE,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,QAAQ,GAAG,aAAa,GAAG,aAAa,GAAG,0BAA0B,CAAC;CAC/E,CAAC;AAEN,sEAAsE;AACtE,eAAO,MAAM,gBAAgB,SAAS,CAAC;AACvC,eAAO,MAAM,kBAAkB,WAAW,CAAC;AAE3C,8EAA8E;AAC9E,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;CAC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