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
refactor([issue-2928]): resolve walk runs per selection entry instead of scanning grok/ only
[._.]/ Adam Eivy1ffe1be8a591
diff --git a/server/services/sprites/walk.js b/server/services/sprites/walk.jsindex e8780ee448..b4ee25bc02 100644--- a/server/services/sprites/walk.js+++ b/server/services/sprites/walk.js@@ -42,6 +42,12 @@ const selectionRelPath = (id) => `walk/${id}-walk-selection-v1.json`;export const walkSetRelPath = (id) => `walk/${id}-walk-set-v1.json`;const runRelPath = (runId) => `grok/${runId}`;const RUN_RECORD_NAME = 'animation-run.json';+// The two on-disk layouts that carry a full `animation-run.json` record:+// PortOS's own generations write `grok/<run-id>/`, while the source-pipeline+// importer (#2895) preserves its own `runs/<run-id>/` tree. Same reader, same+// record shape — only the prefix differs. Kept in sync with paths.js's+// RUN_RAW_INTERMEDIATE, which excludes raw frames under either prefix.+const RUN_RECORD_LAYOUT = /^(grok|runs)\/[^/]+/;// Serialize walk-state read-modify-writes per record (run records, the// selection file, the walk set, and trim versioning share one lifecycle) —@@ -70,8 +76,14 @@ function seedSelection(recordId) {};}+// Read a run record by its directory (record-relative), so `grok/<id>/` and+// the importer's `runs/<id>/` share one reader.+async function loadRunRecordAt(recordId, runDirRel) {+ return readJSONFile(join(spriteDir(recordId), runDirRel, RUN_RECORD_NAME), null);+}+async function loadRunRecord(recordId, runId) {- return readJSONFile(join(spriteDir(recordId), runRelPath(runId), RUN_RECORD_NAME), null);+ return loadRunRecordAt(recordId, runRelPath(runId));}async function saveRunRecord(recordId, run) {@@ -175,45 +187,75 @@ async function loadRedrawRun(recordId, direction, entry) {};}+/**+ * Resolve the run behind ONE selection/walk-set entry (issue #2928). The+ * entry names its own storage layout via `runPath`/`runManifest`, so this is+ * the single dispatch point every walk source routes through — adding a+ * fourth layout means another branch here, not another append-and-dedupe+ * block in `getWalkState`:+ *+ * grok/<run-id>/ → PortOS's own generations (animation-run.json)+ * runs/<run-id>/ → the source-pipeline importer's layout (same record)+ * anything else → an imagegen/vN redraw manifest (#2924), synthesized+ *+ * Every branch returns the same run-shaped object, so routes and the client+ * are layout-agnostic. Returns null when the entry names nothing readable.+ */+async function loadRunForEntry(recordId, direction, entry) {+ // The run directory names the layout; fall back to the manifest's own path+ // for an entry that carries only a manifest.+ const layoutPath = toRecordRelativeAssetPath(recordId, entry.runPath)+ || toRecordRelativeAssetPath(recordId, entry.runManifest);+ if (!layoutPath) return null;+ const runDirRel = RUN_RECORD_LAYOUT.exec(layoutPath)?.[0];+ if (runDirRel) {+ const run = await loadRunRecordAt(recordId, runDirRel);+ return run ? normalizeStripPreview(recordId, run) : null;+ }+ return loadRedrawRun(recordId, direction, entry);+}+/*** Walk-workflow view for the detail endpoint: every animation run (newest* first), the per-direction selection, and the finalized set when present.+ *+ * The selection (or the finalized walk set) is the index: each approved+ * direction resolves through `loadRunForEntry`, whatever layout it names.+ * The `grok/` directory scan then only has to cover runs that have NO+ * selection entry by definition — unapproved candidates and in-flight+ * generations, which PortOS always writes under `grok/`.*/export async function getWalkState(recordId) {- const grokDir = join(spriteDir(recordId), 'grok');- let entries = [];- try {- entries = await readdir(grokDir, { withFileTypes: true });- } catch {- // no runs yet- }- const [runs, selection, walkSet] = await Promise.all([- Promise.all(- entries- .filter((e) => e.isDirectory() && e.name.startsWith('walk-'))- .map((e) => loadRunRecord(recordId, e.name)),- ).then((loaded) => loaded- .filter(Boolean)- .map((run) => normalizeStripPreview(recordId, run))),- loadSelection(recordId),- loadWalkSet(recordId),- ]);-- // Directions approved from an imported redraw manifest have no grok run- // record to scan — synthesize their preview from the manifest (#2924).+ const [selection, walkSet] = await Promise.all([loadSelection(recordId), loadWalkSet(recordId)]);+const approvedDirections = walkSet?.directions || selection?.directions || {};- const scannedRunIds = new Set(runs.map((run) => run.id));- const redrawRuns = (await Promise.all(+ const entryRuns = (await Promise.all(Object.entries(approvedDirections)// Gate on `approved` because loadRedrawRun stamps that status// unconditionally — a rejected/pending entry must not surface as an- // approved run next to live Generate/Approve buttons.- .filter(([, entry]) => entry?.status === 'approved' && entry.runId && entry.runManifest- && !scannedRunIds.has(entry.runId))- .map(([direction, entry]) => loadRedrawRun(recordId, direction, entry)),+ // approved run next to live Generate/Approve buttons. A rejected grok+ // entry still surfaces via the scan below, with its own real status.+ .filter(([, entry]) => entry?.status === 'approved' && entry.runId+ && (entry.runPath || entry.runManifest))+ .map(([direction, entry]) => loadRunForEntry(recordId, direction, entry)),)).filter(Boolean);+ const resolvedRunIds = new Set(entryRuns.map((run) => run.id));- const allRuns = [...runs, ...redrawRuns]+ let dirEntries = [];+ try {+ dirEntries = await readdir(join(spriteDir(recordId), 'grok'), { withFileTypes: true });+ } catch {+ // no native runs yet+ }+ const scannedRuns = (await Promise.all(+ dirEntries+ .filter((e) => e.isDirectory() && e.name.startsWith('walk-') && !resolvedRunIds.has(e.name))+ .map((e) => loadRunRecord(recordId, e.name)),+ )).filter(Boolean)+ .filter((run) => !resolvedRunIds.has(run.id))+ .map((run) => normalizeStripPreview(recordId, run));++ const allRuns = [...entryRuns, ...scannedRuns].sort((a, b) => runCreatedAtMs(b.createdAt) - runCreatedAtMs(a.createdAt));return { runs: allRuns, selection, walkSet };}diff --git a/server/services/sprites/walk.test.js b/server/services/sprites/walk.test.jsindex b4608d2e04..53bb42f791 100644--- a/server/services/sprites/walk.test.js+++ b/server/services/sprites/walk.test.js@@ -417,6 +417,95 @@ describe('getWalkState', () => {expect(await readFile(legacyPath)).toEqual(legacyBytesBefore);});+ // Issue #2928: the importer also lays source-pipeline runs down under+ // `runs/<run-id>/` rather than `grok/<run-id>/`. The selection entry names+ // the layout, so resolving runs off the entry (not off a grok-only+ // directory scan) surfaces it with no per-layout special case.+ it('resolves an approved direction whose runPath is under runs/<run-id>/', async () => {+ const id = await characterWithLockedAnchors(newId(), ['east']);+ const runId = 'walk-east-imported1';+ const stripRel = `runs/${runId}/generated/${id}-walk-east-strip.png`;+ const runDir = join(TEST_ROOT, 'sprites', id, 'runs', runId, 'generated');+ await mkdir(runDir, { recursive: true });+ await writeFile(join(runDir, `${id}-walk-east-strip.png`), 'imported-strip');+ await writeFile(join(TEST_ROOT, 'sprites', id, 'runs', runId, 'animation-run.json'), JSON.stringify({+ schemaVersion: 1,+ kind: 'grok-game-animation-frames-run',+ status: 'candidate',+ id: runId,+ characterId: id,+ direction: 'east',+ createdAt: 1700000000.123456,+ stripPreview: {+ // Source-repo-anchored, exactly as the importer copies it.+ path: `art-source/sprites/${id}/${stripRel}`,+ frameCount: 8, fps: 12, cellWidth: 384, cellHeight: 384, row: 0, startColumn: 0,+ },+ }));+ await mkdir(join(TEST_ROOT, 'sprites', id, 'walk'), { recursive: true });+ await writeFile(join(TEST_ROOT, 'sprites', id, 'walk', `${id}-walk-selection-v1.json`), JSON.stringify({+ schemaVersion: 1,+ kind: 'reviewed-directional-walk-selection',+ characterId: id,+ status: 'in-progress',+ directions: {+ east: {+ status: 'approved',+ runId,+ runPath: `art-source/sprites/${id}/runs/${runId}`,+ runManifest: `art-source/sprites/${id}/runs/${runId}/generated/manifest.json`,+ approvedAt: '2026-07-01T00:00:00.000Z',+ },+ },+ }));++ const { runs } = await getWalkState(id);+ expect(runs).toHaveLength(1);+ expect(runs[0]).toMatchObject({+ id: runId,+ direction: 'east',+ kind: 'grok-game-animation-frames-run',+ // The record's own source-repo-anchored stripPreview is normalized the+ // same way a grok/-layout imported run's is.+ stripPreview: { stripPath: stripRel, frameCount: 8 },+ });+ });++ // A `runs/`-layout entry whose record is missing on disk must surface+ // nothing rather than a half-shaped run — the grok scan can't cover it.+ it('surfaces nothing for a runs/<run-id>/ entry with no run record on disk', async () => {+ const id = await characterWithLockedAnchors(newId(), ['east']);+ await mkdir(join(TEST_ROOT, 'sprites', id, 'walk'), { recursive: true });+ await writeFile(join(TEST_ROOT, 'sprites', id, 'walk', `${id}-walk-selection-v1.json`), JSON.stringify({+ schemaVersion: 1,+ kind: 'reviewed-directional-walk-selection',+ characterId: id,+ status: 'in-progress',+ directions: {+ east: {+ status: 'approved', runId: 'walk-east-ghost001', runPath: 'runs/walk-east-ghost001', approvedAt: 'x',+ },+ },+ }));+ expect((await getWalkState(id)).runs).toEqual([]);+ });++ // Unapproved candidates and in-flight generations have no selection entry+ // by definition, so the grok/ scan is still what surfaces them alongside+ // the entry-resolved approved run.+ it('unions entry-resolved approved runs with unapproved scanned grok runs', async () => {+ const id = await characterWithLockedAnchors(newId(), ['east', 'west']);+ const { runId: approvedRunId } = await makeCandidateRun(id, 'east');+ await approveWalkDirection(id, { direction: 'east', runId: approvedRunId });+ const { runId: pendingRunId } = await startWalkGeneration(id, { direction: 'west' });++ const { runs } = await getWalkState(id);+ expect(runs).toHaveLength(2);+ expect(runs.map((r) => r.id).sort()).toEqual([approvedRunId, pendingRunId].sort());+ // The approved run is not duplicated by the scan.+ expect(runs.filter((r) => r.id === approvedRunId)).toHaveLength(1);+ });+// Issue #2924: a direction approved from the source pipeline's video-first// imagegen REDRAW path has no grok/ run directory at all — its selection// entry points at imagegen/vN. Synthesize the preview from that manifest's