fix: clean promoted instinct sources

This commit is contained in:
Affaan Mustafa
2026-07-26 06:07:48 -04:00
parent 4d0b501b05
commit 130d6c97ec
2 changed files with 252 additions and 0 deletions
+144
View File
@@ -117,6 +117,13 @@ function runGit(cwd, args) {
return result.stdout.trim();
}
function initGitProject(parentDir, name = 'repo') {
const repoDir = path.join(parentDir, name);
fs.mkdirSync(repoDir, { recursive: true });
runGit(repoDir, ['init']);
return repoDir;
}
function runCli(root, args, options = {}) {
return spawnSync(PYTHON3, [cliPath, ...args], {
cwd: options.cwd || repoRoot,
@@ -330,6 +337,143 @@ test('status migrates legacy no-remote linked worktree project dirs to main work
}
});
test('promote removes only the promoted instinct block from project source', () => {
const root = createTempDir();
const repoParent = createTempDir();
try {
const repoDir = initGitProject(repoParent);
const projectId = projectHash(runGit(repoDir, ['rev-parse', '--show-toplevel']));
const sourceFile = path.join(root, 'projects', projectId, 'instincts', 'personal', 'mixed.yaml');
const retainedBlock = [
'---',
'id: keep-me',
'trigger: "when value: contains colon"',
'confidence: 0.72',
'domain: workflow',
'tags: [alpha, beta]',
'---',
'',
'Keep this block exactly.',
'',
].join('\n');
fs.mkdirSync(path.dirname(sourceFile), { recursive: true });
fs.writeFileSync(
sourceFile,
[
'---',
'id: promote-me',
'trigger: "when promoting"',
'confidence: 0.91',
'domain: workflow',
'---',
'',
'Promote this block.',
'',
retainedBlock,
].join('\n')
);
const result = runCli(root, ['promote', 'promote-me', '--force'], { cwd: repoDir });
assert.strictEqual(result.status, 0, result.stderr);
assert.ok(fs.existsSync(path.join(root, 'instincts', 'personal', 'promote-me.yaml')));
assert.strictEqual(fs.readFileSync(sourceFile, 'utf8'), retainedBlock);
} finally {
cleanupDir(root);
cleanupDir(repoParent);
}
});
test('promote deletes project source file when it only contained the promoted instinct', () => {
const root = createTempDir();
const repoParent = createTempDir();
try {
const repoDir = initGitProject(repoParent);
const projectId = projectHash(runGit(repoDir, ['rev-parse', '--show-toplevel']));
const sourceFile = path.join(root, 'projects', projectId, 'instincts', 'personal', 'single.yaml');
writeInstinct(sourceFile, 'promote-single', 0.93);
const result = runCli(root, ['promote', 'promote-single', '--force'], { cwd: repoDir });
assert.strictEqual(result.status, 0, result.stderr);
assert.ok(fs.existsSync(path.join(root, 'instincts', 'personal', 'promote-single.yaml')));
assert.ok(!fs.existsSync(sourceFile));
} finally {
cleanupDir(root);
cleanupDir(repoParent);
}
});
test('promote preserves malformed and foreign source blocks while removing target', () => {
const root = createTempDir();
const repoParent = createTempDir();
try {
const repoDir = initGitProject(repoParent);
const projectId = projectHash(runGit(repoDir, ['rev-parse', '--show-toplevel']));
const sourceFile = path.join(root, 'projects', projectId, 'instincts', 'personal', 'foreign.yaml');
const foreignContent = [
'---',
'title: foreign block without id',
'---',
'',
'Do not drop this content.',
'',
'---',
'id: keep-foreign-neighbor',
'trigger: "when nearby"',
'confidence: not-a-float',
'---',
'',
'This parse-tolerated block must also stay raw.',
'',
].join('\n');
fs.mkdirSync(path.dirname(sourceFile), { recursive: true });
fs.writeFileSync(
sourceFile,
[
foreignContent,
'---',
'id: promote-foreign',
'trigger: "when target appears"',
'confidence: 0.95',
'domain: workflow',
'---',
'',
'Only this block should be removed.',
'',
].join('\n')
);
const result = runCli(root, ['promote', 'promote-foreign', '--force'], { cwd: repoDir });
assert.strictEqual(result.status, 0, result.stderr);
assert.ok(fs.existsSync(path.join(root, 'instincts', 'personal', 'promote-foreign.yaml')));
assert.strictEqual(fs.readFileSync(sourceFile, 'utf8'), `${foreignContent}\n`);
} finally {
cleanupDir(root);
cleanupDir(repoParent);
}
});
test('auto-promote removes promoted source copies from every contributing project', () => {
const root = createTempDir();
try {
const registryPath = path.join(root, 'projects.json');
const projectOne = seedProject(root, 'proj111', { personal: ['shared-auto'] });
const projectTwo = seedProject(root, 'proj222', { personal: ['shared-auto'] });
writeJson(registryPath, {
proj111: { name: 'one', root: '/repo/one', remote: '', last_seen: '2026-01-01T00:00:00Z' },
proj222: { name: 'two', root: '/repo/two', remote: '', last_seen: '2026-01-02T00:00:00Z' },
});
const result = runCli(root, ['promote', '--force'], { cwd: root });
assert.strictEqual(result.status, 0, result.stderr);
assert.match(result.stdout, /Promoted 1 instincts to global scope/);
assert.ok(fs.existsSync(path.join(root, 'instincts', 'personal', 'shared-auto.yaml')));
assert.ok(!fs.existsSync(path.join(projectOne, 'instincts', 'personal', 'shared-auto.yaml')));
assert.ok(!fs.existsSync(path.join(projectTwo, 'instincts', 'personal', 'shared-auto.yaml')));
} finally {
cleanupDir(root);
}
});
console.log(`\nPassed: ${passed}`);
console.log(`Failed: ${failed}`);