Skip to content

Commit 0e410f1

Browse files
committed
fix(es-app): Work-host pins follow handoffs in the All projects view too (BUG-08)
1 parent 208777a commit 0e410f1

5 files changed

Lines changed: 79 additions & 31 deletions

File tree

‎packages/es-app/src/handoffs.test.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,31 @@ describe("Work host pins", () => {
5050
const pin = '["/mini","P"]'
5151
const rows = [{ host: "mini", id: "P", directory: "/mini" }, { host: "mini", id: "S", directory: "/mini" }]
5252
const edge = { lane: "orchestrator", predecessor: "P", successor: "S" }
53+
// One editspace holding every directory, as a single-project read gives.
54+
const one = (lineage: { edges: typeof edge[]; owners: { lane: string; owner: string }[] }) => ({ byEditspace: { fixture: lineage }, editspaceOf: () => "fixture" })
5355
test("follows a verified handoff using only that host's rows", () => {
54-
expect(reconcileHostPins("mini", [pin], { edges: [edge], owners: [{ lane: "orchestrator", owner: "S" }] }, rows))
56+
expect(reconcileHostPins("mini", [pin], one({ edges: [edge], owners: [{ lane: "orchestrator", owner: "S" }] }), rows))
5557
.toEqual(['["/mini","S"]'])
56-
expect(reconcileHostPins("mini", [pin], { edges: [edge], owners: [{ lane: "orchestrator", owner: "S" }] },
58+
expect(reconcileHostPins("mini", [pin], one({ edges: [edge], owners: [{ lane: "orchestrator", owner: "S" }] }),
5759
[{ host: "mini", id: "P", directory: "/mini" }, { host: "local", id: "S", directory: "/mini" }])).toEqual([pin])
5860
})
5961
test("retains the pin if the owner is not the tip", () => {
60-
expect(reconcileHostPins("mini", [pin], { edges: [edge], owners: [{ lane: "orchestrator", owner: "P" }] }, rows)).toEqual([pin])
62+
expect(reconcileHostPins("mini", [pin], one({ edges: [edge], owners: [{ lane: "orchestrator", owner: "P" }] }), rows)).toEqual([pin])
6163
})
6264
test("retains the pin if the successor is absent or appears twice", () => {
6365
const lineage = { edges: [edge], owners: [{ lane: "orchestrator", owner: "S" }] }
64-
expect(reconcileHostPins("mini", [pin], lineage, rows.slice(0, 1))).toEqual([pin])
65-
expect(reconcileHostPins("mini", [pin], lineage, [...rows, rows[1]!])).toEqual([pin])
66+
expect(reconcileHostPins("mini", [pin], one(lineage), rows.slice(0, 1))).toEqual([pin])
67+
expect(reconcileHostPins("mini", [pin], one(lineage), [...rows, rows[1]!])).toEqual([pin])
6668
})
6769
test("retains the pin if the predecessor has two outgoing edges or the read failed", () => {
68-
expect(reconcileHostPins("mini", [pin], { edges: [edge, { ...edge, successor: "T" }], owners: [{ lane: "orchestrator", owner: "S" }] }, rows)).toEqual([pin])
70+
expect(reconcileHostPins("mini", [pin], one({ edges: [edge, { ...edge, successor: "T" }], owners: [{ lane: "orchestrator", owner: "S" }] }), rows)).toEqual([pin])
6971
expect(reconcileHostPins("mini", [pin], undefined, rows)).toEqual([pin])
7072
})
73+
test("each pin follows only the lineage of the editspace holding its directory", () => {
74+
// Another editspace reuses the lane name; its pin (Q) has no lineage read and stays.
75+
const other = '["/other","Q"]'
76+
const lineage = { byEditspace: { mini: { edges: [edge, { lane: "orchestrator", predecessor: "Q", successor: "R" }], owners: [{ lane: "orchestrator", owner: "S" }] } },
77+
editspaceOf: (directory: string) => directory === "/mini" ? "mini" : directory === "/other" ? "other" : undefined }
78+
expect(reconcileHostPins("mini", [pin, other], lineage, [...rows, { host: "mini", id: "Q", directory: "/other" }, { host: "mini", id: "R", directory: "/other" }])).toEqual(['["/mini","S"]', other])
79+
})
7180
})

‎packages/es-app/src/handoffs.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ export const reconcilePins = (pins: string[], handoffs: Handoff[], owners: LaneO
7373
export const reconcileHostPins = (
7474
host: string,
7575
pins: string[],
76-
lineage: { edges: Handoff[]; owners: LaneOwner[] } | undefined,
76+
lineage: { byEditspace: Record<string, { edges: Handoff[]; owners: LaneOwner[] }>; editspaceOf: (directory: string) => string | undefined } | undefined,
7777
rows: { id: string; directory: string; host?: string }[],
78-
) => lineage ? reconcilePins(pins, lineage.edges, lineage.owners, rows.filter((row) => row.host === host)) : pins
78+
) => {
79+
if (!lineage) return pins
80+
const own = rows.filter((row) => row.host === host)
81+
// Each pin follows only the lineage of the editspace that holds its directory.
82+
return [...new Set(pins.flatMap((key) => {
83+
const directory = (() => { try { const value: unknown = JSON.parse(key); return Array.isArray(value) && typeof value[0] === "string" ? value[0] : undefined } catch { return undefined } })()
84+
const scope = directory === undefined ? undefined : lineage.byEditspace[lineage.editspaceOf(directory) ?? ""]
85+
return scope ? reconcilePins([key], scope.edges, scope.owners, own) : [key]
86+
}))]
87+
}

‎packages/es-app/src/host-feed.ts‎

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,15 @@ const DECISIONS_MS = 30_000
102102
const LINEAGE_MS = 60_000
103103
const SESSION_EVENTS = ["session.created", "session.updated", "session.deleted", "session.status"]
104104

105-
/** `project`: the selected project name, undefined for all projects, null while not yet known. */
106-
export const createHostFeed = (host: WorkHost, project: () => string | undefined | null) => {
105+
/** That host's handoff lineage, per editspace (lane names repeat across editspaces), and which editspace holds a
106+
* directory. `project`: the selected project, or "*" for All projects. */
107+
export type HostLineage = { project: string; byEditspace: Record<string, { edges: Handoff[]; owners: LaneOwner[] }>; editspaceOf: (directory: string) => string | undefined }
108+
109+
const MAX_LINEAGE_EDITSPACES = 4
110+
111+
/** `project`: the selected project name, undefined for all projects, null while not yet known. `pinned`: directories of
112+
* this host's pinned sessions; in All projects, lineage is read only for the editspaces that hold them. */
113+
export const createHostFeed = (host: WorkHost, project: () => string | undefined | null, pinned: () => string[] = () => []) => {
107114
const base = `/hosts/${host.id}`
108115
// Until the first read answers, show what this host last listed (if the project matches), marked by its age.
109116
const seen = (() => {
@@ -118,7 +125,7 @@ export const createHostFeed = (host: WorkHost, project: () => string | undefined
118125
...(seen ? { cachedAt: seen.cachedAt } : {}),
119126
})
120127
const [live, setLive] = createSignal<Record<string, string>>({})
121-
const [lineage, setLineage] = createSignal<{ project: string; edges: Handoff[]; owners: LaneOwner[] }>()
128+
const [lineage, setLineage] = createSignal<HostLineage>()
122129
let lastLineageRead = 0
123130
let lineageRead: Promise<void> | undefined
124131
let loading: Promise<void> | undefined
@@ -141,32 +148,47 @@ export const createHostFeed = (host: WorkHost, project: () => string | undefined
141148
.then((list) => Array.isArray(list.records) ? list.records.filter((record) => record.status === "open") : undefined)
142149
.catch(() => undefined)
143150
}
151+
const readLineage = async (es: string) => {
152+
const [handoffs, dashboard] = await Promise.all([
153+
read<{ handoffs: Handoff[] }>(`/es/api/handoffs?es=${encodeURIComponent(es)}`),
154+
read<{ editspace: string; threads: { lanes?: { lane: string; owner?: string }[] }[] }>(`/es/api/state?es=${encodeURIComponent(es)}`),
155+
])
156+
if (dashboard.editspace !== es || !Array.isArray(handoffs.handoffs) || !Array.isArray(dashboard.threads)) throw new Error("Invalid host lineage")
157+
return {
158+
edges: handoffs.handoffs,
159+
owners: dashboard.threads.flatMap((thread) => (thread.lanes ?? []).flatMap((lane): LaneOwner[] =>
160+
lane.owner ? [{ lane: lane.lane, owner: lane.owner }] : [])),
161+
}
162+
}
144163
const refreshLineage = () => {
145164
const current = scope
146-
const es = current?.es
147-
if (!es || state().status !== "ready" || lineageRead || Date.now() - lastLineageRead < LINEAGE_MS) return
165+
// One project: its editspace. All projects: the editspaces holding this host's pinned sessions, found from that
166+
// host's own editspace roots.
167+
const all = !!current && current.project === undefined
168+
const key = all ? "*" : current?.es
169+
if (!key || (all && !pinned().length) || state().status !== "ready" || lineageRead || Date.now() - lastLineageRead < LINEAGE_MS) return
148170
lastLineageRead = Date.now()
149171
setLineage(undefined)
172+
const same = () => (scope?.project === undefined ? "*" : scope?.es) === key && state().status === "ready"
150173
lineageRead = (async () => {
151174
try {
152-
const [handoffs, dashboard] = await Promise.all([
153-
read<{ handoffs: Handoff[] }>(`/es/api/handoffs?es=${encodeURIComponent(es)}`),
154-
read<{ editspace: string; threads: { lanes?: { lane: string; owner?: string }[] }[] }>(`/es/api/state?es=${encodeURIComponent(es)}`),
155-
])
156-
if (dashboard.editspace !== es || !Array.isArray(handoffs.handoffs) || !Array.isArray(dashboard.threads)) throw new Error("Invalid host lineage")
157-
if (scope?.es !== es || state().status !== "ready") return
158-
setLineage({
159-
project: es,
160-
edges: handoffs.handoffs,
161-
owners: dashboard.threads.flatMap((thread) => (thread.lanes ?? []).flatMap((lane): LaneOwner[] =>
162-
lane.owner ? [{ lane: lane.lane, owner: lane.owner }] : [])),
163-
})
175+
const roots = all
176+
? (await read<{ editspaces: { name: string; root: string }[] }>("/es/api/editspaces")).editspaces
177+
.map((item) => ({ name: item.name, base: item.root.replace(/\/+$/, "").replace(/\/\.editspace$/, "") }))
178+
: [{ name: key, base: "" }]
179+
const editspaceOf = (directory: string) => all
180+
? roots.filter((item) => directory === item.base || directory.startsWith(`${item.base}/`)).toSorted((a, b) => b.base.length - a.base.length)[0]?.name
181+
: key
182+
const names = [...new Set(pinned().flatMap((directory) => editspaceOf(directory) ?? []))].slice(0, MAX_LINEAGE_EDITSPACES)
183+
const lineages = await Promise.all((all ? names : [key]).map(async (es) => [es, await readLineage(es)] as const))
184+
if (!same()) return
185+
setLineage({ project: key, byEditspace: Object.fromEntries(lineages), editspaceOf })
164186
} catch {
165-
if (scope?.es === es) setLineage(undefined)
187+
if (same()) setLineage(undefined)
166188
}
167189
})().finally(() => {
168190
lineageRead = undefined
169-
if (scope?.es !== es) refreshLineage()
191+
if (!same()) refreshLineage()
170192
})
171193
}
172194
const run = async () => {

‎packages/es-app/src/state.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,9 @@ export function DashboardProvider(props: ParentProps) {
583583
if (info.remote) return
584584
// A host removed from the Work view (or unpaired) leaves no cached rows behind.
585585
forgetSeen(info.work.map((item) => item.id))
586-
runWithOwner(owner, () => setFeeds(info.work.filter((item) => item.id !== info.id).map((item) => createHostFeed(item, workProject))))
586+
runWithOwner(owner, () => setFeeds(info.work.filter((item) => item.id !== info.id).map((item) => createHostFeed(item, workProject, () => (hostPins()[item.id] ?? []).flatMap((key) => {
587+
try { const value: unknown = JSON.parse(key); return Array.isArray(value) && typeof value[0] === "string" ? [value[0]] : [] } catch { return [] }
588+
})))))
587589
})
588590
createEffect((previous: string | null | undefined) => {
589591
const project = workProject()
@@ -605,7 +607,8 @@ export function DashboardProvider(props: ParentProps) {
605607
for (const feed of feeds()) {
606608
const state = feed.state()
607609
const lineage = feed.lineage()
608-
if (state.status !== "ready" || !state.scoped || lineage?.project !== workProject()) continue
610+
const project = workProject()
611+
if (state.status !== "ready" || (project !== undefined && !state.scoped) || lineage?.project !== (project ?? "*")) continue
609612
const current = hostPins()[feed.host.id] ?? []
610613
const next = reconcileHostPins(feed.host.id, current, lineage, state.rows)
611614
if (next.length === current.length && next.every((key, index) => key === current[index])) continue

‎packages/es-app/test/host-pin-lineage.browser.mjs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const dir = "/Users/mini/fixture/.editspace/lanes/orchestrator/repos/o/r"
99
const pin = (id) => JSON.stringify([dir, id])
1010
const key = "host:mini:es-app-sidebar-pins"
1111
const rows = ["P", "S"].map((id) => ({ id, title: `Mini ${id}`, directory: dir, time: { created: 1, updated: Date.now() } }))
12-
await page.addInitScript(([storage, value]) => localStorage.setItem(storage, JSON.stringify([value])), [key, pin("P")])
12+
await page.addInitScript(([storage, value]) => { if (!sessionStorage.getItem("seeded")) { sessionStorage.setItem("seeded", "1"); localStorage.setItem(storage, JSON.stringify([value])) } }, [key, pin("P")])
1313
await page.route("**/__es/host", (route) => route.fulfill({ json: { id: "local", label: "Local", remote: false, hosts: [], work: [
1414
{ id: "local", label: "Local", origin: preview.fixture.url },
1515
{ id: "mini", label: "Mac Mini", origin: "http://127.0.0.1:65001", profile: "work" },
@@ -33,7 +33,12 @@ try {
3333
await page.goto(preview.url)
3434
await expect(page.locator(".sidebar .pinned-section .nav-item[data-host-key='mini:S']")).toBeVisible()
3535
expect(JSON.parse(await page.evaluate((storage) => localStorage.getItem(storage), key))).toEqual([pin("S")])
36-
console.log("PASS host pin lineage: Mini P pin follows verified handoff to S")
36+
// All projects view (BUG-08 rollover): the pin still follows, reading lineage for the editspace that holds it.
37+
await page.evaluate(([storage, value]) => { localStorage.setItem(storage, JSON.stringify([value])); localStorage.setItem("es-app-all-projects", "true") }, [key, pin("P")])
38+
await page.reload()
39+
await expect(page.locator(".sidebar .pinned-section .nav-item[data-host-key='mini:S']")).toBeVisible()
40+
expect(JSON.parse(await page.evaluate((storage) => localStorage.getItem(storage), key))).toEqual([pin("S")])
41+
console.log("PASS host pin lineage: Mini P pin follows verified handoff to S, in one project and in All projects")
3742
} finally {
3843
await browser.close()
3944
await preview.close()

0 commit comments

Comments
 (0)