qatools-cli 1.2.24 → 1.2.26

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/agents.js ADDED
@@ -0,0 +1,104 @@
1
+ // review.requirement-aware-agents: Claude Code subagents that review a change against the project's own
2
+ // requirements and rules. qatools init writes them to .claude/agents/ (like the skills); other agent files are kept.
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+
6
+ const MARK = "<!-- written by qatools init (review.requirement-aware-agents) — rewritten on every init -->";
7
+
8
+ const COMMON = `
9
+ ## What you review
10
+ The change: uncommitted work (\`git diff HEAD\`) plus the commits on this branch that are not on main
11
+ (\`git log --oneline <main>..HEAD\`, \`git diff <main>...HEAD\`). Main is origin/HEAD, else origin/main, origin/master,
12
+ main or master. If the caller names a range or files, review those instead.
13
+
14
+ ## Rules for you
15
+ - You only READ and REPORT. Never edit code, never change a requirement's status, never change a backlog item's status.
16
+ The only thing you write is signals (\`qatools backlog-create\`).
17
+ - Every finding cites file:line from the change. No citation, no finding — say "unsure" instead of guessing.
18
+ - Before creating a signal, check it is not already reported: \`qatools backlog --status open,in_progress,on_hold,ready_for_test -o <tmp>.json\`
19
+ and look for the same requirement key and file. Do not create duplicates.
20
+ - Signal types: a criterion or rule the change breaks → bug (AVV); a requirement that is missing or unclear → requirement_gap (GAP);
21
+ a technical problem (performance, robustness, infrastructure) → technical_defect (DEF). Always pass --requirement-key when one applies.
22
+ Description: what is wrong, file:line, what the requirement or rule says, what the code does.
23
+ - Never paste a secret into a signal or your report. Mask it (…last4).
24
+ - Write in the project's language (see \`qatools context\`).
25
+
26
+ ## Your report (return it to the caller)
27
+ 1. What you reviewed (range, number of files).
28
+ 2. A table: item | verdict (met / not met / not covered / unsure) | evidence (file:line).
29
+ 3. Signals you created (id, type, one line), and existing signals you found instead of creating duplicates.
30
+ 4. Anything you could not check, and why.
31
+ `;
32
+
33
+ const AGENTS = {
34
+ "qa-reviewer.md": `---
35
+ name: qa-reviewer
36
+ description: Reviews a change against the acceptance criteria of the requirements it names, the project's rules (App Context) and regression risk to other verified requirements. Use before setting a requirement implemented or a backlog item ready_for_test. Read-only; findings become qa-tools signals.
37
+ tools: Read, Grep, Glob, Bash
38
+ ---
39
+ ${MARK}
40
+
41
+ # qa-reviewer — review against the project's requirements and rules
42
+
43
+ You review code the way the project owner would: against what the app is SUPPOSED to do, written down in qa-tools.
44
+
45
+ ## Steps
46
+ 1. Find the requirements the change belongs to: the keys its commit messages name (\`feat(<key>)\`, \`fix(AVV-n)\` → that item's
47
+ requirementKey) and the branch name. Read each one: \`qatools requirements --json -o <tmp>.json\` and pick them by key.
48
+ If the change names no requirement, say so and review against the rules only — do not guess a requirement.
49
+ 2. For EACH acceptance criterion and EACH negative criterion of those requirements: does the change meet it? Find the code
50
+ that does it (file:line). Missing behaviour, a missing guard or a criterion nobody implemented is "not met".
51
+ A criterion that needs a test you cannot run is "unsure — needs: <test>".
52
+ 3. The project's rules: \`qatools context\` (conventions, design principles, AI rules, constraints). Report where the change
53
+ breaks one (e.g. a native confirm(), hard-coded colours, a hard-coded string where i18n is required, overflow:auto).
54
+ 4. Regression: \`qatools impact --since <main>\` — verified requirements whose code this change rewrote. List them; they
55
+ must be verified again.
56
+ 5. Create signals for what is not met or broken (see the rules), then return the report.
57
+ ${COMMON}`,
58
+
59
+ "qa-security-reviewer.md": `---
60
+ name: qa-security-reviewer
61
+ description: Reviews a change against the project's security requirements (including those inherited from the requirement library) and runs qatools doctor. Use before setting a requirement implemented or a backlog item ready_for_test when the change touches access, data, secrets, input or dependencies. Read-only; findings become qa-tools signals.
62
+ tools: Read, Grep, Glob, Bash
63
+ ---
64
+ ${MARK}
65
+
66
+ # qa-security-reviewer — review against the project's security requirements
67
+
68
+ You check a change against the security the project has DECIDED it needs — its own security requirements — and against
69
+ plain security mistakes.
70
+
71
+ ## Steps
72
+ 1. The project's security requirements: \`qatools requirements --json -o <tmp>.json\`, then those with category Security,
73
+ an epic about security, or a key starting with security., auth. or access (inherited library requirements included).
74
+ 2. Which of them the change touches: access control and authorisation (who may read or change what — backend checks,
75
+ database rules, API guards), authentication, storing or sending secrets, personal data, input validation and output
76
+ encoding, file uploads, dependencies. For each one touched: does the change still meet its criteria? Cite file:line.
77
+ 3. Plain mistakes even without a requirement: a check done only in the client, a query not scoped to the user's project
78
+ or tenant, a secret in code or config, injection (SQL, shell, HTML), missing rate limit on a public endpoint, a new
79
+ dependency with known vulnerabilities (\`npm audit\` / the ecosystem's equivalent if available). Report these as
80
+ findings too; if no requirement covers one, create a GAP signal so the user can decide.
81
+ 4. Run \`qatools doctor\` (and \`--history\` if the change removed a secret). Report what it found; a secret in history
82
+ means the key must be rotated.
83
+ 5. Create signals for what is not met or broken (see the rules), then return the report.
84
+ ${COMMON}`,
85
+ };
86
+
87
+ // Write the agents; files the repo already has under other names are untouched. Returns the names written.
88
+ function installAgents(cwd = process.cwd()) {
89
+ const dir = path.join(cwd, ".claude", "agents");
90
+ fs.mkdirSync(dir, { recursive: true });
91
+ const written = [];
92
+ for (const [name, text] of Object.entries(AGENTS)) {
93
+ const file = path.join(dir, name);
94
+ if (fs.existsSync(file) && !fs.readFileSync(file, "utf8").includes(MARK)) continue; // the repo's own file with this name
95
+ fs.writeFileSync(file, text);
96
+ written.push(name);
97
+ }
98
+ return written;
99
+ }
100
+
101
+ const agentsInstalled = (cwd = process.cwd()) =>
102
+ Object.keys(AGENTS).every(n => { try { return fs.readFileSync(path.join(cwd, ".claude", "agents", n), "utf8").includes(MARK); } catch { return false; } });
103
+
104
+ module.exports = { AGENTS, installAgents, agentsInstalled, MARK };
package/hooks.js CHANGED
@@ -60,7 +60,7 @@ function refusalText(res) {
60
60
  ` ${res.reason}`,
61
61
  " Examples: feat(reports.export): … fix(AVV-15): … chore: … (housekeeping)",
62
62
  ...(res.suggestions?.length ? [` Keys that match words in your message: ${res.suggestions.join(", ")}`] : []),
63
- " All keys: qatools requirement-keys",
63
+ " All keys: qatools requirement-keys. Just created the requirement? qatools brief refreshes the key list.",
64
64
  ].join("\n");
