@indigoai-us/hq-cloud 6.18.10 → 6.18.11-alpha.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.
@@ -5,11 +5,12 @@
5
5
  * port: the control flow, three-way drift classification, routing, YAML
6
6
  * stamping, mtime restoration, backup snapshot and summary are implemented in
7
7
  * TypeScript, while the heavy primitives the bash itself delegated to —
8
- * `git` (clone/checkout/hash-object/cat-file/ls-tree/show/log), `rsync` (the
9
- * overlay), and the YAML editor (`yq`/`python3`) — are still invoked as
10
- * subprocesses. Reimplementing git's blob hashing or rsync's include/exclude
11
- * filter engine in pure JS would *increase* the risk of behavioural drift, so
12
- * the port reuses the exact same tools with the exact same argument vectors.
8
+ * `git` (clone/checkout/hash-object/cat-file/ls-tree/show/log) and `rsync`
9
+ * (the overlay) — are still invoked as subprocesses. YAML stamp reads and
10
+ * writes are handled in-process so a rescue does not depend on optional YAML
11
+ * executables being present. Reimplementing git's blob hashing or rsync's
12
+ * include/exclude filter engine in pure JS would *increase* the risk of
13
+ * behavioural drift, so the port reuses those exact tools and argument vectors.
13
14
  *
14
15
  * Behaviour (classification outcomes, on-disk results, log lines, exit codes)
15
16
  * is preserved 1:1 with the legacy script. See the original script's header in
@@ -31,8 +32,10 @@ import * as crypto from "crypto";
31
32
  import * as fs from "fs";
32
33
  import * as os from "os";
33
34
  import * as path from "path";
35
+ import yaml from "js-yaml";
34
36
  import { assertRescuePathTypeUnchanged, isRescueTransientDirectory, } from "./rescue-snapshot.js";
35
37
  import { applyBackupPrune, canonicalRootHash, inventoryManagedSnapshots, isManagedSnapshotManifest, } from "../backup-prune.js";
38
+ import { readCoreSourceStamp, writeCoreSourceStamp } from "./core-source-stamp.js";
36
39
  // Paths always preserved across wipe+overlay (shuttled out, restored after).
37
40
  const CARVE_OUT_PATHS = [
38
41
  "core/packages",
@@ -268,11 +271,6 @@ function runGitWithNetworkRetry(args, opts, out, beforeRetry) {
268
271
  }
269
272
  throw new Error("Git retry loop ended without a result");
270
273
  }
271
- /** `command -v <bin>` — is a binary on PATH? */
272
- function hasCmd(bin, env) {
273
- const r = spawnSync(bin, ["--version"], { env, stdio: "ignore" });
274
- return !r.error && r.status === 0;
275
- }
276
274
  function boundedDiagnostic(value, limit) {
277
275
  const marker = "… (truncated)";
278
276
  if (value.length <= limit)
@@ -729,7 +727,7 @@ export function runRescue(argv, opts = {}) {
729
727
  return recoverSnapshots(cfg, out, err);
730
728
  return doRescue(cfg, env, out, err, (d) => {
731
729
  tmpdir = d;
732
- }, opts.gitReachabilityProbeTimeoutMs ?? GIT_REACHABILITY_PROBE_TIMEOUT_MS, opts.restoreOnRsyncFailure === true);
730
+ }, opts.gitReachabilityProbeTimeoutMs ?? GIT_REACHABILITY_PROBE_TIMEOUT_MS, opts.coreSourceStampWriter, opts.restoreOnRsyncFailure === true);
733
731
  }
734
732
  catch (e) {
735
733
  if (e instanceof ExitError)
@@ -1516,7 +1514,7 @@ function recoveryProof(unresolved, cfg, hqRoot, srcDir, srcSha, rsyncLauncher, r
1516
1514
  }
1517
1515
  return { eligible: !hasSkippedSnapshotEntries(newest.entry.manifest), failures };
1518
1516
  }
