From ea7e77664a5ffb709a91c2cac38a3960ae4c62cd Mon Sep 17 00:00:00 2001 From: Jeffrey Montoya Date: Wed, 18 Nov 2026 15:38:30 -0500 Subject: [PATCH] fix(clv2): keep the config timestamp check, normalize observer.enabled Address the second round of review findings: - Cache: trust an entry only while it is newer than the repository .git/config as well as while git reports the stored origin, so a core.worktree change that moves the root is not served stale. - observe.sh: emit observer.enabled as a literal true or false so a value containing a newline cannot shift the fields that follow. - Tests: remove GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, GIT_COMMON_DIR and GIT_PREFIX from the detection test environment; add a config-change test and an observer.enabled newline test. Refresh the registry digest. --- manifests/context-packs/skill-triggers@1.json | 2 +- .../continuous-learning-v2/hooks/observe.sh | 4 +- .../scripts/detect-project.sh | 13 +++- tests/hooks/detect-project-cache.test.js | 77 ++++++++++++++++++- 4 files changed, 87 insertions(+), 9 deletions(-) diff --git a/manifests/context-packs/skill-triggers@1.json b/manifests/context-packs/skill-triggers@1.json index 32dff3c6c..3b26ae3b2 100644 --- a/manifests/context-packs/skill-triggers@1.json +++ b/manifests/context-packs/skill-triggers@1.json @@ -10,7 +10,7 @@ "id": "hand-seeded", "source": "manual-curation-pending-regeneration" }, - "registryDigest": "5205bb7d63ca24328d2808837dd9cc07e4c660cd023eddde8fd7d4b8ff3a6d6d", + "registryDigest": "461dbb0857be6b07bc6df8c17f18705485ba5cd80c64376f8decea9909e83220", "schemaVersion": 1, "triggers": { "skill:agent-runtime-gateway-smoke-test": [ diff --git a/skills/continuous-learning-v2/hooks/observe.sh b/skills/continuous-learning-v2/hooks/observe.sh index 9d3d9cb18..4bc268b4c 100755 --- a/skills/continuous-learning-v2/hooks/observe.sh +++ b/skills/continuous-learning-v2/hooks/observe.sh @@ -325,7 +325,9 @@ if config_path: try: with open(config_path) as f: cfg = json.load(f) - enabled = str(cfg.get("observer", {}).get("enabled", False)).lower() + # Only the literal values may leave this block: a user-written string + # with a newline in it would shift the fields that follow. + enabled = "true" if str(cfg.get("observer", {}).get("enabled", False)).lower() == "true" else "false" except Exception: enabled = "false" diff --git a/skills/continuous-learning-v2/scripts/detect-project.sh b/skills/continuous-learning-v2/scripts/detect-project.sh index f8ffb33b1..310523323 100755 --- a/skills/continuous-learning-v2/scripts/detect-project.sh +++ b/skills/continuous-learning-v2/scripts/detect-project.sh @@ -351,14 +351,16 @@ atomic_write_json(registry_path, registry) # A cached entry is rejected, and full detection reruns, when: # - it was written on another day (so the registry last_seen still refreshes # at least daily) +# - the repository's .git/config is not older than the entry (a setting that +# decides the root or the id, such as core.worktree, was changed) # - git no longer reports the origin the entry was derived from (remote # added, changed or removed, which changes the project id) # - the project storage directory or the project root no longer exists # - the stored id is not a shape detection can produce # -# The origin is asked from git on every hit (one `git remote get-url`) rather -# than inferred from file timestamps: the effective origin can come from the -# repository config, a file it includes, the global or system config, or +# The origin is asked from git on every hit (one `git remote get-url`), in +# addition to the timestamp check, because the effective origin can come from +# the repository config, a file it includes, the global or system config, or # GIT_CONFIG_* variables, and only git resolves all of those. The entry stores # the origin that detection itself read, so an entry written while the config # was being changed fails this comparison too. @@ -446,6 +448,11 @@ _clv2_project_cache_load() { # and still a repository with its own .git/config file (see the store side). [ -n "$root" ] && [ -d "$root" ] || return 1 [ -f "${root}/.git/config" ] || return 1 + # Settings in the repository's own config (core.worktree moves the root, + # a remote changes the id) are trusted only while the entry is strictly + # newer than that file. Equal timestamps count as stale so a change in the + # same clock tick is not missed on filesystems with coarse mtimes. + [ "$file" -nt "${root}/.git/config" ] || return 1 # The id is derived from the origin, so the entry is trusted only while # git reports the same origin that detection read when it wrote the entry. # A repository without an origin stores an empty line and must still have diff --git a/tests/hooks/detect-project-cache.test.js b/tests/hooks/detect-project-cache.test.js index b96c621d9..a11fb8a1b 100644 --- a/tests/hooks/detect-project-cache.test.js +++ b/tests/hooks/detect-project-cache.test.js @@ -11,6 +11,7 @@ * (to ask for the origin) * - a changed origin forces re-detection wherever it is configured: the * repository config, a file it includes, or the global config + * - a .git/config newer than the entry forces re-detection (core.worktree) * - an entry that holds an origin git does not report is rejected * - credentials embedded in the origin never reach the cache file * - an entry from another day forces re-detection @@ -77,8 +78,19 @@ function bashSupportsCache() { return major > 4 || (major === 4 && minor >= 2); } +// Variables that point git at a particular repository. The test runner already +// removes them; they are removed here too so the file also passes when run +// directly from a shell (or a git hook) that has them exported. +const GIT_LOCATION_VARS = ['GIT_DIR', 'GIT_WORK_TREE', 'GIT_INDEX_FILE', 'GIT_COMMON_DIR', 'GIT_PREFIX']; + +function scrubbedEnv(base) { + const env = { ...base }; + for (const name of GIT_LOCATION_VARS) delete env[name]; + return env; +} + function git(cwd, ...args) { - execFileSync('git', args, { cwd, stdio: 'ignore' }); + execFileSync('git', args, { cwd, stdio: 'ignore', env: scrubbedEnv(process.env) }); } function makeRepo(parent, name, remote) { @@ -86,13 +98,18 @@ function makeRepo(parent, name, remote) { fs.mkdirSync(repo, { recursive: true }); git(repo, 'init', '-q'); if (remote) git(repo, 'remote', 'add', 'origin', remote); + // An entry is trusted only while it is strictly newer than .git/config. + // Backdate the config so that holds even where mtimes (or the shell's -nt) + // only resolve whole seconds. + const past = new Date(Date.now() - 10000); + fs.utimesSync(path.join(repo, '.git', 'config'), past, past); return repo; } function baseEnv(homeDir, extra = {}) { // HOME is a fresh directory, so no global git config leaks in. The system // config and any GIT_CONFIG_* overrides from the caller are shut out too. - const env = { ...process.env, HOME: homeDir, USERPROFILE: homeDir, GIT_CONFIG_NOSYSTEM: '1' }; + const env = scrubbedEnv({ ...process.env, HOME: homeDir, USERPROFILE: homeDir, GIT_CONFIG_NOSYSTEM: '1' }); for (const name of Object.keys(env)) { if (/^GIT_CONFIG_(COUNT|KEY_\d+|VALUE_\d+|GLOBAL|SYSTEM|PARAMETERS)$/.test(name)) delete env[name]; } @@ -303,6 +320,28 @@ test('an origin rewritten by the global config invalidates the entry', () => { } }); +test('a changed .git/config forces re-detection even when the origin is unchanged', () => { + const tmp = createTempDir(); + try { + const home = path.join(tmp, 'home'); + fs.mkdirSync(home); + // No origin: nothing the origin check could notice. A setting such as + // core.worktree, which decides the project root, lives in this same file. + const repo = makeRepo(tmp, 'gamma-cfg'); + + detect(repo, home); + assert.strictEqual(detect(repo, home).hit, '1', 'an untouched config is a hit'); + + git(repo, 'config', 'core.worktree', repo); + const future = new Date(Date.now() + 5000); + fs.utimesSync(path.join(repo, '.git', 'config'), future, future); + + assert.strictEqual(detect(repo, home).hit, '0', 'a config newer than the entry must force re-detection'); + } finally { + cleanupDir(tmp); + } +}); + test('an entry holding an origin git does not report is rejected', () => { const tmp = createTempDir(); try { @@ -596,17 +635,47 @@ test('observe.sh records the same project on a cache hit as on a miss', () => { } }); -function runObserve(repo, home, payload) { +function runObserve(repo, home, payload, extraEnv = {}) { const r = spawnSync('bash', [observePath, 'pre'], { cwd: repo, input: payload, encoding: 'utf8', timeout: 60000, - env: baseEnv(home, { CLAUDE_CODE_ENTRYPOINT: 'cli', ECC_HOOK_PROFILE: 'standard', ECC_SKIP_OBSERVE: '0' }) + env: baseEnv(home, { CLAUDE_CODE_ENTRYPOINT: 'cli', ECC_HOOK_PROFILE: 'standard', ECC_SKIP_OBSERVE: '0', ...extraEnv }) }); assert.strictEqual(r.status, 0, `observe.sh should exit 0, stderr: ${r.stderr}`); } +test('observe.sh survives an observer.enabled value that contains a newline', () => { + const tmp = createTempDir(); + try { + const home = path.join(tmp, 'home'); + fs.mkdirSync(home); + const repo = makeRepo(tmp, 'nu', 'https://github.com/example/nu.git'); + // A hand-edited config whose flag spans two lines would shift the fields + // the first Python start prints, move text into the cwd, and send the + // observation to the no-project path instead of the repository. + const config = path.join(tmp, 'config.json'); + fs.writeFileSync(config, JSON.stringify({ observer: { enabled: 'false\nstray line' } })); + const payload = JSON.stringify({ session_id: 'en', cwd: repo, tool_name: 'Read', tool_input: {} }); + + runObserve(repo, home, payload, { CLV2_CONFIG: config }); + + const projectsDir = path.join(homunculusDir(home), 'projects'); + assert.ok(fs.existsSync(projectsDir), 'the observation must be stored under the repository project'); + const [projectId] = fs.readdirSync(projectsDir); + const [record] = fs + .readFileSync(path.join(projectsDir, projectId, 'observations.jsonl'), 'utf8') + .split('\n') + .filter(Boolean) + .map((line) => JSON.parse(line)); + assert.strictEqual(record.project_id, projectId); + assert.strictEqual(record.cwd, fs.realpathSync(repo), 'the recorded cwd is the payload cwd, nothing else'); + } finally { + cleanupDir(tmp); + } +}); + test('observe.sh housekeeping runs once a day whoever filled the detection cache', () => { const tmp = createTempDir(); try {