@hone-ai/cli 1.19.0 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/hone-cli.js CHANGED
@@ -144,6 +144,130 @@ process.on('exit', () => {
144
144
  }
145
145
  });
146
146
 
147
+ // ── AGENT-EVAL command (HC-COMM-011-followup-1) ──────────────────────────────────
148
+ // Runs deterministic adversarial probes against a configured agent target. $0 —
149
+ // no LLM judge. A probe passes iff the executor ran AND every grader passes
150
+ // (canary absent AND refusal present). Exits non-zero if any probe fails.
151
+ program
152
+ .command('agent-eval')
153
+ .description('Run agent-eval probes against configured agent targets (HC-COMM-011-followup-1)')
154
+ .option('--category <category>', 'Probe category (adversarial|faithfulness|safety|boundary)', 'adversarial')
155
+ .option('--target <name>', 'Run against a specific agent target (default: all configured)')
156
+ .option('--output <format>', 'Output format (text|json)', 'text')
157
+ .option('--judge', 'Add the free NLI judge layer (faithfulness only): claims are checked for entailment against the probe context')
158
+ .option('--provider <name>', 'Judge provider: gh-models (free, default) | claude (paid Sonnet)', 'gh-models')
159
+ .option('--report', 'Report the agent-eval verdict to the run (advisory) — needs --workflow-id')
160
+ .option('--workflow-id <id>', 'The run id to report the verdict against (for --report)')
161
+ .action(async (opts) => {
162
+ const { readAgentEvalConfig } = require('./lib/pipeline-config');
163
+ const { runAgent } = require('./lib/agent-executor');
164
+ const { runCheck } = require('./lib/eval-graders');
165
+ const config = readAgentEvalConfig(process.cwd());
166
+
167
+ if (!config.targets || config.targets.length === 0) {
168
+ console.log('No agent targets configured in .pipeline-config.yml (agent_eval.targets)');
169
+ process.exit(0);
170
+ }
171
+
172
+ const PROBE_PACKS = {
173
+ adversarial: './lib/agent-eval-probes-adversarial',
174
+ faithfulness: './lib/agent-eval-probes-faithfulness',
175
+ safety: './lib/agent-eval-probes-safety',
176
+ boundary: './lib/agent-eval-probes-boundary',
177
+ };
178
+ let probes;
179
+ if (PROBE_PACKS[opts.category]) {
180
+ probes = require(PROBE_PACKS[opts.category]);
181
+ } else {
182
+ console.error(`Unknown probe category: ${opts.category} (supported: ${Object.keys(PROBE_PACKS).join(', ')})`);
183
+ process.exit(1);
184
+ }
185
+
186
+ const targets = opts.target
187
+ ? config.targets.filter((t) => t.name === opts.target)
188
+ : config.targets;
189
+ if (targets.length === 0) {
190
+ console.error(`No agent target named "${opts.target}" in config`);
191
+ process.exit(1);
192
+ }
193
+
194
+ // Optional free NLI judge layer (faithfulness only). Build the provider callLLM
195
+ // ONCE — a missing credential is a hard error, not a silent skip.
196
+ let judgeCall = null;
197
+ if (opts.judge) {
198
+ if (opts.category !== 'faithfulness') {
199
+ console.error(`--judge applies to --category faithfulness only (got: ${opts.category})`);
200
+ process.exit(1);
201
+ }
202
+ const { buildJudgeCallLLM } = require('./lib/judge-provider');
203
+ const built = buildJudgeCallLLM(opts.provider, { axios });
204
+ if (built.error) { console.error(built.error); process.exit(1); }
205
+ judgeCall = built.callLLM;
206
+ }
207
+
208
+ const results = [];
209
+ let anyFailed = false;
210
+ for (const target of targets) {
211
+ for (const probe of probes) {
212
+ const run = await runAgent(target, probe.input);
213
+ const output = run && typeof run.output === 'string' ? run.output : '';
214
+ const graders = probe.graders.map((g) => runCheck(output, g));
215
+ let passed = !run.error && graders.every((r) => r.passed);
216
+
217
+ // Judge layer: only for grounded probes (those with a context to entail against).
218
+ let judge = null;
219
+ if (judgeCall && probe.context) {
220
+ const { judgeFaithfulness } = require('./lib/agent-eval-judge');
221
+ judge = await judgeFaithfulness({ output, context: probe.context, callLLM: judgeCall });
222
+ passed = passed && judge.passed;
223
+ }
224
+ if (!passed) anyFailed = true;
225
+ results.push({ target: target.name, probe: probe.id, passed, error: run.error || null, graders, judge });
226
+ }
227
+ }
228
+
229
+ if (opts.output === 'json') {
230
+ console.log(JSON.stringify({ category: opts.category, passed: !anyFailed, results }, null, 2));
231
+ } else {
232
+ for (const r of results) {
233
+ const mark = r.passed ? '✓' : '✗';
234
+ console.log(` ${mark} ${r.target} / ${r.probe}${r.error ? ` (executor: ${r.error})` : ''}`);
235
+ if (!r.passed) {
236
+ for (const g of r.graders.filter((x) => !x.passed)) console.log(` - ${g.type}: ${g.detail}`);
237
+ if (r.judge && !r.judge.passed) console.log(` - judge: ${r.judge.detail}`);
238
+ }
239
+ }
240
+ const pass = results.filter((r) => r.passed).length;
241
+ console.log(`\n${pass}/${results.length} probes passed (category: ${opts.category})`);
242
+ }
243
+
244
+ // Closed loop (HC-COMM-011-followup-5): report the verdict to the run. agent-eval
245
+ // binds to step_4 (a prompt change is a step_4 diff) and is ADVISORY — it records
246
+ // into config.verifications[], never auto-gates. Best-effort; never changes the
247
+ // local exit code.
248
+ if (opts.report) {
249
+ if (!opts.workflowId) {
250
+ if (opts.output !== 'json') console.warn(' ⚠ --report needs --workflow-id <run id> to address the run; skipping report.');
251
+ } else {
252
+ try {
253
+ const client = api(getConfig());
254
+ await client.post(`/orchestrate/${opts.workflowId}/verdict`, {
255
+ source: 'agent-eval',
256
+ stepKey: 'step_4',
257
+ verdict: anyFailed ? 'fail' : 'pass',
258
+ exitCode: anyFailed ? 1 : 0,
259
+ detail: `agent-eval ${opts.category}: ${results.filter((r) => r.passed).length}/${results.length} probes passed`,
260
+ });
261
+ if (opts.output !== 'json') console.log(` → reported agent-eval verdict '${anyFailed ? 'fail' : 'pass'}' to run ${opts.workflowId} (advisory)`);
262
+ } catch (e) {
263
+ if (opts.output !== 'json') console.warn(` ⚠ verdict report failed (non-fatal): ${e.response?.data?.error || e.message}`);
264
+ }
265
+ }
266
+ }
267
+
268
+ process.exit(anyFailed ? 1 : 0);
269
+ });
270
+
147
271
  // ── SETUP-LOCAL-CI command (HC-101-followup-3) ─────────────────────────────────
148
272
  //
149
273
  // Scaffolds HC-101's local-CI assets into the adopter's repo so they can
@@ -1777,6 +1901,314 @@ program
1777
1901
  }
1778
1902
  });
1779
1903
 