1519
- function doRescue(cfg, env, out, err, setTmp, gitReachabilityProbeTimeoutMs = GIT_REACHABILITY_PROBE_TIMEOUT_MS, restoreOnRsyncFailure = false) {
1517
+ function doRescue(cfg, env, out, err, setTmp, gitReachabilityProbeTimeoutMs = GIT_REACHABILITY_PROBE_TIMEOUT_MS, stampWriter = writeCoreSourceStamp, restoreOnRsyncFailure = false) {
1520
1518
  const hqRoot = resolveHqRoot(cfg, err);
1521
1519
  const RUN_TS = utcStamp(new Date(), "dash");
1522
1520
  // Both the release overlay and a restorable snapshot require rsync. A shim
@@ -1548,13 +1546,20 @@ function doRescue(cfg, env, out, err, setTmp, gitReachabilityProbeTimeoutMs = GI
1548
1546
  let prevSyncRef = "";
1549
1547
  let prevSyncAt = "";
1550
1548
  const coreYaml = path.join(hqRoot, "core", "core.yaml");
1551
- const yqAvailable = hasCmd("yq", env);
1552
- if (isFileFollow(coreYaml) && yqAvailable) {
1553
- const yq = (expr) => run("yq", ["-r", expr, coreYaml], { env }).stdout.trim();
1554
- prevSyncSha = yq('.replaced_from_source.last_sync_sha // .replaced_from_staging.last_sync_sha // ""');
1555
- prevSyncSource = yq('.replaced_from_source.source // .replaced_from_staging.source // ""');
1556
- prevSyncRef = yq('.replaced_from_source.ref // .replaced_from_staging.ref // ""');
1557
- prevSyncAt = yq('.replaced_from_source.last_sync_at // .replaced_from_staging.last_sync_at // ""');
1549
+ if (isFileFollow(coreYaml)) {
1550
+ const priorCoreYaml = fs.readFileSync(coreYaml, "utf-8");
1551
+ try {
1552
+ const stamp = readCoreSourceStamp(priorCoreYaml);
1553
+ prevSyncSha = stamp.last_sync_sha;
1554
+ prevSyncSource = stamp.source;
1555
+ prevSyncRef = stamp.ref;
1556
+ prevSyncAt = stamp.last_sync_at;
1557
+ }
1558
+ catch (error) {
1559
+ if (!(error instanceof yaml.YAMLException))
1560
+ throw error;
1561
+ err(" WARN: prior stamp in core/core.yaml is invalid YAML; treating as unstamped\n");
1562
+ }
1558
1563
  }
1559
1564
  // An explicit caller floor is the recovery path for a missing or stale stamp.
1560
1565
  // It remains safe because the selected release clone must still contain it.
@@ -2261,61 +2266,20 @@ function doRescue(cfg, env, out, err, setTmp, gitReachabilityProbeTimeoutMs = GI
2261
2266
  !coreYamlSkipped &&
2262
2267
  !Number.isNaN(srcCommitDate.getTime())) {
2263
2268
  const nowUtc = utcStamp(srcCommitDate, "colon");
2264
- if (yqAvailable) {
2265
- const stampEnv = {
2266
- ...env,
2267
- SHA: srcSha,
2268
- SOURCE: cfg.sourceRepo,
2269
- THE_REF: cfg.ref,
2270
- AT: nowUtc,
2271
- };
2272
- run("yq", [
2273
- "-i",
2274
- ".replaced_from_source.source = strenv(SOURCE) |\n" +
2275
- " .replaced_from_source.ref = strenv(THE_REF) |\n" +
2276
- " .replaced_from_source.last_sync_sha = strenv(SHA) |\n" +
2277
- " .replaced_from_source.last_sync_at = strenv(AT) |\n" +
2278
- " del(.replaced_from_staging)",
2279
- coreYaml,
2280
- ], { env: stampEnv });
2281
- out(`==> Stamped core/core.yaml: replaced_from_source.last_sync_sha=${srcSha}\n`);
2282
- }
2283
- else if (hasCmd("python3", env) &&
2284
- run("python3", ["-c", "import yaml"], { env }).status === 0) {
2285
- const pyEnv = {
2286
- ...env,
2287
- SHA: srcSha,
2288
- SOURCE: cfg.sourceRepo,
2289
- THE_REF: cfg.ref,
2290
- AT: nowUtc,
2291
- CORE: coreYaml,
2292
- };
2293
- run("python3", [
2294
- "-c",
2295
- [
2296
- "import os, yaml",
2297
- 'path = os.environ["CORE"]',
2298
- "try:",
2299
- " with open(path) as f:",
2300
- " d = yaml.safe_load(f) or {}",
2301
- "except FileNotFoundError:",
2302
- " d = {}",
2303
- 'd["replaced_from_source"] = {',
2304
- ' "source": os.environ["SOURCE"],',
2305
- ' "ref": os.environ["THE_REF"],',
2306
- ' "last_sync_sha": os.environ["SHA"],',
2307
- ' "last_sync_at": os.environ["AT"],',
2308
- "}",
2309
- 'd.pop("replaced_from_staging", None)',
2310
- 'with open(path, "w") as f:',
2311
- " yaml.safe_dump(d, f, default_flow_style=False, sort_keys=False)",
2312
- ].join("\n"),
2313
- ], { env: pyEnv });
2314
- out(`==> Stamped core/core.yaml: replaced_from_source.last_sync_sha=${srcSha}\n`);
2269
+ try {
2270
+ stampWriter(coreYaml, {
2271
+ source: cfg.sourceRepo,
2272
+ ref: cfg.ref,
2273
+ last_sync_sha: srcSha,
2274
+ last_sync_at: nowUtc,
2275
+ });
2315
2276
  }
2316
- else {
2317
- err(" WARN: neither yq nor python3+PyYAML available — skipping core/core.yaml stamp\n");
2277
+ catch (error) {
2278
+ const message = error instanceof Error ? error.message : "unknown file write error";
2279
+ err(` error: failed to stamp core/core.yaml: ${message}\n`);
2280
+ throw new ExitError(1);
2318
2281
  }
2282
+ out(`==> Stamped core/core.yaml: replaced_from_source.last_sync_sha=${srcSha}\n`);
2319
2283
  }
2320
2284
  // NOTE (6.2.4): the post-rescue "sync-journal baseline reconcile" that lived
2321
2285
  // here (6.2.1 itemize-parse, 6.2.3 hash-diff) is intentionally GONE. Masking