@indigoai-us/hq-cli 5.345.269 → 5.345.270

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.
@@ -65,12 +65,8 @@ import { CORE_NEXT_FLAG, coreNextEnabled } from './core-next-flag.js';
65
65
  export { coreNextStrictReason, ENGINE_STRICT_ENV, ENGINE_STRICT_FILE } from './core-next-kill.js';
66
66
  import { coreNextStrictReason, ENGINE_STRICT_FILE } from './core-next-kill.js';
67
67
  const HOOK_ENGINE_LOG_RELATIVE = path.join('.hq', 'logs', 'hook-engine.jsonl');
68
- /** Name of the release-stable link next to the pinned engine versions. */
69
- export const STABLE_LINK_NAME = 'stable';
70
- /** Record of canary-accepted hook engines committed to `stable`, under the home folder. */
71
- export const ACCEPTED_HOOK_ENGINE_RECORD = path.join('.hq', 'bin', 'hq-hook', 'accepted-engine.json');
72
- /** Schema of [`ACCEPTED_HOOK_ENGINE_RECORD`]. */
73
- export const ACCEPTED_HOOK_ENGINE_SCHEMA = 'hq.accepted-hook-engine.v1';
68
+ export { ACCEPTED_HOOK_ENGINE_RECORD, ACCEPTED_HOOK_ENGINE_SCHEMA, STABLE_LINK_NAME, StableDowngradeRefused, linkStableEngine, readAcceptedHookEngineRecord, stableEnginePath, } from '../lib/hook-engine/stable-link.js';
69
+ import { ACCEPTED_HOOK_ENGINE_RECORD, ACCEPTED_HOOK_ENGINE_SCHEMA, STABLE_LINK_NAME, StableDowngradeRefused, hookEngineDir, linkStableEngine, readAcceptedHookEngineRecord, stableEnginePath, } from '../lib/hook-engine/stable-link.js';
74
70
  const coreNextCandidates = new WeakMap();
75
71
  /** Read the candidate engine metadata attached by [`coreNextTarget`], if any. */
