Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

test: catch the deferred barrel reaches the transition guard was blind to (review round 1)

[._.]/ Adam Eivy1d64f223ede3
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 56ca893fb5..85aef4d8c1 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -70,4 +70,4 @@
- **The CoS prompt-builder suite let the contributor's own global `CLAUDE.md` decide its results, so its suppression checks had never actually run on CI.** `buildAgentPrompt` reads `~/.claude/CLAUDE.md` off the real filesystem and splices it verbatim into the prompt, and `agentPromptBuilder.test.js` then asserts the prompt does *not* contain things like `gh pr merge`. That made every negative assertion in the file unsound in both directions: it false-failed on a machine whose personal global instructions happened to mention the string, and — worse — false-passed on a machine with no such file at all, which is exactly what CI is, so the suppression those assertions exist to prove was never exercised there. `homedir()` is now stubbed to a path that cannot exist, making the global section deterministically empty, and a new fixture-backed test closes the gap from the other side: it points the stub at a throwaway home whose global `CLAUDE.md` deliberately *does* contain the suppressed strings, asserts the fixture really is spliced in verbatim, and then asserts every section the builder itself authored still omits them on the reasoning-only path. Verified by breaking the suppression and watching the new test fail (#3475).
-- **`completeAgent` had three addresses, and the four callers that most needed one address were using the wrong one.** Continuing #3450's sequencing: `agentManagement.js`, `agentLifecycle.js`, `agentFinalization.js` and `agentCliSpawning.js` all reached the completion transition through the `cosAgents.js` barrel. These four are the callers the "move the caller above the facade" recipe cannot reach — the first two DEFINE transitions `agentOrchestrator.js` re-exports, so importing the facade from either is the cycle itself, and the other two are leaves those two must call to spawn and to finalize. So the rule they follow is the other half of the same property: above the facade's closure a transition comes from the facade, inside it from the module that DECLARES it (`cosAgentLifecycle.js`), and never from a barrel. `cleanupOrphanedAgents`'s `await import('./cos.js')` went with it — it was pulling `completeAgent` and `getAgents` through the `cos.js` re-export block while the module was already a static import three lines up, so the deferral bought no cycle protection at all. The new guard in `agentImportCycles.test.js` derives the transition names from the facade's export blocks and walks re-export edges to work out which modules forward them, so a barrel added later is covered without being named; it catches a named import, a renamed one, a namespace property read and a destructure off a namespace handle, each bypass-probed.
+- **`completeAgent` had three addresses, and the four callers that most needed one address were using the wrong one.** Continuing #3450's sequencing: `agentManagement.js`, `agentLifecycle.js`, `agentFinalization.js` and `agentCliSpawning.js` all reached the completion transition through the `cosAgents.js` barrel. These four are the callers the "move the caller above the facade" recipe cannot reach — the first two DEFINE transitions `agentOrchestrator.js` re-exports, so importing the facade from either is the cycle itself, and the other two are leaves those two must call to spawn and to finalize. So the rule they follow is the other half of the same property: above the facade's closure a transition comes from the facade, inside it from the module that DECLARES it (`cosAgentLifecycle.js`), and never from a barrel. `cleanupOrphanedAgents`'s `await import('./cos.js')` went with it — it was pulling `completeAgent` and `getAgents` through the `cos.js` re-export block while the module was already a static import three lines up, so the deferral bought no cycle protection at all. The new guard in `agentImportCycles.test.js` derives the transition names from the facade's export blocks and walks re-export edges to work out which modules forward them, so a barrel added later is covered without being named; it catches a named import, a renamed one, a namespace property read, a destructure off a namespace handle, and the three deferred spellings of the same reaches — including `const { completeAgent } = await import('./cos.js')`, the exact pattern this change deleted. Each bypass-probed.
diff --git a/server/services/agentImportCycles.test.js b/server/services/agentImportCycles.test.js
index 36f9e08597..6086a5ae3d 100644
--- a/server/services/agentImportCycles.test.js
+++ b/server/services/agentImportCycles.test.js
@@ -368,14 +368,44 @@ describe('agent lifecycle cluster — no static import cycles (#2837)', () => {
}
}
- // `import * as cos from './cos.js'` names no bindings, so the loop above
- // cannot see it — yet it hands the caller every transition the barrel
- // forwards. Banning the namespace import outright is too blunt (that same
- // handle is how a dozen modules reach the task store, which is not this
- // issue's business), so check the two ways a transition comes back OFF the
- // handle: a property read and a destructure. Both, because either one
+ // DEFERRED forms. `await import()` is invisible to the static graph — by
+ // design, it cannot produce a load-time cycle — but reaching across a
+ // blocked layer with one is this cluster's whole habit, and the exact
+ // pattern this PR deleted from `cleanupOrphanedAgents` was
+ // `const { completeAgent } = await import('./cos.js')`. A guard that only
+ // reads static syntax would go green the day someone writes it again.
+ // In a destructure the LEFT of `a: b` is the exported name, same role `as`
+ // plays in a static import.
+ for (const [, names, spec] of src.matchAll(/(?:const|let|var)\s*\{([^}]*)\}\s*=\s*(?:await\s+)?import\s*\(\s*['"]([^'"]+)['"]\s*\)/g)) {
+ const target = resolveSpec(spec);
+ if (allowed.has(target)) continue;
+ for (const entry of names.split(',')) {
+ const name = entry.split(':')[0].trim();
+ if (!declaredBy.has(name) || declaredBy.get(name).has(target)) continue;
+ offenders.push(`${file} defer-imports ${name} from ${target}`);
+ }
+ }
+ // `(await import('./cos.js')).completeAgent` — the same reach, one step
+ // shorter.
+ for (const [, spec, name] of src.matchAll(/import\s*\(\s*['"]([^'"]+)['"]\s*\)[\s)]*\.\s*(\w+)/g)) {
+ const target = resolveSpec(spec);
+ if (allowed.has(target) || !declaredBy.has(name) || declaredBy.get(name).has(target)) continue;
+ offenders.push(`${file} defer-imports ${name} from ${target}`);
+ }
+
+ // HANDLES onto a whole module — `import * as cos from './cos.js'` and its
+ // deferred twin `const cos = await import('./cos.js')`. Neither names a
+ // binding, so the loops above cannot see them, yet each hands the caller
+ // every transition the barrel forwards. Banning the handle outright is too
+ // blunt (that is how a dozen modules reach the task store, which is not
+ // this issue's business), so check the two ways a transition comes back
+ // OFF it: a property read and a destructure. Both, because either one
// alone reads green over the other.
- for (const [, ns, spec] of src.matchAll(/import\s*\*\s*as\s+(\w+)\s*from\s*['"]([^'"]+)['"]/g)) {
+ const handles = [
+ ...src.matchAll(/import\s*\*\s*as\s+(\w+)\s*from\s*['"]([^'"]+)['"]/g),
+ ...src.matchAll(/(?:const|let|var)\s+(\w+)\s*=\s*(?:await\s+)?import\s*\(\s*['"]([^'"]+)['"]\s*\)/g),
+ ];
+ for (const [, ns, spec] of handles) {
const target = resolveSpec(spec);
if (!forwarders.has(target)) continue;
for (const name of declaredBy.keys()) {