1904
+ // ── BYO-KEY command (HC-COMM-013 / A2) ───────────────────────────────────────
1905
+ // Self-serve management of your org's Anthropic API key for server mode. The
1906
+ // key is read from stdin (piped) or a HIDDEN prompt — NEVER accepted on argv,
1907
+ // which would leak it to shell history (~/.zsh_history), `ps`, and CI logs.
1908
+
1909
+ // Read a secret without echoing it. Piped stdin → read all of it (CI/secret-
1910
+ // manager injection). Interactive TTY → a muted readline prompt.
1911
+ async function _readSecret(promptText) {
1912
+ if (!process.stdin.isTTY) {
1913
+ const chunks = [];
1914
+ for await (const chunk of process.stdin) chunks.push(chunk);
1915
+ return Buffer.concat(chunks).toString('utf8');
1916
+ }
1917
+ const readline = await import('readline');
1918
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
1919
+ return new Promise((resolve) => {
1920
+ process.stdout.write(promptText);
1921
+ // Mute echo of typed characters — the prompt is already written.
1922
+ rl._writeToOutput = () => {};
1923
+ rl.question('', (answer) => {
1924
+ rl.close();
1925
+ process.stdout.write('\n');
1926
+ resolve(answer);
1927
+ });
1928
+ });
1929
+ }
1930
+
1931
+ function _handleByoKeyError(e) {
1932
+ const status = e.response?.status;
1933
+ if (status === 401) {
1934
+ console.error('Not authenticated. Run: hone init --token <token>');
1935
+ } else if (status === 429) {
1936
+ const retry = e.response?.data?.retryAfter;
1937
+ console.error(`Rate limited — too many key writes.${retry ? ` Retry after ${retry}s.` : ''}`);
1938
+ } else if (status === 503) {
1939
+ console.error(`Server cannot store the key right now: ${e.response?.data?.error || 'unavailable'}`);
1940
+ } else if (e.response?.data?.error) {
1941
+ console.error(`Failed: ${e.response.data.error}`);
1942
+ } else {
1943
+ console.error(`Failed: ${e.message}`);
1944
+ }
1945
+ process.exit(1);
1946
+ }
1947
+
1948
+ const MAX_BYO_KEY_BYTES_CLI = 1024;
1949
+
1950
+ const byoKeyCmd = program
1951
+ .command('byo-key')
1952
+ .description("Manage your org's BYO Anthropic API key (server mode)");
1953
+
1954
+ byoKeyCmd
1955
+ .command('set')
1956
+ .description('Set or rotate your key — reads from stdin or a hidden prompt (never pass the key on the command line)')
1957
+ .option('--key-file <path>', 'Read the key from a file (e.g. a 0600 secrets file) instead of stdin/prompt')
1958
+ .action(async (opts) => {
1959
+ const config = getConfig();
1960
+ const client = api(config);
1961
+
1962
+ let key;
1963
+ try {
1964
+ key = opts.keyFile
1965
+ ? fs.readFileSync(opts.keyFile, 'utf8')
1966
+ : await _readSecret('Anthropic API key: ');
1967
+ } catch (e) {
1968
+ console.error(`Could not read key: ${e.message}`);
1969
+ process.exit(1);
1970
+ }
1971
+ key = (key || '').trim();
1972
+
1973
+ // Client-side convenience checks (server remains the authority).
1974
+ if (!key) {
1975
+ console.error('No key provided (empty input). Pipe it in or type it at the prompt.');
1976
+ process.exit(1);
1977
+ }
1978
+ if (Buffer.byteLength(key, 'utf8') > MAX_BYO_KEY_BYTES_CLI) {
1979
+ console.error(`Key too large (max ${MAX_BYO_KEY_BYTES_CLI} bytes). Did you paste the wrong thing?`);
1980
+ process.exit(1);
1981
+ }
1982
+ if (!key.startsWith('sk-ant-')) {
1983
+ // Warn-only (memo §8): Anthropic key formats can change; don't hard-block.
1984
+ console.error('Warning: key does not look like an Anthropic key (expected "sk-ant-" prefix). Sending anyway.');
1985
+ }
1986
+
1987
+ try {
1988
+ const { data } = await client.put('/orgs/me/byo-key', { byo_anthropic_key: key });
1989
+ console.log(data.wasConfigured
1990
+ ? 'BYO key rotated. Pipeline LLM calls now bill your own Anthropic API.'
1991
+ : 'BYO key set. Pipeline LLM calls now bill your own Anthropic API.');
1992
+ } catch (e) { _handleByoKeyError(e); }
1993
+ });
1994
+
1995
+ byoKeyCmd
1996
+ .command('clear')
1997
+ .description('Clear your key — your org reverts to the Hone default key')
1998
+ .action(async () => {
1999
+ const config = getConfig();
2000
+ const client = api(config);
2001
+ try {
2002
+ const { data } = await client.delete('/orgs/me/byo-key');
2003
+ console.log(data.wasConfigured
2004
+ ? 'BYO key cleared. Your org reverts to the Hone default key.'
2005
+ : 'No BYO key was configured — nothing to clear.');
2006
+ } catch (e) { _handleByoKeyError(e); }
2007
+ });
2008
+
2009
+ byoKeyCmd
2010
+ .command('status')
2011
+ .description('Show whether a BYO key is configured (never prints the key)')
2012
+ .option('--format <fmt>', 'Output format: pretty | json', 'pretty')
2013
+ .action(async (opts) => {
2014
+ const config = getConfig();
2015
+ const client = api(config);
2016
+ try {
2017
+ const { data } = await client.get('/orgs/me/byo-key');
2018
+ if (opts.format === 'json') {
2019
+ console.log(JSON.stringify(data, null, 2));
2020
+ return;
2021
+ }
2022
+ console.log(`BYO key configured: ${data.configured ? 'yes' : 'no'}`);
2023
+ if (data.lastWrite) {
2024
+ const when = String(data.lastWrite.at).split('T')[0];
2025
+ console.log(`Last change: ${data.lastWrite.action} by ${data.lastWrite.actorType} on ${when}`);
2026
+ }
2027
+ if (data.configured && data.envelopeOk === false) {
2028
+ console.log('Warning: stored value is not in the expected encrypted envelope shape — contact support.');
2029
+ }
2030
+ // HC-COMM-014: fold a one-line trial summary in (non-fatal if unavailable).
2031
+ try {
2032
+ const { data: plan } = await client.get('/orgs/me/plan');
2033
+ if (plan.mode === 'trial') {
2034
+ console.log(`Trial: active — ${plan.trial.daysLeft}d left, $${Number(plan.trial.creditRemaining).toFixed(2)} credit`);
2035
+ if (plan.trial.creditWarning) {
2036
+ console.log(plan.billingEnabled
2037
+ ? `Heads up: over half your trial credit is used. To continue after the trial you'll need your own key (set ✓) AND a subscription (\`hone upgrade\`).`
2038
+ : `Heads up: over half your trial credit is used. Your own key covers you after the trial.`);
2039
+ }
2040
+ } else if (plan.mode === 'expired') {
2041
+ // §11-F14: with the paywall live, BOTH a key AND a subscription are
2042
+ // needed (an "or" would imply either alone suffices). Billing-off
2043
+ // servers keep the legacy message — a key alone still works there.
2044
+ console.log(plan.billingEnabled
2045
+ ? 'Trial: ended — needs `hone byo-key set` AND `hone upgrade`'
2046
+ : 'Trial: ended (set a key to continue)');
2047
+ }
2048
+ } catch { /* trial line is a nicety — never block byo-key status */ }
2049
+ console.log('Tip: run `hone byo-key check` to verify the key still authenticates with Anthropic.');
2050
+ } catch (e) { _handleByoKeyError(e); }
2051
+ });
2052
+
2053
+ byoKeyCmd
2054
+ .command('check')
2055
+ .description('Verify the configured key still authenticates with Anthropic (zero-token live probe; never prints the key)')
2056
+ .action(async () => {
2057
+ const config = getConfig();
2058
+ const client = api(config);
2059
+ try {
2060
+ const { data } = await client.post('/orgs/me/byo-key/check');
2061
+ if (data.ok) {
2062
+ console.log('BYO key is valid — Anthropic accepted it.');
2063
+ return;
2064
+ }
2065
+ const msg = {
2066
+ unauthorized: 'BYO key was REJECTED by Anthropic (401/403). Rotate it with `hone byo-key set`.',
2067
+ unreachable: 'Could not reach Anthropic to check the key — try again shortly (key not changed).',
2068
+ unexpected: 'Anthropic returned an unexpected status checking the key — try again shortly.',
2069
+ }[data.reason] || `Key check failed: ${data.reason}`;
2070
+ console.error(msg);
2071
+ process.exit(1);
2072
+ } catch (e) { _handleByoKeyError(e); }
2073
+ });
2074
+
2075
+ // ── TRIAL command (HC-COMM-014 / A3) ─────────────────────────────────────────
2076
+ const trialCmd = program
2077
+ .command('trial')
2078
+ .description("View or start your org's Hone-funded trial (server mode)");
2079
+
2080
+ trialCmd
2081
+ .command('status')
2082
+ .description('Show trial mode, days left, and credit remaining')
2083
+ .option('--format <fmt>', 'Output format: pretty | json', 'pretty')
2084
+ .action(async (opts) => {
2085
+ const config = getConfig();
2086
+ const client = api(config);
2087
+ try {
2088
+ const { data } = await client.get('/orgs/me/plan');
2089
+ if (opts.format === 'json') { console.log(JSON.stringify(data, null, 2)); return; }
2090
+ const t = data.trial || {};
2091
+ console.log(`Plan mode: ${data.mode}`);
2092
+ console.log(`Trial status: ${t.status}`);
2093
+ if (t.status === 'active') {
2094
+ console.log(`Days left: ${t.daysLeft}`);
2095
+ console.log(`Credit: $${Number(t.creditUsdUsed || 0).toFixed(2)} used / $${Number(t.creditUsdCap).toFixed(2)} cap ($${Number(t.creditRemaining).toFixed(2)} left)`);
2096
+ if (t.creditWarning) {
2097
+ // Owner decision: proactive heads-up at half the trial credit so the
2098
+ // post-trial requirement (key AND subscription when billing is on)
2099
+ // never lands as a surprise.
2100
+ console.log(data.billingEnabled
2101
+ ? 'Heads up: over half your trial credit is used. After the trial you need `hone byo-key set` AND `hone upgrade` to keep running.'
2102
+ : 'Heads up: over half your trial credit is used. Set your own key (`hone byo-key set`) to keep running after the trial.');
2103
+ }
2104
+ }
2105
+ if (data.subscription && data.billingEnabled) {
2106
+ console.log(`Subscription: ${data.subscription.status}${data.subscription.entitled ? ' (entitled)' : ''}`);
2107
+ if (data.subscription.graceUntil) {
2108
+ console.log(`Grace until: ${String(data.subscription.graceUntil).split('T')[0]} — payment failing, fix via \`hone billing portal\``);
2109
+ }
2110
+ }
2111
+ if (data.mode === 'expired') {
2112
+ // §11-F14: AND, not or — with the paywall live both are required.
2113
+ console.log(data.billingEnabled
2114
+ ? 'Trial ended — run `hone byo-key set` AND `hone upgrade` to continue.'
2115
+ : 'Trial ended — set your own key (`hone byo-key set`) to continue.');
2116
+ }
2117
+ } catch (e) { _handleByoKeyError(e); }
2118
+ });
2119
+
2120
+ trialCmd
2121
+ .command('start')
2122
+ .description('Start your trial now (idempotent — no-op if already started or ended)')
2123
+ .action(async () => {
2124
+ const config = getConfig();
2125
+ const client = api(config);
2126
+ try {
2127
+ const { data } = await client.post('/orgs/me/trial/start');
2128
+ console.log(data.started ? `Trial started — status: ${data.status}.` : `No-op — ${data.note}`);
2129
+ } catch (e) { _handleByoKeyError(e); }
2130
+ });
2131
+
2132
+ // ── UPGRADE + BILLING commands (HC-COMM-015 / A4 / 015f) ─────────────────────
2133
+ // The subscription pays for Hone (orchestration/prompts/skills); LLM usage
2134
+ // stays on your own Anthropic key (`hone byo-key set`). Checkout happens on a
2135
+ // Stripe-hosted page — the CLI only prints the URL, no card data ever touches
2136
+ // Hone or this terminal.
2137
+ function _handleBillingError(e) {
2138
+ const status = e.response?.status;
2139
+ const msg = e.response?.data?.error;
2140
+ if (status === 401) {
2141
+ console.error('Not authenticated. Run: hone init --token <token>');
2142
+ } else if (status === 409) {
2143
+ console.error(msg || 'Conflict — check `hone billing status`.');
2144
+ } else if (status === 503) {
2145
+ console.error(msg === 'Billing not configured'
2146
+ ? "Billing isn't enabled on this server (self-hosted / not yet configured)."
2147
+ : `Server unavailable: ${msg || e.message}`);
2148
+ } else if (status === 429) {
2149
+ console.error('Rate limited — try again shortly.');
2150
+ } else {
2151
+ console.error(`Failed: ${msg || e.message}`);
2152
+ }
2153
+ process.exit(1);
2154
+ }
2155
+
2156
+ program
2157
+ .command('upgrade')
2158
+ .description('Subscribe to Hone (opens a Stripe-hosted checkout URL)')
2159
+ .action(async () => {
2160
+ const config = getConfig();
2161
+ const client = api(config);
2162
+ try {
2163
+ const { data } = await client.post('/orgs/me/billing/checkout');
2164
+ console.log('Complete your subscription in the browser:');
2165
+ console.log(` ${data.url}`);
2166
+ console.log('Then run `hone billing status` to confirm.');
2167
+ } catch (e) { _handleBillingError(e); }
2168
+ });
2169
+
2170
+ const billingCmd = program
2171
+ .command('billing')
2172
+ .description("View or manage your org's Hone subscription");
2173
+
2174
+ billingCmd
2175
+ .command('status')
2176
+ .description('Show subscription status, period end, and grace state')
2177
+ .option('--format <fmt>', 'Output format: pretty | json', 'pretty')
2178
+ .action(async (opts) => {
2179
+ const config = getConfig();
2180
+ const client = api(config);
2181
+ try {
2182
+ const { data } = await client.get('/orgs/me/plan');
2183
+ if (opts.format === 'json') { console.log(JSON.stringify(data, null, 2)); return; }
2184
+ const s = data.subscription || {};
2185
+ console.log(`Plan mode: ${data.mode}`);
2186
+ if (!data.billingEnabled) {
2187
+ console.log('Billing: not enabled on this server');
2188
+ return;
2189
+ }
2190
+ console.log(`Subscription: ${s.status}${s.entitled ? ' (entitled)' : ''}`);
2191
+ if (s.currentPeriodEnd) console.log(`Period ends: ${String(s.currentPeriodEnd).split('T')[0]}`);
2192
+ if (s.graceUntil) console.log(`Grace until: ${String(s.graceUntil).split('T')[0]} — payment failing, fix via \`hone billing portal\``);
2193
+ if (s.status === 'none' || s.status === 'canceled') {
2194
+ console.log('Subscribe with: hone upgrade');
2195
+ }
2196
+ } catch (e) { _handleBillingError(e); }
2197
+ });
2198
+
2199
+ billingCmd
2200
+ .command('portal')
2201
+ .description('Open the Stripe billing portal (update card, cancel, invoices)')
2202
+ .action(async () => {
2203
+ const config = getConfig();
2204
+ const client = api(config);
2205
+ try {
2206
+ const { data } = await client.post('/orgs/me/billing/portal');
2207
+ console.log('Manage your subscription in the browser:');
2208
+ console.log(` ${data.url}`);
2209
+ } catch (e) { _handleBillingError(e); }
2210
+ });
2211
+
1780
2212
  // ── ADMIN-USAGE command ──────────────────────────────────────────────────────
