@narumitw/pi-sync 0.69.0 → 0.70.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.
- package/CHANGELOG.md +18 -0
- package/README.md +2 -2
- package/dist/chunks/{chunk-EG6BUOUQ.ts → chunk-3XN4FR72.ts} +2 -2
- package/dist/chunks/{chunk-YX6DCQ7P.ts → chunk-5T4ZOHYF.ts} +2 -2
- package/dist/chunks/{chunk-RXZVLBPU.ts → chunk-7NQ7SFZK.ts} +3 -3
- package/dist/chunks/{chunk-2JNFEZ3V.ts → chunk-AMJRUXFT.ts} +6 -2
- package/dist/chunks/{chunk-2JNFEZ3V.ts.map → chunk-AMJRUXFT.ts.map} +1 -1
- package/dist/chunks/{chunk-XNUARYLR.ts → chunk-HFMGK6A6.ts} +2 -2
- package/dist/chunks/{chunk-OWSKD5XG.ts → chunk-JPTI4HFC.ts} +4 -4
- package/dist/chunks/{chunk-RZB2DPVU.ts → chunk-N24HEZSA.ts} +3 -3
- package/dist/chunks/{chunk-EI2J5Z4Q.ts → chunk-SPXEDJ2G.ts} +4 -4
- package/dist/chunks/{chunk-GXB74FYB.ts → chunk-V4IGBXBA.ts} +4 -4
- package/dist/chunks/{chunk-4KGDPLQ4.ts → chunk-ZWP6HAYV.ts} +16 -6
- package/dist/chunks/chunk-ZWP6HAYV.ts.map +7 -0
- package/dist/chunks/{command-handler-ZR26OSBX.ts → command-handler-4XHK5PVX.ts} +20 -15
- package/dist/chunks/command-handler-4XHK5PVX.ts.map +7 -0
- package/dist/chunks/{conflict-review-W5BCZIZG.ts → conflict-review-QI77P7KL.ts} +3 -3
- package/dist/chunks/diff-review-ILRZDQ4S.ts +143 -0
- package/dist/chunks/diff-review-ILRZDQ4S.ts.map +7 -0
- package/dist/chunks/diff-review-SOLP33TQ.ts +68 -0
- package/dist/chunks/diff-review-SOLP33TQ.ts.map +7 -0
- package/dist/chunks/{file-selection-STCIRKF6.ts → file-selection-2H3J2HTX.ts} +4 -4
- package/dist/chunks/{manager-result-dispatcher-MPLPDR2P.ts → manager-result-dispatcher-YXSDLTLV.ts} +5 -5
- package/dist/chunks/{manager-ui-OMZOBGYT.ts → manager-ui-RQSZLMJ5.ts} +19 -19
- package/dist/chunks/manager-ui-RQSZLMJ5.ts.map +7 -0
- package/dist/chunks/{merge-review-KTIQBRLL.ts → merge-review-DXSERTCK.ts} +3 -3
- package/dist/chunks/{ownership-recovery-UDDHSDSZ.ts → ownership-recovery-5KXAMFSL.ts} +2 -2
- package/dist/chunks/{remote-selection-ui-CCHMCAHQ.ts → remote-selection-ui-AE6FOEXH.ts} +4 -4
- package/dist/chunks/{setup-switch-J3YLBB6N.ts → setup-switch-LJPEZONY.ts} +3 -3
- package/dist/chunks/{setup-wizard-J3YOR7BR.ts → setup-wizard-FBX7RTZ6.ts} +4 -4
- package/dist/chunks/{sync-inspection-7OADBR57.ts → sync-inspection-TZ4EMSBN.ts} +3 -3
- package/dist/chunks/{sync-operations-3K6WLPXY.ts → sync-operations-QIQ3G66S.ts} +16 -5
- package/dist/chunks/{sync-operations-3K6WLPXY.ts.map → sync-operations-QIQ3G66S.ts.map} +2 -2
- package/dist/daemon/dist/build-id.json +1 -1
- package/dist/daemon/dist/client.js +131 -5
- package/dist/daemon/dist/daemon-debug.js +13 -6
- package/dist/daemon/dist/diff-review-binding.js +11 -0
- package/dist/daemon/dist/diff-review-protocol.js +85 -0
- package/dist/daemon/dist/diff-review-rpc.js +37 -0
- package/dist/daemon/dist/diff-review-store.js +89 -0
- package/dist/daemon/dist/engine/backends/git/git-backend.js +10 -6
- package/dist/daemon/dist/engine/backends/s3/s3-backend.js +3 -3
- package/dist/daemon/dist/engine/backends/s3/s3-client.js +16 -6
- package/dist/daemon/dist/engine/backends/webdav/webdav-backend.js +3 -3
- package/dist/daemon/dist/engine/backends/webdav/webdav-client.js +23 -10
- package/dist/daemon/dist/engine/snapshot/session-paths.js +12 -3
- package/dist/daemon/dist/engine/snapshot/snapshot-capture-limit.js +99 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-codec.js +4 -1
- package/dist/daemon/dist/engine/snapshot/snapshot-difference.js +13 -0
- package/dist/daemon/dist/engine/snapshot/snapshot.js +44 -17
- package/dist/daemon/dist/engine/sync/diff-review-capture.js +210 -0
- package/dist/daemon/dist/engine/sync/diff-review-preview.js +114 -0
- package/dist/daemon/dist/engine/sync/remote-snapshot.js +6 -6
- package/dist/daemon/dist/engine/sync/sync-queries.js +32 -19
- package/dist/daemon/dist/engine/ui/sync-format.js +14 -24
- package/dist/daemon/dist/engine-service.js +5 -0
- package/dist/daemon/dist/index.js +1 -0
- package/dist/daemon/dist/server.js +112 -36
- package/dist/daemon/package.json +1 -1
- package/dist/index.ts +7 -7
- package/docs/tui-contract.md +1 -1
- package/package.json +3 -3
- package/skills/configure-pi-sync/references/configuration.md +1 -1
- package/src/commands/command-execution.ts +9 -4
- package/src/commands/command-handler.ts +3 -3
- package/src/commands/command-types.ts +1 -0
- package/src/daemon/diff-review.ts +68 -0
- package/src/sync/sync-operations.ts +12 -2
- package/src/ui/cancellable-operation.ts +32 -11
- package/src/ui/diff-review.ts +144 -0
- package/src/ui/manager-ui.ts +7 -6
- package/dist/chunks/chunk-4KGDPLQ4.ts.map +0 -7
- package/dist/chunks/command-handler-ZR26OSBX.ts.map +0 -7
- package/dist/chunks/manager-ui-OMZOBGYT.ts.map +0 -7
- /package/dist/chunks/{chunk-EG6BUOUQ.ts.map → chunk-3XN4FR72.ts.map} +0 -0
- /package/dist/chunks/{chunk-YX6DCQ7P.ts.map → chunk-5T4ZOHYF.ts.map} +0 -0
- /package/dist/chunks/{chunk-RXZVLBPU.ts.map → chunk-7NQ7SFZK.ts.map} +0 -0
- /package/dist/chunks/{chunk-XNUARYLR.ts.map → chunk-HFMGK6A6.ts.map} +0 -0
- /package/dist/chunks/{chunk-OWSKD5XG.ts.map → chunk-JPTI4HFC.ts.map} +0 -0
- /package/dist/chunks/{chunk-RZB2DPVU.ts.map → chunk-N24HEZSA.ts.map} +0 -0
- /package/dist/chunks/{chunk-EI2J5Z4Q.ts.map → chunk-SPXEDJ2G.ts.map} +0 -0
- /package/dist/chunks/{chunk-GXB74FYB.ts.map → chunk-V4IGBXBA.ts.map} +0 -0
- /package/dist/chunks/{conflict-review-W5BCZIZG.ts.map → conflict-review-QI77P7KL.ts.map} +0 -0
- /package/dist/chunks/{file-selection-STCIRKF6.ts.map → file-selection-2H3J2HTX.ts.map} +0 -0
- /package/dist/chunks/{manager-result-dispatcher-MPLPDR2P.ts.map → manager-result-dispatcher-YXSDLTLV.ts.map} +0 -0
- /package/dist/chunks/{merge-review-KTIQBRLL.ts.map → merge-review-DXSERTCK.ts.map} +0 -0
- /package/dist/chunks/{ownership-recovery-UDDHSDSZ.ts.map → ownership-recovery-5KXAMFSL.ts.map} +0 -0
- /package/dist/chunks/{remote-selection-ui-CCHMCAHQ.ts.map → remote-selection-ui-AE6FOEXH.ts.map} +0 -0
- /package/dist/chunks/{setup-switch-J3YLBB6N.ts.map → setup-switch-LJPEZONY.ts.map} +0 -0
- /package/dist/chunks/{setup-wizard-J3YOR7BR.ts.map → setup-wizard-FBX7RTZ6.ts.map} +0 -0
- /package/dist/chunks/{sync-inspection-7OADBR57.ts.map → sync-inspection-TZ4EMSBN.ts.map} +0 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { DIFF_REVIEW_CATALOG_BYTES, DIFF_REVIEW_CATALOG_FILES, DIFF_REVIEW_PATH_DEPTH, isDiffReviewPath, } from "../../diff-review-protocol.js";
|
|
3
|
+
import { DIFF_REVIEW_RETAINED_BYTES } from "../../diff-review-store.js";
|
|
4
|
+
import { OWNERSHIP_CLEANUP_REQUIRED, record } from "../../protocol.js";
|
|
5
|
+
import { configFromSettings, syncCheckConfigFingerprint } from "../settings/config.js";
|
|
6
|
+
import { readLocalConfigForDiagnostics } from "../settings/config-file.js";
|
|
7
|
+
import { assertNoRetiredRuleJournal } from "../settings/retired-ignore-recovery.js";
|
|
8
|
+
import { validateSettingsDocument } from "../settings/settings-validation.js";
|
|
9
|
+
import { createSnapshotCaptureLimits, SnapshotCaptureLimitError } from "../snapshot/snapshot-capture-limit.js";
|
|
10
|
+
import { snapshotDifferences } from "../snapshot/snapshot-difference.js";
|
|
11
|
+
import { withStateDirectoryAccess } from "../state/state-directory.js";
|
|
12
|
+
import { formatDiffCounts, formatDiffRow } from "../ui/sync-format.js";
|
|
13
|
+
import { safeTerminalText, snapshotPathLabel } from "../ui/terminal-text.js";
|
|
14
|
+
import { DIFF_REVIEW_FILE_BASE64_CHARS, DIFF_REVIEW_FILE_BYTES, DIFF_REVIEW_LIMIT_NOTICE, } from "./diff-review-preview.js";
|
|
15
|
+
import { sameLocalFields } from "./local-fields.js";
|
|
16
|
+
import { MAX_AGENT_DIRECTORY_ENTRIES, MAX_AGENT_DIRECTORY_FILES } from "./sync-policy.js";
|
|
17
|
+
import { captureDiffComparison } from "./sync-queries.js";
|
|
18
|
+
const CAPTURE_MS = 30_000;
|
|
19
|
+
const CAPTURE_BYTES = 64 * 1024 * 1024;
|
|
20
|
+
const CAPTURE_LIMIT = "The comparison exceeds the file review limits.";
|
|
21
|
+
const RETAINED_LIMIT = "There is not enough review memory for this comparison.";
|
|
22
|
+
const FAILURE = "Could not capture file differences. Check the selected setup and storage, then reopen the review.";
|
|
23
|
+
const SESSION_NOTICE = "Session and JSONL files are metadata-only. Their contents were not returned.";
|
|
24
|
+
const FIELD_NOTICE = "The local and remote settings field policies differ. Settings contents were not returned.";
|
|
25
|
+
function unavailable(message) {
|
|
26
|
+
return { kind: "unavailable", message };
|
|
27
|
+
}
|
|
28
|
+
function inputFor(change, fieldPoliciesMatch) {
|
|
29
|
+
const path = change.path.toLowerCase();
|
|
30
|
+
if (path.startsWith("sessions/") || path.endsWith(".jsonl"))
|
|
31
|
+
return { notice: SESSION_NOTICE };
|
|
32
|
+
if (path === "settings.json" && !fieldPoliciesMatch)
|
|
33
|
+
return { notice: FIELD_NOTICE };
|
|
34
|
+
const local = change.local?.contentBase64;
|
|
35
|
+
const remote = change.remote?.contentBase64;
|
|
36
|
+
if ([local, remote].some((value) => (value?.length ?? 0) > DIFF_REVIEW_FILE_BASE64_CHARS))
|
|
37
|
+
return { notice: DIFF_REVIEW_LIMIT_NOTICE };
|
|
38
|
+
const left = local === undefined ? undefined : Buffer.from(local, "base64");
|
|
39
|
+
const right = remote === undefined ? undefined : Buffer.from(remote, "base64");
|
|
40
|
+
if (Math.max(left?.length ?? 0, right?.length ?? 0) > DIFF_REVIEW_FILE_BYTES)
|
|
41
|
+
return { notice: DIFF_REVIEW_LIMIT_NOTICE };
|
|
42
|
+
return { local: left, remote: right };
|
|
43
|
+
}
|
|
44
|
+
function copyText(value) {
|
|
45
|
+
return Buffer.from(value, "utf16le").toString("utf16le");
|
|
46
|
+
}
|
|
47
|
+
function boundedSummary(reason, comparison, counts, changes) {
|
|
48
|
+
const { local, remote, warnings, selectionState } = comparison;
|
|
49
|
+
const headline = `${reason} File previews are unavailable. Summary only.`;
|
|
50
|
+
const essential = [
|
|
51
|
+
headline,
|
|
52
|
+
...counts,
|
|
53
|
+
...(selectionState?.kind === "different" ? ["Remote included content differs from this setup."] : []),
|
|
54
|
+
...warnings,
|
|
55
|
+
];
|
|
56
|
+
let used = Buffer.byteLength(JSON.stringify(unavailable(essential.join("\n"))));
|
|
57
|
+
let omitted = !changes;
|
|
58
|
+
const truncation = "More summary details were omitted to stay within the overview limits.";
|
|
59
|
+
const add = (line) => {
|
|
60
|
+
const size = Buffer.byteLength(JSON.stringify(line));
|
|
61
|
+
if (used + size + truncation.length + 2 > DIFF_REVIEW_CATALOG_BYTES) {
|
|
62
|
+
omitted = true;
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
used += size;
|
|
66
|
+
essential.push(line);
|
|
67
|
+
return true;
|
|
68
|
+
};
|
|
69
|
+
for (const line of comparison.lines) {
|
|
70
|
+
if (!warnings.includes(line) && !add(safeTerminalText(line)))
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
if (changes) {
|
|
74
|
+
if (!remote)
|
|
75
|
+
add(`Remote is empty. Local push would upload: ${safeTerminalText(local.id)}`);
|
|
76
|
+
else if (!changes.length)
|
|
77
|
+
add("No file differences.");
|
|
78
|
+
for (const change of changes) {
|
|
79
|
+
if (!isDiffReviewPath(change.path)) {
|
|
80
|
+
omitted = true;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (!add(remote ? formatDiffRow(change) : `Add: ${snapshotPathLabel(change.path)}`))
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (omitted)
|
|
88
|
+
essential.push(truncation);
|
|
89
|
+
return unavailable(essential.join("\n"));
|
|
90
|
+
}
|
|
91
|
+
export function prepareDiffReview(comparison, staleSetup) {
|
|
92
|
+
const { local, remote } = comparison;
|
|
93
|
+
const counts = remote ? formatDiffCounts(local, remote) : [`local: ${local.files.length} files`, "Remote is empty."];
|
|
94
|
+
if (local.files.length + (remote?.files.length ?? 0) > 2 * MAX_AGENT_DIRECTORY_FILES)
|
|
95
|
+
return { unavailable: boundedSummary(CAPTURE_LIMIT, comparison, counts) };
|
|
96
|
+
const changes = snapshotDifferences(local.files, remote?.files);
|
|
97
|
+
const fallback = (reason) => boundedSummary(reason, comparison, counts, changes);
|
|
98
|
+
if (changes.length > DIFF_REVIEW_CATALOG_FILES)
|
|
99
|
+
return { unavailable: fallback(CAPTURE_LIMIT) };
|
|
100
|
+
const lines = [
|
|
101
|
+
...comparison.lines.map(safeTerminalText),
|
|
102
|
+
...counts,
|
|
103
|
+
...(changes.length ? [] : ["No file differences."]),
|
|
104
|
+
"Read-only comparison. − Local, + Remote. Neither side is a proposed transfer.",
|
|
105
|
+
].map(copyText);
|
|
106
|
+
const files = [];
|
|
107
|
+
const inventory = { kind: "review", id: randomUUID(), lines, files };
|
|
108
|
+
const rows = new Map();
|
|
109
|
+
let catalogBytes = Buffer.byteLength(JSON.stringify(inventory));
|
|
110
|
+
if (catalogBytes > DIFF_REVIEW_CATALOG_BYTES)
|
|
111
|
+
return { unavailable: fallback(CAPTURE_LIMIT) };
|
|
112
|
+
let retainedBytes = 0;
|
|
113
|
+
const fieldPoliciesMatch = !remote || sameLocalFields(local.localFields ?? [], remote.localFields ?? []);
|
|
114
|
+
for (const change of changes) {
|
|
115
|
+
const { path, status } = change;
|
|
116
|
+
if (!isDiffReviewPath(path))
|
|
117
|
+
return { unavailable: fallback(CAPTURE_LIMIT) };
|
|
118
|
+
const file = { id: randomUUID(), path: copyText(path), status };
|
|
119
|
+
catalogBytes += Buffer.byteLength(JSON.stringify(file)) + 1;
|
|
120
|
+
if (catalogBytes > DIFF_REVIEW_CATALOG_BYTES)
|
|
121
|
+
return { unavailable: fallback(CAPTURE_LIMIT) };
|
|
122
|
+
const input = inputFor(change, fieldPoliciesMatch);
|
|
123
|
+
if (!("notice" in input))
|
|
124
|
+
retainedBytes += (input.local?.length ?? 0) + (input.remote?.length ?? 0);
|
|
125
|
+
files.push(file);
|
|
126
|
+
rows.set(file.id, input);
|
|
127
|
+
if (retainedBytes + 2 * catalogBytes + 512 * files.length > DIFF_REVIEW_RETAINED_BYTES)
|
|
128
|
+
return { unavailable: fallback(RETAINED_LIMIT) };
|
|
129
|
+
}
|
|
130
|
+
return {
|
|
131
|
+
prepared: { inventory, rows, bytes: retainedBytes + 2 * catalogBytes + 512 * files.length, staleSetup },
|
|
132
|
+
fallback,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function readDiffSetup(root, setup, signal) {
|
|
136
|
+
return withStateDirectoryAccess(async () => {
|
|
137
|
+
signal.throwIfAborted();
|
|
138
|
+
assertNoRetiredRuleJournal(root);
|
|
139
|
+
const observation = await readLocalConfigForDiagnostics(root);
|
|
140
|
+
signal.throwIfAborted();
|
|
141
|
+
if (!observation?.stillCurrent())
|
|
142
|
+
throw new Error("Sync setup is unavailable or changed.");
|
|
143
|
+
assertNoRetiredRuleJournal(root);
|
|
144
|
+
return configFromSettings(validateSettingsDocument(observation.document.parsed), setup);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
function setupFreshness(root, setup, fingerprint) {
|
|
148
|
+
return async (signal) => {
|
|
149
|
+
try {
|
|
150
|
+
const current = await readDiffSetup(root, setup, signal);
|
|
151
|
+
return syncCheckConfigFingerprint(current) !== fingerprint;
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
export async function captureDiffReview(ctx, payload, execution) {
|
|
159
|
+
if (!execution.diffReview || ctx.mode !== "tui" || !ctx.hasUI)
|
|
160
|
+
throw new Error("File previews require a live TUI client.");
|
|
161
|
+
if (!record(payload) ||
|
|
162
|
+
Object.keys(payload).some((key) => key !== "setup" && key !== "fingerprint") ||
|
|
163
|
+
(payload.setup !== undefined && (typeof payload.setup !== "string" || payload.setup.length > 512)) ||
|
|
164
|
+
(payload.fingerprint !== undefined &&
|
|
165
|
+
(typeof payload.fingerprint !== "string" || !/^[0-9a-f]{64}$/u.test(payload.fingerprint) || !payload.setup)))
|
|
166
|
+
throw new Error("Invalid diff review request.");
|
|
167
|
+
execution.diffReview.clear();
|
|
168
|
+
const setup = typeof payload.setup === "string" ? payload.setup : undefined;
|
|
169
|
+
const selectedSetup = payload.fingerprint === undefined ? setup : undefined;
|
|
170
|
+
const deadline = AbortSignal.timeout(CAPTURE_MS);
|
|
171
|
+
const signal = AbortSignal.any([execution.signal, deadline]);
|
|
172
|
+
const changed = () => unavailable("The sync setup changed. Reopen the review.");
|
|
173
|
+
try {
|
|
174
|
+
const config = await readDiffSetup(execution.root, selectedSetup, signal);
|
|
175
|
+
execution.validate();
|
|
176
|
+
signal.throwIfAborted();
|
|
177
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
178
|
+
if (payload.fingerprint !== undefined && (setup !== config.setupName || payload.fingerprint !== fingerprint))
|
|
179
|
+
return changed();
|
|
180
|
+
const staleSetup = setupFreshness(execution.root, selectedSetup, fingerprint);
|
|
181
|
+
const local = createSnapshotCaptureLimits({
|
|
182
|
+
bytes: CAPTURE_BYTES,
|
|
183
|
+
entries: MAX_AGENT_DIRECTORY_ENTRIES,
|
|
184
|
+
files: MAX_AGENT_DIRECTORY_FILES + 1,
|
|
185
|
+
depth: DIFF_REVIEW_PATH_DEPTH,
|
|
186
|
+
});
|
|
187
|
+
const comparison = await withStateDirectoryAccess(() => captureDiffComparison(ctx, config, signal, undefined, { local, remoteBytes: CAPTURE_BYTES }));
|
|
188
|
+
execution.validate();
|
|
189
|
+
const stale = await staleSetup(signal);
|
|
190
|
+
execution.validate();
|
|
191
|
+
signal.throwIfAborted();
|
|
192
|
+
if (stale)
|
|
193
|
+
return changed();
|
|
194
|
+
const result = prepareDiffReview(comparison, staleSetup);
|
|
195
|
+
if ("unavailable" in result)
|
|
196
|
+
return result.unavailable;
|
|
197
|
+
return execution.diffReview.retain(result.prepared) ? result.prepared.inventory : result.fallback(RETAINED_LIMIT);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
if (record(error) && error.code === OWNERSHIP_CLEANUP_REQUIRED) {
|
|
201
|
+
throw Object.assign(new Error("Daemon ownership cleanup is required."), { code: OWNERSHIP_CLEANUP_REQUIRED });
|
|
202
|
+
}
|
|
203
|
+
execution.validate();
|
|
204
|
+
if (deadline.aborted)
|
|
205
|
+
return unavailable("Capturing the comparison timed out. No file previews are available.");
|
|
206
|
+
if (error instanceof SnapshotCaptureLimitError)
|
|
207
|
+
return unavailable(`${CAPTURE_LIMIT} The summary could not be completed within those limits.`);
|
|
208
|
+
return unavailable(FAILURE);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { DIFF_REVIEW_OUTPUT_BYTES, DIFF_REVIEW_OUTPUT_LINES, } from "../../diff-review-protocol.js";
|
|
2
|
+
import { edits, lines, text } from "./text-merge.js";
|
|
3
|
+
export const DIFF_REVIEW_FILE_BYTES = 64 * 1024;
|
|
4
|
+
export const DIFF_REVIEW_FILE_BASE64_CHARS = Math.ceil(DIFF_REVIEW_FILE_BYTES / 3) * 4;
|
|
5
|
+
const MAX_EDIT_CELLS = 1_000_000;
|
|
6
|
+
const CONTEXT = 3;
|
|
7
|
+
export const DIFF_REVIEW_LIMIT_NOTICE = "This file exceeds the text preview limits. No partial patch was returned.";
|
|
8
|
+
function notice(message) {
|
|
9
|
+
return { kind: "notice", message };
|
|
10
|
+
}
|
|
11
|
+
function* hunkGroups(changes) {
|
|
12
|
+
let group = [];
|
|
13
|
+
for (const change of changes) {
|
|
14
|
+
const previous = group.at(-1);
|
|
15
|
+
if (previous && change.start - previous.end > 2 * CONTEXT) {
|
|
16
|
+
yield group;
|
|
17
|
+
group = [];
|
|
18
|
+
}
|
|
19
|
+
group.push(change);
|
|
20
|
+
}
|
|
21
|
+
if (group.length)
|
|
22
|
+
yield group;
|
|
23
|
+
}
|
|
24
|
+
function range(start, count) {
|
|
25
|
+
if (count === 1)
|
|
26
|
+
return String(start + 1);
|
|
27
|
+
return `${count === 0 ? start : start + 1},${count}`;
|
|
28
|
+
}
|
|
29
|
+
class DiffOutputLimitError extends Error {
|
|
30
|
+
}
|
|
31
|
+
function unifiedDiff(base, changes, signal) {
|
|
32
|
+
const output = [];
|
|
33
|
+
let bytes = 0;
|
|
34
|
+
const append = (line) => {
|
|
35
|
+
bytes += Buffer.byteLength(line);
|
|
36
|
+
if (bytes > DIFF_REVIEW_OUTPUT_BYTES || output.length >= DIFF_REVIEW_OUTPUT_LINES)
|
|
37
|
+
throw new DiffOutputLimitError();
|
|
38
|
+
output.push(line);
|
|
39
|
+
};
|
|
40
|
+
const sourceLine = (prefix, line) => {
|
|
41
|
+
if (line.endsWith("\n"))
|
|
42
|
+
append(prefix + line);
|
|
43
|
+
else {
|
|
44
|
+
append(`${prefix}${line}\n`);
|
|
45
|
+
append("\\n");
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
append("--- Local\n");
|
|
49
|
+
append("+++ Remote\n");
|
|
50
|
+
let offset = 0;
|
|
51
|
+
for (const group of hunkGroups(changes)) {
|
|
52
|
+
signal?.throwIfAborted();
|
|
53
|
+
const first = group[0];
|
|
54
|
+
const last = group.at(-1);
|
|
55
|
+
if (!first || !last)
|
|
56
|
+
continue;
|
|
57
|
+
const start = Math.max(0, first.start - CONTEXT);
|
|
58
|
+
const end = Math.min(base.length, last.end + CONTEXT);
|
|
59
|
+
const delta = group.reduce((sum, change) => sum + change.lines.length - (change.end - change.start), 0);
|
|
60
|
+
append(`@@ -${range(start, end - start)} +${range(start + offset, end - start + delta)} @@\n`);
|
|
61
|
+
offset += delta;
|
|
62
|
+
let cursor = start;
|
|
63
|
+
for (const change of group) {
|
|
64
|
+
for (const line of base.slice(cursor, change.start))
|
|
65
|
+
sourceLine(" ", line);
|
|
66
|
+
for (const line of base.slice(change.start, change.end))
|
|
67
|
+
sourceLine("-", line);
|
|
68
|
+
for (const line of change.lines)
|
|
69
|
+
sourceLine("+", line);
|
|
70
|
+
cursor = change.end;
|
|
71
|
+
}
|
|
72
|
+
for (const line of base.slice(cursor, end))
|
|
73
|
+
sourceLine(" ", line);
|
|
74
|
+
}
|
|
75
|
+
return output.join("");
|
|
76
|
+
}
|
|
77
|
+
export function buildDiffReviewPreview(input, signal) {
|
|
78
|
+
signal?.throwIfAborted();
|
|
79
|
+
if ("notice" in input)
|
|
80
|
+
return notice(input.notice);
|
|
81
|
+
const localBytes = input.local ?? Buffer.alloc(0);
|
|
82
|
+
const remoteBytes = input.remote ?? Buffer.alloc(0);
|
|
83
|
+
if (Math.max(localBytes.length, remoteBytes.length) > DIFF_REVIEW_FILE_BYTES)
|
|
84
|
+
return notice(DIFF_REVIEW_LIMIT_NOTICE);
|
|
85
|
+
if (localBytes.length === 0 && remoteBytes.length === 0)
|
|
86
|
+
return notice("This empty file differs only in presence. There is no text hunk.");
|
|
87
|
+
let local;
|
|
88
|
+
let remote;
|
|
89
|
+
try {
|
|
90
|
+
const localText = text(localBytes);
|
|
91
|
+
const remoteText = text(remoteBytes);
|
|
92
|
+
if (/\r(?!\n)/u.test(localText) || /\r(?!\n)/u.test(remoteText))
|
|
93
|
+
throw new Error("Unsupported line endings.");
|
|
94
|
+
local = lines(localText);
|
|
95
|
+
remote = lines(remoteText);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return notice("This file is binary or is not supported UTF-8 text. Its contents were not returned.");
|
|
99
|
+
}
|
|
100
|
+
if (Math.max(local.length, remote.length) > DIFF_REVIEW_OUTPUT_LINES ||
|
|
101
|
+
(local.length + 1) * (remote.length + 1) > MAX_EDIT_CELLS)
|
|
102
|
+
return notice(DIFF_REVIEW_LIMIT_NOTICE);
|
|
103
|
+
const changes = edits(local, remote, signal);
|
|
104
|
+
if (!changes.length)
|
|
105
|
+
return notice("There are no text differences in this file.");
|
|
106
|
+
try {
|
|
107
|
+
return { kind: "diff", content: unifiedDiff(local, changes, signal) };
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
if (error instanceof DiffOutputLimitError)
|
|
111
|
+
return notice(DIFF_REVIEW_LIMIT_NOTICE);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -5,8 +5,8 @@ import { sameLocalFields, validatePortableSnapshot } from "./local-fields.js";
|
|
|
5
5
|
import { throwIfAborted } from "./signals.js";
|
|
6
6
|
import { inspectRemoteSelection, remoteSelectionMismatch, requireLegacyTransferPolicy, sameSnapshotSelection, } from "./sync-policy.js";
|
|
7
7
|
import { syncPolicyChanged } from "./sync-state.js";
|
|
8
|
-
async function readValidatedSnapshotForHead(backend, head, signal) {
|
|
9
|
-
const snapshot = await backend.readSnapshot(head.snapshotRef, signal);
|
|
8
|
+
async function readValidatedSnapshotForHead(backend, head, signal, maxBytes) {
|
|
9
|
+
const snapshot = await backend.readSnapshot(head.snapshotRef, signal, maxBytes);
|
|
10
10
|
throwIfAborted(signal);
|
|
11
11
|
return validateSnapshotForHead(snapshot, head);
|
|
12
12
|
}
|
|
@@ -52,7 +52,7 @@ function safeList(values) {
|
|
|
52
52
|
return values.length > 0 ? values.map(safeTerminalText).join(", ") : "none";
|
|
53
53
|
}
|
|
54
54
|
export async function readRemoteSnapshot(backend, config, signal, options = {}) {
|
|
55
|
-
const observed = await readCurrentSnapshot(backend, config.scope === "agent-directory", signal);
|
|
55
|
+
const observed = await readCurrentSnapshot(backend, config.scope === "agent-directory", signal, options.maxBytes);
|
|
56
56
|
if (!observed)
|
|
57
57
|
return { head: undefined, snapshot: undefined, selectionState: undefined };
|
|
58
58
|
const { head, snapshot: rawSnapshot } = observed;
|
|
@@ -64,9 +64,9 @@ export async function readRemoteSnapshot(backend, config, signal, options = {})
|
|
|
64
64
|
const snapshot = filterSnapshotForConfigPolicy(rawSnapshot, config, { sessionDir: options.sessionDir });
|
|
65
65
|
return { head, snapshot, selectionState };
|
|
66
66
|
}
|
|
67
|
-
async function readCurrentSnapshot(backend, singleObservation, signal) {
|
|
67
|
+
async function readCurrentSnapshot(backend, singleObservation, signal, maxBytes) {
|
|
68
68
|
if (singleObservation && backend.readHeadAndSnapshot) {
|
|
69
|
-
const observed = await backend.readHeadAndSnapshot(signal);
|
|
69
|
+
const observed = await backend.readHeadAndSnapshot(signal, maxBytes);
|
|
70
70
|
throwIfAborted(signal);
|
|
71
71
|
if (observed)
|
|
72
72
|
validateSnapshotForHead(observed.snapshot, observed.head);
|
|
@@ -74,7 +74,7 @@ async function readCurrentSnapshot(backend, singleObservation, signal) {
|
|
|
74
74
|
}
|
|
75
75
|
const head = await backend.readHead(signal);
|
|
76
76
|
throwIfAborted(signal);
|
|
77
|
-
return head && { head, snapshot: await readValidatedSnapshotForHead(backend, head, signal) };
|
|
77
|
+
return head && { head, snapshot: await readValidatedSnapshotForHead(backend, head, signal, maxBytes) };
|
|
78
78
|
}
|
|
79
79
|
export async function readSnapshotForHead(backend, head, signal) {
|
|
80
80
|
const snapshot = await readValidatedSnapshotForHead(backend, head, signal);
|
|
@@ -88,31 +88,44 @@ export async function diff(ctx, options, factory = createSyncBackend) {
|
|
|
88
88
|
const config = await loadConfig(options.setup);
|
|
89
89
|
throwIfAborted(options.signal);
|
|
90
90
|
setSyncStatus(ctx, `checking ${config.setupName}`, { activity: "checking" });
|
|
91
|
-
const
|
|
92
|
-
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
93
|
-
const local = portableSnapshot(await createSnapshot(config.snapshotIdentity, snapshotOptions), config.localFields);
|
|
94
|
-
throwIfAborted(options.signal);
|
|
95
|
-
const { snapshot: remote, selectionState } = await readRemoteSnapshot(backend, config, options.signal, {
|
|
96
|
-
allowSelectionDifference: true,
|
|
97
|
-
sessionDir: snapshotOptions.sessionDir,
|
|
98
|
-
});
|
|
99
|
-
throwIfAborted(options.signal);
|
|
91
|
+
const { local, remote, lines, warnings, selectionState } = await captureDiffComparison(ctx, config, options.signal, factory);
|
|
100
92
|
setSyncStatus(ctx, undefined);
|
|
101
|
-
const
|
|
102
|
-
const header = [
|
|
103
|
-
`sync setup: ${config.setupName}`,
|
|
104
|
-
`storage connection: ${config.connectionName}`,
|
|
105
|
-
`storage location: ${safeTerminalText(backend.destination)}`,
|
|
106
|
-
...contentLines(config),
|
|
107
|
-
formatRemoteSelectionStatus(selectionState),
|
|
108
|
-
...warnings,
|
|
109
|
-
].join("\n");
|
|
93
|
+
const header = lines.join("\n");
|
|
110
94
|
const level = warnings.length > 0 || selectionState?.kind === "different" ? "warning" : "info";
|
|
111
95
|
if (!remote) {
|
|
112
96
|
ctx.ui.notify(`${header}\n\n${formatSnapshotOnlyDiff("Remote is empty. Local push would upload", local)}`, level);
|
|
113
97
|
return;
|
|
114
98
|
}
|
|
115
|
-
ctx.ui.notify(`${header}\n\n${formatDiff(local,
|
|
99
|
+
ctx.ui.notify(`${header}\n\n${formatDiff(local, remote)}`, level);
|
|
100
|
+
}
|
|
101
|
+
export async function captureDiffComparison(ctx, config, signal, factory = createSyncBackend, limits) {
|
|
102
|
+
const backend = await factory(config);
|
|
103
|
+
throwIfAborted(signal);
|
|
104
|
+
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, signal);
|
|
105
|
+
throwIfAborted(signal);
|
|
106
|
+
const local = portableSnapshot(await createSnapshot(config.snapshotIdentity, snapshotOptions, limits?.local), config.localFields);
|
|
107
|
+
throwIfAborted(signal);
|
|
108
|
+
const { snapshot: remote, selectionState } = await readRemoteSnapshot(backend, config, signal, {
|
|
109
|
+
allowSelectionDifference: true,
|
|
110
|
+
sessionDir: snapshotOptions.sessionDir,
|
|
111
|
+
maxBytes: limits?.remoteBytes,
|
|
112
|
+
});
|
|
113
|
+
throwIfAborted(signal);
|
|
114
|
+
const warnings = syncSessionsWarnings(config);
|
|
115
|
+
return {
|
|
116
|
+
local,
|
|
117
|
+
remote: remote && portableSnapshot(remote, remote.localFields),
|
|
118
|
+
warnings,
|
|
119
|
+
selectionState,
|
|
120
|
+
lines: [
|
|
121
|
+
`sync setup: ${config.setupName}`,
|
|
122
|
+
`storage connection: ${config.connectionName}`,
|
|
123
|
+
`storage location: ${safeTerminalText(backend.destination)}`,
|
|
124
|
+
...contentLines(config),
|
|
125
|
+
...formatRemoteSelectionStatus(selectionState).split("\n"),
|
|
126
|
+
...warnings,
|
|
127
|
+
],
|
|
128
|
+
};
|
|
116
129
|
}
|
|
117
130
|
export async function doctor(ctx, options, factory = createSyncBackend) {
|
|
118
131
|
const attention = [];
|
|
@@ -1,35 +1,25 @@
|
|
|
1
1
|
import { agentDir } from "../snapshot/session-paths.js";
|
|
2
|
+
import { snapshotDifferences } from "../snapshot/snapshot-difference.js";
|
|
2
3
|
import { inspectRemoteSelection } from "../sync/sync-policy.js";
|
|
3
|
-
import { fileHashMap } from "../sync/sync-state.js";
|
|
4
4
|
import { safeTerminalText, snapshotPathLabel } from "./terminal-text.js";
|
|
5
5
|
import { transferChanges, transferCounts } from "./transfer-changes.js";
|
|
6
6
|
export function formatDiff(local, remote) {
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
const changes = snapshotDifferences(local.files, remote.files);
|
|
8
|
+
return [
|
|
9
|
+
...formatDiffCounts(local, remote),
|
|
10
|
+
"",
|
|
11
|
+
...(changes.length ? changes.map(formatDiffRow) : ["No file differences."]),
|
|
12
|
+
].join("\n");
|
|
13
|
+
}
|
|
14
|
+
export function formatDiffCounts(local, remote) {
|
|
15
|
+
return [
|
|
11
16
|
`local: ${local.files.length} files`,
|
|
12
17
|
`remote: ${safeTerminalText(remote.id)} (${remote.files.length} files)`,
|
|
13
|
-
"",
|
|
14
18
|
];
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
changed += 1;
|
|
20
|
-
}
|
|
21
|
-
else if (!Object.hasOwn(remoteMap, filePath)) {
|
|
22
|
-
lines.push(`Local only: ${snapshotPathLabel(filePath)}`);
|
|
23
|
-
changed += 1;
|
|
24
|
-
}
|
|
25
|
-
else if (localMap[filePath] !== remoteMap[filePath]) {
|
|
26
|
-
lines.push(`Different: ${snapshotPathLabel(filePath)}`);
|
|
27
|
-
changed += 1;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (changed === 0)
|
|
31
|
-
lines.push("No file differences.");
|
|
32
|
-
return lines.join("\n");
|
|
19
|
+
}
|
|
20
|
+
export function formatDiffRow(change) {
|
|
21
|
+
const label = { "local-only": "Local only", "remote-only": "Remote only", different: "Different" };
|
|
22
|
+
return `${label[change.status]}: ${snapshotPathLabel(change.path)}`;
|
|
33
23
|
}
|
|
34
24
|
export function formatSnapshotOnlyDiff(title, snapshot) {
|
|
35
25
|
return [
|
|
@@ -57,6 +57,11 @@ export function createEngineService(root) {
|
|
|
57
57
|
execution.validate();
|
|
58
58
|
if (operation !== "settings.repair-read" && operation !== "monitor.pause")
|
|
59
59
|
assertNoRetiredRuleJournal();
|
|
60
|
+
if (operation === "diff.review") {
|
|
61
|
+
const { captureDiffReview } = await import("./engine/sync/diff-review-capture.js");
|
|
62
|
+
execution.validate();
|
|
63
|
+
return captureDiffReview(createServiceContext(execution), payload, execution);
|
|
64
|
+
}
|
|
60
65
|
if (operation === "daemon.authorize") {
|
|
61
66
|
return Boolean(await authorizeAfterExit(execution, payload));
|
|
62
67
|
}
|
|
@@ -2,6 +2,7 @@ export { DaemonConnection, DaemonError, ensureDaemon, findDaemon, requestDaemon,
|
|
|
2
2
|
export { inspectDaemonRecovery, stopDaemonForRecovery, } from "./daemon-recovery.js";
|
|
3
3
|
export { DaemonUpgradeBlockedError } from "./daemon-startup.js";
|
|
4
4
|
export { inspectDaemonStatus } from "./daemon-status.js";
|
|
5
|
+
export { parseDiffReviewInventory } from "./diff-review-protocol.js";
|
|
5
6
|
export { documentFingerprint } from "./document.js";
|
|
6
7
|
export { isDestinationConflict } from "./foreground-transfer.js";
|
|
7
8
|
export { modelSignature } from "./model-signature.js";
|