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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: surface an invalid stored spec as a 400 naming the part instead of an opaque 500 (review round 2)
[._.]/ Adam Eivy35b8f13a9d14
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex d2701c38a0..adc8dfe6ed 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -72,6 +72,6 @@- **`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.-- **A Three.js part could be scaled to nothing, or reflected into a mirrored node, and the schema called both a valid transform.** Every geometry dimension in the scene spec is bounded to a positive number, but `partSchema.scale` reused the plain `finite` triple — so a provider-authored `[1, 0, 1]` collapsed the part to an invisible plane, and `[1, -1, 1]` reflected it, which three.js absorbs by flipping the front face for the negative world determinant. Neither throws, both export, and both survive a colour or silhouette check, so the resulting "the part is missing" or "the detail reads backwards" is chased anywhere except the one number that caused it. `scale` is now its own tuple floored at `1e-4` — zero, negative, and near-degenerate components are rejected with a `parts.N.scale.M` path — and the generation prompt states the bound alongside the parts shape, so the model is told to build a mirrored counterpart as its own part and to omit a part it doesn't want rendered, rather than discovering the bound through a rejected generation. Nothing in PortOS computes a scale: the value only ever arrives in the provider's JSON, no UI edits it, and no seed or reference data carries one, so the schema is the only place it can be caught. A spec an older install already stored is left exactly as it is rather than machine-repaired — the preview path never re-validates, so nothing a user is looking at changes, and the one thing that does re-validate (the standalone-factory download) now fails loudly with the offending path instead of silently un-mirroring an asymmetric part or resizing a deliberately-collapsed one into view (#3484).+- **A Three.js part could be scaled to nothing, or reflected into a mirrored node, and the schema called both a valid transform.** Every geometry dimension in the scene spec is bounded to a positive number, but `partSchema.scale` reused the plain `finite` triple — so a provider-authored `[1, 0, 1]` collapsed the part to an invisible plane, and `[1, -1, 1]` reflected it, which three.js absorbs by flipping the front face for the negative world determinant. Neither throws, both export, and both survive a colour or silhouette check, so the resulting "the part is missing" or "the detail reads backwards" is chased anywhere except the one number that caused it. `scale` is now its own tuple floored at `1e-4` — zero, negative, and near-degenerate components are rejected with a `parts.N.scale.M` path — and the generation prompt states the bound alongside the parts shape, so the model is told to build a mirrored counterpart as its own part and to omit a part it doesn't want rendered, rather than discovering the bound through a rejected generation. Nothing in PortOS computes a scale: the value only ever arrives in the provider's JSON, no UI edits it, and no seed or reference data carries one, so the schema is the only place it can be caught. A spec an older install already stored is left exactly as it is rather than machine-repaired — the preview path never re-validates, so nothing a user is looking at changes, and the one thing that does re-validate (the standalone-factory download) now answers with a 400 naming the exact part — `parts.0.children.0.scale.1` — instead of silently un-mirroring an asymmetric part or resizing a deliberately-collapsed one into view. That last part needed `buildThreejsFactorySource()` to stop calling bare `.parse`: a raw `ZodError` normalizes to an opaque 500, which is not something a user can act on, so it now routes through the shared `failValidation` the rest of PortOS speaks (#3484).- **The "Pick from Gallery" picker could only be narrowed by typing, so finding one object in one universe meant recalling a word from its prompt.** It now carries two dropdowns beside the search box: a scope select that groups every universe and media collection under `<optgroup>`s, and a type select built from the `entryCategory`/`entryKind` tags the Universe Builder stamps onto sidecars, labelled through the same `humanizeCategory` the Builder uses. Those two are separate vocabularies — a user-authored bucket can legitimately be keyed `canon` — so the type options are prefixed by which field they filter and grouped under Categories/Entry kinds rather than flattened into one list where the bucket and the stage would silently match each other. All three filters AND-combine, and both dropdowns list only universes/collections/types that actually match an image currently in the picker — a scope that can only ever produce an empty grid isn't offered, and a selection that stops matching resets itself to "All" rather than silently blanking the grid. Collection membership is matched on the stored `{ kind, ref }` pair (the same `<kind>:<ref>` string `normalizeImage` puts on `item.key`), not on a bare filename, so a collection holding video refs alongside images filters correctly. Both option fetches are best-effort: if the collections or universes call fails the picker keeps working, falling back to the universe name stamped on each sidecar, and the fetched lists are dropped on close so a reopen can't filter fresh images against stale membership. Sidecars are unvalidated JSON that also arrive from peers, so a tag that isn't a usable string is skipped rather than crashing the render — which surfaced a latent bug in the shared `humanizeCategory`: its label lookup wasn't `hasOwn`-guarded, so a user-authored category keyed `constructor` or `toString` resolved to an inherited function and handed every caller a label that throws on the next string method (#3479).diff --git a/server/lib/threejsModel.js b/server/lib/threejsModel.jsindex 41be780690..20861b52de 100644--- a/server/lib/threejsModel.js+++ b/server/lib/threejsModel.js@@ -9,6 +9,8 @@import { z } from 'zod';+import { failValidation } from './errorHandler.js';+const idSchema = z.string().trim().min(1).max(80).regex(/^[A-Za-z][A-Za-z0-9_-]*$/);const colorSchema = z.string().regex(/^#[0-9a-fA-F]{6}$/);const finite = z.number().finite().min(-10_000).max(10_000);@@ -394,7 +396,14 @@ const toIdentifier = (name) => {* Group factory. No model-authored JavaScript is executed by PortOS.*/export function buildThreejsFactorySource(input) {- const spec = threejsSculptSpecSchema.parse(input);+ // A spec is stored verbatim and re-validated only here, so this is where a+ // record an older install generated under a looser bound surfaces. A raw+ // ZodError normalizes to an opaque 500, which says nothing a user can act on;+ // `failValidation` names the offending path (`parts.0.scale.1`) in a 400, so+ // "the download fails" points at the one part to fix or regenerate.+ const parsed = threejsSculptSpecSchema.safeParse(input);+ if (!parsed.success) failValidation(parsed);+ const spec = parsed.data;const factoryName = `create${toIdentifier(spec.name)}Model`;const serialized = JSON.stringify(spec, null, 2);diff --git a/server/lib/threejsModel.test.js b/server/lib/threejsModel.test.jsindex c52481e421..95204c5cc1 100644--- a/server/lib/threejsModel.test.js+++ b/server/lib/threejsModel.test.js@@ -316,4 +316,21 @@ describe('buildThreejsFactorySource', () => {expect(source).toContain(`${channel}: definition.${channel},`);}});++ // Specs are stored verbatim and re-validated only here, so a record generated+ // by an older install under the looser `finite` scale bound reaches this parse+ // unrepaired. It must name the part rather than normalizing to an opaque 500.+ it('rejects a legacy spec with a non-positive scale as a 400 naming the part path', () => {+ const legacy = validSpec();+ legacy.parts[0].children[0].scale = [1, -1, 1];+ let thrown = null;+ try {+ buildThreejsFactorySource(legacy);+ } catch (error) {+ thrown = error;+ }+ expect(thrown?.message).toMatch(/parts\.0\.children\.0\.scale\.1/);+ expect(thrown?.status).toBe(400);+ expect(thrown?.code).toBe('VALIDATION_ERROR');+ });});