1781
2213
  program
1782
2214
  .command('admin-usage')
@@ -4598,7 +5030,8 @@ program
4598
5030
  .option('--contracts', 'Run contract validation between pipeline agents')
4599
5031
  .option('--snapshot', 'Save current eval + contract results as regression baseline')
4600
5032
  .option('--regression', 'Compare current results against saved baseline (detect drift)')
4601
- .option('--judge', 'Run LLM-as-judge scenarios (requires ANTHROPIC_API_KEY, costs tokens)')
5033
+ .option('--judge', 'Run LLM-as-judge scenarios (default provider: free gh-models via GITHUB_TOKEN)')
5034
+ .option('--provider <name>', 'LLM-judge provider: gh-models (free, default) | claude (paid Sonnet)', 'gh-models')
4602
5035
  .option('--evidence-mode <mode>', 'HC-RC-001 editor-LLM evidence transfer: "local" writes .hone/eval-evidence.json (signed); "off" disables (default)')
4603
5036
  .action(async (opts) => {
4604
5037
  const path = require('path');
@@ -4666,12 +5099,8 @@ program
4666
5099
  const { loadScenarios, formatResults } = require('./lib/eval-runner');
4667
5100
  const { runJudgeScenario } = require('./lib/eval-llm-judge');
4668
5101
 
4669
- const apiKey = process.env.ANTHROPIC_API_KEY;
4670
- if (!apiKey) {
4671
- console.error('ANTHROPIC_API_KEY required for --judge mode. Set: export ANTHROPIC_API_KEY=sk-ant-...');
4672
- process.exit(1);
4673
- }
4674
-
5102
+ // Load + filter FIRST — if there are no judge scenarios, exit cleanly
5103
+ // without demanding any API key / token.
4675
5104
  const scenarios = loadScenarios({
4676
5105
  evalDir, agent: opts.agent, tag: opts.tag, scenarioId: opts.scenario,
4677
5106
  readFile: (p) => fs.readFileSync(p, 'utf8'),
@@ -4685,48 +5114,21 @@ program
4685
5114
  process.exit(0);
4686
5115
  }
4687
5116
 
4688
- // LLM call function using Anthropic API
4689
- async function callLLM(systemPrompt, userPrompt) {
4690
- const { data } = await axios.post('https://api.anthropic.com/v1/messages', {
4691
- // MODEL CHOICE (per feedback_model_choice_cost_amplifier): Sonnet tier,
4692
- // deliberately NOT Opus. The eval judge is not one of the three
4693
- // load-bearing SDLC agents on the OPUS_AGENTS allowlist (architect,
4694
- // security, code-reviewer — HC-COMM-007); it grades eval scenarios,
4695
- // where Sonnet is sufficient and Opus would be a 2.5x input-cost
4696
- // escalation on a call site that can run once per scenario.
4697
- //
4698
- // Previously a dated 2025-05-14 Sonnet id, which Anthropic deprecated
4699
- // with a 2026-06-15 deadline. The #507 Opus 4.8 sweep missed this call
4700
- // site because it searched only for opus ids. Pinned by
4701
- // tests/regression/opus-4-8-upgrade.test.js — note that suite forbids
4702
- // the retired id even inside comments, so name it descriptively here.
4703
- model: 'claude-sonnet-5',
4704
- // Sonnet 5 runs ADAPTIVE THINKING when `thinking` is omitted, unlike
4705
- // the dated Sonnet 4 model this replaced. Two consequences if left
4706
- // default, both silent: content[0] becomes a `thinking` block (so a
4707
- // [0].text read returns undefined and every judge criterion fails to
4708
- // parse), and thinking tokens share max_tokens with the answer.
4709
- // The judge returns a short structured verdict, so keep it off and
4710
- // preserve the previous cost/latency profile.
4711
- thinking: { type: 'disabled' },
4712
- max_tokens: 2048,
4713
- system: systemPrompt,
4714
- messages: [{ role: 'user', content: userPrompt }],
4715
- }, {
4716
- headers: {
4717
- 'x-api-key': apiKey,
4718
- 'anthropic-version': '2023-06-01',
4719
- 'content-type': 'application/json',
4720
- },
4721
- timeout: 60000,
4722
- });
4723
- // Select the first TEXT block rather than content[0]: any future model
4724
- // or config that emits a leading thinking block must not silently
4725
- // degrade every scenario to "could not parse response".
4726
- return (data.content || []).find(b => b?.type === 'text')?.text || '';
4727
- }
4728
-
4729
- console.log(`Running ${judgeScenarios.length} LLM-judge scenario(s)...`);
5117
+ // HC-019n-followup-33 (pipeline-standards G6): the LLM-judge lane must have a
5118
+ // ZERO-COST path (feedback_pipeline_llm_cost_reduction — every LLM-cost gate
5119
+ // needs a free/near-free route so adopters aren't double-billed). DEFAULT to
5120
+ // GH Models (free inference via GITHUB_TOKEN); paid Claude Sonnet is opt-in
5121
+ // via `--provider claude`. Mirrors `hone skill-eval`'s provider convention.
5122
+ // HC-COMM-011-followup-3: provider wiring extracted to lib/judge-provider.js
5123
+ // (shared with `hone agent-eval --judge`). Same zero-cost default (gh-models)
5124
+ // + Sonnet-not-Opus paid path; the retired dated Sonnet id must not appear
5125
+ // (pinned by opus-4-8-upgrade.test.js).
5126
+ const { buildJudgeCallLLM } = require('./lib/judge-provider');
5127
+ const built = buildJudgeCallLLM(opts.provider, { axios });
5128
+ if (built.error) { console.error(built.error); process.exit(1); }
5129
+ const callLLM = built.callLLM;
5130
+
5131
+ console.log(`Running ${judgeScenarios.length} LLM-judge scenario(s) via ${built.provider}...`);
4730
5132
  console.log('');
4731
5133
 
4732
5134
  const results = [];
@@ -5027,6 +5429,8 @@ program
5027
5429
  .option('--format <fmt>', 'Output format: pretty or json', 'pretty')
5028
5430
  .option('--poll-interval <s>', 'Poll interval in seconds', '5')
5029
5431
  .option('--include-paths <paths>', 'Comma-separated list of source file paths to bundle into the codebase context (HC-019n-followup-12). Augments auto-detected paths from the issue body.')
5432
+ .option('--e2e-specs', 'HC-019n-followup-28: force step_3b (Playwright spec generation) ON for this run (overrides e2e.spec_generation config)')
5433
+ .option('--skip-e2e-specs', 'HC-019n-followup-28: force step_3b (Playwright spec generation) OFF for this run (overrides e2e.spec_generation config)')
5030
5434
  .action(async (storyIdOrRunId, opts) => {
5031
5435
  const config = getConfig();
5032
5436
  const client = api(config);
@@ -5166,6 +5570,41 @@ program
5166
5570
  console.warn(' → proceeding without story_description; step_0 may produce placeholder output');
5167
5571
  console.warn(` ⚠ architect-config lookup will use '${storyIdOrRunId}' (likely silent miss)`);
5168
5572
  }
5573
+ } else {
5574
+ // HC-019n-followup-18: roadmap-sourced story. Only the SERVER knows what
5575
+ // the story says (server/seeds/master-roadmap.md is a Hone asset, absent
5576
+ // from adopter repos) and only the CLI can read the adopter's disk — so
5577
+ // ask the server what the description references, then read those files
5578
+ // here. See .github/pipeline/HC-019n-followup-18/architect.md §1 for why
5579
+ // this beats the CLI reading a local roadmap (hardcodes a Hone-internal
5580
+ // path into the adopter CLI, and fails silently anywhere else).
5581
+ //
5582
+ // Advisory, never fatal: a story with no resolvable context still runs,
5583
+ // it just runs blind — and loudly, via the followup-17 warning below.
5584
+ try {
5585
+ const ctxClient = api(config);
5586
+ const { data } = await ctxClient.get(
5587
+ `/stories/${encodeURIComponent(storyIdOrRunId)}/context`
5588
+ );
5589
+ if (data && data.description) {
5590
+ orchestrateConfig.story_description = data.description;
5591
+ issueBodyForFiles = data.description;
5592
+ const n = Array.isArray(data.referencedPaths) ? data.referencedPaths.length : 0;
5593
+ console.log(
5594
+ ` → resolved story context from roadmap (${data.description.length} chars, ` +
5595
+ `${n} referenced path${n === 1 ? '' : 's'})`
5596
+ );
5597
+ }
5598
+ } catch (e) {
5599
+ const status = e && e.response && e.response.status;
5600
+ if (status === 404) {
5601
+ console.warn(` ⚠ no roadmap row found for '${storyIdOrRunId}'`);
5602
+ } else {
5603
+ const msg = (e && e.message) ? e.message.split('\n')[0] : String(e);
5604
+ console.warn(` ⚠ could not fetch story context: ${msg}`);
5605
+ }
5606
+ console.warn(' → proceeding without story context; the warning below applies');
5607
+ }
5169
5608
  }
5170
5609
 
5171
5610
  // HC-019n-followup-12: auto-bundle codebase files referenced in the
@@ -5207,6 +5646,45 @@ program
5207
5646
  candidates.add(p);
5208
5647
  }
5209
5648
  }
5649
+ // HC-019n-followup-26: FILE_PATH_RE above requires a directory segment,
5650
+ // so a story that names a file BARE (`derive-worker.js:787` in prose)
5651
+ // extracts 0 paths and step_4 runs blind — and, blind, hallucinates a
5652
+ // file at a non-existent path (2026-08-30 e2e demo). Catch bare refs too
5653
+ // and resolve each against the repo's own file list: bundle ONLY on a
5654
+ // unique basename match; never guess an ambiguous or absent one.
5655
+ // HC-019n-followup-27: candidate path → referenced line number, so the
5656
+ // bundler can WINDOW a too-big file around its edit site instead of
5657
+ // head-truncating past it (the demo's line 787 sits at char 37K).
5658
+ const candidateLines = new Map();
5659
+ if (issueBodyForFiles) {
5660
+ const { extractBareRefs, resolveBareName, alreadyCovered } = require('./lib/bundle-paths');
5661
+ const { gitEnv } = require('./lib/git-env');
5662
+ const bareRefs = extractBareRefs(issueBodyForFiles);
5663
+ if (bareRefs.length > 0) {
5664
+ let repoFiles = null;
5665
+ const listRepoFiles = () => {
5666
+ if (repoFiles) return repoFiles;
5667
+ try {
5668
+ repoFiles = execSync('git ls-files', {
5669
+ cwd: process.cwd(), env: gitEnv(), encoding: 'utf8', maxBuffer: 64 * 1024 * 1024,
5670
+ }).split('\n').filter(Boolean);
5671
+ } catch { repoFiles = []; }
5672
+ return repoFiles;
5673
+ };
5674
+ for (const { name, line } of bareRefs) {
5675
+ if (alreadyCovered(name, candidates)) continue;
5676
+ const r = resolveBareName(name, listRepoFiles());
5677
+ if (r.status === 'unique') {
5678
+ candidates.add(r.path);
5679
+ if (line && !candidateLines.has(r.path)) candidateLines.set(r.path, line);
5680
+ console.log(` → resolved bare ref '${name}${line ? ':' + line : ''}' → ${r.path}`);
5681
+ } else if (r.status === 'ambiguous') {
5682
+ console.warn(` ⚠ '${name}' is ambiguous (${r.matches.length} files) — not bundling; use --include-paths to disambiguate`);
5683
+ }
5684
+ // status === 'none' → not every filename in prose is a repo file; skip quietly
5685
+ }
5686
+ }
5687
+ }
5210
5688
  // HC-019n-followup-13d: bump caps. Pre-fix MAX_FILES=10 /
5211
5689
  // MAX_TOTAL_CHARS=50_000 / MAX_PER_FILE_CHARS=10_000 was overly
5212
5690
  // conservative — OptionsFlow #96 hit 49,783/50,000 with 6 files
@@ -5230,7 +5708,23 @@ program
5230
5708
  content = fs.readFileSync(abs, 'utf8');
5231
5709
  } catch { continue; }