65
65
  }
66
66
 
@@ -103,6 +103,46 @@ function installCommitHook(cwd = process.cwd()) {
103
103
  return { installed: true, path: hook };
104
104
  }
105
105
 
106
+ // security.no-secrets-in-commits: the pre-commit hook; an existing one is kept as pre-commit.local and runs first
107
+ const PRE_MARK = "# qatools pre-commit hook";
108
+ function installPreCommitHook(cwd = process.cwd()) {
109
+ const dir = gitPath(cwd, "hooks");
110
+ if (!dir) return { installed: false, reason: "not a git repository" };
111
+ fs.mkdirSync(dir, { recursive: true });
112
+ const hook = path.join(dir, "pre-commit");
113
+ if (fs.existsSync(hook)) {
114
+ const cur = fs.readFileSync(hook, "utf8");
115
+ if (cur.includes(PRE_MARK)) return { installed: true, already: true, path: hook };
116
+ fs.renameSync(hook, path.join(dir, "pre-commit.local"));
117
+ }
118
+ fs.writeFileSync(hook, [
119
+ "#!/bin/sh",
120
+ PRE_MARK + " (security.no-secrets-in-commits) — written by qatools init",
121
+ 'HOOKS="$(dirname "$0")"',
122
+ 'if [ -x "$HOOKS/pre-commit.local" ]; then "$HOOKS/pre-commit.local" "$@" || exit $?; fi',
123
+ 'command -v qatools >/dev/null 2>&1 || exit 0',
124
+ '# Only the qa-tools check itself may refuse; an old CLI or any other failure lets the commit through',
125
+ 'OUT="$(qatools secret-check 2>&1)"; CODE=$?',
126
+ 'if [ $CODE -eq 1 ] && printf "%s" "$OUT" | grep -q "qa-tools: this commit adds a secret"; then printf "%s\\n" "$OUT" >&2; exit 1; fi',
127
+ 'exit 0',
128
+ "",
129
+ ].join("\n"));
130
+ try { fs.chmodSync(hook, 0o755); } catch {}
131
+ return { installed: true, path: hook };
132
+ }
133
+
134
+ // Which qa-tools hooks a repo has: { commitMsg, preCommit, sessionBrief }
135
+ function hookStatus(cwd = process.cwd()) {
136
+ const dir = gitPath(cwd, "hooks");
137
+ const has = (name, mark) => { try { return !!dir && fs.readFileSync(path.join(dir, name), "utf8").includes(mark); } catch { return false; } };
138
+ let sessionBrief = false;
139
+ try {
140
+ const s = JSON.parse(fs.readFileSync(path.join(cwd, ".claude", "settings.json"), "utf8"));
141
+ sessionBrief = (s.hooks?.SessionStart || []).some(m => (m.hooks || []).some(h => h.command === BRIEF_COMMAND));
142
+ } catch {}
143
+ return { commitMsg: has("commit-msg", MARK), preCommit: has("pre-commit", PRE_MARK), sessionBrief };
144
+ }
145
+
106
146
  // Add the SessionStart hook to .claude/settings.json, keeping everything already there
107
147
  const BRIEF_COMMAND = "qatools brief --hook";
108
148
  function installSessionHook(cwd = process.cwd()) {
@@ -121,4 +161,4 @@ function installSessionHook(cwd = process.cwd()) {
121
161
  return { installed: true, path: file };
122
162
  }
123
163
 
124
- module.exports = { checkMessage, suggestKeys, runCommitCheck, installCommitHook, installSessionHook, writeKeyCache, readKeyCache, BRIEF_COMMAND, MARK };
164
+ module.exports = { checkMessage, suggestKeys, runCommitCheck, installCommitHook, installPreCommitHook, hookStatus, installSessionHook, writeKeyCache, readKeyCache, BRIEF_COMMAND, MARK, PRE_MARK };
package/impact.js CHANGED
@@ -183,4 +183,30 @@ function unmergedBranches({ cwd, main, knownKeys }) {
183
183
  return out.sort((x, y) => String(y.newest).localeCompare(String(x.newest)));
184
184
  }
185
185
 
186
- module.exports = { computeImpact, isGitRepo, baseForDate, keysIn, DEFAULT_IGNORE, mainRef, notOnMain, unmergedBranches, evidenceCommit };
186
+ // traceability.evidence-commit-backfill: for verified requirements without an evidence commit, the newest commit on
187
+ // main whose message names the key exactly (not just a longer key that starts the same way)
188
+ // A commit names a key as its own when the key is in the subject, or in the body as "key: …" at the start of a line,
189
+ // "(key)" or "[req:key]" — not when the body only mentions it, e.g. as a quoted example.
190
+ function namesAsOwn(subject, body, key, known) {
191
+ if (keysIn(subject, known).has(key)) return true;
192
+ const k = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
193
+ // …or as part of a list of keys that starts a line: "a.b, c.d, e.f. From …"
194
+ // …or "Implements / Fixes / Closes / Resolves / Refs <key>"
195
+ return new RegExp(`(^|\\n)\\s*[-*]?\\s*([a-z0-9][\\w-]*(\\.[\\w-]+)+\\s*,\\s*)*${k}(?![\\w-]|\\.[\\w-])\\s*[:,.]?(\\s|$)|\\(${k}\\)|\\[req:${k}\\]|\\b(implements|fixes|closes|resolves|refs)\\s+${k}(?![\\w-]|\\.[\\w-])`, "i").test(body);
196
+ }
197
+
198
+ function findKeyCommits({ cwd, requirements, main }) {
199
+ const known = new Set(requirements.map(r => r.key).filter(Boolean));
200
+ const found = [], none = [];
201
+ for (const r of requirements) {
202
+ if (r.status !== "implemented" && r.status !== "tested") continue;
203
+ if (evidenceCommit(r)) continue;
204
+ const log = git(cwd, ["log", main, "-F", `--grep=${r.key}`, "-n", "30", "--format=%x1e%H%x1f%cs%x1f%s%x1f%B"]);
205
+ const hit = log.split("\x1e").filter(Boolean).map(c => c.split("\x1f")).find(([, , subject, body]) => namesAsOwn(subject || "", body || "", r.key, known));
206
+ if (hit) found.push({ id: r.id, key: r.key, commit: hit[0].trim(), date: hit[1], subject: (hit[2] || "").trim() });
207
+ else none.push(r.key);
208
+ }
209
+ return { found, none };
210
+ }
211
+
212
+ module.exports = { computeImpact, isGitRepo, baseForDate, keysIn, DEFAULT_IGNORE, mainRef, notOnMain, unmergedBranches, evidenceCommit, findKeyCommits };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qatools-cli",
3
- "version": "1.2.24",
3
+ "version": "1.2.26",
4
4
  "description": "CLI for QA Tools — requirements, epics, sprints, sessions, App Context sync, project agenda. The companion to qa-tools.com.",
5
5
  "scripts": {
6
6
  "release": "npm version patch --no-git-tag-version && npm publish",
@@ -15,7 +15,9 @@
15
15
  "impact.js",
16
16
  "context-sync.js",
17
17
  "hooks.js",
18
- "brief.js"
18
+ "brief.js",
19
+ "secrets.js",
20
+ "agents.js"
19
21
  ],
20
22
  "keywords": [
21
23
  "qa",
package/qatools.js CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  // traceability.commit-names-requirement: the git commit-msg hook calls this — no Firebase, no network
4
4
  if (process.argv[2] === "commit-check") process.exit(require("./hooks").runCommitCheck(process.argv[3]));
5
+ // security.no-secrets-in-commits: the git pre-commit hook calls this — no Firebase, no network
6
+ if (process.argv[2] === "secret-check") process.exit(require("./secrets").runSecretCheck());
5
7
 
6
8
  const { program } = require("commander");
7
9
  const { initializeApp } = require("@firebase/app");
@@ -883,6 +885,54 @@ program
883
885
  process.exit(0);
884
886
  });
