@indigoai-us/hq-cloud 6.14.6 → 6.14.8

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.
Files changed (97) hide show
  1. package/dist/backup-prune.d.ts +68 -0
  2. package/dist/backup-prune.d.ts.map +1 -0
  3. package/dist/backup-prune.js +196 -0
  4. package/dist/backup-prune.js.map +1 -0
  5. package/dist/backup-prune.test.d.ts +2 -0
  6. package/dist/backup-prune.test.d.ts.map +1 -0
  7. package/dist/backup-prune.test.js +89 -0
  8. package/dist/backup-prune.test.js.map +1 -0
  9. package/dist/bin/backup-prune-runner.d.ts +3 -0
  10. package/dist/bin/backup-prune-runner.d.ts.map +1 -0
  11. package/dist/bin/backup-prune-runner.js +50 -0
  12. package/dist/bin/backup-prune-runner.js.map +1 -0
  13. package/dist/bin/sync-runner-watch-loop.d.ts.map +1 -1
  14. package/dist/bin/sync-runner-watch-loop.js +28 -0
  15. package/dist/bin/sync-runner-watch-loop.js.map +1 -1
  16. package/dist/bin/sync-runner.d.ts +2 -0
  17. package/dist/bin/sync-runner.d.ts.map +1 -1
  18. package/dist/bin/sync-runner.js.map +1 -1
  19. package/dist/cli/reindex.d.ts.map +1 -1
  20. package/dist/cli/reindex.js +5 -3
  21. package/dist/cli/reindex.js.map +1 -1
  22. package/dist/cli/rescue-classify-ordering.test.js +105 -0
  23. package/dist/cli/rescue-classify-ordering.test.js.map +1 -1
  24. package/dist/cli/rescue-core.d.ts +7 -0
  25. package/dist/cli/rescue-core.d.ts.map +1 -1
  26. package/dist/cli/rescue-core.js +509 -282
  27. package/dist/cli/rescue-core.js.map +1 -1
  28. package/dist/cli/rescue-snapshot.d.ts +14 -0
  29. package/dist/cli/rescue-snapshot.d.ts.map +1 -0
  30. package/dist/cli/rescue-snapshot.js +39 -0
  31. package/dist/cli/rescue-snapshot.js.map +1 -0
  32. package/dist/cli/rescue-snapshot.test.d.ts +2 -0
  33. package/dist/cli/rescue-snapshot.test.d.ts.map +1 -0
  34. package/dist/cli/rescue-snapshot.test.js +46 -0
  35. package/dist/cli/rescue-snapshot.test.js.map +1 -0
  36. package/dist/cli/rescue.test.js +24 -0
  37. package/dist/cli/rescue.test.js.map +1 -1
  38. package/dist/cli/share.d.ts.map +1 -1
  39. package/dist/cli/share.js +12 -11
  40. package/dist/cli/share.js.map +1 -1
  41. package/dist/cli/sync.d.ts.map +1 -1
  42. package/dist/cli/sync.js +41 -6
  43. package/dist/cli/sync.js.map +1 -1
  44. package/dist/cli/sync.test.js +51 -0
  45. package/dist/cli/sync.test.js.map +1 -1
  46. package/dist/local-path-codec.d.ts +19 -0
  47. package/dist/local-path-codec.d.ts.map +1 -0
  48. package/dist/local-path-codec.js +69 -0
  49. package/dist/local-path-codec.js.map +1 -0
  50. package/dist/object-io.d.ts +19 -2
  51. package/dist/object-io.d.ts.map +1 -1
  52. package/dist/object-io.js +168 -32
  53. package/dist/object-io.js.map +1 -1
  54. package/dist/object-io.test.js +157 -1
  55. package/dist/object-io.test.js.map +1 -1
  56. package/dist/s3.d.ts +8 -0
  57. package/dist/s3.d.ts.map +1 -1
  58. package/dist/s3.js +52 -22
  59. package/dist/s3.js.map +1 -1
  60. package/dist/scope-shrink.d.ts.map +1 -1
  61. package/dist/scope-shrink.js +4 -3
  62. package/dist/scope-shrink.js.map +1 -1
  63. package/dist/signals/get.test.js +21 -1
  64. package/dist/signals/get.test.js.map +1 -1
  65. package/dist/signals/list.test.js +21 -1
  66. package/dist/signals/list.test.js.map +1 -1
  67. package/dist/sources/get.test.js +21 -1
  68. package/dist/sources/get.test.js.map +1 -1
  69. package/dist/sources/list.test.js +21 -1
  70. package/dist/sources/list.test.js.map +1 -1
  71. package/package.json +3 -2
  72. package/scripts/presign-transport-e2e.mjs +57 -10
  73. package/scripts/vault-rebaseline.sh +49 -1
  74. package/scripts/vault-rescue.sh +42 -1
  75. package/src/backup-prune.test.ts +98 -0
  76. package/src/backup-prune.ts +182 -0
  77. package/src/bin/backup-prune-runner.ts +33 -0
  78. package/src/bin/sync-runner-watch-loop.ts +18 -0
  79. package/src/bin/sync-runner.ts +2 -0
  80. package/src/cli/reindex.ts +5 -3
  81. package/src/cli/rescue-classify-ordering.test.ts +121 -0
  82. package/src/cli/rescue-core.ts +311 -89
  83. package/src/cli/rescue-snapshot.test.ts +57 -0
  84. package/src/cli/rescue-snapshot.ts +51 -0
  85. package/src/cli/rescue.test.ts +34 -0
  86. package/src/cli/share.ts +12 -11
  87. package/src/cli/sync.test.ts +62 -0
  88. package/src/cli/sync.ts +51 -7
  89. package/src/local-path-codec.ts +95 -0
  90. package/src/object-io.test.ts +175 -0
  91. package/src/object-io.ts +213 -32
  92. package/src/s3.ts +61 -12
  93. package/src/scope-shrink.ts +4 -3
  94. package/src/signals/get.test.ts +26 -2
  95. package/src/signals/list.test.ts +26 -2
  96. package/src/sources/get.test.ts +26 -2
  97. package/src/sources/list.test.ts +26 -2
