Repository navigation
fix(sidebar): nest a delegated remote worker under its coordinator #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
2fc332e
67f213e
0537cf2
4b5d038
96c76d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,6 +84,40 @@ export function isFederatedDispatchRelayEligible( | |
| ) | ||
| } | ||
|
|
||
| export type DelegatedWorktreePlacementRow = { | ||
| dispatch_id: string | ||
| environment_id: string | ||
| remote_worktree_id: string | ||
| creator_handle: string | null | ||
| creator_pane_key: string | null | ||
| } | ||
|
|
||
| /** | ||
| * Where each remote worker landed, and which coordinator terminal sent it. | ||
| * | ||
| * Settled Dispatches are included on purpose: a released worker keeps its | ||
| * workspace, and its card should not jump out of the tree when it finishes. | ||
| * Newest first, so the cap keeps the most recent placements. | ||
| */ | ||
| export function listDelegatedWorktreePlacements( | ||
| this: OrchestrationDb, | ||
| limit: number | ||
| ): DelegatedWorktreePlacementRow[] { | ||
| const rows = this.db | ||
| .prepare( | ||
| `SELECT fd.dispatch_id, fd.environment_id, fd.remote_worktree_id, | ||
| dc.creator_handle, dc.creator_pane_key | ||
| FROM federated_dispatches fd | ||
| INNER JOIN dispatch_contexts dc ON dc.id = fd.dispatch_id | ||
| WHERE fd.remote_worktree_id IS NOT NULL | ||
| ORDER BY fd.rowid DESC | ||
| LIMIT ?` | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Once the database contains more than 500 newer federated dispatch rows, this unconditional limit omits older placements even when their remote workspaces are still visible, causing those cards to jump back to top level. Repeated dispatches onto one shared workspace make this especially easy because duplicate placements consume the entire cap; deduplicate/filter by current child workspace before applying the limit so historical rows cannot evict unrelated live edges. Useful? React with 👍 / 👎. |
||
| ) | ||
| .all(limit) | ||
| // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the SELECT list names exactly these five columns. | ||
| return rows as DelegatedWorktreePlacementRow[] | ||
| } | ||
|
Comment on lines
+102
to
+119
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 performance · medium Per-graph-sync SQL scan of unbounded dispatch table Every 📋 Prompt for AI AgentsTwo changes: (1) in src/main/runtime/orchestration/db/schema/create-graph-tables-sql.ts add a partial index |
||
|
|
||
| export function updateFederatedDispatchResources( | ||
| this: OrchestrationDb, | ||
| params: { | ||
|
|
@@ -129,6 +163,7 @@ export type FederatedDispatchStoreMethods = { | |
| getFederatedDispatch: typeof getFederatedDispatch | ||
| listFederatedDispatchesByIds: typeof listFederatedDispatchesByIds | ||
| listActiveFederatedDispatches: typeof listActiveFederatedDispatches | ||
| listDelegatedWorktreePlacements: typeof listDelegatedWorktreePlacements | ||
| findNextTerminalFederatedDispatchPendingAcknowledgment: typeof findNextTerminalFederatedDispatchPendingAcknowledgment | ||
| isFederatedDispatchRelayEligible: typeof isFederatedDispatchRelayEligible | ||
| updateFederatedDispatchResources: typeof updateFederatedDispatchResources | ||
|
|
@@ -140,6 +175,7 @@ export function attachFederatedDispatchStore(ctor: { prototype: object }): void | |
| getFederatedDispatch, | ||
| listFederatedDispatchesByIds, | ||
| listActiveFederatedDispatches, | ||
| listDelegatedWorktreePlacements, | ||
| findNextTerminalFederatedDispatchPendingAcknowledgment, | ||
| isFederatedDispatchRelayEligible, | ||
| updateFederatedDispatchResources, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { RuntimeDelegatedWorktreeEdgeProjection } from './runtime-delegated-worktree-edge-projection' | ||
|
|
||
| type Placement = { | ||
| dispatch_id: string | ||
| environment_id: string | ||
| remote_worktree_id: string | ||
| creator_handle: string | null | ||
| creator_pane_key: string | null | ||
| } | ||
|
|
||
| function projection(args: { | ||
| placements?: Placement[] | ||
| worktreeByHandle?: Record<string, string> | ||
| handleByPaneKey?: Record<string, string> | ||
| db?: 'missing' | 'without-query' | ||
| }): RuntimeDelegatedWorktreeEdgeProjection { | ||
| const db = | ||
| args.db === 'missing' | ||
| ? null | ||
| : args.db === 'without-query' | ||
| ? {} | ||
| : { listDelegatedWorktreePlacements: () => args.placements ?? [] } | ||
| return new RuntimeDelegatedWorktreeEdgeProjection({ | ||
| getDb: () => db, | ||
| getWorktreeId: (handle) => args.worktreeByHandle?.[handle] ?? null, | ||
| getHandleForPaneKey: (paneKey) => args.handleByPaneKey?.[paneKey] ?? null | ||
| }) | ||
| } | ||
|
|
||
| const placement: Placement = { | ||
| dispatch_id: 'ctx_1', | ||
| environment_id: 'env-1', | ||
| remote_worktree_id: 'repo-remote::/home/ubuntu/worker', | ||
| creator_handle: 'term_coordinator', | ||
| creator_pane_key: 'pane-1' | ||
| } | ||
|
|
||
| describe('RuntimeDelegatedWorktreeEdgeProjection', () => { | ||
| it('pairs the remote worktree with the coordinator terminal that dispatched it', () => { | ||
| const edges = projection({ | ||
| placements: [placement], | ||
| worktreeByHandle: { term_coordinator: 'repo-1::/home/alex/coordinator' } | ||
| }).build() | ||
| expect(edges).toEqual({ | ||
| known: true, | ||
| edges: [ | ||
| { | ||
| parentWorktreeId: 'repo-1::/home/alex/coordinator', | ||
| childHostId: 'runtime:env-1', | ||
| childWorktreeId: 'repo-remote::/home/ubuntu/worker', | ||
| dispatchId: 'ctx_1' | ||
| } | ||
| ] | ||
| }) | ||
| }) | ||
|
|
||
| it('falls back to the pane key when the handle was reminted by a restart', () => { | ||
| const edges = projection({ | ||
| placements: [placement], | ||
| handleByPaneKey: { 'pane-1': 'term_reminted' }, | ||
| worktreeByHandle: { term_reminted: 'repo-1::/home/alex/coordinator' } | ||
| }).build() | ||
| expect(edges.known && edges.edges[0]?.parentWorktreeId).toBe('repo-1::/home/alex/coordinator') | ||
| }) | ||
|
|
||
| it('drops a placement whose coordinator terminal is gone', () => { | ||
| expect(projection({ placements: [placement] }).build()).toEqual({ known: true, edges: [] }) | ||
| }) | ||
|
|
||
| it('keeps an edge whose coordinator and worker share a bare id on different hosts', () => { | ||
| const edges = projection({ | ||
| placements: [placement], | ||
| worktreeByHandle: { term_coordinator: placement.remote_worktree_id } | ||
| }).build() | ||
| expect(edges).toEqual({ | ||
| known: true, | ||
| edges: [ | ||
| { | ||
| parentWorktreeId: placement.remote_worktree_id, | ||
| childHostId: 'runtime:env-1', | ||
| childWorktreeId: placement.remote_worktree_id, | ||
| dispatchId: 'ctx_1' | ||
| } | ||
| ] | ||
| }) | ||
| }) | ||
|
|
||
| it('separates a db that cannot answer from a db that answers zero', () => { | ||
| expect(projection({ db: 'missing' }).build()).toEqual({ known: false }) | ||
| expect(projection({ db: 'without-query' }).build()).toEqual({ known: false }) | ||
| expect(projection({ placements: [] }).build()).toEqual({ known: true, edges: [] }) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { toRuntimeExecutionHostId } from '../../shared/execution-host' | ||
| import type { DelegatedWorktreeEdge } from '../../shared/worktree/delegated-worktree-edge' | ||
| import type { DelegatedWorktreePlacementRow } from './orchestration/db/federation/federated-dispatch-store' | ||
|
|
||
| /** Enough for any realistic sidebar; the renderer drops edges whose rows it cannot see anyway. */ | ||
| const MAX_DELEGATED_EDGES = 500 | ||
|
|
||
| /** Only the placement query, so a test — and an older db without it — needs no stand-in for the rest. */ | ||
| type DelegatedWorktreePlacementSource = { | ||
| listDelegatedWorktreePlacements?: (limit: number) => DelegatedWorktreePlacementRow[] | ||
| } | ||
|
|
||
| type DelegatedWorktreeEdgeDependencies = { | ||
| getDb(): DelegatedWorktreePlacementSource | null | ||
| /** Worktree the coordinator terminal runs in, by handle. */ | ||
| getWorktreeId(handle: string): string | null | ||
| /** Handles are minted per process; after a restart only the pane identity still names the terminal. */ | ||
| getHandleForPaneKey(paneKey: string): string | null | ||
| } | ||
|
|
||
| /** | ||
| * What this runtime knows about its coordinated placements right now. | ||
| * | ||
| * `known: true` with no edges is a real answer — this runtime coordinated | ||
| * nothing. A db that is not up yet, or predates the placement query, cannot | ||
| * answer at all, and publishing that as emptiness would wipe live edges from | ||
| * every reader until the db returns. | ||
| */ | ||
| export type DelegatedWorktreeEdgeProjectionResult = | ||
| | { known: true; edges: DelegatedWorktreeEdge[] } | ||
| | { known: false } | ||
|
|
||
| /** | ||
| * The coordinator side of a cross-host delegation, for the sidebar. | ||
| * | ||
| * Only this runtime can build it: the worker host knows nothing about the | ||
| * coordinator's workspace, and the edge would fail the same-repository, | ||
| * same-host rule that git lineage enforces there. | ||
| */ | ||
| export class RuntimeDelegatedWorktreeEdgeProjection { | ||
| constructor(private readonly deps: DelegatedWorktreeEdgeDependencies) {} | ||
|
|
||
| build(): DelegatedWorktreeEdgeProjectionResult { | ||
| const db = this.deps.getDb() | ||
| if (!db?.listDelegatedWorktreePlacements) { | ||
| return { known: false } | ||
| } | ||
| const edges: DelegatedWorktreeEdge[] = [] | ||
| for (const placement of db.listDelegatedWorktreePlacements(MAX_DELEGATED_EDGES)) { | ||
| // No bare-id comparison here: ids collide across hosts, and a genuine | ||
| // self-reference is only decidable once both ends are host-qualified, | ||
| // which resolveDelegatedWorktreeNesting already does. | ||
| const parentWorktreeId = this.resolveCoordinatorWorktreeId(placement) | ||
| if (!parentWorktreeId) { | ||
| continue | ||
| } | ||
| edges.push({ | ||
| parentWorktreeId, | ||
| childHostId: toRuntimeExecutionHostId(placement.environment_id), | ||
| childWorktreeId: placement.remote_worktree_id, | ||
| dispatchId: placement.dispatch_id | ||
| }) | ||
| } | ||
| return { known: true, edges } | ||
|
Comment on lines
+48
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 bug · medium 500-placement cap evicts live edges over time
📋 Prompt for AI AgentsIn src/main/runtime/runtime-delegated-worktree-edge-projection.ts line 8 and src/main/runtime/orchestration/db/federation/federated-dispatch-store.ts lines 102-119, make the placement query retention-aware instead of a bare |
||
| } | ||
|
|
||
| private resolveCoordinatorWorktreeId(placement: { | ||
| creator_handle: string | null | ||
| creator_pane_key: string | null | ||
| }): string | null { | ||
| const byHandle = placement.creator_handle | ||
| ? this.deps.getWorktreeId(placement.creator_handle) | ||
| : null | ||
| if (byHandle) { | ||
| return byHandle | ||
| } | ||
| const handle = placement.creator_pane_key | ||
| ? this.deps.getHandleForPaneKey(placement.creator_pane_key) | ||
| : null | ||
| return handle ? this.deps.getWorktreeId(handle) : null | ||
|
Comment on lines
+71
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 bug · medium Pane-key fallback rebinds settled placements to wrong coordinator The projection resolves the coordinator worktree by 📋 Prompt for AI AgentsIn src/main/runtime/runtime-delegated-worktree-edge-projection.ts lines 67-81, tighten |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,6 +63,7 @@ const WorktreeList = React.memo(function WorktreeList({ | |
| const worktreeMap = useWorktreeMap() | ||
| const repos = useAppStore((s) => s.repos) | ||
| const worktreeLineageById = useAppStore((s) => s.worktreeLineageById) | ||
| const delegatedWorktreeEdges = useAppStore((s) => s.delegatedWorktreeEdges) | ||
| const workspaceLineageByChildKey = useAppStore((s) => s.workspaceLineageByChildKey) | ||
| const detectedWorktreesByRepo = useAppStore((s) => s.detectedWorktreesByRepo) | ||
| const activeWorktreeId = useAppStore((s) => s.activeWorktreeId) | ||
|
|
@@ -163,6 +164,7 @@ const WorktreeList = React.memo(function WorktreeList({ | |
| repoMap, | ||
| worktreeMap, | ||
| worktreeLineageById, | ||
| delegatedWorktreeEdges, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the coordinator's repo section or lineage fold is collapsed, selecting its delegated worker through pending reveal or the agent-send picker cannot expose the card. The edge is passed only to row construction here, while Useful? React with 👍 / 👎. |
||
| prCache, | ||
| settings, | ||
| workspaceStatuses, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,10 @@ import type { AppState } from '../../../../store/types' | |
| import { LOCAL_EXECUTION_HOST_ID } from '../../../../../../shared/execution-host' | ||
| import type { ExecutionHostId } from '../../../../../../shared/execution-host' | ||
| import { getWorktreeHostIdentity } from '../../../../../../shared/worktree/host-qualified-identity' | ||
| import { getCyclicProjectedWorktreeLineageIds } from '../../worktree-lineage-projection' | ||
| import { | ||
| getCyclicProjectedWorktreeLineageIds, | ||
| getLineageRenderInfo | ||
| } from '../../worktree-lineage-projection' | ||
| import { ALL_GROUP_KEY, ALL_GROUP_META } from './group-keys' | ||
| import { appendOrderedGroups } from './group-sections' | ||
| import type { SectionAppendContext } from './group-sections' | ||
|
|
@@ -43,6 +46,8 @@ import type { | |
| WorktreeGroupBy | ||
| } from './row-types' | ||
| import { getRenderedNaturalAnchorRepoIds, withRepoSectionDisplayLabels } from './section-order' | ||
| import { resolveDelegatedWorktreeNesting } from './delegated-worktree-nesting' | ||
| import type { DelegatedWorktreeEdge } from '../../../../../../shared/worktree/delegated-worktree-edge' | ||
| import { buildOrderedGroups } from './worktree-grouping' | ||
|
|
||
| export function buildRows( | ||
|
|
@@ -70,7 +75,8 @@ export function buildRows( | |
| folderWorkspaces: readonly FolderWorkspace[] = [], | ||
| hostLabelById?: ReadonlyMap<string, string>, | ||
| defaultHostId: ExecutionHostId = LOCAL_EXECUTION_HOST_ID, | ||
| pinnedDisplayPolicy: PinnedWorktreeDisplayPolicy = getPinnedWorktreeDisplayPolicy(settings) | ||
| pinnedDisplayPolicy: PinnedWorktreeDisplayPolicy = getPinnedWorktreeDisplayPolicy(settings), | ||
| delegatedWorktreeEdges: readonly DelegatedWorktreeEdge[] = [] | ||
| ): Row[] { | ||
| const result: Row[] = [] | ||
| const projectIndex = buildProjectGroupingIndex(projectGrouping) | ||
|
|
@@ -80,6 +86,25 @@ export function buildRows( | |
| const cyclicLineageIds = nestLineage | ||
| ? getCyclicProjectedWorktreeLineageIds(lineageById, worktreeMap) | ||
| : new Set<string>() | ||
| // Sections are per repo and repo ids are per host, so a delegated child is | ||
| // rendered in its coordinator's section; anywhere else there is no parent row | ||
| // to nest under. Status and PR lanes keep lane membership, as lineage does. | ||
| const delegatedNesting = | ||
| nestLineage && delegatedWorktreeEdges.length > 0 | ||
| ? resolveDelegatedWorktreeNesting({ | ||
|
Comment on lines
+92
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With the default single-location pinned policy, pinning the coordinator does not pull its delegated child into the Pinned section because Useful? React with 👍 / 👎. |
||
| worktrees, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When either endpoint is an exact folder workspace—a supported federated placement—the edge can never match because only AGENTS.md reference: AGENTS.md:L93-L95 Useful? React with 👍 / 👎. |
||
| edges: delegatedWorktreeEdges, | ||
| // Not defaultHostId: the edges arrive on this window's own graph sync, so their | ||
| // coordinator is always a local row, while the focused host follows the host filter. | ||
| homeHostId: LOCAL_EXECUTION_HOST_ID, | ||
| getLineageParentIdentity: (worktree) => { | ||
| const info = getLineageRenderInfo(worktree, lineageById, worktreeMap, cyclicLineageIds) | ||
| return info.state === 'valid' ? getWorktreeHostIdentity(info.parent) : undefined | ||
| } | ||
| }) | ||
| : undefined | ||
|
Comment on lines
+92
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 bug · medium Delegated child of pinned coordinator renders unnested A delegated worker whose coordinator worktree is pinned is anchored by 📋 Prompt for AI AgentsIn src/renderer/src/components/sidebar/worktree-list/grouping/build-rows.ts around lines 92-107, delegated nesting does not account for pinned coordinators: |
||
| const sectionAnchorByChildIdentity = | ||
| groupBy === 'repo' ? delegatedNesting?.sectionAnchorByChildIdentity : undefined | ||
|
|
||
| const pendingByRepo = new Map<string, PendingCreationRef[]>() | ||
| for (const creation of pendingCreations) { | ||
|
|
@@ -183,7 +208,8 @@ export function buildRows( | |
| groupDepth: 0, | ||
| sectionKey: ALL_GROUP_KEY, | ||
| hostContextLabelByWorktreeIdentity: mixedWorktreeHostContextLabels, | ||
| cyclicLineageIds | ||
| cyclicLineageIds, | ||
| delegatedParentIdentityByChildIdentity: delegatedNesting?.parentIdentityByChildIdentity | ||
| }) | ||
| for (const pair of [...renderableFolderWorkspaces].sort((left, right) => | ||
| compareFolderWorkspacesForDisplay(left.folderWorkspace, right.folderWorkspace) | ||
|
|
@@ -209,7 +235,8 @@ export function buildRows( | |
| pendingByRepo, | ||
| repoOrder, | ||
| projectOrderBy, | ||
| folderWorkspaces: renderableFolderWorkspaces | ||
| folderWorkspaces: renderableFolderWorkspaces, | ||
| sectionAnchorByChildIdentity | ||
| }) | ||
|
|
||
| const sectionContext: SectionAppendContext = { | ||
|
|
@@ -229,7 +256,8 @@ export function buildRows( | |
| lineageById, | ||
| worktreeMap, | ||
| nestLineage, | ||
| cyclicLineageIds | ||
| cyclicLineageIds, | ||
| delegatedParentIdentityByChildIdentity: delegatedNesting?.parentIdentityByChildIdentity | ||
|
Comment on lines
+259
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the user explicitly selects two or more hosts, Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| if (groupBy !== 'repo' || projectGroups.length === 0) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a remote worker becomes ready after the renderer's last graph publication,
updateFederatedDispatchResourceschanges only SQLite and does not schedule another renderer graph sync, while this projection is sampled only insidesyncWindowGraph. The remote-worktree refresh can therefore make the worker card appear at top level, where it remains until an unrelated tab, title, or status event triggers another sync; publish or invalidate the edge projection when the placement is written.Useful? React with 👍 / 👎.