5232
5710
  if (content.length > MAX_PER_FILE_CHARS) {
5233
- content = content.slice(0, MAX_PER_FILE_CHARS) + `\n\n# [HC-019n-followup-12: truncated at ${MAX_PER_FILE_CHARS} chars; full file is ${content.length} chars]`;
5711
+ const line = candidateLines.get(candidate);
5712
+ if (line) {
5713
+ // HC-019n-followup-27: window around the referenced line so step_4
5714
+ // SEES the edit site (head-truncation would hide a site past the
5715
+ // cut) and can write a region-edit anchor. The window marker tells
5716
+ // step_4 to emit an `edit:` block, not a whole-file block.
5717
+ const { windowAroundLine } = require('./lib/bundle-paths');
5718
+ const w = windowAroundLine(content, line, Math.floor((MAX_PER_FILE_CHARS - 800) / 2));
5719
+ content =
5720
+ `# [HC-019n-followup-27: windowed excerpt, lines ${w.startLine}-${w.endLine} of ${w.totalLines} ` +
5721
+ `(centered on referenced line ${line}); you were NOT shown the whole file — ` +
5722
+ `emit an \`edit:\` block with a unique OLD anchor from this excerpt, NOT a whole-file \`file:\` block]\n` +
5723
+ w.excerpt +
5724
+ `\n# [end windowed excerpt of ${candidate}]`;
5725
+ } else {
5726
+ content = content.slice(0, MAX_PER_FILE_CHARS) + `\n\n# [HC-019n-followup-12: truncated at ${MAX_PER_FILE_CHARS} chars; full file is ${content.length} chars]`;
5727
+ }
5234
5728
  }
5235
5729
  if (totalChars + content.length > MAX_TOTAL_CHARS) break;
5236
5730
  totalChars += content.length;
@@ -5246,6 +5740,33 @@ program
5246
5740
  }
5247
5741
  }
5248
5742
 
5743
+ // HC-019n-followup-17: warn when the code-aware steps will run BLIND.
5744
+ //
5745
+ // Auto-detection reads file paths out of `issueBodyForFiles`, which is only
5746
+ // populated on the numeric-GitHub-issue branch above. A roadmap-sourced id
5747
+ // (HC-CI-005, SC-002-followup-2, ...) never enters that branch, so
5748
+ // `codebaseFiles` stays unset and step_1 / step_4 / step_5 / step_5d /
5749
+ // step_5e all run with no source to read — the exact "confidently wrong"
5750
+ // failure HC-019n-followup-12 was built to prevent, still open for roadmap
5751
+ // ids. Observed 2026-08-29: run 61fddfb2 wrote 8,444 chars of code for
5752
+ // HC-CI-005 having never seen the files that story names.
5753
+ //
5754
+ // A fix is structurally awkward: only the CLI can read adopter files, and
5755
+ // only the SERVER knows the roadmap description (server/seeds/master-
5756
+ // roadmap.md is a Hone asset, absent from adopter repos). Closing it needs
5757
+ // the CLI to fetch the resolved description before queueing — a round trip
5758
+ // that belongs in its own story. Until then the gap is at least LOUD
5759
+ // instead of silent, which is the part that actually bit.
5760
+ if (!orchestrateConfig.codebaseFiles) {
5761
+ console.warn(' ⚠ no codebase files bundled — step_1 / step_4 / step_5 / step_5d / step_5e');
5762
+ console.warn(' will run WITHOUT source context and may invent APIs that do not exist.');
5763
+ if (!/^\d+$/.test(storyIdOrRunId)) {
5764
+ console.warn(` '${storyIdOrRunId}' is not a GitHub issue number, so file paths cannot be`);
5765
+ console.warn(' auto-detected from an issue body.');
5766
+ }
5767
+ console.warn(' Fix: hone run-story <id> --include-paths path/one.js,path/two.js');
5768
+ }
5769
+
5249
5770
  // HC-101-followup-2: pass the adopter's CI gate config to the
5250
5771
  // orchestrator so step_5c can branch (github / local / both / none).
5251
5772
  // Defaults to gate=github + local_command='make ci' when the config
@@ -5264,6 +5785,56 @@ program
5264
5785
  orchestrateConfig.ci_local_command = 'make ci';
5265
5786
  }
5266
5787
 
5788
+ // HC-019n-followup-28: resolve the E2E spec-generation mode (step_3b /
5789
+ // Playwright). Precedence mirrors ci.gate: per-run flag > .pipeline-config.yml
5790
+ // `e2e.spec_generation` > default `auto`. `auto` = the orchestrator runs
5791
+ // step_3b when the story requires an E2E plan (step_0 emitted `yes`); `never`
5792
+ // = step_3b is always skipped. The runtime "is E2E required?" decision stays
5793
+ // server-side (checkE2eRequirement reads step_0's output) — the CLI only
5794
+ // passes the MODE.
5795
+ try {
5796
+ const { readE2eSpecMode } = require('./lib/pipeline-config');
5797
+ let e2eMode, source;
5798
+ if (opts.e2eSpecs) { e2eMode = 'auto'; source = '--e2e-specs'; }
5799
+ else if (opts.skipE2eSpecs) { e2eMode = 'never'; source = '--skip-e2e-specs'; }
5800
+ else { e2eMode = readE2eSpecMode(process.cwd()); source = 'config/default'; }
5801
+ orchestrateConfig.e2e_spec_generation = e2eMode;
5802
+ // Enable step_3b (conditional) unless the adopter opted out. The row is
5803
+ // then created; the orchestrator still skips it at runtime for a story that
5804
+ // does not require an E2E plan (HC-019n-followup-28).
5805
+ if (e2eMode !== 'never') {
5806
+ const ec = new Set(orchestrateConfig.enableConditional || []);
5807
+ ec.add('step_3b');
5808
+ orchestrateConfig.enableConditional = [...ec];
5809
+ }
5810
+ console.log(` → E2E spec generation (step_3b): ${e2eMode} (source: ${source})`);
5811
+ } catch (e) {
5812
+ const msg = (e && e.message) ? e.message.split('\n')[0] : String(e);
5813
+ console.warn(` ⚠ e2e spec-mode read failed (non-fatal, defaulting to auto): ${msg}`);
5814
+ orchestrateConfig.e2e_spec_generation = 'auto';
5815
+ }
5816
+
5817
+ // HC-019n-followup-34: thread the closed-loop verification gate mode from
5818
+ // .pipeline-config.yml. Advisory (default) = the server RECORDS verify-patch/
5819
+ // verify-pr verdicts reported via --report; enforce (follow-up) = a red
5820
+ // verdict blocks. Default-safe: never fatal.
5821
+ try {
5822
+ const { readVerificationGateConfig, readVerificationGateSources } = require('./lib/pipeline-config');
5823
+ orchestrateConfig.verification_gate = readVerificationGateConfig(process.cwd());
5824
+ // HC-COMM-011-followup-6: which verdict sources may DRIVE the step_4 gate under
5825
+ // enforce (default ['verify-patch']). An adopter adds 'agent-eval' to let a
5826
+ // green agent-eval verdict auto-advance a prompt change.
5827
+ orchestrateConfig.verification_gate_sources = readVerificationGateSources(process.cwd());
5828
+ if (orchestrateConfig.verification_gate !== 'advisory') {
5829
+ console.log(` → verification gate: ${orchestrateConfig.verification_gate} (sources: ${orchestrateConfig.verification_gate_sources.join(', ')})`);
5830
+ }
5831
+ } catch (e) {
5832
+ const msg = (e && e.message) ? e.message.split('\n')[0] : String(e);
5833
+ console.warn(` ⚠ verification-gate read failed (non-fatal, defaulting to advisory): ${msg}`);
5834
+ orchestrateConfig.verification_gate = 'advisory';
5835
+ orchestrateConfig.verification_gate_sources = ['verify-patch'];
5836
+ }
5837
+
5267
5838
  // HC-019b: read per-story architect flags from .github/EXECUTION_PLAN.yml
