Files
portal/tools/router-tool-gate.test.mjs
T
Дмитрий 55123bfe9f feat(router): §17 mode-based gate, continuation NOT exempt (phase 2 task 13)
Spec §4.4 — shouldBlock rewritten on mode='off'|'warn-only'|'enforce'. Old
boolean warnOnly API kept as legacy fallback. Continuation deliberately NOT
in the §17 exempt set (D1) — an inherited 'feature' classification still
triggers the gate.

- tools/router-tool-gate.mjs:
  + NON_BLOCKING_TASK_TYPES = ['conversation','micro','manual_override']
  + shouldBlock returns false OR { block: true, reason } with reason ∈
    {'no_skill_found_block','direct_in_non_conversation'}.
  + Reads state.classification.task_type (v4 snake_case) with fallback to
    legacy taskType — backward-compatible until Task 14 updates prehook.
  + resolveMode(): options.mode wins; legacy warnOnly=false maps to enforce.
  + decideDecision returns decision/reason/reason_code on block, warning on
    warn-only with non-exempt classification, empty on proceed/exempt.
  + gateMode() now recognises 'off' alongside warn-only/enforce.
- tools/router-tool-gate.test.mjs: rewritten 25 tests (mode-based) — covers
  §17 exempt set, no_skill_found path, skill invoked, routing-tag escape,
  read-only Bash, tool whitelist, legacy back-compat (warnOnly + taskType),
  decideDecision reason_code + warn-only warning suppression on exempt tasks.

Tests: 25/25 PASS.
2026-05-25 14:28:25 +03:00

164 lines
6.2 KiB
JavaScript

