@indigoai-us/hq-cli 5.85.3 → 5.87.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,27 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [5.87.0]
6
+
7
+ ### Added
8
+
9
+ - Added the hidden `hq core worker` subgroup for worker-scoped skill
10
+ maintenance: `hq core worker lint` fails when a skill shared by multiple
11
+ workers is duplicated by copy instead of single-sourced (or a shared-skill
12
+ symlink dangles), and `hq core worker share` migrates duplicated copies onto
13
+ one canonical file plus relative symlinks (dry-run by default; refuses
14
+ cross-scope paths and never silently clobbers a drifted copy). (#309)
15
+ - Added single-flight background reindex. (#308)
16
+
17
+ ## [5.86.0]
18
+
19
+ ### Added
20
+
21
+ - Added `hq search` and `hq index` commands for keyword, semantic, and hybrid
22
+ search across reconciled QMD collections, with explicit opt-in embedding and
23
+ package-local QMD resolution. Registered unmanaged collections remain
24
+ untouched and are reported as `registered (unmanaged)`. (#306)
25
+
5
26
  ## [5.85.3]
6
27
 
7
28
  ### Changed
@@ -0,0 +1,143 @@
1
+ #!/usr/bin/env bash
2
+ # lint-shared-worker-skills.sh — fail if a worker-scoped skill is duplicated by
3
+ # copy instead of being single-sourced through the shared-skills convention.
4
+ #
5
+ # THE DRIFT PROBLEM this guards against: a skill used by several workers used to
6
+ # be physically COPIED into each worker's skills/ directory. Editing one copy
7
+ # left the others stale, so the copies drifted apart over time (e.g. a shared
8
+ # e2e-testing skill splitting into two divergent versions). The fix is to keep
9
+ # ONE canonical file under a scope-appropriate `_shared-skills/` store and point
10
+ # each sharing worker's skill entry at it with a relative symlink — see
11
+ # core/knowledge/public/hq-core/shared-worker-skills.md.
12
+ #
13
+ # This linter makes that convention enforceable. It flags two failure shapes:
14
+ #
15
+ # DUPLICATE — two or more NON-symlink skill files, in any worker under the
16
+ # scanned roots, whose byte content is identical. Identical bytes
17
+ # across two real files is exactly an un-single-sourced copy: the
18
+ # moment someone edits one, they drift. (Symlinks that resolve to
19
+ # a shared canonical are single-sourced and are NOT flagged, even
20
+ # though their resolved content matches the canonical.)
21
+ # BROKEN — a skill entry that is a symlink whose target does not resolve.
22
+ #
23
+ # It deliberately does NOT flag two same-NAMED skills whose content differs
24
+ # (e.g. an API-level vs a browser-level e2e skill): distinct content means they
25
+ # are distinct skills that merely share a filename, not a drifted share.
26
+ #
27
+ # Usage: lint-shared-worker-skills.sh [root ...]
28
+ # Default roots: core/workers core/packages (the SHIPPED hq-core scope —
29
+ # company `_shared-skills/` stores live under companies/<co>/ and are linted
30
+ # per-tenant, not here).
31
+ #
32
+ # Exit 0 + "OK:" line when clean; exit 1 + a report naming every offending group
33
+ # when not. Matches the loud-and-specific style of lint-skill-script-refs.sh.
34
+
35
+ set -euo pipefail
36
+
37
+ repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
38
+ cd "$repo_root"
39
+
40
+ roots=("$@")
41
+ if [[ ${#roots[@]} -eq 0 ]]; then
42
+ roots=(core/workers core/packages)
43
+ fi
44
+
45
+ # Content hash of a file, portable across Linux and macOS (neither guaranteed to
46
+ # ship the other's tool): md5sum (GNU) → md5 (BSD/macOS) → shasum (Perl, both).
47
+ content_hash() {
48
+ if command -v md5sum >/dev/null 2>&1; then
49
+ md5sum "$1" | cut -d' ' -f1
50
+ elif command -v md5 >/dev/null 2>&1; then
51
+ md5 -q "$1"
52
+ else
53
+ shasum "$1" | cut -d' ' -f1
54
+ fi
55
+ }
56
+
57
+ # Collect skill entries: regular files AND symlinks named *.md under any
58
+ # .../skills/... path. -type l must be matched explicitly — a symlink is not a
59
+ # -type f, so a skills symlink would otherwise be invisible to the scan.
60
+ mapfile -t entries < <(
61
+ find "${roots[@]}" \( -type f -o -type l \) -path '*/skills/*.md' 2>/dev/null | sort
62
+ )
63
+
64
+ # Empty is clean — and guards `"${entries[@]}"` under `set -u` on bash 3.2
65
+ # (macOS), which errors on an empty array expansion.
66
+ if [[ ${#entries[@]} -eq 0 ]]; then
67
+ echo "OK: worker-scoped skills are single-sourced (0 skill entries under: ${roots[*]})"
68
+ exit 0
69
+ fi
70
+
71
+ broken=()
72
+ # Parallel arrays keyed by content hash: hash_keys[i] is a hash, and
73
+ # hash_regfiles[i] is a newline-joined list of the NON-symlink files with that
74
+ # hash. Bash 3.2 (macOS) has no associative arrays in a portable-guaranteed way,
75
+ # so a linear scan over parallel arrays keeps this runnable everywhere HQ runs.
76
+ hash_keys=()
77
+ hash_regfiles=()
78
+
79
+ hash_index() { # echo the index of $1 in hash_keys, or -1
80
+ local want="$1" i
81
+ for i in "${!hash_keys[@]}"; do
82
+ if [[ "${hash_keys[$i]}" == "$want" ]]; then
83
+ echo "$i"; return 0
84
+ fi
85
+ done
86
+ echo "-1"
87
+ }
88
+
89
+ for entry in "${entries[@]}"; do
90
+ if [[ -L "$entry" ]]; then
91
+ # Symlink: single-sourced by design. Only a DANGLING one is a problem.
92
+ if [[ ! -e "$entry" ]]; then
93
+ broken+=("$entry")
94
+ fi
95
+ continue
96
+ fi
97
+ # Regular file: hash its bytes and bucket it. Two regular files sharing a
98
+ # bucket are two copies of the same skill — the drift hazard.
99
+ h="$(content_hash "$entry")"
100
+ idx="$(hash_index "$h")"
101
+ if [[ "$idx" == "-1" ]]; then
102
+ hash_keys+=("$h")
103
+ hash_regfiles+=("$entry")
104
+ else
105
+ hash_regfiles[$idx]="${hash_regfiles[$idx]}"$'\n'"$entry"
106
+ fi
107
+ done
108
+
109
+ findings=0
110
+
111
+ for i in "${!hash_keys[@]}"; do
112
+ group="${hash_regfiles[$i]}"
113
+ count="$(printf '%s\n' "$group" | grep -c .)"
114
+ if [[ "$count" -ge 2 ]]; then
115
+ if [[ $findings -eq 0 ]]; then
116
+ echo "lint-shared-worker-skills: FAIL — duplicated worker skills (single-source these via _shared-skills/ + relative symlinks):" >&2
117
+ fi
118
+ findings=$((findings + 1))
119
+ echo " DUPLICATE (identical content, ${count} copies — pick one canonical and symlink the rest):" >&2
120
+ printf ' %s\n' "$group" >&2
121
+ fi
122
+ done
123
+
124
+ if [[ ${#broken[@]} -gt 0 ]]; then
125
+ if [[ $findings -eq 0 ]]; then
126
+ echo "lint-shared-worker-skills: FAIL — broken shared-skill symlink(s):" >&2
127
+ fi
128
+ findings=$((findings + ${#broken[@]}))
129
+ echo " BROKEN (symlink target does not resolve):" >&2
130
+ printf ' %s\n' "${broken[@]}" >&2
131
+ fi
132
+
133
+ if [[ $findings -gt 0 ]]; then
134
+ echo "" >&2
135
+ echo " Fix: keep ONE canonical file in the narrowest-scope _shared-skills/ store" >&2
136
+ echo " (core/workers/_shared-skills, core/packages/<pack>/workers/_shared-skills," >&2
137
+ echo " or companies/<co>/workers/_shared-skills) and replace each duplicate with a" >&2
138
+ echo " relative symlink. Helper: hq core worker share" >&2
139
+ echo " Doc: core/knowledge/public/hq-core/shared-worker-skills.md" >&2
140
+ exit 1
141
+ fi
142
+
143
+ echo "OK: worker-scoped skills are single-sourced (${#entries[@]} skill entries scanned under: ${roots[*]})"
@@ -0,0 +1,178 @@
1
+ #!/usr/bin/env bash
2
+ # share-worker-skill.sh — convert duplicated worker-scoped skill copies into a
3
+ # single canonical file plus relative symlinks, the migration step for the
4
+ # shared-worker-skills convention (core/knowledge/public/hq-core/shared-worker-skills.md).
5
+ #
6
+ # This performs the ONLY mechanical move the design needs: it never edits any
7
+ # worker.yaml (the skill name still resolves to the same path — only the file
8
+ # type changes from regular file to symlink), and it never touches a skill
9
+ # resolver. `/run` reads {worker}/skills/{skill}.md and follows the symlink
10
+ # transparently, so the change is purely file-layout.
11
+ #
12
+ # Usage:
13
+ # share-worker-skill.sh <scope-dir> <skill-name> <worker-skill-path> [<worker-skill-path> ...]
14
+ #
15
+ # <scope-dir> The narrowest scope that contains EVERY sharing worker,
16
+ # e.g. core/workers, core/packages/<pack>/workers, or
17
+ # companies/<co>/workers. The canonical file is created at
18
+ # <scope-dir>/_shared-skills/<skill-name>.md.
19
+ # <skill-name> Bare skill name (no .md), e.g. e2e-testing.
20
+ # <worker-skill-path> Each worker's current copy, e.g.
21
+ # core/workers/foo/skills/e2e-testing.md
22
+ #
23
+ # Safety rails:
24
+ # * Every worker path MUST live under <scope-dir> (no cross-scope symlinks,
25
+ # which would break on pack install / hq-sync). Refuses otherwise.
26
+ # * If no canonical exists yet, the FIRST worker path is promoted to canonical.
27
+ # * A worker copy whose content DIFFERS from the canonical is a DRIFTED copy:
28
+ # the script refuses and tells you to reconcile by hand first (the human
29
+ # decision the design reserves — never a silent overwrite). Re-run after
30
+ # reconciling, or pass --force to accept the canonical for that copy.
31
+ # * Idempotent: a path already symlinked to the canonical is left alone.
32
+ # * DRY-RUN BY DEFAULT. Pass --apply to make changes.
33
+
34
+ set -euo pipefail
35
+
36
+ APPLY=0
37
+ FORCE=0
38
+ args=()
39
+ for a in "$@"; do
40
+ case "$a" in
41
+ --apply) APPLY=1 ;;
42
+ --force) FORCE=1 ;;
43
+ -*) echo "unknown flag: $a" >&2; exit 2 ;;
44
+ *) args+=("$a") ;;
45
+ esac
46
+ done
47
+
48
+ if [[ ${#args[@]} -lt 3 ]]; then
49
+ echo "usage: $0 [--apply] [--force] <scope-dir> <skill-name> <worker-skill-path>..." >&2
50
+ exit 2
51
+ fi
52
+
53
+ repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
54
+ cd "$repo_root"
55
+
56
+ scope_dir="${args[0]%/}"
57
+ skill_name="${args[1]}"
58
+ worker_paths=("${args[@]:2}")
59
+
60
+ if [[ ! "$skill_name" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]]; then
61
+ echo "error: skill-name '$skill_name' is not a bare skill name (no slashes, no .md)" >&2
62
+ exit 2
63
+ fi
64
+ if [[ ! -d "$scope_dir" ]]; then
65
+ echo "error: scope dir does not exist: $scope_dir" >&2
66
+ exit 2
67
+ fi
68
+
69
+ canonical="$scope_dir/_shared-skills/$skill_name.md"
70
+
71
+ note() { printf '%s\n' "$*"; }
72
+ would() { if [[ $APPLY -eq 1 ]]; then note " did: $*"; else note " would: $*"; fi; }
73
+
74
+ # Normalize a path to repo-relative without requiring GNU realpath (macOS-safe).
75
+ rel_under() { # rel_under <path> <dir> → 0 if <path> is inside <dir>
76
+ local p="${1#./}" d="${2#./}"
77
+ [[ "$p" == "$d/"* ]]
78
+ }
79
+
80
+ # rel_path <from-dir> <to-file> → relative path from <from-dir> to <to-file>,
81
+ # computed purely from the two repo-relative strings (both already normalized,
82
+ # no `..` segments, no symlinks in the literal path). Portable: no python3, no
83
+ # GNU realpath --relative-to (macOS ships neither).
84
+ rel_path() {
85
+ local from="${1#./}" to="${2#./}"
86
+ local -a fa ta
87
+ IFS='/' read -r -a fa <<< "$from"
88
+ IFS='/' read -r -a ta <<< "$to"
89
+ local i=0
90
+ while [[ $i -lt ${#fa[@]} && $i -lt ${#ta[@]} && "${fa[$i]}" == "${ta[$i]}" ]]; do
91
+ i=$((i + 1))
92
+ done
93
+ local up="" j
94
+ for (( j=i; j<${#fa[@]}; j++ )); do up="../$up"; done
95
+ local down="" k
96
+ for (( k=i; k<${#ta[@]}; k++ )); do down="$down${ta[$k]}/"; done
97
+ down="${down%/}"
98
+ printf '%s%s' "$up" "$down"
99
+ }
100
+
101
+ # Every worker path must be inside the scope dir.
102
+ for wp in "${worker_paths[@]}"; do
103
+ wp="${wp#./}"
104
+ if ! rel_under "$wp" "$scope_dir"; then
105
+ echo "error: worker path '$wp' is not under scope dir '$scope_dir'" >&2
106
+ echo " canonical must live in the narrowest scope covering all sharers;" >&2
107
+ echo " cross-scope symlinks are disallowed. Aborting (no changes made)." >&2
108
+ exit 2
109
+ fi
110
+ done
111
+
112
+ note "Canonical: $canonical"
113
+ note "Scope: $scope_dir"
114
+ [[ $APPLY -eq 1 ]] || note "(dry run — pass --apply to make changes)"
115
+
116
+ # 1) Establish the canonical file.
117
+ if [[ -e "$canonical" && ! -L "$canonical" ]]; then
118
+ note "canonical already exists (regular file) — reusing it"
119
+ else
120
+ # Promote the first worker path that is a real (non-symlink) file.
121
+ seed=""
122
+ for wp in "${worker_paths[@]}"; do
123
+ if [[ -f "$wp" && ! -L "$wp" ]]; then seed="$wp"; break; fi
124
+ done
125
+ if [[ -z "$seed" ]]; then
126
+ echo "error: no canonical exists and no worker path is a regular file to seed it from" >&2
127
+ exit 2
128
+ fi
129
+ would "mkdir -p $scope_dir/_shared-skills"
130
+ would "git mv $seed $canonical (promote first copy to canonical)"
131
+ if [[ $APPLY -eq 1 ]]; then
132
+ mkdir -p "$scope_dir/_shared-skills"
133
+ git mv "$seed" "$canonical" 2>/dev/null || mv "$seed" "$canonical"
134
+ fi
135
+ fi
136
+
137
+ # 2) Point each worker path at the canonical via a relative symlink.
138
+ for wp in "${worker_paths[@]}"; do
139
+ wp="${wp#./}"
140
+ # Already the canonical file itself (post-promotion) → link it too so every
141
+ # worker slot is a symlink and the canonical lives only under _shared-skills.
142
+ target_dir="$(dirname "$wp")"
143
+ # Relative path from the worker's skills dir to the canonical (portable).
144
+ rel="$(rel_path "$target_dir" "$canonical")"
145
+
146
+ if [[ -L "$wp" ]]; then
147
+ cur="$(readlink "$wp")"
148
+ if [[ "$cur" == "$rel" ]]; then
149
+ note "ok: $wp already links to canonical"
150
+ continue
151
+ fi
152
+ would "relink $wp -> $rel (was: $cur)"
153
+ if [[ $APPLY -eq 1 ]]; then ln -sfn "$rel" "$wp"; fi
154
+ continue
155
+ fi
156
+
157
+ if [[ -e "$wp" ]]; then
158
+ # Regular file present — must match canonical or be reconciled first.
159
+ if cmp -s "$wp" "$canonical"; then
160
+ would "replace identical copy $wp with symlink -> $rel"
161
+ if [[ $APPLY -eq 1 ]]; then rm -f "$wp"; ln -s "$rel" "$wp"; fi
162
+ else
163
+ if [[ $FORCE -eq 1 ]]; then
164
+ would "FORCE replace DRIFTED copy $wp with symlink -> $rel (content discarded)"
165
+ if [[ $APPLY -eq 1 ]]; then rm -f "$wp"; ln -s "$rel" "$wp"; fi
166
+ else
167
+ echo " DRIFT: $wp differs from canonical — reconcile by hand, then re-run" >&2
168
+ echo " (diff $wp $canonical), or pass --force to accept canonical." >&2
169
+ exit 3
170
+ fi
171
+ fi
172
+ else
173
+ would "create symlink $wp -> $rel"
174
+ if [[ $APPLY -eq 1 ]]; then mkdir -p "$target_dir"; ln -s "$rel" "$wp"; fi
175
+ fi
176
+ done
177
+
178
+ note "done."
@@ -68,6 +68,21 @@ export type RebuildIndexTarget = ScaffoldAsset & {
68
68
  /** Rebuild target — `hq core rebuild-index <target>`. */
69
69
  target: string;
70
70
  };
71
+ export type WorkerSubcommand = ScaffoldAsset & {
72
+ /** Subcommand name under the `worker` group — `hq core worker <name>`. */
73
+ name: string;
74
+ };
75
+ /**
76
+ * The `hq core worker <name>` subgroup — worker-scoped skill maintenance.
77
+ *
78
+ * Both are cold, explicitly-invoked operations (a linter and a one-shot
79
+ * migration), which the scaffold-vs-cli-code-ownership policy permits in the
80
+ * CLI. They are `root: "cwd"` because each derives its root from the caller
81
+ * (`git rev-parse --show-toplevel || pwd`): the CLI must inject nothing, or it
82
+ * would retarget the lint at the CLI's own checkout instead of the HQ tree the
83
+ * operator is standing in. See core/knowledge/public/hq-core/shared-worker-skills.md.
84
+ */
85
+ export declare const WORKER_SUBCOMMANDS: WorkerSubcommand[];
71
86
  /**
72
87
  * The ten index rebuild targets. The command registration below intentionally
73
88
  * loops over this table for lookup, so adding a target never requires another
@@ -32,6 +32,30 @@ import { Option } from "commander";
32
32
  import { registerCoreCheckpointCommand } from "./core-checkpoint.js";
33
33
  import { resolveLiveRoot } from "../utils/hq-roots.js";
34
34
  import { runBundledScript } from "../utils/run-bundled-script.js";
35
+ /**
36
+ * The `hq core worker <name>` subgroup — worker-scoped skill maintenance.
37
+ *
38
+ * Both are cold, explicitly-invoked operations (a linter and a one-shot
39
+ * migration), which the scaffold-vs-cli-code-ownership policy permits in the
40
+ * CLI. They are `root: "cwd"` because each derives its root from the caller
41
+ * (`git rev-parse --show-toplevel || pwd`): the CLI must inject nothing, or it
42
+ * would retarget the lint at the CLI's own checkout instead of the HQ tree the
43
+ * operator is standing in. See core/knowledge/public/hq-core/shared-worker-skills.md.
44
+ */
45
+ export const WORKER_SUBCOMMANDS = [
46
+ {
47
+ name: "lint",
48
+ asset: "core/scripts/lint-shared-worker-skills.sh",
49
+ root: "cwd",
50
+ summary: "Fail on worker-scoped skills duplicated by copy instead of single-sourced",
51
+ },
52
+ {
53
+ name: "share",
54
+ asset: "core/scripts/share-worker-skill.sh",
55
+ root: "cwd",
56
+ summary: "Migrate duplicated worker skills onto one canonical file + relative symlinks",
57
+ },
58
+ ];
35
59
  /**
36
60
  * The ten index rebuild targets. The command registration below intentionally
37
61
  * loops over this table for lookup, so adding a target never requires another
@@ -181,6 +205,7 @@ export const SCAFFOLD_COMMANDS = [
181
205
  export const SCAFFOLD_ASSETS = [
182
206
  ...REBUILD_INDEX_TARGETS,
183
207
  ...SCAFFOLD_COMMANDS,
208
+ ...WORKER_SUBCOMMANDS,
184
209
  ];
185
210
  /**
186
211
  * Resolve the tree an entry runs against, and the cwd to run it in.
@@ -237,6 +262,27 @@ export function registerCoreCommands(program) {
237
262
  // This group primarily hosts manifest-driven bundled assets, but it also
238
263
  // hosts native TypeScript plumbing when a scaffold contract needs it.
239
264
  registerCoreCheckpointCommand(core);
265
+ // `hq core worker <name>` — a nested subgroup for worker-scoped skill
266
+ // maintenance. Nested (rather than flat `hq core worker-<name>`) so the two
267
+ // related operations read as one family.
268
+ const worker = core
269
+ .command("worker")
270
+ .description("Worker-scoped skill maintenance (lint, share)");
271
+ for (const entry of WORKER_SUBCOMMANDS) {
272
+ worker
273
+ .command(entry.name)
274
+ .description(entry.summary)
275
+ // Pure passthrough: the wrapped script owns its own argument grammar.
276
+ .allowUnknownOption()
277
+ .allowExcessArguments()
278
+ .helpOption(false)
279
+ .argument("[args...]", "arguments passed through to the script")
280
+ .action((args = [], _opts, cmd) => {
281
+ const scope = core.opts();
282
+ const operands = cmd.args.length > 0 ? cmd.args : args;
283
+ runEntry(entry, scope, operands);
284
+ });
285
+ }
240
286
  const runCommand = (entry, args = [], cmd, target) => {
241
287
  const scope = core.opts();
242
288
  // `cmd.args` is the authoritative operand list: with
@@ -0,0 +1,20 @@
1
+ import { Command } from 'commander';
2
+ import { type RunQmdOptions, type SearchCollection, type QmdProcessResult } from '../lib/search-index/index.js';
3
+ import { type BackgroundDependencies, type BackgroundResult, type BackgroundStatus } from '../lib/search-index/background.js';
4
+ export type SearchIndexDependencies = {
5
+ reconcileCollections: (hqRoot: string) => unknown;
6
+ deriveCollections: (hqRoot: string) => SearchCollection[];
7
+ listRegisteredCollections: (hqRoot: string, options?: RunQmdOptions) => Set<string>;
8
+ resolveQmdBin: () => string;
9
+ resolveQmdVersion: () => string | undefined;
10
+ runQmd: (args: string[], options?: RunQmdOptions) => QmdProcessResult;
11
+ runBackgroundLauncher?: (dependencies: BackgroundDependencies) => BackgroundResult;
12
+ runBackgroundWorker?: (dependencies: BackgroundDependencies) => BackgroundResult;
13
+ backgroundStatus?: (dependencies: BackgroundDependencies) => BackgroundStatus;
14
+ };
15
+ /** Incrementally update qmd, embedding only when an operator explicitly asks. */
16
+ export declare function syncSearchIndex(hqRoot: string, embed: boolean, dependencies?: SearchIndexDependencies): void;
17
+ export declare function collectionStatusLines(expected: SearchCollection[], registered: ReadonlySet<string>): string[];
18
+ export declare function collectionSummary(expected: SearchCollection[], registered: ReadonlySet<string>): string;
19
+ export declare function registerIndexCommand(program: Command, dependencies?: SearchIndexDependencies): void;
20
+ //# sourceMappingURL=index-cmd.d.ts.map
@@ -0,0 +1,110 @@
1
+ import { Option } from 'commander';
2
+ import { deriveCollections, listRegisteredCollections, reconcileCollections, resolveQmdBin, resolveQmdVersion, runQmd, } from '../lib/search-index/index.js';
3
+ import { backgroundStatus, defaultBackgroundDependencies, runBackgroundLauncher, runBackgroundWorker, } from '../lib/search-index/background.js';
4
+ import { findHqRoot } from '../utils/manifest.js';
5
+ const defaults = {
6
+ reconcileCollections,
7
+ deriveCollections,
8
+ listRegisteredCollections,
9
+ resolveQmdBin,
10
+ resolveQmdVersion,
11
+ runQmd,
12
+ runBackgroundLauncher,
13
+ runBackgroundWorker,
14
+ backgroundStatus,
15
+ };
16
+ /** Incrementally update qmd, embedding only when an operator explicitly asks. */
17
+ export function syncSearchIndex(hqRoot, embed, dependencies = defaults) {
18
+ dependencies.reconcileCollections(hqRoot);
19
+ dependencies.runQmd(['update'], { cwd: hqRoot });
20
+ if (embed)
21
+ dependencies.runQmd(['embed'], { cwd: hqRoot });
22
+ }
23
+ function resolveRoot(hqRoot) {
24
+ return hqRoot ?? findHqRoot();
25
+ }
26
+ function makeBackgroundDependencies(hqRoot, dependencies) {
27
+ return {
28
+ ...defaultBackgroundDependencies(hqRoot),
29
+ resolveQmdBin: dependencies.resolveQmdBin,
30
+ reconcileCollections: dependencies.reconcileCollections,
31
+ runQmd: dependencies.runQmd,
32
+ };
33
+ }
34
+ export function collectionStatusLines(expected, registered) {
35
+ const expectedNames = new Set(expected.map((collection) => collection.name));
36
+ const managed = expected.map((collection) => `${registered.has(collection.name) ? 'registered' : 'missing'} ${collection.name} ${collection.path}`);
37
+ const unmanaged = [...registered]
38
+ .filter((name) => !expectedNames.has(name))
39
+ .sort((left, right) => left.localeCompare(right))
40
+ .map((name) => `registered (unmanaged) ${name}`);
41
+ return [...managed, ...unmanaged];
42
+ }
43
+ export function collectionSummary(expected, registered) {
44
+ const expectedNames = new Set(expected.map((collection) => collection.name));
45
+ const unmanaged = [...registered].filter((name) => !expectedNames.has(name)).length;
46
+ return `collections: ${registered.size} registered; ${expected.length} expected; ${unmanaged} unmanaged`;
47
+ }
48
+ export function registerIndexCommand(program, dependencies = defaults) {
49
+ const index = program.command('index').description('Manage the local HQ search index');
50
+ index
51
+ .command('sync')
52
+ .description('Reconcile collections and incrementally update the qmd index')
53
+ .option('--embed', 'Also rebuild expensive semantic embeddings')
54
+ .option('--hq-root <path>', 'HQ root to index (defaults to auto-detected root)')
55
+ .action((options) => {
56
+ const hqRoot = resolveRoot(options.hqRoot);
57
+ syncSearchIndex(hqRoot, options.embed === true);
58
+ console.log(`Updated search index for ${hqRoot}${options.embed ? ' (including embeddings)' : ''}.`);
59
+ });
60
+ index
61
+ .command('collections')
62
+ .description('Show expected and registered qmd collections')
63
+ .option('--hq-root <path>', 'HQ root to inspect (defaults to auto-detected root)')
64
+ .action((options) => {
65
+ const hqRoot = resolveRoot(options.hqRoot);
66
+ const registered = dependencies.listRegisteredCollections(hqRoot);
67
+ for (const line of collectionStatusLines(dependencies.deriveCollections(hqRoot), registered))
68
+ console.log(line);
69
+ });
70
+ index
71
+ .command('background')
72
+ .description('Run a detached, single-flight qmd cleanup and reindex')
73
+ .option('--log <path>', 'Write worker output to this log file')
74
+ .addOption(new Option('--worker').hideHelp())
75
+ .option('--hq-root <path>', 'HQ root to index (defaults to auto-detected root)')
76
+ .action((options) => {
77
+ const hqRoot = resolveRoot(options.hqRoot);
78
+ const background = makeBackgroundDependencies(hqRoot, dependencies);
79
+ if (options.log)
80
+ background.env = { ...background.env, QMD_REINDEX_LOG: options.log };
81
+ const result = options.worker
82
+ ? (dependencies.runBackgroundWorker ?? runBackgroundWorker)(background)
83
+ : (dependencies.runBackgroundLauncher ?? runBackgroundLauncher)(background);
84
+ if (!options.worker && result.state === 'launched')
85
+ console.log(result.pid);
86
+ else if (!options.worker && (result.state === 'skipped-agent' || result.state === 'skipped'))
87
+ console.log(result.state);
88
+ });
89
+ index
90
+ .command('status')
91
+ .description('Show qmd binary, collection, and index status')
92
+ .option('--hq-root <path>', 'HQ root to inspect (defaults to auto-detected root)')
93
+ .action((options) => {
94
+ const hqRoot = resolveRoot(options.hqRoot);
95
+ const bin = dependencies.resolveQmdBin();
96
+ const registered = dependencies.listRegisteredCollections(hqRoot, { bin, cwd: hqRoot });
97
+ const expected = dependencies.deriveCollections(hqRoot);
98
+ const qmdStatus = dependencies.runQmd(['status'], { bin, cwd: hqRoot });
99
+ const qmdVersion = dependencies.resolveQmdVersion();
100
+ const background = (dependencies.backgroundStatus ?? backgroundStatus)(makeBackgroundDependencies(hqRoot, dependencies));
101
+ console.log(`qmd: ${bin}${qmdVersion ? ` (version ${qmdVersion})` : ''}`);
102
+ console.log(collectionSummary(expected, registered));
103
+ console.log(`background: lock ${background.lock}; last completed ${background.completedAt ?? 'never'}`);
104
+ if (qmdStatus.stdout)
105
+ process.stdout.write(qmdStatus.stdout);
106
+ if (qmdStatus.stderr)
107
+ process.stderr.write(qmdStatus.stderr);
108
+ });
109
+ }
110
+ //# sourceMappingURL=index-cmd.js.map
@@ -0,0 +1,12 @@
1
+ import { Command } from 'commander';
2
+ export type SearchMode = 'keyword' | 'semantic' | 'hybrid';
3
+ export type SearchOptions = {
4
+ mode?: SearchMode;
5
+ collection?: string;
6
+ count?: number;
7
+ json?: boolean;
8
+ };
9
+ export declare function buildSearchArgs(query: string, options?: SearchOptions): string[];
10
+ export declare function buildGetArgs(document: string, options?: Pick<SearchOptions, 'collection'>): string[];
11
+ export declare function registerSearchCommand(program: Command): void;
12
+ //# sourceMappingURL=search.d.ts.map
@@ -0,0 +1,48 @@
1
+ import { runQmd } from '../lib/search-index/index.js';
2
+ export function buildSearchArgs(query, options = {}) {
3
+ const command = { keyword: 'search', semantic: 'vsearch', hybrid: 'query' }[options.mode ?? 'keyword'];
4
+ const args = [command, query];
5
+ if (options.collection)
6
+ args.push('-c', options.collection);
7
+ if (options.count !== undefined)
8
+ args.push('-n', String(options.count));
9
+ if (options.json)
10
+ args.push('--json');
11
+ return args;
12
+ }
13
+ export function buildGetArgs(document, options = {}) {
14
+ const args = ['get', document];
15
+ if (options.collection)
16
+ args.push('-c', options.collection);
17
+ return args;
18
+ }
19
+ function relay(result) {
20
+ if (result.stdout)
21
+ process.stdout.write(result.stdout);
22
+ if (result.stderr)
23
+ process.stderr.write(result.stderr);
24
+ }
25
+ export function registerSearchCommand(program) {
26
+ const search = program.command('search').description('Search the local HQ qmd index');
27
+ search
28
+ .command('get <document>')
29
+ .description('Retrieve a qmd document by path or document id')
30
+ .option('-c, --collection <collection>', 'Restrict retrieval to a collection')
31
+ .action((document, options) => {
32
+ relay(runQmd(buildGetArgs(document, options)));
33
+ });
34
+ search
35
+ .command('<query>')
36
+ .description('Search the local HQ qmd index')
37
+ .option('--mode <mode>', 'Search mode: keyword, semantic, or hybrid', 'keyword')
38
+ .option('-c, --collection <collection>', 'Restrict search to a collection')
39
+ .option('-n, --count <count>', 'Maximum result count', (value) => Number(value))
40
+ .option('--json', 'Request machine-readable qmd output')
41
+ .action((query, options) => {
42
+ if (!['keyword', 'semantic', 'hybrid'].includes(options.mode ?? 'keyword')) {
43
+ throw new Error(`Unknown search mode '${options.mode}'. Expected keyword, semantic, or hybrid.`);
44
+ }
45
+ relay(runQmd(buildSearchArgs(query, options)));
46
+ });
47
+ }
48
+ //# sourceMappingURL=search.js.map
@@ -0,0 +1,39 @@
1
+ import { type QmdProcessResult, type RunQmdOptions } from './index.js';
2
+ export type BackgroundResult = {
3
+ state: 'skipped-agent' | 'skipped' | 'quiet' | 'busy' | 'completed' | 'update-failed';
4
+ } | {
5
+ state: 'launched';
6
+ pid: number;
7
+ };
8
+ export type BackgroundDependencies = {
9
+ env: NodeJS.ProcessEnv;
10
+ hqRoot: string;
11
+ now: () => number;
12
+ pid: number;
13
+ random: () => string;
14
+ isProcessAlive: (pid: number) => boolean;
15
+ resolveQmdBin: () => string;
16
+ reconcileCollections: (hqRoot: string) => unknown;
17
+ runQmd: (args: string[], options?: RunQmdOptions) => QmdProcessResult;
18
+ spawnWorker: (options: {
19
+ logPath: string;
20
+ }) => number;
21
+ /** Test seam for simulating a competing owner replacing the atomic record. */
22
+ afterOwnerPublish?: (ownerFile: string) => void;
23
+ };
24
+ export type BackgroundStatus = {
25
+ lock: 'held' | 'stale' | 'free';
26
+ completedAt?: number;
27
+ };
28
+ /** Defaults used by the CLI; tests supply every nondeterministic dependency. */
29
+ export declare function defaultBackgroundDependencies(hqRoot: string): BackgroundDependencies;
30
+ /** Match the shell forwarder's hosted-agent markers before looking up qmd. */
31
+ export declare function isHostedAgent(env?: NodeJS.ProcessEnv): boolean;
32
+ export declare function installWorkerCleanup(cleanup: () => void, processEvents?: Pick<NodeJS.Process, 'once'>, exit?: (code: number) => void): void;
33
+ /** Start a detached worker; this public entry never owns the qmd pipeline. */
34
+ export declare function runBackgroundLauncher(dependencies: BackgroundDependencies): BackgroundResult;
35
+ /** Run the single-flight cleanup → update → embed pipeline in a worker only. */
36
+ export declare function runBackgroundWorker(dependencies: BackgroundDependencies): BackgroundResult;
37
+ /** Report the background lock and latest successful completion for `hq index status`. */
38
+ export declare function backgroundStatus(dependencies: BackgroundDependencies): BackgroundStatus;
39
+ //# sourceMappingURL=background.d.ts.map