76
72
  export function getCoreNextCandidate(target) {
@@ -192,113 +188,6 @@ export function checkStableEngine(stable, pinned) {
192
188
  if (!fs.statSync(resolved).isFile())
193
189
  throw new Error(`${resolved} is not a file`);
194
190
  }
195
- function hookEngineDir(home) {
196
- return path.join(home, '.hq', 'bin', 'hq-hook');
197
- }
198
- /** The stable path that stands for a pinned engine path. */
199
- export function stableEnginePath(pinned, home) {
200
- const relative = path.relative(hookEngineDir(home), pinned);
201
- const [, ...rest] = relative.split(/[\\/]/);
202
- if (relative.startsWith('..') || path.isAbsolute(relative) || rest.length === 0) {
203
- throw new Error(`hook engine ${pinned} is not under ${hookEngineDir(home)}`);
204
- }
205
- return path.join(hookEngineDir(home), STABLE_LINK_NAME, ...rest);
206
- }
207
- /**
208
- * Point `<home>/.hq/bin/hq-hook/stable` at the pinned engine's version folder
209
- * and return the stable engine path. The link is replaced by rename, so a
210
- * planted file at the temporary name or the link itself is never followed. A
211
- * `stable` that is a real folder (not a link) is someone else's and is left
212
- * alone: the call fails and the caller keeps the current registrations.
213
- */
214
- export function linkStableEngine(pinned, home, platform = process.platform) {
215
- const root = hookEngineDir(home);
216
- const relative = path.relative(root, pinned).split(/[\\/]/);
217
- const version = relative[0];
218
- if (!version || version === '..' || version === STABLE_LINK_NAME || path.isAbsolute(path.relative(root, pinned))) {
219
- throw new Error(`hook engine ${pinned} is not under ${root}`);
220
- }
221
- const stable = path.join(root, STABLE_LINK_NAME);
222
- // Store rule v2: every path component this follows or creates under the home
223
- // folder is a real folder of this user that group and others cannot write:
224
- // .hq, .hq/bin, .hq/bin/hq-hook, the version folder and any folder below it
225
- // up to the engine, and the engine file itself.
226
- const components = [path.join(home, '.hq'), path.join(home, '.hq', 'bin'), root];
227
- for (let i = 0; i < relative.length - 1; i += 1)
228
- components.push(path.join(root, ...relative.slice(0, i + 1)));
229
- for (const component of components)
230
- checkOwnedEntry(component, 'folder', platform);
231
- checkOwnedEntry(pinned, 'file', platform);
232
- // Windows creates a junction, which needs an absolute target; elsewhere the
233
- // link is relative so it names the version folder beside it.
234
- const target = platform === 'win32' ? path.join(root, version) : version;
235
- let current;
236
- try {
237
- current = fs.lstatSync(stable);
238
- }
239
- catch (error) {
240
- if (error.code !== 'ENOENT')
241
- throw error;
242
- }
243
- if (current && !current.isSymbolicLink())
244
- throw new Error(`${stable} exists and is not a link; leaving it in place`);
245
- if (current && pointsAt(stable, path.join(root, version)))
246
- return stableEnginePath(pinned, home);
247
- if (platform === 'win32') {
248
- // A junction cannot be renamed over another. A running engine keeps its
249
- // own image open, which the junction does not hold.
250
- if (current) {
251
- try {
252
- fs.unlinkSync(stable);
253
- }
254
- catch {
255
- fs.rmdirSync(stable);
256
- }
257
- }
258
- fs.symlinkSync(target, stable, 'junction');
259
- }
260
- else {
261
- const staged = path.join(root, `.${STABLE_LINK_NAME}.${process.pid}.tmp`);
262
- try {
263
- fs.unlinkSync(staged);
264
- }
265
- catch (error) {
266
- if (error.code !== 'ENOENT')
267
- throw error;
268
- }
269
- fs.symlinkSync(target, staged);
270
- try {
271
- fs.renameSync(staged, stable);
272
- }
273
- catch (error) {
274
- try {
275
- fs.unlinkSync(staged);
276
- }
277
- catch { /* already gone */ }
278
- throw error;
279
- }
280
- }
281
- return stableEnginePath(pinned, home);
282
- }
283
- /** A real folder or file (not a link) of this user that group and others cannot write. */
284
- function checkOwnedEntry(entry, kind, platform) {
285
- const stat = fs.lstatSync(entry);
286
- if (stat.isSymbolicLink() || (kind === 'folder' ? !stat.isDirectory() : !stat.isFile()))
287
- throw new Error(`${entry} is not a ${kind}`);
288
- if (typeof process.getuid === 'function' && stat.uid !== process.getuid())
289
- throw new Error(`${entry} is not owned by this user`);
290
- if (platform !== 'win32' && (stat.mode & 0o022) !== 0)
291
- throw new Error(`${entry} is writable by group or others`);
292
- }
293
- /** True when the link resolves to `folder` (Windows reports junction targets in its own spelling). */
294
- function pointsAt(link, folder) {
295
- try {
296
- return fs.realpathSync(link) === fs.realpathSync(folder);
297
- }
298
- catch {
299
- return false;
300
- }
301
- }
302
191
  /** Why this home stays on shell registrations, or null when engine selection may run. */
303
192
  export function coreNextOffReason(home, env, platform) {
304
193
  if (platform === 'win32')
@@ -362,54 +251,6 @@ function isRunnableEngineFile(filePath, platform) {
362
251
  return null;
363
252
  }
364
253
  }
365
- /**
366
- * Read `<home>/.hq/bin/hq-hook/accepted-engine.json`; null when absent,
367
- * unreadable, invalid, or writable by another user.
368
- */
369
- export function readAcceptedHookEngineRecord(home, platform = process.platform) {
370
- const file = path.join(home, ACCEPTED_HOOK_ENGINE_RECORD);
371
- let text;
372
- try {
373
- checkOwnedEntry(file, 'file', platform);
374
- text = fs.readFileSync(file, 'utf8');
375
- }
376
- catch {
377
- return null;
378
- }
379
- let doc;
380
- try {
381
- doc = JSON.parse(text);
382
- }
383
- catch {
384
- return null;
385
- }
386
- const rec = doc;
387
- if (!rec || rec.schema !== ACCEPTED_HOOK_ENGINE_SCHEMA || !rec.engines || typeof rec.engines !== 'object' || Array.isArray(rec.engines)) {
388
- return null;
389
- }
390
- const engines = {};
391
- for (const [key, raw] of Object.entries(rec.engines)) {
392
- if (!raw || typeof raw !== 'object' || Array.isArray(raw))
393
- continue;
394
- const entry = raw;
395
- if (typeof entry.engineVersion !== 'string' || !entry.engineVersion)
396
- continue;
397
- if (typeof entry.pinnedEngine !== 'string' || !entry.pinnedEngine)
398
- continue;
399
- if (!Array.isArray(entry.hosts))
400
- continue;
401
- const hosts = entry.hosts.filter((h) => typeof h === 'string');
402
- if (hosts.length === 0)
403
- continue;
404
- engines[key] = {
405
- engineVersion: entry.engineVersion,
406
- pinnedEngine: entry.pinnedEngine,
407
- acceptedAt: typeof entry.acceptedAt === 'string' ? entry.acceptedAt : '',
408
- hosts,
409
- };
410
- }
411
- return { schema: ACCEPTED_HOOK_ENGINE_SCHEMA, engines };
412
- }
413
254
  /**
414
255
  * Record `pinned` (`engineVersion`) as canary-accepted for `hosts` after
415
256
  * committing `<home>/.hq/bin/hq-hook/stable`. Merges `hosts` when the same
@@ -510,16 +351,32 @@ export function existingAcceptedStableHookEngine(home, options) {
510
351
  * Windows), regardless of `core.next`. When the npm engine package is gone,
511
352
  * reuses an existing runnable `stable` or relinks `stable` from an already-
512
353
  * pinned copy of this CLI's version under `<home>/.hq/bin/hq-hook/<version>`.
513
- * Returns null when no usable engine exists on disk.
354
+ * When `stable` names a newer engine that still runs, it is kept and returned
355
+ * (the link never moves back on its own). Returns null when no usable engine
356
+ * exists on disk.
514
357
  */
515
358
  export function ensureStableHookEngine(home, options = {}) {
516
359
  const platform = options.platform ?? process.platform;
517
360
  const installed = installHookEngine(options.lookup ? { ...options.lookup, home, platform } : { home, platform });
518
- if (installed) {
519
- const stable = linkStableEngine(installed, home, platform);
520
- checkStableEngine(stable, installed);
521
- return stable;
522
- }
361
+ // `stable` naming a newer engine that still runs (this CLI was downgraded on
362
+ // purpose, or an older one is running): keep using that engine.
363
+ const linkOrKeepNewer = (engine) => {
364
+ try {
365
+ const stable = linkStableEngine(engine, home, platform);
366
+ checkStableEngine(stable, engine);
367
+ return stable;
368
+ }
369
+ catch (error) {
370
+ if (!(error instanceof StableDowngradeRefused))
371
+ throw error;
372
+ const existing = existingStableHookEngine(home, { platform });
373
+ if (!existing)
374
+ throw error;
375
+ return existing;
376
+ }
377
+ };
378
+ if (installed)
379
+ return linkOrKeepNewer(installed);
523
380
  const existing = existingStableHookEngine(home, { platform });
524
381
  if (existing)
525
382
  return existing;
@@ -531,9 +388,7 @@ export function ensureStableHookEngine(home, options = {}) {
531
388
  for (const candidate of engineBinaryCandidates(pinnedDir, platform)) {
532
389
  if (!isRunnableEngineFile(candidate, platform))
533
390
  continue;
534
- const stable = linkStableEngine(candidate, home, platform);
535
- checkStableEngine(stable, candidate);
536
- return stable;
391
+ return linkOrKeepNewer(candidate);
537
392
  }
538
393
  }
539
394
  return null;
@@ -637,20 +492,30 @@ export function coreNextTarget(home, env = process.env, options = {}) {
637
492
  probe: options.probe,
638
493
  deferLink: options.deferLink,
639
494
  });
640
- const target = coreNextTargetFor(home, env, stable, caps, platform, options.warn);
495
+ const passedCanaryHosts = new Set();
496
+ const target = coreNextTargetFor(home, env, stable, caps, platform, options.warn, (host) => {
497
+ passedCanaryHosts.add(host);
498
+ });
641
499
  if (!target)
642
500
  return null;
501
+ const recordAccepted = (acceptedHosts) => {
502
+ if (!acceptedHosts || acceptedHosts.length === 0 || canaryGateSkipped(env))
503
+ return;
504
+ const verified = acceptedHosts.filter((host) => CANARY_HOSTS.includes(host) && passedCanaryHosts.has(host));
505
+ if (verified.length === 0)
506
+ return;
507
+ recordAcceptedHookEngine(home, pinned, caps.version, verified, platform);
508
+ };
643
509
  coreNextCandidates.set(target, {
644
510
  pinnedEngine: pinned,
645
511
  engineVersion: caps.version,
512
+ recordAccepted,
646
513
  ...(options.deferLink
647
514
  ? {
648
515
  commitStable: (acceptedHosts) => {
649
516
  const linked = linkStableEngine(pinned, home, platform);
650
517
  checkStableEngine(linked, pinned);
651
- if (acceptedHosts && acceptedHosts.length > 0 && !canaryGateSkipped(env)) {
652
- recordAcceptedHookEngine(home, pinned, caps.version, acceptedHosts, platform);
653
- }
518
+ recordAccepted(acceptedHosts);
654
519
  return linked;
655
520
  },
656
521
  }
@@ -664,7 +529,7 @@ export function coreNextTarget(home, env = process.env, options = {}) {
664
529
  * capable event. [`coreNextTarget`] builds its target here, and `hq doctor`
665
530
  * uses it to ask the same questions without installing anything.
666
531
  */
667
- export function coreNextTargetFor(home, env, stable, caps, platform = process.platform, warn) {
532
+ export function coreNextTargetFor(home, env, stable, caps, platform = process.platform, warn, onCanaryPassed) {
668
533
  const events = capableEvents(caps);
669
534
  if (!events.length)
670
535
  return null;
@@ -673,7 +538,7 @@ export function coreNextTargetFor(home, env, stable, caps, platform = process.pl
673
538
  events,
674
539
  covers: caps.covers ?? {},
675
540
  ...(caps.registrationHosts ? { registrationHosts: caps.registrationHosts } : {}),
676
- canary: hostCanaryGate(home, env, caps.version, platform, warn),
541
+ canary: hostCanaryGate(home, env, caps.version, platform, warn, onCanaryPassed),
677
542
  }, env);
678
543
  }
679
544
  const warned = new Set();
@@ -721,7 +586,7 @@ function appendStrictCanaryLog(home, root, host, version, reason, strict) {
721
586
  // Best-effort engine log; the stderr error still surfaces the failure.
722
587
  }
723
588
  }
724
- function hostCanaryGate(home, env, version, platform, warn) {
589
+ function hostCanaryGate(home, env, version, platform, warn, onCanaryPassed) {
725
590
  const strictLogged = new Set();
726
591
  const logStrictOnce = (root, host, reason, strict) => {
727
592
  const key = `${root}\0${host}`;
@@ -752,6 +617,8 @@ function hostCanaryGate(home, env, version, platform, warn) {
752
617
  : `hq: warning: a reindex started by a hook keeps the old hook registrations on ${host}: it never changes a host's core. Run hq reindex to switch ${host} to the new core (hq-hook ${version})`, warn);
753
618
  }
754
619
  }
620
+ if (current && gate.allowed && !gate.skipped)
621
+ onCanaryPassed?.(host);
755
622
  return current;
756
623
  }
757
624
  if (gate.skipped) {
@@ -765,6 +632,9 @@ function hostCanaryGate(home, env, version, platform, warn) {
765
632
  }
766
633
  warnOnce(`hq: warning: flag ${CORE_NEXT_FLAG} is on, but ${host} has no passing host canary for hq-hook ${version} and this HQ root (${gate.reason}); keeping the old hook registrations on ${host}. Run scripts/host-canary.mjs --host ${host}, or bypass the gate with hq reindex --skip-host-canary-gate`, warn);
767
634
  }
635
+ else {
636
+ onCanaryPassed?.(host);
637
+ }
768
638
  return gate.allowed;
769
639
  };
770
640
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@indigoai-us/hq-cli",
3
- "version": "5.345.269",
3
+ "version": "5.345.270",
4
4
  "description": "HQ by Indigo management CLI — modules and cloud sync",
5
5
  "main": "dist/index.js",
6
6
  "bin": {
@@ -128,11 +128,11 @@
128
128
  "*.{ts,tsx,mts,cts,js,jsx,mjs,cjs}": "eslint --cache --no-error-on-unmatched-pattern"
129
129
  },
130
130
  "optionalDependencies": {
131
- "@indigoai-us/hq-hook-win32-x64": "5.345.269",
132
- "@indigoai-us/hq-hook-win32-arm64": "5.345.269",
133
- "@indigoai-us/hq-hook-darwin-x64": "5.345.269",
134
- "@indigoai-us/hq-hook-darwin-arm64": "5.345.269",
135
- "@indigoai-us/hq-hook-linux-x64": "5.345.269",
136
- "@indigoai-us/hq-hook-linux-arm64": "5.345.269"
131
+ "@indigoai-us/hq-hook-win32-x64": "5.345.270",
132
+ "@indigoai-us/hq-hook-win32-arm64": "5.345.270",
133
+ "@indigoai-us/hq-hook-darwin-x64": "5.345.270",
134
+ "@indigoai-us/hq-hook-darwin-arm64": "5.345.270",
135
+ "@indigoai-us/hq-hook-linux-x64": "5.345.270",
136
+ "@indigoai-us/hq-hook-linux-arm64": "5.345.270"
137
137
  }
138
138
  }