The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix(context): respect indirect skill citations in auto routing
Yann Robertof302863d5804
diff --git a/scripts/lib/context-selection.js b/scripts/lib/context-selection.jsindex 85a496800..dba849cc7 100644--- a/scripts/lib/context-selection.js+++ b/scripts/lib/context-selection.js@@ -28,9 +28,48 @@ const FALLBACK_MIN_TERMS = 2;const FALLBACK_MARGIN = 1.1;// v4: an explicit empty proposal (decline) is honored; the tier-2 fallback no// longer overrides declines at the launch/selection call sites.-const ROUTING_POLICY_VERSION = 4;+// v5: indirect name citations cannot bypass review through exact or scored admission.+const ROUTING_POLICY_VERSION = 5;const TASK_KEYS = new Set(['sessionId', 'taskId', 'revision', 'phase', 'query', 'explicitIds', 'proposedIds', 'noWorkflow']);+const DIRECTIVE_VERB = /\b(use|apply|invoke|run|follow|load)\s+(the\s+)?/i;+const normalizedQueryName = text => text.replace(/n['\u2019]t\b/gi, ' not')+ .replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/_/g, ' ')+ .toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim();++// Classify each named skill locally: a rejected/quoted X must not suppress an+// affirmative Y in another clause. Ambiguous references stay available for the+// bounded proposal, but cannot admit themselves via either retrieval threshold.+function citationFor(candidate, query) {+ if (!candidate.exact) return 'none';+ const aliases = [...new Set([candidate.exactAlias, candidate.id.slice('skill:'.length)]+ .filter(Boolean).map(normalizedQueryName))];+ const quotedParts = query.match(/"[^"]*"|\u201c[^\u201d]*\u201d|(?:^|[\s(:])'[^']*'|\u2018[^\u2019]*\u2019|\x60[^\x60]*\x60/g) || [];+ let directive = false;+ let indirect = false;+ for (const clause of query.match(/[^.!?;\n]+[.!?;\n]*/g) || []) {+ const text = normalizedQueryName(clause);+ for (const name of aliases) {+ const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');+ const directivePattern = DIRECTIVE_VERB.source + '(skill\\s*:?\\s*)?' + escaped + '\\b';+ if (quotedParts.some(part => new RegExp(directivePattern, 'i').test(normalizedQueryName(part)))) indirect = true;+ for (const mention of text.matchAll(new RegExp('\\b' + escaped + '\\b', 'gi'))) {+ const prefix = text.slice(0, mention.index);+ const polite = /^(?:(?:please )?(?:can|could|would|will) you (?:please )?|do )(?:use|apply|invoke|run|follow|load)\b/.test(text);+ const question = !polite && (clause.includes('?')+ || /^(can|could|would|should|may|might|do|does|did|is|are|why|how|what|when|where|which)\b/.test(text));+ const quoted = /^\s*["'\u201c\u2018\x60]/.test(clause);+ if (question || quoted || /\b(do not|never|no|not|avoid|says|said|reads|told|mentions?|quoted?|document)\b/.test(prefix)) {+ indirect = true;+ } else if (new RegExp(DIRECTIVE_VERB.source + '(skill\\s*:?\\s*)?$', 'i').test(prefix)) {+ directive = true;+ }+ }+ }+ }+ return indirect ? 'indirect' : directive ? 'directive' : 'none';+}+function validateTask(task) {if (!task || typeof task !== 'object' || Array.isArray(task)) throw new Error('Task must be an object');for (const key of Object.keys(task)) if (!TASK_KEYS.has(key)) throw new Error(`Unknown task field: ${key}`);@@ -179,28 +218,8 @@ function resolveTaskContext({ repoRoot = DEFAULT_REPO_ROOT, task, profileId = 'l// questions, negations, reported speech, multiple cited names — never admit// implicitly. Everything else keeps the bounded-proposal path so the// primary agent decides ambiguous cases during work it was already doing.- const DIRECTIVE_VERB = /\b(use|apply|invoke|run|follow|load)\s+(the\s+)?/i;- const normalizedQueryName = text => text.replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/_/g, ' ')- .toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim();- const directiveCitation = candidate => {- if (!candidate || !candidate.exact) return false;- const text = normalizedQueryName(task.query || '');- const aliases = [...new Set([candidate.exactAlias,- candidate.id.slice('skill:'.length).toLowerCase(),- candidate.id.slice('skill:'.length).toLowerCase().replace(/-/g, ' ')].filter(Boolean))];- for (const name of aliases) {- const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');- const pattern = new RegExp(`${DIRECTIVE_VERB.source}(skill\\s*:?\\s*)?${escaped}(\\s+(skill|workflow|guidance))?\\b`, 'i');- const match = pattern.exec(text);- if (!match) continue;- const window = text.slice(Math.max(0, match.index - 28), match.index);- if (/\b(do not|don't|never|no)\b/.test(window)) return false;- if (/\b(says|said|reads|told|document)\b/i.test(task.query || '')) return false;- return true;- }- return false;- };- const exactAnchors = candidates.filter(directiveCitation);+ const citations = new Map(candidates.map(candidate => [candidate.id, citationFor(candidate, task.query || '')]));+ const exactAnchors = candidates.filter(candidate => citations.get(candidate.id) === 'directive');let autoSelection = null;if (!task.noWorkflow && selectionMode === 'auto' && !reused && !explicitIds.length && !proposedIds.length && candidates.length) {if (exactAnchors.length === 1) {@@ -209,7 +228,7 @@ function resolveTaskContext({ repoRoot = DEFAULT_REPO_ROOT, task, profileId = 'l} else if (!exactAnchors.length) {const top = candidates[0];const second = candidates[1];- if (top.bm25 >= AUTO_ADMIT_MIN_BM25 && top.matchedTerms.length >= AUTO_ADMIT_MIN_TERMS+ if (citations.get(top.id) !== 'indirect' && top.bm25 >= AUTO_ADMIT_MIN_BM25 && top.matchedTerms.length >= AUTO_ADMIT_MIN_TERMS&& (!second || top.bm25 >= AUTO_ADMIT_MARGIN * (second.bm25 || 0))) {autoSelection = { id: top.id, bm25: top.bm25, matchedTerms: top.matchedTerms.length, exact: false };}@@ -220,7 +239,7 @@ function resolveTaskContext({ repoRoot = DEFAULT_REPO_ROOT, task, profileId = 'l&& !explicitIds.length && !proposedIds.length && candidates.length && !exactAnchors.length) {const top = candidates[0];const second = candidates[1];- if (top.bm25 >= FALLBACK_MIN_BM25 && top.matchedTerms.length >= FALLBACK_MIN_TERMS+ if (citations.get(top.id) !== 'indirect' && top.bm25 >= FALLBACK_MIN_BM25 && top.matchedTerms.length >= FALLBACK_MIN_TERMS&& (!second || top.bm25 >= FALLBACK_MARGIN * (second.bm25 || 0))) {fallback = { id: top.id, bm25: top.bm25, matchedTerms: top.matchedTerms.length };}diff --git a/tests/lib/context-profile-launch.test.js b/tests/lib/context-profile-launch.test.jsindex 59d03ecdc..b87c72836 100644--- a/tests/lib/context-profile-launch.test.js+++ b/tests/lib/context-profile-launch.test.js@@ -155,3 +155,21 @@ test('spawn failures and timeout signals remain unsuccessful without a native exassert.equal(result.taskSuccess, 'unverified');}}));++for (const query of ["Don't use the feature skill.", 'Don\u2019t use the feature skill.',+ 'Can I use the feature skill?', 'The README says use the feature skill.']) {+ test('indirect citation uses the proposal path and honors decline: ' + query, () => withFixture(repoRoot => {+ const phases = [];+ const result = launchTaskContext({ repoRoot, task: { ...input, query, explicitIds: [] },+ execute(command, args, options) {+ phases.push(options.phase);+ if (options.phase === 'selection') return { status: 0, stdout: '{"selectedIds":[]}' };+ assert.doesNotMatch(options.input, /# feature/);+ return { status: 0, stdout: 'ok' };+ } });+ assert.deepEqual(phases, ['selection', 'task']);+ assert.equal(result.routingCalls, 1);+ assert.deepEqual(result.selection.loadedIds, []);+ assert.equal(result.selection.reason, 'agent-declined-selection');+ }));+}diff --git a/tests/lib/context-selection.test.js b/tests/lib/context-selection.test.jsindex 4e3be6169..f0b94ecf2 100644--- a/tests/lib/context-selection.test.js+++ b/tests/lib/context-selection.test.js@@ -301,3 +301,84 @@ test('invalid input and oversized bodies fail closed', () => withFixture(repoRoooverrides: [{ id: 'skill:feature', requiredResources: ['skills/feature/references/details.md'] }] }));assert.throws(() => resolve(repoRoot, { explicitIds: ['skill:feature'] }, { load: true }), /budget/);}));++for (const query of [+ "You shouldn't use feature.",+ "You can\u2019t use feature.",+ "For feature, don't use the feature skill.",+ 'For feature, never invoke feature.',+ 'The phrase "Use feature" is an example.',+ 'The phrase "Use feature." is an example.',+ 'The phrase \u201cUse feature\u201d is an example.',+ 'The phrase `Use feature` is an example.',+ "Don't use the feature skill.",+ 'Don\u2019t use the feature skill.',+ 'Do not use the feature skill.',+ 'Never use the feature skill.',+ 'Can I use the feature skill?',+ 'Should we use the feature skill?',+ 'Why use the feature skill?',+ 'The README says to use the feature skill.',+ '"Use the feature skill" is an example.',+]) {+ test('indirect name citation needs an agent decision: ' + query, () => withFixture(repoRoot => {+ const result = resolve(repoRoot, { query }, { load: true });+ assert.ok(result.candidates.some(candidate => candidate.id === 'skill:feature'));+ assert.deepEqual(result.loadedIds, []);+ assert.equal(result.reason, 'agent-selection-required');+ assert.equal(result.fallback, null);+ }));+}++test('a negated citation does not suppress a separate affirmative directive', () => withFixture(repoRoot => {+ for (const query of [+ "Don't use feature. Use shared guidance.",+ 'Don\u2019t use feature; please use shared guidance.',+ 'The document says use feature. Use shared guidance.',+ ]) {+ const result = resolve(repoRoot, { query }, { load: true });+ assert.deepEqual(result.loadedIds, ['skill:shared']);+ }+}));++test('ordinary and polite directives preserve automatic and explicit selection', () => withFixture(repoRoot => {+ for (const query of ['Please use the feature skill.', 'Can you use the feature skill?',+ 'Use feature without changing the code.', 'Use feature to document the change.',+ 'Use "feature".', 'Use `feature`.', 'Do use feature.', 'Please can you use feature?']) {+ assert.deepEqual(resolve(repoRoot, { query }, { load: true }).loadedIds, ['skill:feature']);+ }+ for (const field of ['explicitIds', 'proposedIds']) {+ assert.deepEqual(resolve(repoRoot, { query: 'Should we use feature?', [field]: ['skill:feature'] },+ { load: true }).loadedIds, ['skill:feature']);+ }+}));++for (const prefix of ["Don't use database-migrations.", 'Do not use database-migrations.',+ 'Should we use database-migrations?', 'The README says use database-migrations.']) {+ test('strong retrieval cannot bypass an indirect citation: ' + prefix, () => {+ const query = prefix + ' Review a PostgreSQL migration that adds an indexed nullable column without downtime.';+ const result = resolveTaskContext({ task: task({ query }), load: true });+ const candidate = result.candidates.find(value => value.id === 'skill:database-migrations');+ assert.ok(candidate && candidate.bm25 >= 20, 'Exercise the BM25 auto-admission threshold');+ assert.deepEqual(result.loadedIds, []);+ assert.equal(result.fallback, null);+ });+}++test('routing policy changes invalidate receipts created by the old citation policy', () => withFixture(repoRoot => {+ const query = "Don't use feature.";+ const first = resolve(repoRoot, { query, explicitIds: ['skill:feature'] });+ const { digestObject } = require('../../scripts/lib/context-profile-support');+ const { compileContextProfile } = require('../../scripts/lib/context-profiles');+ const plan = compileContextProfile({ repoRoot, selectionMode: 'auto' });+ const bindingDigest = digestObject({ sessionId: 'session-1', taskId: 'task-1', revision: 1,+ phase: 'implement', planDigest: plan.planDigest, routingPolicyVersion: 4,+ triggersDigest: digestObject({}), queryDigest: digestObject(query) });+ const { receiptDigest: _receiptDigest, ...receipt } = first.receipt;+ const oldReceipt = { ...receipt, bindingDigest, explicitIds: [],+ selectionDigest: digestObject({ bindingDigest, selectedIds: ['skill:feature'], explicitIds: [] }) };+ const previous = { ...oldReceipt, receiptDigest: digestObject(oldReceipt) };+ const result = resolve(repoRoot, { query }, { previous, load: true });+ assert.equal(result.reused, false);+ assert.deepEqual(result.loadedIds, []);+}));