import { describe, it, expect } from 'vitest';
import {
shouldBlock,
decodeRoutingTag,
isReadOnlyBash,
decideDecision,
} from './router-tool-gate.mjs';
const baseState = {
skillInvokedThisTurn: false,
classification: {
task_type: 'feature',
no_skill_found: false,
recommendedNode: '#19',
recommendedChain: 'L1',
},
chainProgress: [],
};
describe('isReadOnlyBash', () => {
it('detects ls / cat / grep / git status as read-only', () => {
expect(isReadOnlyBash('ls -la')).toBe(true);
expect(isReadOnlyBash('cat file.txt')).toBe(true);
expect(isReadOnlyBash('grep "x" file')).toBe(true);
expect(isReadOnlyBash('git status')).toBe(true);
expect(isReadOnlyBash('git log')).toBe(true);
expect(isReadOnlyBash('git rev-parse HEAD')).toBe(true);
});
it('does not classify git commit / push as read-only', () => {
expect(isReadOnlyBash('git commit -m "x"')).toBe(false);
expect(isReadOnlyBash('git push origin main')).toBe(false);
});
it('does not classify rm / cp / mv as read-only', () => {
expect(isReadOnlyBash('rm file')).toBe(false);
expect(isReadOnlyBash('cp a b')).toBe(false);
});
});
describe('decodeRoutingTag', () => {
it('parses direct_justified=true with reason', () => {
const r = decodeRoutingTag('<!-- routing: direct_justified=true reason="micro fix per user override" -->');
expect(r.directJustified).toBe(true);
expect(r.reason).toContain('micro fix');
});
it('returns null on missing tag', () => {
expect(decodeRoutingTag('just a regular response')).toBeNull();
});
it('rejects direct_justified=true WITHOUT reason', () => {
const r = decodeRoutingTag('<!-- routing: direct_justified=true -->');
expect(r).toBeNull();
});
});
describe('shouldBlock — §17 mode-based (Phase 2 Task 13)', () => {
it('mode=off never blocks', () => {
expect(shouldBlock('Edit', baseState, '', { mode: 'off' })).toBe(false);
});
it('warn-only never blocks (always returns false)', () => {
expect(shouldBlock('Edit', baseState, '', { mode: 'warn-only' })).toBe(false);
});
it('enforce blocks Edit on feature without skill invoked', () => {
expect(shouldBlock('Edit', baseState, '', { mode: 'enforce' })).toMatchObject({
block: true,
reason: 'direct_in_non_conversation',
});
});
it('enforce passes conversation task_type (§17 exempt)', () => {
const s = { ...baseState, classification: { task_type: 'conversation', no_skill_found: false } };
expect(shouldBlock('Edit', s, '', { mode: 'enforce' })).toBe(false);
});
it('enforce passes micro / manual_override (§17 exempt)', () => {
for (const t of ['micro', 'manual_override']) {
const s = { ...baseState, classification: { task_type: t, no_skill_found: false } };
expect(shouldBlock('Edit', s, '', { mode: 'enforce' })).toBe(false);
}
});
it('enforce does NOT block when skill invoked this turn', () => {
const s = { ...baseState, skillInvokedThisTurn: true };
expect(shouldBlock('Edit', s, '', { mode: 'enforce' })).toBe(false);
});
it('enforce blocks no_skill_found=true with specific reason', () => {
const s = { ...baseState, classification: { task_type: 'feature', no_skill_found: true } };
expect(shouldBlock('Edit', s, '', { mode: 'enforce' })).toMatchObject({
block: true,
reason: 'no_skill_found_block',
});
});
it('continuation-inherited feature is NOT exempt (D1 — same shape as base)', () => {
expect(shouldBlock('Edit', baseState, '', { mode: 'enforce' })).toMatchObject({ block: true });
});
it('enforce does NOT block when routing-tag has direct_justified=true with reason', () => {
expect(shouldBlock('Edit', baseState, '<!-- routing: direct_justified=true reason="testing" -->', { mode: 'enforce' })).toBe(false);
});
it('enforce does NOT block read-only Bash', () => {
expect(shouldBlock('Bash', baseState, '', { mode: 'enforce', bashCommand: 'ls' })).toBe(false);
});
it('enforce does NOT block tools outside whitelist (e.g. Read)', () => {
expect(shouldBlock('Read', baseState, '', { mode: 'enforce' })).toBe(false);
});
it('legacy back-compat: warnOnly=false maps to enforce', () => {
expect(shouldBlock('Edit', baseState, '', { warnOnly: false })).toMatchObject({ block: true });
});
it('legacy back-compat: taskType (camelCase) still recognised', () => {
const s = { ...baseState, classification: { taskType: 'conversation', no_skill_found: false } };
expect(shouldBlock('Edit', s, '', { mode: 'enforce' })).toBe(false);
});
});
describe('decideDecision — §17 mode-based', () => {
it('returns decision: block with reason text and reason_code when shouldBlock blocks', () => {
const r = decideDecision('Edit', baseState, '', { mode: 'enforce' });
expect(r.decision).toBe('block');
expect(r.reason).toMatch(/#19/);
expect(r.reason_code).toBe('direct_in_non_conversation');
});
it('returns no_skill_found_block reason_code when classifier signalled no match', () => {
const s = { ...baseState, classification: { task_type: 'feature', no_skill_found: true, recommendedNode: null } };
const r = decideDecision('Edit', s, '', { mode: 'enforce' });
expect(r.decision).toBe('block');
expect(r.reason_code).toBe('no_skill_found_block');
});
it('returns empty (proceed) when skill invoked', () => {
const r = decideDecision('Edit', { ...baseState, skillInvokedThisTurn: true }, '', { mode: 'enforce' });
expect(r.decision).toBeUndefined();
});
it('warn-only mode emits warning string but does not block', () => {
const r = decideDecision('Edit', baseState, '', { mode: 'warn-only' });
expect(r.decision).toBeUndefined();
expect(r.warning).toMatch(/#19/);
});
it('warn-only mode does NOT emit warning when task is exempt (conversation)', () => {
const s = { ...baseState, classification: { task_type: 'conversation', no_skill_found: false } };
const r = decideDecision('Edit', s, '', { mode: 'warn-only' });
expect(r.warning).toBeUndefined();
});
});
describe('UTF-8 cyrillic stdin (regression — Stage 3 fix 1)', () => {
it('module loads with UTF-8 helper wired (smoke)', async () => {
const mod = await import('./router-tool-gate.mjs');
expect(typeof mod.shouldBlock).toBe('function');
});
});