885
887
 
888
+ // security.repo-doctor: secrets in files and history, and the qa-tools hooks. No network; exit 1 when a secret is found.
889
+ program
890
+ .command("doctor")
891
+ .description("Check this repo: secrets in tracked files (and in history with --history), qa-tools hooks, the synced CLAUDE.md block")
892
+ .option("--history", "Also search the whole git history (slower)")
893
+ .option("--json", "Output as JSON")
894
+ .action((options) => {
895
+ const cwd = process.cwd();
896
+ const { isGitRepo } = require("./impact");
897
+ if (!isGitRepo(cwd)) { console.error("qatools doctor must run in a git repository"); process.exit(1); }
898
+ const sec = require("./secrets");
899
+ const hooks = require("./hooks");
900
+ const { BEGIN_MARKER } = require("./context-sync");
901
+ const tracked = sec.scanTracked(cwd);
902
+ const history = options.history ? sec.scanHistory(cwd) : null;
903
+ const h = hooks.hookStatus(cwd);
904
+ const mdPath = fs.existsSync(path.join(cwd, ".claude", "CLAUDE.md")) ? path.join(cwd, ".claude", "CLAUDE.md") : path.join(cwd, "CLAUDE.md");
905
+ let md = "";
906
+ try { md = fs.readFileSync(mdPath, "utf8"); } catch {}
907
+ const synced = md.match(/Last synced: (\S+)/);
908
+ const ageDays = synced ? Math.floor((Date.now() - Date.parse(synced[1])) / 86400000) : null;
909
+ const result = {
910
+ secretsInFiles: tracked, secretsInHistory: history,
911
+ hooks: h, claudeMd: { file: path.relative(cwd, mdPath), syncedBlock: md.includes(BEGIN_MARKER), lastSynced: synced ? synced[1] : null, ageDays },
912
+ };
913
+ const bad = tracked.length || (history && history.length);
914
+ if (options.json) { console.log(JSON.stringify(result, null, 2)); process.exit(bad ? 1 : 0); }
915
+ const warn = [];
916
+ if (tracked.length) {
917
+ console.log(`✗ ${tracked.length} secret(s) in tracked files — remove them, move them to a secret store, and rotate them if they were pushed:`);
918
+ for (const f of tracked.slice(0, 30)) console.log(` ${f.file}:${f.line} ${f.kind} (${f.masked})`);
919
+ }
920
+ if (history && history.length) {
921
+ console.log(`✗ ${history.length} secret(s) in git history — removing the line does not remove it from history: rotate each one:`);
922
+ for (const f of history.slice(0, 30)) console.log(` ${f.commit} ${f.date} ${f.file} ${f.kind} (${f.masked})`);
923
+ }
924
+ if (!h.commitMsg) warn.push("commit message check (feat(<key>) / fix(AVV-n))");
925
+ if (!h.preCommit) warn.push("secret check before commit");
926
+ if (!h.sessionBrief) warn.push("session brief for Claude Code");
927
+ if (!require("./agents").agentsInstalled(cwd)) warn.push("review agents (qa-reviewer, qa-security-reviewer)");
928
+ if (warn.length) console.log(`⚠ Missing qa-tools hooks: ${warn.join(", ")}. Run: qatools init`);
929
+ if (!result.claudeMd.syncedBlock) console.log(`⚠ ${result.claudeMd.file} has no synced qa-tools block. Run: qatools sync`);
930
+ else if (ageDays !== null && ageDays > 14) console.log(`⚠ ${result.claudeMd.file} was synced ${ageDays} days ago. Run: qatools sync`);
931
+ if (!options.history) console.log(" (git history not searched — add --history)");
932
+ if (!bad && !warn.length && result.claudeMd.syncedBlock && !(ageDays > 14)) console.log("✓ All checks passed.");
933
+ process.exit(bad ? 1 : 0);
934
+ });
935
+
886
936
  // context.session-brief: what a session starts with. --hook: for Claude Code's SessionStart hook — never blocks,
887
937
  // prints nothing on any failure, gives up after 5 seconds.
888
938
  program
@@ -1258,8 +1308,9 @@ program
1258
1308
  console.log("No templates found.");
