The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix(context): preserve skill requests after task preambles
Yann Robertob5a8bf060374
diff --git a/scripts/lib/context-selection.js b/scripts/lib/context-selection.jsindex 402ffcf9d..535376c09 100644--- a/scripts/lib/context-selection.js+++ b/scripts/lib/context-selection.js@@ -32,26 +32,36 @@ const FALLBACK_MARGIN = 1.1;// v6: ordered directives and singular named references share the same admission guard.// v7: independent directives scope negation and imperatives allow question punctuation.// v8: only recognized request/rejection scopes can authorize coordinated directives.-const ROUTING_POLICY_VERSION = 8;+// v9: neutral task preambles do not suppress a later explicit skill request.+const ROUTING_POLICY_VERSION = 9;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 DIRECTIVE_REQUEST = /^(?:please )?(?:(?:can|could|would|will) you (?:please )?|do )?(?:use|apply|invoke|run|follow|load)\b/;const DIRECTIVE_REJECTION = /^(?:please )?(?:(?:(?:do )?not|never) (?:use|apply|invoke|run|follow|load)\b|avoid\b)/;const DIRECTIVE_NAME_PREFIX = new RegExp(DIRECTIVE_REQUEST.source + '\\s+(?:the\\s+)?(?:skill\\s*)?$');-const QUESTION_START = /^(?!do not\b)(can|could|would|should|shall|will|may|might|do|does|did|is|are|why|how|what|when|where|which)\b/;+const QUESTION_START = /^(?!do not\b)(can|could|would|should|shall|will|may|might|must|ought|do|does|did|is|are|why|how|what|when|where|which)\b/;const REPORTED_INSTRUCTION = /\b(say|says|said|reads|told|mentions?|quoted?|document|states?|stated|recommends?|recommended|asserts?|asserted)\b/;+// Subject-led statements may describe someone else's instructions or intentions.+const SUBJECT_PREFIX = /^(?:the|a|an|i|you|he|she|it|we|they|my|your|his|her|its|our|their|this|that|these|those)\b/;// Rejections keep their whole list until another instruction/question begins.-const INSTRUCTION_BOUNDARY = /(?:,\s*(?:(?:and|but)\s+)?|\s+(?:and|but)\s+)(?=(?:please\s+)?(?:do\s+(?:not\s+)?|not\s+|never\s+)?(?:use|apply|invoke|run|follow|load)\b|(?:can|could|would|should|shall|will|may|might|do|does|did|is|are|why|how|what|when|where|which)\b)/i;+const INSTRUCTION_BOUNDARY = /(?:,\s*(?:(?:and|but)\s+)?|\s+(?:and|but)\s+)(?=(?:please\s+)?(?:do\s+(?:not\s+)?|not\s+|never\s+)?(?:use|apply|invoke|run|follow|load)\b|(?:can|could|would|should|shall|will|may|might|must|ought|do|does|did|is|are|why|how|what|when|where|which)\b)/i;// Admission additionally checks intervening scopes, including unknown wording.const ADMISSION_BOUNDARY = /(?:,\s*(?:(?:and|but)\s+)?|\s+(?:and|but)\s+)(?=\S)/i;-/** Normalize citation names while preserving contracted negation as a separate word. */+/** Normalize citation names while preserving contracted negation as a separate word.+ * @param {string} text Raw query text or a registry alias.+ * @returns {string} Lowercase alphanumeric words with negation preserved.+ */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();/** Return canonical/native aliases plus conservative singular forms. Singular- * multiword aliases only guard indirect references; they never create exact admission. */+ * multiword aliases only guard indirect references; they never create exact admission.+ * @param {object} candidate Ranked skill candidate with its canonical ID and exact alias.+ * @param {string} nativeName Registry name that may be absent from exact retrieval.+ * @returns {Array<{name: string, exact: boolean}>} Normalized names and admission eligibility.+ */function citationAliases(candidate, nativeName) {const names = [...new Set([candidate.exactAlias, candidate.id.slice('skill:'.length), nativeName].filter(Boolean).map(normalizedQueryName).filter(Boolean))];@@ -87,10 +97,13 @@ function citationFor(candidate, query, nativeName) {const prefix = text.slice(0, mention.index);const scopes = prefix.split(ADMISSION_BOUNDARY);const instructionPrefix = prefix.split(INSTRUCTION_BOUNDARY).at(-1);- // Unknown scopes cannot authorize a later "and use ...". Positive scope- // evidence is required for admission, while existing rejection forms remain- // usable and indirect discussion cannot withdraw an earlier directive.- const instruction = scopes.every(scope => DIRECTIVE_REQUEST.test(scope) || DIRECTIVE_REJECTION.test(scope));+ // A task preamble such as "fix the bug" does not change a later request.+ // Only preceding scopes may be neutral: an embedded instruction, question,+ // or subject-led statement still needs review. The final scope must remain+ // a direct request; neutral text never supplies admission evidence itself.+ const instruction = scopes.every((scope, index) => DIRECTIVE_REQUEST.test(scope) || DIRECTIVE_REJECTION.test(scope)+ || (index < scopes.length - 1 && !DIRECTIVE_VERB.test(scope)+ && !QUESTION_START.test(scope) && !REPORTED_INSTRUCTION.test(scope) && !SUBJECT_PREFIX.test(scope)));const question = (!DIRECTIVE_REQUEST.test(text) && QUESTION_START.test(text))|| (!DIRECTIVE_REQUEST.test(instructionPrefix)&& (QUESTION_START.test(instructionPrefix) || clause.includes('?')));@@ -213,7 +226,13 @@ function validatePrevious(previous) {}}-/** Pure task-scoped resolver. Returned context never invokes a native skill or changes permissions. */+/** Resolve task-scoped skill context without invoking native skills or changing permissions.+ * @param {object} [options] Repository, task, profile, and selection constraints.+ * @param {object} options.task Task identity, query, and explicit or proposed skill IDs.+ * @param {boolean} [options.load=false] Include verified resource contents in the result.+ * @param {object|null} [options.previous=null] Receipt reusable only when its binding matches.+ * @returns {object} Selection decision, candidates, verified resources, and a bound receipt.+ */function resolveTaskContext({ repoRoot = DEFAULT_REPO_ROOT, task, profileId = 'lean@1', target = 'codex',selectionMode = 'auto', include = [], exclude = [], load = false, previous = null, expectedDigest = null } = {}) {validateTask(task);diff --git a/tests/lib/context-profile-launch.test.js b/tests/lib/context-profile-launch.test.jsindex 7193bf83e..2e77cbd56 100644--- a/tests/lib/context-profile-launch.test.js+++ b/tests/lib/context-profile-launch.test.js@@ -174,6 +174,15 @@ for (const query of ["Don't use the feature skill.", 'Don\u2019t use the feature'Please use the existing code, can I use feature?','Use standard tools and should we use feature?','Use standard tools but should we use feature?',+ 'Fix the bug, shall we use feature?',+ 'Fix the bug, the spec states do not use feature and use shared guidance.',+ 'The docs say fix the bug and use feature.',+ 'Can we fix the bug and use feature?',+ 'The spec stipulates: fix the bug and use feature.',+ 'Our policy stipulates repair the bug and use feature.',+ 'Fix the bug, ought we to repair it and use feature?',+ 'Fix the bug, must we repair it and use feature?',+ 'Fix the bug, feature.','Do not use feature and shared guidance.',"Use feature, don't use feature.","Use feature and don't use feature.",@@ -198,6 +207,13 @@ for (const query of ["Don't use the feature skill.", 'Don\u2019t use the feature}for (const [query, skill] of [+ ['Fix the bug, use feature', 'feature'],+ ['Fix the bug and use feature.', 'feature'],+ ['Please fix the bug, please use feature.', 'feature'],+ ['Write the regression test, use feature.', 'feature'],+ ['Investigate the bug but use feature.', 'feature'],+ ['For this task, use feature.', 'feature'],+ ["Fix the bug, don't use feature and use shared guidance.", 'shared'],["Don't use feature, use shared guidance.", 'shared'],["Don't use feature and use shared guidance.", 'shared'],["Don't use feature but use shared guidance.", 'shared'],diff --git a/tests/lib/context-selection.test.js b/tests/lib/context-selection.test.jsindex ef6e2c4af..7320c1f24 100644--- a/tests/lib/context-selection.test.js+++ b/tests/lib/context-selection.test.js@@ -434,6 +434,45 @@ test('ordinary and polite directives preserve automatic and explicit selection',}}));+for (const [query, expectedId] of [+ ['Fix the bug, use feature', 'skill:feature'],+ ['Fix the bug and use feature.', 'skill:feature'],+ ['Please fix the bug, please use feature.', 'skill:feature'],+ ['Write the regression test, use feature.', 'skill:feature'],+ ['Investigate the bug but use feature.', 'skill:feature'],+ ['For this task, use feature.', 'skill:feature'],+ ["Fix the bug, don't use feature and use shared guidance.", 'skill:shared'],+]) {+ test('a general task preamble preserves a later directive: ' + query, () => withFixture(repoRoot => {+ const result = resolve(repoRoot, { query }, { load: true });+ assert.deepEqual(result.selectedIds, [expectedId]);+ assert.deepEqual(result.loadedIds, [expectedId]);+ assert.equal(result.reason, 'auto-selection');+ assert.equal(result.receipt.autoSelection.exact, true);+ }));+}++for (const query of [+ 'Fix the bug, shall we use feature?',+ 'Fix the bug, the spec states do not use feature and use shared guidance.',+ 'The docs say fix the bug and use feature.',+ 'Can we fix the bug and use feature?',+ 'The spec stipulates: fix the bug and use feature.',+ 'Our policy stipulates repair the bug and use feature.',+ 'Fix the bug, ought we to repair it and use feature?',+ 'Fix the bug, must we repair it and use feature?',+ 'Fix the bug, feature.',+]) {+ test('a general task preamble cannot promote an indirect citation: ' + query, () => withFixture(repoRoot => {+ const result = resolve(repoRoot, { query }, { load: true });+ assert.ok(result.candidates.some(candidate => candidate.id === 'skill:feature'));+ assert.deepEqual(result.selectedIds, []);+ assert.deepEqual(result.loadedIds, []);+ assert.equal(result.reason, 'agent-selection-required');+ assert.equal(result.fallback, null);+ }));+}+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, () => {@@ -467,25 +506,30 @@ test('routing policy changes invalidate receipts created by the old citation polassert.equal(result.fallback, null);}));-test('an old empty selection is reconsidered when directive policy changes', () => withFixture(repoRoot => {- const query = "Don't use feature, use shared guidance.";- const first = resolve(repoRoot, { query });- 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: 6,- triggersDigest: digestObject({}), queryDigest: digestObject(query) });- const { receiptDigest: _receiptDigest, autoSelection: _autoSelection, ...receipt } = first.receipt;- const oldReceipt = { ...receipt, bindingDigest, decision: 'none', selectedIds: [], explicitIds: [],- loadedIds: [], resources: [], selectionDigest: digestObject({ bindingDigest, selectedIds: [], explicitIds: [] }) };- const previous = { ...oldReceipt, receiptDigest: digestObject(oldReceipt) };- const result = resolve(repoRoot, { query }, { previous, load: true });- assert.equal(result.reused, false);- assert.notEqual(result.receipt.bindingDigest, previous.bindingDigest);- assert.deepEqual(result.loadedIds, ['skill:shared']);- assert.equal(result.reason, 'auto-selection');-}));+for (const [routingPolicyVersion, query, expectedId] of [+ [6, "Don't use feature, use shared guidance.", 'skill:shared'],+ [8, 'Fix the bug, use feature', 'skill:feature'],+]) {+ test('an old policy ' + routingPolicyVersion + ' empty selection is reconsidered after policy changes', () => withFixture(repoRoot => {+ const first = resolve(repoRoot, { query });+ 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,+ triggersDigest: digestObject({}), queryDigest: digestObject(query) });+ const { receiptDigest: _receiptDigest, autoSelection: _autoSelection, ...receipt } = first.receipt;+ const oldReceipt = { ...receipt, bindingDigest, decision: 'none', selectedIds: [], explicitIds: [],+ loadedIds: [], resources: [], selectionDigest: digestObject({ bindingDigest, selectedIds: [], explicitIds: [] }) };+ const previous = { ...oldReceipt, receiptDigest: digestObject(oldReceipt) };+ const result = resolve(repoRoot, { query }, { previous, load: true });+ assert.equal(result.reused, false);+ assert.notEqual(result.receipt.bindingDigest, previous.bindingDigest);+ assert.deepEqual(result.selectedIds, [expectedId]);+ assert.deepEqual(result.loadedIds, [expectedId]);+ assert.equal(result.reason, 'auto-selection');+ }));+}for (const query of ['Should we use feature? Use feature.',