@@ -9,12 +9,17 @@
9
9
  */
10
10
 
11
11
  import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
12
+ import * as http from "node:http";
13
+ import { once } from "node:events";
14
+ import { spawn } from "node:child_process";
12
15
  import {
13
16
  PresignObjectIO,
14
17
  S3SdkObjectIO,
15
18
  setObjectIOFactory,
19
+ setPresignedGetTransportForTesting,
16
20
  resolveObjectIO,
17
21
  presignObjectIOFactory,
22
+ type PresignedGetTransport,
18
23
  type PresignTransportClient,
19
24
  } from "./object-io.js";
20
25
  import type { EntityContext } from "./types.js";
@@ -23,6 +28,55 @@ import { isAccessDenied } from "./sync-core.js";
23
28
 
24
29
  const COMPANY = "cmp_test";
25
30
 
31
+ function fetchBackedPresignedGetTransport(): PresignedGetTransport {
32
+ return {
33
+ async get(url, headers) {
34
+ const response = await fetch(url, { method: "GET", headers });
35
+ const body = response.body;
36
+ return {
37
+ status: response.status,
38
+ headers: response.headers,
39
+ body: body
40
+ ? (async function* (): AsyncIterable<Uint8Array> {
41
+ const reader = body.getReader();
42
+ let exhausted = false;
43
+ try {
44
+ while (true) {
45
+ const { done, value } = await reader.read();
46
+ if (done) {
47
+ exhausted = true;
48
+ return;
49
+ }
50
+ if (value.byteLength > 0) yield value;
51
+ }
52
+ } finally {
53
+ if (!exhausted) {
54
+ try {
55
+ await reader.cancel();
56
+ } catch {
57
+ // The response may already have been cancelled by destroy.
58
+ }
59
+ }
60
+ reader.releaseLock();
61
+ }
62
+ })()
63
+ : undefined,
64
+ destroy: () => {
65
+ void body?.cancel().catch(() => undefined);
66
+ },
67
+ };
68
+ },
69
+ };
70
+ }
71
+
72
+ beforeEach(() => {
73
+ setPresignedGetTransportForTesting(fetchBackedPresignedGetTransport());
74
+ });
75
+
76
+ afterEach(() => {
77
+ setPresignedGetTransportForTesting(null);
78
+ });
79
+
26
80
  function ctx(): EntityContext {
27
81
  return {
28
82
  uid: COMPANY,
@@ -506,6 +560,27 @@ describe("PresignObjectIO.getObject", () => {
506
560
  });
507
561
  });
508
562
 