1259
1309
  } else {
1260
1310
  for (const t of templates) {
1261
- const statusIcon = { draft: "📝", pending_review: "🔍", approved: "✅", rejected: "❌" }[t.status] || "?";
1262
- console.log(` ${statusIcon} ${t.name} v${t.version || "-"} [${t.status}] (${t.sectionsCount} sections) ID: ${t.id}`);
1311
+ const statusIcon = { draft: "📝", pending_review: "🔍", approved: "✅", rejected: "❌", superseded: "↷" }[t.status] || "?";
1312
+ const ch = t.changes ? ` — since v${t.changes.fromVersion}: +${t.changes.added.length} −${t.changes.removed.length}` : "";
1313
+ console.log(` ${statusIcon} ${t.name} v${t.versionNumber || 1} [${t.status}] ${(t.uploadedAt || "").slice(0, 10)} (${t.testCount ?? "?"} tests)${ch} ID: ${t.id}`);
1263
1314
  if (t.reviewComment) console.log(` 💬 ${t.reviewComment}`);
1264
1315
  }
1265
1316
  }
@@ -1434,6 +1485,13 @@ program
1434
1485
  }
1435
1486
  }
1436
1487
  console.log(`\n\nDone. ${created} created, ${skipped} skipped (exact dup), ${similar} blocked (semantically similar).`);
1488
+ // traceability.commit-names-requirement: new keys are committable right away
1489
+ if (created) {
1490
+ try {
1491
+ const { requirements } = (await httpsCallable(functions, "listRequirements")({ projectId, status: null })).data;
1492
+ require("./hooks").writeKeyCache(process.cwd(), projectId, requirements.map(r => r.key).filter(Boolean));
1493
+ } catch {}
1494
+ }
1437
1495
  if (similar > 0) {
1438
1496
  console.log(`\nSimilar matches found — review before deciding:\n`);
1439
1497
  for (const rep of similarReports) {
@@ -1926,10 +1984,32 @@ program
1926
1984
  process.exit(0);
1927
1985
  });
1928
1986
 
1987
+ // testing.protocol-versions: close a protocol (e.g. a superseded version) without the web app
1988
+ program
1989
+ .command("template-reject")
1990
+ .description("Reject a test protocol with a reason (min 5 chars), e.g. a superseded version")
1991
+ .requiredOption("-i, --id <id>", "Test protocol ID")
1992
+ .requiredOption("--reason <text>", "Why it is rejected — kept with the protocol")
1993
+ .option("--project <id>", "Target project ID (overrides default)")
1994
+ .option("-e, --email <email>", "Login email")
1995
+ .option("-p, --password <password>", "Login password")
1996
+ .action(async (options) => {
1997
+ if (String(options.reason || "").trim().length < 5) { console.error("✗ --reason needs at least 5 characters"); process.exit(2); }
1998
+ await authenticate(options);
1999
+ const projectId = options.project || getCredentials()?.projectId;
2000
+ if (!projectId) { console.error("No project configured. Run: qatools setup, or pass --project <id>"); process.exit(1); }
2001
+ try {
2002
+ await httpsCallable(functions, "rejectTemplate")({ projectId, templateId: options.id, reason: options.reason });
2003
+ console.log(`✓ Test protocol ${options.id} rejected.`);
2004
+ } catch (e) { console.error("Failed:", e.message); process.exit(1); }
2005
+ process.exit(0);
2006
+ });
2007
+
1929
2008
  program
1930
2009
  .command("template-upload")
1931
- .description("Upload a test template JSON to the project")
2010
+ .description("Upload a test protocol JSON. The same name as an existing protocol makes the next version (v1, v2 …); older unreviewed versions are superseded")
1932
2011
  .requiredOption("-f, --file <path>", "Path to template JSON file")
2012
+ .option("--replaces <id>", "Make this the next version of that protocol (even under another name) and supersede it")
1933
2013
  .option("--project <id>", "Target project ID (overrides default)")
1934
2014
  .option("-e, --email <email>", "Login email")
1935
2015
  .option("-p, --password <password>", "Login password")
@@ -1952,9 +2032,13 @@ program
1952
2032
  name: data.name,
1953
2033
  version: data.version || "",
1954
2034
  sections: data.sections,
2035
+ ...(options.replaces ? { replaces: options.replaces } : {}),
1955
2036
  });
1956
- console.log(`Template uploaded: ${result.data.name} (${result.data.sectionsCount} sections)`);
1957
- console.log(`ID: ${result.data.id}`);
2037
+ const r = result.data;
2038
+ console.log(`Template uploaded: ${r.name} v${r.versionNumber || 1} (${r.sectionsCount} sections), pending review`);
2039
+ console.log(`ID: ${r.id}`);
2040
+ if (r.changes) console.log(`Since v${r.changes.fromVersion}: added ${r.changes.added.join(", ") || "none"}; removed ${r.changes.removed.join(", ") || "none"}`);
2041
+ if (r.superseded?.length) console.log(`Superseded: ${r.superseded.join(", ")}`);
1958
2042
  } catch (e) {
1959
2043
  console.error("Failed:", e.message);
1960
2044
  process.exit(1);
@@ -2046,7 +2130,7 @@ program
2046
2130
  .description("Update one or more backlog items. status=resolved REQUIRES --evidence (cite the verification).")
2047
2131
  .option("-f, --file <path>", "JSON file with updates: { '<id>': { status, evidence, comment, ... } }")
2048
2132
  .option("-i, --id <id>", "Single item ID to update")
2049
- .option("--status <status>", "New status")
2133
+ .option("--status <status>", "New status: open, in_progress, on_hold (needs --comment saying what it waits for), ready_for_test, resolved, rejected")
2050
2134
  .option("--evidence <text>", "REQUIRED when status=resolved (except for GAP). Cite the verification: test output, file:line, expected vs actual, or 'no test applies because <reason>'.")
2051
2135
  .option("--attach <files...>", "Image file(s) as evidence (png/jpg/webp/gif, max 4 MB each)")
2052
2136
  .option("--comment <text>", "Add comment")
@@ -2092,6 +2176,13 @@ program
2092
2176
  process.exit(1);
2093
2177
  }
2094
2178
 
2179
+ // backlog.on-hold-status: putting an item on hold says what it waits for (also checked by the backend)
2180
+ const noWait = Object.entries(updates).filter(([, u]) => u.status === "on_hold" && String(u.comment || "").trim().length < 5).map(([id]) => id);
2181
+ if (noWait.length) {
2182
+ console.error(`✗ Refusing status=on_hold without --comment saying what it waits for (min 5 chars): ${noWait.join(", ")}`);
2183
+ process.exit(2);
2184
+ }
2185
+
2095
2186
  // Pre-flight: status=resolved needs evidence, and AVV/DEF/IMP must have been through ready_for_test.
