@indigoai-us/hq-cli 5.345.243 → 5.345.245

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.
@@ -42,7 +42,7 @@ import * as fs from "node:fs";
42
42
  import * as os from "node:os";
43
43
  import * as path from "node:path";
44
44
  // A cycle with hook-requests.ts, which dispatches to this module: each side uses the other only inside functions.
45
- import { spawnRequestProgram } from "./hook-requests.js";
45
+ import { fleetLocalSettingsUnderRoot, spawnRequestProgram } from "./hook-requests.js";
46
46
  /** The folder under the fleet home that holds the session-start caches. */
47
47
  export const FLEET_SESSION_START_CACHE_DIR = "cache/hook-session-start";
48
48
  /** The in-flight marker in `<root>/workspace/sessions/<session>/`. */
@@ -141,6 +141,51 @@ function writeAtomically(target, text) {
141
141
  return false;
142
142
  }
143
143
  }
144
+ async function runPinnedEngine(request, event, switches, options) {
145
+ const base = { ...(options.env ?? process.env) };
146
+ const home = options.home ?? base.HOME ?? os.homedir();
147
+ const engine = pinnedHookEngine(home);
148
+ if (!engine || !request.body)
149
+ return undefined;
150
+ // Only the note decides the adapter's switches: none of the daemon's own reach the engine.
151
+ for (const name of Object.keys(base))
152
+ if (name.startsWith("HQ_FLEET_LOCAL_") || name === "HQ_FLEET_HOME")
153
+ delete base[name];
154
+ const env = {
155
+ ...base, HQ_HOOK_REQUEST_ID: request.id,
156
+ ...(switches.fleetHome !== null ? { HQ_FLEET_HOME: switches.fleetHome } : {}),
157
+ ...(switches.skip.length ? { HQ_FLEET_LOCAL_HOOK_SKIP: switches.skip.join(" ") } : {}),
158
+ ...(switches.budget !== null ? { HQ_FLEET_LOCAL_HOOK_BUDGET_SECONDS: switches.budget } : {}),
159
+ HQ_FLEET_LOCAL_HELPER_LIGHT: switches.helperLight ? "1" : "0",
160
+ };
161
+ const body = request.body;
162
+ return new Promise(resolve => {
163
+ if (options.signal?.aborted) {
164
+ resolve({ code: 1, ended: true, stdout: "" });
165
+ return;
166
+ }
167
+ const { child, settle } = spawnRequestProgram(engine, ["fleet-settings", event, "--hq-root", request.root], {
168
+ cwd: request.root, env, windowsHide: true, stdio: ["pipe", "pipe", "ignore"],
169
+ }, { timeoutMs: options.timeoutMs ?? FLEET_SESSION_START_KILL_MS, killGraceMs: options.killGraceMs, signal: options.signal });
170
+ child.stdin?.on("error", () => undefined);
171
+ child.stdin?.end(body);
172
+ let out = "";
173
+ child.stdout?.setEncoding("utf8");
174
+ child.stdout?.on("data", (text) => { if (out.length < STDOUT_MAX_BYTES)
175
+ out += text; });
176
+ child.stdout?.on("error", () => undefined);
177
+ let settled = false;
178
+ const done = (code) => {
179
+ if (settled)
180
+ return;
181
+ settled = true;
182
+ const ended = settle();
183
+ resolve({ code: ended && code === 0 ? 1 : code, ended, stdout: out });
184
+ };
185
+ child.once("error", () => done(1));
186
+ child.once("close", code => done(code ?? 1));
187
+ });
188
+ }
144
189
  /** Run one `fleet-session-start` note. */
