@indigoai-us/hq-cli 5.277.0 → 5.278.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 CHANGED
@@ -2,6 +2,29 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [5.278.0] — 2026-09-29
6
+
7
+ ### Changed
8
+
9
+ - The CI and no-hermes pull request checks use read-only repository access, and CI pins its third-party actions to fixed commits. The fast-route benchmark workflow is pinned in a follow-up.
10
+
11
+ ### Added
12
+
13
+ - Reconcile can stop an over-budget same-user search process descended from a
14
+ running lane when `rg`, `grep`, `ugrep`, or `ag` disables ignores on a broad
15
+ HQ root, or `find` searches a broad root without a shallow-depth or prune
16
+ exemption. The `lanes.runaway-tool-guard` hq-flags key is off by default. A
17
+ stopped search records its command and duration, then gets a notice to narrow
18
+ the search.
19
+
20
+ - Add the default-off `checkpoint.ontology.hub-mention-floor` hq-flags key.
21
+ When it is on, checkpoint ontology apply rejects a knowledge link to a
22
+ non-person entity that 50 or more other entity files link to when the input
23
+ names it fewer than 2 times in the text the model read (the chunk's line
24
+ range of the annotation-stripped copy when there is one). Concept links
25
+ redirected to a project are checked the same way. The rejection is reported
26
+ as `hub_under_mentioned`.
27
+
5
28
  ## [5.277.0] — 2026-09-29
6
29
 
7
30
  ### Fixed
@@ -30,6 +30,10 @@ export const MIN_CREATE_CONFIDENCE = 0.7;
30
30
  const DOMINANT_SIBLING_RATIO = 3;
31
31
  /** Inbound links the dominant sibling must have before a redirect. */
32
32
  const DOMINANT_SIBLING_MIN_INBOUND = 5;