2096
2187
  // GAP is exempt (its "fix" is writing a requirement, not testing code).
2097
2188
  if (Object.values(updates).some(u => u.status === 'resolved')) {
@@ -2607,10 +2698,11 @@ program
2607
2698
  console.error(" ✗ Login failed:", e.message);
2608
2699
  process.exit(1);
2609
2700
  }
2610
- } else if (existingCreds?.projectId) {
2611
- console.log(`Step 1/5: Already logged in: ${existingCreds.projectName || existingCreds.projectId}`);
2612
- // Ensure Firebase auth is initialized
2701
+ } else if (existingCreds?.projectId || process.env.QATOOLS_API_KEY) {
2702
+ // Ensure Firebase auth is initialized; with QATOOLS_API_KEY that also picks the key's project for this run
2613
2703
  try { await authenticate(options); } catch (e) { /* best effort */ }
2704
+ const now = getCredentials(); // AVV-38: name the project this run actually uses
2705
+ console.log(`Step 1/5: ${process.env.QATOOLS_API_KEY ? "Logged in with QATOOLS_API_KEY" : "Already logged in"}: ${now?.projectName || now?.projectId || existingCreds?.projectName || ""}`);
2614
2706
  } else {
2615
2707
  console.log("Step 1/5: No API key provided. Run: qatools init --api-key <key>");
2616
2708
  console.log(" Get your key from: qa-tools.com → your project → Members → Claude AI Integration");
@@ -2887,6 +2979,8 @@ App Context → Epics → Requirements → Sprint → Implementation → Test
2887
2979
 
2888
2980
  5. NEVER create requirements directly as backlog items. If a GAP needs to become a requirement, flag it — the user decides.
2889
2981
 
2982
+ 5b. Before you set a requirement \`implemented\` or a backlog item \`ready_for_test\`: run the \`qa-reviewer\` subagent, and \`qa-security-reviewer\` when the change touches access, data, secrets, input or dependencies. Fix what they find, or leave it recorded as the signals they created — never skip them to save time.
2983
+
2890
2984
  6. Check for pending test sessions:
2891
2985
  \`qatools pending\`
2892
2986
  \`qatools read-session -s <id> -o session.json\`
@@ -3256,7 +3350,8 @@ If the verification fails: do NOT mark tested. Go back to step 1, find the discr
3256
3350
  }
3257
3351
 
3258
3352
  // 3. Update or create CLAUDE.md
3259
- const claudeMdPath = path.join(cwd, "CLAUDE.md");
3353
+ // AVV-39: a repo that keeps its CLAUDE.md in .claude/ gets the section there, not a second CLAUDE.md in the root
3354
+ const claudeMdPath = fs.existsSync(path.join(cwd, ".claude", "CLAUDE.md")) ? path.join(cwd, ".claude", "CLAUDE.md") : path.join(cwd, "CLAUDE.md");
3260
3355
  const qaSection = `
3261
3356
  ## QA-Tools Integration
3262
3357
 
@@ -3264,9 +3359,7 @@ This project uses qa-tools (https://qa-tools.com) for requirement-driven AI deve
3264
3359
 
3265
3360
  **Read \`.claude/skills/qa-purpose.md\` before doing anything in this project.** It explains what qa-tools expects from the AI: not a coding assistant, a substitute for the developer.
3266
3361
 
3267
- \`\`\`bash
3268
- export QATOOLS_API_KEY=${apiKey || "<din-nyckel>"}
3269
- \`\`\`
3362
+ The CLI is already logged in on this machine (\`qatools init\`). Never write an API key into this file or anywhere in the repo — it is a secret (AVV-18). In CI, give it as a secret environment variable QATOOLS_API_KEY.
3270
3363
 
3271
3364
  ### Commands
3272
3365
  - \`qatools context\` — read the project's rule book (App Context)
@@ -3295,13 +3388,13 @@ See \`.claude/skills/\` for role-specific instructions. Start with \`qa-purpose.
3295
3388
  const existing = fs.readFileSync(claudeMdPath, "utf8");
3296
3389
  if (!existing.includes("QA-Tools Integration")) {
3297
3390
  fs.appendFileSync(claudeMdPath, "\n" + qaSection);
3298
- console.log("\n✓ Added QA-Tools section to CLAUDE.md");
3391
+ console.log(`\n✓ Added QA-Tools section to ${path.relative(cwd, claudeMdPath)}`);
3299
3392
  } else {
3300
- console.log("\n⚠ CLAUDE.md already has QA-Tools section (skipped)");
3393
+ console.log(`\n⚠ ${path.relative(cwd, claudeMdPath)} already has QA-Tools section (skipped)`);
3301
3394
  }
3302
3395
  } else {
3303
3396
  fs.writeFileSync(claudeMdPath, "# Project\n" + qaSection);
3304
- console.log("\n✓ Created CLAUDE.md with QA-Tools section");
3397
+ console.log(`\n✓ Created ${path.relative(cwd, claudeMdPath)} with QA-Tools section`);
3305
3398
  }
3306
3399
 
3307
3400
  // Step 4/5: Sync App Context into CLAUDE.md
@@ -3355,6 +3448,10 @@ See \`.claude/skills/\` for role-specific instructions. Start with \`qa-purpose.
3355
3448
  console.log(sh.installed ? ` ✓ Session brief hook ${sh.already ? "already in" : "added to"} .claude/settings.json (qatools brief --hook)` : ` ⚠ Session brief hook not added: ${sh.reason}`);
3356
3449
  const ch = hooks.installCommitHook(cwd);
3357
3450
  console.log(ch.installed ? ` ✓ Commit message check ${ch.already ? "already installed" : "installed"} (${path.relative(cwd, ch.path)})` : ` ⚠ Commit message check not installed: ${ch.reason}`);
3451
+ const ag = require("./agents").installAgents(cwd);
3452
+ console.log(ag.length ? ` ✓ Review agents written to .claude/agents/: ${ag.join(", ")}` : " ⚠ Review agents not written: the repo has its own files with these names");
3453
+ const pc = hooks.installPreCommitHook(cwd);
3454
+ console.log(pc.installed ? ` ✓ Secret check ${pc.already ? "already installed" : "installed"} (${path.relative(cwd, pc.path)})` : ` ⚠ Secret check not installed: ${pc.reason}`);
3358
3455
  } catch (e) {
3359
3456
  console.log(` ⚠ Hooks skipped (${e.message})`);
3360
3457
  }
@@ -3865,7 +3962,7 @@ const MCP_PROTOCOL_VERSIONS = ["2025-11-25", "2025-06-18", "2025-03-26", "2024-1
3865
3962
  const MCP_INSTRUCTIONS = `qa-tools: requirements-driven QA. Rules that apply to every tool:
3866
3963
  - Build from requirements. Planning is Epics → Requirements → Sprints (requirement.sprintId).
3867
3964
  - The backlog is signals (bug=AVV, requirement_gap=GAP, technical_defect=DEF, improvement=IMP), not a task list.
3868
- - Backlog flow: open → in_progress → ready_for_test → resolved. Set ready_for_test when you believe a fix is done; resolved needs evidence (min 10 chars) and, except GAP, a prior ready_for_test.
3965
+ - Backlog flow: open → in_progress → ready_for_test → resolved. Set ready_for_test when you believe a fix is done; resolved needs evidence (min 10 chars) and, except GAP, a prior ready_for_test. on_hold = deliberately waiting for something outside the team (a decision, information); it needs a comment saying what it waits for, and goes back to open or in_progress when the wait is over.
3869
3966
  - Requirement status implemented/tested needs evidence: test output, file:line, expected vs actual, or "no test applies because <reason>".
3870
3967
  - Requirement flow: approved → in_progress → implemented → tested. When you start implementing an approved requirement, set status in_progress and assignedTo "me" (update_requirement); if you stop without finishing, set it back to approved.
3871
3968
  - Requirements with a libraryId inherit from the account's requirement library: never try to change their title, description, criteria, type or category — only applicability, status, sprint, epic and assignee are the project's. When get_agenda lists libraryReverify, verify those requirements again against the new library text.
@@ -3879,7 +3976,7 @@ const mcpBool = (description) => ({ type: "boolean", description });
3879
3976
  const mcpStrArray = (description, items = { type: "string" }) => ({ type: "array", items, description });
3880
3977
  const MCP_PROJECT_PROP = { projectId: mcpStr("Project ID. Defaults to the configured project.") };
3881
3978
  const MCP_REQ_STATUSES = ["draft", "approved", "in_progress", "rejected", "implemented", "tested", "superseded"];
3882
- const MCP_BACKLOG_STATUSES = ["open", "in_progress", "ready_for_test", "resolved", "rejected"];
3979
+ const MCP_BACKLOG_STATUSES = ["open", "in_progress", "on_hold", "ready_for_test", "resolved", "rejected"];
3883
3980
  const MCP_BACKLOG_TYPES = ["bug", "improvement", "requirement_gap", "technical_defect"];
3884
3981
  const MCP_PRIORITY = mcpInt("Priority: 1 (high), 2 (medium), 3 (low)", { enum: [1, 2, 3] });
3885
3982
 
@@ -4331,6 +4428,7 @@ const mcpTools = [
4331
4428
  if (a.comment) { u.comment = a.comment; u.authorName = a.authorName || "Claude"; }
4332
4429
  if (a.tags) u.tags = a.tags.map(t => t.trim().toLowerCase());
4333
4430
  if (Object.keys(u).length === 0) throw new McpToolError("No fields to update.");
4431
+ if (u.status === "on_hold" && String(u.comment || "").trim().length < 5) throw new McpToolError("Refusing status=on_hold without a comment saying what it waits for (min 5 chars).");
4334
4432
  const updates = { [a.itemId]: u };
4335
4433
  if (u.status === "resolved") {
4336
4434
  // Fail closed: unlike the interactive CLI, the MCP server refuses when the pre-check can't run.
@@ -4383,6 +4481,27 @@ const mcpTools = [
4383
4481
  inputSchema: mcpSchema(),
4384
4482
  run: async () => require("./context-sync").requestsIntegrationPrompt(getCredentials()?.projectName || ""),
4385
4483
  },
4484
+ {
4485
+ name: "new_app_questions",
4486
+ description: "projects.new-app-guide: the questions for a new app (purpose, users, client, backend and hosting, database, login, language, where code and CI live) with their choices. Ask them one at a time; then create_project and apply_new_app_answers. Prompt new_app has the whole flow.",
4487
+ inputSchema: { type: "object", properties: {}, additionalProperties: false },
4488
+ run: async () => mcpCall("getNewAppQuestions", {}),
4489
+ },
4490
+ {
4491
+ name: "create_project",
4492
+ description: "Create a new qa-tools project owned by the logged-in user. Needs the user's own login (qatools login --google); refused with a Claude API key, which belongs to one project.",
4493
+ inputSchema: { type: "object", properties: { name: mcpStr("The app's name"), description: mcpStr("One sentence: what it does") }, required: ["name"], additionalProperties: false },
4494
+ run: async (a) => mcpCall("createProject", { name: a.name, description: a.description || "" }),
4495
+ },
4496
+ {
4497
+ name: "apply_new_app_answers",
4498
+ description: "Fill a project's App Context from the new-app answers: { purpose, users, client, backend, database, auth: [], languages: [], codeHost } with choice values from new_app_questions, \"other:<text>\" or \"undecided\". Fields that already have content are reported as conflicts and not saved unless overwrite is true — ask the user first.",
4499
+ inputSchema: mcpSchema({
4500
+ answers: { type: "object", description: "The answers, keyed by question id", additionalProperties: true },
4501
+ overwrite: mcpBool("Replace fields that already have content — only after the user confirmed"),
4502
+ }, ["answers"]),
4503
+ run: async (a) => mcpCall("applyNewAppAnswers", { projectId: a.projectId || getCredentials()?.projectId, answers: a.answers, overwrite: !!a.overwrite }),
4504
+ },
4386
4505
  {
4387
4506
  name: "baseline_prompt",
4388
4507
  description: "Procedure for reconstructing requirements for what is already built (requirements.baseline-from-code), with the project's language, its epics and all existing requirements. Use when code exists that qa-tools has no requirements for.",
@@ -4475,12 +4594,13 @@ const mcpTools = [
4475
4594
  additionalProperties: false,
4476
4595
  },
4477
4596
  },
4597
+ replaces: mcpStr("Optional: the id of a protocol this is the next version of (it is superseded). The same name as an existing protocol makes the next version anyway"),
4478
4598
  }, ["sprintId", "name", "sections"]),
4479
4599
  run: async (a) => {
4480
4600
  for (const sec of a.sections) for (const t of sec.tests || []) {
4481
4601
  if (!String(t.purpose || "").trim()) throw new McpToolError(`Test "${t.name}" needs a purpose saying why a person must perform it`);
4482
4602
  }
4483
- const r = await mcpCall("uploadTemplate", { projectId: a.projectId, sprintId: a.sprintId, name: a.name, sections: a.sections });
4603
+ const r = await mcpCall("uploadTemplate", { projectId: a.projectId, sprintId: a.sprintId, name: a.name, sections: a.sections, ...(a.replaces ? { replaces: a.replaces } : {}) });
4484
4604
  return { ...r, next: "Pending review. A project member approves it in qa-tools under Test protocols." };
4485
4605
  },
4486
4606
  },
@@ -4578,6 +4698,17 @@ Follow these steps in order:
4578
4698
  arguments: [{ name: "projectId", description: "Project ID (default: configured project)", required: false }],
4579
4699
  text: (a) => dailyAgendaRunPrompt(a.projectId),
4580
4700
  },
