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([issue-877]): unambiguous agent-app mapping + limit:0 semantics

Adam Eivy77136ac3431c
diff --git a/server/services/citysnapshots.js b/server/services/citysnapshots.js
index 4b58f2acff..8516000afd 100644
--- a/server/services/citysnapshots.js
+++ b/server/services/citysnapshots.js
@@ -110,14 +110,30 @@ async function loadSnapshots() {
return snapshotCache;
}
+// True when `child` is `parent` itself or a path strictly nested under it.
+// Plain `startsWith` would mis-match sibling-prefix repos (/repos/proj would
+// "contain" /repos/project), so require a path boundary at the join.
+function isPathUnder(child, parent) {
+ if (child === parent) return true;
+ const base = parent.endsWith('/') ? parent : `${parent}/`;
+ return child.startsWith(base);
+}
+
// Map a CoS agent to the app it's working in by matching its workspacePath
-// against each app's repoPath — the same rule the client's `agentMap` uses.
-// workspacePath may sit on the agent or in its metadata depending on spawn path.
+// against each app's repoPath. workspacePath may sit on the agent or in its
+// metadata depending on spawn path. Unlike the client's live agentMap (which
+// recomputes every render), this mapping is persisted replay data, so it must
+// be unambiguous: match on a path boundary and let the LONGEST matching
+// repoPath win, so a nested app repo beats its parent.
function resolveAgentApp(agent, appStatuses) {
const workspacePath = agent?.workspacePath || agent?.metadata?.workspacePath;
if (!workspacePath || !Array.isArray(appStatuses)) return null;
- const match = appStatuses.find(a => a.repoPath && workspacePath.startsWith(a.repoPath));
- return match?.id ?? null;
+ let best = null;
+ for (const a of appStatuses) {
+ if (!a.repoPath || !isPathUnder(workspacePath, a.repoPath)) continue;
+ if (!best || a.repoPath.length > best.repoPath.length) best = a;
+ }
+ return best?.id ?? null;
}
/**
@@ -269,11 +285,11 @@ export async function getSnapshots({ limit, since } = {}) {
}
const total = frames.length;
- // `limit > 0` (not `>= 0`): a direct caller passing 0 means "none," but
- // slice(-0) returns the whole array — guard against that footgun. The route's
- // Zod schema already enforces limit >= 1, so this only hardens direct callers.
- if (Number.isFinite(limit) && limit > 0 && limit < frames.length) {
- frames = frames.slice(-limit); // most-recent N, still chronological
+ // A direct caller passing limit 0 means "none" — but slice(-0) returns the
+ // WHOLE array, so handle 0 explicitly. The route's Zod schema enforces
+ // limit >= 1, so this only hardens direct callers.
+ if (Number.isFinite(limit) && limit >= 0 && limit < frames.length) {
+ frames = limit === 0 ? [] : frames.slice(-limit); // most-recent N, chronological
}
return { total, snapshots: frames };
diff --git a/server/services/citysnapshots.test.js b/server/services/citysnapshots.test.js
index 63a9572d42..9da7f4a22f 100644
--- a/server/services/citysnapshots.test.js
+++ b/server/services/citysnapshots.test.js
@@ -221,6 +221,37 @@ describe('citysnapshots service', () => {
expect(sinceResult.snapshots.every(f => Date.parse(f.ts) >= Date.parse(since))).toBe(true);
});
+ it('maps an agent to the longest matching repoPath, not a sibling-prefix app', async () => {
+ const svc = await loadService({
+ sources: {
+ // App order deliberately puts the prefix-only app FIRST; boundary +
+ // longest-match must still pick the nested app the agent actually works in.
+ appStatuses: [
+ { id: 'proj', name: 'Proj', repoPath: '/repos/proj', overallStatus: 'online' },
+ { id: 'project', name: 'Project', repoPath: '/repos/project', overallStatus: 'online' },
+ ],
+ agents: [
+ { id: 'agent-1', status: 'running', workspacePath: '/repos/project/sub/dir' },
+ // A bare-prefix path that is NOT under /repos/proj (no boundary) maps to nothing.
+ { id: 'agent-2', status: 'running', workspacePath: '/repos/projectile' },
+ ],
+ },
+ });
+ const frame = await svc.captureSnapshot();
+ expect(frame.assignments).toEqual([
+ { agentId: 'agent-1', appId: 'project', status: 'running' },
+ { agentId: 'agent-2', appId: null, status: 'running' },
+ ]);
+ });
+
+ it('getSnapshots({ limit: 0 }) returns no frames (not the whole series)', async () => {
+ const svc = await loadService();
+ for (let i = 0; i < 3; i += 1) await svc.captureSnapshot();
+ const result = await svc.getSnapshots({ limit: 0 });
+ expect(result.total).toBe(3);
+ expect(result.snapshots).toEqual([]);
+ });
+
it('serializes concurrent captures so none are lost', async () => {
const svc = await loadService();
await Promise.all(Array.from({ length: 12 }, () => svc.captureSnapshot()));