Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Commands
doctor Fire a test payload through the hook to prove it is wired
verify Exit non-zero if a decoy was read or a canary leaked
clean Remove planted decoys and unregister the hook
hook Internal: read a PreToolUse payload on stdin and judge it
hook Internal: read a tool-call hook payload on stdin and judge it
session-start Internal: report outstanding trips at the start of a session

Options
Expand All @@ -36,6 +36,28 @@ Options
Docs: https://github.com/aiagentflow/canaryhook
`;

/** Flags each command accepts beyond --json, -h and -v. */
const FLAGS: Record<string, string[]> = {
init: ["--commit", "--force"],
clean: ["--keep-alerts"],
status: [],
alerts: [],
doctor: [],
verify: [],
};

/**
* A mistyped flag would otherwise be ignored, so `init --froce` reports
* "already initialized" and `clean --keep-alert` deletes the record it meant
* to keep. The internal hook commands are left out: a hook must never fail.
*/
function unknownArg(command: string, args: string[]): string | null {
const allowed = FLAGS[command];
if (allowed === undefined) return null;
const known = new Set([...allowed, "--json", "-h", "--help", "-v", "--version"]);
return args.find((arg) => (arg.startsWith("-") ? !known.has(arg) : arg !== command)) ?? null;
}

async function main(argv: string[]): Promise<number> {
const args = argv.slice(2);
const has = (flag: string) => args.includes(flag);
Expand All @@ -54,6 +76,12 @@ async function main(argv: string[]): Promise<number> {
return 0;
}

const unknown = unknownArg(command, args);
if (unknown !== null) {
process.stderr.write(`Unknown option for ${command}: ${unknown}\n\n${HELP}`);
return 1;
}

switch (command) {
case "hook":
return runHook();
Expand Down
9 changes: 9 additions & 0 deletions test/root.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,12 @@ test("verify passes a fresh install with no trips", () => {
const { root } = project();
assert.equal(run(["verify"], root).status, 0);
});

test("rejects a mistyped flag instead of ignoring it", () => {
const { root } = project();
const result = run(["clean", "--keep-alert"], root);
assert.equal(result.status, 1);
assert.match(result.stderr, /Unknown option for clean: --keep-alert/);
assert.equal(run(["status", "extra"], root).status, 1);
assert.equal(run(["status", "--json"], root).status, 0);
});
Loading