fix: port continuous-learning observer fixes

Ports continuous-learning observer signal, storage, remote normalization, and v1 deprecation fixes onto current main.
This commit is contained in:
Affaan Mustafa
2026-05-11 03:35:42 -04:00
committed by GitHub
parent e674a7dbd7
commit 12e1bc424d
17 changed files with 512 additions and 56 deletions

View File

@@ -248,7 +248,7 @@ function withPrependedPath(binDir, env = {}) {
}
function assertNoProjectDetectionSideEffects(homeDir, testName) {
const homunculusDir = path.join(homeDir, '.claude', 'homunculus');
const homunculusDir = path.join(homeDir, '.local', 'share', 'ecc-homunculus');
const registryPath = path.join(homunculusDir, 'projects.json');
const projectsDir = path.join(homunculusDir, 'projects');
@@ -2885,11 +2885,12 @@ async function runTests() {
assert.strictEqual(code, 0, `detect-project should source cleanly, stderr: ${stderr}`);
const [projectId, projectDir] = stdout.trim().split(/\r?\n/);
const registryPath = path.join(homeDir, '.claude', 'homunculus', 'projects.json');
const registryPath = path.join(homeDir, '.local', 'share', 'ecc-homunculus', 'projects.json');
const expectedProjectDir = path.join(
homeDir,
'.claude',
'homunculus',
'.local',
'share',
'ecc-homunculus',
'projects',
projectId
);
@@ -2963,7 +2964,7 @@ async function runTests() {
assert.strictEqual(result.code, 0, `observe.sh should exit successfully, stderr: ${result.stderr}`);
const projectsDir = path.join(homeDir, '.claude', 'homunculus', 'projects');
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');

View File

@@ -112,7 +112,7 @@ function runObserve({ homeDir, cwd }) {
}
function readSingleProjectMetadata(homeDir) {
const projectsDir = path.join(homeDir, '.claude', 'homunculus', 'projects');
const projectsDir = path.join(homeDir, '.local', 'share', 'ecc-homunculus', 'projects');
const projectIds = fs.readdirSync(projectsDir);
assert.strictEqual(projectIds.length, 1, 'Expected exactly one project directory');
const projectDir = path.join(projectsDir, projectIds[0]);

View File

@@ -96,7 +96,8 @@ test('observer-loop.sh defines ANALYZING guard variable', () => {
test('on_usr1 checks ANALYZING before starting analysis', () => {
const content = fs.readFileSync(observerLoopPath, 'utf8');
assert.ok(content.includes('if [ "$ANALYZING" -eq 1 ]'), 'on_usr1 should check ANALYZING flag');
assert.ok(content.includes('Analysis already in progress, skipping signal'), 'on_usr1 should log when skipping due to re-entrancy');
assert.ok(content.includes('Analysis already in progress, deferring signal'), 'on_usr1 should log when deferring due to re-entrancy');
assert.ok(content.includes('PENDING_ANALYSIS=1'), 'on_usr1 should preserve re-entrant nudges for the next loop iteration');
});
test('on_usr1 sets ANALYZING=1 before and ANALYZING=0 after analysis', () => {
@@ -110,6 +111,15 @@ test('on_usr1 sets ANALYZING=1 before and ANALYZING=0 after analysis', () => {
assert.ok(analyzeReset > analyzeObsCall, 'ANALYZING=0 should follow analyze_observations');
});
test('observer-loop checks pending analysis before sleeping', () => {
const content = fs.readFileSync(observerLoopPath, 'utf8');
assert.ok(/^PENDING_ANALYSIS=0$/m.test(content), 'PENDING_ANALYSIS should initialize to 0');
assert.ok(
/if \[ "\$PENDING_ANALYSIS" -eq 1 \]; then[\s\S]*?analyze_observations[\s\S]*?continue[\s\S]*?sleep "\$OBSERVER_INTERVAL_SECONDS"/.test(content),
'observer-loop should process deferred analysis before the interval sleep'
);
});
// ──────────────────────────────────────────────────────
// Test group 3: observer-loop.sh cooldown throttle
// ──────────────────────────────────────────────────────
@@ -334,8 +344,10 @@ test('observe.sh creates counter file and increments on each call', () => {
// Create a minimal detect-project.sh that sets required vars
const skillRoot = path.join(testDir, 'skill');
const scriptsDir = path.join(skillRoot, 'scripts');
const scriptsLibDir = path.join(scriptsDir, 'lib');
const hooksDir = path.join(skillRoot, 'hooks');
fs.mkdirSync(scriptsDir, { recursive: true });
fs.mkdirSync(scriptsLibDir, { recursive: true });
fs.mkdirSync(hooksDir, { recursive: true });
// Minimal detect-project.sh stub
@@ -351,6 +363,14 @@ test('observe.sh creates counter file and increments on each call', () => {
''
].join('\n')
);
fs.writeFileSync(
path.join(scriptsLibDir, 'homunculus-dir.sh'),
[
'#!/bin/bash',
'_ecc_resolve_homunculus_dir() { printf "%s\\n" "$HOME/.local/share/ecc-homunculus"; }',
''
].join('\n')
);
// Copy observe.sh but patch SKILL_ROOT to our test dir
let observeContent = fs.readFileSync(observeShPath, 'utf8');

View File

@@ -226,6 +226,15 @@ function cleanupTestDir(testDir) {
fs.rmSync(testDir, { recursive: true, force: true });
}
function getTestHomunculusEnv(testDir) {
const xdgDataHome = path.join(testDir, '.local', 'share');
return {
HOME: testDir,
XDG_DATA_HOME: xdgDataHome,
homunculusDir: path.join(xdgDataHome, 'ecc-homunculus'),
};
}
function writeInstinctFile(filePath, entries) {
const body = entries.map(entry => `---
id: ${entry.id}
@@ -380,19 +389,20 @@ async function runTests() {
try {
const sessionId = `session-${Date.now()}`;
const homunculusEnv = getTestHomunculusEnv(testDir);
const result = await runHookWithInput(
path.join(scriptsDir, 'session-start.js'),
{},
{
HOME: testDir,
HOME: homunculusEnv.HOME,
XDG_DATA_HOME: homunculusEnv.XDG_DATA_HOME,
CLAUDE_PROJECT_DIR: projectDir,
CLAUDE_SESSION_ID: sessionId
}
);
assert.strictEqual(result.code, 0, 'SessionStart should exit 0');
const homunculusDir = path.join(testDir, '.claude', 'homunculus');
const projectsDir = path.join(homunculusDir, 'projects');
const projectsDir = path.join(homunculusEnv.homunculusDir, 'projects');
const projectEntries = fs.existsSync(projectsDir) ? fs.readdirSync(projectsDir) : [];
assert.ok(projectEntries.length > 0, 'SessionStart should create a homunculus project directory');
const leaseDir = path.join(projectsDir, projectEntries[0], '.observer-sessions');
@@ -410,7 +420,8 @@ async function runTests() {
try {
const projectId = crypto.createHash('sha256').update(projectDir).digest('hex').slice(0, 12);
const homunculusDir = path.join(testDir, '.claude', 'homunculus');
const homunculusEnv = getTestHomunculusEnv(testDir);
const homunculusDir = homunculusEnv.homunculusDir;
const projectInstinctDir = path.join(homunculusDir, 'projects', projectId, 'instincts', 'personal');
const globalInstinctDir = path.join(homunculusDir, 'instincts', 'inherited');
@@ -445,7 +456,8 @@ async function runTests() {
path.join(scriptsDir, 'session-start.js'),
{},
{
HOME: testDir,
HOME: homunculusEnv.HOME,
XDG_DATA_HOME: homunculusEnv.XDG_DATA_HOME,
CLAUDE_PROJECT_DIR: projectDir,
}
);
@@ -474,18 +486,19 @@ async function runTests() {
});
try {
const homunculusEnv = getTestHomunculusEnv(testDir);
await runHookWithInput(
path.join(scriptsDir, 'session-start.js'),
{},
{
HOME: testDir,
HOME: homunculusEnv.HOME,
XDG_DATA_HOME: homunculusEnv.XDG_DATA_HOME,
CLAUDE_PROJECT_DIR: projectDir,
CLAUDE_SESSION_ID: sessionId
}
);
const homunculusDir = path.join(testDir, '.claude', 'homunculus');
const projectsDir = path.join(homunculusDir, 'projects');
const projectsDir = path.join(homunculusEnv.homunculusDir, 'projects');
const projectEntries = fs.existsSync(projectsDir) ? fs.readdirSync(projectsDir) : [];
assert.ok(projectEntries.length > 0, 'Expected SessionStart to create a homunculus project directory');
const projectStorageDir = path.join(projectsDir, projectEntries[0]);
@@ -497,7 +510,8 @@ async function runTests() {
path.join(scriptsDir, 'session-end-marker.js'),
markerInput,
{
HOME: testDir,
HOME: homunculusEnv.HOME,
XDG_DATA_HOME: homunculusEnv.XDG_DATA_HOME,
CLAUDE_PROJECT_DIR: projectDir,
CLAUDE_SESSION_ID: sessionId
}

View File

@@ -0,0 +1,134 @@
const assert = require('assert');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { spawnSync } = require('child_process');
const {
getHomunculusDir,
normalizeRemoteUrl,
resolveProjectContext,
} = require('../../scripts/lib/observer-sessions');
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(`${name}`);
passed += 1;
} catch (error) {
console.log(`${name}`);
console.log(` ${error.message}`);
failed += 1;
}
}
function createTempDir() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-observer-sessions-'));
}
function cleanup(dir) {
try {
fs.rmSync(dir, { recursive: true, force: true });
} catch {
// ignore cleanup errors
}
}
function withEnv(overrides, fn) {
const previous = {};
for (const key of Object.keys(overrides)) {
previous[key] = process.env[key];
if (overrides[key] === undefined) {
delete process.env[key];
} else {
process.env[key] = overrides[key];
}
}
try {
return fn();
} finally {
for (const [key, value] of Object.entries(previous)) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
}
}
function initRepo(repoDir, remoteUrl) {
fs.mkdirSync(repoDir, { recursive: true });
spawnSync('git', ['init'], { cwd: repoDir, stdio: 'ignore' });
spawnSync('git', ['remote', 'add', 'origin', remoteUrl], { cwd: repoDir, stdio: 'ignore' });
}
console.log('\n=== observer-sessions tests ===\n');
test('getHomunculusDir prefers absolute CLV2_HOMUNCULUS_DIR', () => {
const root = createTempDir();
try {
const override = path.join(root, 'custom-store');
withEnv({ CLV2_HOMUNCULUS_DIR: override, XDG_DATA_HOME: path.join(root, 'xdg') }, () => {
assert.strictEqual(getHomunculusDir(), override);
});
} finally {
cleanup(root);
}
});
test('getHomunculusDir ignores relative overrides and uses XDG_DATA_HOME', () => {
const root = createTempDir();
try {
const xdg = path.join(root, 'xdg');
withEnv({ CLV2_HOMUNCULUS_DIR: 'relative-store', XDG_DATA_HOME: xdg }, () => {
assert.strictEqual(getHomunculusDir(), path.join(xdg, 'ecc-homunculus'));
});
} finally {
cleanup(root);
}
});
test('normalizeRemoteUrl collapses common network remote variants', () => {
const expected = 'github.com/owner/repo';
assert.strictEqual(normalizeRemoteUrl('git@github.com:Owner/Repo.git'), expected);
assert.strictEqual(normalizeRemoteUrl('https://github.com/owner/repo.git'), expected);
assert.strictEqual(normalizeRemoteUrl('ssh://git@github.com/Owner/Repo.git'), expected);
assert.strictEqual(normalizeRemoteUrl('https://token@github.com/owner/repo.git'), expected);
});
test('normalizeRemoteUrl preserves local path case', () => {
assert.strictEqual(normalizeRemoteUrl('/tmp/Repos/MyProject'), '/tmp/Repos/MyProject');
assert.strictEqual(normalizeRemoteUrl('file:///tmp/Repos/MyProject.git'), '/tmp/Repos/MyProject');
});
test('resolveProjectContext gives SSH and HTTPS clones the same project id', () => {
const root = createTempDir();
try {
const storage = path.join(root, 'store');
const sshRepo = path.join(root, 'ssh-clone');
const httpsRepo = path.join(root, 'https-clone');
initRepo(sshRepo, 'git@github.com:Owner/Repo.git');
initRepo(httpsRepo, 'https://github.com/owner/repo.git');
withEnv({
CLV2_HOMUNCULUS_DIR: storage,
XDG_DATA_HOME: undefined,
CLAUDE_PROJECT_DIR: undefined,
}, () => {
const sshContext = resolveProjectContext(sshRepo);
const httpsContext = resolveProjectContext(httpsRepo);
assert.strictEqual(sshContext.projectId, httpsContext.projectId);
assert.strictEqual(sshContext.projectDir, httpsContext.projectDir);
});
} finally {
cleanup(root);
}
});
console.log(`\nPassed: ${passed}`);
console.log(`Failed: ${failed}`);
process.exit(failed > 0 ? 1 : 0);