@indigoai-us/hq-cli 5.345.215 → 5.345.218
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 +168 -0
- package/README.md +46 -1
- package/assets/bot-workers/setup/skills/add-a-pack.md +3 -3
- package/assets/scaffold/core/scripts/hq-session.sh +4 -0
- package/assets/scaffold/core/scripts/lib/epochrealtime.sh +30 -0
- package/assets/scaffold/core/scripts/lib/work-mesh-enqueue.sh +12 -5
- package/dist/command-catalog.generated.d.ts +59 -4
- package/dist/command-catalog.generated.js +74 -4
- package/dist/command-help.js +3 -1
- package/dist/command-registration-plan.d.ts +6 -0
- package/dist/command-registration-plan.js +1 -0
- package/dist/commands/auth.d.ts +1 -1
- package/dist/commands/auth.js +11 -52
- package/dist/commands/bot.js +11 -4
- package/dist/commands/cloud-demote.d.ts +19 -4
- package/dist/commands/cloud-demote.js +85 -12
- package/dist/commands/cloud-provision.d.ts +5 -1
- package/dist/commands/cloud-provision.js +6 -2
- package/dist/commands/cloud.d.ts +37 -1
- package/dist/commands/cloud.js +110 -27
- package/dist/commands/core-hq-session-native.js +3 -0
- package/dist/commands/core.js +1 -0
- package/dist/commands/daemon.js +4 -1
- package/dist/commands/dm.js +7 -0
- package/dist/commands/doctor-usage.d.ts +8 -0
- package/dist/commands/doctor-usage.js +20 -0
- package/dist/commands/doctor.js +7 -1
- package/dist/commands/env.d.ts +18 -0
- package/dist/commands/env.js +82 -0
- package/dist/commands/install-global.js +3 -3
- package/dist/commands/lanes.js +100 -24
- package/dist/commands/logout.d.ts +7 -0
- package/dist/commands/logout.js +41 -34
- package/dist/commands/members.js +1 -1
- package/dist/commands/pack-start-bot.d.ts +1 -1
- package/dist/commands/pack-start-bot.js +2 -2
- package/dist/commands/pkg-update.js +1 -1
- package/dist/commands/sources.d.ts +8 -2
- package/dist/commands/sources.js +65 -9
- package/dist/commands/sync-narrow.js +1 -1
- package/dist/commands/team-sync.js +1 -1
- package/dist/commands/whoami.js +26 -1
- package/dist/data/command-states.json +22 -0
- package/dist/index.js +52 -4
- package/dist/lazy-commands.js +2 -1
- package/dist/lib/anywhere-person-setting.d.ts +2 -0
- package/dist/lib/anywhere-person-setting.js +6 -3
- package/dist/lib/billing/usage-card.d.ts +25 -4
- package/dist/lib/billing/usage-card.js +76 -9
- package/dist/lib/billing/usage-cost.d.ts +3 -3
- package/dist/lib/billing/usage-cost.js +40 -15
- package/dist/lib/billing/usage-only.js +4 -2
- package/dist/lib/billing/usage-rate-card.d.ts +2 -2
- package/dist/lib/billing/usage-rate-card.js +3 -0
- package/dist/lib/bot/fleet-cron.js +93 -6
- package/dist/lib/bot/inflight.d.ts +9 -0
- package/dist/lib/bot/inflight.js +10 -0
- package/dist/lib/bot/jobs/hot-signal-sync.js +12 -1
- package/dist/lib/bot/pack-link.d.ts +2 -2
- package/dist/lib/bot/pack-link.js +2 -2
- package/dist/lib/bot/runtime/grok-acp.d.ts +1 -0
- package/dist/lib/bot/runtime/grok-acp.js +26 -11
- package/dist/lib/bot/runtime/grok.js +1 -1
- package/dist/lib/daemon/flag-monitor.d.ts +8 -0
- package/dist/lib/daemon/flag-monitor.js +17 -6
- package/dist/lib/daemon/flag.d.ts +65 -1
- package/dist/lib/daemon/flag.js +190 -40
- package/dist/lib/daemon/hook-env.d.ts +32 -0
- package/dist/lib/daemon/hook-env.js +51 -0
- package/dist/lib/daemon/ops/policy.js +67 -8
- package/dist/lib/daemon/paths.d.ts +72 -2
- package/dist/lib/daemon/paths.js +154 -3
- package/dist/lib/daemon/run.js +32 -21
- package/dist/lib/daemon/selfcheck.d.ts +22 -0
- package/dist/lib/daemon/selfcheck.js +40 -1
- package/dist/lib/daemon/server.d.ts +3 -1
- package/dist/lib/daemon/server.js +7 -22
- package/dist/lib/daemon/state-file-switch.d.ts +57 -0
- package/dist/lib/daemon/state-file-switch.js +88 -0
- package/dist/lib/daemon/state-file.d.ts +198 -0
- package/dist/lib/daemon/state-file.js +419 -0
- package/dist/lib/daemon/timed-flag-monitors.d.ts +47 -0
- package/dist/lib/daemon/timed-flag-monitors.js +75 -0
- package/dist/lib/doctor/checks/host-core.d.ts +52 -0
- package/dist/lib/doctor/checks/host-core.js +337 -0
- package/dist/lib/doctor/checks/replay-recorder.d.ts +39 -0
- package/dist/lib/doctor/checks/replay-recorder.js +261 -0
- package/dist/lib/doctor/checks/sync-runtime.js +3 -3
- package/dist/lib/doctor/registry.js +4 -0
- package/dist/lib/environment/credential-shapes.d.ts +61 -0
- package/dist/lib/environment/credential-shapes.js +333 -0
- package/dist/lib/environment/profile.d.ts +239 -0
- package/dist/lib/environment/profile.js +239 -0
- package/dist/lib/hook-engine/coverage.d.ts +13 -0
- package/dist/lib/hook-engine/coverage.js +124 -0
- package/dist/lib/hook-engine/reconcile.js +300 -94
- package/dist/lib/hook-engine/registration.d.ts +17 -1
- package/dist/lib/hook-engine/registration.js +62 -7
- package/dist/lib/hot-channels/adapters/slack.d.ts +21 -5
- package/dist/lib/hot-channels/adapters/slack.js +64 -21
- package/dist/lib/hot-channels/alert.d.ts +2 -0
- package/dist/lib/hot-channels/alert.js +8 -1
- package/dist/lib/hot-channels/contract.d.ts +12 -0
- package/dist/lib/hot-channels/contract.js +7 -0
- package/dist/lib/hot-channels/loop.js +1 -0
- package/dist/lib/hot-channels/run.d.ts +6 -0
- package/dist/lib/hot-channels/run.js +4 -0
- package/dist/lib/hot-channels/signals-pass.d.ts +64 -1
- package/dist/lib/hot-channels/signals-pass.js +21 -49
- package/dist/lib/install/claude-targets.d.ts +12 -1
- package/dist/lib/install/claude-targets.js +70 -5
- package/dist/lib/install/codex-pack-contributions.js +1 -1
- package/dist/lib/install/codex-targets.d.ts +2 -2
- package/dist/lib/install/codex-targets.js +48 -5
- package/dist/lib/install/hook-target.d.ts +4 -1
- package/dist/lib/install/hook-target.js +14 -1
- package/dist/lib/lanes/canonical-host.d.ts +35 -0
- package/dist/lib/lanes/canonical-host.js +65 -0
- package/dist/lib/lanes/host-capacity.d.ts +30 -0
- package/dist/lib/lanes/host-capacity.js +29 -3
- package/dist/lib/mesh/live/daemon/run.d.ts +2 -0
- package/dist/lib/mesh/live/daemon/run.js +5 -2
- package/dist/lib/mesh/live/flush.d.ts +5 -1
- package/dist/lib/mesh/live/flush.js +12 -6
- package/dist/lib/mesh/live/reporting-privacy.d.ts +32 -0
- package/dist/lib/mesh/live/reporting-privacy.js +56 -1
- package/dist/lib/mesh/live/spool.d.ts +7 -0
- package/dist/lib/mesh/live/spool.js +25 -0
- package/dist/lib/packages/setup-block.d.ts +2 -2
- package/dist/lib/packages/setup-block.js +2 -2
- package/dist/lib/packages/start-bot.d.ts +1 -1
- package/dist/lib/packages/start-bot.js +1 -1
- package/dist/lib/person-setting-cache.d.ts +38 -0
- package/dist/lib/person-setting-cache.js +98 -0
- package/dist/lib/safe-store.d.ts +245 -0
- package/dist/lib/safe-store.js +777 -0
- package/dist/lib/signals/engine.d.ts +29 -2
- package/dist/lib/signals/engine.js +103 -4
- package/dist/lib/signals/reply.js +73 -18
- package/dist/lib/signals/steps/adapters.js +7 -2
- package/dist/lib/signals/steps/extract.d.ts +13 -0
- package/dist/lib/signals/steps/extract.js +84 -4
- package/dist/main.js +47 -4
- package/dist/schemas/hq-environment-profile.v1.schema.json +438 -0
- package/dist/types.d.ts +1 -1
- package/dist/utils/auth-expiry.d.ts +8 -0
- package/dist/utils/auth-expiry.js +53 -0
- package/dist/utils/callback-port-busy.d.ts +1 -1
- package/dist/utils/callback-port-busy.js +4 -4
- package/dist/utils/cli-telemetry.d.ts +21 -0
- package/dist/utils/cli-telemetry.js +34 -0
- package/dist/utils/command-usage.d.ts +275 -0
- package/dist/utils/command-usage.js +797 -0
- package/dist/utils/hook-trust.d.ts +7 -0
- package/dist/utils/hook-trust.js +34 -4
- package/dist/utils/host-registration.d.ts +179 -0
- package/dist/utils/host-registration.js +505 -0
- package/dist/utils/resident-package-preload.js +2 -0
- package/package.json +10 -8
- package/scripts/bench-core.mjs +1096 -0
- package/scripts/check-function-manifest.mjs +702 -0
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Checks the HQ function manifest (core/functions.yaml in hq-core-staging).
|
|
3
|
+
//
|
|
4
|
+
// The manifest lists every hq command, daemon function, hook and core script
|
|
5
|
+
// with exactly one owner. This check fails when:
|
|
6
|
+
// two-owners a function has more than one owner (an owner list, or the
|
|
7
|
+
// same id listed twice)
|
|
8
|
+
// unlisted something exists on disk that the manifest does not list
|
|
9
|
+
// reference something still names a function whose state is retiring or
|
|
10
|
+
// removed
|
|
11
|
+
// and on the supporting rules a manifest needs to stay usable: entry shape,
|
|
12
|
+
// a live function whose file is gone, a removed file that is back on disk.
|
|
13
|
+
//
|
|
14
|
+
// Usage:
|
|
15
|
+
// node scripts/check-function-manifest.mjs [options]
|
|
16
|
+
//
|
|
17
|
+
// --manifest <file> the manifest (default: <core-root>/core/functions.yaml)
|
|
18
|
+
// --core-root <dir> an hq-core-staging tree; checks its hooks and scripts
|
|
19
|
+
// --cli-root <dir> an hq-cli source tree; checks its commands and daemon
|
|
20
|
+
// files (default: the tree this script sits in, when it
|
|
21
|
+
// is a source checkout)
|
|
22
|
+
// --cli-package <dir> an installed @indigoai-us/hq-cli package instead of a
|
|
23
|
+
// source tree. The package may be older than the
|
|
24
|
+
// manifest, so only "the package has it and the
|
|
25
|
+
// manifest does not list it" is a failure
|
|
26
|
+
// --cli-states <file> also compare src/data/command-states.json with the
|
|
27
|
+
// manifest. A missing file is reported and passes
|
|
28
|
+
// --require-cli-states a missing --cli-states file fails instead
|
|
29
|
+
// --no-manifest run without a manifest (hq-cli pull request job: the
|
|
30
|
+
// manifest lives in another repository). Only the
|
|
31
|
+
// command-states file is checked against the hq-cli tree
|
|
32
|
+
// --json print the result as JSON
|
|
33
|
+
//
|
|
34
|
+
// Exit 0: no problem. Exit 1: at least one problem. Exit 2: usage error.
|
|
35
|
+
//
|
|
36
|
+
// Trees covered by the "exists on disk" rule
|
|
37
|
+
// hq-core-staging: .claude/hooks, core/hooks, .codex/hooks, .grok/hooks (kind
|
|
38
|
+
// hook) and core/scripts (kind script). Test folders (tests, __tests__,
|
|
39
|
+
// hook-corpus, fixtures, __fixtures__) and .gitkeep / .DS_Store files are
|
|
40
|
+
// outside the rule. Hooks under personal/, companies/<company>/ and
|
|
41
|
+
// core/packages/ are not core functions and are not covered.
|
|
42
|
+
// hq-cli: every command in src/command-catalog.generated.ts (kind command)
|
|
43
|
+
// and every source file under src/lib/daemon (kind daemon; each file must
|
|
44
|
+
// be the owner of at least one daemon entry). Test files are outside the rule.
|
|
45
|
+
//
|
|
46
|
+
// What counts as a reference to a retiring or removed function
|
|
47
|
+
// Searched: every text file of the core tree and of the hq-cli tree.
|
|
48
|
+
// Not counted: the manifest; the function's own file, its owner and bundled
|
|
49
|
+
// copies of it; files named in the entry's allowed_refs; for an entry with
|
|
50
|
+
// released_sha256, core/scripts/remove-retired-core-hooks.sh (it deletes the
|
|
51
|
+
// file from installs); changelogs
|
|
52
|
+
// (CHANGELOG*, changelog.d/, MIGRATION.md); anything under _archive/; test
|
|
53
|
+
// files and fixtures; generated listings (core drift baselines, the generated
|
|
54
|
+
// CLI reference, the scaffold freshness tables, *.generated.* files, the
|
|
55
|
+
// command-states file); comment lines in code and config files.
|
|
56
|
+
// hook, script: the full path; or the file name after a directory that does
|
|
57
|
+
// not contradict the function's own folder. A name with no directory, a
|
|
58
|
+
// directory held in a variable, or a partial directory counts only when no
|
|
59
|
+
// live function has the same file name and fits the same text. A dot
|
|
60
|
+
// folder written under the home folder (~/.codex/hooks.json) names a user
|
|
61
|
+
// file and is not counted.
|
|
62
|
+
// command: `hq <command path>` as text, through every alias spelling when
|
|
63
|
+
// the hq-cli catalog is available. Options between the words, argument
|
|
64
|
+
// arrays and commands built from variables are not found. With
|
|
65
|
+
// --no-manifest the owner is not known, so a file under src/ that
|
|
66
|
+
// registers the command (`command("<leaf>"`) counts as its own source.
|
|
67
|
+
// daemon: an op as a quoted string or after `daemon call`, outside
|
|
68
|
+
// src/lib/daemon. Other daemon functions: the owner path as text, or an
|
|
69
|
+
// import of the owner (a relative import is resolved from the importing
|
|
70
|
+
// file), in any file including other daemon files.
|
|
71
|
+
|
|
72
|
+
import { execFileSync } from "node:child_process";
|
|
73
|
+
import { existsSync, lstatSync, readFileSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
74
|
+
import path from "node:path";
|
|
75
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
76
|
+
|
|
77
|
+
const KINDS = ["command", "daemon", "hook", "script"];
|
|
78
|
+
const STATES = ["live", "trial", "retiring", "removed"];
|
|
79
|
+
const RUNTIMES = ["all", "claude", "codex", "grok", "cursor", "fleet", "windows"];
|
|
80
|
+
const HOT_PATHS = ["tool-call", "turn", "session", "hook-helper", "background", "cold"];
|
|
81
|
+
const KEYS = ["id", "kind", "owner", "runtimes", "hot_path", "state", "replaced_by", "released_sha256", "allowed_refs"];
|
|
82
|
+
|
|
83
|
+
const HOOK_DIRS = [".claude/hooks", "core/hooks", ".codex/hooks", ".grok/hooks"];
|
|
84
|
+
const SCRIPT_DIRS = ["core/scripts"];
|
|
85
|
+
const DAEMON_DIR = "src/lib/daemon";
|
|
86
|
+
const MANIFEST_REL = "core/functions.yaml";
|
|
87
|
+
// Deletes released copies of removed files from installs. It names them to
|
|
88
|
+
// delete them and never runs them.
|
|
89
|
+
const RETIRED_CLEANER = "core/scripts/remove-retired-core-hooks.sh";
|
|
90
|
+
const NOT_A_FUNCTION = new Set([".gitkeep", ".DS_Store"]);
|
|
91
|
+
const SCOPE_TEST_PART = /(^|\/)(tests|__tests__|hook-corpus|fixtures|__fixtures__)\//;
|
|
92
|
+
|
|
93
|
+
const CHANGELOG_RE = /(^|\/)(CHANGELOG[^/]*|changelog\.d\/.*|MIGRATION\.md)$/i;
|
|
94
|
+
const ARCHIVE_RE = /(^|\/)_archive\//;
|
|
95
|
+
const TEST_DIR_RE = /(^|\/)(tests?|__tests__|__testing__|__fixtures__|fixtures|__snapshots__|hook-corpus|hook-tests|corpus|differential|e2e)(\/|$)/;
|
|
96
|
+
const TEST_FILE_RE = /(\.test\.|\.spec\.|_test\.|-test\.|(^|\/)test-[^/]*$)/;
|
|
97
|
+
const LISTING_RE = /(^|\/)core\/workspace\/core-drift-baselines\/[^/]+\.json$|(^|\/)knowledge\/public\/hq-core\/hq-cli-reference\.md$|(^|\/)core\/scripts\/lint-shell-portability\.allow$|(^|\/)scripts\/scaffold-freshness[^/]*\.json$|\.generated\.[a-z]+$|(^|\/)src\/data\/command-states\.json$/;
|
|
98
|
+
const BACKUP_RE = /\.(hq-rescue|drift)-|\.bak$|\.orig$|~$/;
|
|
99
|
+
// Files that may hold a secret are never read.
|
|
100
|
+
const CREDENTIAL_RE = /(^|\/)\.env([^A-Za-z0-9_]|$)|(^|\/)\.(npmrc|netrc|git-credentials)$|(^|\/)\.(aws|ssh)(\/|$)|(^|\/)id_(rsa|dsa|ecdsa|ed25519)|(^|\/)(credentials|token)\.json$|\.(pem|key)$/i;
|
|
101
|
+
const SKIP_DIR_RE = /(^|\/)(node_modules|\.git|dist|target|coverage|\.pnpm-store|vendor)(\/|$)/;
|
|
102
|
+
const TEXT_EXT_RE = /\.(sh|bash|zsh|mjs|cjs|js|jsx|ts|mts|tsx|py|rb|md|mdx|mdc|json|jsonl|yaml|yml|toml|txt|rs|ps1|cmd|bat|plist|service|cfg|ini|html|awk|allow|deny)$/i;
|
|
103
|
+
const DOC_EXT_RE = /\.(md|mdx|mdc|txt|rst|html?)$/i;
|
|
104
|
+
const COMMENT_RE = /^\s*(#(?!!)|\/\/|\/\*|\*(\s|$)|<!--|rem\s|::\s)/i;
|
|
105
|
+
const SCRIPT_NAME_RE = /\.(sh|bash|mjs|cjs|js|py|ps1|cmd|awk)$/;
|
|
106
|
+
const MAX_FILE_BYTES = 2_000_000;
|
|
107
|
+
|
|
108
|
+
// ---------- small helpers ----------
|
|
109
|
+
const posix = (p) => p.split(path.sep).join("/");
|
|
110
|
+
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
111
|
+
|
|
112
|
+
function isGitRoot(root) {
|
|
113
|
+
try {
|
|
114
|
+
const top = execFileSync("git", ["-C", root, "rev-parse", "--show-toplevel"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim();
|
|
115
|
+
return path.resolve(top) === path.resolve(statSync(root).isDirectory() ? realPath(root) : root);
|
|
116
|
+
} catch {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function realPath(p) {
|
|
121
|
+
try { return execFileSync("pwd", ["-P"], { cwd: p, encoding: "utf8" }).trim(); } catch { return path.resolve(p); }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Files under `dirs` (repo-relative, forward slashes) that exist on disk. */
|
|
125
|
+
function listFiles(root, dirs) {
|
|
126
|
+
const present = dirs.filter((d) => existsSync(path.join(root, d)));
|
|
127
|
+
if (!present.length) return [];
|
|
128
|
+
if (isGitRoot(root)) {
|
|
129
|
+
// Tracked files plus new files that are not ignored, so a file added and
|
|
130
|
+
// not yet committed is seen. A file deleted in the work tree is dropped.
|
|
131
|
+
const out = execFileSync("git", ["-C", root, "ls-files", "-z", "--cached", "--others", "--exclude-standard", "--", ...present], { encoding: "utf8", maxBuffer: 512 * 1024 * 1024 });
|
|
132
|
+
return [...new Set(out.split("\0").filter(Boolean))].filter((f) => !SKIP_DIR_RE.test(f) && existsSync(path.join(root, f)));
|
|
133
|
+
}
|
|
134
|
+
const found = [];
|
|
135
|
+
const walk = (rel) => {
|
|
136
|
+
for (const entry of readdirSync(path.join(root, rel), { withFileTypes: true })) {
|
|
137
|
+
const child = rel === "." ? entry.name : `${rel}/${entry.name}`;
|
|
138
|
+
if (SKIP_DIR_RE.test(child)) continue;
|
|
139
|
+
if (entry.isDirectory()) walk(child);
|
|
140
|
+
else if (entry.isFile()) found.push(child);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
for (const d of present) {
|
|
144
|
+
if (lstatSync(path.join(root, d)).isDirectory()) walk(d);
|
|
145
|
+
else found.push(d);
|
|
146
|
+
}
|
|
147
|
+
return found;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ---------- manifest ----------
|
|
151
|
+
/** Parses the manifest text. Returns the entries and any shape problems. */
|
|
152
|
+
export function parseManifest(text) {
|
|
153
|
+
const entries = [];
|
|
154
|
+
const problems = [];
|
|
155
|
+
let schema;
|
|
156
|
+
let inList = false;
|
|
157
|
+
text.split("\n").forEach((raw, i) => {
|
|
158
|
+
const line = raw.replace(/\r$/, "");
|
|
159
|
+
if (!line.trim() || /^\s*#/.test(line)) return;
|
|
160
|
+
const sm = /^schema:\s*(\S+)\s*$/.exec(line);
|
|
161
|
+
if (sm) { schema = sm[1]; return; }
|
|
162
|
+
if (/^functions:\s*$/.test(line)) { inList = true; return; }
|
|
163
|
+
const em = /^ - (\{.*\})\s*$/.exec(line);
|
|
164
|
+
if (em && inList) {
|
|
165
|
+
try {
|
|
166
|
+
const entry = JSON.parse(em[1]);
|
|
167
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) throw new Error("not a mapping");
|
|
168
|
+
entries.push({ ...entry, line: i + 1 });
|
|
169
|
+
} catch (err) {
|
|
170
|
+
problems.push({ rule: "manifest", line: i + 1, message: `line ${i + 1} is not a one-line JSON mapping: ${err.message}` });
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
problems.push({ rule: "manifest", line: i + 1, message: `line ${i + 1} is not a comment, "schema: 1", "functions:" or a one-line entry` });
|
|
175
|
+
});
|
|
176
|
+
if (schema !== "1") problems.push({ rule: "manifest", message: `schema must be 1 (found ${schema ?? "none"})` });
|
|
177
|
+
return { entries, problems };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const label = (e) => `${typeof e.kind === "string" ? e.kind : "function"} "${typeof e.id === "string" ? e.id : `line ${e.line}`}"`;
|
|
181
|
+
|
|
182
|
+
/** Rules that need only the manifest. */
|
|
183
|
+
export function checkEntries(entries) {
|
|
184
|
+
const problems = [];
|
|
185
|
+
const first = new Map();
|
|
186
|
+
for (const e of entries) {
|
|
187
|
+
const name = label(e);
|
|
188
|
+
const fail = (rule, message) => problems.push({ rule, function: e.id, line: e.line, message: `${name} (line ${e.line}): ${message}` });
|
|
189
|
+
if (typeof e.id !== "string" || !e.id.trim()) { fail("manifest", "id must be a non-empty string"); continue; }
|
|
190
|
+
if (!KINDS.includes(e.kind)) fail("manifest", `kind must be one of ${KINDS.join(", ")}`);
|
|
191
|
+
|
|
192
|
+
// one owner per function
|
|
193
|
+
if ("owners" in e) fail("two-owners", `has an "owners" list; a function has exactly one owner`);
|
|
194
|
+
if (Array.isArray(e.owner)) {
|
|
195
|
+
fail("two-owners", e.owner.length > 1
|
|
196
|
+
? `has ${e.owner.length} owner implementations (${e.owner.join(", ")}); a function has exactly one owner`
|
|
197
|
+
: "owner must be one path or module, not a list");
|
|
198
|
+
} else if (typeof e.owner !== "string" || !e.owner.trim()) fail("manifest", "owner must be one path or module");
|
|
199
|
+
const key = `${e.kind}\u0000${e.id}`;
|
|
200
|
+
if (first.has(key)) {
|
|
201
|
+
const other = first.get(key);
|
|
202
|
+
fail("two-owners", `is listed twice (lines ${other.line} and ${e.line}) with owners ${JSON.stringify(other.owner)} and ${JSON.stringify(e.owner)}; a function has exactly one owner`);
|
|
203
|
+
} else first.set(key, e);
|
|
204
|
+
|
|
205
|
+
if (!Array.isArray(e.runtimes) || !e.runtimes.length || e.runtimes.some((r) => !RUNTIMES.includes(r))) fail("manifest", `runtimes must be a list from ${RUNTIMES.join(", ")}`);
|
|
206
|
+
if (!HOT_PATHS.includes(e.hot_path)) fail("manifest", `hot_path must be one of ${HOT_PATHS.join(", ")}`);
|
|
207
|
+
if (!STATES.includes(e.state)) fail("manifest", `state must be one of ${STATES.join(", ")}`);
|
|
208
|
+
const nonLive = e.state === "retiring" || e.state === "removed";
|
|
209
|
+
if (nonLive && (typeof e.replaced_by !== "string" || !e.replaced_by.trim())) fail("manifest", `state ${e.state} needs replaced_by: what to use instead`);
|
|
210
|
+
if (!nonLive && "replaced_by" in e) fail("manifest", `replaced_by is only for state retiring or removed`);
|
|
211
|
+
if ("released_sha256" in e) {
|
|
212
|
+
if (e.state !== "removed" || (e.kind !== "hook" && e.kind !== "script")) fail("manifest", "released_sha256 is only for a removed hook or script");
|
|
213
|
+
// The update cleanup deletes these paths on installs: keep them inside
|
|
214
|
+
// the core-owned folders its hard-coded list covered (core/, .codex/)
|
|
215
|
+
// and .claude/hooks, and free of whitespace.
|
|
216
|
+
if (typeof e.id === "string" && (/\s/.test(e.id) || !/^(core\/|\.codex\/|\.claude\/hooks\/)/.test(e.id) || e.id.split("/").includes(".."))) fail("manifest", "released_sha256 is only for a path under core/, .codex/ or .claude/hooks/ with no whitespace");
|
|
217
|
+
if (!Array.isArray(e.released_sha256) || !e.released_sha256.length || e.released_sha256.some((s) => !/^[0-9a-f]{64}$/.test(s))) fail("manifest", "released_sha256 must be a list of sha256 hex strings");
|
|
218
|
+
}
|
|
219
|
+
if ("allowed_refs" in e && (!nonLive || !Array.isArray(e.allowed_refs) || e.allowed_refs.some((s) => typeof s !== "string" || !s))) fail("manifest", "allowed_refs must be a list of file paths on a retiring or removed function");
|
|
220
|
+
for (const k of Object.keys(e)) if (k !== "line" && k !== "owners" && !KEYS.includes(k)) fail("manifest", `unknown key "${k}"`);
|
|
221
|
+
if ((e.kind === "hook" || e.kind === "script") && (/^\/|(^|\/)\.\.(\/|$)|\\/.test(e.id))) fail("manifest", "id must be a path from the HQ root");
|
|
222
|
+
if (e.kind === "command" && !/^[a-z0-9_][a-z0-9_-]*( [a-z0-9_][a-z0-9_-]*)*$/.test(e.id)) fail("manifest", "a command id is the command path as typed, without hq and without arguments");
|
|
223
|
+
}
|
|
224
|
+
// an owner written as `hq <command>` must be a listed command that is not removed
|
|
225
|
+
const commands = new Map(entries.filter((e) => e.kind === "command" && typeof e.id === "string").map((e) => [e.id, e]));
|
|
226
|
+
for (const e of entries) {
|
|
227
|
+
if (typeof e.owner !== "string" || !e.owner.startsWith("hq ") || e.state === "removed") continue;
|
|
228
|
+
const target = commands.get(e.owner.slice(3).trim());
|
|
229
|
+
if (!target) problems.push({ rule: "manifest", function: e.id, line: e.line, message: `${label(e)} (line ${e.line}): owner "${e.owner}" is not a command in the manifest` });
|
|
230
|
+
else if (target.state === "removed") problems.push({ rule: "manifest", function: e.id, line: e.line, message: `${label(e)} (line ${e.line}): owner "${e.owner}" is a removed command` });
|
|
231
|
+
}
|
|
232
|
+
return problems;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// ---------- hq-core-staging tree ----------
|
|
236
|
+
const inScope = (f) => !SCOPE_TEST_PART.test(f) && !NOT_A_FUNCTION.has(path.posix.basename(f));
|
|
237
|
+
const kindOfPath = (f) => (HOOK_DIRS.some((d) => f.startsWith(`${d}/`)) ? "hook" : SCRIPT_DIRS.some((d) => f.startsWith(`${d}/`)) ? "script" : null);
|
|
238
|
+
const isCliPath = (p) => /^(src|crates|assets|scripts)\//.test(p);
|
|
239
|
+
|
|
240
|
+
export function checkCoreTree(entries, coreRoot) {
|
|
241
|
+
const problems = [];
|
|
242
|
+
const files = listFiles(coreRoot, [...HOOK_DIRS, ...SCRIPT_DIRS]).filter(inScope);
|
|
243
|
+
const byId = new Map(entries.filter((e) => e.kind === "hook" || e.kind === "script").map((e) => [e.id, e]));
|
|
244
|
+
for (const f of files) {
|
|
245
|
+
const e = byId.get(f);
|
|
246
|
+
if (!e) problems.push({ rule: "unlisted", file: f, message: `${f} exists on disk and the manifest does not list it. Add a ${kindOfPath(f)} entry to ${MANIFEST_REL}, or delete the file.` });
|
|
247
|
+
else if (e.state === "removed") problems.push({ rule: "removed-present", function: e.id, file: f, message: `${label(e)} is removed in the manifest and the file is on disk. ${replacedText(e)}` });
|
|
248
|
+
else if (e.kind !== kindOfPath(f)) problems.push({ rule: "manifest", function: e.id, message: `${label(e)}: a file under ${path.posix.dirname(f)} is a ${kindOfPath(f)}` });
|
|
249
|
+
}
|
|
250
|
+
const onDisk = new Set(files);
|
|
251
|
+
for (const e of byId.values()) {
|
|
252
|
+
if (typeof e.id !== "string") continue;
|
|
253
|
+
const live = e.state === "live" || e.state === "trial";
|
|
254
|
+
if (live && !kindOfPath(e.id)) problems.push({ rule: "manifest", function: e.id, message: `${label(e)}: the path is outside ${[...HOOK_DIRS, ...SCRIPT_DIRS].join(", ")}` });
|
|
255
|
+
else if (live && !onDisk.has(e.id) && !existsSync(path.join(coreRoot, e.id))) problems.push({ rule: "missing", function: e.id, message: `${label(e)} is ${e.state} in the manifest and the file is not on disk. Mark it removed (with replaced_by) or restore the file.` });
|
|
256
|
+
if (e.state === "removed" && !onDisk.has(e.id) && existsSync(path.join(coreRoot, e.id))) problems.push({ rule: "removed-present", function: e.id, file: e.id, message: `${label(e)} is removed in the manifest and the file is on disk. ${replacedText(e)}` });
|
|
257
|
+
}
|
|
258
|
+
for (const e of entries) {
|
|
259
|
+
if (typeof e.owner !== "string" || e.owner.startsWith("hq ") || isCliPath(e.owner) || e.kind === "command" || e.kind === "daemon") continue;
|
|
260
|
+
if ((e.state === "live" || e.state === "trial") && !existsSync(path.join(coreRoot, e.owner))) problems.push({ rule: "missing", function: e.id, message: `${label(e)}: owner ${e.owner} is not on disk` });
|
|
261
|
+
}
|
|
262
|
+
return { problems, files: files.length };
|
|
263
|
+
}
|
|
264
|
+
const replacedText = (e) => (e.replaced_by ? `Use instead: ${e.replaced_by}` : "");
|
|
265
|
+
|
|
266
|
+
// ---------- hq-cli tree ----------
|
|
267
|
+
function flattenCatalog(nodes, prefix = [], aliases = new Map(), out = []) {
|
|
268
|
+
for (const node of nodes || []) {
|
|
269
|
+
const tokens = [...prefix, node.name];
|
|
270
|
+
out.push(tokens.join(" "));
|
|
271
|
+
aliases.set(tokens.join(" "), node.aliases || []);
|
|
272
|
+
flattenCatalog(node.subcommands, tokens, aliases, out);
|
|
273
|
+
}
|
|
274
|
+
return { commands: out, aliases };
|
|
275
|
+
}
|
|
276
|
+
function readSourceCatalog(cliRoot) {
|
|
277
|
+
const file = path.join(cliRoot, "src/command-catalog.generated.ts");
|
|
278
|
+
if (!existsSync(file)) return null;
|
|
279
|
+
const text = readFileSync(file, "utf8");
|
|
280
|
+
const marker = "export const COMMAND_CATALOG = ";
|
|
281
|
+
const start = text.indexOf(marker);
|
|
282
|
+
const end = text.lastIndexOf("] as const");
|
|
283
|
+
if (start < 0 || end < 0) throw new Error(`${file}: cannot find COMMAND_CATALOG`);
|
|
284
|
+
return flattenCatalog(JSON.parse(text.slice(start + marker.length, end + 1)));
|
|
285
|
+
}
|
|
286
|
+
async function readPackageCatalog(pkgRoot) {
|
|
287
|
+
const file = path.join(pkgRoot, "dist/command-catalog.generated.js");
|
|
288
|
+
if (!existsSync(file)) return null;
|
|
289
|
+
const mod = await import(pathToFileURL(file).href);
|
|
290
|
+
return flattenCatalog(mod.COMMAND_CATALOG);
|
|
291
|
+
}
|
|
292
|
+
const isDaemonSource = (f) => /\.(ts|mts)$/.test(f) && !/\.d\.ts$/.test(f) && !TEST_FILE_RE.test(f) && !/(^|\/)(__tests__|__fixtures__|fixtures)\//.test(f);
|
|
293
|
+
|
|
294
|
+
export function checkCliTree(entries, cliRoot, catalog) {
|
|
295
|
+
const problems = [];
|
|
296
|
+
const commands = new Map(entries.filter((e) => e.kind === "command").map((e) => [e.id, e]));
|
|
297
|
+
const inCatalog = new Set(catalog.commands);
|
|
298
|
+
for (const c of catalog.commands) {
|
|
299
|
+
const e = commands.get(c);
|
|
300
|
+
if (!e) problems.push({ rule: "unlisted", command: c, message: `hq ${c} is a command in hq-cli and the manifest does not list it. Add a command entry with id "${c}" to ${MANIFEST_REL}.` });
|
|
301
|
+
else if (e.state === "removed") problems.push({ rule: "removed-present", function: e.id, message: `${label(e)} is removed in the manifest and hq-cli still has the command. ${replacedText(e)}` });
|
|
302
|
+
}
|
|
303
|
+
for (const e of commands.values()) {
|
|
304
|
+
if ((e.state === "live" || e.state === "trial") && !inCatalog.has(e.id)) problems.push({ rule: "missing", function: e.id, message: `${label(e)} is ${e.state} in the manifest and hq-cli has no such command. Mark it removed (with replaced_by) or fix the id.` });
|
|
305
|
+
}
|
|
306
|
+
const daemonFiles = listFiles(cliRoot, [DAEMON_DIR]).filter(isDaemonSource);
|
|
307
|
+
const daemonOwners = new Set(entries.filter((e) => e.kind === "daemon" && e.state !== "removed").map((e) => e.owner));
|
|
308
|
+
for (const f of daemonFiles) {
|
|
309
|
+
if (!daemonOwners.has(f)) problems.push({ rule: "unlisted", file: f, message: `${f} exists on disk and no daemon entry in the manifest names it as owner. Add a daemon entry to ${MANIFEST_REL}, or delete the file.` });
|
|
310
|
+
}
|
|
311
|
+
for (const e of entries) {
|
|
312
|
+
if (typeof e.owner !== "string" || !isCliPath(e.owner)) continue;
|
|
313
|
+
if ((e.state === "live" || e.state === "trial") && !existsSync(path.join(cliRoot, e.owner))) problems.push({ rule: "missing", function: e.id, message: `${label(e)}: owner ${e.owner} is not in hq-cli` });
|
|
314
|
+
}
|
|
315
|
+
return { problems, commands: catalog.commands.length, daemonFiles: daemonFiles.length };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** An installed package may be older than the manifest: only unlisted items fail. */
|
|
319
|
+
export function checkCliPackage(entries, pkgRoot, catalog) {
|
|
320
|
+
const problems = [];
|
|
321
|
+
const commands = new Set(entries.filter((e) => e.kind === "command").map((e) => e.id));
|
|
322
|
+
for (const c of catalog.commands) {
|
|
323
|
+
if (!commands.has(c)) problems.push({ rule: "unlisted", command: c, message: `hq ${c} is a command in the installed hq-cli and the manifest does not list it. Add a command entry with id "${c}" to ${MANIFEST_REL}.` });
|
|
324
|
+
}
|
|
325
|
+
const dir = path.join(pkgRoot, "dist/lib/daemon");
|
|
326
|
+
const owners = new Set(entries.filter((e) => e.kind === "daemon").map((e) => e.owner));
|
|
327
|
+
let daemonFiles = 0;
|
|
328
|
+
if (existsSync(dir)) {
|
|
329
|
+
// dist is ignored by git and skipped by listFiles, so walk it directly.
|
|
330
|
+
const built = [];
|
|
331
|
+
const walk = (abs) => {
|
|
332
|
+
for (const entry of readdirSync(abs, { withFileTypes: true })) {
|
|
333
|
+
const child = path.join(abs, entry.name);
|
|
334
|
+
if (entry.isDirectory()) walk(child);
|
|
335
|
+
else if (entry.isFile()) built.push(posix(path.relative(pkgRoot, child)));
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
walk(dir);
|
|
339
|
+
for (const f of built) {
|
|
340
|
+
if (!/\.m?js$/.test(f) || TEST_FILE_RE.test(f)) continue;
|
|
341
|
+
daemonFiles++;
|
|
342
|
+
const src = f.replace(/^dist\//, "src/").replace(/\.mjs$/, ".mts").replace(/\.js$/, ".ts");
|
|
343
|
+
if (!owners.has(src) && !owners.has(src.replace(/\.ts$/, ".mts"))) problems.push({ rule: "unlisted", file: src, message: `${src} is in the installed hq-cli and no daemon entry in the manifest names it as owner. Add a daemon entry to ${MANIFEST_REL}.` });
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return { problems, commands: catalog.commands.length, daemonFiles };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ---------- references to retiring and removed functions ----------
|
|
350
|
+
function fileMatcher(e, liveByBase) {
|
|
351
|
+
const id = e.id;
|
|
352
|
+
const base = path.posix.basename(id);
|
|
353
|
+
const dirsRev = path.posix.dirname(id).split("/").reverse();
|
|
354
|
+
const byName = SCRIPT_NAME_RE.test(base);
|
|
355
|
+
const rivals = (liveByBase.get(base) || []).map((p) => path.posix.dirname(p).split("/").reverse());
|
|
356
|
+
const before = (text, i) => (i === 0 ? "" : text[i - 1]);
|
|
357
|
+
const endsClean = (text, j) => {
|
|
358
|
+
const c = text[j];
|
|
359
|
+
if (c === undefined) return true;
|
|
360
|
+
if (/[A-Za-z0-9_-]/.test(c)) return false;
|
|
361
|
+
return !(c === "." && /[A-Za-z0-9]/.test(text[j + 1] || ""));
|
|
362
|
+
};
|
|
363
|
+
// Directory words written right before the name, nearest first. `open` is
|
|
364
|
+
// true when the directory ends in a variable or is not written at all.
|
|
365
|
+
const written = (text, i) => {
|
|
366
|
+
let s = i;
|
|
367
|
+
while (s > 0 && /[A-Za-z0-9_.,@${}()~/-]/.test(text[s - 1])) s--;
|
|
368
|
+
const token = text.slice(s, i);
|
|
369
|
+
if (!token.endsWith("/")) return { comps: [], open: true, home: false };
|
|
370
|
+
const comps = [];
|
|
371
|
+
let open = false;
|
|
372
|
+
for (const part of token.slice(0, -1).split("/").reverse()) {
|
|
373
|
+
if (part === "" || part === "." || part === ".." || /[${}()~]/.test(part)) { open = true; break; }
|
|
374
|
+
comps.push(part);
|
|
375
|
+
}
|
|
376
|
+
return { comps, open, home: /(^|[^A-Za-z0-9_])(~|\$HOME|\$\{HOME\})\/[^$]*$/.test(token) };
|
|
377
|
+
};
|
|
378
|
+
const fits = (comps, dirs) => comps.every((c, k) => k >= dirs.length || c === dirs[k]);
|
|
379
|
+
return (text) => {
|
|
380
|
+
if (!text.includes(base)) return null;
|
|
381
|
+
for (let i = text.indexOf(base); i >= 0; i = text.indexOf(base, i + 1)) {
|
|
382
|
+
if (/[A-Za-z0-9_.-]/.test(before(text, i)) || !endsClean(text, i + base.length)) continue;
|
|
383
|
+
const w = written(text, i);
|
|
384
|
+
// ~/.codex/hooks.json is the user's own file, not the one in the HQ root.
|
|
385
|
+
if (w.home && id.startsWith(".")) continue;
|
|
386
|
+
if (!fits(w.comps, dirsRev)) continue;
|
|
387
|
+
const full = w.comps.length >= dirsRev.length;
|
|
388
|
+
if (full) return "path";
|
|
389
|
+
if (!byName) continue;
|
|
390
|
+
// partial, bare or variable directory: a live file of the same name that
|
|
391
|
+
// fits the same text is the one meant
|
|
392
|
+
if (rivals.some((r) => fits(w.comps, r))) continue;
|
|
393
|
+
return w.comps.length ? "partial path" : "name";
|
|
394
|
+
}
|
|
395
|
+
return null;
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function commandMatcher(e, aliases) {
|
|
399
|
+
const tokens = e.id.split(" ");
|
|
400
|
+
let variants = [[]];
|
|
401
|
+
tokens.forEach((t, i) => {
|
|
402
|
+
const names = [t, ...((aliases && aliases.get(tokens.slice(0, i + 1).join(" "))) || [])];
|
|
403
|
+
variants = variants.flatMap((v) => names.map((n) => [...v, n]));
|
|
404
|
+
});
|
|
405
|
+
const re = /(?<![A-Za-z0-9_.\/-])hq((?:[ \t]+[a-z_][a-z0-9_-]*){1,8})/g;
|
|
406
|
+
return (text) => {
|
|
407
|
+
if (!text.includes("hq")) return null;
|
|
408
|
+
re.lastIndex = 0;
|
|
409
|
+
let m;
|
|
410
|
+
while ((m = re.exec(text))) {
|
|
411
|
+
const words = m[1].trim().split(/[ \t]+/);
|
|
412
|
+
if (variants.some((v) => v.length <= words.length && v.every((x, i) => x === words[i]))) return "command";
|
|
413
|
+
}
|
|
414
|
+
return null;
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
function daemonMatcher(e) {
|
|
418
|
+
const q = "[\"'`]";
|
|
419
|
+
if (/^op\s/.test(e.id)) {
|
|
420
|
+
const ops = e.id.replace(/^op\s+/, "").split("/").map((s) => s.trim().split(/\s/)[0]).filter(Boolean);
|
|
421
|
+
const res = ops.map((op) => new RegExp(`${q}${esc(op)}${q}|daemon\\s+call\\s+${esc(op)}(?![A-Za-z0-9_.-])`));
|
|
422
|
+
return (text, file) => (/^src\/lib\/daemon\//.test(file) ? null : res.some((r) => r.test(text)) ? "daemon op" : null);
|
|
423
|
+
}
|
|
424
|
+
// A daemon module: its path as text, or an import of it. Other daemon files
|
|
425
|
+
// that import it are callers, so src/lib/daemon is searched too.
|
|
426
|
+
const owner = typeof e.owner === "string" ? e.owner : "";
|
|
427
|
+
if (!owner) return () => null;
|
|
428
|
+
const base = path.posix.basename(owner).replace(/\.m?ts$/, "");
|
|
429
|
+
const pathRe = new RegExp(`${esc(owner)}(?![A-Za-z0-9_-])`);
|
|
430
|
+
const specRe = new RegExp(`${q}([^"'\`\\s]*${esc(base)}\\.m?js)${q}`, "g");
|
|
431
|
+
const sourceOf = (p) => p.replace(/\.mjs$/, ".mts").replace(/\.js$/, ".ts");
|
|
432
|
+
return (text, file) => {
|
|
433
|
+
if (pathRe.test(text)) return "daemon module";
|
|
434
|
+
specRe.lastIndex = 0;
|
|
435
|
+
let m;
|
|
436
|
+
while ((m = specRe.exec(text))) {
|
|
437
|
+
// A relative import is resolved from the importing file.
|
|
438
|
+
const spec = m[1];
|
|
439
|
+
const resolved = spec.startsWith(".") ? path.posix.normalize(path.posix.join(path.posix.dirname(file), spec)) : spec;
|
|
440
|
+
if (sourceOf(resolved) === owner || sourceOf(resolved).replace(/\.ts$/, ".mts") === owner) return "daemon module import";
|
|
441
|
+
}
|
|
442
|
+
return null;
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function refExcluded(file) {
|
|
447
|
+
if (file === MANIFEST_REL || file.endsWith(`/${MANIFEST_REL}`)) return "manifest";
|
|
448
|
+
if (CHANGELOG_RE.test(file)) return "changelog";
|
|
449
|
+
if (ARCHIVE_RE.test(file)) return "archive";
|
|
450
|
+
if (TEST_DIR_RE.test(file) || TEST_FILE_RE.test(file)) return "test";
|
|
451
|
+
if (LISTING_RE.test(file)) return "listing";
|
|
452
|
+
if (BACKUP_RE.test(file)) return "backup";
|
|
453
|
+
if (CREDENTIAL_RE.test(file)) return "credential";
|
|
454
|
+
if (!TEXT_EXT_RE.test(file) && path.posix.basename(file).includes(".")) return "not text";
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Lines in the tree that name a retiring or removed function. */
|
|
459
|
+
export function findReferences(targets, root, { allFiles } = {}) {
|
|
460
|
+
const problems = [];
|
|
461
|
+
if (!targets.length) return { problems, files: 0 };
|
|
462
|
+
const files = (allFiles || listFiles(root, ["."])).filter((f) => !refExcluded(f));
|
|
463
|
+
let scanned = 0;
|
|
464
|
+
for (const file of files) {
|
|
465
|
+
let stat;
|
|
466
|
+
try { stat = statSync(path.join(root, file)); } catch { continue; }
|
|
467
|
+
if (!stat.isFile() || stat.size > MAX_FILE_BYTES) continue;
|
|
468
|
+
const body = readFileSync(path.join(root, file), "utf8");
|
|
469
|
+
if (body.includes("\u0000")) continue;
|
|
470
|
+
scanned++;
|
|
471
|
+
const live = targets.filter((t) => t.needles.some((n) => body.includes(n)));
|
|
472
|
+
if (!live.length) continue;
|
|
473
|
+
const isDoc = DOC_EXT_RE.test(file);
|
|
474
|
+
const lines = body.split("\n");
|
|
475
|
+
for (const t of live) {
|
|
476
|
+
if (t.self(file)) continue;
|
|
477
|
+
// Without a manifest the owner is not known: a source file that
|
|
478
|
+
// registers the command is taken as the command's own source.
|
|
479
|
+
if (t.registration && /^src\//.test(file) && t.registration.some((n) => body.includes(n))) continue;
|
|
480
|
+
for (let i = 0; i < lines.length; i++) {
|
|
481
|
+
const text = lines[i];
|
|
482
|
+
if (text.length > 6000) continue;
|
|
483
|
+
if (!isDoc && COMMENT_RE.test(text)) continue;
|
|
484
|
+
const how = t.match(text, file);
|
|
485
|
+
if (!how) continue;
|
|
486
|
+
problems.push({
|
|
487
|
+
rule: "reference", function: t.entry.id, file, line: i + 1,
|
|
488
|
+
message: `${file}:${i + 1} references ${label(t.entry)}, which is ${t.entry.state} (${how}). ${replacedText(t.entry)}`,
|
|
489
|
+
});
|
|
490
|
+
break; // one line per file and function is enough to act on
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
return { problems, files: scanned };
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function referenceTargets(entries, aliases) {
|
|
498
|
+
const liveByBase = new Map();
|
|
499
|
+
for (const e of entries) {
|
|
500
|
+
if ((e.kind !== "hook" && e.kind !== "script") || (e.state !== "live" && e.state !== "trial") || typeof e.id !== "string") continue;
|
|
501
|
+
const b = path.posix.basename(e.id);
|
|
502
|
+
if (!liveByBase.has(b)) liveByBase.set(b, []);
|
|
503
|
+
liveByBase.get(b).push(e.id);
|
|
504
|
+
}
|
|
505
|
+
const targets = [];
|
|
506
|
+
for (const e of entries) {
|
|
507
|
+
if (e.state !== "retiring" && e.state !== "removed") continue;
|
|
508
|
+
if (typeof e.id !== "string" || typeof e.owner !== "string") continue;
|
|
509
|
+
const allowed = new Set(Array.isArray(e.allowed_refs) ? e.allowed_refs : []);
|
|
510
|
+
const own = new Set([e.id, e.owner]);
|
|
511
|
+
const cleaned = Array.isArray(e.released_sha256);
|
|
512
|
+
const self = (file) => own.has(file) || allowed.has(file) || file.endsWith(`/${e.id}`) || (cleaned && file === RETIRED_CLEANER);
|
|
513
|
+
if (e.kind === "hook" || e.kind === "script") {
|
|
514
|
+
targets.push({ entry: e, needles: [path.posix.basename(e.id)], match: fileMatcher(e, liveByBase), self });
|
|
515
|
+
} else if (e.kind === "command") {
|
|
516
|
+
const leaf = e.id.split(" ").pop();
|
|
517
|
+
const registration = e.owner ? null : [`command("${leaf}`, `command('${leaf}`, `Command("${leaf}`, `Command('${leaf}`, "command(`" + leaf];
|
|
518
|
+
targets.push({ entry: e, needles: ["hq"], match: commandMatcher(e, aliases), self, registration });
|
|
519
|
+
} else if (e.kind === "daemon") {
|
|
520
|
+
const needles = /^op\s/.test(e.id)
|
|
521
|
+
? e.id.replace(/^op\s+/, "").split("/").map((s) => s.trim().split(/\s/)[0]).filter(Boolean)
|
|
522
|
+
: [path.posix.basename(e.owner).replace(/\.m?ts$/, "")];
|
|
523
|
+
targets.push({ entry: e, needles, match: daemonMatcher(e), self });
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
return targets;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// ---------- command states inside hq-cli ----------
|
|
530
|
+
export function readCliStates(file) {
|
|
531
|
+
if (!existsSync(file)) return { missing: true };
|
|
532
|
+
let data;
|
|
533
|
+
try { data = JSON.parse(readFileSync(file, "utf8")); } catch (err) { return { problems: [{ rule: "cli-states", message: `${file} is not JSON: ${err.message}` }] }; }
|
|
534
|
+
const problems = [];
|
|
535
|
+
if (data?.schema !== 1) problems.push({ rule: "cli-states", message: `${file}: schema must be 1` });
|
|
536
|
+
const commands = data?.commands;
|
|
537
|
+
if (!commands || typeof commands !== "object" || Array.isArray(commands)) {
|
|
538
|
+
problems.push({ rule: "cli-states", message: `${file}: commands must be an object keyed by command path` });
|
|
539
|
+
return { problems, commands: {} };
|
|
540
|
+
}
|
|
541
|
+
for (const [id, value] of Object.entries(commands)) {
|
|
542
|
+
if (!value || typeof value !== "object" || !STATES.includes(value.state) || value.state === "live") {
|
|
543
|
+
problems.push({ rule: "cli-states", command: id, message: `${file}: "${id}" needs a state of trial, retiring or removed (a live command is not listed)` });
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return { problems, commands };
|
|
547
|
+
}
|
|
548
|
+
export function checkCliStates(entries, states, file) {
|
|
549
|
+
const problems = [];
|
|
550
|
+
const notices = [];
|
|
551
|
+
const manifest = new Map(entries.filter((e) => e.kind === "command").map((e) => [e.id, e]));
|
|
552
|
+
for (const [id, value] of Object.entries(states)) {
|
|
553
|
+
const e = manifest.get(id);
|
|
554
|
+
if (!e) problems.push({ rule: "cli-states", command: id, message: `${file} lists "${id}" as ${value?.state} and the manifest has no such command` });
|
|
555
|
+
else if (e.state !== value?.state) problems.push({ rule: "cli-states", command: id, message: `${file} lists "${id}" as ${value?.state} and the manifest says ${e.state}` });
|
|
556
|
+
else if ((e.replaced_by || "") !== (value?.replacedBy || "") && e.replaced_by) notices.push(`${file}: "${id}" replacedBy differs from the manifest's replaced_by`);
|
|
557
|
+
}
|
|
558
|
+
for (const e of manifest.values()) {
|
|
559
|
+
if ((e.state === "retiring" || e.state === "removed") && !(e.id in states)) {
|
|
560
|
+
problems.push({ rule: "cli-states", command: e.id, message: `the manifest says command "${e.id}" is ${e.state} and ${file} does not list it` });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return { problems, notices };
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// ---------- main ----------
|
|
567
|
+
function parseArgs(argv) {
|
|
568
|
+
const opt = { json: false, noManifest: false };
|
|
569
|
+
const value = (i) => {
|
|
570
|
+
if (argv[i + 1] === undefined) usage(`${argv[i]} needs a value`);
|
|
571
|
+
return argv[i + 1];
|
|
572
|
+
};
|
|
573
|
+
for (let i = 0; i < argv.length; i++) {
|
|
574
|
+
const a = argv[i];
|
|
575
|
+
if (a === "--manifest") opt.manifest = path.resolve(value(i++));
|
|
576
|
+
else if (a === "--core-root") opt.coreRoot = path.resolve(value(i++));
|
|
577
|
+
else if (a === "--cli-root") opt.cliRoot = path.resolve(value(i++));
|
|
578
|
+
else if (a === "--cli-package") opt.cliPackage = path.resolve(value(i++));
|
|
579
|
+
else if (a === "--cli-states") opt.cliStates = path.resolve(value(i++));
|
|
580
|
+
else if (a === "--require-cli-states") opt.requireCliStates = true;
|
|
581
|
+
else if (a === "--no-manifest") opt.noManifest = true;
|
|
582
|
+
else if (a === "--json") opt.json = true;
|
|
583
|
+
else if (a === "--help" || a === "-h") usage();
|
|
584
|
+
else usage(`unknown argument: ${a}`);
|
|
585
|
+
}
|
|
586
|
+
return opt;
|
|
587
|
+
}
|
|
588
|
+
function usage(error) {
|
|
589
|
+
if (error) console.error(`check-function-manifest: ${error}`);
|
|
590
|
+
console.error("usage: node scripts/check-function-manifest.mjs [--manifest <file>] [--core-root <dir>] [--cli-root <dir> | --cli-package <dir>] [--cli-states <file> [--require-cli-states]] [--no-manifest] [--json]");
|
|
591
|
+
process.exit(2);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
export async function run(argv) {
|
|
595
|
+
const started = Date.now();
|
|
596
|
+
const opt = parseArgs(argv);
|
|
597
|
+
const here = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
598
|
+
if (!opt.cliRoot && !opt.cliPackage) {
|
|
599
|
+
if (existsSync(path.join(here, "src/command-catalog.generated.ts"))) opt.cliRoot = here;
|
|
600
|
+
else if (existsSync(path.join(here, "dist/command-catalog.generated.js"))) opt.cliPackage = here;
|
|
601
|
+
}
|
|
602
|
+
if (opt.cliRoot && opt.cliPackage) usage("give --cli-root or --cli-package, not both");
|
|
603
|
+
for (const [flag, dir] of [["--core-root", opt.coreRoot], ["--cli-root", opt.cliRoot], ["--cli-package", opt.cliPackage]]) {
|
|
604
|
+
if (dir && !existsSync(dir)) usage(`${flag} ${dir} does not exist`);
|
|
605
|
+
}
|
|
606
|
+
if (!opt.manifest && opt.coreRoot) opt.manifest = path.join(opt.coreRoot, MANIFEST_REL);
|
|
607
|
+
if (opt.noManifest && (opt.manifest || opt.coreRoot)) usage("--no-manifest cannot be combined with --manifest or --core-root");
|
|
608
|
+
if (!opt.noManifest && !opt.manifest) usage("no manifest: pass --core-root or --manifest (or --no-manifest in the hq-cli pull request job)");
|
|
609
|
+
if (opt.manifest && !existsSync(opt.manifest)) usage(`manifest ${opt.manifest} does not exist`);
|
|
610
|
+
|
|
611
|
+
const problems = [];
|
|
612
|
+
const notices = [];
|
|
613
|
+
const checked = [];
|
|
614
|
+
let entries = [];
|
|
615
|
+
let catalog = null;
|
|
616
|
+
if (opt.cliRoot) catalog = readSourceCatalog(opt.cliRoot);
|
|
617
|
+
else if (opt.cliPackage) catalog = await readPackageCatalog(opt.cliPackage);
|
|
618
|
+
if ((opt.cliRoot || opt.cliPackage) && !catalog) usage(`${opt.cliRoot || opt.cliPackage} has no command catalog`);
|
|
619
|
+
|
|
620
|
+
if (opt.manifest) {
|
|
621
|
+
const parsed = parseManifest(readFileSync(opt.manifest, "utf8"));
|
|
622
|
+
entries = parsed.entries;
|
|
623
|
+
problems.push(...parsed.problems, ...checkEntries(entries));
|
|
624
|
+
const n = (pred) => entries.filter(pred).length;
|
|
625
|
+
checked.push(`manifest ${opt.manifest}: ${entries.length} entries (${KINDS.map((k) => `${n((e) => e.kind === k)} ${k}`).join(", ")}; ${STATES.map((s) => `${n((e) => e.state === s)} ${s}`).join(", ")})`);
|
|
626
|
+
|
|
627
|
+
const targets = referenceTargets(entries, catalog?.aliases);
|
|
628
|
+
if (opt.coreRoot) {
|
|
629
|
+
const tree = checkCoreTree(entries, opt.coreRoot);
|
|
630
|
+
problems.push(...tree.problems);
|
|
631
|
+
const refs = findReferences(targets, opt.coreRoot);
|
|
632
|
+
problems.push(...refs.problems);
|
|
633
|
+
checked.push(`core tree ${opt.coreRoot}: ${tree.files} hook and script files; references searched in ${refs.files} files`);
|
|
634
|
+
} else notices.push("no --core-root: hooks and scripts on disk were not compared with the manifest");
|
|
635
|
+
if (opt.cliRoot) {
|
|
636
|
+
const tree = checkCliTree(entries, opt.cliRoot, catalog);
|
|
637
|
+
problems.push(...tree.problems);
|
|
638
|
+
const refs = findReferences(targets, opt.cliRoot);
|
|
639
|
+
problems.push(...refs.problems);
|
|
640
|
+
checked.push(`hq-cli source ${opt.cliRoot}: ${tree.commands} commands, ${tree.daemonFiles} daemon files; references searched in ${refs.files} files`);
|
|
641
|
+
} else if (opt.cliPackage) {
|
|
642
|
+
const tree = checkCliPackage(entries, opt.cliPackage, catalog);
|
|
643
|
+
problems.push(...tree.problems);
|
|
644
|
+
checked.push(`installed hq-cli ${opt.cliPackage}: ${tree.commands} commands, ${tree.daemonFiles} daemon files (unlisted items only; the package may be older than the manifest)`);
|
|
645
|
+
} else notices.push("no hq-cli tree: commands and daemon files were not compared with the manifest");
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
if (opt.cliStates) {
|
|
649
|
+
const states = readCliStates(opt.cliStates);
|
|
650
|
+
if (states.missing && opt.requireCliStates) problems.push({ rule: "cli-states", message: `${opt.cliStates} does not exist and --require-cli-states is set` });
|
|
651
|
+
else if (states.missing) notices.push(`${opt.cliStates} does not exist: command states were not compared (this passes)`);
|
|
652
|
+
else {
|
|
653
|
+
problems.push(...(states.problems || []));
|
|
654
|
+
if (opt.manifest) {
|
|
655
|
+
const r = checkCliStates(entries, states.commands, opt.cliStates);
|
|
656
|
+
problems.push(...r.problems);
|
|
657
|
+
notices.push(...r.notices);
|
|
658
|
+
checked.push(`command states ${opt.cliStates}: ${Object.keys(states.commands).length} commands compared with the manifest`);
|
|
659
|
+
} else if (opt.cliRoot) {
|
|
660
|
+
// No manifest: hold the file to the hq-cli tree it ships in.
|
|
661
|
+
const inCatalog = new Set(catalog.commands);
|
|
662
|
+
const pseudo = [];
|
|
663
|
+
for (const [id, value] of Object.entries(states.commands)) {
|
|
664
|
+
if (value?.state === "removed" && inCatalog.has(id)) problems.push({ rule: "cli-states", command: id, message: `${opt.cliStates} lists "${id}" as removed and hq-cli still has the command` });
|
|
665
|
+
if ((value?.state === "retiring" || value?.state === "trial") && !inCatalog.has(id)) problems.push({ rule: "cli-states", command: id, message: `${opt.cliStates} lists "${id}" as ${value.state} and hq-cli has no such command` });
|
|
666
|
+
if (value?.state === "retiring" || value?.state === "removed") pseudo.push({ id, kind: "command", owner: "", state: value.state, replaced_by: value.replacedBy });
|
|
667
|
+
}
|
|
668
|
+
const refs = findReferences(referenceTargets(pseudo, catalog.aliases), opt.cliRoot);
|
|
669
|
+
problems.push(...refs.problems);
|
|
670
|
+
checked.push(`command states ${opt.cliStates}: ${Object.keys(states.commands).length} commands held to the hq-cli tree (${catalog.commands.length} commands); references searched in ${refs.files} files`);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
if (opt.noManifest) notices.push("--no-manifest: the manifest rules did not run here; they run where the manifest is (hq-core-staging pull requests and a local run with --core-root)");
|
|
675
|
+
|
|
676
|
+
const seconds = ((Date.now() - started) / 1000).toFixed(1);
|
|
677
|
+
if (opt.json) {
|
|
678
|
+
console.log(JSON.stringify({ ok: problems.length === 0, seconds: Number(seconds), checked, notices, problems }, null, 2));
|
|
679
|
+
} else {
|
|
680
|
+
for (const c of checked) console.log(`checked: ${c}`);
|
|
681
|
+
for (const n of notices) console.log(`note: ${n}`);
|
|
682
|
+
for (const p of problems) console.error(`FAIL [${p.rule}] ${p.message}`);
|
|
683
|
+
if (problems.length) console.error(`function manifest check failed: ${problems.length} problem${problems.length === 1 ? "" : "s"} (${seconds} s)`);
|
|
684
|
+
else console.log(`function manifest check passed (${seconds} s)`);
|
|
685
|
+
}
|
|
686
|
+
return problems.length ? 1 : 0;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// Compare real paths: through a symlinked folder (npm link, a global prefix)
|
|
690
|
+
// argv[1] is the symlinked path and import.meta.url is the real one.
|
|
691
|
+
function isMain() {
|
|
692
|
+
if (!process.argv[1]) return false;
|
|
693
|
+
const self = fileURLToPath(import.meta.url);
|
|
694
|
+
try { return realpathSync(process.argv[1]) === realpathSync(self); } catch { return path.resolve(process.argv[1]) === self; }
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
if (isMain()) {
|
|
698
|
+
run(process.argv.slice(2)).then((code) => { process.exitCode = code; }, (err) => {
|
|
699
|
+
console.error(`check-function-manifest: ${err?.stack || err}`);
|
|
700
|
+
process.exitCode = 2;
|
|
701
|
+
});
|
|
702
|
+
}
|