563
+ it("drains the injected transport body instead of returning empty content", async () => {
564
+ const { vault, setPresign } = makeVault();
565
+ setPresign([{ key: "shared/a.md", op: "get", url: "https://s3/get" }]);
566
+ const transportGet = vi.fn(async () => ({
567
+ status: 200,
568
+ headers: new Headers({ "x-amz-meta-source": "transport" }),
569
+ body: (async function* (): AsyncIterable<Uint8Array> {
570
+ yield Buffer.from("transport-bytes");
571
+ })(),
572
+ destroy: () => undefined,
573
+ }));
574
+ setPresignedGetTransportForTesting({ get: transportGet });
575
+
576
+ const io = new PresignObjectIO(vault, COMPANY);
577
+ const res = await io.getObject("shared/a.md");
578
+
579
+ expect(res.body.toString("utf-8")).toBe("transport-bytes");
580
+ expect(res.metadata).toEqual({ source: "transport" });
581
+ expect(transportGet).toHaveBeenCalledWith("https://s3/get", undefined);
582
+ });
583
+
509
584
  it("throws a NotFound-named error on 404 (so s3.ts catch sites match)", async () => {
510
585
  const { vault, setPresign } = makeVault();
511
586
  setPresign([{ key: "gone", op: "get", url: "https://s3/get" }]);
@@ -1044,3 +1119,103 @@ describe("presignObjectIOFactory — personal vaults route to S3 SDK", () => {
1044
1119
  expect(personal).toBeInstanceOf(S3SdkObjectIO);
1045
1120
  });
1046
1121
  });
1122
+
1123
+ describe("PresignObjectIO — core HTTP presigned GET", () => {
1124
+ function streamingVault(baseUrl: string): PresignTransportClient {
1125
+ return {
1126
+ presign: async (input) => ({
1127
+ results: input.keys.map((key) => ({
1128
+ key: key.key,
1129
+ op: "get" as const,
1130
+ url: `${baseUrl}/${key.key}`,
1131
+ })),
1132
+ expiresAt: "2099-01-01T00:00:00.000Z",
1133
+ }),
1134
+ listFiles: async () => ({ objects: [], cursor: null, truncated: false }),
1135
+ };
1136
+ }
1137
+
1138
+ it("destroys an early-exit response and rejects a FIN-short body", async () => {
1139
+ setPresignedGetTransportForTesting(null);
1140
+ let responseClosed: Promise<unknown> | undefined;
1141
+ const server = http.createServer((req, res) => {
1142
+ if (req.url === "/short") {
1143
+ res.writeHead(200, { "content-length": "10" });
1144
+ res.end("short");
1145
+ return;
1146
+ }
1147
+
1148
+ res.writeHead(200, { "content-length": "131072" });
1149
+ res.write(Buffer.alloc(65_536, 1));
1150
+ responseClosed = once(res, "close");
1151
+ const finish = setTimeout(() => res.end(Buffer.alloc(65_536, 2)), 1_000);
1152
+ res.once("close", () => clearTimeout(finish));
1153
+ });
1154
+ server.listen(0, "127.0.0.1");
1155
+ await once(server, "listening");
1156
+ const address = server.address();
1157
+ if (!address || typeof address === "string") {
1158
+ throw new Error("test server did not bind a TCP port");
1159
+ }
1160
+
1161
+ try {
1162
+ const io = new PresignObjectIO(
1163
+ streamingVault(`http://127.0.0.1:${address.port}`),
1164
+ COMPANY,
1165
+ );
1166
+ const streamed = await io.getObjectStream("large");
1167
+ const iterator = streamed.body[Symbol.asyncIterator]();
1168
+ expect((await iterator.next()).value).toBeInstanceOf(Uint8Array);
1169
+ await iterator.return?.();
1170
+ if (!responseClosed) throw new Error("response did not start streaming");
1171
+ await expect(responseClosed).resolves.toBeDefined();
1172
+
1173
+ await expect(io.getObject("short")).rejects.toThrow(
1174
+ /content-length mismatch/,
1175
+ );
1176
+ } finally {
1177
+ server.close();
1178
+ await once(server, "close");
1179
+ }
1180
+ });
1181
+
1182
+ it.runIf(
1183
+ process.platform === "win32" && /^(22|24)\./.test(process.versions.node),
1184
+ )("keeps a child FIN safe after the consumer has applied backpressure", async () => {
1185
+ setPresignedGetTransportForTesting(null);
1186
+ const child = spawn(process.execPath, [
1187
+ "-e",
1188
+ [
1189
+ "const http = require('node:http');",
1190
+ "const body = Buffer.alloc(524288, 7);",
1191
+ "const server = http.createServer((_req, res) => {",
1192
+ " res.writeHead(200, { 'content-length': String(body.length) });",
1193
+ " for (let offset = 0; offset < body.length; offset += 16384) res.write(body.subarray(offset, offset + 16384));",
1194
+ " res.end();",
1195
+ "});",
1196
+ "server.listen(0, '127.0.0.1', () => process.stdout.write(String(server.address().port) + '\\n'));",
1197
+ "process.on('SIGTERM', () => server.close(() => process.exit(0)));",
1198
+ ].join("\n"),
1199
+ ], { stdio: ["ignore", "pipe", "inherit"] });
1200
+
1201
+ const port = await new Promise<number>((resolve, reject) => {
1202
+ child.once("error", reject);
1203
+ child.stdout?.once("data", (data: Buffer) => resolve(Number(data.toString())));
1204
+ child.once("exit", (code) => reject(new Error(`child server exited (${code})`)));
1205
+ });
1206
+
1207
+ try {
1208
+ const io = new PresignObjectIO(
1209
+ streamingVault(`http://127.0.0.1:${port}`),
1210
+ COMPANY,
1211
+ );
1212
+ const streamed = await io.getObjectStream("bulk");
1213
+ await new Promise((resolve) => setTimeout(resolve, 40));
1214
+ let bytes = 0;
1215
+ for await (const chunk of streamed.body) bytes += chunk.byteLength;
1216
+ expect(bytes).toBe(524_288);
1217
+ } finally {
1218
+ child.kill("SIGTERM");
1219
+ }
1220
+ });
1221
+ });
package/src/object-io.ts CHANGED
@@ -13,8 +13,8 @@
13
13
  * - `PresignObjectIO` — the presigned-URL path. The vault-service decides
