harnery 0.7.1 → 0.9.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 (98) hide show
  1. package/README.md +2 -1
  2. package/dist/commander.d.ts +9 -0
  3. package/dist/commander.d.ts.map +1 -1
  4. package/dist/commander.js +2 -0
  5. package/dist/commands/callers.d.ts.map +1 -1
  6. package/dist/commands/callers.js +69 -17
  7. package/dist/commands/doctor.d.ts.map +1 -1
  8. package/dist/commands/doctor.js +84 -2
  9. package/dist/commands/grep.d.ts +77 -0
  10. package/dist/commands/grep.d.ts.map +1 -1
  11. package/dist/commands/grep.js +598 -109
  12. package/dist/commands/init.d.ts +15 -5
  13. package/dist/commands/init.d.ts.map +1 -1
  14. package/dist/commands/init.js +125 -14
  15. package/dist/commands/workflow.d.ts +4 -0
  16. package/dist/commands/workflow.d.ts.map +1 -0
  17. package/dist/commands/workflow.js +89 -0
  18. package/dist/core/agents/cli.js +10 -3
  19. package/dist/core/agents/rules/claim-conflict.d.ts.map +1 -1
  20. package/dist/core/agents/rules/claim-conflict.js +26 -1
  21. package/dist/core/agents/rules/stop-hook.d.ts +8 -0
  22. package/dist/core/agents/rules/stop-hook.d.ts.map +1 -1
  23. package/dist/core/agents/rules/stop-hook.js +8 -0
  24. package/dist/core/agents/state/heartbeat-projector.d.ts +2 -0
  25. package/dist/core/agents/state/heartbeat-projector.d.ts.map +1 -1
  26. package/dist/core/agents/state/heartbeat-projector.js +13 -2
  27. package/dist/core/agents/state/heartbeat-writer.d.ts +16 -2
  28. package/dist/core/agents/state/heartbeat-writer.d.ts.map +1 -1
  29. package/dist/core/agents/state/heartbeat-writer.js +21 -4
  30. package/dist/core/config.d.ts +18 -0
  31. package/dist/core/config.d.ts.map +1 -1
  32. package/dist/core/config.js +38 -0
  33. package/dist/core/hooks/cli.js +29 -3
  34. package/dist/core/hooks/events/schema.d.ts +4 -0
  35. package/dist/core/hooks/events/schema.d.ts.map +1 -1
  36. package/dist/core/hooks/harness/events.d.ts +7 -0
  37. package/dist/core/hooks/harness/events.d.ts.map +1 -1
  38. package/dist/core/hooks/harness/events.js +1 -0
  39. package/dist/core/hooks/resolve/coord-root.d.ts +11 -0
  40. package/dist/core/hooks/resolve/coord-root.d.ts.map +1 -1
  41. package/dist/core/hooks/resolve/coord-root.js +28 -6
  42. package/dist/core/workflow/billing.d.ts +48 -0
  43. package/dist/core/workflow/billing.d.ts.map +1 -0
  44. package/dist/core/workflow/billing.js +102 -0
  45. package/dist/core/workflow/child-env.d.ts +30 -0
  46. package/dist/core/workflow/child-env.d.ts.map +1 -0
  47. package/dist/core/workflow/child-env.js +43 -0
  48. package/dist/core/workflow/engine.d.ts +21 -0
  49. package/dist/core/workflow/engine.d.ts.map +1 -0
  50. package/dist/core/workflow/engine.js +340 -0
  51. package/dist/core/workflow/harnesses.d.ts +17 -0
  52. package/dist/core/workflow/harnesses.d.ts.map +1 -0
  53. package/dist/core/workflow/harnesses.js +30 -0
  54. package/dist/core/workflow/spawn-claude.d.ts +22 -0
  55. package/dist/core/workflow/spawn-claude.d.ts.map +1 -0
  56. package/dist/core/workflow/spawn-claude.js +82 -0
  57. package/dist/core/workflow/spawn-codex.d.ts +19 -0
  58. package/dist/core/workflow/spawn-codex.d.ts.map +1 -0
  59. package/dist/core/workflow/spawn-codex.js +66 -0
  60. package/dist/core/workflow/spawn-cursor.d.ts +26 -0
  61. package/dist/core/workflow/spawn-cursor.d.ts.map +1 -0
  62. package/dist/core/workflow/spawn-cursor.js +72 -0
  63. package/dist/core/workflow/types.d.ts +145 -0
  64. package/dist/core/workflow/types.d.ts.map +1 -0
  65. package/dist/core/workflow/types.js +9 -0
  66. package/dist/core/workflow/validate.d.ts +15 -0
  67. package/dist/core/workflow/validate.d.ts.map +1 -0
  68. package/dist/core/workflow/validate.js +70 -0
  69. package/dist/lib/tools/ripgrep.d.ts +61 -0
  70. package/dist/lib/tools/ripgrep.d.ts.map +1 -0
  71. package/dist/lib/tools/ripgrep.js +217 -0
  72. package/package.json +1 -1
  73. package/src/commander.ts +11 -0
  74. package/src/commands/callers.ts +90 -26
  75. package/src/commands/doctor.ts +95 -2
  76. package/src/commands/grep.ts +737 -113
  77. package/src/commands/init.ts +138 -17
  78. package/src/commands/workflow.ts +132 -0
  79. package/src/core/agents/cli.ts +11 -4
  80. package/src/core/agents/rules/claim-conflict.ts +26 -1
  81. package/src/core/agents/rules/stop-hook.ts +17 -0
  82. package/src/core/agents/state/heartbeat-projector.ts +13 -1
  83. package/src/core/agents/state/heartbeat-writer.ts +30 -5
  84. package/src/core/config.ts +47 -0
  85. package/src/core/hooks/cli.ts +30 -3
  86. package/src/core/hooks/events/schema.ts +4 -0
  87. package/src/core/hooks/harness/events.ts +8 -0
  88. package/src/core/hooks/resolve/coord-root.ts +28 -6
  89. package/src/core/workflow/billing.ts +146 -0
  90. package/src/core/workflow/child-env.ts +47 -0
  91. package/src/core/workflow/engine.ts +394 -0
  92. package/src/core/workflow/harnesses.ts +38 -0
  93. package/src/core/workflow/spawn-claude.ts +99 -0
  94. package/src/core/workflow/spawn-codex.ts +74 -0
  95. package/src/core/workflow/spawn-cursor.ts +89 -0
  96. package/src/core/workflow/types.ts +153 -0
  97. package/src/core/workflow/validate.ts +75 -0
  98. package/src/lib/tools/ripgrep.ts +244 -0
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Workflow engine contracts. A workflow is a small throwaway JS script with
3
+ * bounded, schema-gated stages that fan work out to headless harness-CLI
4
+ * subagents; the SCRIPT (deterministic code), not any model, decides routing
5
+ * between stages, and the run always terminates when the script returns.
6
+ *
7
+ * Design record: decision 0015 (portable coordination-aware workflows).
8
+ */
9
+
10
+ import type { BillingMode, BillingProber } from "./billing.ts";
11
+
12
+ /** JSON-schema *subset* accepted by stage gates (see validate.ts). */
13
+ export interface StageSchema {
14
+ type: "object" | "array" | "string" | "number" | "boolean";
15
+ /** type=object */
16
+ properties?: Record<string, StageSchema>;
17
+ required?: string[];
18
+ /** type=array */
19
+ items?: StageSchema;
20
+ /** any type: closed value set (compared with ===) */
21
+ enum?: Array<string | number | boolean>;
22
+ }
23
+
24
+ export interface AgentOpts {
25
+ /** Stage gate: when present, the agent's reply must strict-parse as JSON and
26
+ * validate; the engine retries with the validation error appended, up to
27
+ * `maxAttempts`. Without it, `agent()` resolves to the raw reply text. */
28
+ schema?: StageSchema;
29
+ /** Model slug passed through to the harness CLI (default: the CLI's default). */
30
+ model?: string;
31
+ /** Attempt ceiling for the schema-retry loop (default 2). */
32
+ maxAttempts?: number;
33
+ /** Subprocess timeout ms (default 300_000). */
34
+ timeoutMs?: number;
35
+ /** Harness-turn ceiling for the child (default 25; use 1 for pure
36
+ * classification stages — cheaper and faster). */
37
+ maxTurns?: number;
38
+ /** Display label in the journal (default: prompt head). */
39
+ label?: string;
40
+ /** Which harness runs this agent (default: the run's default harness).
41
+ * Mixed-harness workflows are legal: triage on one CLI, deep work on
42
+ * another. */
43
+ harness?: HarnessName;
44
+ }
45
+
46
+ export type HarnessName = "claude-code" | "codex" | "cursor";
47
+
48
+ /** What a spawn adapter returns for one subagent run. */
49
+ export interface SpawnResult {
50
+ ok: boolean;
51
+ /** The model's final reply text (envelope-unwrapped). */
52
+ text: string;
53
+ /** Child harness session id when the envelope carries one. */
54
+ sessionId?: string;
55
+ costUsd?: number;
56
+ durationMs: number;
57
+ /** Populated when ok=false. */
58
+ error?: string;
59
+ }
60
+
61
+ export interface SpawnRequest {
62
+ prompt: string;
63
+ model?: string;
64
+ timeoutMs: number;
65
+ maxTurns: number;
66
+ cwd: string;
67
+ /** Run id, stamped into the child env (HARNERY_WORKFLOW_RUN_ID) so the
68
+ * coord layer can associate child sessions with their workflow run. */
69
+ runId?: string;
70
+ /** Scrub all API-key vars from the child env so it can only authenticate
71
+ * via its stored (subscription) login. See billing.ts. */
72
+ subscriptionOnly?: boolean;
73
+ }
74
+
75
+ /** One headless-subagent runner. The engine is adapter-agnostic; claude-code
76
+ * ships first, codex/cursor land behind the same signature (plan Phase 4). */
77
+ export type Spawner = (req: SpawnRequest) => Promise<SpawnResult>;
78
+
79
+ /** The API surface injected into a workflow script's default export. Explicit
80
+ * injection (no ambient globals): keeps scripts portable and unit-testable. */
81
+ export interface WorkflowContext {
82
+ /** Spawn one subagent; resolves to validated JSON (schema) or reply text. */
83
+ agent: (prompt: string, opts?: AgentOpts) => Promise<unknown>;
84
+ /** Run thunks with bounded concurrency; a rejected thunk resolves to null. */
85
+ parallel: <T>(thunks: Array<() => Promise<T>>) => Promise<Array<T | null>>;
86
+ /** Declare the current stage (journal + progress grouping). */
87
+ stage: (title: string) => void;
88
+ /** Narrate progress (stderr + journal). */
89
+ log: (message: string) => void;
90
+ }
91
+
92
+ export interface WorkflowMeta {
93
+ name: string;
94
+ description?: string;
95
+ }
96
+
97
+ /** Loaded script shape: `export const meta` + `export default async (ctx) => …`. */
98
+ export interface WorkflowModule {
99
+ meta?: WorkflowMeta;
100
+ default: (ctx: WorkflowContext) => Promise<unknown>;
101
+ }
102
+
103
+ export interface EngineOpts {
104
+ /** Repo root whose .harnery/ receives the run journal. */
105
+ coordRoot: string;
106
+ /** Spawner registry keyed by harness. A single-harness caller registers one
107
+ * entry and names it in `defaultHarness`. */
108
+ spawners: Partial<Record<HarnessName, Spawner>>;
109
+ /** Harness used when an agent() call doesn't name one (default "claude-code"). */
110
+ defaultHarness?: HarnessName;
111
+ /** Resume: run id of a prior run whose journal supplies cached results.
112
+ * agent() calls whose (stage, prompt, model, maxTurns, schema) key matches a
113
+ * completed prior agent return the journaled result without spawning. */
114
+ resumeFrom?: string;
115
+ /** Total-agent ceiling for the run (default 50): the runaway backstop. */
116
+ maxAgents?: number;
117
+ /** Concurrent-subagent cap for parallel() (default 4). */
118
+ concurrency?: number;
119
+ /** Working directory children spawn in (default: coordRoot). */
120
+ cwd?: string;
121
+ /** Progress sink (default: process.stderr). */
122
+ onLog?: (line: string) => void;
123
+ /** Guarantee subscription billing: API-key vars are scrubbed from every
124
+ * child env, and a harness whose stored login is provably absent fails
125
+ * loud before spawning. */
126
+ subscriptionOnly?: boolean;
127
+ /** Permit the api-key-override billing state (an exported API key silently
128
+ * shadowing a stored subscription login), which the engine otherwise
129
+ * refuses. Deliberate key-only hosts don't need this — only the
130
+ * both-present case does. */
131
+ allowApiBilling?: boolean;
132
+ /** Billing-probe override for tests (default: the real probeBilling). */
133
+ probeBilling?: BillingProber;
134
+ }
135
+
136
+ export interface RunReport {
137
+ runId: string;
138
+ name: string;
139
+ /** What the script's default export returned. */
140
+ result: unknown;
141
+ agentsSpawned: number;
142
+ /** agent() calls satisfied from the resumeFrom journal without spawning. */
143
+ agentsCached: number;
144
+ costUsd: number;
145
+ durationMs: number;
146
+ journalPath: string;
147
+ /** Estimated tokens of repo instructions (CLAUDE.md/AGENTS.md at the child
148
+ * cwd) that EVERY child cache-writes on spawn — the fixed per-child context
149
+ * overhead a fan-out multiplies. bytes/4 heuristic; 0 when no such file. */
150
+ contextTokensPerChildEstimate: number;
151
+ /** Billing mode per harness actually used this run (probed on first use). */
152
+ billing: Array<{ harness: HarnessName; mode: BillingMode }>;
153
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Minimal validator for the StageSchema JSON-schema subset. Deliberately tiny:
3
+ * a full JSON Schema implementation would pull a dependency (ajv) for
4
+ * validation depth workflow gates don't need. Supported: type, properties,
5
+ * required, items, enum. Returns a list of human-readable problems (empty =
6
+ * valid) so the engine can feed failures back into the retry prompt verbatim.
7
+ */
8
+
9
+ import type { StageSchema } from "./types.ts";
10
+
11
+ export function validateAgainstSchema(value: unknown, schema: StageSchema, path = "$"): string[] {
12
+ const problems: string[] = [];
13
+
14
+ if (schema.enum) {
15
+ if (!schema.enum.some((v) => v === value)) {
16
+ problems.push(`${path}: expected one of ${JSON.stringify(schema.enum)}, got ${short(value)}`);
17
+ }
18
+ return problems; // enum is exhaustive; type check is implied by membership
19
+ }
20
+
21
+ switch (schema.type) {
22
+ case "object": {
23
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
24
+ return [`${path}: expected object, got ${short(value)}`];
25
+ }
26
+ const obj = value as Record<string, unknown>;
27
+ for (const key of schema.required ?? []) {
28
+ if (!(key in obj)) problems.push(`${path}.${key}: required property missing`);
29
+ }
30
+ for (const [key, sub] of Object.entries(schema.properties ?? {})) {
31
+ if (key in obj) problems.push(...validateAgainstSchema(obj[key], sub, `${path}.${key}`));
32
+ }
33
+ return problems;
34
+ }
35
+ case "array": {
36
+ if (!Array.isArray(value)) return [`${path}: expected array, got ${short(value)}`];
37
+ if (schema.items) {
38
+ for (const [i, item] of value.entries()) {
39
+ problems.push(
40
+ ...validateAgainstSchema(item, schema.items as StageSchema, `${path}[${i}]`),
41
+ );
42
+ }
43
+ }
44
+ return problems;
45
+ }
46
+ case "string":
47
+ case "number":
48
+ case "boolean": {
49
+ if (typeof value !== schema.type) {
50
+ problems.push(`${path}: expected ${schema.type}, got ${short(value)}`);
51
+ }
52
+ return problems;
53
+ }
54
+ default:
55
+ return [`${path}: unsupported schema type ${String((schema as { type?: unknown }).type)}`];
56
+ }
57
+ }
58
+
59
+ /** Strip accidental markdown code fences, then strict-parse JSON. */
60
+ export function parseStageOutput(text: string): { value?: unknown; error?: string } {
61
+ const stripped = text
62
+ .trim()
63
+ .replace(/^```(?:json)?\s*/i, "")
64
+ .replace(/\s*```$/, "");
65
+ try {
66
+ return { value: JSON.parse(stripped) };
67
+ } catch (err) {
68
+ return { error: `not valid JSON: ${(err as Error).message}` };
69
+ }
70
+ }
71
+
72
+ function short(value: unknown): string {
73
+ const s = JSON.stringify(value);
74
+ return s === undefined ? String(value) : s.length > 60 ? `${s.slice(0, 60)}…` : s;
75
+ }
@@ -0,0 +1,244 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import { createHash } from "node:crypto";
3
+ import {
4
+ chmodSync,
5
+ existsSync,
6
+ mkdirSync,
7
+ renameSync,
8
+ rmSync,
9
+ statSync,
10
+ writeFileSync,
11
+ } from "node:fs";
12
+ import { readFile, stat } from "node:fs/promises";
13
+ import { homedir, tmpdir } from "node:os";
14
+ import { join } from "node:path";
15
+ import { resolveBinName, ripgrepAutoInstall } from "../../core/config.ts";
16
+ import { coordEnv } from "../env.ts";
17
+
18
+ /**
19
+ * Managed ripgrep provisioning for `grep --files` / content search.
20
+ *
21
+ * harnery can install a pinned, checksum-verified ripgrep into its own tools
22
+ * directory (`$XDG_DATA_HOME/harnery/tools`, default `~/.local/share/harnery/
23
+ * tools`) so `harn grep` gets the fast engine even on machines where nobody
24
+ * ever installed rg. The download is version-pinned and every artifact's
25
+ * sha256 is baked in below (cross-checked against the official release
26
+ * checksums), so the install is reproducible, never "whatever is latest."
27
+ *
28
+ * Consent model: the installer only runs automatically when the host project
29
+ * opts in via `.harnery/config.jsonc` `{ "tools": { "ripgrep": { "autoInstall":
30
+ * true } } }` — a repo commits that once and every clone self-heals. Without
31
+ * the opt-in, a missing rg produces a rate-limited stderr hint naming
32
+ * `harn doctor --fix` (explicit install). Environments that forbid downloads
33
+ * lose nothing: every failure path falls back to GNU grep.
34
+ */
35
+
36
+ export const RG_VERSION = "14.1.1";
37
+
38
+ /** sha256 pins verified against the official ripgrep release .sha256 assets. */
39
+ const ARTIFACTS: Record<string, { name: string; sha256: string }> = {
40
+ "linux-x64": {
41
+ name: `ripgrep-${RG_VERSION}-x86_64-unknown-linux-musl.tar.gz`,
42
+ sha256: "4cf9f2741e6c465ffdb7c26f38056a59e2a2544b51f7cc128ef28337eeae4d8e",
43
+ },
44
+ "linux-arm64": {
45
+ name: `ripgrep-${RG_VERSION}-aarch64-unknown-linux-gnu.tar.gz`,
46
+ sha256: "c827481c4ff4ea10c9dc7a4022c8de5db34a5737cb74484d62eb94a95841ab2f",
47
+ },
48
+ "darwin-x64": {
49
+ name: `ripgrep-${RG_VERSION}-x86_64-apple-darwin.tar.gz`,
50
+ sha256: "fc87e78f7cb3fea12d69072e7ef3b21509754717b746368fd40d88963630e2b3",
51
+ },
52
+ "darwin-arm64": {
53
+ name: `ripgrep-${RG_VERSION}-aarch64-apple-darwin.tar.gz`,
54
+ sha256: "24ad76777745fbff131c8fbc466742b011f925bfa4fffa2ded6def23b5b937be",
55
+ },
56
+ };
57
+
58
+ function platformKey(): string {
59
+ return `${process.platform}-${process.arch}`;
60
+ }
61
+
62
+ /** True when a pinned artifact exists for this OS/arch (Windows: not yet). */
63
+ export function rgInstallSupported(): boolean {
64
+ return platformKey() in ARTIFACTS;
65
+ }
66
+
67
+ /** Machine-level harnery tools dir (NOT the per-project .harnery/ state dir). */
68
+ export function toolsDir(): string {
69
+ const xdg = process.env.XDG_DATA_HOME;
70
+ const base = xdg && xdg.trim() !== "" ? xdg : join(homedir(), ".local", "share");
71
+ return join(base, "harnery", "tools");
72
+ }
73
+
74
+ /** Path the managed rg binary lives at once installed. */
75
+ export function managedRgPath(): string {
76
+ return join(toolsDir(), "rg");
77
+ }
78
+
79
+ function isExecutable(p: string): boolean {
80
+ try {
81
+ const r = spawnSync(p, ["--version"], { stdio: "ignore" });
82
+ return r.status === 0;
83
+ } catch {
84
+ return false;
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Locate a usable ripgrep. Precedence:
90
+ * 1. HARNERY_RG_PATH (explicit override)
91
+ * 2. the managed tools-dir install (works even when PATH doesn't carry it)
92
+ * 3. `rg` on PATH
93
+ * Returns the spawnable path/name, or null when none respond to --version.
94
+ */
95
+ export function findRg(): string | null {
96
+ const override = coordEnv("RG_PATH");
97
+ if (override && override.trim() !== "") {
98
+ return isExecutable(override) ? override : null;
99
+ }
100
+ const managed = managedRgPath();
101
+ if (existsSync(managed) && isExecutable(managed)) return managed;
102
+ if (isExecutable("rg")) return "rg";
103
+ return null;
104
+ }
105
+
106
+ /**
107
+ * Download + verify + install the pinned ripgrep into the tools dir.
108
+ * Atomic: extracts to a temp dir and renames the binary into place.
109
+ * Throws on unsupported platform, network failure, or checksum mismatch —
110
+ * callers are expected to fall back to grep and say why.
111
+ */
112
+ export async function installRg(log?: (line: string) => void): Promise<string> {
113
+ const key = platformKey();
114
+ const artifact = ARTIFACTS[key];
115
+ if (!artifact) {
116
+ throw new Error(
117
+ `no pinned ripgrep artifact for ${key} — install ripgrep manually (https://github.com/BurntSushi/ripgrep#installation)`,
118
+ );
119
+ }
120
+
121
+ const url = `https://github.com/BurntSushi/ripgrep/releases/download/${RG_VERSION}/${artifact.name}`;
122
+ log?.(`downloading ${artifact.name} (pinned ${RG_VERSION})...`);
123
+ const res = await fetch(url);
124
+ if (!res.ok) throw new Error(`download failed: HTTP ${res.status} for ${url}`);
125
+ const bytes = Buffer.from(await res.arrayBuffer());
126
+
127
+ const digest = createHash("sha256").update(bytes).digest("hex");
128
+ if (digest !== artifact.sha256) {
129
+ throw new Error(
130
+ `sha256 mismatch for ${artifact.name}: expected ${artifact.sha256}, got ${digest} — refusing to install`,
131
+ );
132
+ }
133
+
134
+ const work = join(tmpdir(), `harnery-rg-${process.pid}`);
135
+ rmSync(work, { recursive: true, force: true });
136
+ mkdirSync(work, { recursive: true });
137
+ try {
138
+ const tarball = join(work, artifact.name);
139
+ writeFileSync(tarball, bytes);
140
+ await extractTar(tarball, work);
141
+
142
+ const extractedRg = join(work, artifact.name.replace(/\.tar\.gz$/, ""), "rg");
143
+ const rgStat = await stat(extractedRg);
144
+ if (!rgStat.isFile()) throw new Error(`extracted archive missing rg binary at ${extractedRg}`);
145
+
146
+ mkdirSync(toolsDir(), { recursive: true });
147
+ const dest = managedRgPath();
148
+ // rename() can't cross filesystems (tmp is often tmpfs); copy + rename
149
+ // within the destination dir keeps the final placement atomic.
150
+ const staged = `${dest}.tmp-${process.pid}`;
151
+ writeFileSync(staged, await readFile(extractedRg));
152
+ chmodSync(staged, 0o755);
153
+ renameSync(staged, dest);
154
+
155
+ if (!isExecutable(dest)) {
156
+ rmSync(dest, { force: true });
157
+ throw new Error(`installed rg at ${dest} but it failed to execute`);
158
+ }
159
+ log?.(`installed ripgrep ${RG_VERSION} -> ${dest}`);
160
+ return dest;
161
+ } finally {
162
+ rmSync(work, { recursive: true, force: true });
163
+ }
164
+ }
165
+
166
+ function extractTar(tarball: string, cwd: string): Promise<void> {
167
+ return new Promise((resolveP, reject) => {
168
+ const proc = spawn("tar", ["xzf", tarball], { cwd, stdio: ["ignore", "ignore", "pipe"] });
169
+ let stderr = "";
170
+ proc.stderr.setEncoding("utf8");
171
+ proc.stderr.on("data", (c: string) => {
172
+ stderr += c;
173
+ });
174
+ proc.on("error", reject);
175
+ proc.on("close", (code) => {
176
+ if (code === 0) resolveP();
177
+ else reject(new Error(`tar exited ${code}: ${stderr.trim()}`));
178
+ });
179
+ });
180
+ }
181
+
182
+ export type SearchEngine = "rg" | "grep";
183
+
184
+ /**
185
+ * Shared engine resolver for the ripgrep-backed search commands (`grep`,
186
+ * `callers`). Returns the engine plus the spawnable rg path.
187
+ *
188
+ * `HARNERY_GREP_ENGINE=rg|grep` forces one; otherwise findRg() probes
189
+ * HARNERY_RG_PATH → the managed tools-dir install → PATH. On a miss, the
190
+ * host's `.harnery/config.jsonc` `tools.ripgrep.autoInstall` consent triggers
191
+ * a pinned, checksum-verified install into the harnery tools dir (any failure
192
+ * falls back to grep with a stderr note); without consent, a rate-limited
193
+ * stderr hint (keyed per `hintKey`) names the explicit install command.
194
+ */
195
+ export async function resolveSearchEngine(
196
+ hintKey = "grep",
197
+ ): Promise<{ engine: SearchEngine; rgBin: string }> {
198
+ const forced = coordEnv("GREP_ENGINE");
199
+ if (forced === "grep") return { engine: "grep", rgBin: "rg" };
200
+ const found = findRg();
201
+ if (found) return { engine: "rg", rgBin: found };
202
+ if (forced === "rg") {
203
+ // Forced rg with none findable: let the spawn error surface loudly.
204
+ return { engine: "rg", rgBin: "rg" };
205
+ }
206
+ if (rgInstallSupported()) {
207
+ if (ripgrepAutoInstall()) {
208
+ try {
209
+ const installed = await installRg((line) => process.stderr.write(`harnery: ${line}\n`));
210
+ return { engine: "rg", rgBin: installed };
211
+ } catch (err) {
212
+ process.stderr.write(
213
+ `harnery: ripgrep auto-install failed (${(err as Error).message}); using grep fallback\n`,
214
+ );
215
+ return { engine: "grep", rgBin: "rg" };
216
+ }
217
+ }
218
+ const bin = resolveBinName();
219
+ hintOncePerDay(
220
+ `${bin} ${hintKey}: ripgrep not found; using the slower GNU grep fallback. ` +
221
+ `Run \`${bin} doctor --fix\` to install it (pinned + checksum-verified), ` +
222
+ `or set { "tools": { "ripgrep": { "autoInstall": true } } } in .harnery/config.jsonc.`,
223
+ );
224
+ }
225
+ return { engine: "grep", rgBin: "rg" };
226
+ }
227
+
228
+ /**
229
+ * Emit `line` to stderr at most once per 24h (stamp file in the tools dir).
230
+ * Keeps the "rg missing" nudge from nagging on every single grep invocation.
231
+ */
232
+ export function hintOncePerDay(line: string): void {
233
+ try {
234
+ const stamp = join(toolsDir(), ".rg-hint-stamp");
235
+ if (existsSync(stamp) && Date.now() - statSync(stamp).mtimeMs < 24 * 60 * 60 * 1000) {
236
+ return;
237
+ }
238
+ mkdirSync(toolsDir(), { recursive: true });
239
+ writeFileSync(stamp, String(Date.now()));
240
+ } catch {
241
+ // stamping failed; still emit the hint
242
+ }
243
+ process.stderr.write(`${line}\n`);
244
+ }