5268
5839
  // and plumb them into workflow_runs.config. The orchestrator's
5269
5840
  // validateStepPreConditions (server/src/services/workflow-dag.js:340)
@@ -6440,6 +7011,690 @@ showCmd
6440
7011
  }
6441
7012
  });
6442
7013
 
7014
+ // ── HC-019n-followup-20: check-patch (pipeline-recovery condition 5) ──────────
7015
+ // Fetch step_4's output, extract its diff, and run `git apply --check` against
7016
+ // the real tree. The server can only verify the output LOOKS like a diff
7017
+ // (patch-validator); only the CLI, running inside the repo, can prove it
7018
+ // APPLIES. Run c122c92a produced a diff that passed the server check and failed
7019
+ // to apply — structure is not applicability.
7020
+ program
7021
+ .command('check-patch <workflowId>')
7022
+ .description('Verify step_4\'s diff applies to the current working tree (condition 5)')
7023
+ .option('--step-key <key>', 'Step to check (default: step_4)', 'step_4')
7024
+ .option('--attempt <n>', 'Specific attempt number (default: latest)')
7025
+ .option('--format <fmt>', 'Output format: pretty or json', 'pretty')
7026
+ .action(async (workflowId, opts) => {
7027
+ const { execFileSync } = require('child_process');
7028
+ const os = require('os');
7029
+ const { extractDiff, interpretApplyCheck } = require('./lib/patch-apply');
7030
+ const { extractFileBlocks, buildDiff, nodeMaterializeIO, MaterializeError } = require('./lib/materialize-diff');
7031
+ const { gitEnv } = require('./lib/git-env');
7032
+ const config = getConfig();
7033
+ const client = api(config);
7034
+
7035
+ const emit = (obj, humanLine) => {
7036
+ if (opts.format === 'json') console.log(JSON.stringify(obj));
7037
+ else console.log(humanLine);
7038
+ };
7039
+
7040
+ let output;
7041
+ try {
7042
+ const url = `/orchestrate/${workflowId}/step/${opts.stepKey}` +
7043
+ (opts.attempt ? `?attempt=${opts.attempt}` : '');
7044
+ const r = await client.get(url);
7045
+ output = r.data?.output;
7046
+ } catch (e) {
7047
+ const msg = e.response?.data?.error || e.message;
7048
+ emit({ ok: false, reason: 'fetch_failed', detail: msg },
7049
+ `✗ could not fetch ${opts.stepKey} output: ${msg}`);
7050
+ process.exit(1);
7051
+ }
7052
+
7053
+ // HC-019n-followup-23/27: prefer whole-file blocks + region edits
7054
+ // (## Changed Files). git computes the hunks, so the assembled diff applies
7055
+ // by construction — no LLM line-counting. Fall back to the raw-diff path
7056
+ // (followup-20) for stories still on the old ## Patch contract.
7057
+ let diff, source, noChanges;
7058
+ const blocks = extractFileBlocks(output);
7059
+ if (blocks.noChanges) {
7060
+ emit({ ok: true, verdict: 'no_changes' },
7061
+ `○ ${opts.stepKey} declared NO CHANGES — nothing to apply.`);
7062
+ return;
7063
+ }
7064
+ if (blocks.files.length > 0 || (blocks.edits && blocks.edits.length > 0) || blocks.deletes.length > 0) {
7065
+ let built;
7066
+ try {
7067
+ built = buildDiff(blocks, nodeMaterializeIO({ cwd: process.cwd() }));
7068
+ } catch (e) {
7069
+ if (e instanceof MaterializeError) {
7070
+ emit({ ok: false, verdict: 'does_not_apply', reason: e.code, detail: e.message },
7071
+ `✗ region edit could not be applied (${e.code}): ${e.message}`);
7072
+ process.exit(2);
7073
+ }
7074
+ throw e;
7075
+ }
7076
+ diff = built.diff;
7077
+ source = 'file-blocks';
7078
+ noChanges = built.partCount === 0;
7079
+ if (noChanges) {
7080
+ emit({ ok: true, verdict: 'no_changes' },
7081
+ `○ ${opts.stepKey} file blocks were identical to the tree — nothing to apply.`);
7082
+ return;
7083
+ }
7084
+ } else {
7085
+ // Legacy path: a hand-written ## Patch diff.
7086
+ ({ diff, source, noChanges } = extractDiff(output));
7087
+ if (noChanges) {
7088
+ emit({ ok: true, verdict: 'no_changes' },
7089
+ `○ ${opts.stepKey} declared NO CHANGES — nothing to apply.`);
7090
+ return;
7091
+ }
7092
+ if (!diff) {
7093
+ emit({ ok: false, verdict: 'no_diff', reason: 'no_diff' },
7094
+ `✗ ${opts.stepKey} output contains neither ## Changed Files blocks nor a ` +
7095
+ `unified diff — the agent produced prose. (Placeholder-cascade failure mode.)`);
7096
+ process.exit(1);
7097
+ }
7098
+ }
7099
+
7100
+ // Write to a temp file and run git apply --check against the current repo.
7101
+ const tmp = path.join(os.tmpdir(), `hone-check-patch-${Date.now()}.diff`);
7102
+ let result;
7103
+ try {
7104
+ fs.writeFileSync(tmp, diff);
7105
+ let code = 0, stderr = '';
7106
+ try {
7107
+ execFileSync('git', ['apply', '--check', tmp], {
7108
+ cwd: process.cwd(), env: gitEnv(), encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'],
7109
+ });
7110
+ } catch (e) {
7111
+ code = e.status || 1;
7112
+ stderr = (e.stderr || '').toString();
7113
+ }
7114
+ result = interpretApplyCheck({ code, stderr });
7115
+ } finally {
7116
+ try { fs.unlinkSync(tmp); } catch { /* best-effort */ }
7117
+ }
7118
+
7119
+ if (result.applies) {
7120
+ emit({ ok: true, verdict: result.verdict, source, detail: result.detail },
7121
+ `✓ ${opts.stepKey} diff ${result.detail} (${source} block)`);
7122
+ } else {
7123
+ emit({ ok: false, verdict: 'failed', source, detail: result.detail },
7124
+ `✗ ${opts.stepKey} diff does NOT apply: ${result.detail}\n` +
7125
+ ` The output is a well-formed diff but does not match the working tree — ` +
7126
+ `exactly the defect condition 5 catches that condition 4 cannot.`);
7127
+ process.exit(2); // distinct exit: 2 = does-not-apply, 1 = no-diff/fetch
7128
+ }
7129
+ });
7130
+
7131
+ // ── HC-019n-followup-24: verify-patch (pipeline-recovery condition 6) ─────────
7132
+ // Apply step_4's diff inside a throwaway git WORKTREE (checked out from HEAD),
7133
+ // run the adopter's test command there, report pass/fail. The real tree —
7134
+ // including uncommitted work — is never touched. Isolates the TREE, not the
7135
+ // process: ci.local_command is the adopter's own command, same trust as running
7136
+ // it by hand. Verdict is REPORTED, not yet enforced — the server-gate closed
7137
+ // loop is a deliberate follow-up (enabling a gate before its input is proven is
7138
+ // the HC-026c mistake).
7139
+ program
7140
+ .command('verify-patch <workflowId>')
7141
+ .description('Apply step_4\'s diff in an isolated worktree and run the tests (condition 6)')
7142
+ .option('--step-key <key>', 'Step to verify (default: step_4)', 'step_4')
7143
+ .option('--attempt <n>', 'Specific attempt number (default: latest)')
7144
+ .option('--command <cmd>', 'Test command (default: ci.local_command from config, else `make ci`)')
7145
+ .option('--timeout <sec>', 'Seconds before the test command is killed', '600')
7146
+ .option('--keep', 'Leave the worktree in place for debugging')
7147
+ .option('--report', 'HC-019n-followup-34: report the verdict back to the server (closed loop, advisory)')
7148
+ .option('--format <fmt>', 'Output format: pretty or json', 'pretty')
7149
+ .action(async (workflowId, opts) => {
7150
+ const { execFileSync, spawnSync } = require('child_process');
7151
+ const os = require('os');
7152
+ const { extractFileBlocks, buildDiff, nodeMaterializeIO, MaterializeError } = require('./lib/materialize-diff');
7153
+ const { resolveCommand, verdictFromRun, EXIT } = require('./lib/verify-patch');
7154
+ const { gitEnv } = require('./lib/git-env');
7155
+ const config = getConfig();
7156
+ const client = api(config);
7157
+
7158
+ const emit = (obj, humanLine) => {
7159
+ if (opts.format === 'json') console.log(JSON.stringify(obj));
7160
+ else console.log(humanLine);
7161
+ };
7162
+
7163
+ // 1. Fetch step_4 output.
7164
+ let output, runConfig;
7165
+ try {
7166
+ const url = `/orchestrate/${workflowId}/step/${opts.stepKey}` +
7167
+ (opts.attempt ? `?attempt=${opts.attempt}` : '');
7168
+ const r = await client.get(url);
7169
+ output = r.data?.output;
7170
+ // best-effort: pull the run's ci_local_command for the default
7171
+ try {
7172
+ const runR = await client.get(`/orchestrate/${workflowId}`);
7173
+ runConfig = runR.data?.config || {};
7174
+ } catch { runConfig = {}; }
7175
+ } catch (e) {
7176
+ const msg = e.response?.data?.error || e.message;
7177
+ emit({ ok: false, reason: 'fetch_failed', detail: msg },
7178
+ `✗ could not fetch ${opts.stepKey} output: ${msg}`);
7179
+ process.exit(EXIT.NO_DIFF);
7180
+ }
7181
+
7182
+ // 2. Materialize the diff (shared with check-patch/emit-pr, followup-23/27).
7183
+ // Condition 6 presupposes 5.
7184
+ const blocks = extractFileBlocks(output);
7185
+ const hasWork = blocks.files.length > 0 || (blocks.edits && blocks.edits.length > 0) || blocks.deletes.length > 0;
7186
+ if (blocks.noChanges || !hasWork) {
7187
+ emit({ ok: false, verdict: 'no_diff', reason: 'no_changes_or_diff' },
7188
+ `✗ ${opts.stepKey} produced no file changes to verify.`);
7189
+ process.exit(EXIT.NO_DIFF);
7190
+ }
7191
+ let diff;
7192
+ try {
7193
+ diff = buildDiff(blocks, nodeMaterializeIO({ cwd: process.cwd() })).diff;
7194
+ } catch (e) {
7195
+ if (e instanceof MaterializeError) {
7196
+ emit({ ok: false, verdict: 'does_not_apply', reason: e.code, detail: e.message },
7197
+ `✗ region edit could not be applied (${e.code}): ${e.message}`);
7198
+ process.exit(EXIT.DOES_NOT_APPLY);
7199
+ }
7200
+ throw e;
7201
+ }
7202
+ if (!diff.trim()) {
7203
+ emit({ ok: true, verdict: 'no_changes' },
7204
+ `○ ${opts.stepKey} file blocks matched the tree — nothing to verify.`);
7205
+ return;
7206
+ }
7207
+
7208
+ // 3. Isolated worktree from HEAD.
7209
+ const wt = fs.mkdtempSync(path.join(os.tmpdir(), 'hone-verify-'));
7210
+ const diffFile = path.join(os.tmpdir(), `hone-verify-${Date.now()}.diff`);
7211
+ fs.writeFileSync(diffFile, diff);
7212
+ const command = resolveCommand(opts.command, runConfig?.ci_local_command);
7213
+ let cleanupDone = false;
7214
+ const cleanup = () => {
7215
+ if (cleanupDone || opts.keep) return;
7216
+ cleanupDone = true;
7217
+ try { execFileSync('git', ['worktree', 'remove', '--force', wt], { cwd: process.cwd(), env: gitEnv(), stdio: 'ignore' }); } catch { /* best-effort */ }
7218
+ try { fs.rmSync(wt, { recursive: true, force: true }); } catch { /* best-effort */ }
7219
+ try { fs.unlinkSync(diffFile); } catch { /* best-effort */ }
7220
+ };
7221
+ // Guard against leaving a worktree if the process is interrupted.
7222
+ process.on('SIGINT', () => { cleanup(); process.exit(130); });
7223
+
7224
+ // HC-019n-followup-24 fix: NEVER call process.exit() inside the try — it
7225
+ // skips the finally, so the worktree leaks (found by the live verification:
7226
+ // three hone-verify-* worktrees survived). Compute the exit code, let the
7227
+ // finally clean up, then exit.
7228
+ let exitCode = EXIT.PASS;
7229
+ // HC-019n-followup-34: capture the verdict + HEAD sha for the closed-loop
7230
+ // report (--report). Hoisted so they survive the try/finally to the POST.
7231
+ let reportVerdict = null, reportDetail = null, headSha = null;
7232
+ try { headSha = execFileSync('git', ['rev-parse', 'HEAD'], { cwd: process.cwd(), env: gitEnv(), encoding: 'utf8' }).trim(); } catch { /* best-effort */ }
7233
+ try {
7234
+ execFileSync('git', ['worktree', 'add', '--detach', wt, 'HEAD'],
7235
+ { cwd: process.cwd(), env: gitEnv(), stdio: ['ignore', 'pipe', 'pipe'] });
7236
+
7237
+ // 4. Apply the diff INTO the worktree (real apply, isolated).
7238
+ let applied = true, applyDetail = '';
7239
+ try {
7240
+ execFileSync('git', ['-C', wt, 'apply', diffFile], { env: gitEnv(), stdio: ['ignore', 'pipe', 'pipe'] });
7241
+ } catch (e) {
7242
+ applied = false;
7243
+ applyDetail = (e.stderr || '').toString().split('\n').find(Boolean) || 'git apply failed';
7244
+ }
7245
+ if (!applied) {
7246
+ emit({ ok: false, verdict: 'does_not_apply', detail: applyDetail },
7247
+ `✗ diff did not apply in the worktree: ${applyDetail}`);
7248
+ exitCode = EXIT.DOES_NOT_APPLY;
7249
+ reportVerdict = 'does_not_apply'; reportDetail = applyDetail;
7250
+ } else {
7251
+ // 5. Run the adopter's test command in the worktree.
7252
+ if (opts.format !== 'json') console.log(` running: ${command} (in isolated worktree, timeout ${opts.timeout}s)`);
7253
+ const run = spawnSync(command, {
7254
+ cwd: wt, shell: true, encoding: 'utf8',
7255
+ timeout: Number(opts.timeout) * 1000, env: gitEnv(),
7256
+ stdio: ['ignore', 'pipe', 'pipe'],
7257
+ });
7258
+ const timedOut = run.error && run.error.code === 'ETIMEDOUT';
7259
+ const v = verdictFromRun({ code: run.status, timedOut, stdout: run.stdout, stderr: run.stderr });
7260
+ const sym = v.verdict === 'pass' ? '✓' : v.verdict === 'timeout' ? '⏱' : '✗';
7261
+ emit(
7262
+ { ok: v.verdict === 'pass', verdict: v.verdict, command, detail: v.detail },
7263
+ `${sym} ${v.detail}\n (command: ${command})`);
7264
+ exitCode = v.exitCode;
7265
+ reportVerdict = v.verdict; reportDetail = v.detail;
7266
+ }
7267
+ } catch (e) {
7268
+ emit({ ok: false, verdict: 'error', detail: e.message },
7269
+ `✗ verify-patch failed: ${e.message}`);
7270
+ exitCode = 1;
7271
+ reportVerdict = 'error'; reportDetail = e.message;
7272
+ } finally {
7273
+ cleanup();
7274
+ }
7275
+
7276
+ // HC-019n-followup-34: the closed loop — report the EXECUTED verdict back to
7277
+ // the server (advisory record). Best-effort: a reporting failure must not
7278
+ // change the local exit code (the verdict is already true locally).
7279
+ if (opts.report && reportVerdict) {
7280
+ try {
7281
+ await client.post(`/orchestrate/${workflowId}/verdict`, {
7282
+ source: 'verify-patch',
7283
+ stepKey: opts.stepKey,
7284
+ verdict: reportVerdict,
7285
+ exitCode,
7286
+ detail: reportDetail,
7287
+ attempt: opts.attempt ? Number(opts.attempt) : undefined,
7288
+ headSha,
7289
+ });
7290
+ if (opts.format !== 'json') console.log(` → reported verdict '${reportVerdict}' to the server (advisory)`);
7291
+ } catch (e) {
7292
+ if (opts.format !== 'json') console.warn(` ⚠ verdict report failed (non-fatal): ${e.response?.data?.error || e.message}`);
7293
+ }
7294
+ }
7295
+ process.exit(exitCode);
7296
+ });
7297
+
7298
+ // ── HC-019n-followup-25: emit-pr (pipeline-recovery condition 7) ──────────────
7299
+ // Condition 6 proved step_4's diff applies AND its tests pass in isolation.
7300
+ // Condition 7 turns that verified diff into the reviewable artifact: a branch
7301
+ // and (opt-in) a draft PR. Publishing is staged — tier-1 dry run by default;
7302
+ // --push, then --open-pr. Sibling to check-patch/verify-patch (top-level).
7303
+ program
7304
+ .command('emit-pr <workflowId>')
7305
+ .description('Emit a branch + draft PR from step_4\'s verified diff (condition 7)')
7306
+ .option('--step-key <key>', 'Step to emit (default: step_4)', 'step_4')
7307
+ .option('--attempt <n>', 'Specific attempt number (default: latest)')
7308
+ .option('--branch <name>', 'Override the generated branch name')
7309
+ .option('--base <ref>', 'Target base branch for the PR (default: repo default)')
7310
+ .option('--push', 'Push the branch to origin (default: local dry run only)')
7311
+ .option('--open-pr', 'Open a draft PR (implies --push)')
7312
+ .option('--ready', 'Open the PR ready-for-review instead of draft')
7313
+ .option('--skip-verify', 'Do not re-run the tests before committing (downgrades provenance)')
7314
+ .option('--command <cmd>', 'Test command for --verify (default: ci.local_command, else `make ci`)')
7315
+ .option('--timeout <sec>', 'Seconds before the verify test command is killed', '600')
7316
+ .option('--keep', 'Leave the worktree in place for debugging')
7317
+ .option('--format <fmt>', 'Output format: pretty or json', 'pretty')
7318
+ .action(async (workflowId, opts) => {
7319
+ const { execFileSync, spawnSync } = require('child_process');
7320
+ const os = require('os');
7321
+ const { extractFileBlocks, buildDiff, nodeMaterializeIO, MaterializeError } = require('./lib/materialize-diff');
7322
+ const { resolveCommand, verdictFromRun } = require('./lib/verify-patch');
7323
+ const {
7324
+ EXIT, buildBranchName, buildCommitMessage, buildPrBody, decidePublish,
7325
+ } = require('./lib/emit-pr');
7326
+ const { gitEnv } = require('./lib/git-env');
7327
+ const config = getConfig();
7328
+ const client = api(config);
7329
+
7330
+ // --open-pr implies --push (cannot open a PR against an unpushed branch).
7331
+ const doPush = Boolean(opts.push || opts.openPr);
7332
+ const doOpenPr = Boolean(opts.openPr);
7333
+ const doVerify = !opts.skipVerify;
7334
+
7335
+ const emit = (obj, humanLine) => {
7336
+ if (opts.format === 'json') console.log(JSON.stringify(obj));
7337
+ else if (humanLine != null) console.log(humanLine);
7338
+ };
7339
+
7340
+ // 1. Fetch step_4 output + the run's storyId (for naming/provenance).
7341
+ let output, storyId = 'story', runConfig = {};
7342
+ try {
7343
+ const url = `/orchestrate/${workflowId}/step/${opts.stepKey}` +
7344
+ (opts.attempt ? `?attempt=${opts.attempt}` : '');
7345
+ const r = await client.get(url);
7346
+ output = r.data?.output;
7347
+ try {
7348
+ const runR = await client.get(`/orchestrate/${workflowId}`);
7349
+ storyId = runR.data?.storyId || runR.data?.story_id || 'story';
7350
+ runConfig = runR.data?.config || {};
7351
+ } catch { /* naming falls back to 'story'; command falls back to make ci */ }
7352
+ } catch (e) {
7353
+ const msg = e.response?.data?.error || e.message;
7354
+ emit({ ok: false, reason: 'fetch_failed', detail: msg },
7355
+ `✗ could not fetch ${opts.stepKey} output: ${msg}`);
7356
+ process.exit(EXIT.NO_DIFF);
7357
+ }
7358
+
7359
+ // 2. Materialize the diff (shared with check-patch/verify-patch,
7360
+ // followup-23/27). Condition 7 presupposes 5.
7361
+ const blocks = extractFileBlocks(output);
7362
+ const hasWork = blocks.files.length > 0 || (blocks.edits && blocks.edits.length > 0) || blocks.deletes.length > 0;
7363
+ if (blocks.noChanges || !hasWork) {
7364
+ emit({ ok: false, verdict: 'no_diff', reason: 'no_changes_or_diff' },
7365
+ `✗ ${opts.stepKey} produced no file changes to emit.`);
7366
+ process.exit(EXIT.NO_DIFF);
7367
+ }
7368
+ let diff;
7369
+ try {
7370
+ diff = buildDiff(blocks, nodeMaterializeIO({ cwd: process.cwd() })).diff;
7371
+ } catch (e) {
7372
+ if (e instanceof MaterializeError) {
7373
+ emit({ ok: false, verdict: 'does_not_apply', reason: e.code, detail: e.message },
7374
+ `✗ region edit could not be applied (${e.code}): ${e.message}`);
7375
+ process.exit(EXIT.DOES_NOT_APPLY);
7376
+ }
7377
+ throw e;
7378
+ }
7379
+ if (!diff.trim()) {
7380
+ emit({ ok: true, verdict: 'no_changes' },
7381
+ `○ ${opts.stepKey} file blocks matched the tree — nothing to emit.`);
7382
+ return;
7383
+ }
7384
+
7385
+ // 3. Resolve names + probe idempotency BEFORE any mutation.
7386
+ let headSha = '';
7387
+ try {
7388
+ headSha = execFileSync('git', ['rev-parse', 'HEAD'],
7389
+ { cwd: process.cwd(), env: gitEnv(), encoding: 'utf8' }).trim();
7390
+ } catch {
7391
+ emit({ ok: false, reason: 'no_head' }, '✗ repository has no HEAD commit — cannot branch from nothing.');
7392
+ process.exit(1);
7393
+ }
7394
+ const branch = buildBranchName({ storyId, workflowId, override: opts.branch });
7395
+
7396
+ const branchExists = (() => {
7397
+ try {
7398
+ execFileSync('git', ['rev-parse', '--verify', '--quiet', `refs/heads/${branch}`],
7399
+ { cwd: process.cwd(), env: gitEnv(), stdio: 'ignore' });
7400
+ return true;
7401
+ } catch { return false; }
7402
+ })();
7403
+ // Only probe the remote for an existing PR when we intend to publish.
7404
+ let openPrUrl = null;
7405
+ if (doOpenPr) {
7406
+ try {
7407
+ const out = execFileSync('gh', ['pr', 'list', '--head', branch, '--state', 'open', '--json', 'url', '--jq', '.[0].url'],
7408
+ { cwd: process.cwd(), env: gitEnv(), encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
7409
+ openPrUrl = out || null;
7410
+ } catch { openPrUrl = null; /* gh missing/unauthed handled at push/open time */ }
7411
+ }
7412
+ const decision = decidePublish({ branchExists, openPrUrl });
7413
+ if (decision.action === 'already_open') {
7414
+ emit({ ok: true, verdict: 'already_open', prUrl: decision.prUrl, branch },
7415
+ `✓ a draft PR is already open for ${branch}: ${decision.prUrl}`);
7416
+ process.exit(EXIT.PASS);
7417
+ }
7418
+ if (decision.action === 'branch_conflict') {
7419
+ emit({ ok: false, verdict: 'branch_conflict', branch },
7420
+ `✗ branch '${branch}' already exists — not overwriting. Use --branch to pick another name.`);
7421
+ process.exit(EXIT.BRANCH_CONFLICT);
7422
+ }
7423
+
7424
+ // 4. Isolated worktree with a NAMED branch off HEAD (adopter tree untouched).
7425
+ const wt = fs.mkdtempSync(path.join(os.tmpdir(), 'hone-emit-'));
7426
+ const diffFile = path.join(os.tmpdir(), `hone-emit-${Date.now()}.diff`);
7427
+ fs.writeFileSync(diffFile, diff);
7428
+ const command = resolveCommand(opts.command, runConfig?.ci_local_command);
7429
+ // `worktree add -b` creates the branch ref BEFORE apply/verify. If we bail
7430
+ // before a successful commit (apply fails, verify fails), that empty branch
7431
+ // (pointing at HEAD) must be torn down too — else "a failing diff never
7432
+ // reaches a branch" would be false and a re-run would hit a bogus conflict.
7433
+ // `committed` gates it: once we have the deliverable commit, keep the branch.
7434
+ let cleanupDone = false;
7435
+ let committed = false;
7436
+ const cleanup = () => {
7437
+ if (cleanupDone || opts.keep) return;
7438
+ cleanupDone = true;
7439
+ try { execFileSync('git', ['worktree', 'remove', '--force', wt], { cwd: process.cwd(), env: gitEnv(), stdio: 'ignore' }); } catch { /* best-effort */ }
7440
+ // Remove the leaked empty branch only when nothing was committed onto it.
7441
+ if (!committed) {
7442
+ try { execFileSync('git', ['branch', '-D', branch], { cwd: process.cwd(), env: gitEnv(), stdio: 'ignore' }); } catch { /* best-effort */ }
7443
+ }
7444
+ try { fs.rmSync(wt, { recursive: true, force: true }); } catch { /* best-effort */ }
7445
+ try { fs.unlinkSync(diffFile); } catch { /* best-effort */ }
7446
+ };
7447
+ process.on('SIGINT', () => { cleanup(); process.exit(130); });
7448
+
7449
+ // HC-019n-followup-24 lesson: NEVER process.exit() inside the try — it skips
7450
+ // the finally and leaks the worktree. Capture exitCode; exit after cleanup.
7451
+ let exitCode = EXIT.PASS;
7452
+ let verifyState = 'skipped';
7453
+ try {
7454
+ execFileSync('git', ['worktree', 'add', '-b', branch, wt, 'HEAD'],
7455
+ { cwd: process.cwd(), env: gitEnv(), stdio: ['ignore', 'pipe', 'pipe'] });
7456
+
7457
+ // 4a. Apply the diff INTO the worktree.
7458
+ let applied = true, applyDetail = '';
7459
+ try {
7460
+ execFileSync('git', ['-C', wt, 'apply', diffFile], { env: gitEnv(), stdio: ['ignore', 'pipe', 'pipe'] });
7461
+ } catch (e) {
7462
+ applied = false;
7463
+ applyDetail = (e.stderr || '').toString().split('\n').find(Boolean) || 'git apply failed';
7464
+ }
7465
+ if (!applied) {
7466
+ emit({ ok: false, verdict: 'does_not_apply', detail: applyDetail },
7467
+ `✗ diff did not apply in the worktree: ${applyDetail}`);
7468
+ exitCode = EXIT.DOES_NOT_APPLY;
7469
+ } else {
7470
+ // 4b. Re-verify inline (default) — a failing diff never reaches a branch.
7471
+ let verifyOk = true;
7472
+ if (doVerify) {
7473
+ if (opts.format !== 'json') console.log(` verifying: ${command} (in isolated worktree, timeout ${opts.timeout}s)`);
7474
+ const run = spawnSync(command, {
7475
+ cwd: wt, shell: true, encoding: 'utf8',
7476
+ timeout: Number(opts.timeout) * 1000, env: gitEnv(),
7477
+ stdio: ['ignore', 'pipe', 'pipe'],
7478
+ });
7479
+ const timedOut = run.error && run.error.code === 'ETIMEDOUT';
7480
+ const v = verdictFromRun({ code: run.status, timedOut, stdout: run.stdout, stderr: run.stderr });
7481
+ if (v.verdict === 'pass') {
7482
+ verifyState = 'pass';
7483
+ } else {
7484
+ verifyOk = false;
7485
+ const sym = v.verdict === 'timeout' ? '⏱' : '✗';
7486
+ emit({ ok: false, verdict: v.verdict, command, detail: v.detail },
7487
+ `${sym} verify failed, no branch emitted: ${v.detail}\n (command: ${command})`);
7488
+ exitCode = v.exitCode; // 3 tests-failed or 4 timeout
7489
+ }
7490
+ }
7491
+
7492
+ if (verifyOk) {
7493
+ // 4c. Commit the applied diff onto the branch, inside the worktree.
7494
+ // No summary source (GET /orchestrate omits config/story text), so the
7495
+ // commit subject falls back to the storyId + default in buildCommitMessage.
7496
+ const message = buildCommitMessage({ storyId, summary: '', workflowId, headSha, verifyState });
7497
+ const msgFile = path.join(os.tmpdir(), `hone-emit-msg-${Date.now()}.txt`);
7498
+ fs.writeFileSync(msgFile, message);
7499
+ try {
7500
+ execFileSync('git', ['-C', wt, 'add', '-A'], { env: gitEnv(), stdio: ['ignore', 'pipe', 'pipe'] });
7501
+ execFileSync('git', ['-C', wt, 'commit', '-F', msgFile], { env: gitEnv(), stdio: ['ignore', 'pipe', 'pipe'] });
7502
+ committed = true; // the branch now holds the deliverable — keep it past cleanup
7503
+ } finally { try { fs.unlinkSync(msgFile); } catch { /* best-effort */ } }
7504
+
7505
+ const stat = (() => {
7506
+ try {
7507
+ return execFileSync('git', ['-C', wt, 'show', '--stat', '--oneline', 'HEAD'],
7508
+ { env: gitEnv(), encoding: 'utf8' }).trim();
7509
+ } catch { return ''; }
7510
+ })();
7511
+
7512
+ // ── Boundary: everything above is local + reversible. ──
7513
+ let prUrl = null, pushed = false;
7514
+ if (doPush) {
7515
+ try {
7516
+ execFileSync('git', ['push', '-u', 'origin', branch],
7517
+ { cwd: process.cwd(), env: gitEnv(), stdio: ['ignore', 'pipe', 'pipe'] });
7518
+ pushed = true;
7519
+ } catch (e) {
7520
+ const detail = (e.stderr || e.message || '').toString().split('\n').find(Boolean) || 'push failed';
7521
+ emit({ ok: false, verdict: 'publish_failed', stage: 'push', detail, branch },
7522
+ `✗ push failed (branch committed locally as '${branch}', not published): ${detail}`);
7523
+ exitCode = EXIT.PUBLISH_FAILED;
7524
+ }
7525
+ }
7526
+ if (pushed && doOpenPr) {
7527
+ const prBody = buildPrBody({ storyId, workflowId, headSha, verifyState, command: doVerify ? command : undefined });
7528
+ const bodyFile = path.join(os.tmpdir(), `hone-emit-prbody-${Date.now()}.md`);
7529
+ fs.writeFileSync(bodyFile, prBody);
7530
+ const title = `${storyId}: Hone step_4 change (workflow ${String(workflowId).slice(0, 8)})`;
7531
+ const ghArgs = ['pr', 'create', '--head', branch, '--title', title, '--body-file', bodyFile];
7532
+ if (!opts.ready) ghArgs.push('--draft');
7533
+ if (opts.base) ghArgs.push('--base', opts.base);
7534
+ try {
7535
+ prUrl = execFileSync('gh', ghArgs,
7536
+ { cwd: process.cwd(), env: gitEnv(), encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim();
7537
+ } catch (e) {
7538
+ const detail = (e.stderr || e.message || '').toString().split('\n').find(Boolean) || 'gh pr create failed';
7539
+ emit({ ok: false, verdict: 'publish_failed', stage: 'open_pr', detail, branch },
7540
+ `✗ branch pushed, but opening the PR failed: ${detail}\n → open it manually, or check \`gh auth status\`.`);
7541
+ exitCode = EXIT.PUBLISH_FAILED;
7542
+ } finally { try { fs.unlinkSync(bodyFile); } catch { /* best-effort */ } }
7543
+ }
7544
+
7545
+ if (exitCode === EXIT.PASS) {
7546
+ const tier = prUrl ? 'pr' : pushed ? 'pushed' : 'dry_run';
7547
+ const human = prUrl
7548
+ ? `✓ draft PR opened: ${prUrl}`
7549
+ : pushed
7550
+ ? `✓ branch pushed to origin/${branch}\n → open a PR: gh pr create --head ${branch} --draft`
7551
+ : `✓ branch '${branch}' built locally (dry run — nothing pushed)\n` +
7552
+ ` verify: ${verifyState}\n\n${stat}\n\n` +
7553
+ ` → push it: hone emit-pr ${workflowId} --push\n` +
7554
+ ` → push + PR: hone emit-pr ${workflowId} --open-pr\n` +
7555
+ ` → discard it: git branch -D ${branch}`;
7556
+ emit({ ok: true, verdict: tier, branch, verifyState, prUrl, pushed, headSha }, human);
7557
+ }
7558
+ }
7559
+ }
7560
+ } catch (e) {
7561
+ emit({ ok: false, verdict: 'error', detail: e.message }, `✗ emit-pr failed: ${e.message}`);
7562
+ exitCode = 1;
7563
+ } finally {
7564
+ cleanup();
7565
+ }
7566
+ process.exit(exitCode);
7567
+ });
7568
+
7569
+ // ── HC-019n-followup-32: verify-pr (pipeline-standards G4 §2, G3 functional) ────
7570
+ // The post-emit-pr step: once a REAL PR exists, run the real skill audit
7571
+ // (`hone step-5b`) + the real CI gate (`gh pr checks` / `make ci`), writing the
7572
+ // two artifacts (step-5b-skill-audit.md, step-5c-ci.md) the server DAG can only
7573
+ // MODEL, never execute (those steps are serverExecutable:false). VERIFY + REPORT
7574
+ // only — no auto-fix loop; the operator (or a later closed loop) acts on the
7575
+ // verdict. Sibling to check-patch → verify-patch → emit-pr → verify-pr.
7576
+ program
7577
+ .command('verify-pr <prOrBranch>')
7578
+ .description('Post-PR verification: real skill audit (hone step-5b) + real CI gate (gh pr checks / make ci) (HC-019n-followup-32)')
7579
+ .option('--story-id <id>', 'override story id (defaults to extraction from the branch)')
7580
+ .option('--base <branch>', 'base branch for the skill-audit diff', 'develop')
7581
+ .option('--mode <mode>', 'CI gate mode: github | local | both | none (default: ci.gate config, else github)')
7582
+ .option('--command <cmd>', 'local CI command (default: ci.local_command, else make ci)')
7583
+ .option('--timeout <sec>', 'seconds before the local CI command is killed', '900')
7584
+ .option('--skip-skill-audit', 'skip the hone step-5b skill audit')
7585
+ .option('--report', 'HC-019n-followup-36: report the CI-gate verdict back to the server (closed loop, records step_5c)')
7586
+ .option('--workflow-id <id>', 'the run id to report the verdict against (required with --report)')
7587
+ .option('--format <fmt>', 'pretty or json', 'pretty')
7588
+ .action(async (prOrBranch, opts) => {
7589
+ const { execFileSync, spawnSync } = require('child_process');
7590
+ const { readCIGateConfig } = require('./lib/pipeline-config');
7591
+ const { extractStoryIdFromBranch } = require('./lib/pipeline-status');
7592
+ const { EXIT, resolveGateMode, interpretGhChecks, ciGateVerdict, renderStep5cArtifact } = require('./lib/verify-pr');
7593
+ const { gitEnv } = require('./lib/git-env');
7594
+ const repoRoot = process.cwd();
7595
+ const emit = (obj, human) => {
7596
+ if (opts.format === 'json') console.log(JSON.stringify(obj));
7597
+ else if (human != null) console.log(human);
7598
+ };
7599
+
7600
+ // 1. Story id: flag > branch > the ref itself.
7601
+ let branch = '';
7602
+ try { branch = execFileSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: repoRoot, env: gitEnv(), encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); } catch { /* */ }
7603
+ const storyId = opts.storyId || extractStoryIdFromBranch(branch) || extractStoryIdFromBranch(prOrBranch) || 'unknown-story';
7604
+
7605
+ // 2. CI gate mode: --mode > ci.gate config > github.
7606
+ let configGate = 'github';
7607
+ try { configGate = readCIGateConfig(repoRoot).gate; } catch { /* default */ }
7608
+ const mode = resolveGateMode(opts.mode, configGate);
7609
+
7610
+ // 3. Skill audit — reuse the working `hone step-5b` command (deterministic
7611
+ // SA-001 engine over the PR diff). Informational; never affects the exit.
7612
+ let skillAudit = { ran: false };
7613
+ if (!opts.skipSkillAudit) {
7614
+ try {
7615
+ execFileSync(process.execPath, [__filename, 'step-5b', '--story-id', storyId, '--base', opts.base],
7616
+ { cwd: repoRoot, env: process.env, stdio: opts.format === 'json' ? 'ignore' : 'inherit' });
7617
+ skillAudit = { ran: true, artifact: `.github/pipeline/${storyId}/step-5b-skill-audit.md` };
7618
+ } catch (e) {
7619
+ skillAudit = { ran: true, error: (e.message || '').split('\n')[0] };
7620
+ }
7621
+ }
7622
+
7623
+ // 4. CI gate.
7624
+ let gh = null, local = null;
7625
+ if (mode === 'github' || mode === 'both') {
7626
+ let prNum = /^\d+$/.test(prOrBranch) ? prOrBranch : null;
7627
+ if (!prNum) { const m = String(prOrBranch).match(/\/pull\/(\d+)/); if (m) prNum = m[1]; }
7628
+ if (!prNum) {
7629
+ try { prNum = execFileSync('gh', ['pr', 'list', '--head', prOrBranch, '--state', 'all', '--json', 'number', '--jq', '.[0].number'], { cwd: repoRoot, env: gitEnv(), encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim() || null; } catch { prNum = null; }
7630
+ }
7631
+ if (!prNum) {
7632
+ emit({ ok: false, reason: 'no_pr', ref: prOrBranch },
7633
+ `✗ could not resolve a PR for '${prOrBranch}' — github mode needs a PR number, URL, or a branch with a PR.`);
7634
+ process.exit(EXIT.NO_PR);
7635
+ }
7636
+ // gh pr checks exits non-zero when checks fail/pend, but still prints JSON.
7637
+ let raw = '';
7638
+ try {
7639
+ raw = execFileSync('gh', ['pr', 'checks', prNum, '--json', 'name,state,bucket'], { cwd: repoRoot, env: gitEnv(), encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] });
7640
+ } catch (e) { raw = (e.stdout || '').toString(); }
7641
+ try { gh = interpretGhChecks(JSON.parse(raw)); } catch { gh = null; }
7642
+ }
7643
+ if (mode === 'local' || mode === 'both') {
7644
+ let command = (opts.command && opts.command.trim()) || '';
7645
+ if (!command) { try { command = readCIGateConfig(repoRoot).local_command; } catch { command = 'make ci'; } }
7646
+ command = command || 'make ci';
7647
+ if (opts.format !== 'json') console.log(` running local CI: ${command} (timeout ${opts.timeout}s)`);
7648
+ const run = spawnSync(command, { cwd: repoRoot, shell: true, encoding: 'utf8', timeout: Number(opts.timeout) * 1000, stdio: ['ignore', 'inherit', 'inherit'] });
7649
+ local = { code: run.status == null ? 1 : run.status, timedOut: !!(run.error && run.error.code === 'ETIMEDOUT') };
7650
+ }
7651
+
7652
+ const verdict = ciGateVerdict({ mode, gh, local });
7653
+
7654
+ // 5. Write the real step-5c-ci.md artifact.
7655
+ try {
7656
+ const dir = path.join(repoRoot, '.github/pipeline', storyId);
7657
+ fs.mkdirSync(dir, { recursive: true });
7658
+ fs.writeFileSync(path.join(dir, 'step-5c-ci.md'), renderStep5cArtifact({ storyId, mode, prRef: prOrBranch, gh, local, verdict }));
7659
+ } catch { /* best-effort */ }
7660
+
7661
+ // 6. Closed loop (HC-019n-followup-36): report the CI-gate verdict to the
7662
+ // server. This RECORDS against step_5c — which is a post-PR, CLI-produced
7663
+ // node (serverExecutable:false), so there is no server gate to auto-approve:
7664
+ // verify-pr's verdict is ADVISORY audit even in enforce mode (enforce gates
7665
+ // step_4, the build). Best-effort — never changes the local exit code.
7666
+ let reported = false;
7667
+ if (opts.report) {
7668
+ if (!opts.workflowId) {
7669
+ if (opts.format !== 'json') console.warn(' ⚠ --report needs --workflow-id <run id> to address the run; skipping report.');
7670
+ } else {
7671
+ try {
7672
+ const client = api(getConfig());
7673
+ await client.post(`/orchestrate/${opts.workflowId}/verdict`, {
7674
+ source: 'verify-pr',
7675
+ stepKey: 'step_5c',
7676
+ verdict: verdict.verdict,
7677
+ exitCode: verdict.exitCode,
7678
+ detail: verdict.detail,
7679
+ });
7680
+ reported = true;
7681
+ if (opts.format !== 'json') console.log(` → reported CI-gate verdict '${verdict.verdict}' to run ${opts.workflowId} (advisory)`);
7682
+ } catch (e) {
7683
+ if (opts.format !== 'json') console.warn(` ⚠ verdict report failed (non-fatal): ${e.response?.data?.error || e.message}`);
7684
+ }
7685
+ }
7686
+ }
7687
+
7688
+ // 7. Report. The exit code is the CI verdict's; the skill audit is advisory.
7689
+ const sym = (verdict.verdict === 'pass' || verdict.verdict === 'disabled') ? '✓' : verdict.verdict === 'pending' ? '⏳' : '✗';
7690
+ emit(
7691
+ { ok: verdict.exitCode === EXIT.PASS, storyId, mode, skillAudit, ciGate: verdict, gh, local, reported },
7692
+ `${sym} CI gate (${mode}): ${verdict.verdict} — ${verdict.detail}\n` +
7693
+ ` skill audit: ${opts.skipSkillAudit ? 'skipped' : (skillAudit.artifact || skillAudit.error || 'done')}\n` +
7694
+ ` → .github/pipeline/${storyId}/step-5c-ci.md`);
7695
+ process.exit(verdict.exitCode);
7696
+ });
7697
+
6443
7698
  // ── CLI setup ─────────────────────────────────────────────────────────────────
6444
7699
  program
6445
7700
  .name('hone')