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

fix([extract-mergeexpandintodraft-draft-result-from]): address codex review iter-1

[._.]/ Adam Eivy621e5038d531
diff --git a/client/src/lib/universeBuilderExpand.js b/client/src/lib/universeBuilderExpand.js
index 664c6febce..ac6fdf8b0c 100644
--- a/client/src/lib/universeBuilderExpand.js
+++ b/client/src/lib/universeBuilderExpand.js
@@ -26,7 +26,12 @@
// - Variations/sheets aren't lock-scoped, but per-item `locked: true` rows
// are preserved and merged ahead of LLM output.
// - Canon arrays merge by name/slugline/alias collision (existing wins).
-import { mergeInfluencesWithLocks } from '../services/api';
+// Imports the influence-merge from the targeted apiUniverseBuilder module
+// (NOT the global `services/api` barrel) — the helpers are pure functions
+// that happen to be exported alongside HTTP wrappers, and tracing through
+// the barrel pulls all ~40 service modules into this lib's dep graph,
+// defeating the point of a `lib/` vs `services/` boundary.
+import { mergeInfluencesWithLocks } from '../services/apiUniverseBuilder';
import { BIBLE_LIMITS } from './bibleLimits';
import { normalizeSlugline } from './scenePrompt';
@@ -178,18 +183,18 @@ const mergeCategoriesWithLocks = (draftCategories, llmCategories, preservedVaria
return mergedCategories;
};
-// Composite-sheet merge follows the same locked-first + dedupe-by-label pattern
-// as variations. Labels are case-insensitive; sheets without a label are dropped
-// (mirrors mergeVariations' missing-key handling).
+// Composite-sheet merge: preserved (locked) sheets keep their slot at the top
+// unconditionally — the upstream invariant (only `locked: true` rows reach
+// here) means a missing label is a bug elsewhere, not a row to silently drop.
+// Their labels still seed the dedupe Set so a fresh LLM sheet with the same
+// label is suppressed. Mirrors the pre-extraction handleExpand behavior
+// byte-for-byte (a malformed preserved sheet with no `label` would have
+// crashed the pre-refactor code on `.toLowerCase()` — keeping the same fault
+// surface so this is a pure boundary move, not a behavior change).
const mergeCompositeSheets = (preservedSheets, llmSheets) => {
- const seen = new Set();
- const out = [];
- for (const s of preservedSheets || []) {
- const key = s?.label?.toLowerCase();
- if (!key || seen.has(key)) continue;
- seen.add(key);
- out.push(s);
- }
+ const preserved = preservedSheets || [];
+ const seen = new Set(preserved.map((s) => s.label.toLowerCase()));
+ const out = [...preserved];
for (const s of llmSheets || []) {
const key = s?.label?.toLowerCase();
if (!key || seen.has(key)) continue;
diff --git a/client/src/lib/universeBuilderExpand.test.js b/client/src/lib/universeBuilderExpand.test.js
index b4249e71f3..f430efbe84 100644
--- a/client/src/lib/universeBuilderExpand.test.js
+++ b/client/src/lib/universeBuilderExpand.test.js
@@ -219,6 +219,20 @@ describe('mergeExpandIntoDraft', () => {
expect(expandedDraft.categories.factions.kind).toBe('characters');
});
+ it('locked composite sheets without a label crash on merge (pre-refactor byte-equivalence)', () => {
+ // The pre-extraction handleExpand code did
+ // `new Set(preservedCompositeSheets.map((s) => s.label.toLowerCase()))`
+ // which throws TypeError when a locked sheet lacks `label`. We preserve
+ // that fault surface so the refactor is a behavioral no-op — the upstream
+ // invariant (only `locked: true` rows reach here) means a label-less
+ // preserved sheet is a producer bug, not a row to silently drop. Test
+ // pins the throw so a future "defensive" rewrite is loud.
+ const draft = draftFixture({
+ compositeSheets: [{ locked: true /* missing label */ }],
+ });
+ expect(() => mergeExpandIntoDraft(draft, {})).toThrow(TypeError);
+ });
+
it('locked composite sheets survive; LLM dups dropped by case-insensitive label', () => {
const draft = draftFixture({
compositeSheets: [