Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/adr/0084-the-sidebar-orders-stacks-by-name-or-last-opened.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# ADR-0084: The sidebar orders the stacks by name or by last opened, a choice kept in the browser

- **Status:** Accepted (card in the pull request); built
- **Date:** 2026-11-02
- **Deciders:** project owner
- **Related:** [ADR-0081](0081-a-stack-has-a-page-every-lane-the-same-view.md) (the sidebar's stack rows), [ADR-0078](0078-the-hub-follows-every-stacks-worktrees.md) (every stack's trees listed), [ADR-0070](0070-a-review-card-comes-before-the-code-an-adr-records-what-was-built.md); issue #377

## Context

The sidebar listed every stack by name, and nothing moved when something
started or stopped (#374). With more than a handful of stacks, a user wants
the ones they work on near the top. Issue #377 asked for favourites, an
order of one's own (dragging, by name, last used) and hiding. Dragging was
not wanted; sorting the trees within a stack waits until this proves too
little.

## Decision

The owner approved the points in the conversation on **2026-11-02**:

| # | Category | Point | Beats | Reversing costs |
|---|---|---|---|---|
| 1 | concept | The user chooses the order of the stacks in a menu beside `+`: by name (the default) or last opened first. | Dragging, favourites, an order by state. | Low: the menu goes, a stored choice is ignored. |
| 2 | concept | The order does not change while the page is open: last opened is taken when the page loads or the choice is made. | Re-sorting on every click, a row moving under the pointer. | Low: UI code only. |
| 3 | concept | There is no order by state: a start or a stop moves nothing (as since #374); the dot tells the state. | Running first. | One more entry in the menu. |
| 4 | contract | The choice and when each stack was last opened live in the browser's `localStorage`, as the fold state does. | A setting in the hub, per machine. | Moving it to the hub later loses the values per browser. |
| 5 | concept | The trees within a stack and "Shared with you" stay ordered by name. | An order per section. | Adding a choice per section. |

As built:

1. **The menu.** A button (↕) beside `+` in the "Stacks" heading opens a
menu with "By name" and "Last opened first"; the current choice has a
check mark. Choosing takes a new snapshot of the times.
2. **Opened.** A stack counts as opened whenever it is shown (its page or
a lane's views), however it was reached: the sidebar, the switcher, the
palette, a link. The time is noted at once, and used from the next load.
3. **Never opened.** A stack never opened in this browser, or added while
the page is open, follows the opened ones, by name.
4. **Keys.** `devstack.sidebar.order` (`name` or `opened`) and
`devstack.sidebar.opened` (stack id to milliseconds). Removing a stack
from the dashboard drops its time.
5. **Code.** `ui/src/lib/stackOrder.ts`, `ui/src/components/Sidebar.vue`.
Tests: `stackOrder.test.ts`, `Sidebar.test.ts`.

## Consequences

- Each browser has its own order; two browsers on one machine can differ.
- Last opened says what this browser showed, not what an agent or the CLI
used: a stack an agent works on through MCP does not rise.
- Favourites and hiding (#377) stay open.
1 change: 1 addition & 0 deletions docs/adr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,4 @@ stay.
| [0081](0081-a-stack-has-a-page-every-lane-the-same-view.md) | A stack has a page of its own; every lane, `main` included, has the same view | Accepted (card in the pull request) |
| [0082](0082-the-ports-of-a-machine.md) | The ports of a machine: every stack says what it holds, a stack can run shifted | Accepted (card in the pull request); built: points 1 (`/v1/ports`, `/hub/ports`, `doctor`) and 2 (the graph) in 0.33.0, points 3 to 7 (the shift, the claims, `up --shift`) after it |
| [0083](0083-the-hub-takes-out-what-an-agent-switched-off-leaves.md) | The hub takes out what an agent switched off leaves: devstack's generated skills, and the folders a removed skill empties | Accepted (card in the pull request); built |
| [0084](0084-the-sidebar-orders-stacks-by-name-or-last-opened.md) | The sidebar orders the stacks by name or by last opened, a choice kept in the browser | Accepted (card in the pull request); built |
33 changes: 33 additions & 0 deletions ui/src/components/Sidebar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,37 @@ describe('the sidebar', () => {
expect(router.currentRoute.value.fullPath).toBe('/s/ab12/l/feat/events');
unmount();
});

it('orders the stacks by name, or by last opened as a snapshot that opening does not move (#377)', async () => {
const zoo: StackEntry = { ...entry, id: 'cd34', name: 'zoo', repo: '/src/zoo', running: false, daemon: null };
fake.on('/hub/stacks/cd34/worktrees', ok([]));
persist.set('sidebar.opened', { cd34: 10 });
await router.push('/pool');
const hub = useHub();
await hub.init();
fake.stream('/hub/events').handlers.onMessage({ type: 'stacks', stacks: [zoo, entry] });
const el = document.createElement('div');
document.body.appendChild(el);
const app = createApp(Sidebar);
app.use(router);
app.mount(el);
await settle();
const names = () => [...el.querySelectorAll('[data-testid="sidebar-stack"] .label')].map((x) => x.textContent?.trim());
expect(names()).toEqual(['shop', 'zoo']);

q(el, '[data-testid="sidebar-order"]').click();
await settle();
document.querySelector<HTMLElement>('[data-testid="sidebar-order-menu"] [data-order="opened"]')!.click();
await settle();
// zoo was opened here, shop never.
expect(names()).toEqual(['zoo', 'shop']);
expect(persist.get('sidebar.order')).toBe('opened');

// Opening shop notes it, and moves nothing until the page loads again.
await router.push('/s/ab12');
await settle();
expect(names()).toEqual(['zoo', 'shop']);
expect((persist.get<Record<string, number>>('sidebar.opened') ?? {}).ab12).toBeGreaterThan(10);
app.unmount();
});
});
116 changes: 111 additions & 5 deletions ui/src/components/Sidebar.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
// Stacks and their worktrees (lanes, ADR-0024) as a sidebar: every stack the
// hub knows, by name (a click opens the stack's page, ADR-0081), and under
// hub knows, by name or by when this browser last opened each, as the user
// chooses (#377; a click opens the stack's page, ADR-0081), and under
// each all of its trees, named by their branch: the lane `main` (the main
// working tree) first, then the others by name, started or not (of a stack
// whose daemon does not run, the hub lists them, ADR-0078); a click opens
Expand All @@ -21,6 +22,7 @@ import { laneLabel, lastTab, MAIN, nodeLabel, runCounts, runsAnything, started }
import { laneView, stackPage } from '@/lib/stackRoutes';
import { persist } from '@/lib/persist';
import { statusBucket } from '@/lib/status';
import { forgetOpened, noteOpened, openedTimes, orderStacks, savedOrder, saveOrder, type StackOrder } from '@/lib/stackOrder';
import { useHub } from '@/stores/hub';
import { connectedTo, usePool } from '@/stores/pool';
import { useStack } from '@/stores/stack';
Expand Down Expand Up @@ -70,8 +72,57 @@ function toggle(id: string) {
persist.set('sidebar.collapsed', collapsed.value);
}

// By name, never by state: a start or a stop moves nothing in the list.
const stacks = computed(() => [...hub.stacks].sort((a, b) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id)));
// By name or by last opened, never by state: a start or a stop moves nothing
// in the list. Last opened is the snapshot taken when the page loaded or the
// choice was made, so opening a stack does not move it under the pointer.
const order = ref<StackOrder>(savedOrder());
const openedSnapshot = ref(openedTimes());
const stacks = computed(() => orderStacks(hub.stacks, order.value, openedSnapshot.value));
// A stack shown is opened; the hub's list may arrive after the route.
watch(
() => [route.params.stack, hub.stacks.length] as const,
([id]) => {
if (typeof id === 'string' && hub.entry(id)) noteOpened(id);
},
{ immediate: true },
);
const orderMenu = ref(false);
const orderEl = ref<HTMLElement | null>(null);
const orderBtn = ref<HTMLElement | null>(null);
const orderPos = ref({ x: 0, y: 0 });
function openOrderMenu() {
const r = orderBtn.value!.getBoundingClientRect();
orderPos.value = { x: Math.max(4, r.right - 180), y: r.bottom + 2 };
orderMenu.value = true;
void nextTick(() => orderEl.value?.querySelector<HTMLButtonElement>('[aria-checked="true"]')?.focus());
}
function closeOrderMenu() {
orderMenu.value = false;
}
function chooseOrder(o: StackOrder) {
order.value = o;
saveOrder(o);
openedSnapshot.value = openedTimes();
closeOrderMenu();
}
function onOrderDown(e: PointerEvent) {
if (orderEl.value?.contains(e.target as Node) || orderBtn.value?.contains(e.target as Node)) return;
closeOrderMenu();
}
function onOrderKey(e: KeyboardEvent) {
if (e.key === 'Escape') {
e.preventDefault();
closeOrderMenu();
orderBtn.value?.focus();
}
}
watch(orderMenu, (open) => {
const add = open ? 'addEventListener' : 'removeEventListener';
document[add]('pointerdown', onOrderDown as EventListener, true);
document[add]('keydown', onOrderKey as EventListener);
window[add]('blur', closeOrderMenu);
});
onBeforeUnmount(closeOrderMenu);

/**
* The worktrees of every stack, kept across navigation: replaced when news
Expand Down Expand Up @@ -309,6 +360,7 @@ async function removeStack(s: StackEntry) {
const res = await act({ label: `Remove ${s.name}`, method: 'DELETE', path: paths.forget(s.id), success: `${s.name} removed from the dashboard` });
if (!res.ok) return;
persist.forgetStack(s.id);
forgetOpened(s.id);
delete known.value[s.id];
if (isCurrent(s)) void router.push('/');
}
Expand All @@ -326,7 +378,21 @@ async function copyPath(s: StackEntry) {
<template>
<aside class="sidebar" aria-label="Stacks and worktrees" data-testid="sidebar">
<div class="head head-row">
<span>Stacks</span>
<span class="head-label">Stacks</span>
<button
ref="orderBtn"
type="button"
class="head-btn"
:class="{ open: orderMenu }"
:title="`Order: ${order === 'name' ? 'by name' : 'last opened first'}`"
aria-label="Order of the stacks"
aria-haspopup="menu"
:aria-expanded="orderMenu"
data-testid="sidebar-order"
@click="orderMenu ? closeOrderMenu() : openOrderMenu()"
>
<Icon name="arrowUpDown" :size="14" />
</button>
<button type="button" class="head-btn" title="Add a stack…" aria-label="Add a stack" data-testid="sidebar-add-stack" @click="adding = true">
<Icon name="plus" :size="14" />
</button>
Expand Down Expand Up @@ -487,6 +553,35 @@ async function copyPath(s: StackEntry) {
</button>
</div>
<AddStackDialog :open="adding" @close="adding = false" />
<Teleport to="body">
<div
v-if="orderMenu"
ref="orderEl"
class="ctx-menu order-menu"
role="menu"
aria-label="Order of the stacks"
:style="{ left: `${orderPos.x}px`, top: `${orderPos.y}px` }"
data-testid="sidebar-order-menu"
>
<button
v-for="o in [
{ value: 'name', label: 'By name' },
{ value: 'opened', label: 'Last opened first' },
] as const"
:key="o.value"
type="button"
class="opt"
role="menuitemradio"
:aria-checked="order === o.value"
:data-order="o.value"
@click="chooseOrder(o.value)"
>
<Icon v-if="order === o.value" name="check" :size="14" />
<span v-else class="check-space" aria-hidden="true" />
{{ o.label }}
</button>
</div>
</Teleport>
<Teleport to="body">
<div
v-if="menu"
Expand Down Expand Up @@ -542,9 +637,12 @@ async function copyPath(s: StackEntry) {
.head-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 2px;
padding-right: 2px;
}
.head-label {
flex: 1;
}
.head-btn,
.more-btn {
display: grid;
Expand All @@ -559,6 +657,7 @@ async function copyPath(s: StackEntry) {
cursor: pointer;
}
.head-btn:hover,
.head-btn.open,
.more-btn:hover,
.more-btn.open {
background: var(--surface-hover);
Expand Down Expand Up @@ -763,6 +862,13 @@ ul {
.ctx-menu .opt.danger:not(:disabled) {
color: var(--danger);
}
.order-menu {
min-width: 180px;
}
.check-space {
width: 14px;
flex: none;
}
.ctx-menu .sep {
height: 1px;
margin: 4px 2px;
Expand Down
28 changes: 28 additions & 0 deletions ui/src/lib/stackOrder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { forgetOpened, noteOpened, openedTimes, orderStacks, savedOrder, saveOrder } from './stackOrder';

const s = (id: string, name: string) => ({ id, name });
const all = [s('c', 'web'), s('a', 'api'), s('b', 'worker'), s('d', 'db')];

beforeEach(() => localStorage.clear());

describe('the order of the stacks', () => {
it('is by name unless chosen otherwise', () => {
expect(savedOrder()).toBe('name');
expect(orderStacks(all, 'name', { b: 9 }).map((x) => x.name)).toEqual(['api', 'db', 'web', 'worker']);
saveOrder('opened');
expect(savedOrder()).toBe('opened');
});

it('by last opened puts the most recent first, the never opened after it by name', () => {
expect(orderStacks(all, 'opened', { b: 20, c: 30 }).map((x) => x.name)).toEqual(['web', 'worker', 'api', 'db']);
});

it('keeps when each stack was opened, and forgets a removed one', () => {
noteOpened('a', 5);
noteOpened('b', 7);
expect(openedTimes()).toEqual({ a: 5, b: 7 });
forgetOpened('a');
expect(openedTimes()).toEqual({ b: 7 });
});
});
47 changes: 47 additions & 0 deletions ui/src/lib/stackOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// The order of the stacks in the sidebar (#377): by name, or by when this
// browser last opened each. Both are kept in this browser, as the fold state
// is. "Last opened" is a snapshot taken when the page loads or the choice is
// made: opening a stack moves nothing while the page is open.
import { persist } from './persist';

export type StackOrder = 'name' | 'opened';

const ORDER_KEY = 'sidebar.order';
const OPENED_KEY = 'sidebar.opened';

export function savedOrder(): StackOrder {
return persist.get<StackOrder>(ORDER_KEY) === 'opened' ? 'opened' : 'name';
}

export function saveOrder(order: StackOrder): void {
persist.set(ORDER_KEY, order);
}

/** When this browser last opened each stack, by id (milliseconds). */
export function openedTimes(): Record<string, number> {
return persist.get<Record<string, number>>(OPENED_KEY) ?? {};
}

export function noteOpened(id: string, at = Date.now()): void {
persist.set(OPENED_KEY, { ...openedTimes(), [id]: at });
}

export function forgetOpened(id: string): void {
const all = openedTimes();
if (!(id in all)) return;
delete all[id];
persist.set(OPENED_KEY, all);
}

type Named = { id: string; name: string };

const byName = (a: Named, b: Named) => a.name.localeCompare(b.name) || a.id.localeCompare(b.id);

/**
* The stacks in the chosen order. By last opened, the most recent first; a
* stack never opened here (or added since the snapshot) follows, by name.
*/
export function orderStacks<T extends Named>(stacks: readonly T[], order: StackOrder, opened: Record<string, number>): T[] {
if (order === 'name') return [...stacks].sort(byName);
return [...stacks].sort((a, b) => (opened[b.id] ?? -1) - (opened[a.id] ?? -1) || byName(a, b));
}