fix(endsession): parse arguments with the arg-parser so a slug with -h is no help request, and only the operator starts it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-10-23 14:45:25 +02:00
commit 403afe606f
Signed by: ktg
SSH key fingerprint: SHA256:JakMjO6FTBBzN0Bhfj9saOoEjaFxlSdYuZQQpM/lF9Q
5 changed files with 148 additions and 7 deletions

View file

@ -1,7 +1,12 @@
import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { spawnSync } from 'node:child_process';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseArgs } from '../../lib/parsers/arg-parser.mjs';
const PARSER = join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'lib', 'parsers', 'arg-parser.mjs');
test('trekbrief — empty args', () => {
const r = parseArgs('', 'trekbrief');
assert.equal(r.command, 'trekbrief');
@ -155,3 +160,43 @@ test('trekcontinue — .md positional accepted by parser (rejection is command-l
assert.deepEqual(r.positional, ['NEXT-SESSION-PROMPT.local.md']);
assert.deepEqual(r.errors, []);
});
// --- #15 — /trekendsession reads its arguments with the parser ---
// A slug or label holding "-h" is an argument, not a help request: only the
// exact --help flag or a lone -h as the first positional asks for help.
test('[#15] trekendsession — a slug is one positional, with no errors', () => {
const r = parseArgs('2026-05-01-hotfix', 'trekendsession');
assert.deepEqual(r.errors, []);
assert.deepEqual(r.positional, ['2026-05-01-hotfix']);
assert.equal(r.flags['--help'], undefined);
});
test('[#15] trekendsession — a path and a quoted label are two positionals, with no --help', () => {
const r = parseArgs('fix-header "Session 2"', 'trekendsession');
assert.deepEqual(r.errors, []);
assert.deepEqual(r.positional, ['fix-header', 'Session 2']);
assert.equal(r.flags['--help'], undefined);
});
test('[#15] trekendsession — --help is the boolean help flag', () => {
const r = parseArgs('--help', 'trekendsession');
assert.deepEqual(r.errors, []);
assert.equal(r.flags['--help'], true);
assert.deepEqual(r.unknown, []);
});
test('[#15] trekendsession — the CLI with --command trekendsession -- fix-header exits 0', () => {
const r = spawnSync(process.execPath, [PARSER, '--command', 'trekendsession', '--', 'fix-header'], { encoding: 'utf8' });
assert.equal(r.status, 0, `the parser CLI refused trekendsession: ${r.stdout}${r.stderr}`);
const out = JSON.parse(r.stdout);
assert.deepEqual(out.positional, ['fix-header']);
assert.equal(out.flags['--help'], undefined);
});
test('trekendsession — -h stays a positional (no alias resolution)', () => {
const r = parseArgs('-h', 'trekendsession');
assert.deepEqual(r.positional, ['-h']);
assert.deepEqual(r.errors, []);
assert.equal(r.flags['--help'], undefined);
});

View file

@ -1041,3 +1041,74 @@ test('[D67] the End to end section says to record run_seen, also when no app was
assert.match(s, /The verdict is still the `End-to-end:` command\./,
'the End to end section no longer says the End-to-end: command is the verdict');
});
// --- #15 / #67 -----------------------------------------------------------------------
// #15: /trekendsession's Phase 0 printed its usage when $ARGUMENTS merely
// contained "--help" or "-h", so a slug such as "fix-header" asked for help.
// It now runs the arg parser and dispatches only on its exact result.
// #67: "NEVER auto-invoke this command" was a sentence only; the frontmatter
// now carries disable-model-invocation, so the model cannot start it.
const ENDSESSION = 'commands/trekendsession.md';
const ENDSESSION_PARSER_LINE = 'node ${CLAUDE_PLUGIN_ROOT}/lib/parsers/arg-parser.mjs --command trekendsession -- $ARGUMENTS';
/** The Phase 0 parser line run through bash() with `args` in place of $ARGUMENTS. */
function runEndsessionParser(cmd, args) {
return bash(cmd.split('$ARGUMENTS').join(args), ROOT);
}
test('[#15] /trekendsession Phase 0 has no "contains --help or -h" check, and its parser line run on fix-header gives no help', () => {
const phase0 = section(read(ENDSESSION), '## Phase 0 — `--help` handling');
assert.doesNotMatch(flat(phase0), /contains\s+`?--help`?\s+or\s+`?-h`?/i,
'Phase 0 still prints help when $ARGUMENTS merely contains --help or -h');
assert.match(flat(phase0), /`flags\['--help'\] === true`/, 'Phase 0 does not dispatch on the parsed --help flag');
assert.match(flat(phase0), /`positional\[0\] === '-h'`/, 'Phase 0 does not dispatch on a lone -h as the first positional');
const cmd = line(phase0, 'parsers/arg-parser.mjs');
assert.equal(cmd, ENDSESSION_PARSER_LINE, 'the Phase 0 parser line is not the trekendsession parser call on $ARGUMENTS');
const block = phase0.split('\n```bash\n').slice(1).map((b) => b.split('\n```')[0]).find((b) => b.includes(cmd));
assert.ok(block, 'the parser line is not in a runtime ```bash block');
assert.equal(block.split('\n')[0], 'setopt nonomatch 2>/dev/null || true', 'the block does not open with setopt nonomatch');
const plain = runEndsessionParser(cmd, 'fix-header');
assert.equal(plain.status, 0, `the parser line refused fix-header: ${plain.stdout}${plain.stderr}`);
const r = JSON.parse(plain.stdout);
assert.equal(r.flags['--help'], undefined, 'fix-header set the --help flag');
assert.deepEqual(r.positional, ['fix-header']);
assert.notEqual(r.positional[0], '-h');
const help = runEndsessionParser(cmd, '--help');
assert.equal(help.status, 0, help.stdout + help.stderr);
assert.equal(JSON.parse(help.stdout).flags['--help'], true, 'control: --help must set the help flag');
const two = runEndsessionParser(cmd, 'fix-header "Session 2"');
assert.equal(two.status, 0, two.stdout + two.stderr);
assert.deepEqual(JSON.parse(two.stdout).positional, ['fix-header', 'Session 2']);
});
test('[#15] /trekendsession Phase 2 reads the brief path and the label from the parser\'s positional', () => {
const phase2 = flat(section(read(ENDSESSION), '## Phase 2 — Required args check (headless-safe)'));
assert.match(phase2, /`positional\[0\]`/, 'Phase 2 does not read <next-brief-path> from positional[0]');
assert.match(phase2, /`positional\[1\]`/, 'Phase 2 does not read <next-label> from positional[1]');
});
test('[#67] /trekendsession sets disable-model-invocation, and CLAUDE.md names it as the one command that does', () => {
const text = read(ENDSESSION);
const fm = readFrontmatterFields(text);
assert.ok(fm.ok, fm.error);
assert.equal(fm.fields['disable-model-invocation']?.value, 'true',
'commands/trekendsession.md does not set disable-model-invocation: true');
assert.match(flat(text),
/\*\*NEVER auto-invoke this command\.\*\* It is operator-typed only at session-end \(the frontmatter field keeps the model from starting it; the operator types it\)/,
'the hard rule does not say the frontmatter field keeps the model from starting it');
const withField = readdirSync(join(ROOT, 'commands')).filter((f) => f.endsWith('.md'))
.filter((f) => readFrontmatterFields(read(`commands/${f}`)).fields['disable-model-invocation'] !== undefined);
assert.deepEqual(withField, ['trekendsession.md'], 'trekendsession must be the one command with the field');
const trunk = read('CLAUDE.md').split('\n').find((l) => l.startsWith('> **Commands are the trunk**'));
assert.ok(trunk, 'CLAUDE.md has no "Commands are the trunk" note');
assert.doesNotMatch(trunk, /none sets `disable-model-invocation`/, 'CLAUDE.md still says no command or skill sets the field');
assert.ok(trunk.includes('no skill sets `disable-model-invocation`; the one command that does is `/trekendsession`, operator-typed only (Claude Code still lets the operator type it; the model cannot start it)'),
'CLAUDE.md does not name /trekendsession as the one command that sets the field');
});
test('keep: commands/trekreview.md carries no disable-model-invocation field (/trekexecute starts it through the Skill tool)', () => {
const fm = readFrontmatterFields(read('commands/trekreview.md'));
assert.ok(fm.ok, fm.error);
assert.equal(fm.fields['disable-model-invocation'], undefined);
});