@indigoai-us/hq-cli 5.280.0 → 5.282.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.
Files changed (35) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/assets/scaffold/core/scripts/knowledge-prefs.sh +55 -0
  3. package/assets/scaffold/core/scripts/ontology-audience-grant.sh +81 -0
  4. package/assets/scaffold/core/scripts/ontology-brief.mjs +93 -0
  5. package/assets/scaffold/core/scripts/ontology-candidate.sh +100 -0
  6. package/assets/scaffold/core/scripts/ontology-garden.mjs +238 -0
  7. package/assets/scaffold/core/scripts/ontology-source-worklist.sh +79 -0
  8. package/assets/scaffold/core/scripts/source-yaml-validate.sh +36 -0
  9. package/dist/command-catalog.generated.d.ts +80 -2
  10. package/dist/command-catalog.generated.js +98 -2
  11. package/dist/commands/auth.js +40 -2
  12. package/dist/commands/core-ontology-native.d.ts +16 -0
  13. package/dist/commands/core-ontology-native.js +924 -0
  14. package/dist/commands/core-ontology.js +11 -0
  15. package/dist/commands/core.js +9 -0
  16. package/dist/commands/group-grants.js +3 -2
  17. package/dist/commands/lanes-message.js +1 -0
  18. package/dist/commands/lanes-move.js +2 -0
  19. package/dist/commands/lanes-resume.js +1 -0
  20. package/dist/commands/lanes-shared.d.ts +6 -0
  21. package/dist/commands/lanes.d.ts +1 -0
  22. package/dist/commands/lanes.js +283 -13
  23. package/dist/lib/lanes/keep-alive.js +1 -0
  24. package/dist/lib/lanes/outpost-dispatch.d.ts +3 -1
  25. package/dist/lib/lanes/outpost-dispatch.js +4 -0
  26. package/dist/lib/lanes/reconcile.d.ts +7 -1
  27. package/dist/lib/lanes/reconcile.js +88 -16
  28. package/dist/lib/lanes/spawn.js +5 -0
  29. package/dist/lib/lanes/store.js +38 -0
  30. package/dist/lib/lanes/throttle.d.ts +29 -2
  31. package/dist/lib/lanes/throttle.js +145 -5
  32. package/dist/lib/lanes/types.d.ts +12 -1
  33. package/dist/lib/lanes/types.js +3 -2
  34. package/dist/lib/lanes/watcher.js +1 -0
  35. package/package.json +1 -1
@@ -27,7 +27,7 @@ export interface OutpostDispatchResult {
27
27
  stderr: string;
28
28
  }
