@narumitw/pi-sync 0.69.1 → 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 +11 -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/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 +97 -28
- 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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/sync/sync-operations.ts"],
|
|
4
|
-
"sourcesContent": ["import type { ExtensionContext } from \"@earendil-works/pi-coding-agent\";\nimport type { CommandOptions } from \"../commands/command-types.js\";\nimport { remoteOperation } from \"../daemon/pi-client.js\";\nimport type { SetupPullOutcome } from \"./setup-switch.js\";\n\nfunction transferResult(result: unknown): SetupPullOutcome | undefined {\n if (result === undefined || result === \"applied\" || result === \"cancelled\") return result;\n throw new Error(\"Invalid transfer result from pi-sync-daemon.\");\n}\nexport const push = async (ctx: ExtensionContext, options: CommandOptions) =>\n transferResult(await remoteOperation(ctx, \"push\", options));\nexport const pull = async (ctx: ExtensionContext, options: CommandOptions) =>\n transferResult(await remoteOperation(ctx, \"pull\", options));\nexport async function syncBoth(ctx: ExtensionContext, options: CommandOptions) {\n const result = await remoteOperation(ctx, options.auto ? \"startup-transfer\" : \"sync\", options);\n if (\n result === \"archived\" &&\n !options.auto &&\n !options.agentRetryRefusal &&\n !options.yes &&\n ctx.hasUI &&\n (ctx.mode === \"tui\" || ctx.mode === \"rpc\")\n )\n return result;\n return transferResult(result);\n}\nexport async function rollback(ctx: ExtensionContext, options: CommandOptions) {\n await remoteOperation(ctx, \"rollback\", options);\n}\nexport
|
|
5
|
-
"mappings": ";;;;;;;;;;;AAKA,SAAS,eAAe,QAA+C;AACrE,MAAI,WAAW,UAAa,WAAW,aAAa,WAAW,YAAa,QAAO;AACnF,QAAM,IAAI,MAAM,8CAA8C;AAChE;AACO,IAAM,OAAO,OAAO,KAAuB,YAChD,eAAe,MAAM,gBAAgB,KAAK,QAAQ,OAAO,CAAC;AACrD,IAAM,OAAO,OAAO,KAAuB,YAChD,eAAe,MAAM,gBAAgB,KAAK,QAAQ,OAAO,CAAC;AAC5D,eAAsB,SAAS,KAAuB,SAAyB;AAC7E,QAAM,SAAS,MAAM,gBAAgB,KAAK,QAAQ,OAAO,qBAAqB,QAAQ,OAAO;AAC7F,MACE,WAAW,cACX,CAAC,QAAQ,QACT,CAAC,QAAQ,qBACT,CAAC,QAAQ,OACT,IAAI,UACH,IAAI,SAAS,SAAS,IAAI,SAAS;AAEpC,WAAO;AACT,SAAO,eAAe,MAAM;AAC9B;AACA,eAAsB,SAAS,KAAuB,SAAyB;AAC7E,QAAM,gBAAgB,KAAK,YAAY,OAAO;AAChD;
|
|
4
|
+
"sourcesContent": ["import type { ExtensionContext } from \"@earendil-works/pi-coding-agent\";\nimport type { CommandOptions } from \"../commands/command-types.js\";\nimport { remoteOperation } from \"../daemon/pi-client.js\";\nimport type { SetupPullOutcome } from \"./setup-switch.js\";\n\nfunction transferResult(result: unknown): SetupPullOutcome | undefined {\n if (result === undefined || result === \"applied\" || result === \"cancelled\") return result;\n throw new Error(\"Invalid transfer result from pi-sync-daemon.\");\n}\nexport const push = async (ctx: ExtensionContext, options: CommandOptions) =>\n transferResult(await remoteOperation(ctx, \"push\", options));\nexport const pull = async (ctx: ExtensionContext, options: CommandOptions) =>\n transferResult(await remoteOperation(ctx, \"pull\", options));\nexport async function syncBoth(ctx: ExtensionContext, options: CommandOptions) {\n const result = await remoteOperation(ctx, options.auto ? \"startup-transfer\" : \"sync\", options);\n if (\n result === \"archived\" &&\n !options.auto &&\n !options.agentRetryRefusal &&\n !options.yes &&\n ctx.hasUI &&\n (ctx.mode === \"tui\" || ctx.mode === \"rpc\")\n )\n return result;\n return transferResult(result);\n}\nexport async function rollback(ctx: ExtensionContext, options: CommandOptions) {\n await remoteOperation(ctx, \"rollback\", options);\n}\nexport function diff(ctx: ExtensionContext, options: CommandOptions) {\n if (ctx.mode !== \"tui\" || !ctx.hasUI) return remoteOperation(ctx, \"diff\", options).then(() => {});\n return Promise.all([import(\"../daemon/diff-review.js\"), import(\"../ui/diff-review.js\")]).then(\n ([{ openDiffReview }, { showSyncDiffReview }]) => {\n options.signal?.throwIfAborted();\n return showSyncDiffReview(\n ctx,\n (signal) => openDiffReview(ctx, options.setup, options.diffReviewFingerprint, signal),\n options.signal,\n );\n },\n );\n}\nexport async function history(ctx: ExtensionContext, options: CommandOptions) {\n await remoteOperation(ctx, \"history\", options);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;AAKA,SAAS,eAAe,QAA+C;AACrE,MAAI,WAAW,UAAa,WAAW,aAAa,WAAW,YAAa,QAAO;AACnF,QAAM,IAAI,MAAM,8CAA8C;AAChE;AACO,IAAM,OAAO,OAAO,KAAuB,YAChD,eAAe,MAAM,gBAAgB,KAAK,QAAQ,OAAO,CAAC;AACrD,IAAM,OAAO,OAAO,KAAuB,YAChD,eAAe,MAAM,gBAAgB,KAAK,QAAQ,OAAO,CAAC;AAC5D,eAAsB,SAAS,KAAuB,SAAyB;AAC7E,QAAM,SAAS,MAAM,gBAAgB,KAAK,QAAQ,OAAO,qBAAqB,QAAQ,OAAO;AAC7F,MACE,WAAW,cACX,CAAC,QAAQ,QACT,CAAC,QAAQ,qBACT,CAAC,QAAQ,OACT,IAAI,UACH,IAAI,SAAS,SAAS,IAAI,SAAS;AAEpC,WAAO;AACT,SAAO,eAAe,MAAM;AAC9B;AACA,eAAsB,SAAS,KAAuB,SAAyB;AAC7E,QAAM,gBAAgB,KAAK,YAAY,OAAO;AAChD;AACO,SAAS,KAAK,KAAuB,SAAyB;AACnE,MAAI,IAAI,SAAS,SAAS,CAAC,IAAI,MAAO,QAAO,gBAAgB,KAAK,QAAQ,OAAO,EAAE,KAAK,MAAM;AAAA,EAAC,CAAC;AAChG,SAAO,QAAQ,IAAI,CAAC,OAAO,2BAA0B,GAAG,OAAO,2BAAsB,CAAC,CAAC,EAAE;AAAA,IACvF,CAAC,CAAC,EAAE,eAAe,GAAG,EAAE,mBAAmB,CAAC,MAAM;AAChD,cAAQ,QAAQ,eAAe;AAC/B,aAAO;AAAA,QACL;AAAA,QACA,CAAC,WAAW,eAAe,KAAK,QAAQ,OAAO,QAAQ,uBAAuB,MAAM;AAAA,QACpF,QAAQ;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;AACA,eAAsB,QAAQ,KAAuB,SAAyB;AAC5E,QAAM,gBAAgB,KAAK,WAAW,OAAO;AAC/C;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"
|
|
1
|
+
"f8b913923befa9472a13eb6663c5a8ac9d4aadb5a69499d0b2258135abc1cd38"
|
|
@@ -2,11 +2,13 @@ import { createHash, randomUUID } from "node:crypto";
|
|
|
2
2
|
import { setTimeout as delay } from "node:timers/promises";
|
|
3
3
|
import { DaemonError, requestDaemon } from "./client-transport.js";
|
|
4
4
|
import { stopDaemonAfterForegroundQueueFailure } from "./daemon-recovery.js";
|
|
5
|
+
import { sameDiffReviewBinding } from "./diff-review-binding.js";
|
|
6
|
+
import { DIFF_REVIEW_REQUEST_MS, isDiffReviewId, parseDiffReviewInventory, readDiffReviewPreview, } from "./diff-review-protocol.js";
|
|
5
7
|
import { isForegroundDirection, isForegroundQueueOutcome } from "./foreground-queue-outcome.js";
|
|
6
8
|
import { canonicalRootGuard, readProcessBirth } from "./identity.js";
|
|
7
9
|
import { withCleanup } from "./lifecycle.js";
|
|
8
10
|
import { presentPagedReview, reviewScope } from "./paged-review.js";
|
|
9
|
-
import { MAX_REVIEW_BYTES, operationPrefix, REVIEW_PAGE_CHARS, record, UNKNOWN_JOB_RESULT, } from "./protocol.js";
|
|
11
|
+
import { MAX_REVIEW_BYTES, OWNERSHIP_CLEANUP_REQUIRED, operationPrefix, REVIEW_PAGE_CHARS, record, UNKNOWN_JOB_RESULT, } from "./protocol.js";
|
|
10
12
|
import { STATE_WAIT_FAILURE_LIMIT, stateWaitJobs, stateWaitResult } from "./state-wait-jobs.js";
|
|
11
13
|
export { DaemonError, hello, requestDaemon } from "./client-transport.js";
|
|
12
14
|
export { acquireStartupLock, checkUpgradeConsent, ensureDaemon, findDaemon, finishUpgrade, stopDaemon, waitForDaemonExit, } from "./daemon-startup.js";
|
|
@@ -28,6 +30,9 @@ export class DaemonConnection {
|
|
|
28
30
|
controller = new AbortController();
|
|
29
31
|
callbacks = new Map();
|
|
30
32
|
callbackIO = new AbortController();
|
|
33
|
+
diffReviewIO;
|
|
34
|
+
diffReviewPaused = false;
|
|
35
|
+
diffReviewBinding;
|
|
31
36
|
callbackTasks = new Set();
|
|
32
37
|
updates = Promise.resolve();
|
|
33
38
|
attached = false;
|
|
@@ -61,6 +66,16 @@ export class DaemonConnection {
|
|
|
61
66
|
this.facts = facts;
|
|
62
67
|
this.options = options;
|
|
63
68
|
}
|
|
69
|
+
setFacts(facts) {
|
|
70
|
+
const changed = !sameDiffReviewBinding(this.facts, facts);
|
|
71
|
+
for (const key of Object.keys(this.facts))
|
|
72
|
+
if (!Object.hasOwn(facts, key))
|
|
73
|
+
delete this.facts[key];
|
|
74
|
+
Object.assign(this.facts, facts);
|
|
75
|
+
this.diffReviewBinding = { ...facts };
|
|
76
|
+
if (changed)
|
|
77
|
+
this.retireDiffReviewIO();
|
|
78
|
+
}
|
|
64
79
|
/** Latest owner observation, or undefined if contact is unsupported or not observed. */
|
|
65
80
|
get contactState() {
|
|
66
81
|
return this.observedContact;
|
|
@@ -82,9 +97,13 @@ export class DaemonConnection {
|
|
|
82
97
|
this.disposing)
|
|
83
98
|
throw new Error("Daemon client is unavailable; reconnect after draining work.");
|
|
84
99
|
this.admitting = true;
|
|
100
|
+
this.diffReviewPaused = false;
|
|
101
|
+
this.diffReviewBinding = undefined;
|
|
102
|
+
this.retireDiffReviewIO();
|
|
85
103
|
this.lifecycleRevision++;
|
|
86
104
|
this.contactRevision++;
|
|
87
|
-
const
|
|
105
|
+
const facts = { ...this.facts };
|
|
106
|
+
const request = requestDaemon(this.paths, this.identity, "attach", facts, signal ? AbortSignal.any([signal, this.controller.signal]) : this.controller.signal, this.options.mergeReview ? { reviewDetails: 1 } : undefined);
|
|
88
107
|
// Poll during attach: stopping a previous controller may need release answers
|
|
89
108
|
// from this reattached participant before the attach response can complete.
|
|
90
109
|
this.polling ??= this.poll();
|
|
@@ -92,11 +111,14 @@ export class DaemonConnection {
|
|
|
92
111
|
try {
|
|
93
112
|
const result = await request;
|
|
94
113
|
admitted = true;
|
|
114
|
+
if (this.closed || this.disposing)
|
|
115
|
+
throw new Error("Daemon client closed during attach.");
|
|
95
116
|
this.publishContact(this.consume(result));
|
|
96
117
|
this.observesState = record(result) && result.stateWait === 1;
|
|
97
118
|
this.stateRevision = undefined;
|
|
98
119
|
this.terminalJobs.clear();
|
|
99
120
|
this.attached = true;
|
|
121
|
+
this.diffReviewBinding = facts;
|
|
100
122
|
return this;
|
|
101
123
|
}
|
|
102
124
|
catch (error) {
|
|
@@ -132,10 +154,15 @@ export class DaemonConnection {
|
|
|
132
154
|
const lifecycle = this.lifecycleRevision;
|
|
133
155
|
if (this.admitting || this.pausing || this.disposing)
|
|
134
156
|
throw new Error("Daemon client is changing; wait before updating facts.");
|
|
157
|
+
facts = structuredClone(facts);
|
|
135
158
|
const update = this.updates.then(async () => {
|
|
136
159
|
if (this.closed || this.uncertain || this.resumeUncertain || lifecycle !== this.lifecycleRevision)
|
|
137
160
|
throw new Error("Daemon client is unavailable; reconnect after draining work.");
|
|
138
161
|
const revision = this.contactRevision;
|
|
162
|
+
if (!this.diffReviewBinding || !sameDiffReviewBinding(this.diffReviewBinding, facts)) {
|
|
163
|
+
this.diffReviewBinding = undefined;
|
|
164
|
+
this.retireDiffReviewIO();
|
|
165
|
+
}
|
|
139
166
|
let result;
|
|
140
167
|
try {
|
|
141
168
|
result = await requestDaemon(this.paths, this.identity, "update", { facts }, this.controller.signal);
|
|
@@ -156,7 +183,7 @@ export class DaemonConnection {
|
|
|
156
183
|
const contact = this.consume(result);
|
|
157
184
|
if (revision !== this.contactRevision)
|
|
158
185
|
throw new Error("Daemon client changed during update.");
|
|
159
|
-
|
|
186
|
+
this.setFacts(facts);
|
|
160
187
|
this.publishContact(contact);
|
|
161
188
|
});
|
|
162
189
|
this.updates = update.catch(() => { });
|
|
@@ -189,7 +216,7 @@ export class DaemonConnection {
|
|
|
189
216
|
ownedSignal.throwIfAborted();
|
|
190
217
|
if (contact !== "active")
|
|
191
218
|
throw new Error("Daemon did not confirm resumed contact.");
|
|
192
|
-
|
|
219
|
+
this.setFacts(facts);
|
|
193
220
|
this.publishContact(contact);
|
|
194
221
|
});
|
|
195
222
|
this.updates = attempt.catch(() => { });
|
|
@@ -229,6 +256,8 @@ export class DaemonConnection {
|
|
|
229
256
|
this.contactState === "inactive")
|
|
230
257
|
throw new Error("Daemon client is unavailable; confirm contact and drain work before submitting.");
|
|
231
258
|
signal?.throwIfAborted();
|
|
259
|
+
if (operation === "diff.review" && !this.hasConfirmedDiffReviewBinding())
|
|
260
|
+
throw new Error("The daemon file review session is unconfirmed. Update session facts or reload Pi.");
|
|
232
261
|
const { paths, identity } = this;
|
|
233
262
|
const id = `${operationPrefix(identity)}${randomUUID()}`;
|
|
234
263
|
const jobParams = { id, client: this.facts.id };
|
|
@@ -269,6 +298,15 @@ export class DaemonConnection {
|
|
|
269
298
|
throw new DaemonError(job.error ?? { name: "Error", message: "Daemon operation failed." });
|
|
270
299
|
if (!foreground)
|
|
271
300
|
signal?.throwIfAborted();
|
|
301
|
+
if (operation === "diff.review") {
|
|
302
|
+
try {
|
|
303
|
+
return parseDiffReviewInventory(job.result);
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
abort();
|
|
307
|
+
throw error;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
272
310
|
if (assertRoot && isForegroundQueueOutcome(job.result))
|
|
273
311
|
this.foregroundRecovery = {
|
|
274
312
|
job: id,
|
|
@@ -368,10 +406,89 @@ export class DaemonConnection {
|
|
|
368
406
|
: "Operation outcome is uncertain; reload to reconnect and reconcile, never replay the write.", { cause: error });
|
|
369
407
|
}
|
|
370
408
|
}
|
|
371
|
-
})
|
|
409
|
+
})
|
|
410
|
+
.catch((error) => {
|
|
411
|
+
if (operation !== "diff.review")
|
|
412
|
+
throw error;
|
|
413
|
+
if (error instanceof DaemonError && error.remote.properties?.code === OWNERSHIP_CLEANUP_REQUIRED) {
|
|
414
|
+
throw new DaemonError({
|
|
415
|
+
name: "Error",
|
|
416
|
+
message: "Daemon ownership cleanup must be confirmed before opening a file review.",
|
|
417
|
+
properties: { code: OWNERSHIP_CLEANUP_REQUIRED },
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
if (this.uncertain)
|
|
421
|
+
throw new Error("The daemon diff review outcome is uncertain. Reload Pi to reconnect before opening a review.");
|
|
422
|
+
signal?.throwIfAborted();
|
|
423
|
+
if (error instanceof DaemonError && error.message === "Unsupported engine operation.")
|
|
424
|
+
throw new DaemonError({ name: "Error", message: "Unsupported engine operation." });
|
|
425
|
+
throw new Error("Could not open the daemon diff review. Check the connection and try again.");
|
|
426
|
+
})
|
|
427
|
+
.finally(() => {
|
|
372
428
|
this.pendingJobs.delete(id);
|
|
373
429
|
});
|
|
374
430
|
}
|
|
431
|
+
retireDiffReviewIO() {
|
|
432
|
+
const io = this.diffReviewIO;
|
|
433
|
+
this.diffReviewIO = undefined;
|
|
434
|
+
io?.abort();
|
|
435
|
+
}
|
|
436
|
+
hasConfirmedDiffReviewBinding() {
|
|
437
|
+
return this.diffReviewBinding !== undefined && sameDiffReviewBinding(this.diffReviewBinding, this.facts);
|
|
438
|
+
}
|
|
439
|
+
canReadDiffReview() {
|
|
440
|
+
return (this.attached &&
|
|
441
|
+
!this.admitting &&
|
|
442
|
+
!this.closed &&
|
|
443
|
+
!this.pausing &&
|
|
444
|
+
!this.diffReviewPaused &&
|
|
445
|
+
this.hasConfirmedDiffReviewBinding() &&
|
|
446
|
+
!this.disposing &&
|
|
447
|
+
!this.uncertain &&
|
|
448
|
+
this.contactResumeState === "idle" &&
|
|
449
|
+
this.contactState !== "expired" &&
|
|
450
|
+
this.contactState !== "inactive" &&
|
|
451
|
+
this.facts.mode === "tui" &&
|
|
452
|
+
this.facts.hasUI);
|
|
453
|
+
}
|
|
454
|
+
async diffReviewRequest(method, id, fileId, signal, stale, decode) {
|
|
455
|
+
signal?.throwIfAborted();
|
|
456
|
+
if (!isDiffReviewId(id) || (method === "diff-review-read" && !isDiffReviewId(fileId)))
|
|
457
|
+
throw new Error("Invalid diff review identity.");
|
|
458
|
+
if (!this.canReadDiffReview())
|
|
459
|
+
return stale;
|
|
460
|
+
const binding = { ...this.facts };
|
|
461
|
+
this.diffReviewIO ??= new AbortController();
|
|
462
|
+
const io = this.diffReviewIO;
|
|
463
|
+
const current = () => this.diffReviewIO === io && this.canReadDiffReview() && sameDiffReviewBinding(binding, this.facts);
|
|
464
|
+
const owned = AbortSignal.any([
|
|
465
|
+
this.controller.signal,
|
|
466
|
+
io.signal,
|
|
467
|
+
AbortSignal.timeout(DIFF_REVIEW_REQUEST_MS),
|
|
468
|
+
...(signal ? [signal] : []),
|
|
469
|
+
]);
|
|
470
|
+
let reply;
|
|
471
|
+
try {
|
|
472
|
+
reply = await requestDaemon(this.paths, this.identity, method, { client: binding.id, generation: binding.generation, id, fileId }, owned);
|
|
473
|
+
owned.throwIfAborted();
|
|
474
|
+
}
|
|
475
|
+
catch {
|
|
476
|
+
signal?.throwIfAborted();
|
|
477
|
+
if (!current())
|
|
478
|
+
return stale;
|
|
479
|
+
throw new Error("Daemon file review request failed. Try again or reopen the review.");
|
|
480
|
+
}
|
|
481
|
+
return current() ? decode(reply) : stale;
|
|
482
|
+
}
|
|
483
|
+
readDiffReview(id, fileId, signal) {
|
|
484
|
+
return this.diffReviewRequest("diff-review-read", id, fileId, signal, { kind: "stale" }, readDiffReviewPreview);
|
|
485
|
+
}
|
|
486
|
+
closeDiffReview(id, signal) {
|
|
487
|
+
return this.diffReviewRequest("diff-review-close", id, undefined, signal, undefined, (reply) => {
|
|
488
|
+
if (reply !== true)
|
|
489
|
+
throw new Error("Invalid daemon diff review release acknowledgement.");
|
|
490
|
+
});
|
|
491
|
+
}
|
|
375
492
|
canRecoverForeground(job) {
|
|
376
493
|
const target = this.foregroundRecovery;
|
|
377
494
|
return (!!target &&
|
|
@@ -401,6 +518,7 @@ export class DaemonConnection {
|
|
|
401
518
|
}
|
|
402
519
|
markWorkUncertain() {
|
|
403
520
|
this.uncertain = true;
|
|
521
|
+
this.retireDiffReviewIO();
|
|
404
522
|
for (const callback of this.callbacks.values())
|
|
405
523
|
if (callback.kind !== "queue-release")
|
|
406
524
|
callback.controller.abort();
|
|
@@ -408,6 +526,8 @@ export class DaemonConnection {
|
|
|
408
526
|
publishContact(contact) {
|
|
409
527
|
this.contactRevision++;
|
|
410
528
|
this.observedContact = contact;
|
|
529
|
+
if (contact === "expired" || contact === "inactive")
|
|
530
|
+
this.retireDiffReviewIO();
|
|
411
531
|
}
|
|
412
532
|
consume(reply) {
|
|
413
533
|
if (!record(reply) ||
|
|
@@ -731,11 +851,15 @@ export class DaemonConnection {
|
|
|
731
851
|
if (this.pausing)
|
|
732
852
|
return this.pausing;
|
|
733
853
|
this.resuming?.abort(new DOMException("Client paused", "AbortError"));
|
|
854
|
+
this.retireDiffReviewIO();
|
|
734
855
|
this.lifecycleRevision++;
|
|
735
856
|
this.contactRevision++;
|
|
736
857
|
this.pausing = (async () => {
|
|
737
858
|
const reply = await requestDaemon(this.paths, this.identity, "pause", { client: this.facts.id });
|
|
859
|
+
if (this.closed || this.disposing)
|
|
860
|
+
return;
|
|
738
861
|
const contact = this.consume(reply);
|
|
862
|
+
this.diffReviewPaused = true;
|
|
739
863
|
if (!this.disposing)
|
|
740
864
|
this.publishContact(contact);
|
|
741
865
|
})().finally(() => {
|
|
@@ -749,6 +873,7 @@ export class DaemonConnection {
|
|
|
749
873
|
if (this.closed)
|
|
750
874
|
return;
|
|
751
875
|
this.resuming?.abort(new DOMException("Client disposed", "AbortError"));
|
|
876
|
+
this.retireDiffReviewIO();
|
|
752
877
|
this.lifecycleRevision++;
|
|
753
878
|
this.contactRevision++;
|
|
754
879
|
this.disposing = (async () => {
|
|
@@ -774,6 +899,7 @@ export class DaemonConnection {
|
|
|
774
899
|
}
|
|
775
900
|
async closeLocal() {
|
|
776
901
|
this.closed = true;
|
|
902
|
+
this.retireDiffReviewIO();
|
|
777
903
|
this.controller.abort(new DOMException("Client disposed", "AbortError"));
|
|
778
904
|
this.callbackIO.abort(new DOMException("Client callback transport disposed", "AbortError"));
|
|
779
905
|
for (const { controller } of this.callbacks.values())
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export function sameDiffReviewBinding(left, right) {
|
|
2
|
+
return (left.id === right.id &&
|
|
3
|
+
left.pid === right.pid &&
|
|
4
|
+
left.generation === right.generation &&
|
|
5
|
+
left.mode === right.mode &&
|
|
6
|
+
left.hasUI === right.hasUI &&
|
|
7
|
+
left.cwd === right.cwd &&
|
|
8
|
+
left.sessionId === right.sessionId &&
|
|
9
|
+
left.sessionDir === right.sessionDir &&
|
|
10
|
+
left.sessionFile === right.sessionFile);
|
|
11
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { record } from "./protocol.js";
|
|
2
|
+
export const DIFF_REVIEW_CATALOG_BYTES = 256 * 1024;
|
|
3
|
+
export const DIFF_REVIEW_CATALOG_FILES = 1024;
|
|
4
|
+
export const DIFF_REVIEW_PATH_BYTES = 4096;
|
|
5
|
+
export const DIFF_REVIEW_PATH_DEPTH = 64;
|
|
6
|
+
export const DIFF_REVIEW_OUTPUT_BYTES = 128 * 1024;
|
|
7
|
+
export const DIFF_REVIEW_OUTPUT_LINES = 2000;
|
|
8
|
+
export const DIFF_REVIEW_REQUEST_MS = 5_000;
|
|
9
|
+
export function isDiffReviewId(value) {
|
|
10
|
+
return typeof value === "string" && /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/u.test(value);
|
|
11
|
+
}
|
|
12
|
+
export function isDiffReviewPath(value) {
|
|
13
|
+
if (typeof value !== "string" ||
|
|
14
|
+
!value ||
|
|
15
|
+
value.length > DIFF_REVIEW_PATH_BYTES ||
|
|
16
|
+
Buffer.byteLength(value) > DIFF_REVIEW_PATH_BYTES ||
|
|
17
|
+
value.includes("\\") ||
|
|
18
|
+
value.includes("\0"))
|
|
19
|
+
return false;
|
|
20
|
+
const parts = value.split("/");
|
|
21
|
+
return parts.length <= DIFF_REVIEW_PATH_DEPTH && parts.every((part) => part && part !== "." && part !== "..");
|
|
22
|
+
}
|
|
23
|
+
export function parseDiffReviewInventory(value) {
|
|
24
|
+
const invalid = () => new Error("Invalid daemon diff review inventory.");
|
|
25
|
+
if (record(value)) {
|
|
26
|
+
if (value.kind === "unavailable" &&
|
|
27
|
+
Object.keys(value).length === 2 &&
|
|
28
|
+
typeof value.message === "string" &&
|
|
29
|
+
value.message.length > 0 &&
|
|
30
|
+
value.message.length <= DIFF_REVIEW_CATALOG_BYTES &&
|
|
31
|
+
Buffer.byteLength(JSON.stringify(value)) <= DIFF_REVIEW_CATALOG_BYTES)
|
|
32
|
+
return { kind: "unavailable", message: value.message };
|
|
33
|
+
if (value.kind === "review" &&
|
|
34
|
+
Object.keys(value).length === 4 &&
|
|
35
|
+
isDiffReviewId(value.id) &&
|
|
36
|
+
Array.isArray(value.lines) &&
|
|
37
|
+
value.lines.length <= DIFF_REVIEW_CATALOG_FILES &&
|
|
38
|
+
value.lines.every((line) => typeof line === "string" && line.length <= DIFF_REVIEW_CATALOG_BYTES) &&
|
|
39
|
+
Array.isArray(value.files) &&
|
|
40
|
+
value.files.length <= DIFF_REVIEW_CATALOG_FILES) {
|
|
41
|
+
const ids = new Set();
|
|
42
|
+
const paths = new Set();
|
|
43
|
+
const files = [];
|
|
44
|
+
for (const file of value.files) {
|
|
45
|
+
if (!record(file) ||
|
|
46
|
+
Object.keys(file).length !== 3 ||
|
|
47
|
+
!isDiffReviewId(file.id) ||
|
|
48
|
+
!isDiffReviewPath(file.path) ||
|
|
49
|
+
(file.status !== "local-only" && file.status !== "remote-only" && file.status !== "different") ||
|
|
50
|
+
ids.has(file.id) ||
|
|
51
|
+
paths.has(file.path))
|
|
52
|
+
throw invalid();
|
|
53
|
+
ids.add(file.id);
|
|
54
|
+
paths.add(file.path);
|
|
55
|
+
files.push({ id: file.id, path: file.path, status: file.status });
|
|
56
|
+
}
|
|
57
|
+
const inventory = { kind: "review", id: value.id, lines: [...value.lines], files };
|
|
58
|
+
if (Buffer.byteLength(JSON.stringify(inventory)) <= DIFF_REVIEW_CATALOG_BYTES)
|
|
59
|
+
return inventory;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
throw invalid();
|
|
63
|
+
}
|
|
64
|
+
export function readDiffReviewPreview(value) {
|
|
65
|
+
if (record(value)) {
|
|
66
|
+
const keys = Object.keys(value).length;
|
|
67
|
+
if (keys === 1 && value.kind === "stale")
|
|
68
|
+
return { kind: "stale" };
|
|
69
|
+
if (keys === 2 &&
|
|
70
|
+
value.kind === "notice" &&
|
|
71
|
+
typeof value.message === "string" &&
|
|
72
|
+
value.message.length > 0 &&
|
|
73
|
+
value.message.length <= 1024)
|
|
74
|
+
return { kind: "notice", message: value.message };
|
|
75
|
+
if (keys === 2 &&
|
|
76
|
+
value.kind === "diff" &&
|
|
77
|
+
typeof value.content === "string" &&
|
|
78
|
+
value.content.length <= DIFF_REVIEW_OUTPUT_BYTES &&
|
|
79
|
+
Buffer.byteLength(value.content) <= DIFF_REVIEW_OUTPUT_BYTES &&
|
|
80
|
+
value.content.startsWith("--- Local\n+++ Remote\n@@ ") &&
|
|
81
|
+
(value.content.match(/\n/gu)?.length ?? 0) <= DIFF_REVIEW_OUTPUT_LINES)
|
|
82
|
+
return { kind: "diff", content: value.content };
|
|
83
|
+
}
|
|
84
|
+
throw new Error("Invalid daemon diff review preview.");
|
|
85
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { DIFF_REVIEW_REQUEST_MS, isDiffReviewId } from "./diff-review-protocol.js";
|
|
2
|
+
import { validId } from "./protocol.js";
|
|
3
|
+
export async function handleDiffReviewRequest(method, params, signal, store, currentOwner, assertOwner, publish) {
|
|
4
|
+
const reading = method === "diff-review-read";
|
|
5
|
+
const { client, generation, id, fileId } = params;
|
|
6
|
+
if (Object.keys(params).length !== (reading ? 4 : 3) ||
|
|
7
|
+
!validId(client) ||
|
|
8
|
+
!validId(generation) ||
|
|
9
|
+
!isDiffReviewId(id) ||
|
|
10
|
+
(reading ? !isDiffReviewId(fileId) : Object.hasOwn(params, "fileId")))
|
|
11
|
+
throw new Error("Invalid diff review request.");
|
|
12
|
+
const owner = currentOwner(client, generation);
|
|
13
|
+
if (!reading) {
|
|
14
|
+
if (owner)
|
|
15
|
+
store.release(owner, id);
|
|
16
|
+
publish(true);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const stale = { kind: "stale" };
|
|
20
|
+
if (!owner || !isDiffReviewId(fileId)) {
|
|
21
|
+
publish(stale);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!signal)
|
|
25
|
+
throw new Error("File preview transport is unavailable.");
|
|
26
|
+
const owned = AbortSignal.any([signal, AbortSignal.timeout(DIFF_REVIEW_REQUEST_MS)]);
|
|
27
|
+
let result;
|
|
28
|
+
try {
|
|
29
|
+
result = await store.read(owner, id, fileId, owned);
|
|
30
|
+
owned.throwIfAborted();
|
|
31
|
+
assertOwner();
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
throw new Error("File preview could not be loaded. Try again or reopen the review.");
|
|
35
|
+
}
|
|
36
|
+
publish(currentOwner(client, generation) === owner && store.isCurrent(owner, id) ? result : stale);
|
|
37
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { buildDiffReviewPreview } from "./engine/sync/diff-review-preview.js";
|
|
2
|
+
const REVIEW_LIFETIME_MS = 120_000;
|
|
3
|
+
export const DIFF_REVIEW_RETAINED_BYTES = 8 * 1024 * 1024;
|
|
4
|
+
const TOTAL_RETAINED_BYTES = 32 * 1024 * 1024;
|
|
5
|
+
const MAX_READS = 4;
|
|
6
|
+
export function createDiffReviewStore(options = {}) {
|
|
7
|
+
const lifetime = options.lifetime ?? REVIEW_LIFETIME_MS;
|
|
8
|
+
const totalBytes = options.totalBytes ?? TOTAL_RETAINED_BYTES;
|
|
9
|
+
const captures = new Map();
|
|
10
|
+
const reads = new Set();
|
|
11
|
+
let bytes = 0;
|
|
12
|
+
function release(owner, id) {
|
|
13
|
+
const capture = captures.get(owner);
|
|
14
|
+
if (!capture || (id !== undefined && id !== capture.id))
|
|
15
|
+
return;
|
|
16
|
+
captures.delete(owner);
|
|
17
|
+
bytes -= capture.bytes;
|
|
18
|
+
capture.rows.clear();
|
|
19
|
+
capture.controller.abort();
|
|
20
|
+
capture.unbind();
|
|
21
|
+
clearTimeout(capture.timer);
|
|
22
|
+
}
|
|
23
|
+
function current(owner, id) {
|
|
24
|
+
const capture = captures.get(owner);
|
|
25
|
+
if (capture?.id !== id)
|
|
26
|
+
return;
|
|
27
|
+
if (performance.now() >= capture.deadline) {
|
|
28
|
+
release(owner, id);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
return capture;
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
retain(owner, prepared, signal) {
|
|
35
|
+
signal.throwIfAborted();
|
|
36
|
+
release(owner);
|
|
37
|
+
if (prepared.bytes > DIFF_REVIEW_RETAINED_BYTES || bytes + prepared.bytes > totalBytes)
|
|
38
|
+
return false;
|
|
39
|
+
const { inventory: { id }, rows, staleSetup, } = prepared;
|
|
40
|
+
const abort = () => release(owner, id);
|
|
41
|
+
const timer = setTimeout(abort, lifetime);
|
|
42
|
+
timer.unref();
|
|
43
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
44
|
+
captures.set(owner, {
|
|
45
|
+
id,
|
|
46
|
+
bytes: prepared.bytes,
|
|
47
|
+
rows,
|
|
48
|
+
staleSetup,
|
|
49
|
+
timer,
|
|
50
|
+
deadline: performance.now() + lifetime,
|
|
51
|
+
controller: new AbortController(),
|
|
52
|
+
unbind: () => signal.removeEventListener("abort", abort),
|
|
53
|
+
});
|
|
54
|
+
bytes += prepared.bytes;
|
|
55
|
+
return true;
|
|
56
|
+
},
|
|
57
|
+
async read(owner, id, fileId, signal) {
|
|
58
|
+
signal.throwIfAborted();
|
|
59
|
+
const capture = current(owner, id);
|
|
60
|
+
if (!capture?.rows.has(fileId))
|
|
61
|
+
return { kind: "stale" };
|
|
62
|
+
if (reads.has(owner) || reads.size >= MAX_READS)
|
|
63
|
+
return { kind: "notice", message: "File previews are busy. Try this file again." };
|
|
64
|
+
const owned = AbortSignal.any([signal, capture.controller.signal]);
|
|
65
|
+
reads.add(owner);
|
|
66
|
+
try {
|
|
67
|
+
const stale = await capture.staleSetup(owned);
|
|
68
|
+
signal.throwIfAborted();
|
|
69
|
+
if (current(owner, id) !== capture)
|
|
70
|
+
return { kind: "stale" };
|
|
71
|
+
if (stale) {
|
|
72
|
+
release(owner, id);
|
|
73
|
+
return { kind: "stale" };
|
|
74
|
+
}
|
|
75
|
+
const row = capture.rows.get(fileId);
|
|
76
|
+
return row ? buildDiffReviewPreview(row, owned) : { kind: "stale" };
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
reads.delete(owner);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
isCurrent: (owner, id) => current(owner, id) !== undefined,
|
|
83
|
+
release,
|
|
84
|
+
close() {
|
|
85
|
+
for (const owner of captures.keys())
|
|
86
|
+
release(owner);
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -2,6 +2,7 @@ import { createHash, randomUUID } from "node:crypto";
|
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { posixJoin } from "../../paths.js";
|
|
5
|
+
import { SnapshotCaptureLimitError } from "../../snapshot/snapshot-capture-limit.js";
|
|
5
6
|
import { snapshotFileFromBlob } from "../../snapshot/snapshot-payload.js";
|
|
6
7
|
import { stateDir } from "../../state/state-directory.js";
|
|
7
8
|
import { throwIfAborted } from "../../sync/signals.js";
|
|
@@ -56,22 +57,22 @@ export class GitSyncBackend {
|
|
|
56
57
|
return undefined;
|
|
57
58
|
return this.headForSha(sha, signal);
|
|
58
59
|
}
|
|
59
|
-
async readHeadAndSnapshot(signal) {
|
|
60
|
+
async readHeadAndSnapshot(signal, maxBytes) {
|
|
60
61
|
const commit = await this.fetchRemoteHead(signal);
|
|
61
62
|
if (!commit)
|
|
62
63
|
return undefined;
|
|
63
64
|
const publication = await this.readCachedPublication(commit, signal);
|
|
64
65
|
await this.requireReachablePublication(commit, commit, undefined, signal);
|
|
65
|
-
const snapshot = await this.readPublicationSnapshot(publication, signal);
|
|
66
|
+
const snapshot = await this.readPublicationSnapshot(publication, signal, maxBytes);
|
|
66
67
|
return { head: remoteHead(commit, publication.manifest, this.identity), snapshot };
|
|
67
68
|
}
|
|
68
|
-
async readSnapshot(reference, signal) {
|
|
69
|
+
async readSnapshot(reference, signal, maxBytes) {
|
|
69
70
|
const head = await this.fetchRemoteHead(signal);
|
|
70
71
|
if (!head)
|
|
71
72
|
throw new Error(`Git snapshot publication was not found: ${reference}`);
|
|
72
73
|
const commit = await this.resolveSnapshotReference(reference, head, signal);
|
|
73
74
|
await this.requireReachablePublication(reference, commit, head, signal);
|
|
74
|
-
return this.readPublicationSnapshot(await this.readCachedPublication(commit, signal), signal);
|
|
75
|
+
return this.readPublicationSnapshot(await this.readCachedPublication(commit, signal), signal, maxBytes);
|
|
75
76
|
}
|
|
76
77
|
async requireReachablePublication(reference, commit, head, signal) {
|
|
77
78
|
try {
|
|
@@ -84,7 +85,10 @@ export class GitSyncBackend {
|
|
|
84
85
|
throw new Error(`Git snapshot publication was not found: ${reference}`, { cause: error });
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
|
-
async readPublicationSnapshot({ manifest, payloadEntries }, signal) {
|
|
88
|
+
async readPublicationSnapshot({ manifest, payloadEntries }, signal, maxBytes) {
|
|
89
|
+
const payloadBytes = manifest.files.reduce((total, file) => total + file.size, 0);
|
|
90
|
+
if (maxBytes !== undefined && payloadBytes > maxBytes)
|
|
91
|
+
throw new SnapshotCaptureLimitError();
|
|
88
92
|
let blobs;
|
|
89
93
|
try {
|
|
90
94
|
blobs = await readGitBlobs(payloadEntries.map((entry) => entry.object), {
|
|
@@ -92,7 +96,7 @@ export class GitSyncBackend {
|
|
|
92
96
|
signal,
|
|
93
97
|
timeoutMs: this.commandTimeoutMs,
|
|
94
98
|
allowFileProtocol: this.allowLocalRemotes,
|
|
95
|
-
maxOutputBytes:
|
|
99
|
+
maxOutputBytes: payloadBytes,
|
|
96
100
|
});
|
|
97
101
|
}
|
|
98
102
|
catch (error) {
|
|
@@ -58,16 +58,16 @@ export class S3SyncBackend {
|
|
|
58
58
|
}
|
|
59
59
|
return head;
|
|
60
60
|
}
|
|
61
|
-
async readSnapshot(reference, signal) {
|
|
61
|
+
async readSnapshot(reference, signal, maxBytes) {
|
|
62
62
|
const expectedChecksum = this.checksums.get(reference) ?? (await this.resolveChecksum(reference, signal));
|
|
63
|
-
const object = await new S3Client(this.config, signal).getBuffer(snapshotKey(this.config, reference));
|
|
63
|
+
const object = await new S3Client(this.config, signal).getBuffer(snapshotKey(this.config, reference), maxBytes);
|
|
64
64
|
throwIfAborted(signal);
|
|
65
65
|
if (!object.value)
|
|
66
66
|
throw new Error(`Snapshot not found: ${reference}`);
|
|
67
67
|
if (expectedChecksum && sha256(object.value) !== expectedChecksum) {
|
|
68
68
|
throw new Error("Remote snapshot checksum mismatch.");
|
|
69
69
|
}
|
|
70
|
-
const snapshot = await decodeSnapshot(object.value, { signal });
|
|
70
|
+
const snapshot = await decodeSnapshot(object.value, { signal, maxOutputLength: maxBytes });
|
|
71
71
|
if (snapshot.id !== reference || snapshot.profile !== this.config.destination.namespace) {
|
|
72
72
|
throw new Error("Remote snapshot identity mismatch.");
|
|
73
73
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash, createHmac } from "node:crypto";
|
|
2
2
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
3
3
|
import { encodeKey, posixJoin } from "../../paths.js";
|
|
4
|
+
import { SnapshotCaptureLimitError } from "../../snapshot/snapshot-capture-limit.js";
|
|
4
5
|
import { retryAfter } from "../../sync/continuous-errors.js";
|
|
5
6
|
const MAX_JSON_RESPONSE_BYTES = 1024 * 1024;
|
|
6
7
|
const MAX_SNAPSHOT_RESPONSE_BYTES = 256 * 1024 * 1024;
|
|
@@ -102,17 +103,26 @@ export class S3Client {
|
|
|
102
103
|
}
|
|
103
104
|
throw lastError;
|
|
104
105
|
}
|
|
105
|
-
async getBuffer(key) {
|
|
106
|
+
async getBuffer(key, maxBytes) {
|
|
106
107
|
const maxAttempts = 3;
|
|
107
108
|
let lastError;
|
|
108
109
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
109
110
|
const object = await this.request("GET", key);
|
|
110
|
-
if (object.status === 404)
|
|
111
|
+
if (object.status === 404) {
|
|
112
|
+
if (maxBytes !== undefined)
|
|
113
|
+
await object.body?.cancel().catch(() => undefined);
|
|
111
114
|
return { missing: true };
|
|
115
|
+
}
|
|
112
116
|
if (!object.ok) {
|
|
113
117
|
throw new S3HttpError(`S3 GET failed (${object.status}): ${await this.readErrorText(object)}`, object.status, retryAfter(object));
|
|
114
118
|
}
|
|
115
|
-
const
|
|
119
|
+
const encoding = object.headers.get("content-encoding")?.toLowerCase();
|
|
120
|
+
if (maxBytes !== undefined && encoding && encoding !== "identity") {
|
|
121
|
+
await object.body?.cancel().catch(() => undefined);
|
|
122
|
+
throw new SnapshotCaptureLimitError();
|
|
123
|
+
}
|
|
124
|
+
const limit = Math.min(maxBytes ?? MAX_SNAPSHOT_RESPONSE_BYTES, MAX_SNAPSHOT_RESPONSE_BYTES);
|
|
125
|
+
const buffer = await readBoundedBuffer(object, limit, "S3 snapshot response", maxBytes === undefined ? undefined : () => new SnapshotCaptureLimitError());
|
|
116
126
|
// R2 can intermittently return an empty 200 body on a long-lived
|
|
117
127
|
// keep-alive connection (same root cause as the getJson retry
|
|
118
128
|
// above). getBuffer is only used for snapshot .json.gz payloads,
|
|
@@ -270,12 +280,12 @@ export class S3Client {
|
|
|
270
280
|
async function readBoundedText(response, limit, label) {
|
|
271
281
|
return (await readBoundedBuffer(response, limit, label)).toString("utf8");
|
|
272
282
|
}
|
|
273
|
-
async function readBoundedBuffer(response, limit, label) {
|
|
283
|
+
async function readBoundedBuffer(response, limit, label, limitError = () => new Error(`${label} exceeds the ${limit}-byte limit.`)) {
|
|
274
284
|
const contentLength = Number(response.headers.get("content-length"));
|
|
275
285
|
if (Number.isFinite(contentLength) && contentLength > limit) {
|
|
276
286
|
// Do not await cancellation of a cloned stream before its sibling can be released.
|
|
277
287
|
void response.body?.cancel().catch(() => undefined);
|
|
278
|
-
throw
|
|
288
|
+
throw limitError();
|
|
279
289
|
}
|
|
280
290
|
if (!response.body)
|
|
281
291
|
return Buffer.alloc(0);
|
|
@@ -290,7 +300,7 @@ async function readBoundedBuffer(response, limit, label) {
|
|
|
290
300
|
total += value.byteLength;
|
|
291
301
|
if (total > limit) {
|
|
292
302
|
void reader.cancel().catch(() => undefined);
|
|
293
|
-
throw
|
|
303
|
+
throw limitError();
|
|
294
304
|
}
|
|
295
305
|
chunks.push(Buffer.from(value));
|
|
296
306
|
}
|