145
190
  export async function runFleetSessionStart(request, operation, options) {
146
191
  const cache = operation.fleetHome !== null && operation.cache !== null
@@ -156,48 +201,9 @@ export async function runFleetSessionStart(request, operation, options) {
156
201
  catch { /* gone, or not an empty folder */ }
157
202
  };
158
203
  try {
159
- const base = { ...(options.env ?? process.env) };
160
- const home = options.home ?? base.HOME ?? os.homedir();
161
- const engine = pinnedHookEngine(home);
162
- if (!engine || !request.body)
204
+ const ran = await runPinnedEngine(request, "SessionStart", operation, options);
205
+ if (!ran)
163
206
  return { code: 1 };
164
- // Only the note decides the adapter's switches: none of the daemon's own reach the engine.
165
- for (const name of Object.keys(base))
166
- if (name.startsWith("HQ_FLEET_LOCAL_") || name === "HQ_FLEET_HOME")
167
- delete base[name];
168
- const env = {
169
- ...base, HQ_HOOK_REQUEST_ID: request.id,
170
- ...(operation.fleetHome !== null ? { HQ_FLEET_HOME: operation.fleetHome } : {}),
171
- ...(operation.skip.length ? { HQ_FLEET_LOCAL_HOOK_SKIP: operation.skip.join(" ") } : {}),
172
- ...(operation.budget !== null ? { HQ_FLEET_LOCAL_HOOK_BUDGET_SECONDS: operation.budget } : {}),
173
- HQ_FLEET_LOCAL_HELPER_LIGHT: operation.helperLight ? "1" : "0",
174
- };
175
- const ran = await new Promise(resolve => {
176
- if (options.signal?.aborted) {
177
- resolve({ code: 1, ended: true, stdout: "" });
178
- return;
179
- }
180
- const { child, settle } = spawnRequestProgram(engine, ["fleet-settings", "SessionStart", "--hq-root", request.root], {
181
- cwd: request.root, env, windowsHide: true, stdio: ["pipe", "pipe", "ignore"],
182
- }, { timeoutMs: options.timeoutMs ?? FLEET_SESSION_START_KILL_MS, killGraceMs: options.killGraceMs, signal: options.signal });
183
- child.stdin?.on("error", () => undefined);
184
- child.stdin?.end(request.body);
185
- let out = "";
186
- child.stdout?.setEncoding("utf8");
187
- child.stdout?.on("data", (text) => { if (out.length < STDOUT_MAX_BYTES)
188
- out += text; });
189
- child.stdout?.on("error", () => undefined);
190
- let settled = false;
191
- const done = (code) => {
192
- if (settled)
193
- return;
194
- settled = true;
195
- const ended = settle();
196
- resolve({ code: ended && code === 0 ? 1 : code, ended, stdout: out });
197
- };
198
- child.once("error", () => done(1));
199
- child.once("close", code => done(code ?? 1));
200
- });
201
207
  // An engine ended at its limit (or a daemon stop) writes nothing, as a killed subshell's trap never ran.
202
208
  if (ran.ended)
203
209
  return { code: 1 };
@@ -211,4 +217,42 @@ export async function runFleetSessionStart(request, operation, options) {
211
217
  finish();
212
218
  }
213
219
  }
220
+ export const FLEET_LOCAL_DEFERRED_DIR = ".hq-fleet-local-deferred";
221
+ export function parseFleetLocalPrompt(kind, args, session) {
222
+ if (kind !== "fleet-local-prompt")
223
+ return undefined;
224
+ const keys = Object.keys(args).sort();
225
+ if (keys.join(",") !== "budget,deliver,helperLight,skip")
226
+ return undefined;
227
+ const { deliver, skip, budget, helperLight } = args;
228
+ if (typeof deliver !== "boolean" || (deliver && !plainName(session)))
229
+ return undefined;
230
+ if (!Array.isArray(skip) || skip.length > SKIP_MAX || !skip.every(word => typeof word === "string" && SKIP_WORD.test(word)))
231
+ return undefined;
232
+ if (budget !== null && (typeof budget !== "string" || !BUDGET.test(budget)))
233
+ return undefined;
234
+ if (typeof helperLight !== "boolean")
235
+ return undefined;
236
+ return { kind, deliver, skip: skip, budget, helperLight };
237
+ }
238
+ export async function runFleetLocalPrompt(request, operation, options) {
239
+ const ran = await runPinnedEngine(request, "UserPromptSubmit", { fleetHome: null, ...operation }, options);
240
+ if (!ran)
241
+ return { code: 1 };
242
+ const context = printedContext(ran.stdout);
243
+ if (ran.ended || ran.code !== 0 || !context || !operation.deliver || !plainName(request.session))
244
+ return { code: 0 };
245
+ let folder = request.root;
246
+ for (const part of ["workspace", "sessions", request.session, FLEET_LOCAL_DEFERRED_DIR]) {
247
+ folder = path.join(folder, part);
248
+ try {
249
+ fs.mkdirSync(folder, { mode: 0o700 });
250
+ }
251
+ catch { /* already there, or checked below */ }
252
+ if (!fleetLocalSettingsUnderRoot(request.root, folder) || fs.lstatSync(folder, { throwIfNoEntry: false })?.isDirectory() !== true)
253
+ return { code: 0 };
254
+ }
255
+ writeAtomically(path.join(folder, `${Date.now()}.${process.pid}.${request.id}.txt`), context);
256
+ return { code: 0 };
257
+ }
214
258
  //# sourceMappingURL=hook-requests-fleet-session-start.js.map
@@ -53,7 +53,7 @@ import { type ShareCandidate } from "./hook-requests-acl-suggest.js";
53
53
  import { type MeshOperation } from "./hook-requests-mesh.js";
54
54
  import { type MachineCheckResult, type RunMachineChecksOptions } from "./machine-checks.js";
55
55
  import { type MeshToolWritesOperation } from "./hook-requests-mesh-tool-writes.js";
56
- import { type FleetSessionStartOperation } from "./hook-requests-fleet-session-start.js";
56
+ import { type FleetLocalPromptOperation, type FleetSessionStartOperation } from "./hook-requests-fleet-session-start.js";
57
57
  import { type ConductLinkOperation } from "./hook-requests-conduct-link.js";
58
58
  import { type CodexSettingsOperation } from "./hook-requests-codex-settings.js";
59
59
  /** What the daemon is asked to do. A closed list, the same as `Kind` in daemon_req.rs. */
@@ -108,7 +108,7 @@ export type HookOperation = {
108
108
  disabled?: MeshSessionBindSwitch;
109
109
  }
110
110
  /** One machine check run by the daemon for a SessionStart where the hooks own the checks (the machine-check notes at the end of this file). */
111
- | MachineCheckOperation | MeshOperation | MeshToolWritesOperation | CodexSettingsOperation | FleetSessionStartOperation
111
+ | MachineCheckOperation | MeshOperation | MeshToolWritesOperation | CodexSettingsOperation | FleetSessionStartOperation | FleetLocalPromptOperation
112
112
  /** The Codex adapter's SubagentStop runs of the root's conduct-link hooks (hook-requests-conduct-link.ts). */
113
113
  | ConductLinkOperation;
114
114
  /**
@@ -62,7 +62,7 @@ import { executeAclSuggestion, parseShareCandidates } from "./hook-requests-acl-
62
62
  import { parseMeshOperation, runMeshReconcile, runMeshRegisterRetry } from "./hook-requests-mesh.js";
63
63
  import { MACHINE_CHECKS, machineCheckCollectOnlySupported, runMachineChecks } from "./machine-checks.js";
64
64
  import { parseMeshToolWrites, runMeshToolWrites } from "./hook-requests-mesh-tool-writes.js";
65
- import { parseFleetSessionStart, runFleetSessionStart } from "./hook-requests-fleet-session-start.js";
65
+ import { parseFleetLocalPrompt, parseFleetSessionStart, runFleetLocalPrompt, runFleetSessionStart } from "./hook-requests-fleet-session-start.js";
66
66
  import { isConductLinkKind, parseConductLink, runConductLink } from "./hook-requests-conduct-link.js";
67
67
  import { parseCodexSettings, runCodexSettings } from "./hook-requests-codex-settings.js";
68
68
  /** The longest note the Rust writer leaves (`NOTE_MAX_BYTES`). */
@@ -306,6 +306,12 @@ export function parseHookRequest(text, stem) {
306
306
  return undefined;
307
307
  operation = fleet;
308
308
  }
309
+ else if (value.kind === "fleet-local-prompt") {
310
+ const prompt = parseFleetLocalPrompt(value.kind, args, value.session);
311
+ if (!prompt)
312
+ return undefined;
313
+ operation = prompt;
314
+ }
309
315
  else if (isConductLinkKind(value.kind)) {
310
316
  const conduct = parseConductLink(value.kind, args, value.session);
311
317
  if (!conduct)
@@ -389,7 +395,8 @@ export function hookRequestHasBody(operation) {
389
395
  || operation.kind === "startwork-prefetch"
390
396
  || operation.kind === "fleet-local-settings"
391
397
  || operation.kind === "codex-settings"
392
- || operation.kind === "fleet-session-start";
398
+ || operation.kind === "fleet-session-start"
399
+ || operation.kind === "fleet-local-prompt";
393
400
  }
394
401
  /** The helper a plan-sync request runs, inside the HQ root. */
395
402
  export const PLAN_SYNC_HELPER = "core/scripts/session-project.sh";
@@ -424,7 +431,8 @@ export function hookRequestArguments(request) {
424
431
  // Run by hook-requests-fleet-session-start.ts with the pinned engine.
425
432
  // Not a CLI request: the Codex settings commands are started one by one (hook-requests-codex-settings.ts).
426
433
  case "codex-settings": return [];
427
- case "fleet-session-start": return [];
434
+ case "fleet-session-start":
435
+ case "fleet-local-prompt": return [];
428
436
  // Not a CLI request: the root's own conduct-link hook script is run (`runConductLink`).
429
437
  case "conduct-lanes-link-deliver":
430
438
  case "conduct-link-inherit": return [];
@@ -1625,6 +1633,8 @@ export function executeHookRequest(request, options) {
1625
1633
  return runMeshToolWrites(operation, { ...options, root: request.root });
1626
1634
  if (operation.kind === "fleet-session-start")
1627
1635
  return runFleetSessionStart(request, operation, options);
1636
+ if (operation.kind === "fleet-local-prompt")
1637
+ return runFleetLocalPrompt(request, operation, options);
1628
1638
  if (operation.kind === "codex-settings")
1629
1639
  return runCodexSettings(request, operation, { ...options, timeoutMs: options.timeoutMs ?? HOOK_REQUEST_KILL_MS, killGraceMs: options.killGraceMs ?? HOOK_REQUEST_KILL_GRACE_MS }).catch(() => ({ code: 1 }));
1630
1640
  const cliArgs = hookRequestArguments(request);