Files
portal/tools/status-md-generator.test.mjs
T
Дмитрий 675b7f2237 Merge branch 'fix/enforce-9-holes' into main
Brain-retro #5 candidate C — closes 7 of 9 enforce bypasses, defers 2.
+ enforce mode flipped from warn-only to enforce in runtime.

Hole fixes:
  1. Remove self-override via assistant text (ce02d1ad)
  2. Task/Agent in MUTATING_TOOLS (7e5c2973)
  5. Tighten nodeMatches to exact/segment match (a846eed9)
  4. Triggers_matched fallback when classifier silent (56829266)
  8. Override-usage monitor in STATUS.md + new module (08e2a969)
  9. Rationalization-audit blocks on 3rd flag + expanded vocab (0ea3b5d7)
  7. ремонт инфраструктуры requires justification line (57a7f55b)

Deferred (architectural):
  3. Confidence threshold (separate spec)
  6. Stop-event post-mutation timing (separate spec)

152 enforce-* tests GREEN.

# Conflicts:
#	docs/observer/STATUS.md
#	tools/status-md-generator.mjs
2026-05-26 11:48:16 +03:00

392 lines
17 KiB
JavaScript

import { describe, it, expect } from 'vitest';
import { renderStatus, computeCostBlock, computeAnomalyBlock, computeSelfRetrospectBlock, computeReviewerBlock, computeSessionLengthBlock } from './status-md-generator.mjs';
const baseInputs = (overrides = {}) => ({
now: '2026-05-19T10:00:00+03:00',
c1: { status: 'ok', detail: 'no drift' },
c2: { status: 'ok', detail: '0 version drift' },
c3: { status: 'ok', detail: 'last read today' },
c5: { status: 'ok', detail: 'coverage OK · registration OK' },
c6: { status: 'ok', detail: '14 chains in sync' },
observer: { episodeCount: 12, observerErrors: 0, piiMatches: 0 },
missed: { totalMissed: 0, byNode: {}, byClassification: {} },
...overrides,
});
describe('renderStatus', () => {
it('renders all 5 controllers + metrics', () => {
const md = renderStatus(baseInputs());
expect(md).toContain('# Brain Status');
expect(md).toContain('| C1 L1-watcher | ✅');
expect(md).toContain('| C2 Cross-ref consistency | ✅');
expect(md).toContain('| C3 Observer-of-observer | ✅');
expect(md).toContain('| C4 Сигнальный статус | ✅');
expect(md).toContain('| C5 Observer-coverage | ✅');
expect(md).toContain('12 episodes');
});
it('includes a C6 chain-map row', () => {
const md = renderStatus(baseInputs());
expect(md).toContain('| C6 Chain map sync | ✅');
});
it('shows a warn status for the coverage controller', () => {
const md = renderStatus(baseInputs({ c5: { status: 'warn', detail: '3 commits, 0 episodes' } }));
expect(md).toContain('| C5 Observer-coverage | ⚠️');
});
it('shows the observer_error count in the metrics block', () => {
const md = renderStatus(baseInputs({ observer: { episodeCount: 4, observerErrors: 2, piiMatches: 0 } }));
expect(md).toContain('2 observer_error markers');
});
it('shows a red status for failing controllers', () => {
const md = renderStatus(baseInputs({ c1: { status: 'fail', detail: '2 plugins not formalized' } }));
expect(md).toContain('| C1 L1-watcher | 🔴');
});
it('mentions the conditional capability-readiness behavioral rule (§16.4 v1.36)', () => {
const md = renderStatus(baseInputs());
expect(md).toContain('Неиспользованные узлы — не алерт');
expect(md).toContain('если профильной задачи не было');
expect(md).toContain('feedback_brain_unused_tools_not_problem');
});
it('shows piiMatches > 0 when counter file has data (Task 3)', () => {
const md = renderStatus(baseInputs({ observer: { episodeCount: 24, observerErrors: 0, piiMatches: 7 } }));
expect(md).toMatch(/7 PII matches before filter/);
});
});
describe('renderStatus — last /brain-retro (Task 10)', () => {
it('shows last /brain-retro days-ago when counter has data', () => {
const md = renderStatus(baseInputs({ lastRetroDaysAgo: 3 }));
expect(md).toMatch(/Last \/brain-retro:\s*3 day\(s\) ago/);
});
it('shows "never" when lastRetroDaysAgo is null', () => {
const md = renderStatus(baseInputs({ lastRetroDaysAgo: null }));
expect(md).toMatch(/Last \/brain-retro:\s*never/);
});
it('shows "never" when lastRetroDaysAgo is undefined', () => {
const md = renderStatus(baseInputs());
expect(md).toMatch(/Last \/brain-retro:\s*never/);
});
});
describe('renderStatus — v1 episodes count surface (Task 18)', () => {
it('shows v1 count when present', () => {
const md = renderStatus(baseInputs({ observer: { episodeCount: 22, observerErrors: 0, piiMatches: 0, v1Episodes: 5 } }));
expect(md).toMatch(/Legacy v1 episodes \(not in factor analysis\):\s*5/);
});
it('shows 0 when v1Episodes undefined', () => {
const md = renderStatus(baseInputs());
expect(md).toMatch(/Legacy v1 episodes \(not in factor analysis\):\s*0/);
});
});
describe('renderStatus — missed activations (Task 7, Pravila §16.4 v1.36)', () => {
it('renders missed_activations: 0 when there are no misses', () => {
const md = renderStatus(baseInputs());
expect(md).toContain('missed_activations: 0');
});
it('renders missed_activations: N when misses occur', () => {
const md = renderStatus(baseInputs({
missed: { totalMissed: 3, byNode: { '#11': 2, '#12': 1 }, byClassification: { refactor: 3 } },
}));
expect(md).toContain('missed_activations: 3');
});
it('keeps C5 ✅ when controller is ok and no misses', () => {
const md = renderStatus(baseInputs());
expect(md).toContain('| C5 Observer-coverage | ✅');
});
it('honors the c5 status override (warn) regardless of missed count', () => {
const md = renderStatus(baseInputs({
c5: { status: 'warn', detail: '16 missed activation(s)' },
}));
expect(md).toContain('| C5 Observer-coverage | ⚠️');
});
});
describe('renderStatus — discipline block (stage 2)', () => {
const baseInputs = {
now: '2026-05-24T10:00:00Z',
c1: { status: 'ok', detail: 'OK' },
c2: { status: 'ok', detail: 'OK' },
c3: { status: 'ok', detail: 'OK' },
c5: { status: 'ok', detail: 'OK' },
c6: { status: 'ok', detail: 'OK' },
observer: { episodeCount: 10, observerErrors: 0, piiMatches: 0, v1Episodes: 0 },
missed: { totalMissed: 0, byNode: {}, byClassification: {} },
lastRetroDaysAgo: 0,
};
it('renders discipline table when discipline data is provided', () => {
const md = renderStatus({
...baseInputs,
discipline: {
byClassification: {
feature: { episodes: 5, withTriggerMatch: 0, viaSkill: 0, pctTriggerMatch: 0, pctViaSkill: 0 },
bugfix: { episodes: 6, withTriggerMatch: 2, viaSkill: 2, pctTriggerMatch: 0.333, pctViaSkill: 0.333 },
},
routerStep: { distribution: { '1': 10, '3': 1 }, total: 11, suspicious: true },
boundariesRate: { total: 11, withBoundaries: 3, rate: 0.273, byPathType: {} },
},
});
expect(md).toMatch(/## Метрики дисциплины/);
expect(md).toMatch(/feature/);
expect(md).toMatch(/bugfix/);
expect(md).toMatch(/33\.3%/);
expect(md).toMatch(/router step distribution/i);
expect(md).toMatch(/⚠️.*suspicious/i);
expect(md).toMatch(/boundaries applied/i);
expect(md).toMatch(/27\.3%/);
});
it('omits the discipline block when discipline is absent (backward compat)', () => {
const md = renderStatus(baseInputs);
expect(md).not.toMatch(/## Метрики дисциплины/);
});
it('coexists: both sessionLengthBlock (brain-retro candidate B) and overrideUsageBlock (enforce hole 8) appear together in template after merge', () => {
const md = renderStatus({
...baseInputs,
sessionLengthBlock: '## Длинные сессии\n\nflagged content',
overrideUsageBlock: '## Использование override-фраз\n\nflagged content',
});
expect(md).toContain('## Длинные сессии');
expect(md).toContain('## Использование override-фраз');
});
});
// ── Phase 3 deferred #3: 4 new helper blocks ─────────────────────────────────
const PRICING_TEST = {
sonnet46: { input_per_mtok: 3.0, output_per_mtok: 15.0 },
opus47: { input_per_mtok: 15.0, output_per_mtok: 75.0 },
};
const makeEp = (overrides = {}) => ({
schema_version: 2,
task_cost: {
classifier_input_tokens: 0,
classifier_output_tokens: 0,
self_assessment_input_tokens: 0,
self_assessment_output_tokens: 0,
reviewer_subagent_usd: null,
reviewer_input_tokens: 0,
reviewer_output_tokens: 0,
reviewer_direct_fallback_usd: null,
},
review: { reviewed_at: null, reviewer: null, reviewer_error: false },
...overrides,
});
describe('computeCostBlock', () => {
it('sums token costs and formats USD for 3 episodes', () => {
const eps = [
makeEp({ task_cost: { classifier_input_tokens: 1000, classifier_output_tokens: 200, self_assessment_input_tokens: 500, self_assessment_output_tokens: 100, reviewer_subagent_usd: null, reviewer_input_tokens: 0, reviewer_output_tokens: 0, reviewer_direct_fallback_usd: null } }),
makeEp({ task_cost: { classifier_input_tokens: 2000, classifier_output_tokens: 400, self_assessment_input_tokens: 1000, self_assessment_output_tokens: 200, reviewer_subagent_usd: 0.01, reviewer_input_tokens: 500, reviewer_output_tokens: 100, reviewer_direct_fallback_usd: null } }),
makeEp({ task_cost: { classifier_input_tokens: 500, classifier_output_tokens: 100, self_assessment_input_tokens: 250, self_assessment_output_tokens: 50, reviewer_subagent_usd: null, reviewer_input_tokens: 0, reviewer_output_tokens: 0, reviewer_direct_fallback_usd: 0.005 } }),
];
const block = computeCostBlock(eps, PRICING_TEST);
expect(block).toMatch(/## Стоимость месяца/);
expect(block).toMatch(/Classifier/);
expect(block).toMatch(/Self-assessment/);
expect(block).toMatch(/Reviewer/);
expect(block).toMatch(/\$\d+\.\d{2}/);
});
it('returns a block with zeros when episodes array is empty', () => {
const block = computeCostBlock([], PRICING_TEST);
expect(block).toMatch(/## Стоимость месяца/);
expect(block).toMatch(/\$0\.00/);
});
});
describe('computeAnomalyBlock', () => {
it('returns "Аномалий нет." when all outputs are within threshold', () => {
const eps = [
makeEp({ task_cost: { ...makeEp().task_cost, classifier_output_tokens: 100 } }),
makeEp({ task_cost: { ...makeEp().task_cost, classifier_output_tokens: 120 } }),
makeEp({ task_cost: { ...makeEp().task_cost, classifier_output_tokens: 110 } }),
];
const block = computeAnomalyBlock(eps);
expect(block).toMatch(/## Аномалии классификатора/);
expect(block).toMatch(/Аномалий нет\./);
});
it('lists the outlier episode when one exceeds threshold', () => {
const eps = [
makeEp({ task_cost: { ...makeEp().task_cost, classifier_output_tokens: 100 } }),
makeEp({ task_cost: { ...makeEp().task_cost, classifier_output_tokens: 100 } }),
makeEp({ task_cost: { ...makeEp().task_cost, classifier_output_tokens: 100 } }),
makeEp({ task_cost: { ...makeEp().task_cost, classifier_output_tokens: 50000 } }),
];
const block = computeAnomalyBlock(eps);
expect(block).toMatch(/## Аномалии классификатора/);
expect(block).toMatch(/50000/);
});
});
describe('computeSelfRetrospectBlock', () => {
it('returns block with days-ago and no warning when under threshold', () => {
const fakeFs = {
existsSync: () => true,
readFileSync: () => JSON.stringify({ last_run_at: new Date(Date.now() - 2 * 86400000).toISOString(), episodes_since_last: 3, threshold: 10 }),
};
const block = computeSelfRetrospectBlock('fake/path.json', fakeFs);
expect(block).toMatch(/## Авто-ретроспектива/);
expect(block).toMatch(/2 day\(s\) ago/);
expect(block).not.toMatch(/⚠️/);
});
it('adds warning when episodes_since_last >= threshold', () => {
const fakeFs = {
existsSync: () => true,
readFileSync: () => JSON.stringify({ last_run_at: new Date(Date.now() - 5 * 86400000).toISOString(), episodes_since_last: 15, threshold: 10 }),
};
const block = computeSelfRetrospectBlock('fake/path.json', fakeFs);
expect(block).toMatch(/⚠️/);
expect(block).toMatch(/15/);
});
it('returns "never" when counter file is missing', () => {
const fakeFs = { existsSync: () => false };
const block = computeSelfRetrospectBlock('fake/path.json', fakeFs);
expect(block).toMatch(/## Авто-ретроспектива/);
expect(block).toMatch(/never/);
});
});
describe('computeReviewerBlock', () => {
it('shows subagent and fallback counts with percentages', () => {
const eps = [
makeEp({ review: { reviewed_at: '2026-05-01T00:00:00Z', reviewer: 'subagent-opus-4-7', reviewer_error: false } }),
makeEp({ review: { reviewed_at: '2026-05-02T00:00:00Z', reviewer: 'subagent-opus-4-7', reviewer_error: false } }),
makeEp({ review: { reviewed_at: '2026-05-03T00:00:00Z', reviewer: 'direct-opus-fallback', reviewer_error: false } }),
makeEp({ review: { reviewed_at: null, reviewer: null, reviewer_error: false } }),
];
const block = computeReviewerBlock(eps);
expect(block).toMatch(/## Reviewer: субагент vs fallback/);
expect(block).toMatch(/subagent-opus-4-7/);
expect(block).toMatch(/direct-opus-fallback/);
expect(block).toMatch(/\d+%/);
});
it('shows fallback message when no episodes were reviewed', () => {
const eps = [
makeEp(),
makeEp(),
];
const block = computeReviewerBlock(eps);
expect(block).toMatch(/## Reviewer: субагент vs fallback/);
expect(block).toMatch(/0 эпизодов проверено/);
});
});
describe('renderStatus — 4 new optional blocks integration', () => {
const minInputs = {
now: '2026-05-25T10:00:00Z',
c1: { status: 'ok', detail: 'OK' },
c2: { status: 'ok', detail: 'OK' },
c3: { status: 'ok', detail: 'OK' },
c5: { status: 'ok', detail: 'OK' },
c6: { status: 'ok', detail: 'OK' },
observer: { episodeCount: 5, observerErrors: 0, piiMatches: 0, v1Episodes: 0 },
missed: { totalMissed: 0, byNode: {}, byClassification: {} },
};
it('renders all 4 blocks when provided as strings', () => {
const md = renderStatus({
...minInputs,
costBlock: '## Стоимость месяца\ncost content',
anomalyBlock: '## Аномалии классификатора\nanomaly content',
selfRetrospectBlock: '## Авто-ретроспектива\nretro content',
reviewerBlock: '## Reviewer: субагент vs fallback\nreviewer content',
});
expect(md).toContain('## Стоимость месяца');
expect(md).toContain('## Аномалии классификатора');
expect(md).toContain('## Авто-ретроспектива');
expect(md).toContain('## Reviewer: субагент vs fallback');
expect(md).toContain('## Алерт-индикаторы');
});
it('omits all 4 blocks when absent (backward compat)', () => {
const md = renderStatus(minInputs);
expect(md).not.toContain('## Стоимость месяца');
expect(md).not.toContain('## Аномалии классификатора');
expect(md).not.toContain('## Авто-ретроспектива');
expect(md).not.toContain('## Reviewer: субагент vs fallback');
});
});
// -----------------------------------------------------------------------------
// computeSessionLengthBlock — brain-retro #5 candidate B (2026-05-26)
// Long sessions correlate with discipline drift; surface a warning when any
// session today (UTC) has ≥50 turns.
// -----------------------------------------------------------------------------
describe('computeSessionLengthBlock', () => {
const day = '2026-05-26';
const ep = (turn, opts = {}) => ({
task_id: opts.id ?? 'sess-1',
timestamps: { started_at: `${opts.day ?? day}T01:00:0${turn % 10}Z`, ended_at: `${opts.day ?? day}T01:00:0${turn % 10}Z` },
environment: { session_turn: turn },
path_type: opts.regulated ? 'regulated' : 'improvised',
});
it('returns "no data" placeholder when episodes empty', () => {
expect(computeSessionLengthBlock([])).toContain('(нет данных)');
});
it('returns OK (✅) when no session reaches threshold', () => {
const out = computeSessionLengthBlock([ep(1), ep(2), ep(10)], { now: `${day}T05:00:00Z` });
expect(out).toContain('✅');
expect(out).toContain('Ни одной сессии');
});
it('flags a session that crossed threshold', () => {
const eps = Array.from({ length: 55 }, (_, i) => ep(i + 1));
const out = computeSessionLengthBlock(eps, { now: `${day}T05:00:00Z` });
expect(out).toContain('⚠️');
expect(out).toContain('`sess-1');
expect(out).toContain('55'); // max turn
});
it('respects custom threshold', () => {
const eps = Array.from({ length: 15 }, (_, i) => ep(i + 1));
const flagged = computeSessionLengthBlock(eps, { now: `${day}T05:00:00Z`, threshold: 10 });
const notFlagged = computeSessionLengthBlock(eps, { now: `${day}T05:00:00Z`, threshold: 20 });
expect(flagged).toContain('⚠️');
expect(notFlagged).toContain('✅');
});
it('ignores episodes from other UTC days', () => {
const eps = Array.from({ length: 55 }, (_, i) => ep(i + 1, { day: '2026-05-25' }));
const out = computeSessionLengthBlock(eps, { now: `${day}T05:00:00Z` });
expect(out).toContain('✅'); // yesterday's session not counted
});
it('computes regulated % per long session', () => {
const eps = Array.from({ length: 50 }, (_, i) => ep(i + 1, { regulated: i < 10 }));
const out = computeSessionLengthBlock(eps, { now: `${day}T05:00:00Z`, threshold: 40 });
expect(out).toContain('⚠️');
expect(out).toContain('20%'); // 10 regulated out of 50 = 20%
});
it('handles missing session_turn / task_id gracefully', () => {
const eps = [
{ task_id: 'x', timestamps: { started_at: `${day}T01:00:00Z` } }, // no session_turn
{ timestamps: { started_at: `${day}T01:00:00Z` }, environment: { session_turn: 60 } }, // no task_id
ep(70, { id: 'real' }),
];
const out = computeSessionLengthBlock(eps, { now: `${day}T05:00:00Z` });
expect(out).toContain('⚠️');
expect(out).toContain('`real');
expect(out).toContain('70');
});
});