4701
+ {
4702
+ name: "new_app",
4703
+ description: "projects.new-app-guide: start a new app — ask the user how it should be built, create the project and fill its App Context.",
4704
+ arguments: [{ name: "name", description: "The app's name, if already known", required: false }],
4705
+ text: (a) => `Help the user start a new app in qa-tools${a.name ? ` called "${a.name}"` : ""}.
4706
+
4707
+ 1. Get the questions: new_app_questions. Ask them ONE AT A TIME in the user's language, offering the listed choices plus "other" and "not decided yet". Let the user skip any question. Do not choose for the user; "not decided yet" is a fine answer.
4708
+ 2. When you have the answers and a name, create the project: create_project (name, description = the purpose). It needs the user's own login; if it is refused because the MCP server runs with a Claude API key, tell the user to create the project in the web app (or run qatools login --google) and give you its ID, then continue with step 3.
4709
+ 3. Fill its App Context: apply_new_app_answers with projectId and the answers (choice values, "other:<text>" or "undecided"). If it reports conflicts (an App Context that already has content), show the user which fields would change and call it again with overwrite: true only if they say yes.
4710
+ 4. Tell the user the next step: in the new repo, qatools setup (pick the project) and qatools init; then you can propose the first epics from the App Context.`,
4711
+ },
4581
4712
  ];
