voyage/lib/parsers/arg-parser.mjs
Kjell Tore Guttormsen f5dc08f660
feat(intent): /trekplan halts without an approved intent; the approval is stamped on both paths
Veikart steg 1, økt 1 av 2. The three holes measured in the grounds:
(1) ## Intent / ## Goal were only checked for existence, (2) no approval
marker existed, (3) brief-approved was emitted only on the auto path.

- lib/validators/intent-approval.mjs: --check / --stamp. The marker is
  intent_approved_hash (sha256 over normalized ## Intent + ## Goal) +
  intent_approved_at. Editing either section after approval → STALE.
- /trekplan Phase 1 (Read the brief, both --brief and --project): runs
  --check and HALTS on BRIEF_INTENT_NOT_APPROVED / _STALE / _INVALID with
  the remedy spelled out; an unrunnable check halts too.
- /trekbrief Phase 4h (before the Phase 5 fork, asked even in --quick):
  shows Intent + Goal verbatim, AskUserQuestion Approve / Revise / Leave;
  only "Approve" runs --stamp. --stamp emits brief-approved, so the manual
  (default) path records it; the auto path's own emission is removed.
  New mode /trekbrief --approve <project-dir> = Phase 4h alone.
- README, CLAUDE.md, command-modes, HANDOVER-CONTRACTS §Handover 1,
  jsonl-schemas (trekbrief-stats gains intent_approved).

Valgt ingen brief_version-bump fordi skjemaendringen er rent additiv (to
valgfrie felt) og kravet sitter i /trekplan — enhver produsents brief kan
godkjennes via /trekbrief --approve uten produsentendring. Valgt eget
--check-kall i stedet for et flagg på brief-validator fordi --brief-stien
i dag ikke kjører validatoren i det hele tatt; et nytt validatorkall der
ville også stoppe på andre feil. Valgt fallback-datamappe = målestokkens
(plugins/data/voyage-…) fordi CLAUDE_PLUGIN_DATA er tom i Bash-miljøet og
event-emits egen fallback er stille skip — slik ble brief-approved 0 records.

What the marker does NOT prove (module header, command prose, contract):
same user, same machine — any session can stamp. A trace, not a signature.

Suite 1183 → 1201 (1199/0/2). Mutants M1–M5 (stale check off, check
always valid, gate line removed, stamp emits nothing, no normalization)
each fell ≥ 1 test. yardstick unchanged: RED, 1 of 3 countable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 07:50:51 +02:00

127 lines
3.3 KiB
JavaScript

// lib/parsers/arg-parser.mjs
// Parse $ARGUMENTS strings for the four voyage commands.
//
// Each command has its own valid-flag set; passing flags from another command
// produces an `unknown_flags` array but does not error — the caller decides.
const FLAG_SCHEMA = {
trekbrief: {
boolean: ['--quick', '--fg'],
valued: ['--profile', '--approve'],
aliases: {},
},
trekresearch: {
boolean: ['--quick', '--local', '--external', '--fg'],
valued: ['--project', '--profile'],
aliases: {},
},
trekplan: {
boolean: ['--quick', '--fg'],
valued: ['--project', '--brief', '--export', '--decompose', '--profile'],
multi: ['--research'],
aliases: {},
},
trekexecute: {
boolean: ['--resume', '--dry-run', '--validate', '--fg'],
valued: ['--project', '--step', '--session', '--profile'],
aliases: {},
},
trekreview: {
boolean: ['--quick', '--fg', '--dry-run', '--validate', '--workflow'],
valued: ['--project', '--since', '--profile'],
aliases: {},
},
trekcontinue: {
boolean: ['--help', '--cleanup', '--confirm', '--dry-run'],
valued: ['--profile'],
aliases: {},
},
};
/**
* @param {string} argString Raw $ARGUMENTS as the command sees it.
* @param {keyof FLAG_SCHEMA} command
* @returns {{
* command: string,
* flags: Record<string, true | string | string[]>,
* positional: string[],
* unknown: string[],
* errors: Array<{code: string, message: string}>,
* }}
*/
export function parseArgs(argString, command) {
const schema = FLAG_SCHEMA[command];
if (!schema) {
return {
command,
flags: {},
positional: [],
unknown: [],
errors: [{ code: 'ARG_UNKNOWN_COMMAND', message: `Unknown command: ${command}` }],
};
}
const tokens = tokenize(argString);
const flags = {};
const positional = [];
const unknown = [];
const errors = [];
for (let i = 0; i < tokens.length; i++) {
const tok = tokens[i];
if (!tok.startsWith('--')) {
positional.push(tok);
continue;
}
if (schema.boolean.includes(tok)) {
flags[tok] = true;
continue;
}
if (schema.valued.includes(tok)) {
const next = tokens[i + 1];
if (next === undefined || next.startsWith('--')) {
errors.push({ code: 'ARG_MISSING_VALUE', message: `Flag ${tok} requires a value` });
} else {
flags[tok] = next;
i++;
}
continue;
}
if (schema.multi && schema.multi.includes(tok)) {
const collected = [];
while (i + 1 < tokens.length && !tokens[i + 1].startsWith('--')) {
collected.push(tokens[i + 1]);
i++;
}
if (collected.length === 0) {
errors.push({ code: 'ARG_MISSING_VALUE', message: `Flag ${tok} requires at least one value` });
} else {
flags[tok] = collected;
}
continue;
}
unknown.push(tok);
}
return { command, flags, positional, unknown, errors };
}
function tokenize(s) {
if (typeof s !== 'string') return [];
const trimmed = s.trim();
if (trimmed === '') return [];
const out = [];
const re = /"([^"]*)"|'([^']*)'|(\S+)/g;
let m;
while ((m = re.exec(trimmed)) !== null) {
out.push(m[1] !== undefined ? m[1] : m[2] !== undefined ? m[2] : m[3]);
}
return out;
}
export { FLAG_SCHEMA };