mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-05-21 20:28:26 +08:00
fix(learning): add project registry maintenance
This commit is contained in:
committed by
Affaan Mustafa
parent
98bd517451
commit
bc519e5b8e
@@ -181,29 +181,14 @@ test('detect-project.sh sets PROJECT_NAME and non-global PROJECT_ID for worktree
|
||||
}
|
||||
});
|
||||
|
||||
// Create a worktree-like directory with .git as a file
|
||||
const worktreeDir = path.join(testDir, 'my-worktree');
|
||||
fs.mkdirSync(worktreeDir, { recursive: true });
|
||||
|
||||
// Set up the worktree directory structure in the main repo
|
||||
const worktreesDir = path.join(mainRepo, '.git', 'worktrees', 'my-worktree');
|
||||
fs.mkdirSync(worktreesDir, { recursive: true });
|
||||
|
||||
// Create the gitdir file and commondir in the worktree metadata
|
||||
const mainGitDir = path.join(mainRepo, '.git');
|
||||
fs.writeFileSync(
|
||||
path.join(worktreesDir, 'commondir'),
|
||||
'../..\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(worktreesDir, 'HEAD'),
|
||||
fs.readFileSync(path.join(mainGitDir, 'HEAD'), 'utf8')
|
||||
);
|
||||
|
||||
// Write .git file in the worktree directory (this is what git worktree creates)
|
||||
fs.writeFileSync(
|
||||
path.join(worktreeDir, '.git'),
|
||||
`gitdir: ${worktreesDir}\n`
|
||||
execSync(`git worktree add "${worktreeDir}" -b feature/project-id`, {
|
||||
cwd: mainRepo,
|
||||
stdio: 'pipe'
|
||||
});
|
||||
assert.ok(
|
||||
fs.statSync(path.join(worktreeDir, '.git')).isFile(),
|
||||
'linked worktree should expose .git as a file'
|
||||
);
|
||||
|
||||
// Source detect-project.sh from the worktree directory and capture results
|
||||
@@ -248,6 +233,61 @@ test('detect-project.sh sets PROJECT_NAME and non-global PROJECT_ID for worktree
|
||||
}
|
||||
});
|
||||
|
||||
test('detect-project.sh uses the main worktree hash when no remote exists', () => {
|
||||
const testDir = createTempDir();
|
||||
|
||||
try {
|
||||
const mainRepo = path.join(testDir, 'main-repo');
|
||||
const worktreeDir = path.join(testDir, 'feature-worktree');
|
||||
const homeDir = path.join(testDir, 'home');
|
||||
fs.mkdirSync(mainRepo, { recursive: true });
|
||||
fs.mkdirSync(homeDir, { recursive: true });
|
||||
execSync('git init', { cwd: mainRepo, stdio: 'pipe' });
|
||||
execSync('git commit --allow-empty -m "init"', {
|
||||
cwd: mainRepo,
|
||||
stdio: 'pipe',
|
||||
env: {
|
||||
...process.env,
|
||||
GIT_AUTHOR_NAME: 'Test',
|
||||
GIT_AUTHOR_EMAIL: 'test@test.com',
|
||||
GIT_COMMITTER_NAME: 'Test',
|
||||
GIT_COMMITTER_EMAIL: 'test@test.com'
|
||||
}
|
||||
});
|
||||
execSync(`git worktree add "${worktreeDir}" -b feature/no-remote`, {
|
||||
cwd: mainRepo,
|
||||
stdio: 'pipe'
|
||||
});
|
||||
|
||||
function detectId(targetDir) {
|
||||
const script = `
|
||||
export HOME="${toBashPath(homeDir)}"
|
||||
export USERPROFILE="${toBashPath(homeDir)}"
|
||||
export CLAUDE_PROJECT_DIR="${toBashPath(targetDir)}"
|
||||
source "${toBashPath(detectProjectPath)}" >/dev/null
|
||||
printf "%s" "$PROJECT_ID"
|
||||
`;
|
||||
return execFileSync('bash', ['-lc', script], {
|
||||
cwd: targetDir,
|
||||
timeout: 10000,
|
||||
env: {
|
||||
...process.env,
|
||||
HOME: toBashPath(homeDir),
|
||||
USERPROFILE: toBashPath(homeDir),
|
||||
CLAUDE_PROJECT_DIR: toBashPath(targetDir)
|
||||
}
|
||||
}).toString();
|
||||
}
|
||||
|
||||
const mainId = detectId(mainRepo);
|
||||
const worktreeId = detectId(worktreeDir);
|
||||
assert.ok(mainId && mainId !== 'global', 'main repo should get a project id');
|
||||
assert.strictEqual(worktreeId, mainId, 'linked worktree should share the main worktree project id');
|
||||
} finally {
|
||||
cleanupDir(testDir);
|
||||
}
|
||||
});
|
||||
|
||||
// ──────────────────────────────────────────────────────
|
||||
// Summary
|
||||
// ──────────────────────────────────────────────────────
|
||||
|
||||
@@ -3300,11 +3300,14 @@ async function runTests() {
|
||||
|
||||
assert.strictEqual(result.code, 0, `observe.sh should exit successfully, stderr: ${result.stderr}`);
|
||||
|
||||
const projectsDir = path.join(homeDir, '.local', 'share', 'ecc-homunculus', 'projects');
|
||||
const projectIds = fs.readdirSync(projectsDir);
|
||||
assert.strictEqual(projectIds.length, 1, 'observe.sh should create one project-scoped observation directory');
|
||||
const homunculusDir = path.join(homeDir, '.local', 'share', 'ecc-homunculus');
|
||||
const projectsDir = path.join(homunculusDir, 'projects');
|
||||
assert.ok(
|
||||
!fs.existsSync(projectsDir) || fs.readdirSync(projectsDir).length === 0,
|
||||
'observe.sh should not create a project-scoped directory for a non-git cwd'
|
||||
);
|
||||
|
||||
const observationsPath = path.join(projectsDir, projectIds[0], 'observations.jsonl');
|
||||
const observationsPath = path.join(homunculusDir, 'observations.jsonl');
|
||||
const observations = fs.readFileSync(observationsPath, 'utf8').trim().split('\n').filter(Boolean);
|
||||
assert.ok(observations.length > 0, 'observe.sh should append at least one observation');
|
||||
|
||||
|
||||
@@ -135,8 +135,8 @@ test('observe.sh resolves cwd to git root before setting CLAUDE_PROJECT_DIR', ()
|
||||
'observe.sh should resolve STDIN_CWD to git repo root'
|
||||
);
|
||||
assert.ok(
|
||||
content.includes('${_GIT_ROOT:-$STDIN_CWD}'),
|
||||
'observe.sh should fall back to raw cwd when git root is unavailable'
|
||||
content.includes('export CLV2_NO_PROJECT=1'),
|
||||
'observe.sh should mark non-git cwd payloads as global instead of registering raw cwd'
|
||||
);
|
||||
});
|
||||
|
||||
@@ -250,7 +250,7 @@ test('observe.sh falls back to CLAUDE_HOOK_EVENT_NAME when no phase argument is
|
||||
}
|
||||
});
|
||||
|
||||
test('observe.sh keeps the raw cwd when the directory is not inside a git repo', () => {
|
||||
test('observe.sh records non-git cwd payloads globally without project registry side effects', () => {
|
||||
const testRoot = createTempDir();
|
||||
|
||||
try {
|
||||
@@ -262,12 +262,17 @@ test('observe.sh keeps the raw cwd when the directory is not inside a git repo',
|
||||
const result = runObserve({ homeDir, cwd: nonGitDir });
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
|
||||
const { metadata } = readSingleProjectMetadata(homeDir);
|
||||
assert.strictEqual(
|
||||
normalizeComparablePath(metadata.root),
|
||||
normalizeComparablePath(nonGitDir),
|
||||
'project metadata root should stay on the non-git cwd'
|
||||
const homunculusDir = path.join(homeDir, '.local', 'share', 'ecc-homunculus');
|
||||
const projectsDir = path.join(homunculusDir, 'projects');
|
||||
const registryPath = path.join(homunculusDir, 'projects.json');
|
||||
const observationsPath = path.join(homunculusDir, 'observations.jsonl');
|
||||
|
||||
assert.ok(!fs.existsSync(registryPath), 'non-git cwd should not create projects.json');
|
||||
assert.ok(
|
||||
!fs.existsSync(projectsDir) || fs.readdirSync(projectsDir).length === 0,
|
||||
'non-git cwd should not create project directories'
|
||||
);
|
||||
assert.ok(fs.existsSync(observationsPath), 'non-git cwd should still record a global observation');
|
||||
} finally {
|
||||
cleanupDir(testRoot);
|
||||
}
|
||||
|
||||
260
tests/scripts/instinct-cli-projects.test.js
Normal file
260
tests/scripts/instinct-cli-projects.test.js
Normal file
@@ -0,0 +1,260 @@
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const { spawnSync } = require('child_process');
|
||||
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
|
||||
const repoRoot = path.resolve(__dirname, '..', '..');
|
||||
const cliPath = path.join(
|
||||
repoRoot,
|
||||
'skills',
|
||||
'continuous-learning-v2',
|
||||
'scripts',
|
||||
'instinct-cli.py'
|
||||
);
|
||||
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(` ✓ ${name}`);
|
||||
passed += 1;
|
||||
} catch (error) {
|
||||
console.log(` ✗ ${name}`);
|
||||
console.log(` Error: ${error.message}`);
|
||||
failed += 1;
|
||||
}
|
||||
}
|
||||
|
||||
function createTempDir() {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-instinct-cli-projects-'));
|
||||
}
|
||||
|
||||
function cleanupDir(dir) {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
function writeJson(filePath, payload) {
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.writeFileSync(filePath, `${JSON.stringify(payload, null, 2)}\n`);
|
||||
}
|
||||
|
||||
function readJson(filePath) {
|
||||
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
||||
}
|
||||
|
||||
function writeInstinct(filePath, id, confidence = 0.9) {
|
||||
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
filePath,
|
||||
[
|
||||
'---',
|
||||
`id: ${id}`,
|
||||
'trigger: "when repeated"',
|
||||
`confidence: ${confidence}`,
|
||||
'domain: workflow',
|
||||
'---',
|
||||
'',
|
||||
`Action for ${id}.`,
|
||||
'',
|
||||
].join('\n')
|
||||
);
|
||||
}
|
||||
|
||||
function seedProject(root, id, options = {}) {
|
||||
const projectDir = path.join(root, 'projects', id);
|
||||
const personalDir = path.join(projectDir, 'instincts', 'personal');
|
||||
const inheritedDir = path.join(projectDir, 'instincts', 'inherited');
|
||||
fs.mkdirSync(personalDir, { recursive: true });
|
||||
fs.mkdirSync(inheritedDir, { recursive: true });
|
||||
|
||||
for (const instinct of options.personal || []) {
|
||||
writeInstinct(path.join(personalDir, `${instinct}.yaml`), instinct);
|
||||
}
|
||||
for (const instinct of options.inherited || []) {
|
||||
writeInstinct(path.join(inheritedDir, `${instinct}.yaml`), instinct);
|
||||
}
|
||||
if (options.observations) {
|
||||
fs.writeFileSync(
|
||||
path.join(projectDir, 'observations.jsonl'),
|
||||
options.observations.map(row => JSON.stringify(row)).join('\n') + '\n'
|
||||
);
|
||||
}
|
||||
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
function projectHash(value) {
|
||||
return crypto.createHash('sha256').update(value).digest('hex').slice(0, 12);
|
||||
}
|
||||
|
||||
function runGit(cwd, args) {
|
||||
const result = spawnSync('git', args, {
|
||||
cwd,
|
||||
encoding: 'utf8',
|
||||
});
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
return result.stdout.trim();
|
||||
}
|
||||
|
||||
function runCli(root, args, options = {}) {
|
||||
return spawnSync('python3', [cliPath, ...args], {
|
||||
cwd: options.cwd || repoRoot,
|
||||
encoding: 'utf8',
|
||||
env: {
|
||||
...process.env,
|
||||
CLV2_HOMUNCULUS_DIR: root,
|
||||
HOME: path.join(root, 'home'),
|
||||
USERPROFILE: path.join(root, 'home'),
|
||||
CLAUDE_PROJECT_DIR: '',
|
||||
...(options.env || {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
console.log('\n=== Testing instinct-cli.py projects maintenance ===\n');
|
||||
|
||||
test('projects delete --dry-run preserves registry and project files', () => {
|
||||
const root = createTempDir();
|
||||
try {
|
||||
const registryPath = path.join(root, 'projects.json');
|
||||
seedProject(root, 'alpha123', {
|
||||
personal: ['keep-me'],
|
||||
observations: [{ event: 'tool_complete' }],
|
||||
});
|
||||
writeJson(registryPath, {
|
||||
alpha123: { name: 'alpha', root: '/repo/alpha', remote: '', last_seen: '2026-01-01T00:00:00Z' },
|
||||
});
|
||||
|
||||
const result = runCli(root, ['projects', 'delete', 'alpha123', '--dry-run']);
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
assert.match(result.stdout, /would delete/i);
|
||||
assert.ok(fs.existsSync(path.join(root, 'projects', 'alpha123')));
|
||||
assert.ok(readJson(registryPath).alpha123);
|
||||
} finally {
|
||||
cleanupDir(root);
|
||||
}
|
||||
});
|
||||
|
||||
test('projects delete --force removes registry entry and project directory', () => {
|
||||
const root = createTempDir();
|
||||
try {
|
||||
const registryPath = path.join(root, 'projects.json');
|
||||
seedProject(root, 'alpha123', { personal: ['delete-me'] });
|
||||
writeJson(registryPath, {
|
||||
alpha123: { name: 'alpha', root: '/repo/alpha', remote: '', last_seen: '2026-01-01T00:00:00Z' },
|
||||
});
|
||||
|
||||
const result = runCli(root, ['projects', 'delete', 'alpha123', '--force']);
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
assert.ok(!fs.existsSync(path.join(root, 'projects', 'alpha123')));
|
||||
assert.ok(!readJson(registryPath).alpha123);
|
||||
} finally {
|
||||
cleanupDir(root);
|
||||
}
|
||||
});
|
||||
|
||||
test('projects gc --force removes only zero-value project entries', () => {
|
||||
const root = createTempDir();
|
||||
try {
|
||||
const registryPath = path.join(root, 'projects.json');
|
||||
seedProject(root, 'empty000');
|
||||
seedProject(root, 'active999', { personal: ['active'] });
|
||||
writeJson(registryPath, {
|
||||
empty000: { name: 'empty', root: '/tmp/empty', remote: '', last_seen: '2026-01-01T00:00:00Z' },
|
||||
active999: { name: 'active', root: '/repo/active', remote: '', last_seen: '2026-01-02T00:00:00Z' },
|
||||
});
|
||||
|
||||
const result = runCli(root, ['projects', 'gc', '--force']);
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
const registry = readJson(registryPath);
|
||||
assert.ok(!registry.empty000);
|
||||
assert.ok(registry.active999);
|
||||
assert.ok(!fs.existsSync(path.join(root, 'projects', 'empty000')));
|
||||
assert.ok(fs.existsSync(path.join(root, 'projects', 'active999')));
|
||||
} finally {
|
||||
cleanupDir(root);
|
||||
}
|
||||
});
|
||||
|
||||
test('projects merge deduplicates instincts, appends observations, and removes source', () => {
|
||||
const root = createTempDir();
|
||||
try {
|
||||
const registryPath = path.join(root, 'projects.json');
|
||||
seedProject(root, 'from111', {
|
||||
personal: ['shared', 'from-only'],
|
||||
observations: [{ event: 'from-event' }],
|
||||
});
|
||||
seedProject(root, 'into222', {
|
||||
personal: ['shared', 'into-only'],
|
||||
observations: [{ event: 'into-event' }],
|
||||
});
|
||||
writeJson(registryPath, {
|
||||
from111: { name: 'from', root: '/repo/from', remote: '', last_seen: '2026-01-01T00:00:00Z' },
|
||||
into222: { name: 'into', root: '/repo/into', remote: '', last_seen: '2026-01-02T00:00:00Z' },
|
||||
});
|
||||
|
||||
const result = runCli(root, ['projects', 'merge', 'from111', 'into222', '--force']);
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
assert.ok(!fs.existsSync(path.join(root, 'projects', 'from111')));
|
||||
assert.ok(!readJson(registryPath).from111);
|
||||
assert.ok(readJson(registryPath).into222);
|
||||
|
||||
const intoPersonal = path.join(root, 'projects', 'into222', 'instincts', 'personal');
|
||||
assert.ok(fs.existsSync(path.join(intoPersonal, 'shared.yaml')));
|
||||
assert.ok(fs.existsSync(path.join(intoPersonal, 'from-only.yaml')));
|
||||
assert.ok(fs.existsSync(path.join(intoPersonal, 'into-only.yaml')));
|
||||
|
||||
const observations = fs.readFileSync(
|
||||
path.join(root, 'projects', 'into222', 'observations.jsonl'),
|
||||
'utf8'
|
||||
);
|
||||
assert.match(observations, /from-event/);
|
||||
assert.match(observations, /into-event/);
|
||||
} finally {
|
||||
cleanupDir(root);
|
||||
}
|
||||
});
|
||||
|
||||
test('status migrates legacy no-remote linked worktree project dirs to main worktree id', () => {
|
||||
const root = createTempDir();
|
||||
const repoParent = createTempDir();
|
||||
try {
|
||||
const mainWorktree = path.join(repoParent, 'main');
|
||||
const linkedWorktree = path.join(repoParent, 'linked');
|
||||
fs.mkdirSync(mainWorktree, { recursive: true });
|
||||
runGit(mainWorktree, ['init']);
|
||||
runGit(mainWorktree, ['config', 'user.email', 'ecc@example.test']);
|
||||
runGit(mainWorktree, ['config', 'user.name', 'ECC Test']);
|
||||
fs.writeFileSync(path.join(mainWorktree, 'README.md'), 'test\n');
|
||||
runGit(mainWorktree, ['add', 'README.md']);
|
||||
runGit(mainWorktree, ['commit', '-m', 'init']);
|
||||
runGit(mainWorktree, ['worktree', 'add', linkedWorktree]);
|
||||
|
||||
const mainRoot = runGit(mainWorktree, ['rev-parse', '--show-toplevel']);
|
||||
const linkedRoot = runGit(linkedWorktree, ['rev-parse', '--show-toplevel']);
|
||||
const oldLinkedId = projectHash(linkedRoot);
|
||||
const mainId = projectHash(mainRoot);
|
||||
seedProject(root, oldLinkedId, { personal: ['legacy-worktree'] });
|
||||
|
||||
const result = runCli(root, ['status'], { cwd: linkedRoot });
|
||||
assert.strictEqual(result.status, 0, result.stderr);
|
||||
assert.ok(!fs.existsSync(path.join(root, 'projects', oldLinkedId)));
|
||||
assert.ok(fs.existsSync(path.join(root, 'projects', mainId)));
|
||||
assert.ok(
|
||||
fs.existsSync(path.join(root, 'projects', mainId, 'instincts', 'personal', 'legacy-worktree.yaml'))
|
||||
);
|
||||
assert.match(result.stdout, new RegExp(`\\(${mainId}\\)`));
|
||||
} finally {
|
||||
cleanupDir(root);
|
||||
cleanupDir(repoParent);
|
||||
}
|
||||
});
|
||||
|
||||
console.log(`\nPassed: ${passed}`);
|
||||
console.log(`Failed: ${failed}`);
|
||||
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
Reference in New Issue
Block a user