fix(instincts): recognize canonical framework and language aliases

This commit is contained in:
Afloat committed 2026-11-17 13:55:18 +08:00
1 parent 3def2b612f
commit 89a8bbc205
3 files changed
+108 -3

No files matched your search

+8 -3
View File
@@ -104,14 +104,19 @@ function detectStackKeywords(projectRoot, projectInfo) {
/**
* Tokenize a free-text field into lowercase word tokens (split on
* non-alphanumerics). Token-set matching avoids substring false positives such
* as the keyword `go` matching the word `good`.
* non-alphanumerics). Preserve punctuation that identifies a language or
* framework before splitting, using the names returned by detectProjectType.
* Token-set matching avoids substring false positives such as `go`/`good`.
* @param {string} value
* @returns {string[]}
*/
function tokenize(value) {
return String(value || '')
.toLowerCase()
.replace(/\bc#(?![a-z0-9#])/g, 'csharp')
.replace(/\bf#(?![a-z0-9#])/g, 'fsharp')
.replace(/\bnext\.js\b/g, 'nextjs')
.replace(/\bnest\.js\b/g, 'nestjs')
.split(/[^a-z0-9]+/)
.filter(Boolean);
}
@@ -131,7 +136,7 @@ function instinctMatchesStack(instinct, stackKeywords) {
...tokenize(instinct.stack),
]);
for (const keyword of stackKeywords) {
if (tokens.has(keyword)) return true;
if (tokens.has(keyword) || (keyword === 'golang' && tokens.has('go'))) return true;
}
return false;
}
@@ -0,0 +1,57 @@
'use strict';
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const { test } = require('node:test');
const script = path.resolve(__dirname, '../../scripts/hooks/session-start.js');
for (const [marker, content, domain] of [
['next.config.js', 'module.exports = {};', 'Next.js'],
['app.csproj', '<Project />', 'C#'],
['go.mod', 'module example.com/app', 'Go'],
]) {
test(`SessionStart selects a ${domain} instinct at the injection limit`, () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-instinct-alias-'));
try {
const project = path.join(root, 'project');
const homunculus = path.join(root, 'homunculus');
const instincts = path.join(homunculus, 'instincts', 'personal');
fs.mkdirSync(project, { recursive: true });
fs.mkdirSync(instincts, { recursive: true });
fs.writeFileSync(path.join(project, marker), content);
fs.writeFileSync(path.join(instincts, 'relevant.md'),
`---\nid: relevant\nconfidence: 0.75\ndomain: "${domain}"\n---\n## Action\nRELEVANT_STACK_GUIDANCE\n`);
fs.writeFileSync(path.join(instincts, 'unrelated.md'),
'---\nid: unrelated\nconfidence: 0.9\ndomain: terraform\n---\n## Action\nUNRELATED_STACK_GUIDANCE\n');
for (const ranking of ['on', 'off']) {
const result = spawnSync(process.execPath, [script], {
cwd: project, input: '{}', encoding: 'utf8', timeout: 20000,
env: {
...process.env,
ECC_AGENT_DATA_HOME: path.join(root, 'agent-data'),
CLV2_HOMUNCULUS_DIR: homunculus,
CLAUDE_PROJECT_DIR: project,
CLAUDE_SESSION_ID: '', ECC_SESSION_ID: '',
ECC_SESSION_START_CONTEXT: 'on', ECC_SESSION_START_MAX_CHARS: '10000',
ECC_INSTINCT_RELEVANCE_RANKING: ranking,
ECC_INSTINCT_CONFIDENCE_THRESHOLD: '0.7', ECC_MAX_INJECTED_INSTINCTS: '1',
},
});
assert.ifError(result.error);
assert.strictEqual(result.status, 0, result.stderr);
const context = JSON.parse(result.stdout).hookSpecificOutput.additionalContext;
const expected = ranking === 'on' ? 'RELEVANT_STACK_GUIDANCE' : 'UNRELATED_STACK_GUIDANCE';
const excluded = ranking === 'on' ? 'UNRELATED_STACK_GUIDANCE' : 'RELEVANT_STACK_GUIDANCE';
assert.ok(context.includes(expected), context);
assert.ok(!context.includes(excluded), context);
}
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
}
+43
View File
@@ -47,6 +47,45 @@ function writeFile(dir, name, content) {
fs.writeFileSync(path.join(dir, name), content);
}
function runAliasTests() {
let passed = 0;
let failed = 0;
if (test('detected stacks match the language and framework names used in instincts', () => {
const cases = [
['go.mod', 'module example.com/app', { trigger: 'when writing Go tests' }],
['next.config.ts', 'export default {};', { trigger: 'when editing Next.js routes' }],
['nest-cli.json', '{}', { stack: 'Nest.js' }],
['app.csproj', '<Project />', { domain: 'C#' }],
['app.fsproj', '<Project />', { trigger: 'when refactoring F# functions' }],
];
for (const [name, content, instinct] of cases) {
const dir = createTempDir();
try {
writeFile(dir, name, content);
const keywords = detectStackKeywords(dir);
assert.strictEqual(instinctMatchesStack(instinct, keywords), true, name);
const relevant = 0.75 + computeRelevanceBoost(instinct, keywords);
const unrelated = 0.9 + computeRelevanceBoost({ domain: 'terraform' }, keywords);
assert.ok(relevant > unrelated, `${name} should retain the relevant instinct`);
} finally {
cleanupDir(dir);
}
}
})) passed++; else failed++;
if (test('punctuated language aliases do not collapse into C or partial framework names', () => {
assert.strictEqual(instinctMatchesStack({ domain: 'C#' }, new Set(['c'])), false);
assert.strictEqual(instinctMatchesStack({ domain: 'F#' }, new Set(['csharp'])), false);
assert.strictEqual(instinctMatchesStack({ domain: 'C' }, new Set(['csharp'])), false);
assert.strictEqual(instinctMatchesStack({ trigger: 'edit next.json' }, new Set(['nextjs'])), false);
assert.strictEqual(instinctMatchesStack({ trigger: 'nest.json settings' }, new Set(['nestjs'])), false);
assert.strictEqual(instinctMatchesStack({ trigger: 'good practices' }, new Set(['golang'])), false);
})) passed++; else failed++;
return { passed, failed };
}
function runTests() {
let passed = 0;
let failed = 0;
@@ -145,6 +184,10 @@ function runTests() {
assert.strictEqual(instinctMatchesStack({ domain: 'good practices' }, kw), false);
})) passed++; else failed++;
const aliasResults = runAliasTests();
passed += aliasResults.passed;
failed += aliasResults.failed;
if (test('instinctMatchesStack is false with empty keyword set or fields', () => {
assert.strictEqual(instinctMatchesStack({ domain: 'terraform' }, new Set()), false);
assert.strictEqual(instinctMatchesStack({}, new Set(['terraform'])), false);