29
29
  export interface OutpostLaneCommand {
30
- action: "message" | "resume" | "stop" | "show" | "grant" | "grant-target" | "reparent-target" | "approve-target";
30
+ action: "message" | "resume" | "stop" | "show" | "logs" | "grant" | "grant-target" | "reparent-target" | "approve-target";
31
31
  laneId: string;
32
32
  hqBinary?: string;
33
33
  text?: string;
@@ -47,6 +47,8 @@ export interface OutpostLaneCommand {
47
47
  senior?: string;
48
48
  reason?: string;
49
49
  compact?: boolean;
50
+ tail?: string;
51
+ json?: boolean;
50
52
  }
51
53
  export declare function buildOutpostQuestionAnswerCommand(input: {
52
54
  questionId: string;
@@ -174,6 +174,10 @@ export function buildOutpostLaneCommand(input) {
174
174
  args.push("--keep-alive");
175
175
  if (input.compact === false)
176
176
  args.push("--no-compact");
177
+ if (input.action === "logs" && input.tail !== undefined)
178
+ args.push("--tail", input.tail);
179
+ if (input.action === "logs" && input.json === true)
180
+ args.push("--json");
177
181
  return withRemoteHqDir(joinCommandParts(args));
178
182
  }
179
183
  /** True when a remote CLI exited after rejecting the forwarded effort flag. */
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { type LivenessDeps } from "./liveness.js";
9
9
  import { type EnvelopeRepairSpawnInput, type SpawnLaneDeps } from "./spawn.js";
10
- import { type ThrottleDeps } from "./throttle.js";
10
+ import { type ThrottleDeps, type CpuBusyObservation } from "./throttle.js";
11
11
  import { readLanesRegistryFlag } from "./registry-flag.js";
12
12
  import { type RunawayProcessInfo } from "./runaway-tool-guard.js";
13
13
  import { type KeepAliveBlockedReason } from "./keep-alive.js";
@@ -78,6 +78,12 @@ export interface ReconcileDeps {
78
78
  readSessionSenior?: (sessionId: string) => SeniorRef;
79
79
  presenceConnected?: boolean;
80
80
  env?: NodeJS.ProcessEnv;
81
+ /** CPU sample taken before acquiring provider admission locks. */
82
+ cpuBusyFraction?: number | null;
83
+ /** CPU observation and cumulative counters taken before provider locks. */
84
+ cpuObservation?: CpuBusyObservation;
85
+ /** Skip admission this pass if candidates appeared after the pre-sample snapshot. */
86
+ skipAdmissionWithoutCpuSample?: boolean;
81
87
  /** Flag reader seam used to assert rollout reads happen before admission locks. */
82
88
  readRegistryFlag?: typeof readLanesRegistryFlag;
83
89
  /** Pre-read manager gate decisions, scoped to each lane for this pass. */
@@ -20,7 +20,7 @@ import { terminalChildResumeHireGrantError, withHireGrantAdmission, } from "./gr
20
20
  import { classifySessionLiveness, defaultLivenessDeps, loadLivenessIndex, readDaemonPresenceConnected, unindexedClaudeSessionLiveness, } from "./liveness.js";
21
21
  import { ANSWERED_QUESTIONS_FILE, armLaneWatcher, envelopeRepairMode, isPidRecordAlive, laneRunDir, readPidRecord, readProcStarttime, readSpawnManifest, markSpawnFailure, spawnEnvelopeRepair, spawnLaneWorker, retryLaneOnAlternateAccount, } from "./spawn.js";
22
22
  import { appendCappedHistory, lanesStoreRoot, parseLaneRecord, parseQuestionRecord, readLane, readQuestion, updateLane, updateQuestion, } from "./store.js";
23
- import { applyThrottleDecision, decideAdmission, defaultThrottleDeps, emptyThrottleState, evaluateThrottle, isCpuCheckEnabled, readAvailableMemMb, readThrottleState, resolveCpuBusyFraction, throttleStatePath, writeThrottleState, } from "./throttle.js";
23
+ import { applyThrottleDecision, decideAdmission, defaultThrottleDeps, emptyThrottleState, evaluateThrottle, isCpuCheckEnabled, readAvailableMemMb, readThrottleState, resolveCpuBusyFraction, sampleCpuBusyObservation, recordCpuBusyObservation, throttleStatePath, writeThrottleState, } from "./throttle.js";
24
24
  import { readLanesRegistryFlag } from "./registry-flag.js";
25
25
  import { resolveLaneCompany } from "./lane-company.js";
26
26
  import { appendDropboxMessage } from "./dropbox.js";
@@ -358,7 +358,7 @@ function casLaneIf(hqRoot, laneId, shouldWrite, updater, dryRun) {
358
358
  function writeQueueReason(hqRoot, lane, reason, dryRun) {
359
359
  if (lane.queued_reason === reason)
360
360
  return;
361
- casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "queued" && current.queued_reason !== reason, (current) => ({ ...current, queued_reason: reason, admission_reserved_at: undefined }), dryRun);
361
+ casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "queued" && current.queued_reason !== reason, (current) => ({ ...current, queued_reason: reason, queued_details: undefined, admission_reserved_at: undefined }), dryRun);
362
362
  }
363
363
  function availableBalancedAccount(hqRoot, lane, occupying, config, host, profileMaxConcurrent, nowMs) {
364
364
  if (!lane.account || !lane.admission_accounts?.includes(lane.account))
@@ -482,6 +482,7 @@ function expireAbandonedPickupValidation(hqRoot, lanes, summary, deps, now) {
482
482
  ...current,
483
483
  state: "failed",
484
484
  queued_reason: undefined,
485
+ queued_details: undefined,
485
486
  pickup_validation_pending: undefined,
486
487
  pickup_validation_pending_since: undefined,
487
488
  terminal_reason: PICKUP_VALIDATION_TIMEOUT_REASON,
@@ -998,14 +999,40 @@ export async function runPassBody(hqRoot, deps) {
998
999
  keepAliveFlagByCompany.set(key, keepAliveEnabled);
999
1000
  supersessionLineageAwareByCompany.set(key, lineageAware);
1000
1001
  }));
1002
+ const admissionThrottleDeps = startDeps.throttleDeps ?? defaultThrottleDeps(startDeps.env ?? process.env);
1003
+ const hasAdmissionCandidates = lanes.some((lane) => laneRunsHere(lane, startDeps.host) &&
1004
+ (lane.state === "queued" || lane.state === "awaiting_input")) || [...keepAliveFlagByCompany.values()].some(Boolean);
1005
+ const priorThrottleState = readThrottleState(throttleStatePath(hqRoot), admissionThrottleDeps);
1006
+ const priorThrottleSnapshot = priorThrottleState === "missing" || priorThrottleState === "unreadable"
1007
+ ? undefined
1008
+ : priorThrottleState.cpu_snapshot;
1009
+ const cpuObservation = await sampleCpuBusyObservation(admissionThrottleDeps, priorThrottleSnapshot, { measureBusy: hasAdmissionCandidates });
1010
+ const admissionDeps = {
1011
+ ...startDeps,
1012
+ cpuBusyFraction: cpuObservation.busy,
1013
+ cpuObservation,
1014
+ skipAdmissionWithoutCpuSample: !hasAdmissionCandidates,
1015
+ };
1001
1016
  await withAllLaneAdmissionLocks(hqRoot, async () => {
1002
1017
  const freshLanes = loadLaneRecords(hqRoot, summary);
1003
1018
  const freshQuestions = loadQuestionRecords(hqRoot, summary);
1004
- await resumeKeepAlive(hqRoot, freshLanes, freshQuestions, summary, startDeps, now, keepAliveFlagByCompany, supersessionLineageAwareByCompany);
1019
+ await resumeKeepAlive(hqRoot, freshLanes, freshQuestions, summary, admissionDeps, now, keepAliveFlagByCompany, supersessionLineageAwareByCompany);
1005
1020
  const drainLanes = loadLaneRecords(hqRoot, summary);
1006
- await admitQueued(hqRoot, drainLanes, summary, startDeps, now);
1007
- await resumeAwaiting(hqRoot, drainLanes, freshQuestions, summary, startDeps);
1021
+ await admitQueued(hqRoot, drainLanes, summary, admissionDeps, now);
1022
+ await resumeAwaiting(hqRoot, drainLanes, freshQuestions, summary, admissionDeps);
1008
1023
  });
1024
+ if (!deps.dryRun && cpuObservation.snapshot !== undefined) {
1025
+ const latestThrottleState = readThrottleState(throttleStatePath(hqRoot), admissionThrottleDeps);
1026
+ if (latestThrottleState === "unreadable") {
1027
+ markError(summary, new LanesError("throttle_state_unreadable", "Throttle state became unreadable while reconciling"));
1028
+ }
1029
+ else {
1030
+ const currentThrottleState = latestThrottleState === "missing"
1031
+ ? emptyThrottleState()
1032
+ : latestThrottleState;
1033
+ writeThrottleState(throttleStatePath(hqRoot), recordCpuBusyObservation(currentThrottleState, cpuObservation), admissionThrottleDeps);
1034
+ }
1035
+ }
1009
1036
  if (!deps.dryRun && summary.errors.length === 0) {
1010
1037
  try {
1011
1038
  writeHeartbeatFile(hqRoot, summary, nowIso);
@@ -1246,6 +1273,7 @@ async function heartbeatAndDeadHosts(hqRoot, lanes, summary, deps, now) {
1246
1273
  ...current,
1247
1274
  state: terminalClassification.state,
1248
1275
  queued_reason: undefined,
1276
+ queued_details: undefined,
1249
1277
  terminal_reason: terminalClassification.reason ?? "host_lost",
1250
1278
  interrupted_at: terminalClassification.state === "interrupted" ? nowIso : undefined,
1251
1279
  ...(providerRejection
@@ -1390,6 +1418,7 @@ async function heartbeatAndDeadHosts(hqRoot, lanes, summary, deps, now) {
1390
1418
  ...current,
1391
1419
  state: "interrupted",
1392
1420
  queued_reason: undefined,
1421
+ queued_details: undefined,
1393
1422
  terminal_reason: "host_lost",
1394
1423
  interrupted_at: nowIso,
1395
1424
  }), deps.dryRun);
@@ -1448,7 +1477,7 @@ async function admitQueued(hqRoot, lanes, summary, deps, now) {
1448
1477
  laneRunsHere(lane, deps.host))
1449
1478
  .filter((lane) => !hasFreshAdmissionReservation(lane, now.getTime()))
1450
1479
  .sort(compareLaneQueueOrder);
1451
- if (localQueued.length === 0)
1480
+ if (localQueued.length === 0 || deps.skipAdmissionWithoutCpuSample === true)
1452
1481
  return;
1453
1482
  const config = loadLanesConfig(hqRoot);
1454
1483
  const throttleDeps = deps.throttleDeps ?? defaultThrottleDeps(deps.env ?? process.env);
@@ -1459,7 +1488,9 @@ async function admitQueued(hqRoot, lanes, summary, deps, now) {
1459
1488
  : loaded === "missing"
1460
1489
  ? emptyThrottleState()
1461
1490
  : loaded;
1462
- const busy = await resolveCpuBusyFraction(throttleDeps);
1491
+ const busy = deps.cpuBusyFraction !== undefined
1492
+ ? deps.cpuBusyFraction
1493
+ : await resolveCpuBusyFraction(throttleDeps);
1463
1494
  const availableMemMb = readAvailableMemMb(throttleDeps);
1464
1495
  const nowMs = now.getTime();
1465
1496
  const decision = evaluateThrottle({
@@ -1476,7 +1507,6 @@ async function admitQueued(hqRoot, lanes, summary, deps, now) {
1476
1507
  hasFreshAdmissionReservation(lane, now.getTime()) ||
1477
1508
  hasActivePendingResume(lane, now.getTime())) &&
1478
1509
  laneRunsHere(lane, deps.host));
1479
- const starvation = decision.admit && decision.reason === "starvation_bound";
1480
1510
  const spawned = [];
1481
1511
  if (!decision.admit) {
1482
1512
  for (const lane of localQueued) {
@@ -1543,8 +1573,6 @@ async function admitQueued(hqRoot, lanes, summary, deps, now) {
1543
1573
  reason: decision.reason,
1544
1574
  });
1545
1575
  spawned.push({ ...admittedLane, state: "running" });
1546
- if (starvation)
1547
- break;
1548
1576
  continue;
1549
1577
  }
1550
1578
  const started = spawnFn(hqRoot, admittedLane);
@@ -1564,10 +1592,38 @@ async function admitQueued(hqRoot, lanes, summary, deps, now) {
1564
1592
  : {}),
1565
1593
  });
1566
1594
  spawned.push(started);
1567
- if (starvation)
1568
- break;
1569
1595
  }
1570
1596
  catch (err) {
1597
+ if (err instanceof LanesError && err.error === "worker_profile_hash_mismatch") {
1598
+ const expectedHash = typeof err.details.expected_hash === "string"
1599
+ ? err.details.expected_hash
1600
+ : null;
1601
+ const actualHash = typeof err.details.actual_hash === "string"
1602
+ ? err.details.actual_hash
1603
+ : undefined;
1604
+ const workerProfile = typeof err.details.worker === "string"
1605
+ ? err.details.worker
1606
+ : lane.worker_profile;
1607
+ if (actualHash !== undefined && workerProfile !== undefined) {
1608
+ casLaneIf(hqRoot, lane.lane_id, (current) => current.state === "queued", (current) => ({
1609
+ ...current,
1610
+ queued_reason: "worker_profile_changed",
1611
+ queued_details: {
1612
+ reason: "worker_profile_changed",
1613
+ expected_hash: expectedHash,
1614
+ actual_hash: actualHash,
1615
+ recovery_command: `hq lanes resume ${lane.lane_id} --worker ${workerProfile}`,
1616
+ },
1617
+ admission_reserved_at: undefined,
1618
+ }), false);
1619
+ summary.skipped.push({
1620
+ kind: "lane",
1621
+ id: lane.lane_id,
1622
+ why: "worker_profile_changed",
1623
+ });
1624
+ continue;
1625
+ }
1626
+ }
1571
1627
  terminalizeModelUnsupported(hqRoot, lane, err);
1572
1628
  markDirty(summary, {
1573
1629
  kind: "lane",
@@ -2273,6 +2329,7 @@ async function resumeKeepAlive(hqRoot, lanes, questions, summary, deps, now, fla
2273
2329
  ...latestLane,
2274
2330
  state: "queued",
2275
2331
  queued_reason: undefined,
2332
+ queued_details: undefined,
2276
2333
  queued_at: nowIso,
2277
2334
  admission_reserved_at: undefined,
2278
2335
  terminal_reason: undefined,
@@ -2348,7 +2405,7 @@ async function resumeAwaiting(hqRoot, lanes, questions, summary, deps, onlyLaneI
2348
2405
  const awaiting = lanes.filter((lane) => lane.state === "awaiting_input" &&
2349
2406
  laneRunsHere(lane, deps.host) &&
2350
2407
  (onlyLaneIds === undefined || onlyLaneIds.has(lane.lane_id)));
2351
- if (awaiting.length === 0)
2408
+ if (awaiting.length === 0 || deps.skipAdmissionWithoutCpuSample === true)
2352
2409
  return;
2353
2410
  const throttleDeps = deps.throttleDeps ?? defaultThrottleDeps(deps.env ?? process.env);
2354
2411
  let throttleDecision;
@@ -2356,7 +2413,9 @@ async function resumeAwaiting(hqRoot, lanes, questions, summary, deps, onlyLaneI
2356
2413
  const loaded = readThrottleState(throttleStatePath(hqRoot), throttleDeps);
2357
2414
  const state = loaded === "missing" ? emptyThrottleState() : loaded;
2358
2415
  throttleDecision = evaluateThrottle({
2359
- busy: await resolveCpuBusyFraction(throttleDeps),
2416
+ busy: deps.cpuBusyFraction !== undefined
2417
+ ? deps.cpuBusyFraction
2418
+ : deps.cpuObservation?.busy ?? await resolveCpuBusyFraction(throttleDeps),
2360
2419
  availableMemMb: readAvailableMemMb(throttleDeps),
2361
2420
  cpuCheckEnabled: isCpuCheckEnabled(throttleDeps.env),
2362
2421
  state,
@@ -2365,7 +2424,7 @@ async function resumeAwaiting(hqRoot, lanes, questions, summary, deps, onlyLaneI
2365
2424
  });
2366
2425
  }
2367
2426
  else {
2368
- const throttle = await decideAdmission(hqRoot, config.throttle, throttleDeps);
2427
+ const throttle = await decideAdmission(hqRoot, config.throttle, throttleDeps, deps.cpuBusyFraction, deps.cpuObservation);
2369
2428
  throttleDecision = throttle.decision;
2370
2429
  }
2371
2430
  const spawnFn = deps.spawn ?? ((root, lane) => spawnLaneWorker(root, lane, spawnDepsForHost(deps, lane)));
@@ -2444,6 +2503,7 @@ async function resumeAwaiting(hqRoot, lanes, questions, summary, deps, onlyLaneI
2444
2503
  ...latest,
2445
2504
  state: "queued",
2446
2505
  queued_reason: queueReason,
2506
+ queued_details: undefined,
2447
2507
  queued_at: answeredAt,
2448
2508
  pending_resume: {
2449
2509
  ...(latest.provider_session_id
@@ -2503,14 +2563,26 @@ export async function runReconcilePass(hqRoot, deps) {
2503
2563
  /** Run the reconcile answer-resume path for one lane before questions answer returns. */
2504
2564
  export async function wakeAnsweredLaneNow(hqRoot, laneId, deps) {
2505
2565
  const summary = emptySummary(deps.dryRun);
2566
+ const throttleDeps = deps.throttleDeps ?? defaultThrottleDeps(deps.env ?? process.env);
2506
2567
  await withReconcileLock(hqRoot, async () => {
2568
+ const previousState = readThrottleState(throttleStatePath(hqRoot), throttleDeps);
2569
+ const previousSnapshot = previousState === "missing" || previousState === "unreadable"
2570
+ ? undefined
2571
+ : previousState.cpu_snapshot;
2572
+ const cpuObservation = await sampleCpuBusyObservation(throttleDeps, previousSnapshot);
2573
+ const admissionDeps = {
2574
+ ...deps,
2575
+ cpuBusyFraction: cpuObservation.busy,
2576
+ cpuObservation,
2577
+ skipAdmissionWithoutCpuSample: false,
2578
+ };
2507
2579
  await withAllLaneAdmissionLocks(hqRoot, async () => {
2508
2580
  const lanes = loadLaneRecords(hqRoot, summary);
2509
2581
  const questions = loadQuestionRecords(hqRoot, summary);
2510
2582
  if (!lanes.some((lane) => lane.lane_id === laneId)) {
2511
2583
  throw new LanesError("lane_not_found", `Lane ${laneId} was not found`, { lane_id: laneId });
2512
2584
  }
2513
- await resumeAwaiting(hqRoot, lanes, questions, summary, deps, new Set([laneId]));
2585
+ await resumeAwaiting(hqRoot, lanes, questions, summary, admissionDeps, new Set([laneId]));
2514
2586
  });
2515
2587
  });
2516
2588
  const latest = readLane(hqRoot, laneId);
@@ -476,6 +476,7 @@ export async function retryLaneOnAlternateAccount(hqRoot, lane, reason, writer =
476
476
  ...row,
477
477
  state: "queued",
478
478
  queued_reason: "account_retry",
479
+ queued_details: undefined,
479
480
  queued_at: at,
480
481
  admission_reserved_at: undefined,
481
482
  terminal_reason: undefined,
@@ -1360,6 +1361,7 @@ export function markSpawnFailure(hqRoot, laneId, reasonRaw, details) {
1360
1361
  ...current,
1361
1362
  state: "failed",
1362
1363
  queued_reason: undefined,
1364
+ queued_details: undefined,
1363
1365
  queued_at: undefined,
1364
1366
  admission_reserved_at: undefined,
1365
1367
  terminal_reason: reason,
@@ -1425,6 +1427,7 @@ function markOutpostSelfTarget(hqRoot, laneId, details) {
1425
1427
  ...current,
1426
1428
  state: "cancelled",
1427
1429
  queued_reason: undefined,
1430
+ queued_details: undefined,
1428
1431
  terminal_reason: "outpost_self_target",
1429
1432
  terminal_details: details,
1430
1433
  };
@@ -1948,6 +1951,7 @@ export function spawnLaneWorker(hqRoot, lane, deps = {}, launch = {}) {
1948
1951
  ...current,
1949
1952
  state: "queued",
1950
1953
  queued_reason: "provider_binary_missing",
1954
+ queued_details: undefined,
1951
1955
  terminal_reason: undefined,
1952
1956
  terminal_details: {
1953
1957
  provider: lane.provider,
@@ -2024,6 +2028,7 @@ export function spawnLaneWorker(hqRoot, lane, deps = {}, launch = {}) {
2024
2028
  ...current,
2025
2029
  state: "running",
2026
2030
  queued_reason: undefined,
2031
+ queued_details: undefined,
2027
2032
  queued_at: undefined,
2028
2033
  admission_reserved_at: undefined,
2029
2034
  terminal_reason: undefined,
@@ -416,6 +416,17 @@ export function parseLaneRecord(raw, filePath) {
416
416
  requireInt(raw.version, "version");
417
417
  requireInt(raw.depth, "depth");
418
418
  requireInt(raw.priority, "priority");
419
+ if (raw.ended_at !== undefined) {
420
+ requireTimestamp(raw.ended_at, "ended_at", filePath);
421
+ }
422
+ if (raw.envelope_summary !== undefined &&
423
+ (typeof raw.envelope_summary !== "string" || raw.envelope_summary.trim() === "")) {
424
+ throw new LaneSchemaError("envelope_summary must be a non-empty string when present", {
425
+ path: filePath,
426
+ field: "envelope_summary",
427
+ value: raw.envelope_summary,
428
+ });
429
+ }
419
430
  if (raw.hire_grant !== undefined &&
420
431
  (typeof raw.hire_grant !== "number" || !Number.isSafeInteger(raw.hire_grant) || raw.hire_grant < 0)) {
421
432
  throw new LaneSchemaError("Expected non-negative safe integer for hire_grant", {
@@ -517,6 +528,33 @@ export function parseLaneRecord(raw, filePath) {
517
528
  value: raw.queued_reason,
518
529
  });
519
530
  }
531
+ if (raw.queued_details !== undefined) {
532
+ const details = raw.queued_details;
533
+ if (details === null || typeof details !== "object" || Array.isArray(details) ||
534
+ typeof details.reason !== "string") {
535
+ throw new LaneSchemaError("queued_details must describe a worker_profile_changed queue reason", {
536
+ path: filePath,
537
+ field: "queued_details",
538
+ });
539
+ }
540
+ const detailsRecord = details;
541
+ if (detailsRecord.reason !== "worker_profile_changed" ||
542
+ (detailsRecord.expected_hash !== null && typeof detailsRecord.expected_hash !== "string") ||
543
+ typeof detailsRecord.actual_hash !== "string" || detailsRecord.actual_hash === "" ||
544
+ typeof detailsRecord.recovery_command !== "string" || detailsRecord.recovery_command.trim() === "") {
545
+ throw new LaneSchemaError("queued_details must describe a worker_profile_changed queue reason", {
546
+ path: filePath,
547
+ field: "queued_details",
548
+ });
549
+ }
550
+ if (raw.queued_reason !== "worker_profile_changed") {
551
+ throw new LaneSchemaError("queued_details requires queued_reason worker_profile_changed", {
552
+ path: filePath,
553
+ field: "queued_details",
554
+ queued_reason: raw.queued_reason,
555
+ });
556
+ }
557
+ }
520
558
  if (raw.queued_at !== undefined)
521
559
  requireTimestamp(raw.queued_at, "queued_at", filePath);
522
560
  if (raw.admission_reserved_at !== undefined) {
@@ -12,7 +12,8 @@
12
12
  import * as fs from "node:fs";
13
13
  import * as os from "node:os";
14
14
  import type { ThrottleConfig } from "./types.js";
15
- export declare const CPU_SAMPLE_MS = 200;
15
+ export declare const CPU_SAMPLE_MS = 5000;
16
+ export declare const CPU_CUMULATIVE_MAX_AGE_MS: number;
16
17
  export declare const STARVATION_MAX_DEFERRALS = 10;
17
18
  export declare const STARVATION_MAX_MS: number;
18
19
  export declare const PROC_MEMINFO = "/proc/meminfo";
@@ -39,12 +40,34 @@ export interface ThrottleDeps {
39
40
  renameSync: typeof fs.renameSync;
40
41
  unlinkSync: typeof fs.unlinkSync;
41
42
  }
43
+ export type CpuBusySource = "interval" | "window" | "override";
44
+ export interface CpuCumulativeSnapshot {
45
+ idle: number;
46
+ total: number;
47
+ sampled_at: string;
48
+ }
49
+ export interface CpuBusyObservation {
50
+ busy: number | null;
51
+ source: CpuBusySource | undefined;
52
+ snapshot: CpuCumulativeSnapshot | undefined;
53
+ }
42
54
  export declare function defaultThrottleDeps(env?: NodeJS.ProcessEnv): ThrottleDeps;
43
55
  export declare function cpuTimesSnapshot(cpus: os.CpuInfo[]): CpuTimes;
44
56
  export declare function busyFractionFromDeltas(before: CpuTimes, after: CpuTimes): number | null;
45
57
  export declare function sampleCpuBusyFraction(deps?: ThrottleDeps, sampleMs?: number): Promise<number | null>;
46
58
  export declare function isCpuCheckEnabled(env?: NodeJS.ProcessEnv): boolean;
47
59
  export declare function resolveCpuBusyFraction(deps?: ThrottleDeps): Promise<number | null>;
60
+ /**
61
+ * Measure CPU busy against the previous reconcile snapshot when it is fresh.
62
+ * A short stable window is used only to establish a baseline or recover from
63
+ * stale/backwards counters (for example, after a reboot).
64
+ */
65
+ export declare function sampleCpuBusyObservation(deps: ThrottleDeps, previous: CpuCumulativeSnapshot | undefined, options?: {
66
+ measureBusy?: boolean;
67
+ }): Promise<CpuBusyObservation>;
68
+ export declare function recordCpuBusyObservation(state: ThrottleState, observation: CpuBusyObservation, options?: {
69
+ advanceSnapshot?: boolean;
70
+ }): ThrottleState;
48
71
  /** Parse MemAvailable kB from /proc/meminfo. */
49
72
  export declare function parseMemAvailableKb(contents: string): number | null;
50
73
  /**
@@ -56,6 +79,10 @@ export interface ThrottleState {
56
79
  consecutiveDeferrals: number;
57
80
  lastAdmissionAt: string | null;
58
81
  cpuPaused: boolean;
82
+ cpu_snapshot?: CpuCumulativeSnapshot;
83
+ last_busy?: number | null;
84
+ busy_source?: CpuBusySource;
85
+ sampled_at?: string;
59
86
  }
60
87
  export type ThrottleAdmitReason = "ok" | "starvation_bound" | "unreadable" | "cpu_check_disabled";
61
88
  export type ThrottleDeferReason = "cpu" | "memory";
@@ -82,7 +109,7 @@ export declare function applyThrottleDecision(state: ThrottleState, decision: Th
82
109
  export declare function throttleStatePath(hqRoot: string): string;
83
110
  export declare function readThrottleState(filePath: string, deps?: ThrottleDeps): ThrottleState | "unreadable" | "missing";
84
111
  export declare function writeThrottleState(filePath: string, state: ThrottleState, deps?: ThrottleDeps): void;
85
- export declare function decideAdmission(hqRoot: string, throttle?: ThrottleConfig, deps?: ThrottleDeps): Promise<{
112
+ export declare function decideAdmission(hqRoot: string, throttle?: ThrottleConfig, deps?: ThrottleDeps, cpuBusyFraction?: number | null, cpuObservation?: CpuBusyObservation): Promise<{
86
113
  decision: ThrottleDecision;
87
114
  state: ThrottleState;
88
115
  }>;
@@ -13,7 +13,10 @@ import * as fs from "node:fs";
13
13
  import * as os from "node:os";
14
14
  import * as path from "node:path";
15
15
  import { DEFAULT_CPU_BUSY_PAUSE, DEFAULT_CPU_BUSY_RESUME, DEFAULT_MIN_AVAILABLE_MEM_MB, } from "./config.js";
16
- export const CPU_SAMPLE_MS = 200;
16
+ // Reconcile normally measures the interval between cumulative CPU snapshots.
17
+ // A five-second window establishes a baseline or recovers from stale counters.
18
+ export const CPU_SAMPLE_MS = 5_000;
19
+ export const CPU_CUMULATIVE_MAX_AGE_MS = 5 * 60 * 1000;
17
20
  export const STARVATION_MAX_DEFERRALS = 10;
18
21
  export const STARVATION_MAX_MS = 30 * 60 * 1000;
19
22
  export const PROC_MEMINFO = "/proc/meminfo";
@@ -83,6 +86,93 @@ export async function resolveCpuBusyFraction(deps = defaultThrottleDeps()) {
83
86
  }
84
87
  return sampleCpuBusyFraction(deps);
85
88
  }
89
+ function snapshotFromDeps(deps) {
90
+ return {
91
+ ...cpuTimesSnapshot(deps.cpus()),
92
+ sampled_at: new Date(deps.now()).toISOString(),
93
+ };
94
+ }
95
+ /**
96
+ * Measure CPU busy against the previous reconcile snapshot when it is fresh.
97
+ * A short stable window is used only to establish a baseline or recover from
98
+ * stale/backwards counters (for example, after a reboot).
99
+ */
100
+ export async function sampleCpuBusyObservation(deps, previous, options = {}) {
101
+ let start;
102
+ try {
103
+ start = snapshotFromDeps(deps);
104
+ }
105
+ catch (err) {
106
+ process.stderr.write(`lanes throttle: CPU counters failed: ${err instanceof Error ? err.message : String(err)}\n`);
107
+ return { busy: null, source: undefined, snapshot: previous };
108
+ }
109
+ if (options.measureBusy === false || !isCpuCheckEnabled(deps.env)) {
110
+ return { busy: null, source: undefined, snapshot: start };
111
+ }
112
+ const rawOverride = deps.env[LANES_CPU_BUSY_OVERRIDE];
113
+ if (rawOverride !== undefined && rawOverride !== "") {
114
+ const override = Number(rawOverride);
115
+ if (Number.isFinite(override)) {
116
+ return {
117
+ busy: Math.max(0, Math.min(1, override)),
118
+ source: "override",
119
+ snapshot: start,
120
+ };
121
+ }
122
+ process.stderr.write(`lanes throttle: ${LANES_CPU_BUSY_OVERRIDE} is not a finite number (${JSON.stringify(rawOverride)}); sampling instead\n`);
123
+ }
124
+ const nowMs = Date.parse(start.sampled_at);
125
+ const previousAtMs = previous ? Date.parse(previous.sampled_at) : Number.NaN;
126
+ if (previous !== undefined &&
127
+ Number.isFinite(previousAtMs) &&
128
+ nowMs >= previousAtMs &&
129
+ nowMs - previousAtMs <= CPU_CUMULATIVE_MAX_AGE_MS &&
130
+ start.idle >= previous.idle &&
131
+ start.total >= previous.total) {
132
+ return {
133
+ busy: busyFractionFromDeltas(previous, start),
134
+ source: "interval",
135
+ snapshot: start,
136
+ };
137
+ }
138
+ try {
139
+ await deps.sleep(CPU_SAMPLE_MS);
140
+ const end = snapshotFromDeps(deps);
141
+ return {
142
+ busy: busyFractionFromDeltas(start, end),
143
+ source: "window",
144
+ snapshot: end,
145
+ };
146
+ }
147
+ catch (err) {
148
+ process.stderr.write(`lanes throttle: CPU window sample failed: ${err instanceof Error ? err.message : String(err)}\n`);
149
+ return { busy: null, source: "window", snapshot: start };
150
+ }
151
+ }
152
+ export function recordCpuBusyObservation(state, observation, options = {}) {
153
+ const observationAt = observation.snapshot === undefined
154
+ ? Number.NaN
155
+ : Date.parse(observation.snapshot.sampled_at);
156
+ const currentSnapshotAt = state.cpu_snapshot === undefined
157
+ ? Number.NaN
158
+ : Date.parse(state.cpu_snapshot.sampled_at);
159
+ const snapshotIsCurrent = options.advanceSnapshot !== false && observation.snapshot !== undefined && (!Number.isFinite(currentSnapshotAt) ||
160
+ observationAt > currentSnapshotAt ||
161
+ (observationAt === currentSnapshotAt && observation.snapshot.total >= (state.cpu_snapshot?.total ?? 0)));
162
+ const currentMetricAt = state.sampled_at === undefined ? Number.NaN : Date.parse(state.sampled_at);
163
+ const metricIsCurrent = observation.source !== undefined && Number.isFinite(observationAt) && (!Number.isFinite(currentMetricAt) || observationAt >= currentMetricAt);
164
+ return {
165
+ ...state,
166
+ ...(snapshotIsCurrent ? { cpu_snapshot: observation.snapshot } : {}),
167
+ ...(metricIsCurrent
168
+ ? {
169
+ last_busy: observation.busy,
170
+ busy_source: observation.source,
171
+ sampled_at: observation.snapshot.sampled_at,
172
+ }
173
+ : {}),
174
+ };
175
+ }
86
176
  /** Parse MemAvailable kB from /proc/meminfo. */
87
177
  export function parseMemAvailableKb(contents) {
88
178
  for (const line of contents.split("\n")) {
@@ -184,12 +274,14 @@ export function evaluateThrottle(input) {
184
274
  export function applyThrottleDecision(state, decision, nowMs) {
185
275
  if (decision.admit) {
186
276
  return {
277
+ ...state,
187
278
  consecutiveDeferrals: 0,
188
279
  lastAdmissionAt: new Date(nowMs).toISOString(),
189
280
  cpuPaused: false,
190
281
  };
191
282
  }
192
283
  return {
284
+ ...state,
193
285
  consecutiveDeferrals: state.consecutiveDeferrals + 1,
194
286
  lastAdmissionAt: state.lastAdmissionAt,
195
287
  cpuPaused: decision.reason === "cpu" ? true : state.cpuPaused,
@@ -231,10 +323,47 @@ export function readThrottleState(filePath, deps = defaultThrottleDeps()) {
231
323
  process.stderr.write(`lanes throttle: state ${filePath} has invalid cpuPaused\n`);
232
324
  return "unreadable";
233
325
  }
326
+ const optionalTimestamp = (value) => typeof value === "string" && value.trim() !== "" && Number.isFinite(Date.parse(value));
327
+ let cpuSnapshot;
328
+ if (obj.cpu_snapshot !== undefined) {
329
+ const rawSnapshot = obj.cpu_snapshot;
330
+ if (rawSnapshot === null || typeof rawSnapshot !== "object" || Array.isArray(rawSnapshot)) {
331
+ process.stderr.write(`lanes throttle: state ${filePath} has invalid cpu_snapshot\n`);
332
+ return "unreadable";
333
+ }
334
+ const snapshot = rawSnapshot;
335
+ if (typeof snapshot.idle !== "number" || !Number.isFinite(snapshot.idle) || snapshot.idle < 0 ||
336
+ typeof snapshot.total !== "number" || !Number.isFinite(snapshot.total) || snapshot.total < 0 ||
337
+ !optionalTimestamp(snapshot.sampled_at)) {
338
+ process.stderr.write(`lanes throttle: state ${filePath} has invalid cpu_snapshot fields\n`);
339
+ return "unreadable";
340
+ }
341
+ cpuSnapshot = {
342
+ idle: snapshot.idle,
343
+ total: snapshot.total,
344
+ sampled_at: snapshot.sampled_at,
345
+ };
346
+ }
347
+ if (obj.last_busy !== undefined && obj.last_busy !== null && (typeof obj.last_busy !== "number" || !Number.isFinite(obj.last_busy) || obj.last_busy < 0 || obj.last_busy > 1)) {
348
+ process.stderr.write(`lanes throttle: state ${filePath} has invalid last_busy\n`);
349
+ return "unreadable";
350
+ }
351
+ if (obj.busy_source !== undefined && !["interval", "window", "override"].includes(String(obj.busy_source))) {
352
+ process.stderr.write(`lanes throttle: state ${filePath} has invalid busy_source\n`);
353
+ return "unreadable";
354
+ }
355
+ if (obj.sampled_at !== undefined && !optionalTimestamp(obj.sampled_at)) {
356
+ process.stderr.write(`lanes throttle: state ${filePath} has invalid sampled_at\n`);
357
+ return "unreadable";
358
+ }
234
359
  return {
235
360
  consecutiveDeferrals: obj.consecutiveDeferrals,
236
361
  lastAdmissionAt: obj.lastAdmissionAt,
237
362
  cpuPaused: obj.cpuPaused,
363
+ ...(cpuSnapshot !== undefined ? { cpu_snapshot: cpuSnapshot } : {}),
364
+ ...(obj.last_busy !== undefined ? { last_busy: obj.last_busy } : {}),
365
+ ...(obj.busy_source !== undefined ? { busy_source: obj.busy_source } : {}),
366
+ ...(obj.sampled_at !== undefined ? { sampled_at: obj.sampled_at } : {}),
238
367
  };
239
368
  }
240
369
  catch (err) {
@@ -269,7 +398,7 @@ export async function decideAdmission(hqRoot, throttle = {
269
398
  cpu_busy_pause: DEFAULT_CPU_BUSY_PAUSE,
270
399
  cpu_busy_resume: DEFAULT_CPU_BUSY_RESUME,
271
400
  min_available_mem_mb: DEFAULT_MIN_AVAILABLE_MEM_MB,
272
- }, deps = defaultThrottleDeps()) {
401
+ }, deps = defaultThrottleDeps(), cpuBusyFraction, cpuObservation) {
273
402
  const filePath = throttleStatePath(hqRoot);
274
403
  const loaded = readThrottleState(filePath, deps);
275
404
  if (loaded === "unreadable") {
@@ -279,18 +408,29 @@ export async function decideAdmission(hqRoot, throttle = {
279
408
  };
280
409
  }
281
410
  const state = loaded === "missing" ? emptyThrottleState() : loaded;
282
- const busy = await resolveCpuBusyFraction(deps);
411
+ const cpuCheckEnabled = isCpuCheckEnabled(deps.env);
412
+ const observation = cpuObservation ?? (cpuBusyFraction === undefined && cpuCheckEnabled
413
+ ? await sampleCpuBusyObservation(deps, state.cpu_snapshot)
414
+ : undefined);
415
+ const busy = cpuBusyFraction === undefined
416
+ ? observation?.busy ?? null
417
+ : cpuBusyFraction;
283
418
  const availableMemMb = readAvailableMemMb(deps);
284
419
  const nowMs = deps.now();
285
420
  const decision = evaluateThrottle({
286
421
  busy,
287
422
  availableMemMb,
288
- cpuCheckEnabled: isCpuCheckEnabled(deps.env),
423
+ cpuCheckEnabled,
289
424
  state,
290
425
  throttle,
291
426
  nowMs,
292
427
  });
293
- const next = applyThrottleDecision(state, decision, nowMs);
428
+ const observedState = observation
429
+ ? recordCpuBusyObservation(state, observation, {
430
+ advanceSnapshot: cpuObservation !== undefined,
431
+ })
432
+ : state;
433
+ const next = applyThrottleDecision(observedState, decision, nowMs);
294
434
  writeThrottleState(filePath, next, deps);
295
435
  return { decision, state: next };
296
436
  }