14
14
  * access as a runtime DDB check (no IAM policy ceiling) and hands back
15
15
  * short-lived presigned GET/PUT/DELETE URLs + (for PUT) the exact headers
16
- * to replay. The client never holds AWS credentials — it just `fetch`es
17
- * the signed URLs.
16
+ * to replay. The client never holds AWS credentials — it requests the
17
+ * signed URLs directly.
18
18
  *
19
19
  * The seam is a per-EntityContext factory resolved INSIDE s3.ts, so every
20
20
  * existing call site (`uploadFile(ctx, …)`, `downloadFile(ctx, …)`, …) keeps
@@ -31,6 +31,9 @@ import {
31
31
  DeleteObjectCommand,
32
32
  HeadObjectCommand,
33
33
  } from "@aws-sdk/client-s3";
34
+ import * as http from "node:http";
35
+ import * as https from "node:https";
36
+ import type { IncomingMessage } from "node:http";
34
37
  import type { EntityContext } from "./types.js";
35
38
  import type {
36
39
  PresignOp,
@@ -344,6 +347,141 @@ function metaFromHeaders(headers: Headers): Record<string, string> {
344
347
  return meta;
345
348
  }
346
349
 
350
+ /**
351
+ * Response shape shared by every presigned GET consumer. Unlike the runtime
352
+ * `fetch` response, its body is backed by Node's core HTTP client, so it never
353
+ * enters the runtime-bundled undici parser path.
354
+ */
355
+ export interface PresignedGetResponse {
356
+ status: number;
357
+ headers: Headers;
358
+ body?: AsyncIterable<Uint8Array>;
359
+ destroy(): void;
360
+ }
361
+
362
+ /** Transport seam retained for deterministic wire-level regression tests. */
363
+ export interface PresignedGetTransport {
364
+ get(
365
+ url: string,
366
+ headers: Record<string, string> | undefined,
367
+ ): Promise<PresignedGetResponse>;
368
+ }
369
+
370
+ function headersFromNode(
371
+ headers: IncomingMessage["headers"],
372
+ ): Headers {
373
+ const out = new Headers();
374
+ for (const [name, value] of Object.entries(headers)) {
375
+ if (value === undefined) continue;
376
+ out.set(name, Array.isArray(value) ? value.join(", ") : value);
377
+ }
378
+ return out;
379
+ }
380
+
381
+ function expectedContentLength(headers: Headers): number | undefined {
382
+ const value = headers.get("content-length");
383
+ if (value === null) return undefined;
384
+ if (!/^\d+$/.test(value)) {
385
+ throw new Error(`invalid presigned GET content-length: ${value}`);
386
+ }
387
+ const length = Number(value);
388
+ if (!Number.isSafeInteger(length)) {
389
+ throw new Error(`invalid presigned GET content-length: ${value}`);
390
+ }
391
+ return length;
392
+ }
393
+
394
+ function contentLengthError(expected: number, actual: number): Error {
395
+ return new Error(
396
+ `presigned GET content-length mismatch: expected ${expected} bytes, received ${actual}`,
397
+ );
398
+ }
399
+
400
+ async function* checkedNodeBody(
401
+ message: IncomingMessage,
402
+ expected: number | undefined,
403
+ ): AsyncIterable<Uint8Array> {
404
+ let received = 0;
405
+ let exhausted = false;
406
+ try {
407
+ try {
408
+ for await (const chunk of message) {
409
+ const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
410
+ received += bytes.byteLength;
411
+ if (expected !== undefined && received > expected) {
412
+ throw contentLengthError(expected, received);
413
+ }
414
+ yield bytes;
415
+ }
416
+ } catch (err) {
417
+ // A FIN before Node has received the declared bytes normally surfaces as
418
+ // an "aborted" stream error. Preserve the useful data-integrity cause.
419
+ if (expected !== undefined && received !== expected) {
420
+ throw contentLengthError(expected, received);
421
+ }
422
+ throw err;
423
+ }
424
+ exhausted = true;
425
+ if (expected !== undefined && received !== expected) {
426
+ throw contentLengthError(expected, received);
427
+ }
428
+ } finally {
429
+ // Consumers such as HEAD only inspect headers, and a caller can stop after
430
+ // a prefix. Destroying in both cases releases the socket immediately.
431
+ if (!exhausted) message.destroy();
432
+ }
433
+ }
434
+
435
+ function nodePresignedGet(
436
+ url: string,
437
+ headers: Record<string, string> | undefined,
438
+ ): Promise<PresignedGetResponse> {
439
+ const target = new URL(url);
440
+ return new Promise((resolve, reject) => {
441
+ const handleResponse = (message: IncomingMessage): void => {
442
+ const responseHeaders = headersFromNode(message.headers);
443
+ let expected: number | undefined;
444
+ try {
445
+ expected = expectedContentLength(responseHeaders);
446
+ } catch (err) {
447
+ message.destroy();
448
+ reject(err);
449
+ return;
450
+ }
451
+ resolve({
452
+ status: message.statusCode ?? 0,
453
+ headers: responseHeaders,
454
+ body: checkedNodeBody(message, expected),
455
+ destroy: () => message.destroy(),
456
+ });
457
+ };
458
+ const request = target.protocol === "https:"
459
+ ? https.request(target, { method: "GET", headers }, handleResponse)
460
+ : target.protocol === "http:"
461
+ ? http.request(target, { method: "GET", headers }, handleResponse)
462
+ : undefined;
463
+ if (!request) {
464
+ reject(new Error(`unsupported presigned GET protocol: ${target.protocol}`));
465
+ return;
466
+ }
467
+ request.once("error", reject);
468
+ request.end();
469
+ });
470
+ }
471
+
472
+ const NODE_PRESIGNED_GET_TRANSPORT: PresignedGetTransport = {
473
+ get: nodePresignedGet,
474
+ };
475
+
476
+ let presignedGetTransport: PresignedGetTransport = NODE_PRESIGNED_GET_TRANSPORT;
477
+
478
+ /** Test-only override; production always uses Node's core HTTP(S) transport. */
479
+ export function setPresignedGetTransportForTesting(
480
+ transport: PresignedGetTransport | null,
481
+ ): void {
482
+ presignedGetTransport = transport ?? NODE_PRESIGNED_GET_TRANSPORT;
483
+ }
484
+
347
485
  /**
348
486
  * Per-key presign denial codes that are EXPECTED and must be reclassified as
349
487
  * skip-with-log — throwing the `name: "Forbidden"` shape (see
@@ -825,20 +963,21 @@ export class PresignObjectIO implements ObjectIO {
825
963
 
826
964
  async getObjectStream(key: string): Promise<GetObjectStreamResult> {
827
965
  const row = await this.resolveGetUrlForBody(key);
828
- const res = await fetchWithRetry(row.url, { method: "GET" }, `presigned GET ${key}`);
966
+ const res = await presignedGetWithRetry(
967
+ row.url,
968
+ row.headers,
969
+ `presigned GET ${key}`,
970
+ );
829
971
  if (res.status === 404) {
830
- await cancelBody(res);
972
+ res.destroy();
831
973
  throw notFoundError(key);
832
974
  }
833
- if (!res.ok) {
834
- const detail = await safeText(res);
975
+ if (res.status < 200 || res.status >= 300) {
976
+ const detail = await safePresignedGetText(res);
835
977
  throw new Error(`presigned GET failed for ${key}: ${res.status} ${detail}`);
836
978
  }
837
- if (!res.body) {
838
- return { body: (async function* () {})(), metadata: metaFromHeaders(res.headers) };
839
- }
840
979
  return {
841
- body: webReadableToAsyncIterable(res.body),
980
+ body: res.body ?? (async function* () {})(),
842
981
  metadata: metaFromHeaders(res.headers),
843
982
  };
844
983
  }
@@ -933,9 +1072,13 @@ export class PresignObjectIO implements ObjectIO {
933
1072
  }
934
1073
  url = row.url;
935
1074
  }
936
- const res = await fetchWithRetry(url, { method: "GET" }, `presigned HEAD ${key}`);
1075
+ const res = await presignedGetWithRetry(
1076
+ url,
1077
+ undefined,
1078
+ `presigned HEAD ${key}`,
1079
+ );
937
1080
  if (res.status === 404) {
938
- await cancelBody(res);
1081
+ res.destroy();
939
1082
  return null;
940
1083
  }
941
1084
  if (res.status === 403) {
@@ -944,12 +1087,11 @@ export class PresignObjectIO implements ObjectIO {
944
1087
  // denial branch above. The SDK transport throws name:"Forbidden" here;
945
1088
  // mirror it so both transports agree and no caller mistakes a denial
946
1089
  // for a missing object.
947
- await cancelBody(res);
1090
+ res.destroy();
948
1091
  throw accessDeniedError(key, "presigned HEAD returned 403");
949
1092
  }
950
- if (!res.ok) {
951
- await cancelBody(res);
952
- const detail = await safeText(res);
1093
+ if (res.status < 200 || res.status >= 300) {
1094
+ const detail = await safePresignedGetText(res);
953
1095
  throw new Error(`presigned HEAD failed for ${key}: ${res.status} ${detail}`);
954
1096
  }
955
1097
  const result: HeadObjectResult = {
@@ -958,7 +1100,7 @@ export class PresignObjectIO implements ObjectIO {
958
1100
  size: Number(res.headers.get("content-length") ?? "0"),
959
1101
  metadata: metaFromHeaders(res.headers),
960
1102
  };
961
- await cancelBody(res);
1103
+ res.destroy();
962
1104
  return result;
963
1105
  }
964
1106
  }
@@ -979,21 +1121,6 @@ async function cancelBody(res: Response): Promise<void> {
979
1121
  }
980
1122
  }
981
1123
 
982
- async function* webReadableToAsyncIterable(
983
- stream: ReadableStream<Uint8Array>,
984
- ): AsyncIterable<Uint8Array> {
985
- const reader = stream.getReader();
986
- try {
987
- while (true) {
988
- const { done, value } = await reader.read();
989
- if (done) return;
990
- if (value && value.byteLength > 0) yield value;
991
- }
992
- } finally {
993
- reader.releaseLock();
994
- }
995
- }
996
-
997
1124
  function parseLastModified(value: string | null): Date {
998
1125
  if (!value) return new Date();
999
1126
  const d = new Date(value);
@@ -1023,6 +1150,60 @@ function sleep(ms: number): Promise<void> {
1023
1150
  return new Promise((resolve) => setTimeout(resolve, ms));
1024
1151
  }
1025
1152
 
1153
+ /**
1154
+ * Shared presigned-GET path for streaming reads and header-only probes.
1155
+ *
1156
+ * GET deliberately uses Node core HTTP(S), not global fetch: on affected
1157
+ * Node/Windows builds the bundled undici parser can assert after a FIN arrives
1158
+ * while the consumer is backpressured. Keep the retry budget identical to the
1159
+ * PUT/DELETE fetch path below.
1160
+ */
1161
+ async function presignedGetWithRetry(
1162
+ url: string,
1163
+ headers: Record<string, string> | undefined,
1164
+ what: string,
1165
+ ): Promise<PresignedGetResponse> {
1166
+ let lastError: unknown;
1167
+ for (let attempt = 0; attempt <= FETCH_MAX_RETRIES; attempt++) {
1168
+ if (attempt > 0) {
1169
+ const backoff = FETCH_BASE_DELAY_MS * 2 ** (attempt - 1);
1170
+ const jitter = Math.floor(Math.random() * FETCH_BASE_DELAY_MS);
1171
+ await sleep(backoff + jitter);
1172
+ }
1173
+ let res: PresignedGetResponse;
1174
+ try {
1175
+ res = await presignedGetTransport.get(url, headers);
1176
+ } catch (err) {
1177
+ lastError = err;
1178
+ continue;
1179
+ }
1180
+ if (isTransientStatus(res.status) && attempt < FETCH_MAX_RETRIES) {
1181
+ res.destroy();
1182
+ lastError = new Error(`${what}: transient ${res.status}`);
1183
+ continue;
1184
+ }
1185
+ return res;
1186
+ }
1187
+ throw lastError instanceof Error
1188
+ ? lastError
1189
+ : new Error(`${what}: failed after ${FETCH_MAX_RETRIES} retries`);
1190
+ }
1191
+
1192
+ async function safePresignedGetText(res: PresignedGetResponse): Promise<string> {
1193
+ try {
1194
+ let text = "";
1195
+ for await (const chunk of res.body ?? []) {
1196
+ text += Buffer.from(chunk).toString("utf-8");
1197
+ if (text.length >= 200) break;
1198
+ }
1199
+ return text.slice(0, 200);
1200
+ } catch {
1201
+ return "";
1202
+ } finally {
1203
+ res.destroy();
1204
+ }
1205
+ }
1206
+
1026
1207
  /**
1027
1208
  * `fetch` with bounded retry on network errors + transient 5xx. The presigned
1028
1209
  * URL is reusable until expiry and the bodies are in-memory Buffers, so a
package/src/s3.ts CHANGED
@@ -764,6 +764,13 @@ export async function uploadSymlink(
764
764
  * return it to callers — e.g. the pull loop reads `created-by` to
765
765
  * attribute downloaded files to their author with zero extra network.
766
766
  */
767
+ export interface DownloadModeWarning {
768
+ /** The mode guardrail that could not be applied exactly. */
769
+ reason: "missing-hq-mode" | "invalid-hq-mode" | "chmod-failed";
770
+ /** Legacy objects can retain a local file's known-good permission bits. */
771
+ fallback?: "preserved-local-mode" | "receiver-default";
772
+ }
773
+
767
774
  export async function downloadFile(
768
775
  ctx: EntityContext,
769
776
  key: string,
@@ -772,6 +779,8 @@ export async function downloadFile(
772
779
  metadata?: Record<string, string>;
773
780
  contentHash?: string;
774
781
  contentSize?: number;
782
+ /** Non-fatal mode guardrail warnings for caller telemetry. */
783
+ modeWarnings?: DownloadModeWarning[];
775
784
  }> {
776
785
  const io = resolveObjectIO(ctx);
777
786
 
@@ -867,15 +876,25 @@ export async function downloadFile(
867
876
  return { metadata };
868
877
  }
869
878
  const tempPath = downloadTempPath(localPath);
879
+ // A legacy object has no hq-mode. If it is replacing a regular local file,
880
+ // that local mode is the only trustworthy signal we have — retaining it is
881
+ // strictly safer than replacing (for example) a 0755 hook with the receiver
882
+ // umask's usual 0644. Never inspect a symlink or infer intent from content.
883
+ let existingLocalMode: number | undefined;
884
+ try {
885
+ const existing = fs.lstatSync(localPath);
886
+ if (existing.isFile()) existingLocalMode = existing.mode & 0o777;
887
+ } catch {
888
+ // Fresh destination / inaccessible prior path: no local fallback.
889
+ }
870
890
  let tempReady = true;
871
891
  let streamed: { hash: string; size: number };
892
+ const modeWarnings: DownloadModeWarning[] = [];
872
893
  try {
873
894
  streamed = await streamRegularFileToTemp(tempPath, initialChunks, iterator);
874
895
 
875
896
  // Bug #5 — apply source-side mode after the byte write. See
876
- // FILE_MODE_META_KEY for the metadata contract. Parses defensively:
877
- // a malformed value falls through with no chmod so the umask default
878
- // applies, matching the legacy back-compat path. The staged path is a
897
+ // FILE_MODE_META_KEY for the metadata contract. The staged path is a
879
898
  // regular file, then it is atomically renamed over the destination.
880
899
  //
881
900
  // Codex P2 (PR #24 round 3): strict octal-only regex BEFORE parseInt.
@@ -886,17 +905,33 @@ export async function downloadFile(
886
905
  // the upload side stamps (`(mode & 0o777).toString(8)` → at most
887
906
  // three digits, all 0–7) and rejects everything else.
888
907
  const modeOctal = metadata?.[FILE_MODE_META_KEY];
908
+ let modeToApply: number | undefined;
889
909
  if (typeof modeOctal === "string" && /^[0-7]{1,4}$/.test(modeOctal)) {
890
910
  const parsed = parseInt(modeOctal, 8);
891
911
  if (Number.isFinite(parsed) && parsed >= 0 && parsed <= 0o777) {
892
- try {
893
- fs.chmodSync(tempPath, parsed);
894
- } catch {
895
- // chmod failure (read-only FS, EPERM) is non-fatal — the file
896
- // is materialized, just with the umask default. Surface via
897
- // S3-side metadata being present but the file not matching;
898
- // a future operator-side audit can reconcile.
899
- }
912
+ modeToApply = parsed;
913
+ }
914
+ } else if (modeOctal === undefined) {
915
+ modeToApply = existingLocalMode;
916
+ modeWarnings.push({
917
+ reason: "missing-hq-mode",
918
+ fallback:
919
+ existingLocalMode === undefined
920
+ ? "receiver-default"
921
+ : "preserved-local-mode",
922
+ });
923
+ } else {
924
+ modeWarnings.push({ reason: "invalid-hq-mode", fallback: "receiver-default" });
925
+ }
926
+
927
+ if (modeToApply !== undefined) {
928
+ try {
929
+ fs.chmodSync(tempPath, modeToApply);
930
+ } catch {
931
+ // chmod failure (read-only FS, EPERM) is non-fatal — the file is
932
+ // materialized, but callers must receive a telemetry signal instead
933
+ // of silently believing the guardrail held.
934
+ modeWarnings.push({ reason: "chmod-failed" });
900
935
  }
901
936
  }
902
937
 
@@ -953,7 +988,21 @@ export async function downloadFile(
953
988
  // distinct creation time, so a future receiver upgrade picks it up
954
989
  // automatically without a server-side data migration.
955
990
 
956
- return { metadata, contentHash: streamed.hash, contentSize: streamed.size };
991
+ if (modeWarnings.length > 0) {
992
+ for (const warning of modeWarnings) {
993
+ console.warn(
994
+ `[hq-sync] mode warning for ${key}: ${warning.reason}` +
995
+ (warning.fallback ? ` (${warning.fallback})` : ""),
996
+ );
997
+ }
998
+ }
999
+
1000
+ return {
1001
+ metadata,
1002
+ contentHash: streamed.hash,
1003
+ contentSize: streamed.size,
1004
+ ...(modeWarnings.length > 0 ? { modeWarnings } : {}),
1005
+ };
957
1006
  }
958
1007
 
959
1008
  export interface RemoteFile {
@@ -40,6 +40,7 @@ import type {
40
40
  SyncJournal,
41
41
  } from "./types.js";
42
42
  import { hashFile, tombstoneEntry } from "./journal.js";
43
+ import { localPathForVaultKey } from "./local-path-codec.js";
43
44
  import {
44
45
  isCoveredByAny,
45
46
  type ScopePrefixInput,
@@ -168,7 +169,7 @@ function classifyOrphan(
168
169
  entry: JournalEntry,
169
170
  hqRoot: string,
170
171
  ): OrphanClassification {
171
- const absPath = path.join(hqRoot, relPath);
172
+ const absPath = localPathForVaultKey(hqRoot, relPath);
172
173
  let stat: fs.Stats;
173
174
  try {
174
175
  stat = fs.lstatSync(absPath);
@@ -436,9 +437,9 @@ export function applyScopeShrink(
436
437
  const dirtyKeptPaths: string[] = [];
437
438
 
438
439
  for (const orphan of plan.clean) {
439
- const absPath = path.join(hqRoot, orphan.path);
440
+ const absPath = localPathForVaultKey(hqRoot, orphan.path);
440
441
  if (quarantining) {
441
- const destAbs = path.join(input.quarantineRoot!, orphan.path);
442
+ const destAbs = localPathForVaultKey(input.quarantineRoot!, orphan.path);
442
443
  quarantineOrphan(absPath, destAbs);
443
444
  tombstoneEntry(journal, orphan.path, reason);
444
445
  cleanQuarantined++;