4582
4713
 
4583
4714
  function mcpValidateArgs(tool, args) {
@@ -4834,6 +4965,44 @@ async function runImpact({ projectId, since, sprint, flag, main, cwd = process.c
4834
4965
  return result;
4835
4966
  }
4836
4967
 
4968
+ // traceability.evidence-commit-backfill: give verified requirements without an evidence commit the commit that built them
4969
+ program
4970
+ .command("evidence-backfill")
4971
+ .description("Verified requirements without an evidence commit: find the newest commit on main naming the key (run in the repo). --apply records it as a commit link — not a new verification")
4972
+ .option("--apply", "Record the commits in qa-tools (default: only show them)")
4973
+ .option("--main <ref>", "The main branch (default: origin/HEAD, else origin/main, origin/master, main, master)")
4974
+ .option("--project <id>", "Target project ID")
4975
+ .action(async (options) => {
4976
+ const { isGitRepo, mainRef, findKeyCommits } = require("./impact");
4977
+ const cwd = process.cwd();
4978
+ if (!isGitRepo(cwd)) { console.error("qatools evidence-backfill must run in the project's git repository"); process.exit(1); }
4979
+ const main = mainRef(cwd, options.main);
4980
+ if (!main) { console.error("No main branch found — pass --main <ref>"); process.exit(1); }
4981
+ await authenticate(options);
4982
+ const projectId = options.project || getCredentials()?.projectId;
4983
+ if (!projectId) { console.error("No project configured. Run: qatools setup, or pass --project <id>"); process.exit(1); }
4984
+ try {
4985
+ const { requirements } = (await httpsCallable(functions, "listRequirements")({ projectId, status: null })).data;
4986
+ const latest = new Map();
4987
+ for (const r of requirements) {
4988
+ if (r.status === "superseded") continue;
4989
+ const cur = latest.get(r.key);
4990
+ if (!cur || (r.version || 1) > (cur.version || 1)) latest.set(r.key, r);
4991
+ }
4992
+ const { found, none } = findKeyCommits({ cwd, requirements: [...latest.values()], main });
4993
+ console.log(`${found.length} verified requirement(s) without an evidence commit have one on ${main}:\n`);
4994
+ for (const f of found) console.log(` ${f.key}: ${f.commit.slice(0, 8)} ${f.date} ${f.subject.slice(0, 70)}`);
4995
+ if (none.length) console.log(`\n${none.length} have no commit on ${main} that names their key: ${none.join(", ")}`);
4996
+ if (!options.apply) { if (found.length) console.log("\nAdd --apply to record these as commit links (not new verifications)."); process.exit(0); }
4997
+ if (!found.length) process.exit(0);
4998
+ const res = (await httpsCallable(functions, "linkEvidenceCommit")({ projectId, items: found.map(f => ({
4999
+ requirementId: f.id, commit: f.commit, how: `newest commit on ${main} naming ${f.key}: ${f.subject.slice(0, 120)} (${f.date})`,
5000
+ })) })).data;
5001
+ console.log(`\nRecorded ${res.linked.length} commit link(s)${res.skipped.length ? `, skipped ${res.skipped.length}` : ""}.`);
5002
+ } catch (e) { console.error("Failed:", e.message); process.exit(1); }
5003
+ process.exit(0);
5004
+ });
5005
+
4837
5006
  // requirements.done-means-merged: branches with requirement work that is not merged
4838
5007
  program
4839
5008
  .command("branches")
package/secrets.js ADDED
@@ -0,0 +1,115 @@
1
+ // security.no-secrets-in-commits and security.repo-doctor: find secrets in text, a staged commit, tracked files and
2
+ // history. Values are never returned whole — only their kind and last 4 characters. No dependencies, no network.
3
+ const { execFileSync } = require("child_process");
4
+
5
+ // Firebase web API keys (AIza…) are public by design and are deliberately not here.
6
+ const PATTERNS = [
7
+ { kind: "qa-tools API key", re: /\b(?:qa|qp|fr|wh)-[A-Za-z0-9]{32}\b/g },
8
+ { kind: "Anthropic API key", re: /\bsk-ant-[A-Za-z0-9_-]{20,}/g },
9
+ { kind: "GitHub token", re: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36}\b|\bgithub_pat_[A-Za-z0-9_]{60,}/g },
10
+ { kind: "npm token", re: /\bnpm_[A-Za-z0-9]{36}\b/g },
11
+ { kind: "AWS access key", re: /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g },
12
+ { kind: "Stripe live key", re: /\b(?:sk|rk)_live_[A-Za-z0-9]{20,}/g },
13
+ { kind: "SendGrid API key", re: /\bSG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}\b/g },
14
+ { kind: "private key", re: /-----BEGIN (?:RSA |EC |DSA |OPENSSH |PGP |ENCRYPTED )?PRIVATE KEY(?: BLOCK)?-----/g },
15
+ ];
16
+ // One regex for git's -G/grep: the alternatives above, without JS-only syntax
17
+ const GIT_PATTERN = "(qa|qp|fr|wh)-[A-Za-z0-9]{32}|sk-ant-[A-Za-z0-9_-]{20,}|(ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36}|github_pat_[A-Za-z0-9_]{60,}|npm_[A-Za-z0-9]{36}|(AKIA|ASIA)[0-9A-Z]{16}|(sk|rk)_live_[A-Za-z0-9]{20,}|SG\\.[A-Za-z0-9_-]{22}\\.[A-Za-z0-9_-]{43}|-----BEGIN [A-Z ]*PRIVATE KEY";
18
+
19
+ // Obvious placeholders are not secrets: alphabet or digit runs, a repeated character, or words like test/example
20
+ const SEQ = "abcdefghijklmnopqrstuvwxyz", DIG = "0123456789";
21
+ function isPlaceholder(value) {
22
+ const v = value.replace(/^(?:qa|qp|fr|wh|sk-ant|ghp|gho|ghu|ghs|ghr|github_pat|npm|sk_live|rk_live|SG)[-_.]?/i, "").toLowerCase();
23
+ if (/(test|example|sample|fake|dummy|placeholder|changeme|your|xxxx|redacted)/.test(v)) return true;
24
+ if (/(.)\1{5,}/.test(v)) return true;
25
+ for (let i = 0; i + 8 <= v.length; i++) {
26
+ const w = v.slice(i, i + 8);
27
+ if (SEQ.includes(w) || DIG.includes(w)) return true;
28
+ }
29
+ return false;
30
+ }
31
+ const ALLOW = "qatools-allow-secret";
32
+
33
+ const mask = (v) => v.startsWith("-----BEGIN") ? "-----BEGIN … PRIVATE KEY-----" : `…${v.slice(-4)}`;
34
+
35
+ // Secrets in one piece of text: [{ kind, masked, line }]
36
+ function scanText(text) {
37
+ const out = [];
38
+ const lines = String(text).split("\n");
39
+ lines.forEach((l, i) => {
40
+ if (l.includes(ALLOW)) return; // explicitly allowed line
41
+ for (const { kind, re } of PATTERNS) {
42
+ for (const m of l.matchAll(re)) {
43
+ if (kind !== "private key" && isPlaceholder(m[0])) continue;
44
+ out.push({ kind, masked: mask(m[0]), line: i + 1 });
45
+ }
46
+ }
47
+ });
48
+ return out;
49
+ }
50
+
51
+ const git = (cwd, args) => execFileSync("git", args, { cwd, encoding: "utf8", maxBuffer: 512 * 1024 * 1024, stdio: ["ignore", "pipe", "ignore"] });
52
+
53
+ // Secrets on the lines a staged commit adds: [{ file, line, kind, masked }]
54
+ function scanStaged(cwd = process.cwd()) {
55
+ const diff = git(cwd, ["diff", "--cached", "--no-color", "-U0", "--no-ext-diff"]);
56
+ const found = [];
57
+ let file = "", lineNo = 0;
58
+ for (const l of diff.split("\n")) {
59
+ if (l.startsWith("+++ ")) { file = l.slice(4).replace(/^b\//, ""); continue; }
60
+ const hunk = l.match(/^@@ -\d+(?:,\d+)? \+(\d+)/);
61
+ if (hunk) { lineNo = Number(hunk[1]); continue; }
62
+ if (l.startsWith("+") && !l.startsWith("+++")) {
63
+ for (const s of scanText(l.slice(1))) found.push({ file, line: lineNo, kind: s.kind, masked: s.masked });
64
+ lineNo++;
65
+ }
66
+ }
67
+ return found;
68
+ }
69
+
70
+ // Secrets in tracked files now: [{ file, line, kind, masked }]
71
+ function scanTracked(cwd = process.cwd()) {
72
+ let out = "";
73
+ try { out = git(cwd, ["grep", "-n", "-I", "-E", GIT_PATTERN]); } catch { return []; } // exit 1 = nothing found
74
+ const found = [];
75
+ for (const l of out.split("\n").filter(Boolean)) {
76
+ const m = l.match(/^([^:]+):(\d+):(.*)$/);
77
+ if (!m) continue;
78
+ for (const s of scanText(m[3])) found.push({ file: m[1], line: Number(m[2]), kind: s.kind, masked: s.masked });
79
+ }
80
+ return found;
81
+ }
82
+
83
+ // Secrets anywhere in history, added by some commit: [{ commit, date, file, kind, masked }], one per secret
84
+ function scanHistory(cwd = process.cwd()) {
85
+ let out = "";
86
+ try { out = git(cwd, ["log", "--all", "-p", "--no-color", "-U0", "--extended-regexp", "--format=@@@%h %cs", "-G", GIT_PATTERN]); } catch { return []; }
87
+ const found = [], seen = new Set();
88
+ let commit = "", date = "", file = "";
89
+ for (const l of out.split("\n")) {
90
+ if (l.startsWith("@@@")) { [commit, date] = l.slice(3).split(" "); continue; }
91
+ if (l.startsWith("+++ ")) { file = l.slice(4).replace(/^b\//, ""); continue; }
92
+ if (l.startsWith("+") && !l.startsWith("+++")) {
93
+ for (const s of scanText(l.slice(1))) {
94
+ const id = s.kind + s.masked;
95
+ if (seen.has(id)) continue;
96
+ seen.add(id);
97
+ found.push({ commit, date, file, kind: s.kind, masked: s.masked });
98
+ }
99
+ }
100
+ }
101
+ return found;
102
+ }
103
+
104
+ // Runs inside the pre-commit hook: exit 1 refuses the commit
105
+ function runSecretCheck(cwd = process.cwd()) {
106
+ let found;
107
+ try { found = scanStaged(cwd); } catch { return 0; }
108
+ if (!found.length) return 0;
109
+ console.error("✗ qa-tools: this commit adds a secret. Remove it and keep it in a secret store or an environment variable:");
110
+ for (const f of found.slice(0, 20)) console.error(` ${f.file}:${f.line} ${f.kind} (${f.masked})`);
111
+ console.error(" If it was ever pushed or shared, rotate it.");
112
+ return 1;
113
+ }
114
+
115
+ module.exports = { isPlaceholder, scanText, scanStaged, scanTracked, scanHistory, runSecretCheck, PATTERNS, GIT_PATTERN };