33
+ /** checkpoint.ontology.hub-mention-floor: other entity files that must link here before the floor applies. */
34
+ const HUB_MIN_INBOUND = 50;
35
+ /** checkpoint.ontology.hub-mention-floor: whole-word canonical-name mentions required to keep the link. */
36
+ const HUB_MIN_MENTIONS = 2;
33
37
  export class ApplyError extends Error {
34
38
  constructor(message) {
35
39
  super(message);
@@ -67,6 +71,16 @@ export function parseCandidate(raw) {
67
71
  evidence: typeof c.evidence === "string" ? c.evidence : "",
68
72
  };
69
73
  }
74
+ function escapeRegExp(name) {
75
+ return name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
76
+ }
77
+ /** Whole-word, any-case matches of `name` in `text`, with Unicode letters and digits as word characters. Empty or whitespace-only names match nothing. */
78
+ function countMentions(text, name) {
79
+ if (name.trim() === "")
80
+ return 0;
81
+ const pattern = new RegExp(`(?<![\\p{L}\\p{N}])${escapeRegExp(name)}(?![\\p{L}\\p{N}])`, "giu");
82
+ return text.match(pattern)?.length ?? 0;
83
+ }
70
84
  /**
71
85
  * checkpoint.ontology.named-evidence: true when `evidence` holds one of
72
86
  * `names` as a word, matching case. A name inside a path, domain, handle, tag
@@ -305,8 +319,11 @@ export function applyOntologyRun(options) {
305
319
  if (types.size > 1)
306
320
  crossTypeNames.add(name);
307
321
  }
308
- // checkpoint.ontology.dominant-sibling-link: cross-type names, and how many other entity files link to each.
309
- const inbound = plan.dominant_sibling_link === true ? countInboundLinks(company.dir, index) : new Map();
322
+ // checkpoint.ontology.dominant-sibling-link and checkpoint.ontology.hub-mention-floor:
323
+ // how many other entity files link to each entity.
324
+ const inbound = plan.dominant_sibling_link === true || plan.hub_mention_floor === true
325
+ ? countInboundLinks(company.dir, index)
326
+ : new Map();
310
327
  const siblings = new Map();
311
328
  if (plan.dominant_sibling_link === true) {
312
329
  const grouped = new Map();
@@ -347,6 +364,32 @@ export function applyOntologyRun(options) {
347
364
  const ownCompanyRel = `ontology/entities/company/${company.slug}.md`;
348
365
  const isOwnCompany = (entity, rel) => entity.type === "company" && (rel === ownCompanyRel || crossTypeKey(entity.canonical_name) === crossTypeKey(company.slug));
349
366
  const inputs = liveInputs(options.hqRoot, company, plan);
367
+ const inputTexts = new Map();
368
+ // The text the model was given for this input: the annotation-stripped copy
369
+ // when there is one, cut to the chunk's line range.
370
+ const inputText = (input) => {
371
+ const cached = inputTexts.get(input.id);
372
+ if (cached !== undefined)
373
+ return cached;
374
+ if (input.read_path !== undefined && input.read_path.split("/").includes("..")) {
375
+ throw new ApplyError("ontology plan read_path leaves the run directory");
376
+ }
377
+ const file = input.read_path !== undefined
378
+ ? path.join(options.runDir, input.read_path)
379
+ : path.join(company.dir, input.path);
380
+ let text = fs.readFileSync(file, "utf8");
381
+ if (input.lines !== null)
382
+ text = text.split("\n").slice(input.lines[0] - 1, input.lines[1]).join("\n");
383
+ inputTexts.set(input.id, text);
384
+ return text;
385
+ };
386
+ // checkpoint.ontology.hub-mention-floor: the final target of a knowledge link is a
387
+ // non-person entity many files link to, and the input names it fewer than twice.
388
+ const underMentionedHub = (input, rel, entity) => plan.hub_mention_floor === true &&
389
+ input.kind === "knowledge" &&
390
+ entity.type !== "person" &&
391
+ (inbound.get(rel) ?? 0) >= HUB_MIN_INBOUND &&
392
+ countMentions(inputText(input), entity.canonical_name) < HUB_MIN_MENTIONS;
350
393
  const blocked = new Set();
351
394
  const updates = new Map();
352
395
  const update = (rel) => {
@@ -414,6 +457,10 @@ export function applyOntologyRun(options) {
414
457
  note(report.rejected, "unnamed_in_evidence");
415
458
  continue;
416
459
  }
460
+ if (underMentionedHub(input, targetRel, targetEntity)) {
461
+ note(report.rejected, "hub_under_mentioned");
462
+ continue;
463
+ }
417
464
  report.matched += 1;
418
465
  report.links.push({
419
466
  input: candidate.input,
@@ -470,6 +517,10 @@ export function applyOntologyRun(options) {
470
517
  continue;
471
518
  }
472
519
  const linked = index.byRel.get(projects[0]);
520
+ if (underMentionedHub(input, projects[0], linked)) {
521
+ note(report.rejected, "hub_under_mentioned");
522
+ continue;
523
+ }
473
524
  report.matched += 1;
474
525
  report.links.push({ input: candidate.input, action: "link", type: linked.type, canonical_name: linked.canonical_name, path: projects[0] });
475
526
  continue;
@@ -81,6 +81,14 @@ export declare const SKIP_AMBIGUOUS_CONCEPT_LINKS_FLAG = "checkpoint.ontology.sk
81
81
  * and recall from 0.784 to 0.816, and never changed a create.
82
82
  */
83
83
  export declare const DOMINANT_SIBLING_LINK_FLAG = "checkpoint.ontology.dominant-sibling-link";
84
+ /**
85
+ * A knowledge link to a non-person entity that 50 or more
86
+ * other entity files link to is rejected as `hub_under_mentioned` when the
87
+ * input names it fewer than 2 times (whole word, any case, canonical name
88
+ * only). The plan carries `hub_mention_floor: true`. CO-62: a replay of six
89
+ * runs raised knowledge precision from 0.753 to 0.781 at the same recall.
90
+ */
91
+ export declare const HUB_MENTION_FLOOR_FLAG = "checkpoint.ontology.hub-mention-floor";
84
92
  /**
85
93
  * A knowledge concept candidate whose name is an alias of more than one
86
94
  * entity, and which apply would defer as `ambiguous_alias`, links instead to
@@ -186,6 +194,8 @@ export interface OntologyPlan {
186
194
  skip_ambiguous_concept_links?: true;
187
195
  /** Set by checkpoint.ontology.dominant-sibling-link: apply redirects a knowledge link from a weak cross-type sibling to the one other files link to. */
188
196
  dominant_sibling_link?: true;
197
+ /** Set by checkpoint.ontology.hub-mention-floor: apply rejects a knowledge link to an under-mentioned non-person hub. */
198
+ hub_mention_floor?: true;
189
199
  /** Set by checkpoint.ontology.concept-project-link: apply links a knowledge concept with an ambiguous alias to the one project of that canonical name. */
190
200
  concept_project_link?: true;
191
201
  /** Set by checkpoint.ontology.named-evidence: apply rejects a knowledge link to the company's own entity, or a concept-project-link redirect, whose evidence does not name it as a word. */
@@ -86,6 +86,14 @@ export const SKIP_AMBIGUOUS_CONCEPT_LINKS_FLAG = "checkpoint.ontology.skip-ambig
86
86
  * and recall from 0.784 to 0.816, and never changed a create.
87
87
  */
88
88
  export const DOMINANT_SIBLING_LINK_FLAG = "checkpoint.ontology.dominant-sibling-link";
89
+ /**
90
+ * A knowledge link to a non-person entity that 50 or more
91
+ * other entity files link to is rejected as `hub_under_mentioned` when the
92
+ * input names it fewer than 2 times (whole word, any case, canonical name
93
+ * only). The plan carries `hub_mention_floor: true`. CO-62: a replay of six
94
+ * runs raised knowledge precision from 0.753 to 0.781 at the same recall.
95
+ */
96
+ export const HUB_MENTION_FLOOR_FLAG = "checkpoint.ontology.hub-mention-floor";
89
97
  /**
90
98
  * A knowledge concept candidate whose name is an alias of more than one
91
99
  * entity, and which apply would defer as `ambiguous_alias`, links instead to
@@ -317,6 +325,7 @@ export async function planOntologyRun(options) {
317
325
  const firstNamePeople = await options.readFlag(FIRST_NAME_PEOPLE_FLAG, company);
318
326
  const skipAmbiguousConceptLinks = await options.readFlag(SKIP_AMBIGUOUS_CONCEPT_LINKS_FLAG, company);
319
327
  const dominantSiblingLink = await options.readFlag(DOMINANT_SIBLING_LINK_FLAG, company);
328
+ const hubMentionFloor = await options.readFlag(HUB_MENTION_FLOOR_FLAG, company);
320
329
  const conceptProjectLink = await options.readFlag(CONCEPT_PROJECT_LINK_FLAG, company);
321
330
  const namedEvidence = await options.readFlag(NAMED_EVIDENCE_FLAG, company);
322
331
  const queuedTouchesOn = borrowed || (await options.readFlag(QUEUED_TOUCHES_FLAG, company));
@@ -441,6 +450,7 @@ export async function planOntologyRun(options) {
441
450
  ...(firstNamePeople ? { first_name_people: true } : {}),
442
451
  ...(skipAmbiguousConceptLinks ? { skip_ambiguous_concept_links: true } : {}),
443
452
  ...(dominantSiblingLink ? { dominant_sibling_link: true } : {}),
453
+ ...(hubMentionFloor ? { hub_mention_floor: true } : {}),
444
454
  ...(conceptProjectLink ? { concept_project_link: true } : {}),
445
455
  ...(namedEvidence ? { named_evidence: true } : {}),
446
456
  inputs,
@@ -7,7 +7,7 @@
7
7
  * that resolve to the same home are refused (identity-keyed-cap rule).
8
8
  */
9
9
  import * as fs from "node:fs";
10
- import { type AccountConfig, type LaneProvider, type LanesConfig, type LivenessWindows, type ProviderConfig, type TrackerChecksConfig } from "./types.js";
10
+ import { type AccountConfig, type LaneProvider, type LanesConfig, type RunawaySearchGuardConfig, type LivenessWindows, type ProviderConfig, type TrackerChecksConfig } from "./types.js";
11
11
  export declare const LANES_CONFIG_REL: string;
12
12
  export declare const DEFAULT_MAX_QUEUED = 3;
13
13
  export declare const DEFAULT_CPU_BUSY_PAUSE = 0.85;
@@ -44,6 +44,7 @@ export declare const DEFAULT_BLOCKER_FRESHNESS_HOURS = 4;
44
44
  /** Manager key results must be re-measured within six hours by default. */
45
45
  export declare const DEFAULT_KR_FRESHNESS_HOURS = 6;
46
46
  export declare const DEFAULT_TRACKER_CHECKS: TrackerChecksConfig;
47
+ export declare const DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG: RunawaySearchGuardConfig;
47
48
  export declare const DEFAULT_LIVENESS_WINDOWS: LivenessWindows;
48
49
  export declare const DEFAULT_PROVIDER_MAX_RUNNING: Record<LaneProvider, number>;
49
50
  export declare const DEFAULT_ACCOUNT_HOMES: Record<LaneProvider, string>;
@@ -56,6 +56,10 @@ export const DEFAULT_TRACKER_CHECKS = {
56
56
  kpi_value_max_age_ms: 24 * 60 * 60 * 1000,
57
57
  directive_ack_timeout_ms: 30 * 60 * 1000,
58
58
  };
59
+ export const DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG = {
60
+ max_age_seconds: 20,
61
+ find_maxdepth_exempt: 3,
62
+ };
59
63
  export const DEFAULT_LIVENESS_WINDOWS = {
60
64
  live_within_ms: DEFAULT_LIVE_WITHIN_MS,
61
65
  turn_start_lost_ms: DEFAULT_TURN_START_LOST_MS,
@@ -417,6 +421,16 @@ function parseTrackerChecks(raw) {
417
421
  directive_ack_timeout_ms: positiveInt(checks.directive_ack_timeout_ms, "tracker.checks.directive_ack_timeout_ms", DEFAULT_TRACKER_CHECKS.directive_ack_timeout_ms),
418
422
  };
419
423
  }
424
+ function parseRunawaySearchGuard(raw) {
425
+ const guard = raw === undefined ? {} : raw;
426
+ if (!isPlainObject(guard)) {
427
+ throw new LanesConfigError("invalid_config", "runaway_search_guard must be an object");
428
+ }
429
+ return {
430
+ max_age_seconds: positiveInt(guard.max_age_seconds, "runaway_search_guard.max_age_seconds", DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG.max_age_seconds),
431
+ find_maxdepth_exempt: positiveInt(guard.find_maxdepth_exempt, "runaway_search_guard.find_maxdepth_exempt", DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG.find_maxdepth_exempt),
432
+ };
433
+ }
420
434
  export function documentedDefaultConfig(deps = defaultDeps()) {
421
435
  const providers = {};
422
436
  for (const provider of LANE_PROVIDERS) {
@@ -440,6 +454,7 @@ export function documentedDefaultConfig(deps = defaultDeps()) {
440
454
  },
441
455
  liveness: { ...DEFAULT_LIVENESS_WINDOWS },
442
456
  tracker: { checks: { ...DEFAULT_TRACKER_CHECKS } },
457
+ runaway_search_guard: { ...DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG },
443
458
  session_question_timeout_secs: DEFAULT_SESSION_QUESTION_TIMEOUT_SECS,
444
459
  blocker_freshness_hours: DEFAULT_BLOCKER_FRESHNESS_HOURS,
445
460
  kr_freshness_hours: DEFAULT_KR_FRESHNESS_HOURS,
@@ -484,6 +499,7 @@ export function parseLanesConfig(raw, deps = defaultDeps()) {
484
499
  throttle: parseThrottle(raw.throttle),
485
500
  liveness: parseLiveness(raw.liveness),
486
501
  tracker: { checks: parseTrackerChecks(raw.tracker) },
502
+ runaway_search_guard: parseRunawaySearchGuard(raw.runaway_search_guard),
487
503
  session_question_timeout_secs: positiveInt(raw.session_question_timeout_secs, "session_question_timeout_secs", DEFAULT_SESSION_QUESTION_TIMEOUT_SECS),
488
504
  blocker_freshness_hours: positiveNumber(raw.blocker_freshness_hours, "blocker_freshness_hours", DEFAULT_BLOCKER_FRESHNESS_HOURS),
489
505
  kr_freshness_hours: positiveNumber(raw.kr_freshness_hours, "kr_freshness_hours", DEFAULT_KR_FRESHNESS_HOURS),
@@ -9,6 +9,7 @@ import { type LivenessDeps } from "./liveness.js";
9
9
  import { type EnvelopeRepairSpawnInput, type SpawnLaneDeps } from "./spawn.js";
10
10
  import { type ThrottleDeps } from "./throttle.js";
11
11
  import { readLanesRegistryFlag } from "./registry-flag.js";
12
+ import { type RunawayProcessInfo } from "./runaway-tool-guard.js";
12
13
  import { type KeepAliveBlockedReason } from "./keep-alive.js";
13
14
  import type { LaneCompanyRef, LaneRecord, LivenessState, QueuedReason, QuestionRecord, SeniorRef } from "./types.js";
14
15
  import { type LaneMesh } from "./mesh-events.js";
@@ -91,6 +92,14 @@ export interface ReconcileDeps {
91
92
  meshEmitTimeoutMs?: number;
92
93
  /** Fresh keep-alive eligibility read; injected to exercise opt-in changes before queueing. */
93
94
  readKeepAliveRecheckLane?: (hqRoot: string, laneId: string) => LaneRecord;
95
+ /** Process-table snapshot seam for the opt-in runaway-search guard. */
96
+ runawayToolProcessLister?: () => readonly RunawayProcessInfo[];
97
+ /** Test seam: prevent real process signals while exercising the guard. */
98
+ runawayToolProcessSignal?: (pid: number, signal: NodeJS.Signals) => void;
99
+ /** Test seam for interleaving a watcher signal with an append-only guard event. */
100
+ runawayToolEventAppend?: (target: string, line: string) => void;
101
+ /** Test seam for validating a candidate PID against its process-table starttime. */
102
+ runawayToolProcessIsCurrent?: (pid: number, expectedStarttime: number) => boolean;
94
103
  /**
95
104
  * Awaited after lane/question records and liveness are loaded, before any
96
105
  * CAS write. Test seam so two in-process passes can share a snapshot.
@@ -18,16 +18,18 @@ import { AccountResolutionError, LanesError, isLaneVersionConflict } from "./err
18
18
  import { loadWorkerProfile } from "./profile.js";
19
19
  import { terminalChildResumeHireGrantError, withHireGrantAdmission, } from "./grants.js";
20
20
  import { classifySessionLiveness, defaultLivenessDeps, loadLivenessIndex, readDaemonPresenceConnected, unindexedClaudeSessionLiveness, } from "./liveness.js";
21
- import { ANSWERED_QUESTIONS_FILE, armLaneWatcher, envelopeRepairMode, isPidRecordAlive, laneRunDir, readPidRecord, readSpawnManifest, markSpawnFailure, spawnEnvelopeRepair, spawnLaneWorker, retryLaneOnAlternateAccount, } from "./spawn.js";
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
23
  import { applyThrottleDecision, decideAdmission, defaultThrottleDeps, emptyThrottleState, evaluateThrottle, isCpuCheckEnabled, readAvailableMemMb, readThrottleState, resolveCpuBusyFraction, throttleStatePath, writeThrottleState, } from "./throttle.js";
24
24
  import { readLanesRegistryFlag } from "./registry-flag.js";
25
25
  import { resolveLaneCompany } from "./lane-company.js";
26
+ import { appendDropboxMessage } from "./dropbox.js";
27
+ import { DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG, listHostProcesses, runRunawayToolGuard, RUNAWAY_TOOL_GUARD_FLAG, } from "./runaway-tool-guard.js";
26
28
  import { LEAD_LANE_KEEP_ALIVE_FLAG, LEAD_LANE_KEEP_ALIVE_INTERRUPTED_RESUME_TEXT, LEAD_LANE_KEEP_ALIVE_MAX_RESUMES, LEAD_LANE_KEEP_ALIVE_RESUME_TEXT, LANE_SUPERSESSION_LINEAGE_AWARE_FLAG, doneKeepAliveVerdict, hasPendingKeepAliveWork, keepAliveExclusion, loadRetiredLaneIds, leadLaneKeepAliveBackoffMs, undeliveredAnsweredQuestions, } from "./keep-alive.js";
27
29
  import { providerSupportsSessionResume } from "./delivery.js";
28
30
  import { applyDefaultQuotaHold, readQuotaSignal, recordAccountQuota } from "./quota.js";
29
31
  import { answeredQuestionEntries, questionRecordRefusalCode, raiseEnvelopeQuestions, resolveQuestionCompanyWithFallback, } from "./questions.js";
30
- import { SILENCE_MS, classifyLaneExit, isPidAlive, missingExpectedEnvelopeFile, readLatestEnvelopeFile, readLatestEnvelopeRecord, watcherEventsPath, watcherOutLogPath, writeWatcherDecision, } from "./watcher.js";
32
+ import { SILENCE_MS, appendWatcherEvent, classifyLaneExit, isPidAlive, missingExpectedEnvelopeFile, readLatestEnvelopeFile, readLatestEnvelopeRecord, watcherEventsPath, watcherOutLogPath, writeWatcherDecision, } from "./watcher.js";
31
33
  import { createLaneMesh, emitLaneDecisionEvents, emitLaneTerminalStateEvent, laneMeshCompany, } from "./mesh-events.js";
32
34
  import { laneStories, storyStatusForEnvelope } from "./stories.js";
33
35
  import { resolveManagerStatusGateForLane } from "./manager-status-gate.js";
@@ -658,6 +660,128 @@ function classifyDeadLocalLane(hqRoot, laneId, now) {
658
660
  lane,
659
661
  };
660
662
  }
663
+ function runawayGuardCompanyKey(company) {
664
+ return company
665
+ ? `${company.uid ?? ""}\u0000${company.slug ?? ""}`
666
+ : "unresolved-company";
667
+ }
668
+ function reportRunawayGuardError(summary, stage, error) {
669
+ summary.ok = false;
670
+ summary.errors.push({
671
+ code: `runaway_tool_guard_${stage}_failed`,
672
+ message: `runaway search guard ${stage} failed; no additional search processes were stopped`,
673
+ });
674
+ process.stderr.write(`${JSON.stringify({
675
+ event: "lanes.runaway_tool_guard.failed",
676
+ stage,
677
+ error: error instanceof Error ? error.name : "UnknownError",
678
+ fallback: "continue_without_argv",
679
+ })}\n`);
680
+ }
681
+ async function runRunawayToolGuardPass(hqRoot, lanes, config, deps, now, summary) {
682
+ if (deps.dryRun)
683
+ return;
684
+ const localRunningLanes = lanes.filter((lane) => lane.state === "running" && laneRunsHere(lane, deps.host));
685
+ if (localRunningLanes.length === 0)
686
+ return;
687
+ const groups = new Map();
688
+ for (const lane of localRunningLanes) {
689
+ let worker;
690
+ try {
691
+ worker = readPidRecord(path.join(laneRunDir(hqRoot, lane.lane_id), "worker.pid"));
692
+ if (!worker ||
693
+ worker.starttime === undefined ||
694
+ !isPidRecordAlive(worker, deps.isAlive))
695
+ continue;
696
+ }
697
+ catch (error) {
698
+ reportRunawayGuardError(summary, "worker_pid_read", error);
699
+ continue;
700
+ }
701
+ let company;
702
+ try {
703
+ company = resolveLaneCompany(hqRoot, lane).company;
704
+ }
705
+ catch (error) {
706
+ reportRunawayGuardError(summary, "company_resolution", error);
707
+ continue;
708
+ }
709
+ const key = runawayGuardCompanyKey(company);
710
+ const group = groups.get(key) ?? { company, lanes: [] };
711
+ group.lanes.push({ laneId: lane.lane_id, agentPid: worker.pid, agentStarttime: worker.starttime });
712
+ groups.set(key, group);
713
+ }
714
+ if (groups.size === 0)
715
+ return;
716
+ const readRegistryFlag = deps.readRegistryFlag ?? readLanesRegistryFlag;
717
+ const enabledGroups = new Set();
718
+ const env = deps.env ?? process.env;
719
+ await Promise.all([...groups.entries()].map(async ([key, group]) => {
720
+ try {
721
+ const enabled = await readRegistryFlag(RUNAWAY_TOOL_GUARD_FLAG, {
722
+ company: group.company ?? undefined,
723
+ env,
724
+ });
725
+ if (enabled)
726
+ enabledGroups.add(key);
727
+ }
728
+ catch (error) {
729
+ reportRunawayGuardError(summary, "flag_read", error);
730
+ }
731
+ }));
732
+ if (enabledGroups.size === 0)
733
+ return;
734
+ const laneProcesses = [...groups.entries()]
735
+ .filter(([key]) => enabledGroups.has(key))
736
+ .flatMap(([, group]) => group.lanes);
737
+ if (laneProcesses.length === 0)
738
+ return;
739
+ let processes;
740
+ try {
741
+ processes = (deps.runawayToolProcessLister ?? listHostProcesses)();
742
+ }
743
+ catch (error) {
744
+ reportRunawayGuardError(summary, "process_table_read", error);
745
+ return;
746
+ }
747
+ const runawayConfig = config.runaway_search_guard ?? DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG;
748
+ try {
749
+ runRunawayToolGuard({
750
+ hqRoot,
751
+ lanes: laneProcesses,
752
+ processes,
753
+ config: runawayConfig,
754
+ now,
755
+ currentUid: process.getuid?.() ?? -1,
756
+ isCurrentProcess: deps.runawayToolProcessIsCurrent ??
757
+ ((pid, expectedStarttime) => readProcStarttime(pid) === expectedStarttime),
758
+ signal: deps.runawayToolProcessSignal ?? ((pid, signal) => process.kill(pid, signal)),
759
+ recordEvent: (laneId, event) => appendWatcherEvent(watcherEventsPath(hqRoot, laneId), event, deps.runawayToolEventAppend),
760
+ notifyLane: (laneId, text) => appendDropboxMessage(hqRoot, laneId, {
761
+ at: now.toISOString(),
762
+ message_id: `runaway-search-guard:${laneId}:${now.getTime()}`,
763
+ kind: "system_notice",
764
+ text,
765
+ }),
766
+ reportError: (stage, errorName) => {
767
+ summary.ok = false;
768
+ summary.errors.push({
769
+ code: `runaway_tool_guard_${stage}_failed`,
770
+ message: `runaway search guard could not ${stage} a stopped process (${errorName})`,
771
+ });
772
+ process.stderr.write(`${JSON.stringify({
773
+ event: "lanes.runaway_tool_guard.failed",
774
+ stage,
775
+ error: errorName,
776
+ fallback: "continue_without_argv",
777
+ })}\n`);
778
+ },
779
+ });
780
+ }
781
+ catch (error) {
782
+ reportRunawayGuardError(summary, "execution", error);
783
+ }
784
+ }
661
785
  /** Unlocked pass body. `runReconcilePass` takes the process lock around this. */
662
786
  export async function runPassBody(hqRoot, deps) {
663
787
  const now = deps.now();
@@ -728,6 +852,7 @@ export async function runPassBody(hqRoot, deps) {
728
852
  : QUESTION_AGE_MS;
729
853
  if (deps.afterLoad)
730
854
  await deps.afterLoad();
855
+ await runRunawayToolGuardPass(hqRoot, lanes, config, deps, now, summary);
731
856
  const startDeps = await prepareManagerStatusGateStarts(hqRoot, lanes, deps, summary);
732
857
  markOverdueLanes(hqRoot, lanes, summary, deps, now);
733
858
  await heartbeatAndDeadHosts(hqRoot, lanes, summary, startDeps, now);
@@ -0,0 +1,38 @@
1
+ import type { RunawaySearchGuardConfig } from "./types.js";
2
+ export declare const RUNAWAY_TOOL_GUARD_FLAG = "lanes.runaway-tool-guard";
3
+ export interface RunawayProcessInfo {
4
+ pid: number;
5
+ ppid: number;
6
+ elapsedSeconds: number;
7
+ uid: number;
8
+ command: string;
9
+ argv: readonly string[];
10
+ cwd: string;
11
+ starttime: number | null;
12
+ }
13
+ export interface RunawayLaneProcess {
14
+ laneId: string;
15
+ agentPid: number;
16
+ agentStarttime: number;
17
+ }
18
+ export interface RunawayToolGuardInput {
19
+ hqRoot: string;
20
+ lanes: readonly RunawayLaneProcess[];
21
+ processes: readonly RunawayProcessInfo[];
22
+ config: RunawaySearchGuardConfig;
23
+ now: Date;
24
+ currentUid: number;
25
+ isCurrentProcess: (pid: number, expectedStarttime: number) => boolean;
26
+ signal: (pid: number, signal: NodeJS.Signals) => void;
27
+ recordEvent: (laneId: string, event: Record<string, unknown>) => void;
28
+ notifyLane: (laneId: string, text: string) => void;
29
+ reportError: (stage: "signal" | "record" | "notify", errorName: string) => void;
30
+ }
31
+ export interface RunawayToolGuardResult {
32
+ stopped: number;
33
+ }
34
+ export declare function runRunawayToolGuard(input: RunawayToolGuardInput): RunawayToolGuardResult;
35
+ /** Read the host process table once. argv and cwd are read only for search tools. */
36
+ export declare function listHostProcesses(): RunawayProcessInfo[];
37
+ export { DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG } from "./config.js";
38
+ //# sourceMappingURL=runaway-tool-guard.d.ts.map
@@ -0,0 +1,284 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import * as fs from "node:fs";
3
+ import * as os from "node:os";
4
+ import * as path from "node:path";
5
+ import { readProcStarttime } from "./spawn.js";
6
+ export const RUNAWAY_TOOL_GUARD_FLAG = "lanes.runaway-tool-guard";
7
+ const SEARCH_COMMANDS = new Set(["rg", "grep", "ugrep", "ag", "find"]);
8
+ const VALUE_OPTIONS = new Set([
9
+ "-A", "--after-context", "-B", "--before-context", "-C", "--context",
10
+ "-e", "--regexp", "-f", "--file", "-g", "--glob", "-t", "--type",
11
+ "-T", "--type-not", "-M", "--max-count", "-m", "--max-count",
12
+ "-j", "--threads", "--ignore-file", "--include", "--exclude",
13
+ "--exclude-dir", "--exclude-from", "--encoding", "--sort",
14
+ ]);
15
+ function commandName(command) {
16
+ return path.basename(command).toLowerCase();
17
+ }
18
+ function hasNoIgnoreFlag(command, args) {
19
+ if (command === "rg" || command === "ag") {
20
+ return args.some((arg) => ["--no-ignore", "--no-ignore-vcs", "--unrestricted", "--skip-vcs-ignores"].includes(arg) ||
21
+ /^-[A-Za-z]*u[A-Za-z]*$/.test(arg));
22
+ }
23
+ if (command === "grep" || command === "ugrep") {
24
+ return args.some((arg) => ["--recursive", "--dereference-recursive"].includes(arg) ||
25
+ /^-[A-Za-z]*[rR][A-Za-z]*$/.test(arg));
26
+ }
27
+ return false;
28
+ }
29
+ function findDepth(args) {
30
+ for (let index = 0; index < args.length; index += 1) {
31
+ if (args[index] !== "-maxdepth")
32
+ continue;
33
+ const value = Number(args[index + 1]);
34
+ return Number.isInteger(value) && value >= 0 ? value : null;
35
+ }
36
+ return null;
37
+ }
38
+ function positionalArgs(command, args) {
39
+ if (command === "find") {
40
+ let index = 0;
41
+ while (index < args.length) {
42
+ const arg = args[index];
43
+ if (["-H", "-L", "-P"].includes(arg) || /^-O\d+$/.test(arg)) {
44
+ index += 1;
45
+ continue;
46
+ }
47
+ if (arg === "-D") {
48
+ index += 2;
49
+ continue;
50
+ }
51
+ if (/^-D.+/.test(arg)) {
52
+ index += 1;
53
+ continue;
54
+ }
55
+ break;
56
+ }
57
+ const paths = [];
58
+ for (; index < args.length; index += 1) {
59
+ const arg = args[index];
60
+ if (arg.startsWith("-"))
61
+ break;
62
+ paths.push(arg);
63
+ }
64
+ return paths;
65
+ }
66
+ const positionals = [];
67
+ let explicitPattern = false;
68
+ let afterTerminator = false;
69
+ let listFiles = false;
70
+ for (let index = 0; index < args.length; index += 1) {
71
+ const arg = args[index];
72
+ if (afterTerminator) {
73
+ positionals.push(arg);
74
+ continue;
75
+ }
76
+ if (arg === "--") {
77
+ afterTerminator = true;
78
+ continue;
79
+ }
80
+ if (arg === "--files") {
81
+ listFiles = true;
82
+ continue;
83
+ }
84
+ if (VALUE_OPTIONS.has(arg)) {
85
+ if (["-e", "--regexp", "-f", "--file"].includes(arg))
86
+ explicitPattern = true;
87
+ index += 1;
88
+ continue;
89
+ }
90
+ if (arg.startsWith("--")) {
91
+ if (arg.startsWith("--regexp=") || arg.startsWith("--file="))
92
+ explicitPattern = true;
93
+ continue;
94
+ }
95
+ if (arg.startsWith("-") && arg.length > 1) {
96
+ if (/^-e.+$/.test(arg) || /^-f.+$/.test(arg))
97
+ explicitPattern = true;
98
+ continue;
99
+ }
100
+ positionals.push(arg);
101
+ }
102
+ if (listFiles || explicitPattern)
103
+ return positionals;
104
+ return positionals.slice(1);
105
+ }
106
+ function canonicalPath(candidate) {
107
+ try {
108
+ return fs.realpathSync(candidate);
109
+ }
110
+ catch {
111
+ return path.resolve(candidate);
112
+ }
113
+ }
114
+ function searchesBroadRoot(hqRoot, processInfo, command, args) {
115
+ const roots = new Set([
116
+ canonicalPath(hqRoot),
117
+ canonicalPath(path.join(hqRoot, "workspace")),
118
+ canonicalPath(os.homedir()),
119
+ path.parse(path.resolve("/")).root,
120
+ ]);
121
+ const operands = positionalArgs(command, args);
122
+ const candidates = operands.length > 0
123
+ ? operands.map((operand) => path.resolve(processInfo.cwd || ".", operand))
124
+ : [processInfo.cwd || "."];
125
+ return candidates.some((candidate) => roots.has(canonicalPath(candidate)));
126
+ }
127
+ function isUnboundedSearch(hqRoot, processInfo, findMaxdepthExempt) {
128
+ const command = commandName(processInfo.command);
129
+ if (!SEARCH_COMMANDS.has(command))
130
+ return false;
131
+ const args = processInfo.argv.slice(1);
132
+ if (command === "find") {
133
+ if (args.includes("-prune"))
134
+ return false;
135
+ const depth = findDepth(args);
136
+ if (depth !== null && depth <= findMaxdepthExempt)
137
+ return false;
138
+ return searchesBroadRoot(hqRoot, processInfo, command, args);
139
+ }
140
+ if (!hasNoIgnoreFlag(command, args))
141
+ return false;
142
+ return searchesBroadRoot(hqRoot, processInfo, command, args);
143
+ }
144
+ function laneAncestor(processInfo, processByPid, laneByPid, currentUid) {
145
+ let parentPid = processInfo.ppid;
146
+ const visited = new Set([processInfo.pid]);
147
+ for (let depth = 0; depth < 128; depth += 1) {
148
+ const laneId = laneByPid.get(parentPid);
149
+ if (laneId)
150
+ return laneId;
151
+ if (parentPid <= 1 || visited.has(parentPid))
152
+ return null;
153
+ visited.add(parentPid);
154
+ const parent = processByPid.get(parentPid);
155
+ if (!parent || parent.uid !== currentUid)
156
+ return null;
157
+ parentPid = parent.ppid;
158
+ }
159
+ return null;
160
+ }
161
+ function errorName(error) {
162
+ return error instanceof Error ? error.name : "UnknownError";
163
+ }
164
+ export function runRunawayToolGuard(input) {
165
+ const processByPid = new Map();
166
+ for (const processInfo of input.processes) {
167
+ if (Number.isSafeInteger(processInfo.pid) && processInfo.pid > 1) {
168
+ processByPid.set(processInfo.pid, processInfo);
169
+ }
170
+ }
171
+ const laneByPid = new Map();
172
+ const ambiguousPids = new Set();
173
+ for (const lane of input.lanes) {
174
+ const agent = processByPid.get(lane.agentPid);
175
+ if (!agent ||
176
+ !Number.isSafeInteger(lane.agentStarttime) ||
177
+ agent.starttime !== lane.agentStarttime ||
178
+ agent.uid !== input.currentUid ||
179
+ agent.pid === agent.ppid)
180
+ continue;
181
+ if (laneByPid.has(lane.agentPid))
182
+ ambiguousPids.add(lane.agentPid);
183
+ else
184
+ laneByPid.set(lane.agentPid, lane.laneId);
185
+ }
186
+ for (const pid of ambiguousPids)
187
+ laneByPid.delete(pid);
188
+ let stopped = 0;
189
+ for (const processInfo of input.processes) {
190
+ if (processInfo.uid !== input.currentUid ||
191
+ !Number.isFinite(processInfo.elapsedSeconds) ||
192
+ processInfo.elapsedSeconds <= input.config.max_age_seconds ||
193
+ processInfo.starttime === null ||
194
+ !isUnboundedSearch(input.hqRoot, processInfo, input.config.find_maxdepth_exempt))
195
+ continue;
196
+ const laneId = laneAncestor(processInfo, processByPid, laneByPid, input.currentUid);
197
+ if (!laneId || !input.isCurrentProcess(processInfo.pid, processInfo.starttime))
198
+ continue;
199
+ try {
200
+ input.signal(processInfo.pid, "SIGTERM");
201
+ }
202
+ catch (error) {
203
+ if (error.code !== "ESRCH") {
204
+ input.reportError("signal", errorName(error));
205
+ }
206
+ continue;
207
+ }
208
+ const command = commandName(processInfo.command);
209
+ const durationSeconds = Math.floor(processInfo.elapsedSeconds);
210
+ stopped += 1;
211
+ try {
212
+ input.recordEvent(laneId, {
213
+ event: "runaway_search_stopped",
214
+ at: input.now.toISOString(),
215
+ pid: processInfo.pid,
216
+ command,
217
+ duration_seconds: durationSeconds,
218
+ });
219
+ }
220
+ catch (error) {
221
+ input.reportError("record", errorName(error));
222
+ }
223
+ try {
224
+ input.notifyLane(laneId, `runaway-search-guard stopped ${command} after ${durationSeconds}s. Narrow the search to the smallest repo path that holds the answer and keep ignore rules on.`);
225
+ }
226
+ catch (error) {
227
+ input.reportError("notify", errorName(error));
228
+ }
229
+ }
230
+ return { stopped };
231
+ }
232
+ /** Read the host process table once. argv and cwd are read only for search tools. */
233
+ export function listHostProcesses() {
234
+ if (process.platform !== "linux")
235
+ return [];
236
+ let output;
237
+ try {
238
+ output = execFileSync("ps", ["-eo", "pid=,ppid=,etimes=,uid=,comm="], {
239
+ encoding: "utf8",
240
+ stdio: ["ignore", "pipe", "ignore"],
241
+ timeout: 2_000,
242
+ maxBuffer: 8 * 1024 * 1024,
243
+ windowsHide: true,
244
+ });
245
+ }
246
+ catch {
247
+ throw new Error("ps process-table read failed");
248
+ }
249
+ const processes = [];
250
+ for (const line of output.split("\n")) {
251
+ const match = line.trim().match(/^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(.+)$/);
252
+ if (!match)
253
+ continue;
254
+ const pid = Number(match[1]);
255
+ const ppid = Number(match[2]);
256
+ const elapsedSeconds = Number(match[3]);
257
+ const uid = Number(match[4]);
258
+ const command = match[5].trim();
259
+ if (![pid, ppid, elapsedSeconds, uid].every(Number.isSafeInteger) || !command)
260
+ continue;
261
+ let argv = [];
262
+ let cwd = "";
263
+ let starttime = null;
264
+ if (SEARCH_COMMANDS.has(commandName(command))) {
265
+ try {
266
+ const commandLine = fs.readFileSync(`/proc/${pid}/cmdline`);
267
+ argv = commandLine.toString("utf8").split("\0");
268
+ if (argv.at(-1) === "")
269
+ argv.pop();
270
+ cwd = fs.readlinkSync(`/proc/${pid}/cwd`);
271
+ starttime = readProcStarttime(pid);
272
+ }
273
+ catch {
274
+ argv = [];
275
+ cwd = "";
276
+ starttime = null;
277
+ }
278
+ }
279
+ processes.push({ pid, ppid, elapsedSeconds, uid, command, argv, cwd, starttime });
280
+ }
281
+ return processes;
282
+ }
283
+ export { DEFAULT_RUNAWAY_SEARCH_GUARD_CONFIG } from "./config.js";
284
+ //# sourceMappingURL=runaway-tool-guard.js.map
@@ -891,6 +891,12 @@ export interface TrackerChecksConfig {
891
891
  kpi_value_max_age_ms: number;
892
892
  directive_ack_timeout_ms: number;
893
893
  }
894
+ export interface RunawaySearchGuardConfig {
895
+ /** Wall-time threshold before an unbounded descendant search is stopped. */
896
+ max_age_seconds: number;
897
+ /** `find` searches at this depth or less are left alone. */
898
+ find_maxdepth_exempt: number;
899
+ }
894
900
  export interface LanesConfig {
895
901
  schema_version: typeof CONFIG_SCHEMA_VERSION | string;
896
902
  providers: Record<LaneProvider, ProviderConfig>;
@@ -899,6 +905,7 @@ export interface LanesConfig {
899
905
  tracker: {
900
906
  checks: TrackerChecksConfig;
901
907
  };
908
+ runaway_search_guard?: RunawaySearchGuardConfig;
902
909
  /** Seconds an unanswered question waits at a session audience before advancing. */
903
910
  session_question_timeout_secs: number;
904
911
  /** Age in hours after which an open manager blocker must be re-verified. */
@@ -130,6 +130,8 @@ export declare function watcherEventsPath(hqRoot: string, laneId: string): strin
130
130
  export declare function watcherOutLogPath(hqRoot: string, laneId: string): string;
131
131
  export declare function appendCappedLog(filePath: string, line: string, maxBytes?: number): void;
132
132
  export declare function writeWatcherDecision(filePath: string, record: Record<string, unknown>, maxBytes?: number): void;
133
+ /** Append a decision to the shared event stream without a read-modify-write. */
134
+ export declare function appendWatcherEvent(filePath: string, record: Record<string, unknown>, appendLine?: (target: string, line: string) => void): void;
133
135
  export declare function watcherLastSignalPath(hqRoot: string, laneId: string): string;
134
136
  export declare function appendWatcherSignal(hqRoot: string, laneId: string, signal: WatcherSignal): void;
135
137
  /**
@@ -223,6 +223,16 @@ export function writeWatcherDecision(filePath, record, maxBytes = WATCHER_OUT_MA
223
223
  }
224
224
  appendCappedLog(filePath, JSON.stringify(compact), maxBytes);
225
225
  }
226
+ /** Append a decision to the shared event stream without a read-modify-write. */
227
+ export function appendWatcherEvent(filePath, record, appendLine = (target, line) => fs.appendFileSync(target, line, "utf8")) {
228
+ const compact = {};
229
+ for (const [key, value] of Object.entries(record)) {
230
+ if (value !== undefined)
231
+ compact[key] = value;
232
+ }
233
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
234
+ appendLine(filePath, `${JSON.stringify(compact)}\n`);
235
+ }
226
236
  export function watcherLastSignalPath(hqRoot, laneId) {
227
237
  return path.join(laneRunDir(hqRoot, laneId), "watcher", "last-signal.json");
228
238
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@indigoai-us/hq-cli",
3
- "version": "5.277.0",
3
+ "version": "5.278.0",
4
4
  "description": "HQ by Indigo management CLI — modules and cloud sync",
5
5
  "main": "dist/index.js",
6
6
  "bin": {