SLOPSHOPPER

Storybloq

Project memory for Claude Code and Codex. Storybloq stores tickets, issues, handovers, and lessons in a .story/ folder inside your repo.

panebandspinnerrowsguardprompt
★ 777v1.16.0PolyForm-Shield-1.0.0updated 2026-10-19Storybloq/storybloq/plugins/storybloq
A shopper browsing a rack in a slop shop
README

<img src="https://raw.githubusercontent.com/Storybloq/storybloq/main/assets/logo.png" width="120" alt="Storybloq logo" />

<h1 align="center">Storybloq</h1>

<strong>Your project’s memory. Your agents’ workflow.</strong><br /> Project memory and workflows for Claude Code and Codex. Keep stories, plans, handovers, and review evidence beside your code. Pick up work across sessions and follow progress in the optional Mac app.

<a href="https://www.npmjs.com/package/@storybloq/storybloq"><img src="https://img.shields.io/npm/v/@storybloq/storybloq?color=333&label=npm" alt="npm version" /></a> <a href="https://github.com/Storybloq/storybloq/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-PolyForm--Shield%201.0-blue" alt="License" /></a> <img src="https://img.shields.io/badge/node-%E2%89%A520-brightgreen" alt="Node" /> <img src="https://img.shields.io/badge/claude%20code-compatible-orange" alt="Claude Code compatible" /> <img src="https://img.shields.io/badge/codex-compatible-111" alt="Codex compatible" />

<a href="https://storybloq.com">storybloq.com</a> · <a href="https://storybloq.com/mac">Mac app</a> · <a href="https://github.com/Storybloq/lenses">Review lenses</a> · <a href="https://storybloq.com/privacy">Privacy</a>


The problem

A new coding session may be missing the decisions and unfinished work from the last. Project instructions describe how to work; handovers, stories, and review records capture what happened and where to continue.

The real cost isn't wasted setup time. It's repeated mistakes, relitigated design decisions, hallucinated context, and linear instead of compounding work.

The idea

Every project gets a .story/ directory of JSON and markdown files. Stories, issues, roadmap phases, session handovers, and lessons learned live there as readable files you can track with Git. Stories use ticket records in the CLI and file format.

  • CLI: storybloq - inspect and mutate .story/ from the terminal.
  • MCP server: structured tools Claude Code and Codex can call directly to read and update the project and guide supported workflows.
  • Skill: invoke /story in Claude Code or $story in Codex to load project state at the start of a session.
  • Mac app: native sidebar that watches .story/ and updates live while your AI client works (separate product, free on the App Store).

From choosing work to the next session

  • Choose the next useful story. Recommendations account for blockers, unfinished work, and what completing a story would unblock. Recorded decisions and a ranked digest of lessons give the agent context for the choice.
  • Build against a reviewed plan. Autonomous mode guides planning, configured independent reviews, tests, and finalization. Approved plan snapshots preserve the approach that advanced to implementation. Confirmed review findings can become durable issues.
  • See what was approved. Know what was checked. Gate acknowledgments in duet workflows can pin acceptance to a plan or staged code state. These records help you inspect the work; they do not guarantee correct code or replace tests, CI, and release checks.
  • Continue with the record intact. Handovers preserve decisions and next steps. Persisted session state supports guarded continuation; automatic recovery depends on the client and the interruption.

A lesson is a recorded pattern or mistake, with reinforcement to help useful guidance surface again. An approval records acceptance of particular work, within the workflow that requested it. Coordination records participants and assignments; the client or configured transport supplies dispatch and messages. Recovery uses saved state and checks before continuing, rather than assuming an interrupted action finished.

Start with one story and a handover. Add autonomous mode, coordination workflows, or federation when the project needs them.

Install

npm install -g @storybloq/storybloq@latest
storybloq setup --client all

Requires Node.js 20+ and at least one AI client: Claude Code or Codex CLI 0.130.0+. Package lives on npm at @storybloq/storybloq; releases are tagged on this repo at github.com/Storybloq/storybloq/releases.

setup --client all installs the Storybloq skill for Claude and Codex, registers this package as an MCP server, and configures available client hooks. It also registers the bundled codex-claude-bridge review backend as the codex-bridge MCP server when Codex is installed; the bridge ships as an optional dependency of this package, so a copy you registered yourself is left alone and storybloq health launches whichever one is registered and reports whether it answers. The bridge needs a Codex CLI login to review (or agy for its Gemini failover); without one it registers but reviews fail. Re-running it is safe. Codex reports installed hooks with trust unknown; open /hooks in Codex to review and trust them. setup-skill remains as a compatibility alias for Claude-only setup.

Setup also installs the ledger dashboard for Claude Code. On version 2.1.272 or newer, a project with a .story/ directory draws its tickets, issues, and progress in a pane beside the transcript, with no extra flags on the install command and nothing to configure. Projects without .story/ show nothing. Turn the dashboard off with /plugin configure storybloq. The context figure in the footer is how full the session is toward its automatic compaction point, not a share of the model's window, so it reaches 100% when compaction is due; it refreshes during a turn as tools run, at most every five seconds.

While a /story auto session runs, the dashboard shows where it is. The In progress card the session is working, a ticket or, while it fixes an issue, that issue, carries its stage after the id, as in T-001 [Code] Native canvas, document objects, and local saving. When no card on screen is the session's own (nothing picked yet, the item not in progress, or cut off by the column's limit), the In progress heading carries the stage instead, as in In progress 1 [Code]. There is no footer line for the session. The stage comes from .story/status.json alone. A status older than 12 hours, the presence TTL, is drawn dimmed with a question mark ([Code?]) because nothing has confirmed it since. When the pane is narrow the title is cut first, and the tag is dropped before the id is shortened or the heading's count is cut.

Your first two sessions

  1. Open your project in Claude Code or Codex. Type /story in Claude Code chat or $story in Codex chat. For a new project, the skill guides you through setup.
  2. Ask: “Record a story to add an empty calendar state. Record the decision that the API owns availability.”
  3. Before stopping, ask: “Write a handover.”
  4. Start a new chat and invoke /story or $story again to load the recorded project context.

See the work

The optional Mac app shows stories, progress, and handovers from your project files. Explore the Mac app or follow the tutorials.

Add structure as your work grows

Start with one project and one agent. Add autonomous workflows, independent review, or connected repositories when the work calls for them. Review records preserve what was checked; they do not guarantee correctness. Tests, CI, and release checks still matter.

The CLI and MCP server are source-available under PolyForm Shield. Your coding client and optional review backends have their own data handling and costs. See the privacy policy and the license below.

Upgrading

npm install -g @storybloq/storybloq@latest
storybloq setup --client all

Same two commands as a fresh install: @latest pulls the newest version, and re-running setup refreshes the Storybloq skill files, re-registers the MCP server, and sweeps any stale hook entries from prior installs.

You'll usually see a one-line banner on the next storybloq invocation whenever a newer version is on npm:

storybloq v1.2.0 is available (you have v1.1.6).
Update: npm install -g @storybloq/storybloq@latest

The CLI also silently refreshes the skill dir and migrates any legacy hook entries (for example, from the pre-rename @anthropologies/claudestory package) on the first run after an upgrade — no manual cleanup needed.

Alternative install via the Claude Code plugin system: see Storybloq/plugin-archive (legacy path; storybloq setup --client all is the recommended install).

Codex plugin marketplace

For the unpublished local board candidate, use the dedicated local plugin installer. The repository marketplace below installs the older skill integration, not that board candidate.

codex plugin marketplace add https://github.com/Storybloq/storybloq
codex plugin add storybloq@storybloq

This installs the Storybloq skill only. It does not run this package's npm install, register the MCP server, or configure hooks -- those still need the CLI, which the skill's own bootstrap step installs for you the first time you invoke it: on your first $story, if the CLI or MCP server isn't set up yet, the skill runs npm install -g @storybloq/storybloq@latest followed by storybloq setup --client codex --skip-skill for you. --skip-skill skips re-copying the skill files, since the plugin already manages that copy -- passing it yourself only matters if you're driving storybloq setup directly instead of letting the skill's bootstrap step do it.

If you already have a standalone Codex skill copy from a prior direct storybloq setup --client codex (at ~/.agents/skills/story/ and/or ~/.codex/skills/story/), installing the marketplace plugin on top of it is an untested configuration, not a verified-harmless one -- Codex's handling of two same-named skill providers isn't something this project has tested. Recommended migration: move the existing copy OUT of the skills root rather than deleting it. A backup left under ~/.agents/skills/ (for example story.bak) still contains a SKILL.md and can be discovered as a second skill, which is the duplicate this procedure exists to remove, so park it one level up instead: mkdir -p ~/.agents/story-skill-backups && mv ~/.agents/skills/story ~/.agents/story-skill-backups/story.$(date +%s), and the same for ~/.codex/skills/story into ~/.codex/story-skill-backups/ if present. Restart Codex and confirm $story still works via the plugin-managed copy. To roll back, move the backup to its original path. Only then, separately and at your own discretion, remove the backup.

If the CLI or MCP server is still missing after installing the plugin and invoking $story once, run the bootstrap command yourself: npm install -g @storybloq/storybloq@latest && storybloq setup --client codex --skip-skill.

Bootstrap a project

cd your-project
storybloq init --name "your-project"

For multi-repo projects, see Federation below.

That scaffolds:

.story/
├── config.json         project config + recipe overrides
├── roadmap.json        phase ordering + metadata
├── tickets/            T-001.json, T-002.json, ...
├── issues/             ISS-001.json, ISS-002.json, ...
├── notes/              N-001.json, N-002.json, ...
├── lessons/            L-001.json, ...
├── handovers/          YYYY-MM-DD-<slug>.md
└── snapshots/          state snapshots (gitignored)

Commit everything except .story/snapshots/.

<img src="https://raw.githubusercontent.com/Storybloq/storybloq/main/assets/board.png" alt="Ticket board showing phases, tickets, and in-progress work" />

Guided setup and verification

On a project without .story/, /story (or $story) plans the project with you and writes it in one engine call, storybloq setup-apply, after you approve the package. storybloq setup-report --setup <id> shows the setup as it is now: one line per setup checkpoint (ready, or waiting on its blockers) and one per recipe stage the level uses (on, or off and why). --historical prints the report frozen when the setup finished.

A setup can propose a pointer block for CLAUDE.md and AGENTS.md. The engine never writes either file: it leaves <file>.storybloq-<setup>.proposed beside it and stops (pointer edit owed, one stop per file) until the block is there (applied) or you keep the file as it is (keep-current). A .proposed file setup did not write is a proposal conflict: it is never adopted or removed.

A stage the setup could not prove yet is established by a ticket that only storybloq setup-verify --ticket <id> completes. It runs the approved command and, on success, turns the stage on in .story/config.json:

StageVerified whenOutcomes that write nothing (exit)
testthe runner's summary reports at least one test and no failuresfailed or no tests (1), evidence unreadable (4: adapter, leave-off or stop)
buildthe command exits 0 within its deadline, nothing it started is left in its process group when it exits, and the group is confirmed gonefailed, timed out or cleanup failed (1: stop), unsupported (4: leave-off or stop)
verify127.0.0.1:<port><path> answers 2xx from a listener the command's process group owns, checked before the request and after the response, with the command itself still running and everything within its startup deadline, then the group stops and the port is freeport occupied, exited early, not ready, foreign listener, ownership unknown, shutdown failed or cleanup failed (1: stop), unsupported (4: leave-off or stop)

Every outcome that can be answered prints (run <hex>); answer it with storybloq setup-verify --ticket <id> --resolve <adapter|leave-off|stop> --owner-words '<words>' --run <hex>. A change to the ticket, its prerequisites, the record or the stages since that run makes the answer stale (exit 5): run setup-verify again. Build and verify are unsupported on Windows (no process groups to own), and verify needs lsof; leave them off there. Ownership is observed, not proven: a foreign process that answers the request and leaves between the two observations, while the command's own listener stays bound, is not detected.

A project created by setup uses checkpoint schema 5: open it with CLI 1.16 or later and the matching Mac app.

Daily use

Inside Claude Code or Codex:

  • /story in Claude Code or $story in Codex - loads project status, reads the latest handover, surfaces open tickets and issues, lists blocked work, summarizes recent changes. When the client can run background agents and the actionable backlog is large, it also surfaces the orchestrate working style proactively (a recommendation, still gated by explicit opt-in).
  • /story auto T-001 T-002 ISS-013 / $story auto T-001 T-002 ISS-013 - autonomous mode scoped to those items. Drives a ticket through plan -> plan review -> implement -> tests -> code review -> commit with handovers at each checkpoint.
  • /story review T-001 / $story review T-001 - runs the multi-lens review (see Storybloq/lenses) against a ticket's diff.
  • /story orchestrate / $story orchestrate - drives a multi-repo (or large single-repo) backlog when the client exposes exact callable workflow/subagent tools. Codex uses multi_agent_v1.spawn_agent, its normalized multi_agent_v1__spawn_agent identifier, or an exact spawn_agent tool. The Claude Agent View-backed storybloq dispatch command is shipped; a product-managed Codex dispatch backend is not.
  • /story triage / $story triage - read-only triage of the open issue backlog: verifies each finding against the pinned current HEAD, flags already-fixed and duplicate issues, groups issues that share one verified root cause, and recommends a prioritized ticket plan. Mutates no issue and no ticket.
  • /story bus / $story bus - polls a task-bound local Bus endpoint so an implementer and an independent reviewer can exchange advisory findings without copy and paste.
  • /story handover / $story handover - writes a session handover capturing decisions, blockers, and next steps.

Both clients support context loading, autonomous mode, MCP, and compaction/status hooks. Codex Desktop can open an autonomous session's owning task and relay an exact owner response to it; Codex CLI safely falls back to a manual task switch. Autonomous code review defaults to a 12-round landing cap (clamped upward by ticket risk): unresolved critical findings and rejects still block, while non-blocking findings become follow-up issues at the cap. Set recipeOverrides.stages.CODE_REVIEW.maxReviewRounds to 0 for unlimited, which disables the cap and the ceiling below it alike. Otherwise, three rounds past the cap a hard ceiling ends the session: the outstanding findings are filed as issues, the work is left uncommitted in the tree, and a handover is written. The item returns to open when the session still owns its claim; if the claim has moved, the item is left exactly as it is.

recipeOverrides.compactThreshold accepts medium, high (default), or critical. The value selects both the pressure limits and the rotation trigger: medium uses lower limits and rotates at medium pressure, while critical uses higher limits and waits for critical pressure. At a clean COMPLETE boundary, threshold pressure ends the bounded session through HANDOVER because Storybloq cannot invoke a client compaction command. When the client itself compacts, the PreCompact and SessionStart hooks preserve the same session; pressure resets only after SessionStart confirms source: compact.

Outside the AI client, the same state is one storybloq invocation away.

Usage-limit auto-resume

Claude Code sessions stop at usage limits ("You've hit your usage limit"), and overnight autonomous work silently dies with them. Storybloq detects the stop through Claude Code's StopFailure hook, parses the reset time from the session transcript, records the stop in a global ledger (~/.claude/storybloq/limit-ledger.json), and resumes the session when the limit resets. On by default once hooks are installed.

  • Autonomous sessions are parked on the same recovery lane as compaction and woken headlessly through the full state machine -- ownership rebind, git-HEAD validation, and recovery mapping all apply, so a wake after the workspace changed is validated, not blindly replayed. Sessions stopped mid-FINALIZE are never auto-resumed (commit replay is not proven safe); you get a notification with manual recovery steps instead.
  • Plain sessions get a desktop notification at reset with the exact claude --resume command. Per-project opt-in (limitResume.plainMode: "headless") wakes them headlessly instead.
  • Permission posture is never escalated. A session that ran with --dangerously-skip-permissions is only woken with that flag if the project explicitly opts in (limitResume.inheritBypass: true); otherwise it notifies.

The wake is driven by a transient detached waker process, not a daemon: it polls the ledger every 30 seconds, resumes what is due (attempt-capped, staggered, concurrency-bounded), and exits when nothing is pending. It survives laptop sleep but not reboot or logout -- after a reboot, the next storybloq invocation or hook fire in any project respawns it, so weekly-scale waits recover on your next activity. That trade is the cost of "no daemon."

Inspect and manage the queue with storybloq limit-status (--cancel <key> destroys a pending auto-resume, --requeue <key> retries a stood-down record). Disable globally with {"limitResume": {"enabled": false}} in ~/.claude/storybloq/config.json, or per project via limitResume in .story/config.json (also maxAttempts, staggerMs, maxConcurrent, notify, and more).

Prior art: the detection-and-reparse approach is modeled on unsnooze (MIT), which pioneered transcript-based limit detection and reset-time parsing for tmux-hosted sessions. Storybloq's version drops the tmux layer in favor of the documented hook surface and resumes autonomous sessions through its own state machine instead of a keystroke.

Storybloq Bus

Storybloq Bus connects two task-bound endpoints in one local checkout. One may implement while the other reviews, but routing follows the paired endpoints rather than role names. Runtime messages live under gitignored .story/bus/; confirmed findings remain canonical Storybloq issues.

Run setup from each participating client task, using its actual client identity. For example, inside a Codex CLI task:

storybloq bus setup --client codex --surface codex_cli --delivery poll
storybloq bus status
storybloq bus endpoint list

Setup initializes the local runtime and binds the current task id (normally discovered from the client environment). If discovery is unavailable, supply the validated client task id with --task-id; do not invent one. A second task runs setup in the same checkout to become the peer. Re-running setup is resumable. bus init and bus join remain lower-level commands; the legacy role argument to join does not select a message destination.

--delivery poll uses explicit polling. The default, --delivery live, requires supported client hooks and configures guarded delivery. Live injection also depends on the session’s available transport. bus auto-attach on is a separate project opt-in for later sessions. Neither configuration guarantees an inactive peer reads a message immediately.

Messages are hash-chained, idempotent, bounded, and delivered through recoverable recipient mailboxes. Critical notices participate in ship checks and require canonical issue handling. A message is peer advice; it never grants owner approval to merge, push, deploy, spend, or perform destructive actions. bus redeliver can move a hop-cap-parked message to a successor thread using its recorded content.

Optional idle wake: bus setup --wake idle opts a supported Codex CLI endpoint into a best-effort wake attempt after mail is committed. It requires a reachable compatible Codex app-server, an idle thread, and proven ownership. This implementation accepts app-server version 0.153.4; other versions are refused. It cannot wake Codex Desktop or Claude endpoints. Omit --wake to preserve an existing policy or pass --wake never to disable it. There is no Bus retry daemon: a failed attempt waits for a later send, and a wake request is not proof of receipt. bus endpoint list reports the policy and last outcome.

Claude usage-limit recovery, described above, is a separate mechanism. It is not Bus message delivery and does not establish equivalent usage-limit recovery in Codex.

<img src="https://raw.githubusercontent.com/Storybloq/storybloq/main/assets/autonomous.png" alt="Autonomous mode running a ticket through plan, implement, test, review" />

Federation

Federation coordinates AI agent work across multiple repos. One project becomes the orchestrator. It declares which repos (nodes) are part of the system, how they depend on each other, and how they communicate at runtime. Each node keeps its own .story/ with its own tickets, issues, and handovers. The orchestrator reads across all of them.

# Create an 
Source 10 files
hooks/mod.ts 39 lines
1/**
2 * The storybloq plugin's one hooks module (Claude Code function hooks).
3 *
4 * hooks/hooks.json names exactly this file: the client admits one module per
5 * plugin, so every storybloq Mod registers through here. Each Mod lives in
6 * its own file and is gated by its own `userConfig` option:
7 *
8 *   - sidebar.ts  T-508, the ledger dashboard (option `sidebar`, on by default)
9 *
10 * The client loads this module only under CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1
11 * and after workspace trust; a Mod whose option is off registers nothing, so
12 * it costs no hook on any event. Event names are string literals at every
13 * on() call, as the client's source scan requires; the pinned list they are
14 * checked against is client-api.ts.
15 *
16 * The sidebar landed at 72f98679 (T-508). The roster Mod (T-507) was removed
17 * by owner ruling on 2026-09-15: one dashboard Mod; the roster core stays.
18 */
19
20import { registerSidebar } from "./sidebar.js";
21
22export type Options = Readonly<Record<string, string | number | boolean | readonly string[]>>;
23type Hook = ($: any, e: any, next: (e: any) => unknown) => unknown;
24export type On = (event: string, hook: Hook) => unknown;
25
26/**
27 * `register(on, options)`: the entry the client calls once per activation.
28 *
29 * T-516: the dashboard is on unless the user turned it off, so the test is
30 * "not false" rather than "is true". 2.1.273 does hand userConfig defaults
31 * through (measured: a probe module saw its declared defaults with nothing
32 * configured), which alone would be enough; reading an absent option as on
33 * keeps a client that does not pass defaults from silently hiding the Mod.
34 */
35export function register(on: On, options: Options): void {
36  const sidebar = options["sidebar"] !== false;
37  if (sidebar) registerSidebar(on, options);
38}
39
hooks/sidebar.ts 1280 lines
1import { createDashboardState, type DashboardState, type ScanItem, type CachedRecord } from "./dashboard-state.js";
2import { inProgressBoard, compactLineNode, paneText, panePlacement, boardLayout, rowBudget, compactBoard, narrowBoard, boardNode, headerNode, contextNode, footerNode, contextLabel } from "./dashboard-view.js";
3export { contextLabel, MOD_VERSION } from "./dashboard-view.js";
4import { wroteLedger } from "./ledger-write-detection.js";
5import { cellWidth, truncate } from "./terminal-text.js";
6import { displaySafe, statusField } from "./stage-label.js";
7/**
8* T-508: the ledger sidebar Mod. Draws `.story/` beside the transcript.
9*
10* Read-only by construction: it reads the ledger through `$.fs` and never
11* writes it. The write path stays the CLI and the MCP server, as the ticket
12* requires, and `claude plugin validate` prints the calls this module makes so
13* a `$.fs.write` added here would show up in a list the tests compare.
14*
15* WHERE THE NUMBERS COME FROM. `sidebar-projection.ts`, which the repo's own
16* vitest holds equal to `storybloq status --compact`. Nothing in this file
17* counts anything; it reads files, caches what it read, and draws.
18*
19* WHY THE SCAN IS CHUNKED. The compact numbers come from the whole
20* file-per-item ledger, which on a mature project is a couple of thousand
21* files, and `.story/status.json` is not a projection of them (it is a session
22* flag, four fields). A hook that read them all in one go would sit on the
23* client's budget, so the first pass runs in chunks on `$.clock.every` and the
24* pane says how many files are left. Afterwards `$.fs.stat` is the only cost
25* for a file that has not changed: the cache in `$.store` is keyed by path to
26* its mtime and the handful of fields the pane shows, so a later session
27* starts warm and a refresh re-reads only what moved.
28*
29* WHY IT POLLS. Nothing tells a session that another process wrote the
30* ledger: a peer session, the Mac app, a git pull and the CLI in a terminal
31* all leave this Mod's events silent, so the pane used to sit on the last
32* projection until this session next finished a turn (T-517). The client
33* exposes no `$.fs.watch`, so the refresh is a poll on the timer that is
34* already running: four `$.fs.stat` calls every two seconds, and nothing
35* further unless one of those four mtimes moved.
36*
37* WIDTH AND PLACEMENT (ISS-1247, ISS-1251; the 2.1.277 declarations). Two
38* client rules, neither ours to set. WHERE a pane sits is the renderer's: the
39* fullscreen (alternate-screen) layout docks it beside the transcript from 110
40* columns, the main-screen layout seats it inline above the prompt at any
41* width; `e.props.placement` says which on every Pane render. WHETHER it draws
42* is judged at each `$.ui.open`: an open answering the person's input (a
43* prompt they entered, a command, a press) is placed at any width; one the
44* plugin makes on its own waits undrawn below 144 columns (110 once asked).
45* The `session.start` open is the plugin's own, so a session started narrow
46* shows the one `AbovePrompt` line instead, and the person's first prompt
47* re-opens the pane (`prompt.submit`), which the client then places.
48*
49* LAYOUT FOLLOWS PLACEMENT (ISS-1254). A docked pane is a sidebar: tall and
50* never wider than 90 columns, so its three columns always stack one under
51* another, whatever its width (the 1.15.4 sidebar the owner asked back after
52* 1.15.5 squeezed four frames side by side at 150 columns and drew only the
53* narrow board at 132). An inline pane is a strip above the prompt: the three
54* columns side by side from BOARD_MIN_COLUMNS up, the narrow board below.
55* A client that reports no placement gets the width rule alone, as 1.15.4
56* did. Colour follows placement too (ISS-1255): the client paints a docked
57* pane's background from its theme, so text there gets a colour for that
58* theme; an inline pane sits on the terminal's own background, so its text
59* takes the terminal's default foreground, which is the one that matches.
60*
61* EVENT NAMES AND `$`. Every event name is a string literal at its `on()` call
62* and every call is spelled `$.noun.member(...)` inline, because the client
63* reads both from this source rather than from a manifest. `client-api.ts` is
64* the documentary pin the tests compare that reading against.
65*/
66import { logoFrame, logoLayout, LOGO_DURATION_MS } from "./storyfield-logo.js";
67import { DashboardMotion } from "./dashboard-motion.js";
68import type { On } from "./mod.js";
69import { extractRecord, projectSidebar, type SidebarIssue, type SidebarRecord, type SidebarTicket, } from "./sidebar-projection.js";
70type Options = Readonly<Record<string, string | number | boolean | readonly string[]>>;
71/** The pane's id. Also the `requestId` its `ui.render` and `ui.close` carry. */
72const PANE_ID = "storybloq";
73const PANE_TITLE = "Storybloq";
74/**
75* The narrowest terminal the client will place a plugin's OWN open into, from
76* the API's rule: an unasked open "waits undrawn below 144 columns (110 once
77* asked)". The `session.start` open is that kind, so below this a narrow
78* start has no pane and the one-line fallback draws instead; the person's
79* first prompt re-opens it as an open answering their input, which is placed
80* at any width (ISS-1251). 110 was the wrong floor and left 110-143 columns
81* with nothing drawn at all (ISS-1235). This is not the dock width: whether
82* a placed pane docks or sits inline is the renderer's call (ISS-1247).
83*/
84const DOCK_COLUMNS = 144;
85const BAND_HINT = ", board opens at your next prompt";
86const STORE_KEY = "sidebar-ledger-cache-v1";
87/** Under the store's 4 MiB, with room for whatever else the plugin keeps. */
88const STORE_BUDGET_BYTES = 3000000;
89/** Files per tick, and the tick, so no single dispatch sits on the budget. */
90const SCAN_CHUNK = 25;
91const SCAN_TICK_MS = 25;
92/**
93* T-517: ticks between idle polls, so eighty of a 25 ms tick is two seconds.
94*
95* Exported because the Mod's own tests count ticks against it, and a poll
96* interval they had to restate as a number would drift from this one.
97*/
98export const IDLE_POLL_TICKS = 80;
99/**
100* How many cards a column ever draws, and the line that stands for the rest.
101*
102* A fixed six, by the owner's ruling, and not a figure derived from
103* `props.scroll.bodyRows`. The derived cap is what produced the bug the owner
104* hit live: a Done column of 27 was headed 27 correctly, drew 18 rows and
105* showed no tail, because the pane clips at `bodyRows` and the tail WAS drawn,
106* below the cut, along with the issues and handover lines under it. Six
107* bounds every column's body at seven rows whatever the pane reports, so the
108* board is the same height on every terminal and nothing is silently cut. It
109* was eight until the owner asked for the height back.
110*
111* The tail is three dots and not "+19 more": the heading already carries the
112* true total, so the tail only has to say that the column goes on.
113*/
114const LEDGER_DIR = ".story";
115const TICKETS_DIR = ".story/tickets";
116const ISSUES_DIR = ".story/issues";
117const HANDOVERS_DIR = ".story/handovers";
118const CONFIG_PATH = ".story/config.json";
119const ROADMAP_PATH = ".story/roadmap.json";
120const STATUS_PATH = ".story/status.json";
121/** T-537: the setup hint's action, in the Claude Code profile's command. */
122export const SETUP_HINT = "Run /story to set this project up; the pane opens when it finishes.";
123/** T-532: tool calls read the context fill at most this often. */
124const CONTEXT_REFRESH_MS = 5000;
125function forgetEverything(dashboard: DashboardState): void {
126  dashboard.cache = {};
127  dashboard.cacheLoaded = false;
128  dashboard.projection = null;
129  dashboard.project = "";
130  dashboard.phases = [];
131  dashboard.handoverFilenames = [];
132  dashboard.queue = [];
133  dashboard.idleTicks = 0;
134  dashboard.polledMtimes = {};
135  dashboard.scanInitializing = false;
136  dashboard.scanActive = false;
137  dashboard.pendingRefresh = false;
138  dashboard.ticking = false;
139  dashboard.timerStarted = false;
140  dashboard.logoElapsed = 0;
141  dashboard.logoStarted = false;
142  dashboard.logoFinished = false;
143  dashboard.logoTicks = 0;
144  dashboard.motion = new DashboardMotion();
145  dashboard.bandDrawn = false;
146  dashboard.sidebarEnabled = false;
147  dashboard.paneOpen = false;
148  dashboard.paneDrawn = false;
149  dashboard.reopenAsked = false;
150  dashboard.themeLight = false;
151  dashboard.paneInline = false;
152  dashboard.sessionActive = false;
153  forgetSession(dashboard);
154  dashboard.contextPercent = null;
155  dashboard.contextReadAt = null;
156  dashboard.warm = false;
157  dashboard.uiAvailable = true;
158  dashboard.noLedger = false;
159  dashboard.saidNoUi = false;
160  dashboard.saidNoLedger = false;
161  dashboard.saidScanFailed = false;
162  dashboard.saidRootUnresolved = false;
163  dashboard.initialCwd = null;
164  dashboard.ledgerRoot = null;
165}
166function isTicketRecord(record: SidebarRecord): record is SidebarTicket {
167  return record.kind === "ticket";
168}
169function isIssueRecord(record: SidebarRecord): record is SidebarIssue {
170  return record.kind === "issue";
171}
172/** Rebuilds the projection from whatever the cache holds right now. */
173function reproject(dashboard: DashboardState, completedScan = false): void {
174  const tickets: SidebarTicket[] = [];
175  const issues: SidebarIssue[] = [];
176  for (const entry of Object.values(dashboard.cache)) {
177    if (isTicketRecord(entry.record))
178      tickets.push(entry.record);
179    else if (isIssueRecord(entry.record))
180      issues.push(entry.record);
181  }
182  dashboard.projection = projectSidebar({ project: dashboard.project, phases: dashboard.phases, tickets, issues, handoverFilenames: dashboard.handoverFilenames });
183  if (completedScan)
184    dashboard.motion.observe(dashboard.projection, dashboard.ledgerRoot);
185}
186/** The one line the narrow fallback draws, and the pane's own summary row. */
187function summaryLine(dashboard: DashboardState): string {
188  // Until one scan has finished (or a warm cache came out of the store) the
189  // numbers are a partial read, and drawing them would be a figure that
190  // changes a second later for no reason the reader can see.
191  const busy = dashboard.scanActive || dashboard.scanInitializing;
192  if (!dashboard.warm || dashboard.projection === null) {
193    return busy
194      ? `Storybloq: reading the ledger, ${dashboard.queue.length} files left`
195      : "Storybloq: no ledger read yet";
196  }
197  // The phase in hand; failing that, why there is none: a project whose
198  // every phase is complete is finished, not phaseless (ISS-1257, the
199  // `complete` sample), and only a roadmap with no phases says "no phase".
200  const phase = dashboard.projection.currentPhase
201    ? dashboard.projection.currentPhase.name
202    : dashboard.projection.phases.length > 0
203      ? "all phases complete"
204      : "no phase";
205  const parts = [
206    phase,
207    `${dashboard.projection.openTickets} open`,
208    `${dashboard.projection.inProgressTickets.length} in progress`,
209    `${dashboard.projection.blockedTickets} blocked`,
210    `${dashboard.projection.openIssues} issues`,
211  ];
212  if (busy)
213    parts.push(`reading ${dashboard.queue.length}`);
214  return `Storybloq: ${parts.join(", ")}`;
215}
216/**
217* The band, cut to the terminal. Below the dock width it ends with what width
218* the board needs, but only when the whole summary fits beside it. Reserve
219* context pressure first so a long phase name cannot push it out of view.
220*/
221const SHORT_TERMINAL_ROWS = 24;
222// The scroll host lays out the plugin tree without a bounded parent height,
223// so percentage heights cannot make a flex spacer consume the dock's room.
224function dockHeight(event: any): number {
225  const rows = event.viewport?.rows;
226  const scroll = event.props?.scroll;
227  if (typeof scroll?.bodyRows === "number" && scroll.bodyRows > 0
228    && scroll.contentRows > scroll.bodyRows) return scroll.bodyRows;
229  return Math.max(1, (typeof rows === "number" ? rows : 40) - 6);
230}
231
232function shortTerminal(event: any): boolean {
233  return typeof event.viewport?.rows === "number"
234    && event.viewport.rows > 0 && event.viewport.rows <= SHORT_TERMINAL_ROWS;
235}
236
237function bandText(dashboard: DashboardState, columns: number, narrow: boolean): string {
238  const context = contextLabel(dashboard.contextPercent, Math.min(columns, 20));
239  const room = columns - cellWidth(context) - 2;
240  if (room <= 0)
241    return context;
242  const summary = summaryLine(dashboard);
243  const line = `${truncate(summary, room)}  ${context}`;
244  const hint = narrow && cellWidth(line) + cellWidth(BAND_HINT) <= columns ? BAND_HINT : "";
245  return truncate(`${line}${hint}`, columns);
246}
247/** config.json, roadmap.json, the handover names and the session flag. */
248async function readHeader(dashboard: DashboardState, $: any): Promise<void> {
249  try {
250    const configText = await $.fs.read(p(dashboard, CONFIG_PATH));
251    const parsed = JSON.parse(configText) as {
252      project?: unknown;
253    };
254    dashboard.project = typeof parsed.project === "string" ? parsed.project : "";
255  }
256  catch {
257    dashboard.project = "";
258  }
259  try {
260    const roadmapText = await $.fs.read(p(dashboard, ROADMAP_PATH));
261    const parsed = JSON.parse(roadmapText) as {
262      phases?: readonly {
263        id?: unknown;
264        name?: unknown;
265        label?: unknown;
266      }[];
267    };
268    const found: {
269      id: string;
270      name: string;
271      label?: string;
272    }[] = [];
273    for (const phase of parsed.phases ?? []) {
274      if (typeof phase.id === "string") {
275        found.push({ id: phase.id, name: typeof phase.name === "string" ? phase.name : phase.id, label: typeof phase.label === "string" ? phase.label : undefined });
276      }
277    }
278    dashboard.phases = found;
279  }
280  catch {
281    dashboard.phases = [];
282  }
283  try {
284    const entries = await $.fs.list(p(dashboard, HANDOVERS_DIR));
285    dashboard.handoverFilenames = entries
286      .filter((entry: {
287      kind: string;
288    }) => entry.kind === "file")
289      .map((entry: {
290      name: string;
291    }) => entry.name);
292  }
293  catch {
294    dashboard.handoverFilenames = [];
295  }
296  // status.json is the session flag and, since T-531, the stage the session
297  // is in; the ledger numbers do not come from it. Each stage field is kept
298  // only when it is a usable string and is independent of the others, and
299  // none of them is read before the flag is decided, so a malformed field
300  // can neither throw past it nor change it.
301  dashboard.sessionActive = false;
302  forgetSession(dashboard);
303  if (await $.fs.exists(p(dashboard, STATUS_PATH))) {
304    try {
305      const parsed = JSON.parse(await $.fs.read(p(dashboard, STATUS_PATH))) as {
306        sessionActive?: unknown;
307        state?: unknown;
308        ticket?: unknown;
309        claudeStatus?: unknown;
310        observedAt?: unknown;
311        currentIssue?: unknown;
312      };
313      dashboard.sessionActive = parsed.sessionActive === true;
314      dashboard.sessionState = statusField(parsed.state);
315      dashboard.sessionTicket = statusField(parsed.ticket);
316      dashboard.sessionClaudeStatus = statusField(parsed.claudeStatus);
317      dashboard.sessionObservedAt = statusField(parsed.observedAt);
318      // T-532: an ISSUE_FIX session names its issue here, with `ticket` null.
319      const issue = parsed.currentIssue;
320      if (typeof issue === "object" && issue !== null && !Array.isArray(issue)) {
321        const { id, displayId } = issue as { id?: unknown; displayId?: unknown };
322        dashboard.sessionIssueId = statusField(id);
323        dashboard.sessionIssue = statusField(displayId) ?? dashboard.sessionIssueId;
324      }
325    }
326    catch {
327      dashboard.sessionActive = false;
328    }
329  }
330}
331/** T-531: the stage fields back to unset. */
332function forgetSession(dashboard: DashboardState): void {
333  dashboard.sessionState = null;
334  dashboard.sessionTicket = null;
335  dashboard.sessionClaudeStatus = null;
336  dashboard.sessionObservedAt = null;
337  dashboard.sessionIssue = null;
338  dashboard.sessionIssueId = null;
339}
340async function loadCache(dashboard: DashboardState, $: any): Promise<void> {
341  if (dashboard.cacheLoaded)
342    return;
343  dashboard.cacheLoaded = true;
344  try {
345    const stored = await $.store.get(STORE_KEY);
346    if (stored && typeof stored === "object" && !Array.isArray(stored)) {
347      dashboard.cache = stored as Record<string, CachedRecord>;
348      // ISS-1306: a record cached before titles were made safe is served until
349      // its file changes, so its title is cleaned here too.
350      for (const entry of Object.values(dashboard.cache)) {
351        const record = entry?.record as { title?: unknown } | undefined;
352        if (record && typeof record.title === "string") record.title = displaySafe(record.title);
353      }
354      // T-486 U2-5: a complete ticket cached before `withdrawn` existed is read
355      // again, so withdrawn work does not sit in Done unmarked until its file
356      // next changes. Every other cached record is still served warm.
357      for (const [path, entry] of Object.entries(dashboard.cache)) {
358        const record = entry?.record as { kind?: unknown; status?: unknown; withdrawn?: unknown } | undefined;
359        if (record && record.kind === "ticket" && record.status === "complete" && typeof record.withdrawn !== "boolean")
360          delete dashboard.cache[path];
361      }
362      // A cache from an earlier session is enough to draw real numbers while
363      // this session's scan confirms them.
364      dashboard.warm = Object.keys(dashboard.cache).length > 0;
365    }
366  }
367  catch {
368    dashboard.cache = {};
369  }
370}
371async function saveCache(dashboard: DashboardState, $: any): Promise<void> {
372  const text = JSON.stringify(dashboard.cache);
373  if (text.length > STORE_BUDGET_BYTES) {
374    $.ui.log(`storybloq sidebar: the ledger cache is over ${STORE_BUDGET_BYTES} bytes, so it is not kept between sessions`);
375    return;
376  }
377  try {
378    await $.store.set(STORE_KEY, dashboard.cache);
379  }
380  catch {
381    // A store that refuses costs a cold start next session, nothing more.
382  }
383}
384/** One guarded line, once: a failing sidebar must not become a chatty one. */
385function noteFailure(dashboard: DashboardState, $: any, what: string): void {
386  if (dashboard.saidScanFailed)
387    return;
388  dashboard.saidScanFailed = true;
389  try {
390    $.ui.log(`storybloq sidebar: ${what}, so the pane may be behind the ledger until a later turn`);
391  }
392  catch {
393    // A refused log is not worth a second failure.
394  }
395}
396/**
397* ONE timer for the module's life, not one per scan.
398*
399* `$.clock.every` runs until its `cancel()`, and a scan that registered its
400* own would leave it running: two scans, two timers, every later tick paying
401* for both. The callback returns at once unless a scan is actually draining.
402*/
403function startTimer(dashboard: DashboardState, $: any): void {
404  if (dashboard.timerStarted)
405    return;
406  try {
407    $.clock.every(SCAN_TICK_MS, () => {
408      tick(dashboard, $).catch(() => {
409        finalizeScan(dashboard, $, "failed");
410      });
411    });
412  }
413  catch {
414    // A hook beneath may refuse the registration. Marking it started before
415    // it returned would mean no later attempt is ever made, and a scan begun
416    // with no timer builds a queue that nothing drains.
417    noteFailure(dashboard, $, "the scan timer could not be started");
418    return;
419  }
420  dashboard.timerStarted = true;
421}
422/**
423* The one exit from a scan, whichever way it ended.
424*
425* Releasing the in-flight flags and consuming the pending refresh belong
426* together: a failure path that released the flags but left the pending flag
427* set would strand the request, because every later tick returns at once with
428* no scan active and nothing else reads that flag. Consumed exactly once, so
429* a failed scan nobody asked to repeat is not retried on its own.
430*/
431function finalizeScan(dashboard: DashboardState, $: any, outcome: "done" | "failed"): void {
432  dashboard.scanActive = false;
433  dashboard.scanInitializing = false;
434  dashboard.ticking = false;
435  if (outcome === "failed")
436    noteFailure(dashboard, $, "a ledger scan did not finish");
437  if (!dashboard.pendingRefresh)
438    return;
439  dashboard.pendingRefresh = false;
440  requestScan(dashboard, $);
441}
442/**
443* Asks for a scan, coalescing.
444*
445* A refresh asked for while one is in flight is REMEMBERED, not dropped: the
446* queue the running scan is draining was listed before the write that
447* prompted this call, so that write would otherwise never be listed at all.
448* Many requests during one scan collapse into the single scan that follows it.
449*/
450/**
451* Joins a pinned root to one of the ledger suffixes with exactly one
452* separator.
453*
454* The trailing-separator trim deliberately refuses to shorten a bare drive
455* root: on Windows `C:\` trimmed to `C:` stops being absolute and becomes
456* drive-RELATIVE, which would reintroduce the very bug this pins down. `/`
457* has the same shape and is left alone for the same reason.
458*/
459function joinRoot(root: string, suffix: string): string {
460  const bareDriveRoot = /^[A-Za-z]:[/\\]$/.test(root);
461  const trimmed = root.length > 1 && !bareDriveRoot ? root.replace(/[/\\]+$/, "") : root;
462  const separated = trimmed.endsWith("/") || trimmed.endsWith("\\");
463  return separated ? `${trimmed}${suffix}` : `${trimmed}/${suffix}`;
464}
465/**
466* A ledger suffix as an absolute path under the pinned root.
467*
468* Every `$.fs` call that touches the ledger goes through here. The seven
469* suffix constants are left exactly as they are, so the ledger-write detector
470* further down, which matches command TEXT rather than filesystem paths, is
471* untouched by this change.
472*/
473function p(dashboard: DashboardState, suffix: string): string {
474  return dashboard.ledgerRoot === null ? suffix : joinRoot(dashboard.ledgerRoot, suffix);
475}
476/** The parent of a directory, or the directory itself once it is a root. */
477function parentDir(dir: string): string {
478  const cut = Math.max(dir.lastIndexOf("/"), dir.lastIndexOf("\\"));
479  if (cut < 0)
480    return dir;
481  if (cut === 0)
482    return dir.slice(0, 1);
483  const parent = dir.slice(0, cut);
484  return /^[A-Za-z]:$/.test(parent) ? `${parent}\\` : parent;
485}
486/** An errno off a rejected `$.fs` call, when the host supplied one. */
487function errnoOf(error: unknown): string {
488  const code = (error as {
489    code?: unknown;
490  } | null)?.code;
491  return typeof code === "string" ? code : "";
492}
493/** A missing path, as opposed to one the host refused to answer for. */
494function isMissing(error: unknown): boolean {
495  return errnoOf(error) === "ENOENT";
496}
497/** How far up the walk will look before calling the question unanswerable. */
498const MAX_ROOT_WALK = 64;
499type RootResolution = {
500  readonly kind: "pinned";
501  readonly root: string;
502} | {
503  readonly kind: "absent";
504} | {
505  readonly kind: "unresolved";
506  readonly reason: string;
507};
508/**
509* Walks up from `start` for the nearest directory holding a `.story/`.
510*
511* Three ANSWERS, and keeping them apart is the point. "pinned" is a root.
512* "absent" is a clean walk that reached the filesystem root without finding
513* one, which is a project that never ran `storybloq init` and is not a
514* failure. "unresolved" is the host refusing the question, which is a failure
515* and must never be mistaken for the second.
516*
517* A ledger is `.story/config.json`, the CLI's own rule (`checkRoot` in
518* src/core/project-root-shared.ts), not the bare directory (ISS-1256). A
519* fresh `storybloq init` writes the config, so it still counts; a `.story/`
520* with no config is a miss and the walk goes on past it. The owner's home
521* directory holds one such stray (`~/.story/sessions/` from May, nothing
522* else), and the bare-directory test pinned every ledgerless project under
523* the home directory to it and drew an all-zero board there.
524*
525* The walk stops when a directory is its own parent, so a filesystem root
526* cannot loop, and is bounded anyway: a bound that is never reached costs
527* nothing and a walk that never ends costs the session.
528*/
529async function resolveLedgerRoot(dashboard: DashboardState, $: any, start: string): Promise<RootResolution> {
530  // A trailing separator would make the first step ask about `/repo//.story`
531  // and the second, after `parentDir` trims it, ask about the same directory
532  // again. Same shape as `joinRoot`: a bare drive root keeps its separator,
533  // because without it it stops being absolute.
534  const bareDriveRoot = /^[A-Za-z]:[/\\]$/.test(start);
535  let dir = start.length > 1 && !bareDriveRoot ? start.replace(/[/\\]+$/, "") : start;
536  for (let step = 0; step < MAX_ROOT_WALK; step += 1) {
537    try {
538      if ((await $.fs.exists(joinRoot(dir, CONFIG_PATH))) !== false) {
539        return { kind: "pinned", root: dir };
540      }
541    }
542    catch (error) {
543      const code = errnoOf(error);
544      return { kind: "unresolved", reason: code === "" ? "the client refused the read" : code };
545    }
546    const parent = parentDir(dir);
547    if (parent === dir)
548      return { kind: "absent" };
549    dir = parent;
550  }
551  return { kind: "absent" };
552}
553/**
554* Opens the pane and reads the ledger: everything `session.start` does once
555* it knows there is something to draw.
556*
557* Also the recovery path. A project that had no `.story/` when the session
558* started gets one the moment someone runs `storybloq init`, and the pane has
559* to appear then rather than at the next reload, so `turn.complete` and a
560* ledger-writing `tool.call` both come back through here.
561*/
562async function attach(dashboard: DashboardState, $: any): Promise<void> {
563  if (!dashboard.paneOpen) {
564    // T-519: the pane's background is the client's. As of the 2.1.273 d.ts,
565    // `PaneOpenArgs` is { id, title, focus, closeOnEscape, holdToasts, rows }
566    // and the `Pane` props are read-only placement data: nothing names a
567    // theme or background. `Box`/`Text` take `backgroundColor`, but that
568    // fixes a colour rather than following the terminal, so none is set and
569    // the tones below are chosen for the client's own pane background.
570    await $.ui.open({ id: PANE_ID, title: PANE_TITLE });
571    dashboard.paneOpen = true;
572  }
573  // The context figures belong to the window, not to the turn: they are
574  // readable the moment the Mod loads into a session that has already had a
575  // response. Reading them only on `turn.complete` is why the owner's header
576  // was blank after a reload, with the fill only appearing a turn later.
577  const read = await refreshContext(dashboard, $);
578  if (read !== null) {
579    dashboard.contextPercent = read.value;
580    dashboard.motion.setContext(read.value, true);
581  }
582  dashboard.themeLight = await readThemeLight(dashboard, $);
583  await readHeader(dashboard, $);
584  await loadCache(dashboard, $);
585  // The idle poll's baseline (T-517). Taken before the timer starts and before
586  // the scan below, so the first poll compares against the ledger as it was
587  // when this session read it: a write between the two is a change, not a
588  // missed one. Taken after the timer, a tick could poll against an empty
589  // baseline, find every path "moved" and rescan for nothing.
590  dashboard.polledMtimes = await ledgerMtimes(dashboard, $);
591  startTimer(dashboard, $);
592  requestScan(dashboard, $);
593}
594/**
595* The ledger was missing; is it there now? Attaches if it is.
596*
597* Nothing is drawn while it is absent, so this is the only way back: every
598* refresh the Mod already made asks the question again, and the answer costs
599* one `$.fs.exists` on a project that has no ledger to read anyway.
600*/
601async function attachIfLedgerArrived(dashboard: DashboardState, $: any): Promise<void> {
602  // From the ORIGIN, never from the live working directory: the session may
603  // have cd-ed anywhere by now, and resolving from there would either miss
604  // the project's ledger or attach to an unrelated nested one.
605  if (dashboard.initialCwd === null)
606    return;
607  const resolution = await resolveLedgerRoot(dashboard, $, dashboard.initialCwd);
608  if (resolution.kind !== "pinned")
609    return;
610  dashboard.ledgerRoot = resolution.root;
611  dashboard.noLedger = false;
612  await attach(dashboard, $);
613}
614function requestScan(dashboard: DashboardState, $: any): void {
615  if (dashboard.scanActive || dashboard.scanInitializing) {
616    dashboard.pendingRefresh = true;
617    return;
618  }
619  // The timer may still be missing because an earlier registration was
620  // refused. Without it a queue would be built that nothing drains, so try
621  // again here and start no scan while it is absent.
622  startTimer(dashboard, $);
623  if (!dashboard.timerStarted)
624    return;
625  beginScan(dashboard, $).catch(() => {
626    // The scan is detached, so nothing else would hear this.
627    finalizeScan(dashboard, $, "failed");
628  });
629}
630/** Lists the ledger and leaves a queue for the ticker to drain. */
631async function beginScan(dashboard: DashboardState, $: any): Promise<void> {
632  dashboard.scanInitializing = true;
633  try {
634    const items: ScanItem[] = [];
635    // ISS-1239: a directory that is MISSING and one the host refused to read
636    // are different facts, and the purge below may only act on the first.
637    // `$.fs` rejects with the OS errno, so they are separable.
638    let refused = false;
639    try {
640      for (const entry of await $.fs.list(p(dashboard, TICKETS_DIR))) {
641        if (entry.kind === "file" && entry.name.endsWith(".json")) {
642          items.push({ path: `${TICKETS_DIR}/${entry.name}`, kind: "ticket" });
643        }
644      }
645    }
646    catch (error) {
647      // No tickets directory: nothing to read from it.
648      if (!isMissing(error))
649        refused = true;
650    }
651    try {
652      for (const entry of await $.fs.list(p(dashboard, ISSUES_DIR))) {
653        if (entry.kind === "file" && entry.name.endsWith(".json")) {
654          items.push({ path: `${ISSUES_DIR}/${entry.name}`, kind: "issue" });
655        }
656      }
657    }
658    catch (error) {
659      // Same.
660      if (!isMissing(error))
661        refused = true;
662    }
663    // A file the ledger no longer has must leave the cache, or a deleted
664    // ticket would keep being counted.
665    //
666    // Skipped on a refusal. Purging then would turn "I could not read this"
667    // into "this was deleted" and empty the board on a transient failure,
668    // which is this issue's bug wearing a different hat. Keeping a stale
669    // record costs a board that is briefly behind; purging costs the board.
670    // An empty scan that really is an empty ledger still clears, because that
671    // path throws ENOENT and leaves `refused` false.
672    if (!refused) {
673      const present = new Set(items.map((item) => item.path));
674      for (const path of Object.keys(dashboard.cache)) {
675        if (!present.has(path))
676          delete dashboard.cache[path];
677      }
678    }
679    dashboard.queue = items;
680    dashboard.scanActive = true;
681    reproject(dashboard);
682    $.ui.invalidate("ui.render");
683  }
684  finally {
685    dashboard.scanInitializing = false;
686  }
687}
688/**
689* What one turn of the module's single timer does: drain an active scan, and
690* once every IDLE_POLL_TICKS look for a write nothing told this session about
691* (T-517).
692*
693* The counter lives here rather than in a second `$.clock.every`, because a
694* second timer would be a second dispatch on every 25 ms tick for the life of
695* the session, and the poll is a two-second thing.
696*/
697async function tick(dashboard: DashboardState, $: any): Promise<void> {
698  // Share the existing clock. Redraw only during the short, visible intro,
699  // at 20 fps. Later effects use this same timer and stop redrawing when idle.
700  if (dashboard.logoStarted && !dashboard.logoFinished) {
701    if (!dashboard.paneOpen || !dashboard.sidebarEnabled)
702      dashboard.logoFinished = true;
703    else {
704      dashboard.logoElapsed += SCAN_TICK_MS;
705      dashboard.logoTicks += 1;
706      if (dashboard.logoElapsed >= LOGO_DURATION_MS)
707        dashboard.logoFinished = true;
708      if (dashboard.logoTicks % 2 === 0 || dashboard.logoFinished)
709        $.ui.invalidate("ui.render");
710    }
711  }
712  if (dashboard.motion.advance(SCAN_TICK_MS) && dashboard.sidebarEnabled && ((dashboard.paneOpen && dashboard.paneDrawn) || dashboard.bandDrawn))
713    $.ui.invalidate("ui.render");
714  await drainChunk(dashboard, $);
715  dashboard.idleTicks += 1;
716  if (dashboard.idleTicks < IDLE_POLL_TICKS)
717    return;
718  dashboard.idleTicks = 0;
719  try {
720    await pollLedger(dashboard, $);
721  }
722  catch {
723    // The timer's catch finalizes the ACTIVE scan as failed, which a poll that
724    // could not stat or read the header has no business doing: the scan is
725    // unrelated to it. A failed poll costs nothing and runs again in two
726    // seconds.
727  }
728}
729/**
730* The four directory mtimes the idle poll watches.
731*
732* Directories and not files: every CLI and MCP write lands by rename or link
733* INTO a directory (project-loader's atomicWrite and atomicCreate), so a
734* create, a delete and a replace all move the directory's own mtime, and four
735* stats stand in for a walk of a couple of thousand files. `.story` itself is
736* where roadmap.json, config.json and status.json land. An in-place edit by
737* an editor moves no directory, and that case is what the turn-end rescan is
738* still for.
739*
740* A path that cannot be stat-ed reads 0, so one that appears later moves.
741*/
742const POLLED_PATHS = [LEDGER_DIR, TICKETS_DIR, ISSUES_DIR, HANDOVERS_DIR] as const;
743async function ledgerMtimes(dashboard: DashboardState, $: any): Promise<Record<string, number>> {
744  const seen: Record<string, number> = {};
745  for (const path of POLLED_PATHS) {
746    try {
747      const stat = await $.fs.stat(p(dashboard, path));
748      seen[path] = typeof stat?.mtimeMs === "number" ? stat.mtimeMs : 0;
749    }
750    catch {
751      seen[path] = 0;
752    }
753  }
754  return seen;
755}
756/**
757* Has anything moved since the last look? If so, refresh.
758*
759* Deliberately NOT gated on `paneOpen`, for the same reason the AbovePrompt
760* line is not: below the client's dock width there is no pane and that line
761* IS the sidebar, so a poll tied to the pane would leave the only thing drawn
762* standing still.
763*
764* Deliberately NOT skipped while a scan is draining either: the running scan
765* took its worklist before this write existed, so it will not see it. Asking
766* mid-scan is what `requestScan`'s pending flag is for, and the rescan
767* follows the one in flight instead of being dropped.
768*
769* Never while `noLedger`: a project with no `.story/` draws nothing at all,
770* and `turn.complete` and a ledger-writing tool call already carry the one
771* question worth asking there (has a ledger arrived?).
772*/
773async function pollLedger(dashboard: DashboardState, $: any): Promise<void> {
774  if (!dashboard.uiAvailable || !dashboard.sidebarEnabled || dashboard.noLedger)
775    return;
776  const seen = await ledgerMtimes(dashboard, $);
777  let moved = false;
778  for (const path of POLLED_PATHS) {
779    if (dashboard.polledMtimes[path] !== seen[path])
780      moved = true;
781  }
782  // The whole of the idle cost: four stats and this comparison. Dropping it
783  // is M-POLL-ALWAYS-RESCANS, which re-reads the ledger every two seconds
784  // whether or not anyone wrote it.
785  if (!moved)
786    return;
787  dashboard.polledMtimes = seen;
788  // The header files (roadmap, config, status, the handover names) land in
789  // `.story` itself, and a scan does not re-read them, so this mirrors what
790  // `turn.complete` does. It runs only when something actually moved.
791  await readHeader(dashboard, $);
792  requestScan(dashboard, $);
793}
794/**
795* One tick: up to SCAN_CHUNK files, each stat-ed and re-read only when its
796* mtime moved. The mtime check is the whole of "updates within one prompt";
797* serving the cached fields without it is the M-STALE-CACHE mutant.
798*/
799async function drainChunk(dashboard: DashboardState, $: any): Promise<void> {
800  // The idle guard. Without it every tick after the first scan reprojects the
801  // whole ledger, serializes it, writes it to the store and invalidates, for
802  // as long as the session lasts.
803  if (dashboard.ticking || !dashboard.scanActive)
804    return;
805  dashboard.ticking = true;
806  let outcome: "done" | "failed" | null = null;
807  try {
808    let read = 0;
809    while (dashboard.queue.length > 0 && read < SCAN_CHUNK) {
810      const item = dashboard.queue.shift()!;
811      read += 1;
812      try {
813        const stat = await $.fs.stat(p(dashboard, item.path));
814        const cached = dashboard.cache[item.path];
815        if (cached && cached.mtimeMs === stat.mtimeMs)
816          continue;
817        const record = extractRecord(item.kind, await $.fs.read(p(dashboard, item.path)));
818        if (record === null)
819          delete dashboard.cache[item.path];
820        else
821          dashboard.cache[item.path] = { mtimeMs: stat.mtimeMs, record };
822      }
823      catch (error) {
824        // Keep the last valid record on transient failures; retry next scan.
825        if (isMissing(error))
826          delete dashboard.cache[item.path];
827        else
828          noteFailure(dashboard, $, "a ledger record could not be read");
829      }
830    }
831    if (dashboard.queue.length === 0) {
832      dashboard.warm = true;
833      reproject(dashboard, true);
834      await saveCache(dashboard, $);
835      $.ui.invalidate("ui.render");
836      outcome = "done";
837    }
838  }
839  catch {
840    // Whatever failed, this scan is over. Which of the two it was changes
841    // only the log line: both leave through the same door.
842    outcome = "failed";
843  }
844  if (outcome === null) {
845    dashboard.ticking = false;
846    return;
847  }
848  finalizeScan(dashboard, $, outcome);
849}
850/**
851* The context fill from what `$.session.usage()` actually answers.
852*
853* `SessionContextUsage` carries `window` always, `tokens` and `percent` only
854* "from the first API response of the live window": a fresh session or one
855* just compacted has neither until its next response. Live, the owner's
856* header stayed empty because this read `percent` alone, so the percent is
857* computed from `tokens` whenever they exist, the engine's `percent` stands
858* in only when they do not, and null (show an unknown reading) when there is no
859* reading at all. `compactWindow` is the settings' auto-compact window, or
860* null for the model's own.
861*/
862/** Whether a theme name is a light one: `light`, `light-daltonized`, `light-ansi`. */
863function isLightTheme(value: unknown): boolean {
864  return typeof value === "string" && value.startsWith("light");
865}
866/**
867* The client's `theme` row, as `$.config.list()` answers it (ISS-1238). A
868* refused or unexpected answer keeps the dark default, never the board.
869*/
870async function readThemeLight(dashboard: DashboardState, $: any): Promise<boolean> {
871  try {
872    const rows = await $.config.list();
873    const row = Array.isArray(rows) ? rows.find((r: any) => r?.key === "theme") : undefined;
874    return isLightTheme(row?.value);
875  }
876  catch {
877    return false;
878  }
879}
880/**
881* The context fill, or null, and never a rejection.
882*
883* The fill is telemetry on the header's right, and `$.session.usage` is a call
884* a host may not have or may refuse. Unguarded in `session.start` it rejects
885* AFTER the pane is opened, which takes the ledger read, the board and
886* `next(e)` with it: a missing figure would cost the whole sidebar. One helper
887* for all three call sites so the shape cannot drift between them.
888*/
889async function readContextFill($: any): Promise<number | null> {
890  try {
891    // Summary is local-only. The session's own threshold survives settings
892    // edits and plugin reloads; reading settings here would change history.
893    const usage = await $.session.usage({ breakdown: "summary" });
894    const threshold = usage?.context?.breakdown?.autoCompactThreshold;
895    const tokens = usage?.context?.tokens;
896    if (typeof threshold === "number" && Number.isFinite(threshold) && threshold > 0
897      && typeof tokens === "number" && Number.isFinite(tokens) && tokens >= 0) {
898      return Math.min(100, Math.round(tokens / threshold * 100));
899    }
900    // Native window percent is a different measure. An unavailable or
901    // disabled compaction threshold must not masquerade as pressure.
902    return null;
903  }
904  catch {
905    return null;
906  }
907}
908/**
909* T-532: a context read, and whether it is still the newest one. Every read
910* takes the next sequence number and a session start takes one too, so a slow
911* read that a later read or a restart has overtaken comes back null and is
912* dropped: a tool call's figure from before a compaction must not replace the
913* compaction's own.
914*/
915async function refreshContext(dashboard: DashboardState, $: any): Promise<{ value: number | null } | null> {
916  const seq = ++dashboard.contextSeq;
917  const value = await readContextFill($);
918  return seq === dashboard.contextSeq ? { value } : null;
919}
920export function registerSidebar(on: On, _options: Options): void {
921  const dashboard = createDashboardState();
922  forgetEverything(dashboard);
923  dashboard.motion = new DashboardMotion(_options["motion"] !== false, _options["changeHighlights"] !== false);
924  // The pane. `ui.render` fires once per input value and again on
925  // `$.ui.invalidate("ui.render")`, so this hook only draws what the refresh
926  // hooks have already computed: it awaits nothing.
927  (on("ui.render", ($: any, e: any, next: (e: any) => unknown) => {
928    // Nothing is drawn without a ledger, pane or band: there is no pane open
929    // to render into, and the band's line would be the empty board in one row.
930    if (dashboard.noLedger)
931      return next(e);
932    if (e.component === "Pane" && e.requestId === PANE_ID) {
933      if (!dashboard.paneDrawn) {
934        // The band may have drawn on this same pass believing the pane
935        // parked (a reload runs `register` fresh, so this flag starts false
936        // while the client keeps the pane up). One redraw and it stands down.
937        dashboard.paneDrawn = true;
938        dashboard.reopenAsked = false;
939        $.ui.invalidate("ui.render");
940      }
941      const elements = $.ui.resolve(e);
942      const { Box, Text } = elements;
943      const width: number = typeof e.props?.bodyColumns === "number" ? e.props.bodyColumns : 40;
944      // The header, then one blank row: the break the owner asked for, so the
945      // wordmark does not read as part of the first column heading. A single
946      // space and not an empty string, because an empty Text collapses to no
947      // row at all in this client and the break simply did not draw.
948      if (shortTerminal(e) && panePlacement(e) !== "dock") {
949        dashboard.logoFinished = true;
950        return compactLineNode(dashboard, elements, width);
951      }
952      const placement = panePlacement(e);
953      dashboard.paneInline = placement === "inline";
954      const layout = boardLayout(placement, width);
955      const stacked = layout === "stacked";
956      // The narrow board is at most five rows and skips the blank rows, so it
957      // needs no budget: it is the inline strip on a small window, where
958      // every row is paid for.
959      const budget = layout === "narrow" ? { body: 0, gaps: false, compact: false } : rowBudget(e, stacked);
960      // Use the actual pane window: viewport.rows includes the transcript
961      // and prompt and is never the available height of an inline pane.
962      const bodyRows = e.props?.scroll?.bodyRows;
963      const intro = typeof bodyRows === "number" && bodyRows <= 1 ? null
964        : logoLayout(width, typeof bodyRows === "number" ? bodyRows - 1 : undefined, placement);
965      if (!shortTerminal(e) && dashboard.motion.enabled && _options["startupLogo"] !== false && !dashboard.logoFinished && intro) {
966        dashboard.logoStarted = true;
967        const art = logoFrame(intro.columns, dashboard.logoElapsed);
968        return Box({ flexDirection: "column", children: [
969            headerNode(dashboard, elements),
970            ...Array.from({ length: intro.top }, (_, index) => Text({ key: `logo-space-${index}`, children: " " })),
971            ...art.map((cells, index) => Text({ key: `storyfield-${index}`, wrap: "truncate", children: [
972                Text({ key: "inset", children: " ".repeat(intro.left) }),
973                ...cells.map((cell, x) => Text({ key: `dot-${x}`, color: cell.color, children: cell.glyph })),
974              ] })),
975            contextNode(dashboard, elements, dashboard.contextPercent, width),
976          ] });
977      }
978      if (placement === "dock" && shortTerminal(e)) {
979        const body = Math.max(1, Math.min(7, dockHeight(e) - 7));
980        return Box({ key: "short-sidebar", height: dockHeight(e), flexDirection: "column", children: [
981          headerNode(dashboard, elements),
982          Text({ children: " " }),
983          inProgressBoard(dashboard, elements, width, body),
984          Box({ key: "footer-space", flexGrow: 1 }),
985          footerNode(dashboard, elements, dashboard.projection?.issuesBySeverity ?? { critical: 0, high: 0, medium: 0, low: 0 }, dashboard.contextPercent, width),
986        ] });
987      }
988      const rows: unknown[] = [headerNode(dashboard, elements)];
989      if (budget.gaps)
990        rows.push(paneText(dashboard, Text, { key: "header-gap", children: " " }));
991      if (dashboard.projection === null) {
992        // Nothing to draw a board from yet: the one line that says why.
993        rows.push(paneText(dashboard, Text, { children: truncate(summaryLine(dashboard), width) }));
994        rows.push(contextNode(dashboard, elements, dashboard.contextPercent, width));
995      }
996      else if (layout === "narrow") {
997        rows.push(narrowBoard(dashboard, elements, dashboard.projection.board, width));
998        rows.push(Box({ key: "footer", flexDirection: "row", justifyContent: "flex-end", children: [contextNode(dashboard, elements, dashboard.contextPercent, width)] }));
999      }
1000      else {
1001        rows.push(budget.compact
1002          ? compactBoard(dashboard, elements, dashboard.projection.board, width)
1003          : boardNode(dashboard, elements, dashboard.projection.board, width, stacked, budget.body));
1004        if (budget.gaps)
1005          rows.push(paneText(dashboard, Text, { key: "issues-gap", children: " " }));
1006        if (placement === "dock") rows.push(Box({ key: "footer-space", flexGrow: 1 }));
1007        rows.push(footerNode(dashboard, elements, dashboard.projection.issuesBySeverity, dashboard.contextPercent, width));
1008      }
1009      return Box({ flexDirection: "column", ...(placement === "dock" ? { height: dockHeight(e) } : {}), children: rows });
1010    }
1011    // The narrow fallback: the client leaves a plugin's pane undrawn on a
1012    // small terminal, so the same numbers go out as one line above the prompt.
1013    // Gated on the Mod being on, and deliberately NOT on the pane being OPEN:
1014    // below DOCK_COLUMNS the client draws no pane at all, so this line IS the
1015    // sidebar, and tying it to `paneOpen` would let a close of something never
1016    // drawn turn off the only thing that was. It IS tied to the pane having
1017    // been DRAWN: a pane opened narrow stays parked after a resize (ISS-1235),
1018    // and a wide terminal with a parked pane still has to show the numbers.
1019    if (e.component === "AbovePrompt" && dashboard.sidebarEnabled) {
1020      // Also catches a Mod reloaded during an already-running main turn.
1021      if (!e.props?.view?.agentId && typeof e.props?.isWorking === "boolean")
1022        dashboard.motion.setWorking(e.props.isWorking);
1023      dashboard.bandDrawn = false;
1024      const columns: number = typeof e.viewport?.columns === "number" ? e.viewport.columns : 0;
1025      const narrow = columns < DOCK_COLUMNS;
1026      if (shortTerminal(e)) {
1027        if (dashboard.paneDrawn) return next(e);
1028        dashboard.bandDrawn = true;
1029        const elements = $.ui.resolve(e);
1030        return compactLineNode(dashboard, elements, columns);
1031      }
1032      if (narrow) {
1033        dashboard.reopenAsked = false;
1034      }
1035      else if (dashboard.paneOpen && !dashboard.paneDrawn && !dashboard.reopenAsked) {
1036        // Wide, open, parked: ask the client to place it again. The width is
1037        // judged at each open (ISS-1235), so this is what a narrow-then-wide
1038        // session needs; once per crossing, and never for a pane the person
1039        // closed (`paneOpen` is false then). A refusal costs nothing: the
1040        // band below is still drawn on this pass.
1041        // Not awaited: a render never waits on an open, and the hook is sync.
1042        dashboard.reopenAsked = true;
1043        Promise.resolve()
1044          .then(() => {
1045          // Re-checked on the microtask: a close or a draw that landed in
1046          // between makes the ask stale, and a closed pane must stay closed.
1047          if (!dashboard.paneOpen || dashboard.paneDrawn)
1048            return;
1049          return $.ui.open({ id: PANE_ID, title: PANE_TITLE });
1050        })
1051          .catch(() => {
1052          // The band stands in; the next crossing asks again.
1053        });
1054      }
1055      if (columns > 0 && (narrow || !dashboard.paneDrawn)) {
1056        dashboard.bandDrawn = true;
1057        const { Text } = $.ui.resolve(e);
1058        // The hint only while there is no board on screen: a placed pane keeps
1059        // its seat when the window shrinks (inline, ISS-1247), and the band
1060        // under it carries the counts the narrow board leaves out (ISS-1252).
1061        return Text({ dimColor: true, children: bandText(dashboard, columns, narrow && !dashboard.paneDrawn) });
1062      }
1063    }
1064    return next(e);
1065    // A client without the UI events refuses this registration rather than
1066    // throwing at the call site; that is the "renders nothing" case, and the
1067    // scan admits `.catch` here and nothing else.
1068  }) as {
1069    catch: (fn: (error: unknown) => void) => void;
1070  }).catch(() => {
1071    dashboard.uiAvailable = false;
1072  });
1073  on("session.start", async ($: any, e: any, next: (e: any) => unknown) => {
1074    // T-532: first, before anything can return or wait. A reload fires this
1075    // again without re-registering: the tool-call throttle starts over, and
1076    // any context read still in flight from before is overtaken.
1077    dashboard.contextReadAt = null;
1078    dashboard.contextSeq += 1;
1079    if (!dashboard.uiAvailable) {
1080      if (!dashboard.saidNoUi) {
1081        dashboard.saidNoUi = true;
1082        $.ui.log("storybloq sidebar: this client has no UI render events, so the pane is not drawn");
1083      }
1084      return next(e);
1085    }
1086    // A `-p` run and the SDK draw nowhere: `surface` is null and nobody is at
1087    // the prompt, so there is no pane to open and no ledger worth reading for
1088    // a sidebar nobody will see.
1089    if (e.surface === null || e.isInteractive !== true)
1090      return next(e);
1091    // On, whatever happens next: the refresh hooks stay armed so the pane can
1092    // appear the moment a ledger does.
1093    dashboard.sidebarEnabled = true;
1094    dashboard.motion = new DashboardMotion(_options["motion"] !== false, _options["changeHighlights"] !== false);
1095    // ISS-1239: pin the root for the session, here and nowhere else. A reload
1096    // fires this event again and re-pins against the new directory, which is
1097    // wanted; `turn.complete` and `tool.call` must never re-resolve, which is
1098    // why neither of them touches these three.
1099    dashboard.ledgerRoot = null;
1100    // Said-once flags are per SESSION START, not per module load: a reload
1101    // fires this event again without re-registering, and leaving them set
1102    // would silently swallow the diagnostic the second time around.
1103    dashboard.saidNoLedger = false;
1104    dashboard.saidRootUnresolved = false;
1105    dashboard.initialCwd = typeof e.cwd === "string" && e.cwd.length > 0 ? e.cwd : null;
1106    if (dashboard.initialCwd === null) {
1107      // No absolute origin to address from. Falling back to relative paths
1108      // here is precisely the defect, so the Mod stays closed and says why
1109      // rather than drawing a board that empties on the first `cd`.
1110      dashboard.noLedger = true;
1111      if (!dashboard.saidNoLedger) {
1112        dashboard.saidNoLedger = true;
1113        $.ui.log("storybloq sidebar: this session start carried no working directory, so the pane stays closed");
1114      }
1115      return next(e);
1116    }
1117    const resolution = await resolveLedgerRoot(dashboard, $, dashboard.initialCwd);
1118    if (resolution.kind === "absent") {
1119      // No `.story/` means no pane, by the owner's ruling. A project that
1120      // never ran `storybloq init` was getting four bordered "none" columns
1121      // and an all-zero issues line, which is a dashboard reporting on
1122      // nothing; the Mod hides instead. T-537: it says how to set the
1123      // project up, once per session and root: a reload from the same root
1124      // stays quiet, a different root is told once. The command is the
1125      // Claude Code profile's, since this Mod runs only in Claude Code's
1126      // hooks runtime. The pane attaches when init finishes, through
1127      // `attachIfLedgerArrived`.
1128      dashboard.noLedger = true;
1129      if (!dashboard.setupHintRoots.has(dashboard.initialCwd)) {
1130        dashboard.setupHintRoots.add(dashboard.initialCwd);
1131        $.ui.log(`storybloq sidebar: no .story directory here, so the pane stays closed. ${SETUP_HINT}`);
1132      }
1133      return next(e);
1134    }
1135    if (resolution.kind === "unresolved") {
1136      // The host refused the walk, so there is no root to address from. An
1137      // earlier draft pinned the origin as a guess and carried on; that is
1138      // wrong, because `attachIfLedgerArrived` only runs while `noLedger` is
1139      // set, so guessing would lock the session to a possibly-wrong root for
1140      // good and disable its own recovery. Hiding costs one board until the
1141      // refusal lifts; the retry loop then re-walks and pins properly. What
1142      // is never done either way is falling back to relative addressing.
1143      dashboard.noLedger = true;
1144      if (!dashboard.saidRootUnresolved) {
1145        dashboard.saidRootUnresolved = true;
1146        $.ui.log(`storybloq sidebar: could not resolve the ledger root (${resolution.reason}), so the pane stays closed until it can be read`);
1147      }
1148      return next(e);
1149    }
1150    dashboard.ledgerRoot = resolution.root;
1151    dashboard.noLedger = false;
1152    // `session.start` fires again on a reload, and an open of an open id only
1153    // retitles it, but asking twice is still asking twice: `attach` asks once.
1154    await attach(dashboard, $);
1155    return next(e);
1156  });
1157  on("turn.start", ($: any, e: any, next: (e: any) => unknown) => {
1158    if (dashboard.uiAvailable && dashboard.sidebarEnabled && !dashboard.noLedger) {
1159      dashboard.motion.setWorking(true, e.turnId);
1160      $.ui.invalidate("ui.render");
1161    }
1162    return next(e);
1163  });
1164  // A turn is the unit the acceptance names: a `.story/` write during it shows
1165  // up by the next prompt.
1166  on("turn.complete", async ($: any, e: any, next: (e: any) => unknown) => {
1167    if (!dashboard.uiAvailable || !dashboard.sidebarEnabled)
1168      return next(e);
1169    dashboard.motion.finishTurn(e.turnId, e.agentId);
1170    $.ui.invalidate("ui.render");
1171    if (dashboard.noLedger) {
1172      await attachIfLedgerArrived(dashboard, $);
1173      return next(e);
1174    }
1175    const read = await refreshContext(dashboard, $);
1176    if (read !== null) {
1177      dashboard.contextPercent = read.value;
1178      dashboard.motion.setContext(read.value);
1179    }
1180    await readHeader(dashboard, $);
1181    requestScan(dashboard, $);
1182    return next(e);
1183  });
1184  // A ledger write inside a turn has to show up inside that turn: the owner
1185  // moved a ticket to in progress, waited five seconds and moved it back, and
1186  // the board sat on the old column the whole time because nothing asked for
1187  // a rescan until the turn ended. So this runs the call first and then, only
1188  // for a call that can have written `.story/`, requests a scan; the scan is
1189  // mtime-keyed, so the cost of one that changed nothing is a stat sweep.
1190  //
1191  // Filtered here rather than by an `on()` matcher on the tool name: a
1192  // matcher prints as `tool.call{tool=/.../}` in the client's scan, and the
1193  // contract test compares that list against the bare event names pinned in
1194  // client-api.ts, which is not this Mod's file to change.
1195  on("tool.call", async ($: any, e: any, next: (e: any) => unknown) => {
1196    const result = await next(e);
1197    if (dashboard.uiAvailable && dashboard.sidebarEnabled && wroteLedger(e)) {
1198      // `storybloq init` is a ledger write like any other, and it is the one
1199      // that turns a hidden Mod into a drawn one, so the no-ledger case goes
1200      // through the same filter rather than waiting for the turn to end.
hooks/dashboard-state.ts 131 lines
1import { DashboardMotion } from "./dashboard-motion.js";
2import type { SidebarProjection, SidebarRecord } from "./sidebar-projection.js";
3export interface CachedRecord {
4  readonly mtimeMs: number;
5  readonly record: SidebarRecord;
6}
7export interface ScanItem {
8  readonly path: string;
9  readonly kind: "ticket" | "issue";
10}
11/** Owned by one registration; host callbacks retain their own instance. */
12export interface DashboardState {
13  cache: Record<string, CachedRecord>;
14  cacheLoaded: boolean;
15  projection: SidebarProjection | null;
16  project: string;
17  phases: {
18    readonly id: string;
19    readonly name: string;
20    readonly label?: string;
21  }[];
22  handoverFilenames: string[];
23  queue: ScanItem[];
24  idleTicks: number;
25  polledMtimes: Record<string, number>;
26  scanInitializing: boolean;
27  scanActive: boolean;
28  pendingRefresh: boolean;
29  ticking: boolean;
30  timerStarted: boolean;
31  logoElapsed: number;
32  logoStarted: boolean;
33  logoFinished: boolean;
34  logoTicks: number;
35  motion: DashboardMotion;
36  bandDrawn: boolean;
37  sidebarEnabled: boolean;
38  paneOpen: boolean;
39  paneDrawn: boolean;
40  reopenAsked: boolean;
41  themeLight: boolean;
42  paneInline: boolean;
43  sessionActive: boolean;
44  /** T-531: status.json's `state`, `ticket`, `claudeStatus` and `observedAt`; null when absent or malformed. */
45  sessionState: string | null;
46  sessionTicket: string | null;
47  sessionClaudeStatus: string | null;
48  sessionObservedAt: string | null;
49  /**
50   * T-532: status.json's `currentIssue`, which ISSUE_FIX carries in place of
51   * `ticket`. `sessionIssue` is its display id, falling back to its id;
52   * `sessionIssueId` is the id alone. Null when absent or malformed.
53   */
54  sessionIssue: string | null;
55  sessionIssueId: string | null;
56  contextPercent: number | null;
57  /** T-532: when a tool call last read the context fill; null before the first. */
58  contextReadAt: number | null;
59  /**
60   * T-532: bumped by every context read and every session start. A read's
61   * result is applied only while its number is still the latest. Monotonic:
62   * never reset.
63   */
64  contextSeq: number;
65  warm: boolean;
66  uiAvailable: boolean;
67  noLedger: boolean;
68  saidNoUi: boolean;
69  saidNoLedger: boolean;
70  saidScanFailed: boolean;
71  saidRootUnresolved: boolean;
72  /**
73   * T-537: the origins the setup hint has already been given for. Once per
74   * session and root, so unlike the said-once flags it is NOT cleared by a
75   * reload's session start: the same root stays quiet, a new one speaks once.
76   */
77  setupHintRoots: Set<string>;
78  initialCwd: string | null;
79  ledgerRoot: string | null;
80}
81export function createDashboardState(): DashboardState {
82  return {
83    cache: {},
84    cacheLoaded: false,
85    projection: null,
86    project: "",
87    phases: [],
88    handoverFilenames: [],
89    queue: [],
90    idleTicks: 0,
91    polledMtimes: {},
92    scanInitializing: false,
93    scanActive: false,
94    pendingRefresh: false,
95    ticking: false,
96    timerStarted: false,
97    logoElapsed: 0,
98    logoStarted: false,
99    logoFinished: false,
100    logoTicks: 0,
101    motion: new DashboardMotion(),
102    bandDrawn: false,
103    sidebarEnabled: false,
104    paneOpen: false,
105    paneDrawn: false,
106    reopenAsked: false,
107    themeLight: false,
108    paneInline: false,
109    sessionActive: false,
110    sessionState: null,
111    sessionTicket: null,
112    sessionClaudeStatus: null,
113    sessionObservedAt: null,
114    sessionIssue: null,
115    sessionIssueId: null,
116    contextPercent: null,
117    contextReadAt: null,
118    contextSeq: 0,
119    warm: false,
120    uiAvailable: true,
121    noLedger: false,
122    saidNoUi: false,
123    saidNoLedger: false,
124    saidScanFailed: false,
125    saidRootUnresolved: false,
126    setupHintRoots: new Set(),
127    initialCwd: null,
128    ledgerRoot: null,
129  };
130}
131
hooks/dashboard-view.ts 740 lines
1import type { DashboardState } from "./dashboard-state.js";
2import type { SidebarBoardCard, SidebarProjection } from "./sidebar-projection.js";
3import { graphemes, cellWidth, truncate } from "./terminal-text.js";
4import { stageLabel, stageStale } from "./stage-label.js";
5const COLUMN_CARD_CAP = 6;
6const COLUMN_TAIL = "...";
7const BOARD_COLUMNS = 3;
8/** What a column with nothing in it says, rather than drawing a blank frame. */
9const EMPTY_COLUMNS = {
10  "board-open": { symbol: "◇", title: "Queue clear", short: "Queue clear", hint: "Add your next story." },
11  "board-inprogress": { symbol: "○", title: "Ready to begin", short: "Ready to start", hint: "Start a story from Open." },
12  "board-done": { symbol: "·", title: "Progress starts here", short: "Room to grow", hint: "Completed work gathers here." },
13} as const;
14type BoardColumnKey = keyof typeof EMPTY_COLUMNS;
15/**
16* The rows the pane spends on everything that is not a card: the header, the
17* two blank rows around the board and the footer, and per column the heading
18* box (text plus its two border rows) and the body box's two border rows.
19*/
20const CHROME_ROWS = 4;
21const GAP_ROWS = 2;
22/** Card rows per column below which the blank rows are not worth their cost. */
23const GAPS_MIN_BODY = 3;
24/** The card's two border rows, its heading row and the rule under it. */
25const COLUMN_FRAME_ROWS = 4;
26/** Each column is a bordered card, and the border costs a column each side. */
27const COLUMN_BORDER = "round";
28const BORDER_COLUMNS = 2;
29/** The rule under a heading, and the gaps between the three columns. */
30const HEADING_RULE = "\u2500";
31const COLUMN_GAP = 1;
32const GAP_TOTAL = COLUMN_GAP * (BOARD_COLUMNS - 1);
33/**
34* From this width, In progress is widened and Done narrowed by about a
35* twentieth of the pane: at that size there is room to weight the board
36* toward the column being worked rather than the one already finished.
37*/
38const WIDE_COLUMNS = 158;
39const WIDE_SHIFT = 0.05;
40const MIN_COLUMN_WIDTH = 12;
41/**
42* The severity buckets in the order the footer names them, with the colour a
43* nonzero one carries and the short label it falls back to.
44*/
45/** What the issues line says when every bucket is empty. */
46const NO_ISSUES = "issues: none";
47const SEVERITY_ORDER = [
48  { key: "critical", long: "critical", short: "crit", tone: "red" },
49  { key: "high", long: "high", short: "high", tone: "yellow" },
50  { key: "medium", long: "medium", short: "med", tone: null },
51  { key: "low", long: "low", short: "low", tone: null },
52] as const;
53/**
54* The colour an issue's id carries on a board row, by severity.
55*
56* The same two tones the footer already uses for the same two buckets, so one
57* red on the board means what a red in the issues line means. Medium and low
58* carry none: a column where every row is coloured marks nothing.
59*/
60const SEVERITY_TONES: Readonly<Record<string, string>> = { critical: "red", high: "yellow" };
61/**
62* How each column's heading is drawn.
63*
64* One column is emphasised and it is the one that says what is happening
65* now: In progress, bold and cyan. Open is plain, being the resting state and the
66* column a reader lands on most; Done recedes, since finished work is
67* reference rather than news. Counts use a quieter weight inside each heading.
68*/
69const COLUMN_STYLES = {
70  open: {},
71  inProgress: { color: "cyan", bold: true },
72  done: { dimColor: true },
73} as const;
74/**
75* Cells kept clear to the right of the pane's own rows.
76*
77* The engine draws its close mark in the last cell of the pane, and the
78* context fill was right-aligned straight into it: live it read "context 7%×",
79* with the mark looking like part of our string. `BoxProps` carries
80* `marginRight`, so the row simply stops short of the edge. The fill now sits
81* at the foot rather than the head, and keeps the clearance there.
82*/
83const PANE_EDGE_CLEARANCE = 3;
84/**
85* Narrower than this and three columns are shredded rather than laid out, so
86* the pane draws the narrow board instead (ISS-1252): the work in hand and
87* the footer, nothing else. A placed pane keeps its seat when the window
88* shrinks (inline on the main screen, docked in fullscreen), so this is the
89* in-between case: a pane that exists but is too narrow to be a board.
90*/
91const BOARD_MIN_COLUMNS = 60;
92/** Cards the narrow board shows before it says how many more there are. */
93const NARROW_BOARD_CARDS = 3;
94/** The ledger directory itself, which is what says a project HAS a ledger. */
95export function paneText(dashboard: DashboardState, Text: (props: Record<string, unknown>) => unknown, props: Record<string, unknown>): unknown {
96  // Inline, the pane is on the terminal's own background and the default
97  // foreground is the one that matches it (ISS-1255); only the dock is painted.
98  if (dashboard.paneInline)
99    return Text(props);
100  return Text({ color: dashboard.themeLight ? "black" : "white", ...props });
101}
102type PanePlacement = "dock" | "inline";
103export function panePlacement(e: any): PanePlacement | null {
104  const value: unknown = e?.props?.placement;
105  return value === "dock" || value === "inline" ? value : null;
106}
107/**
108* The board's shape (ISS-1254): `stacked` is the three framed columns one
109* under another, the sidebar; `columns` is the three side by side; `narrow`
110* is the In progress strip. Docked, always stacked. Inline, side by side
111* from BOARD_MIN_COLUMNS up and the strip below it. No placement reported,
112* the width alone decides between stacked and side by side, as 1.15.4 did.
113*/
114type BoardLayout = "stacked" | "columns" | "narrow";
115export function boardLayout(placement: PanePlacement | null, width: number): BoardLayout {
116  if (placement === "dock")
117    return "stacked";
118  if (width >= BOARD_MIN_COLUMNS)
119    return "columns";
120  return placement === "inline" ? "narrow" : "stacked";
121}
122/**
123* How many card rows each column may draw, and whether the pane can afford
124* the blank rows around the board at all.
125*
126* The pane clips what will not fit, silently, so the budget is counted out
127* before anything is drawn:
128*
129*   header 1, header gap 1, footer gap 1, footer 1   = 4 chrome rows
130*   the card's border, above and below              = 2
131*   its heading row and the rule under it           = 2
132*
133* Side by side the three columns are parallel, so one column's four frame rows
134* are the board's; stacked they run one after another, so the frames cost
135* three times that and the rows left over are shared between them. The blank
136* rows go before the cards do, because a board with one card in it still says
137* something and a gap says nothing. When even three framed headings will not
138* fit, the board falls back to one plain counted row per column, which is the
139* smallest thing that is still the board.
140*
141* What comes back is the rows one BODY may draw, tail included; the board
142* decides how many of those are cards once it knows whether anything was left
143* out.
144*
145* WHY `bodyRows` IS NOT THE ROOM. The owner reloaded a 213 column, 61 row
146* terminal and got the compact fallback where the build before drew eight
147* cards a column. `scroll.bodyRows` is not the height the surface has for us:
148* `SiteScroll` is "where a site's window sits over THE TREE A HOOK DREW in
149* it", and `ui.scroll` spells the same field "how many rows of the tree the
150* window shows at once, AS DRAWN NOW", with `contentRows` beside it and
151* "the window's last offset is `contentRows - bodyRows`, none when the tree
152* fits". A tree that fits is its own window, so `bodyRows` is the height of
153* what we last drew. Reading it as a cap is a ratchet: one short board makes
154* the next budget shorter, the compact fallback draws six rows, and the pane
155* reports six rows for ever after.
156*
157* So the field is allowed to PROVE room and never to deny it. The cap comes
158* from `viewport.rows`, which is the whole surface and so an honest upper
159* bound on the pane ("cells down the whole surface, not the room left for
160* this component"), and `bodyRows` only raises that. Above the rows the whole
161* board needs neither matters and the layout is the one the owner had before
162* any budget existed: the capped cards, a tail, and the blank rows.
163*/
164export function rowBudget(e: any, stacked: boolean): {
165  body: number;
166  gaps: boolean;
167  compact: boolean;
168} {
169  const frames = stacked ? COLUMN_FRAME_ROWS * BOARD_COLUMNS : COLUMN_FRAME_ROWS;
170  const share = stacked ? BOARD_COLUMNS : 1;
171  const whole = CHROME_ROWS + frames + share * (COLUMN_CARD_CAP + 1);
172  const drawn: number = typeof e.props?.scroll?.bodyRows === "number" ? e.props.scroll.bodyRows : 0;
173  const screen: number = typeof e.viewport?.rows === "number" ? e.viewport.rows : 0;
174  const room = Math.max(drawn, screen);
175  if (room <= 0 || room >= whole)
176    return { body: COLUMN_CARD_CAP + 1, gaps: true, compact: false };
177  // With the blank rows first, but only while they are affordable: below
178  // GAPS_MIN_BODY rows of cards per column the gaps are costing more than
179  // they are worth, and a board with cards in it beats a tidy empty one.
180  for (const [gaps, floor] of [[true, GAPS_MIN_BODY], [false, 1]] as const) {
181    const chrome = CHROME_ROWS - (gaps ? 0 : GAP_ROWS);
182    const left = room - chrome - frames;
183    if (left >= share * floor) {
184      return { body: Math.min(COLUMN_CARD_CAP + 1, Math.floor(left / share)), gaps, compact: false };
185    }
186  }
187  return { body: 0, gaps: false, compact: true };
188}
189/**
190* A heading that keeps its count when the column is too narrow for both.
191*
192* The count is the point of the heading, so the label is what gets cut:
193* "In progress 100" at fourteen cells is "In progr… 100", never
194* "In progress 1…", which would quietly report a different number.
195*/
196function headingContent(dashboard: DashboardState, elements: any, label: string, count: number, width: number, stage: StageMark | null = null): unknown[] {
197  const tail = ` ${count}`;
198  const emphasis = dashboard.motion.count(label === "In progress" ? "inProgress" : label === "Done" ? "done" : "open");
199  const tag = headingTag(label, count, stage, width);
200  return [
201    paneText(dashboard, elements.Text, { children: truncate(label, Math.max(1, width - tail.length)) }),
202    paneText(dashboard, elements.Text, { dimColor: !emphasis, bold: emphasis, children: tail }),
203    ...(tag === "" ? [] : [paneText(dashboard, elements.Text, { ...(stage!.stale ? { dimColor: true } : { color: "cyan" }), children: tag })]),
204  ];
205}
206/**
207* T-532: the stage after a heading, " [Code]", for a session no drawn card
208* carries. Only whole, after the whole label and count: the tag is what goes
209* first, so the count is never cut to make room for it. Empty otherwise.
210*/
211function headingTag(label: string, count: number, stage: StageMark | null, width: number): string {
212  if (stage === null || stage.key !== null)
213    return "";
214  const tag = ` ${stage.text.trimEnd()}`;
215  return cellWidth(label) + cellWidth(` ${count}`) + cellWidth(tag) <= width ? tag : "";
216}
217/**
218* T-531: the stage an autonomous session is in and where it is drawn. `key` is
219* the stable key of the drawn card that carries it, or null when no drawn card
220* does and the In progress heading carries it instead (T-532); `text` is the
221* card's tag with its trailing space; `stale` dims it.
222*/
223interface StageMark {
224  readonly key: string | null;
225  readonly text: string;
226  readonly stale: boolean;
227}
228/**
229* The session's stage for an In progress column that draws its first `shown`
230* cards, or null when no session is active or its state did not parse.
231*
232* The card is resolved against the whole column first and only then asked
233* whether it is drawn: a twin past the cap must still make a drawn card
234* ambiguous, and a capped card must not hand its tag to a drawn look-alike.
235* Unresolved, ambiguous or cut by the cap, the stage goes to the heading.
236*/
237function sessionStage(dashboard: DashboardState, cards: readonly SidebarBoardCard[], shown: number, now = Date.now()): StageMark | null {
238  const state = dashboard.sessionState;
239  if (!dashboard.sessionActive || state === null)
240    return null;
241  const card = sessionCard(dashboard, cards);
242  const stale = stageStale(dashboard.sessionObservedAt, now);
243  return {
244    key: card !== null && cards.indexOf(card) < shown ? card.key : null,
245    text: `[${stageLabel(state)}${stale ? "?" : ""}] `,
246    stale,
247  };
248}
249/**
250* The one In progress card that is the session's item, or null.
251*
252* A ticket session matches ticket cards only, by display id (falling back to
253* the id), and two that share it match neither: guessing would put the stage
254* on work the session is not doing. An ISSUE_FIX session (T-532) carries no
255* ticket and matches issue cards only: by its canonical id first, which no
256* reconcile can duplicate, and by display id only when no card has that id.
257*/
258function sessionCard(dashboard: DashboardState, cards: readonly SidebarBoardCard[]): SidebarBoardCard | null {
259  const only = (matches: readonly SidebarBoardCard[]): SidebarBoardCard | null => matches.length === 1 ? matches[0]! : null;
260  const ticket = dashboard.sessionTicket;
261  if (ticket !== null)
262    return only(cards.filter((card) => card.kind === "ticket" && card.id === ticket));
263  const issueId = dashboard.sessionIssueId;
264  const byKey = issueId === null ? null : only(cards.filter((card) => card.kind === "issue" && card.key === `issue:${issueId}`));
265  if (byKey !== null)
266    return byKey;
267  const issue = dashboard.sessionIssue;
268  return issue === null ? null : only(cards.filter((card) => card.kind === "issue" && card.id === issue));
269}
270/**
271* IDs lead in muted text; titles carry emphasis and blockers stay visible.
272*
273* The session's stage tag (T-531) sits after the id, ahead of any marker, and
274* only when the whole id, the tag, the marker and two title cells fit: the
275* title is what gets cut, and the tag goes whole before the id would be. A card
276* without it draws exactly the three runs it always did.
277*/
278function cardRow(dashboard: DashboardState, elements: any, card: SidebarBoardCard, width: number, done = false, stage: StageMark | null = null): unknown {
279  const effect = dashboard.motion.card(card.key);
280  const ready = effect.ready && !card.blocked && !done;
281  // T-486 U2-5: a withdrawn ticket is finished but was never delivered; its Done card says so.
282  const withdrawn = done && card.withdrawn ? (width >= 32 ? "[Withdrawn] " : "[W] ") : "";
283  const blocked = withdrawn !== "" ? withdrawn : card.blocked ? (width >= 32 ? "[Blocked] " : "[!] ") : ready ? (width >= 32 ? "✓ Ready " : "✓ ") : "";
284  const tag = stage !== null && stage.key === card.key
285    && cellWidth(card.id) + 1 + cellWidth(stage.text) + cellWidth(blocked) + 2 <= width ? stage.text : "";
286  const id = truncate(card.id, Math.max(1, width - cellWidth(tag) - cellWidth(blocked) - 3));
287  const room = Math.max(1, width - cellWidth(id) - cellWidth(tag) - cellWidth(blocked) - 1);
288  const tone = card.kind === "issue" && card.severity !== null ? SEVERITY_TONES[card.severity] : undefined;
289  return paneText(dashboard, elements.Text, {
290    key: card.key,
291    wrap: "truncate",
292    children: [
293      paneText(dashboard, elements.Text, { dimColor: true, ...(tone ? { color: tone } : {}), children: `${id} ` }),
294      ...(tag === "" ? [] : [paneText(dashboard, elements.Text, { ...(stage!.stale ? { dimColor: true } : { color: "cyan" }), children: tag })]),
295      paneText(dashboard, elements.Text, { ...(card.blocked ? { color: "yellow" } : withdrawn !== "" ? { dimColor: true } : { dimColor: effect.fading }), children: blocked }),
296      paneText(dashboard, elements.Text, { bold: !done, dimColor: done, children: shimmerTitle(dashboard, elements, truncate(card.title, room), effect.progress) }),
297    ],
298  });
299}
300/** Preserve graphemes and cell widths while a narrow highlight crosses the text. */
301function shimmerTitle(dashboard: DashboardState, elements: any, title: string, progress: number | null): unknown {
302  if (progress === null)
303    return title;
304  const start = Math.round(progress * (cellWidth(title) + 6)) - 6;
305  const runs: {
306    text: string;
307    highlight: boolean;
308  }[] = [];
309  let position = 0;
310  for (const { cluster, cells } of graphemes(title)) {
311    const highlight = position >= start && position < start + 6;
312    const last = runs[runs.length - 1];
313    if (last?.highlight === highlight)
314      last.text += cluster;
315    else
316      runs.push({ text: cluster, highlight });
317    position += cells;
318  }
319  return runs.map((run, index) => paneText(dashboard, elements.Text, {
320    key: `shimmer-${index}`, ...(run.highlight ? { dimColor: false, bold: true, underline: true } : {}), children: run.text,
321  }));
322}
323/**
324* The rows of one column's body, every body the same height.
325*
326* The three bodies draw the same number of rows, so the cards end level
327* instead of leaving a ragged edge: a column with fewer cards is padded with
328* blanks. Empty columns use that same height for a quiet, centered invitation.
329* The tail row is part of that common height, held
330* back by the budget, so a capped column can say it was capped without
331* standing a row taller than the rest.
332*/
333function bodyRowsOf(dashboard: DashboardState, elements: any, cards: readonly SidebarBoardCard[], width: number, shown: number, height: number, column: BoardColumnKey, stage: StageMark | null = null): unknown[] {
334  const rows: unknown[] = [];
335  if (cards.length === 0) {
336    return emptyColumnRows(dashboard, elements, column, width, height);
337  }
338  else {
339    for (const card of cards.slice(0, shown))
340      rows.push(cardRow(dashboard, elements, card, width, column === "board-done", stage));
341    if (cards.length > shown)
342      rows.push(paneText(dashboard, elements.Text, { dimColor: true, wrap: "truncate", children: COLUMN_TAIL }));
343  }
344  while (rows.length < height)
345    rows.push(paneText(dashboard, elements.Text, { children: " " }));
346  return rows;
347}
348function emptyColumnRows(dashboard: DashboardState, elements: any, column: BoardColumnKey, width: number, height: number, centered = true): unknown[] {
349  const state = EMPTY_COLUMNS[column];
350  const title = cellWidth(`${state.symbol} ${state.title}`) <= width ? state.title : state.short;
351  const lines = [`${state.symbol} ${title}`];
352  if (height >= 3 && cellWidth(state.hint) <= width)
353    lines.push(state.hint);
354  const top = centered ? Math.floor((height - lines.length) / 2) : 0;
355  return Array.from({ length: height }, (_, index) => {
356    const line = truncate(lines[index - top] ?? " ", width);
357    const inset = centered ? Math.max(0, Math.floor((width - cellWidth(line)) / 2)) : 0;
358    return paneText(dashboard, elements.Text, {
359      key: `${column}-empty-${index}`, dimColor: true, wrap: "truncate", children: " ".repeat(inset) + line,
360    });
361  });
362}
363/**
364* One column: a bordered card with its heading at the top, a rule under the
365* heading, and the card rows beneath.
366*
367* One box and not two. The heading is enclosed by the card's own top and side
368* borders and the rule below it, which is the divider; two stacked bordered
369* boxes drew a double line between heading and body. The rule is a Text of
370* box-drawing dashes spanning the inner width, so it meets both side borders;
371* it cannot render the ├ and ┤ junctions, since a child of the box cannot
372* reach into the border cells the renderer owns.
373*
374* The count in the heading is the WHOLE column, not the rows drawn, so a
375* capped column still tells the truth about the project; the tail says the
376* column goes on. Titles are cut to the column's width, not the pane's.
377*
378* Takes the resolved element table rather than `$`: these are plain
379* constructors, and the client's scan is strict about where `$` may travel.
380*/
381export function inProgressBoard(dashboard: DashboardState, elements: any, width: number, body: number): unknown {
382  const cards = dashboard.projection?.board.inProgress ?? [];
383  const shown = Math.min(COLUMN_CARD_CAP, Math.max(0, body - (cards.length > body ? 1 : 0)));
384  return boardColumn(dashboard, elements, "board-inprogress", "In progress", COLUMN_STYLES.inProgress,
385    cards, width, shown, Math.max(1, Math.min(body, cards.length || 2)), sessionStage(dashboard, cards, shown));
386}
387
388function boardColumn(dashboard: DashboardState, elements: any, key: BoardColumnKey, heading: string, style: Readonly<Record<string, unknown>>, cards: readonly SidebarBoardCard[], width: number, shown: number, height: number, stage: StageMark | null = null): unknown {
389  // The border takes a column on each side, so the text inside has that much
390  // less. Getting this wrong wraps every row and the board falls apart.
391  const textWidth = Math.max(1, width - BORDER_COLUMNS);
392  return elements.Box({
393    key,
394    flexDirection: "column",
395    borderStyle: COLUMN_BORDER,
396    width,
397    overflow: "hidden",
398    children: [
399      paneText(dashboard, elements.Text, {
400        key: `${key}-heading`,
401        ...style,
402        wrap: "truncate",
403        children: headingContent(dashboard, elements, heading, cards.length, textWidth, stage),
404      }),
405      paneText(dashboard, elements.Text, { key: `${key}-rule`, dimColor: true, wrap: "truncate", children: HEADING_RULE.repeat(textWidth) }),
406      ...bodyRowsOf(dashboard, elements, cards, textWidth, shown, height, key, stage),
407    ],
408  });
409}
410/**
411* The board reduced to three counted rows, when no frame will fit. No card is
412* drawn, so a session's stage rides on the In progress row.
413*/
414export function compactBoard(dashboard: DashboardState, elements: any, board: SidebarProjection["board"], width: number): unknown {
415  const line = (key: string, label: string, style: Readonly<Record<string, unknown>>, cards: readonly SidebarBoardCard[], stage: StageMark | null = null): unknown => paneText(dashboard, elements.Text, { key, ...style, wrap: "truncate", children: headingContent(dashboard, elements, label, cards.length, width, stage) });
416  return elements.Box({
417    key: "board",
418    flexDirection: "column",
419    children: [
420      line("board-open", "Open", COLUMN_STYLES.open, board.open),
421      line("board-inprogress", "In progress", COLUMN_STYLES.inProgress, board.inProgress, sessionStage(dashboard, board.inProgress, 0)),
422      line("board-done", "Done", COLUMN_STYLES.done, board.done),
423    ],
424  });
425}
426/**
427* The narrow board (ISS-1252): below BOARD_MIN_COLUMNS the three framed
428* columns stacked into a strip the person had to scroll, past a cut-off Open
429* card and an empty In progress frame. Owner: "in that view we can just show
430* top 3 in progress and context pressure." So this draws the work in hand
431* only: the In progress heading with its count, up to NARROW_BOARD_CARDS
432* cards, a tail when more exist. Open and Done keep their counts in
433* the band's summary line under the pane. At most five rows, so the client's
434* inline block shows it whole, without a budget.
435*
436* Nothing in progress and the strip shows the Open column the same way
437* (ISS-1254): the owner's project had no work in hand and the strip said
438* "In progress 0, none", which is a count and not the work; the next thing to
439* pick up is what the strip is for then. An active session's stage (T-532)
440* still gets a row there, "In progress 0 [Pick]", above Open, which gives up
441* a card for it so the strip stays five rows; the Open cards never carry it.
442*/
443export function narrowBoard(dashboard: DashboardState, elements: any, board: SidebarProjection["board"], width: number): unknown {
444  const inProgress = board.inProgress as readonly SidebarBoardCard[];
445  const fallback = inProgress.length === 0;
446  const cards = fallback ? (board.open as readonly SidebarBoardCard[]) : inProgress;
447  const stage = sessionStage(dashboard, inProgress, NARROW_BOARD_CARDS);
448  const stageRow = fallback && headingTag("In progress", 0, stage, width) !== "";
449  const limit = stageRow ? NARROW_BOARD_CARDS - 1 : NARROW_BOARD_CARDS;
450  const rows: unknown[] = [];
451  if (stageRow) {
452    rows.push(paneText(dashboard, elements.Text, {
453      key: "narrow-stage-heading",
454      ...COLUMN_STYLES.inProgress,
455      wrap: "truncate",
456      children: headingContent(dashboard, elements, "In progress", 0, width, stage),
457    }));
458  }
459  rows.push(paneText(dashboard, elements.Text, {
460    key: "narrow-heading",
461    ...(fallback ? COLUMN_STYLES.open : COLUMN_STYLES.inProgress),
462    wrap: "truncate",
463    children: headingContent(dashboard, elements, fallback ? "Open" : "In progress", cards.length, width, fallback ? null : stage),
464  }));
465  if (cards.length === 0) {
466    rows.push(...emptyColumnRows(dashboard, elements, fallback ? "board-open" : "board-inprogress", width, 1, false));
467  }
468  else {
469    // The Open fallback is not work in hand, so its cards never carry the stage.
470    cards.slice(0, limit).forEach((card, index) => {
471      rows.push(elements.Box({ key: `narrow-card-${index}`, children: [cardRow(dashboard, elements, card, width, false, fallback ? null : stage)] }));
472    });
473    if (cards.length > limit) {
474      rows.push(paneText(dashboard, elements.Text, {
475        key: "narrow-tail",
476        dimColor: true,
477        wrap: "truncate",
478        children: truncate(`... ${cards.length - limit} more`, width),
479      }));
480    }
481  }
482  return elements.Box({ key: "board", flexDirection: "column", children: rows });
483}
484/**
485* The three column widths.
486*
487* Stacked, every card takes the pane. Side by side, the width less the two
488* gaps splits three ways, the leftover cells going to Open; from WIDE_COLUMNS
489* up, a twentieth of the pane moves from Done to In progress, so the board
490* leans toward the work in hand. The three widths and the gaps always sum to
491* the pane's width, whatever the arithmetic above did.
492*/
493function columnWidths(width: number, stacked: boolean): number[] {
494  if (stacked)
495    return [width, width, width];
496  const base = Math.max(MIN_COLUMN_WIDTH, Math.floor((width - GAP_TOTAL) / BOARD_COLUMNS));
497  const widths = [base, base, base];
498  widths[0] = (widths[0] ?? base) + Math.max(0, width - GAP_TOTAL - base * BOARD_COLUMNS);
499  if (width >= WIDE_COLUMNS) {
500    const shift = Math.min(Math.round(width * WIDE_SHIFT), (widths[2] ?? base) - MIN_COLUMN_WIDTH);
501    if (shift > 0) {
502      widths[1] = (widths[1] ?? base) + shift;
503      widths[2] = (widths[2] ?? base) - shift;
504    }
505  }
506  return widths;
507}
508export function boardNode(dashboard: DashboardState, elements: any, board: SidebarProjection["board"], width: number, stacked: boolean, body: number): unknown {
509  const widths = columnWidths(width, stacked);
510  // Every body the same height, and that height inside the budget: as many
511  // rows as the fullest column can show, one of them given up to the tail
512  // when anything was left out, so a capped column says so without standing a
513  // row taller than the rest.
514  const columns = [board.open, board.inProgress, board.done] as const;
515  const longest = Math.max(...columns.map((column) => column.length));
516  // Never more than the cap, whatever the budget allows: a body of seven rows
517  // is six cards and a tail, not seven cards.
518  let shown = Math.min(body, longest, COLUMN_CARD_CAP);
519  if (columns.some((column) => column.length > shown))
520    shown = Math.max(0, Math.min(shown, body - 1));
521  const omitted = columns.some((column) => column.length > shown);
522  const height = Math.max(1, Math.min(body, shown + (omitted ? 1 : 0)));
523  // Left to right in the order the work moves: what can be
524  // picked up, what is being done, what is finished.
525  return elements.Box({
526    key: "board",
527    flexDirection: stacked ? "column" : "row",
528    gap: stacked ? 0 : COLUMN_GAP,
529    children: [
530      boardColumn(dashboard, elements, "board-open", "Open", COLUMN_STYLES.open, board.open, widths[0]!, shown, height),
531      boardColumn(dashboard, elements, "board-inprogress", "In progress", COLUMN_STYLES.inProgress, board.inProgress, widths[1]!, shown, height, sessionStage(dashboard, board.inProgress, shown)),
532      boardColumn(dashboard, elements, "board-done", "Done", COLUMN_STYLES.done, board.done, widths[2]!, shown, height),
533    ],
534  });
535}
536/**
537* The header keeps identity and an animated wordmark on one row.
538* Context pressure stays in the footer.
539*/
540/**
541* The Mod's own version, drawn in the header (ISS-1266). The Mod cannot
542* read package.json through the client, so this is a literal, bumped with
543* the two plugin manifests on every release; test/plugin/sidebar-validate
544* holds it equal to package.json so a forgotten bump fails before publish.
545*/
546export const MOD_VERSION = "1.16.0";
547function wordmarkNode(dashboard: DashboardState, elements: any): unknown {
548  const sweep = dashboard.motion.activity().sweep;
549  return paneText(dashboard, elements.Text, { key: "wordmark", bold: true,
550            children: sweep === null ? "Storybloq" : [..."Storybloq"].map((letter, index) => {
551              // A soft Gaussian crest blends ivory, champagne, and copper without
552              // toggling font weight or terminal dimness at band edges.
553              const distance = index - (sweep - 3);
554              const blend = Math.exp(-(distance * distance) / 8);
555              // Darker warm equivalents keep the light theme readable.
556              const base = dashboard.themeLight ? [91, 77, 65] : [224, 216, 202];
557              const middle = dashboard.themeLight ? [119, 91, 63] : [219, 198, 165];
558              const crest = dashboard.themeLight ? [139, 91, 63] : [203, 165, 137];
559              const from = blend < 0.75 ? base : middle;
560              const to = blend < 0.75 ? middle : crest;
561              const mix = blend < 0.75 ? blend / 0.75 : (blend - 0.75) / 0.25;
562              const color = "#" + from.map((value, channel) =>
563                Math.round(value + (to[channel]! - value) * mix).toString(16).padStart(2, "0")
564              ).join("");
565              return paneText(dashboard, elements.Text, {
566                key: `wordmark-${index}`, bold: true, color, children: letter,
567              });
568            }),
569          });
570}
571
572export function compactLineNode(dashboard: DashboardState, elements: any, columns: number): unknown {
573  const width = Math.max(1, Math.floor(columns) - PANE_EDGE_CLEARANCE);
574  const contextWidth = Math.min(width, 24);
575  const context = contextLabel(dashboard.contextPercent, contextWidth);
576  const room = width - cellWidth(context) - 3;
577  const text = (props: Record<string, unknown>) => paneText(dashboard, elements.Text, props);
578  if (room < 12) return contextNode(dashboard, elements, dashboard.contextPercent, width);
579  const item = dashboard.projection?.board.inProgress[0];
580  const runs: unknown[] = [wordmarkNode(dashboard, elements)];
581  let used = 9;
582  if (room >= 30) {
583    const status = item ? "In progress" : "Ready";
584    runs.push(text({ dimColor: true, children: "  │  " }));
585    runs.push(text({ color: item ? "cyan" : undefined, children: status }));
586    used += 5 + status.length;
587    if (item && room - used >= cellWidth(item.id) + 8) {
588      runs.push(text({ dimColor: true, children: `  ${item.id}  ` }));
589      used += cellWidth(item.id) + 4;
590      if (item.blocked && room - used >= 15) {
591        runs.push(text({ color: "yellow", children: "[Blocked] " }));
592        used += 10;
593      }
594      const title = truncate(item.title, Math.min(58, room - used));
595      runs.push(text({ bold: true, children: title }));
596      used += cellWidth(title);
597    }
598  }
599  runs.push(text({ children: " ".repeat(width - used - cellWidth(context)) }));
600  runs.push(contextNode(dashboard, elements, dashboard.contextPercent, contextWidth));
601  return text({ key: "compact-line", wrap: "truncate", children: runs });
602}
603
604export function headerNode(dashboard: DashboardState, elements: any): unknown {
605  // "Storybloq (1.15.8) - CPM" (ISS-1266): the wordmark, the version faint,
606  // then the project in the wordmark's own weight. The project is the folder
607  // the ledger sits in, so two checkouts of one project read apart; the
608  // config's `project` name would say "storybloq" for CPM.
609  const name = projectFolderName(dashboard);
610  return elements.Box({
611    key: "header",
612    flexDirection: "row",
613    alignItems: "center",
614    marginRight: PANE_EDGE_CLEARANCE,
615    children: [
616      paneText(dashboard, elements.Text, {
617        wrap: "truncate",
618        children: [
619          wordmarkNode(dashboard, elements),
620          paneText(dashboard, elements.Text, { key: "version", dimColor: true, children: ` (${MOD_VERSION})` }),
621          ...(name === "" ? [] : [paneText(dashboard, elements.Text, { key: "project", bold: true, children: ` - ${name}` })]),
622        ],
623      }),
624    ],
625  });
626}
627/** The last segment of the pinned ledger root, or nothing before one is pinned. */
628function projectFolderName(dashboard: DashboardState): string {
629  if (dashboard.ledgerRoot === null)
630    return "";
631  const segments = dashboard.ledgerRoot.replace(/[/\\]+$/, "").split(/[/\\]/);
632  return segments[segments.length - 1] ?? "";
633}
634/**
635* The foot of the pane: the issues breakdown flush left, the context fill
636* right-aligned on the same row.
637*
638* The four buckets are always all there, so the shape of the line does not
639* move about; what changes is the weight. A zero bucket is dim and a nonzero
640* one is not, critical reads red and high yellow when they have anything in
641* them, and the separators are dim throughout, so the eye lands on the
642* severities that exist. The context fill is neutral and gets its width
643* first, the issues line taking what is left and going to the short labels
644* when the long ones will not fit.
645*
646* No right margin here: the engine's close mark is a top-right thing, and the
647* header is what keeps clear of it.
648*/
649export function contextLabel(context: number | null, width: number, meterValue = context): string {
650  if (width <= 0)
651    return "";
652  if (context === null || !Number.isFinite(context))
653    return truncate(width < 6 ? "--" : width < 14 ? "-- ctx" : "context --", width);
654  const value = Math.max(0, Math.min(100, Math.round(context)));
655  if (width < 14) {
656    const compact = `${value}% ctx`;
657    return truncate(cellWidth(compact) <= width ? compact : `${value}%`, width);
658  }
659  const cells = width >= 48 ? 8 : width >= 32 ? 4 : 0;
660  const meterPercent = meterValue !== null && Number.isFinite(meterValue) ? Math.max(0, Math.min(100, meterValue)) : value;
661  const filled = Math.round(meterPercent / 100 * cells);
662  const meter = cells ? ` [${"━".repeat(filled)}${"·".repeat(cells - filled)}]` : "";
663  return `context${meter} ${value}%`;
664}
665export function contextNode(dashboard: DashboardState, elements: any, context: number | null, width: number): unknown {
666  const high = context !== null && Number.isFinite(context) && context >= 80;
667  return paneText(dashboard, elements.Text, {
668    key: "context", dimColor: !high, bold: high, wrap: "truncate", children: contextLabel(context, width, dashboard.motion.meterValue()),
669  });
670}
671export function footerNode(dashboard: DashboardState, elements: any, bySeverity: Readonly<Record<string, number>>, context: number | null, width: number): unknown {
672  const contextText = contextLabel(context, width);
673  const room = Math.max(0, width - cellWidth(contextText) - 1);
674  const counts = SEVERITY_ORDER.map((severity) => bySeverity[severity.key] ?? 0);
675  // A ledger with nothing open says so in a word. Four zeros is four numbers
676  // to read before finding out there is nothing to read, and it looks like a
677  // pane that failed rather than a project with no open issues. The
678  // abbreviated row says the same word, since there is nothing to abbreviate.
679  if (counts.every((count) => count === 0)) {
680    return elements.Box({
681      key: "footer",
682      flexDirection: "row",
683      justifyContent: "space-between",
684      alignItems: "center",
685      children: [
686        elements.Box({
687          key: "issues",
688          flexDirection: "row",
689          width: Math.min(room, cellWidth(NO_ISSUES)),
690          overflow: "hidden",
691          children: [paneText(dashboard, elements.Text, { dimColor: true, wrap: "truncate", children: NO_ISSUES })],
692        }),
693        contextNode(dashboard, elements, context, width),
694      ],
695    });
696  }
697  const long = `issues: ${SEVERITY_ORDER.map((s, i) => `${counts[i]} ${s.long}`).join(", ")}`;
698  const short = SEVERITY_ORDER.map((s, i) => `${counts[i]} ${s.short}`).join(" ");
699  const abbreviated = cellWidth(long) > room;
700  const parts: unknown[] = [];
701  if (!abbreviated)
702    parts.push(paneText(dashboard, elements.Text, { children: "issues: " }));
703  SEVERITY_ORDER.forEach((severity, index) => {
704    const count = counts[index] ?? 0;
705    const props: Record<string, unknown> = {
706      children: `${count} ${abbreviated ? severity.short : severity.long}`,
707    };
708    if (count === 0)
709      props["dimColor"] = true;
710    else if (severity.tone !== null)
711      props["color"] = severity.tone;
712    if (index > 0) {
713      parts.push(paneText(dashboard, elements.Text, { dimColor: true, children: abbreviated ? " " : ", " }));
714    }
715    parts.push(paneText(dashboard, elements.Text, props));
716  });
717  return elements.Box({
718    key: "footer",
719    flexDirection: "row",
720    justifyContent: "space-between",
721    alignItems: "center",
722    children: [
723      // ONE Text, not a row of them. The coloured fragments are its children,
724      // which keeps each its colour, and the truncation is the parent's: a Box
725      // of Texts has no wrap prop to set, so when even the abbreviated buckets
726      // outgrew the room the row wrapped and the footer took two rows out of a
727      // budget counted for one. The room is the width, so the cut is the
728      // context fill's clearance and not the pane's edge.
729      elements.Box({
730        key: "issues",
731        flexDirection: "row",
732        width: Math.min(room, cellWidth(abbreviated ? short : long)),
733        overflow: "hidden",
734        children: [paneText(dashboard, elements.Text, { wrap: "truncate", children: parts })],
735      }),
736      contextNode(dashboard, elements, context, width),
737    ],
738  });
739}
740
hooks/ledger-write-detection.ts 491 lines
1/**
2 * How a ledger write is recognised at `tool.call`. The MCP names arrive
3 * prefixed by their server, the CLI's own do not; the verb at the end is what
4 * separates a write from a read.
5 */
6const MCP_PREFIX = "mcp__storybloq__";
7const LEDGER_TOOL_PREFIX = "storybloq_";
8const LEDGER_WRITE_VERB = /_(create|update|set|unset|add|init|snapshot|reinforce|supersede)$/;
9/**
10 * The built-in tools that can write a file, from this build's own tool table
11 * (`BuiltinToolInputs` in claude-code.d.ts carries Edit, Write and
12 * NotebookEdit; MultiEdit is named here for builds that have it, and costs
13 * nothing where it does not exist).
14 */
15const MUTATING_FILE_TOOLS = new Set(["Write", "Edit", "MultiEdit", "NotebookEdit"]);
16const BASH_TOOL = "Bash";
17/**
18 * The storybloq CLI's writing subcommands, the same verbs the tool names end
19 * in, and how far after `storybloq` one still counts as the subcommand
20 * (`storybloq note create`, so two words).
21 */
22const CLI_NAME = "storybloq";
23const WRITE_VERBS = ["create", "update", "set", "unset", "add", "init", "snapshot", "reinforce", "supersede"];
24const CLI_VERB_DEPTH = 2;
25/**
26 * The characters that are operators outside a quoted run, the runs of them
27 * that cut one segment from the next, and the one that redirects.
28 */
29const OPERATOR_CHARACTERS = [";", "|", "&", "\n", ">", "<"];
30const SEPARATOR_CHARACTERS = [";", "|", "&", "\n"];
31const REDIRECT = ">";
32/** `&>`, the other way of writing a redirect that takes both streams. */
33const BOTH_STREAMS = "&>";
34/** `<<` and `<<<`: past one, the line is a document rather than a command. */
35const HEREDOC = "<<";
36/** A leading `NAME=value`, which is an assignment and not the command. */
37const ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
38/** The few commands that only lead up to the one that runs. */
39const WRAPPER_COMMANDS = new Set(["env", "npx", "time", "nice", "sudo", "command"]);
40/**
41 * The options of those that take a VALUE in the next word, per wrapper.
42 *
43 * `sudo -u someone storybloq ticket update` runs the CLI, and a reader that
44 * steps over `-u` and stops at `someone` decides the command is a username.
45 * `--option=value` is one word already and needs none of this.
46 */
47const WRAPPER_VALUE_OPTIONS: Readonly<Record<string, readonly string[]>> = {
48  sudo: ["-u", "-g", "-h", "-p"],
49  nice: ["-n"],
50  env: ["-u", "-C", "-S"],
51  time: ["-f", "-o"],
52  npx: ["-p", "--package", "-c", "--call"],
53};
54/** The CLI's own global options that take the next word as their value. */
55const CLI_VALUE_OPTIONS = ["--node", "--format", "--client"];
56/** The shell commands that write, by what each of them writes. */
57const COPY_COMMANDS = new Set(["cp", "install"]);
58const MOVE_COMMAND = "mv";
59const TEE_COMMAND = "tee";
60const REMOVE_COMMAND = "rm";
61const SED_COMMAND = "sed";
62/** Where a path can arrive on a built-in file tool's event. */
63const PATH_ARGUMENTS = ["file_path", "path", "notebook_path"] as const;
64const STORY_DIR = ".story/";
65
66/** One piece of a command line: an operator, or a word to be read as one. */
67interface Token {
68  readonly text: string;
69  readonly operator: boolean;
70}
71
72/**
73 * A command line read ONCE, quotes and escapes honoured, operators kept apart
74 * from arguments.
75 *
76 * Splitting on separators before reading the quotes was the bug: `echo "x;
77 * storybloq ticket update"` came apart into two segments and the second
78 * looked like a CLI call, and `echo '>' .story/config.json` looked like a
79 * redirect into the ledger. A separator, a redirect and a quote mark only
80 * mean what they say OUTSIDE a quoted run, so there is one pass and it knows
81 * which run it is in.
82 *
83 * An unterminated quote makes the REST of the line unreadable, not the whole
84 * of it. Dropping everything was a regression: a heredoc writing a ticket
85 * (`cat > .story/tickets/T-001.json <<'EOF'`) has a body full of apostrophes,
86 * and so does a trailing `# that's it` comment, and the write on the line
87 * before them stopped sweeping. So the tokens read before the bad quote
88 * opened come back with `ok: false`, and the caller judges every segment that
89 * closed before it and discards the one the quote is in.
90 *
91 * A heredoc operator ends the read for the same reason from the other side:
92 * its head is a command and its body is data, so reading the body as shell
93 * would take words out of a document and call them a write.
94 */
95/**
96 * Steps over a heredoc's delimiter word, quoted or bare, and answers with the
97 * index of its last character, the word its terminator line must equal, and
98 * whether `<<-` allowed that line leading tabs.
99 *
100 * The delimiter's own quotes are opened and closed here rather than by the
101 * lexer's, so `<<'EOF'` does not read as a quote left open and throw the rest
102 * of the head away, and a backslash quotes the character after it as it does
103 * in the shell.
104 */
105function afterHeredocDelimiter(command: string, from: number): { index: number; delimiter: string; dashed: boolean } {
106  let index = from;
107  let delimiter = "";
108  let dashed = false;
109  for (let word = 0; word < 2; word += 1) {
110    while (index + 1 < command.length && (command[index + 1] === " " || command[index + 1] === "\t")) index += 1;
111    let quote = "";
112    while (index + 1 < command.length) {
113      const character = command[index + 1]!;
114      if (quote !== "") {
115        index += 1;
116        if (character === quote) quote = "";
117        else delimiter += character;
118        continue;
119      }
120      // A backslash quotes the next character of the delimiter, so `<<\\EOF`
121      // ends at a line reading EOF. Keeping the backslash means the
122      // terminator is never found and the rest of the script is read as body.
123      if (character === "\\") {
124        index += 1;
125        const escaped = command[index + 1];
126        if (escaped !== undefined) {
127          delimiter += escaped;
128          index += 1;
129        }
130        continue;
131      }
132      if (character === '"' || character === "'") {
133        quote = character;
134        index += 1;
135        continue;
136      }
137      if (character === " " || character === "\t" || character === "\n" || OPERATOR_CHARACTERS.includes(character)) break;
138      delimiter += character;
139      index += 1;
140    }
141    // `<<-EOF` allows leading tabs on the terminator; `<<- EOF` writes the
142    // dash as a word of its own, so one more word is read for it.
143    if (!delimiter.startsWith("-")) break;
144    dashed = true;
145    delimiter = delimiter.slice(1);
146    if (delimiter !== "") break;
147  }
148  return { index, delimiter, dashed };
149}
150
151/**
152 * Where a heredoc's body ends: the index of the newline that closes its
153 * terminator line, or -1 where the terminator never comes and the rest of the
154 * command is body.
155 *
156 * The body is never read as shell, but what follows it is: a script that
157 * writes a note and then updates a ticket is one Bash call, and stopping at
158 * the body would lose the write.
159 */
160function afterHeredocBody(command: string, from: number, delimiter: string, dashed: boolean): number {
161  let start = from + 1;
162  while (start <= command.length) {
163    const cut = command.indexOf("\n", start);
164    const last = cut === -1;
165    const end = last ? command.length : cut;
166    let line = command.slice(start, end);
167    if (line.endsWith("\r")) line = line.slice(0, -1);
168    if (dashed) line = line.replace(/^\t+/, "");
169    if (line === delimiter) return last ? command.length - 1 : end;
170    if (last) return -1;
171    start = end + 1;
172  }
173  return -1;
174}
175
176function lex(command: string): { tokens: Token[]; ok: boolean } {
177  const tokens: Token[] = [];
178  let text = "";
179  let started = false;
180  let quote = "";
181  /** How many tokens were whole when the quote now open was opened. */
182  let opened = 0;
183  /**
184   * A heredoc head is being read, so the next newline starts its body, and
185   * the word that ends it. Two heredocs on one head line (`cat <<A <<B`) are
186   * read as one body ending at the LAST delimiter, which is close enough: the
187   * body is never read either way and what follows it still is.
188   */
189  let heredoc = false;
190  let delimiter = "";
191  let dashed = false;
192  const flush = (): void => {
193    if (started) tokens.push({ text, operator: false });
194    text = "";
195    started = false;
196  };
197  for (let index = 0; index < command.length; index += 1) {
198    const character = command[index]!;
199    if (quote !== "") {
200      // Inside single quotes a backslash is a backslash; inside double quotes
201      // it escapes the next character, as the shell reads them. (Bash keeps
202      // the backslash before anything but $ \ " ` and a newline; this drops
203      // it either way, which costs a character in a path nobody writes.)
204      if (character === "\\" && quote === '"' && index + 1 < command.length) {
205        index += 1;
206        text += command[index];
207        started = true;
208        continue;
209      }
210      if (character === quote) quote = "";
211      else {
212        text += character;
213        started = true;
214      }
215      continue;
216    }
217    if (character === "\\") {
218      index += 1;
219      if (index < command.length) {
220        text += command[index];
221        started = true;
222      }
223      continue;
224    }
225    if (character === '"' || character === "'") {
226      // Where the quote opened, so an unclosed one can give back what was
227      // whole before it rather than nothing at all.
228      quote = character;
229      opened = tokens.length;
230      started = true;
231      continue;
232    }
233    if (character === " " || character === "\t" || character === "\r") {
234      flush();
235      continue;
236    }
237    if (OPERATOR_CHARACTERS.includes(character)) {
238      flush();
239      let run = character;
240      while (index + 1 < command.length && command[index + 1] === character) {
241        run += character;
242        index += 1;
243      }
244      // The body of a heredoc starts at the newline after its head and is
245      // never read: it is a document, and a line of it that looks like a write
246      // is prose someone is filing. What comes AFTER the terminator is shell
247      // again, and dropping it was losing the write in the commonest script
248      // Claude Code produces: a note written with `cat <<EOF`, then a ticket
249      // updated on the line below its EOF.
250      if (heredoc && run.startsWith("\n")) {
251        const end = afterHeredocBody(command, index, delimiter, dashed);
252        if (end === -1) return { tokens, ok: true };
253        index = end;
254        heredoc = false;
255        tokens.push({ text: "\n", operator: true });
256        continue;
257      }
258      // `<<` and `<<<`: the head is still a command and the REST OF ITS LINE
259      // still counts, because `cat <<'EOF' > .story/tickets/T-001.json` writes
260      // a ticket with the redirect sitting after the delimiter. So the
261      // operator and its delimiter are stepped over and the line goes on being
262      // read as shell.
263      if (run.startsWith(HEREDOC)) {
264        const head = afterHeredocDelimiter(command, index);
265        index = head.index;
266        // `<<<` is a here-string: its word IS the input, and there is no body
267        // to step over.
268        if (run === HEREDOC) {
269          heredoc = true;
270          delimiter = head.delimiter;
271          dashed = head.dashed;
272        }
273        continue;
274      }
275      // `>&` and `&>` are one redirect written two ways, and a run of one
276      // character would split them: the `&` would then cut the segment and
277      // the redirect would lose its target. `2>&1` is the same shape and
278      // still names no file of ours, so it neither sweeps nor cuts.
279      if (run[0] === REDIRECT && command[index + 1] === "&") {
280        run += "&";
281        index += 1;
282      } else if (run[0] === "&" && command[index + 1] === REDIRECT) {
283        while (command[index + 1] === REDIRECT) {
284          run += REDIRECT;
285          index += 1;
286        }
287      }
288      tokens.push({ text: run, operator: true });
289      continue;
290    }
291    text += character;
292    started = true;
293  }
294  flush();
295  // An unclosed quote: give back what was already whole when it opened. The
296  // word it started, and everything after, is not shell this can read.
297  if (quote !== "") return { tokens: tokens.slice(0, opened), ok: false };
298  return { tokens, ok: true };
299}
300
301/** A word that names something inside the ledger directory. */
302function inLedger(word: string | undefined): boolean {
303  return typeof word === "string" && word.includes(STORY_DIR);
304}
305
306/** The last path segment of a word, which is the name a command runs under. */
307function basename(word: string): string {
308  const cut = word.lastIndexOf("/");
309  return cut === -1 ? word : word.slice(cut + 1);
310}
311
312/**
313 * The command a segment actually runs, past the wrappers that only lead up to
314 * one, and the arguments it was given.
315 *
316 * `storybloq` counts as the CLI only HERE, in executable position: `echo
317 * storybloq ticket update` prints a sentence and writes nothing, and the two
318 * read identically to anything that only looks for the word. The wrappers are
319 * a named few (`env`, `npx`, `time`, `nice`, `sudo`, `command`) with their own
320 * options and any leading `NAME=value` assignments stepped over. Anything
321 * else in front of the command (`xargs`, a subshell, a substitution) is a
322 * line this cannot read, and it returns nothing rather than guess.
323 */
324function executableOf(words: readonly string[]): { name: string; args: string[] } | null {
325  let index = 0;
326  for (;;) {
327    while (index < words.length && ASSIGNMENT.test(words[index]!)) index += 1;
328    if (index < words.length && WRAPPER_COMMANDS.has(basename(words[index]!))) {
329      const takesValue = WRAPPER_VALUE_OPTIONS[basename(words[index]!)] ?? [];
330      index += 1;
331      while (index < words.length && words[index]!.startsWith("-")) {
332        const option = words[index]!;
333        index += 1;
334        if (!option.includes("=") && takesValue.includes(option)) index += 1;
335      }
336      continue;
337    }
338    break;
339  }
340  if (index >= words.length) return null;
341  return { name: words[index]!, args: words.slice(index + 1) };
342}
343
344/**
345 * Did one segment of a command line write the ledger?
346 *
347 * Direction is the whole question. `cat .story/tickets/T-001.json > /tmp/x`
348 * and `cp .story/tickets/T-001.json /tmp/x` both name the ledger and both
349 * write a file, and neither changes a thing we draw; only where the ledger is
350 * the DESTINATION has anything moved. So a redirect counts at its target and
351 * a copy at its last operand. `mv` counts at either end, because moving a
352 * ticket OUT of the ledger takes it off the board as surely as moving one in;
353 * `tee` counts at any of its files, and `rm` and `sed -i` at the path they
354 * are given.
355 */
356function segmentWrote(tokens: readonly Token[]): boolean {
357  const words: string[] = [];
358  for (let index = 0; index < tokens.length; index += 1) {
359    const token = tokens[index]!;
360    if (!token.operator) {
361      words.push(token.text);
362      continue;
363    }
364    // A redirect writes what follows it; `<` reads it, and the rest of the
365    // operators never reach here (they are what the segments were cut on).
366    if (token.text.startsWith(REDIRECT) || token.text.startsWith(BOTH_STREAMS)) {
367      const target = tokens[index + 1];
368      if (target !== undefined && !target.operator && inLedger(target.text)) return true;
369      index += 1;
370    }
371  }
372
373  const run = executableOf(words);
374  if (run === null) return false;
375  const name = basename(run.name);
376  const args = run.args;
377
378  // The CLI resolves `.story/` itself, so the command line need not name it;
379  // what says it writes is the SUBCOMMAND. A verb further along is an
380  // argument (`storybloq note list --tags update`) or prose in a flag's
381  // value, and neither writes anything.
382  if (name === CLI_NAME) {
383    // Only the words that are SUBCOMMANDS count toward the depth: a global
384    // option before the verb (`storybloq --node x ticket update`) would
385    // otherwise push it out of reach and the write would go unseen.
386    let depth = 0;
387    for (let index = 0; index < args.length && depth < CLI_VERB_DEPTH; index += 1) {
388      const word = args[index]!;
389      if (word.startsWith("-")) {
390        if (!word.includes("=") && CLI_VALUE_OPTIONS.includes(word)) index += 1;
391        continue;
392      }
393      depth += 1;
394      if (WRITE_VERBS.includes(word)) return true;
395    }
396    return false;
397  }
398
399  if (name === MOVE_COMMAND || name === TEE_COMMAND || name === REMOVE_COMMAND) return args.some(inLedger);
400  if (COPY_COMMANDS.has(name)) return args.length >= 2 && inLedger(args[args.length - 1]);
401  if (name === SED_COMMAND) return args.some((word) => word.startsWith("-i")) && args.some(inLedger);
402  return false;
403}
404
405/**
406 * Every segment of a command line, cut on the separators, each on its own.
407 *
408 * The last segment is judged only when the line was read to its end: where an
409 * unterminated quote stopped the read, that segment is the one the quote is
410 * in and there is no telling what it says.
411 */
412function commandWroteLedger(command: string): boolean {
413  const { tokens, ok } = lex(command);
414  let segment: Token[] = [];
415  for (const token of tokens) {
416    // `&>` opens with a separator character and is not one: it is a redirect,
417    // and cutting the segment there would leave its target orphaned.
418    if (token.operator && SEPARATOR_CHARACTERS.includes(token.text[0]!) && !token.text.startsWith(BOTH_STREAMS)) {
419      if (segmentWrote(segment)) return true;
420      segment = [];
421      continue;
422    }
423    segment.push(token);
424  }
425  return ok && segmentWrote(segment);
426}
427
428/**
429 * Did this tool call change the ledger?
430 *
431 * The question has to be answered from the tool NAME first, because a ledger
432 * read is the common case and a sweep per read is the cost the chunked scan
433 * exists to avoid. Reading a ticket, globbing `.story`, or catting a config
434 * file all mention the directory and none of them change a thing.
435 *
436 *   tool                                              scan
437 *   storybloq_* / mcp__storybloq__* whose last word   yes
438 *     is a writing verb (ticket_update, meta_set)
439 *   any other storybloq tool (status, list, get)      no
440 *   Write, Edit, MultiEdit, NotebookEdit at a         yes
441 *     path under .story/
442 *   the same four anywhere else                       no
443 *   Bash running the storybloq CLI in EXECUTABLE      yes
444 *     position with a writing verb in SUBCOMMAND
445 *      position (it resolves .story/ itself, so the
446 *      command line need not name the directory)
447 *   Bash writing INTO .story/ (a redirect whose       yes
448 *     destination is there, cp whose last argument
449 *      is, tee at one, rm or sed -i of one)
450 *   Bash moving a file at either end of .story/       yes
451 *     (mv out of it takes a ticket off the board
452 *      as surely as mv into it puts one on)
453 *   Bash reading .story/ and writing elsewhere        no
454 *     (cat a ticket into /tmp, cp one out of it)
455 *   Bash naming the CLI anywhere but executable       no
456 *     position (echo storybloq ticket update), or
457 *      inside quotes, or in a heredoc's body, or in
458 *      the segment an unterminated quote is in
459 *      (the segments that closed before it still
460 *      count, one at a time)
461 *   Bash otherwise (cat, ls, grep, git status,        no
462 *     storybloq status, storybloq ticket list)
463 *   Read, Glob, Grep, LS, anything else               no
464 *
465 * Bash is conservative by construction: what it cannot read confidently does
466 * not sweep. A missed write costs one turn of staleness, since turn.complete
467 * still scans; a false positive costs a stat sweep of the whole ledger for
468 * every ledger read in the session, which is worse.
469 *
470 * Pure, and it reads only the few fields a path or a command arrives in, so a
471 * Write of a megabyte is not serialized to answer a yes or no question.
472 */
473export function wroteLedger(e: any): boolean {
474  const tool: unknown = e?.tool;
475  if (typeof tool !== "string") return false;
476  const bare = tool.startsWith(MCP_PREFIX) ? tool.slice(MCP_PREFIX.length) : tool;
477  if (bare.startsWith(LEDGER_TOOL_PREFIX)) return LEDGER_WRITE_VERB.test(bare);
478  if (tool === BASH_TOOL) {
479    const command: unknown = e?.["command"];
480    return typeof command === "string" && commandWroteLedger(command);
481  }
482  if (!MUTATING_FILE_TOOLS.has(tool)) return false;
483  for (const key of PATH_ARGUMENTS) {
484    const value: unknown = e?.[key];
485    if (typeof value === "string" && value.includes(STORY_DIR)) return true;
486  }
487  return false;
488}
489
490/** Where the client seated the pane, as its Pane props say; null when they do not. */
491
hooks/terminal-text.ts 122 lines
1/** The cell-width approximation's special code points, and the cut mark. */
2const ZERO_WIDTH_JOINER = 0x200d;
3const VARIATION_SELECTOR = 0xfe0f;
4const ELLIPSIS = "\u2026";
5
6
7/**
8 * The graphemes of a string with the cells each one takes.
9 *
10 * One routine, used by both the measuring and the cutting, because two that
11 * disagree is how "👩‍💻abc" cut to four cells came apart in the middle of the
12 * emoji: the measure suppressed the code point after the zero-width joiner
13 * and the cut counted it again. `Intl.Segmenter` gives the clusters (this
14 * runtime has it; where it does not, the fallback is code points, which is
15 * the old behaviour and no worse). A cluster is two cells wide if any code
16 * point in it is wide, or if it carries the emoji variation selector, which
17 * is what makes a text glyph like "♥️" render double.
18 */
19export function graphemes(text: string): { cluster: string; cells: number }[] {
20  const out: { cluster: string; cells: number }[] = [];
21  for (const cluster of clustersOf(text)) {
22    let cells = 0;
23    let emoji = false;
24    for (const character of cluster) {
25      const point = character.codePointAt(0) ?? 0;
26      if (point === VARIATION_SELECTOR) emoji = true;
27      if (isCombining(point) || point === VARIATION_SELECTOR || point === ZERO_WIDTH_JOINER) continue;
28      cells = Math.max(cells, isWide(point) ? 2 : 1);
29    }
30    out.push({ cluster, cells: emoji ? 2 : Math.max(cells, cluster === "" ? 0 : 1) });
31  }
32  return out;
33}
34
35/** Grapheme clusters where the runtime has them, code points where it does not. */
36function clustersOf(text: string): string[] {
37  const segmenter = (Intl as unknown as { Segmenter?: any }).Segmenter;
38  if (typeof segmenter === "function") {
39    const out: string[] = [];
40    for (const part of new segmenter(undefined, { granularity: "grapheme" }).segment(text)) {
41      out.push(part.segment as string);
42    }
43    return out;
44  }
45  return [...text];
46}
47
48/**
49 * How many terminal cells a string takes, which is not its length.
50 *
51 * A CJK ideograph or an emoji occupies two cells and a combining mark none,
52 * so measuring `.length` overruns a column by a cell per wide character, the
53 * row wraps, and the board comes apart. This is the usual approximation (the
54 * East Asian Wide and Fullwidth blocks plus the emoji planes), not a full
55 * Unicode width table, which is more than a sidebar can carry.
56 */
57export function cellWidth(text: string): number {
58  let width = 0;
59  for (const { cells } of graphemes(text)) width += cells;
60  return width;
61}
62
63function isCombining(point: number): boolean {
64  return (
65    (point >= 0x0300 && point <= 0x036f)
66    || (point >= 0x0483 && point <= 0x0489)
67    || (point >= 0x0591 && point <= 0x05bd)
68    || (point >= 0x0610 && point <= 0x061a)
69    || (point >= 0x064b && point <= 0x065f)
70    || (point >= 0x1ab0 && point <= 0x1aff)
71    || (point >= 0x1dc0 && point <= 0x1dff)
72    || (point >= 0x20d0 && point <= 0x20ff)
73    || (point >= 0xfe20 && point <= 0xfe2f)
74  );
75}
76
77function isWide(point: number): boolean {
78  return (
79    (point >= 0x1100 && point <= 0x115f)
80    || (point >= 0x2e80 && point <= 0x303e)
81    || (point >= 0x3041 && point <= 0x33ff)
82    || (point >= 0x3400 && point <= 0x4dbf)
83    || (point >= 0x4e00 && point <= 0x9fff)
84    || (point >= 0xa000 && point <= 0xa4cf)
85    || (point >= 0xac00 && point <= 0xd7a3)
86    || (point >= 0xf900 && point <= 0xfaff)
87    || (point >= 0xfe10 && point <= 0xfe19)
88    || (point >= 0xfe30 && point <= 0xfe6f)
89    || (point >= 0xff00 && point <= 0xff60)
90    || (point >= 0xffe0 && point <= 0xffe6)
91    || (point >= 0x1f300 && point <= 0x1f64f)
92    || (point >= 0x1f680 && point <= 0x1f6ff)
93    || (point >= 0x1f900 && point <= 0x1f9ff)
94    || (point >= 0x20000 && point <= 0x3fffd)
95  );
96}
97
98/**
99 * Cuts a string to fit `cells` terminal cells, ending in one ellipsis where
100 * anything was cut.
101 *
102 * Cluster by cluster, on the same measure the width uses, so a family emoji
103 * or an accented letter is either wholly in or wholly out and never halved.
104 * The ellipsis is U+2026, one cell wide.
105 */
106export function truncate(text: string, cells: number): string {
107  if (cells <= 0) return "";
108  const parts = graphemes(text);
109  let total = 0;
110  for (const part of parts) total += part.cells;
111  if (total <= cells) return text;
112  const room = cells - 1;
113  let width = 0;
114  let out = "";
115  for (const part of parts) {
116    if (width + part.cells > room) break;
117    width += part.cells;
118    out += part.cluster;
119  }
120  return `${out}${ELLIPSIS}`;
121}
122
hooks/stage-label.ts 106 lines
1/**
2 * T-531: the words the dashboard uses for an autonomous session's stage.
3 *
4 * `.story/status.json` names the guide's state ("IMPLEMENT"); the In progress
5 * card, or its heading, says what that means in one short word ("Code",
6 * T-532). The Mod cannot import `src/`, which does not ship with the plugin,
7 * so the table lives here and test/plugin/stage-label.test.ts holds it to the
8 * guide's own state list: a state added there fails that test until it has a
9 * label here.
10 */
11
12export const STAGE_LABELS: Readonly<Record<string, string>> = Object.freeze({
13  INIT: "Start",
14  LOAD_CONTEXT: "Load",
15  PICK_TICKET: "Pick",
16  PLAN: "Plan",
17  PLAN_REVIEW: "Review",
18  WRITE_TESTS: "Tests",
19  IMPLEMENT: "Code",
20  TEST: "Test",
21  CODE_REVIEW: "Review",
22  BUILD: "Build",
23  VERIFY: "Verify",
24  FINALIZE: "Commit",
25  KNOWLEDGE_REVIEW: "Ledger",
26  COMPACT: "Compacted",
27  LESSON_CAPTURE: "Lessons",
28  ISSUE_FIX: "Fix",
29  ISSUE_SWEEP: "Sweep",
30  HANDOVER: "Handover",
31  COMPLETE: "Done",
32  SESSION_END: "Ended",
33});
34
35/**
36 * How old a status may be before the stage is drawn as uncertain: the
37 * presence TTL (`PRESENCE_TTL_MS`, src/presence/types.ts), the window the
38 * presence handler sweeps with. A copy, because `src/` does not ship with the
39 * plugin; the stage-label test fails if the two drift apart.
40 *
41 * It is this long and not minutes on purpose. The status writer skips a write
42 * that would change only `observedAt` (ISS-1012), so the stamp is the time of
43 * the last change, not a heartbeat, and a quiet stage keeps an old stamp while
44 * the session is healthy.
45 */
46export const STAGE_STALE_MS = 12 * 60 * 60 * 1000;
47
48/**
49 * Code points a terminal would act on rather than draw: C0 (ESC, BEL, tab,
50 * CR, LF and the rest), DEL, C1, the Unicode line and paragraph separators,
51 * and the bidirectional embeddings, overrides and isolates.
52 */
53const UNSAFE_RUN = /[\u0000-\u001f\u007f-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]+/g;
54const UNSAFE_ONE = /[\u0000-\u001f\u007f-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]/;
55
56/**
57 * ISS-1306: invisible code points that hide text or flip its direction: the
58 * zero-width space, word joiner and byte-order mark, and the LRM, RLM and
59 * ALM marks. They take no cell, so they are removed, not spaced. U+200C
60 * (ZWNJ, part of Persian and Indic words) and U+200D (ZWJ, which joins an
61 * emoji sequence into one cluster) are real text and stay.
62 */
63const INVISIBLE = /[\u200b\u200e\u200f\u2060\ufeff\u061c]/g;
64const INVISIBLE_ONE = /[\u200b\u200e\u200f\u2060\ufeff\u061c]/;
65
66/**
67 * A string made fit to draw on one line: the invisible code points go, each
68 * run of unsafe code points becomes one space, then the ends are trimmed.
69 * status.json and ledger titles are read, never trusted, so nothing in them
70 * reaches the pane as a control sequence, a line break or hidden text, and
71 * the width arithmetic measures what is actually drawn.
72 */
73export function displaySafe(text: string): string {
74  return text.replace(INVISIBLE, "").replace(UNSAFE_RUN, " ").trim();
75}
76
77/**
78 * A status.json field worth keeping: a string with something in it besides
79 * whitespace, underscores, unsafe and invisible code points. Anything else
80 * is unset.
81 */
82export function statusField(value: unknown): string | null {
83  if (typeof value !== "string") return null;
84  for (const char of value) {
85    if (char !== "_" && char.trim() !== "" && !UNSAFE_ONE.test(char) && !INVISIBLE_ONE.test(char)) return value;
86  }
87  return null;
88}
89
90/**
91 * The label for a state: the table's own entry, otherwise the state itself,
92 * lowercased with its underscores as spaces. Never blank.
93 */
94export function stageLabel(state: string): string {
95  if (Object.hasOwn(STAGE_LABELS, state)) return STAGE_LABELS[state]!;
96  const fallback = displaySafe(state.toLowerCase().replace(/_/g, " ")).replace(/\s+/g, " ");
97  return fallback === "" ? "unknown" : fallback;
98}
99
100/** True only when the stamp reads as a time more than STAGE_STALE_MS before `now`. */
101export function stageStale(observedAt: string | null, now: number): boolean {
102  if (observedAt === null) return false;
103  const at = Date.parse(observedAt);
104  return Number.isFinite(at) && now - at > STAGE_STALE_MS;
105}
106
hooks/storyfield-logo.ts 104 lines
1/** Terminal Storyfield adapter. Samples the homepage mark into a fixed Braille field.
2 * Geometry comes from web/src/components/sections/Hero.tsx (850 by 950 view).
3 * The startup wave changes light, never the mark's structure or cell positions.
4 */
5export const LOGO_DURATION_MS = 2400;
6export type LogoCell = { glyph: string; color: string };
7type Point = readonly [number, number];
8const points: Point[] = [];
9function line(a: Point, b: Point): void { points.push(a, b); }
10function curve(a: Point, b: Point, c: Point, d: Point): void {
11  let last = a;
12  for (let i = 1; i <= 32; i++) {
13    const t = i / 32, s = 1 - t;
14    const next: Point = [s*s*s*a[0]+3*s*s*t*b[0]+3*s*t*t*c[0]+t*t*t*d[0], s*s*s*a[1]+3*s*s*t*b[1]+3*s*t*t*c[1]+t*t*t*d[1]];
15    line(last, next); last = next;
16  }
17}
18line([858,315],[520,315]);
19curve([520,315],[426,315],[354,386],[354,482]);
20curve([354,482],[354,578],[426,648],[518,648]);
21line([532,648],[776,648]);
22curve([776,648],[862,648],[912,716],[912,798]);
23curve([912,798],[912,880],[862,940],[776,940]);
24line([776,940],[342,940]);
25// Cache the raster once per terminal width. Animation only shades cells.
26const lengths: number[] = [];
27let totalLength = 0;
28for (let i = 0; i < points.length; i += 2) {
29  lengths.push(totalLength);
30  totalLength += Math.hypot(points[i + 1]![0] - points[i]![0], points[i + 1]![1] - points[i]![1]);
31}
32function sample(x: number, y: number): number | null {
33  if (Math.hypot(x - 342, y - 940) <= 61) return 0;
34  if (Math.abs(Math.hypot(x - 921, y - 315) - 62) <= 23) return 1;
35  let nearest = Infinity, along = 0;
36  for (let i = 0; i < points.length; i += 2) {
37    const a = points[i]!, b = points[i + 1]!, dx = b[0] - a[0], dy = b[1] - a[1];
38    const t = Math.max(0, Math.min(1, ((x - a[0]) * dx + (y - a[1]) * dy) / (dx * dx + dy * dy)));
39    const distance = Math.hypot(x - a[0] - t * dx, y - a[1] - t * dy);
40    if (distance < nearest) {
41      nearest = distance;
42      along = 1 - (lengths[i / 2]! + t * Math.hypot(dx, dy)) / totalLength;
43    }
44  }
45  return nearest <= 23 ? along : null;
46}
47const bits = [[1, 8], [2, 16], [4, 32], [64, 128]];
48type RasterCell = { glyph: string; along: number; metal: number };
49const rasters = new Map<number, RasterCell[][]>();
50function raster(width: number): RasterCell[][] {
51  const cached = rasters.get(width);
52  if (cached) return cached;
53  const height = Math.round(width * 950 / 850 / 2);
54  const result = Array.from({ length: height }, (_, row) => Array.from({ length: width }, (_, col) => {
55    let mask = 0, along = 0, count = 0;
56    for (let y = 0; y < 4; y++) for (let x = 0; x < 2; x++) {
57      const at = sample(200 + (col * 2 + x + .5) / (width * 2) * 850, 170 + (row * 4 + y + .5) / (height * 4) * 950);
58      if (at !== null) { mask |= bits[y]![x]!; along += at; count++; }
59    }
60    return {
61      glyph: mask ? String.fromCharCode(0x2800 + mask) : ' ',
62      along: count ? along / count : 0,
63      metal: .5 + .5 * Math.sin(col / width * 3.4 + row / height * 2.2),
64    };
65  }));
66  rasters.set(width, result);
67  return result;
68}
69
70// Fixed geometry and deterministic time make resizing and seeking predictable.
71// A single light impulse follows the thread from its solid end to its open ring.
72export function logoFrame(columns: number, elapsedMs: number, reducedMotion = false): LogoCell[][] {
73  const width = Math.max(8, Math.min(32, Math.floor(Number.isFinite(columns) ? columns : 28)));
74  const time = Number.isFinite(elapsedMs) ? Math.max(0, elapsedMs) : LOGO_DURATION_MS;
75  const progress = Math.min(1, time / 2050);
76  const head = -.15 + 1.45 * (progress * progress * (3 - 2 * progress));
77  return raster(width).map(row => row.map(cell => {
78    const distance = cell.along - head;
79    const pulse = reducedMotion || time >= 2050 ? 0
80      : Math.exp(-distance * distance / (distance < 0 ? .022 : .003));
81    const base = [155 + cell.metal * 45, 91 + cell.metal * 49, 58 + cell.metal * 40];
82    const crest = [255, 235, 199];
83    const color = '#' + base.map((v, i) => Math.round(v + (crest[i]! - v) * pulse).toString(16).padStart(2, '0')).join('');
84    return { glyph: cell.glyph, color };
85  }));
86}
87
88/** Fit to the pane body, never to the height of the entire terminal.
89 * Inline panes are intentionally short. Docked panes can show a larger mark.
90 * Missing initial measurements use a conservative bound until the next render.
91 */
92export function logoLayout(columns: number, bodyRows: number | undefined, placement: "dock" | "inline" | null): { columns: number; top: number; left: number; rows: number } | null {
93  const width = Number.isFinite(columns) ? Math.max(0, Math.floor(columns)) : 0;
94  const cap = placement === "dock" ? 18 : 9;
95  const measured = typeof bodyRows === "number" && Number.isFinite(bodyRows) && bodyRows > 0;
96  const room = measured ? Math.min(cap, Math.floor(bodyRows)) : (placement === "dock" ? 14 : 7);
97  // One header row, one breathing row, and at least five rows of artwork.
98  const available = room - 2;
99  const artWidth = Math.min(28, width - 2, Math.floor(available * 850 * 2 / 950));
100  if (artWidth < 8) return null;
101  const height = Math.round(artWidth * 950 / 850 / 2);
102  return { columns: artWidth, left: Math.floor((width - artWidth) / 2), top: 1 + Math.floor((available - height) / 2), rows: height };
103}
104
hooks/dashboard-motion.ts 130 lines
1import type { SidebarProjection } from "./sidebar-projection.js";
2
3const CARD_MS = 1400;
4const READY_MS = 1800;
5const COUNT_MS = 1000;
6const CONTEXT_MS = 650;
7const MAX_CARD_EFFECTS = 128;
8
9type CardState = { column: string; blocked: boolean };
10type CardEffect = { age: number; ready: boolean };
11const clamp = (n: number): number => Math.max(0, Math.min(1, n));
12const ease = (n: number): number => 1 - (1 - clamp(n)) ** 3;
13
14/** Presentation only. Actual statuses, counts, and percentages always come from the ledger/client. */
15export class DashboardMotion {
16  private cards: Map<string, CardState> | null = null;
17  private root: string | null = null;
18  private cardEffects = new Map<string, CardEffect>();
19  private counts = new Map<string, number>();
20  private working = false;
21  private activityMs = 0;
22  private turnId: string | null = null;
23  private context: number | null = null;
24  private lastContext: number | null = null;
25  private contextTween: { from: number; to: number; age: number } | null = null;
26  private frameMs = 0;
27
28  constructor(readonly enabled = true, private readonly highlights = true) {}
29
30  observe(projection: SidebarProjection, root: string | null): void {
31    const current = new Map<string, CardState>();
32    for (const [column, cards] of Object.entries(projection.board)) {
33      for (const card of cards) current.set(card.key, { column, blocked: card.blocked });
34    }
35    if (root !== this.root) {
36      this.cards = null;
37      this.cardEffects.clear();
38      this.counts.clear();
39    }
40    if (this.enabled && this.cards !== null) {
41      if (this.highlights) {
42        for (const [key, card] of current) {
43          const before = this.cards.get(key);
44          if (!before) continue;
45          if (before.column !== card.column || before.blocked !== card.blocked) {
46            this.cardEffects.delete(key);
47            this.cardEffects.set(key, {
48              age: 0,
49              ready: before.blocked && !card.blocked && card.column !== "done",
50            });
51            if (before.column !== card.column) this.counts.set(card.column, 0);
52          }
53        }
54      }
55
56    }
57    for (const key of this.cardEffects.keys()) if (!current.has(key)) this.cardEffects.delete(key);
58    while (this.cardEffects.size > MAX_CARD_EFFECTS) this.cardEffects.delete(this.cardEffects.keys().next().value!);
59    this.cards = current;
60    this.root = root;
61  }
62
63  setWorking(working: boolean, turnId?: string): void {
64    if (working !== this.working) this.activityMs = 0;
65    this.working = working;
66    if (!working) this.turnId = null;
67    else if (turnId) this.turnId = turnId;
68  }
69
70  finishTurn(turnId?: string, agentId?: string): void {
71    if (agentId || (turnId && this.turnId && turnId !== this.turnId)) return;
72    this.setWorking(false);
73  }
74
75  activity(): { working: boolean; sweep: number | null } {
76    return {
77      working: this.working,
78      sweep: this.working && this.enabled ? Math.floor(this.activityMs / 50) * (50 / 120) : null,
79    };
80  }
81
82  setContext(value: number | null, immediate = false): void {
83    const next = value === null || !Number.isFinite(value) ? null : Math.max(0, Math.min(100, value));
84    if (next === this.context && !immediate) return;
85    const from = this.meterValue() ?? this.lastContext;
86    this.context = next;
87    this.contextTween = this.enabled && !immediate && from !== null && next !== null && from !== next
88      ? { from, to: next, age: 0 } : null;
89    if (next !== null) this.lastContext = next;
90  }
91
92  meterValue(): number | null {
93    if (this.context === null) return null;
94    const tween = this.contextTween;
95    return tween ? tween.from + (tween.to - tween.from) * ease(tween.age / CONTEXT_MS) : this.context;
96  }
97
98  card(key: string): { progress: number | null; ready: boolean; fading: boolean } {
99    const effect = this.cardEffects.get(key);
100    return {
101      progress: effect && effect.age < CARD_MS ? ease(effect.age / CARD_MS) : null,
102      ready: !!effect?.ready,
103      fading: !!effect && effect.age > READY_MS - 300,
104    };
105  }
106
107  count(column: string): boolean { return this.counts.has(column); }
108
109  /** One shared clock; at most 20 redraws/sec while effects run, 20/sec for the smooth lettering gradient. */
110  advance(ms: number): boolean {
111    const hadEffects = this.cardEffects.size > 0 || this.counts.size > 0 || this.contextTween !== null;
112    const beforeActivity = this.activity();
113    if (this.working && this.enabled) this.activityMs = (this.activityMs + ms) % 2400;
114    for (const [key, effect] of this.cardEffects) {
115      effect.age += ms;
116      if (effect.age >= (effect.ready ? READY_MS : CARD_MS)) this.cardEffects.delete(key);
117    }
118    for (const [key, age] of this.counts) {
119      if (age + ms >= COUNT_MS) this.counts.delete(key);
120      else this.counts.set(key, age + ms);
121    }
122    if (this.contextTween) { this.contextTween.age += ms; if (this.contextTween.age >= CONTEXT_MS) this.contextTween = null; }
123    const activity = this.activity();
124    this.frameMs = Math.min(50, this.frameMs + ms);
125    const hasEffects = this.cardEffects.size > 0 || this.counts.size > 0 || this.contextTween !== null;
126    if (hadEffects && (this.frameMs >= 50 || !hasEffects)) { this.frameMs = 0; return true; }
127    return beforeActivity.sweep !== activity.sweep;
128  }
129}
130
hooks/sidebar-projection.ts 564 lines
1/**
2 * T-508: the ledger sidebar Mod's projection of `.story/`.
3 *
4 * WHY THIS FILE IS HERE AND NOT IN src/core. A Claude Code hooks module
5 * "imports its own files by relative path and 'claude-code', nothing else":
6 * `claude plugin validate` refuses an import from outside the plugin's folder
7 * and refuses any `node:` import, and `src/core`'s status projection reaches
8 * both. So the numbers are computed here, in a file the client will load, and
9 * `test/plugin/sidebar-projection.test.ts` asserts they equal the ones
10 * `storybloq status --compact` prints for the same fixture `.story/`. That
11 * test is the whole of "do not fork the projection": there is one set of
12 * rules, and two implementations that are held equal.
13 *
14 * Every rule below is the CLI's, reproduced deliberately:
15 *   - active means `lifecycle` absent or "active"; deleted is out of every count
16 *   - a ticket named as another's `parentTicket` is an umbrella and never a leaf
17 *   - counts are over leaves only, so an umbrella's own status is ignored
18 *   - a phase's status aggregates its leaves: all complete is complete, any
19 *     complete or in progress is in progress, otherwise not started
20 *   - a blocker reference resolves by id, then displayId, then a previous
21 *     displayId; one that is missing or ambiguous counts as blocking, which is
22 *     the conservative reading the CLI takes
23 *
24 * Pure: no clock, no I/O, and one import, the plugin's own display-safety
25 * function. The caller reads the files (`$.fs`) and caches what
26 * `extractRecord` returns; this module only counts.
27 */
28
29import { displaySafe } from "./stage-label.js";
30
31/** How long a title may be once cached. The store holds 4 MiB for the whole plugin. */
32const TITLE_CAP = 80;
33
34/** How many handover names the sidebar names below the board. */
35const HANDOVERS_SHOWN = 2;
36
37/** The severities the ledger uses, in the order the sidebar shows them. */
38const SEVERITIES = ["critical", "high", "medium", "low"] as const;
39
40export type LedgerKind = "ticket" | "issue";
41
42export type PhaseStatus = "complete" | "inprogress" | "notstarted";
43
44/** A ticket, reduced to the fields the sidebar counts or shows. */
45export interface SidebarTicket {
46  readonly kind: "ticket";
47  readonly id: string;
48  /** Raw, as the record carries it: absent is null, not the id. The two
49   *  resolvers below differ on exactly that. */
50  readonly displayId: string | null;
51  readonly previousDisplayIds: readonly string[];
52  readonly title: string;
53  readonly status: string;
54  readonly phase: string | null;
55  readonly parentTicket: string | null;
56  readonly blockedBy: readonly string[];
57  readonly lifecycle: string | null;
58  readonly order: number;
59  /** T-486 U2-5: the CLI's effective withdrawal. Optional because a record cached before it existed has none. */
60  readonly withdrawn?: boolean;
61}
62
63/** An issue, reduced the same way. */
64export interface SidebarIssue {
65  readonly kind: "issue";
66  readonly id: string;
67  readonly displayId: string | null;
68  readonly previousDisplayIds: readonly string[];
69  readonly title: string;
70  readonly status: string;
71  readonly severity: string;
72  readonly lifecycle: string | null;
73}
74
75export type SidebarRecord = SidebarTicket | SidebarIssue;
76
77export interface SidebarPhase {
78  readonly label?: string;
79  readonly id: string;
80  readonly name: string;
81  readonly status: PhaseStatus;
82  readonly leafCount: number;
83}
84
85/** One ledger item as a board row: what the column shows and how it is marked. */
86export interface SidebarBoardCard {
87  /** Stable identity, independent of reconciled display IDs. */
88  readonly key: string;
89  readonly id: string;
90  readonly title: string;
91  /** An unfinished ticket whose blockedBy still points at something unfinished. */
92  readonly blocked: boolean;
93  /** Which side of the ledger the row came from. */
94  readonly kind: LedgerKind;
95  /** An issue's severity; null on a ticket, which has none. */
96  readonly severity: string | null;
97  /** T-486 U2-5: a ticket whose withdrawal is effective; always false on an issue. */
98  readonly withdrawn: boolean;
99}
100
101/** Every active leaf and issue appears once, in its recorded status column.
102 * Blocking is a card attribute, so a blocked in-progress story stays there.
103 * Unknown unfinished statuses belong to Open; completed records belong to Done.
104 */
105export interface SidebarBoard {
106  readonly open: readonly SidebarBoardCard[];
107  readonly inProgress: readonly SidebarBoardCard[];
108  readonly done: readonly SidebarBoardCard[];
109}
110
111export interface SidebarTicketRef {
112  readonly id: string;
113  readonly title: string;
114  readonly phase: string | null;
115}
116
117export interface SidebarInput {
118  readonly project: string;
119  readonly phases: readonly { readonly id: string; readonly name: string; readonly label?: string }[];
120  readonly tickets: readonly SidebarTicket[];
121  readonly issues: readonly SidebarIssue[];
122  readonly handoverFilenames: readonly string[];
123}
124
125export interface SidebarProjection {
126  readonly project: string;
127  readonly totalTickets: number;
128  readonly completeTickets: number;
129  readonly openTickets: number;
130  readonly blockedTickets: number;
131  readonly openIssues: number;
132  readonly issuesBySeverity: Readonly<Record<string, number>>;
133  readonly phases: readonly SidebarPhase[];
134  readonly currentPhase: SidebarPhase | null;
135  readonly inProgressTickets: readonly SidebarTicketRef[];
136  readonly board: SidebarBoard;
137  /** The newest handovers, newest first, at most HANDOVERS_SHOWN of them. */
138  readonly latestHandovers: readonly string[];
139}
140
141function asString(value: unknown): string | null {
142  return typeof value === "string" && value.length > 0 ? value : null;
143}
144
145/**
146 * Where a severity sits in the order the board reads them, worst first.
147 *
148 * One the ledger does not use sorts after all of them rather than throwing
149 * the column's order away: this reads a hand-editable file.
150 */
151function severityRank(severity: string): number {
152  const at = (SEVERITIES as readonly string[]).indexOf(severity);
153  return at === -1 ? SEVERITIES.length : at;
154}
155
156function asStringArray(value: unknown): string[] {
157  if (!Array.isArray(value)) return [];
158  const out: string[] = [];
159  for (const entry of value) {
160    if (typeof entry === "string") out.push(entry);
161  }
162  return out;
163}
164
165/**
166 * Reads one ledger file's text into the fields the sidebar keeps, or null when
167 * the text is not a record of that kind.
168 *
169 * Null rather than a throw: the Mod reads whatever is in the directory, and a
170 * half-written file during someone else's transaction must cost one row, not
171 * the pane. The CLI's loader takes the same line (it skips a corrupt entry with
172 * a warning), so skipping here keeps the two projections equal.
173 *
174 * The title is truncated at extraction, not at render: this is what goes into
175 * `$.store`, and a ledger of two thousand untruncated records would spend the
176 * store's whole 4 MiB on prose the pane never shows.
177 */
178export function extractRecord(kind: LedgerKind, text: string): SidebarRecord | null {
179  let parsed: unknown;
180  try {
181    parsed = JSON.parse(text);
182  } catch {
183    return null;
184  }
185  if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
186  const raw = parsed as Record<string, unknown>;
187
188  const id = asString(raw["id"]);
189  const status = asString(raw["status"]);
190  if (id === null || status === null) return null;
191
192  const displayId = asString(raw["displayId"]);
193  // ISS-1306: a title is ledger text any teammate or tool wrote. Made safe to
194  // draw before the cap, so the cap counts what the pane shows.
195  const title = displaySafe(asString(raw["title"]) ?? "").slice(0, TITLE_CAP);
196  const lifecycle = asString(raw["lifecycle"]);
197  const previousDisplayIds = asStringArray(raw["previousDisplayIds"]);
198
199  if (kind === "issue") {
200    const severity = asString(raw["severity"]);
201    if (severity === null) return null;
202    return { kind: "issue", id, displayId, previousDisplayIds, title, status, severity, lifecycle };
203  }
204
205  const orderRaw = raw["order"];
206  return {
207    kind: "ticket",
208    id,
209    displayId,
210    previousDisplayIds,
211    title,
212    status,
213    phase: asString(raw["phase"]),
214    parentTicket: asString(raw["parentTicket"]),
215    blockedBy: asStringArray(raw["blockedBy"]),
216    lifecycle,
217    order: typeof orderRaw === "number" && Number.isFinite(orderRaw) ? orderRaw : 0,
218    withdrawn: isEffectivelyWithdrawn(raw),
219  };
220}
221
222/**
223 * T-486 U2-5: the CLI's effective-withdrawal rule (`ticketResolutionKindView`
224 * in src/core/resolution-kind.ts), reproduced for the same reason as the
225 * counts and held equal by `test/plugin/sidebar-projection.test.ts`. A
226 * well-formed `withdrawn` kind counts only on a complete ticket that is not an
227 * owner checkpoint, closed on the day it was completed. A stale or malformed
228 * kind is not a withdrawal.
229 */
230function isEffectivelyWithdrawn(raw: Record<string, unknown>): boolean {
231  if (!Object.prototype.hasOwnProperty.call(raw, "resolutionKind")) return false;
232  const value = raw["resolutionKind"];
233  if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
234  const kind = value as Record<string, unknown>;
235  const reason = kind["reason"];
236  const closedOn = kind["closedOn"];
237  if (kind["kind"] !== "withdrawn" || typeof reason !== "string" || reason.length === 0) return false;
238  if (typeof closedOn !== "string" || !isCalendarDate(closedOn)) return false;
239  if (Object.prototype.hasOwnProperty.call(raw, "ownerCheckpoint") && raw["ownerCheckpoint"] !== undefined) return false;
240  return raw["status"] === "complete" && closedOn === raw["completedDate"];
241}
242
243/** `YYYY-MM-DD` that names a real day, as the CLI's DateSchema reads it. */
244function isCalendarDate(value: string): boolean {
245  if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
246  const day = new Date(value + "T00:00:00Z");
247  return !isNaN(day.getTime()) && day.toISOString().startsWith(value);
248}
249
250function isActive(record: { readonly lifecycle: string | null }): boolean {
251  return record.lifecycle === null || record.lifecycle === "active";
252}
253
254/** The CLI's three-step reference resolution, over every ticket including deleted ones. */
255type Resolution =
256  | { readonly kind: "found"; readonly item: SidebarTicket }
257  | { readonly kind: "ambiguous" }
258  | { readonly kind: "missing" };
259
260function buildResolver(tickets: readonly SidebarTicket[]): (ref: string) => Resolution {
261  const byId = new Map<string, SidebarTicket>();
262  const byDisplay = new Map<string, SidebarTicket[]>();
263  const byPrev = new Map<string, SidebarTicket[]>();
264  for (const t of tickets) {
265    // First wins, as the CLI's index does.
266    if (!byId.has(t.id)) byId.set(t.id, t);
267    // buildDisplayIndex: the displayId trimmed, and the id where it is blank
268    // or absent.
269    const trimmedDisplay = (t.displayId ?? "").trim();
270    const displayKey = trimmedDisplay === "" ? t.id : trimmedDisplay;
271    const atDisplay = byDisplay.get(displayKey);
272    if (atDisplay) atDisplay.push(t);
273    else byDisplay.set(displayKey, [t]);
274    for (const prev of t.previousDisplayIds) {
275      const trimmed = prev.trim();
276      if (trimmed === "") continue;
277      const atPrev = byPrev.get(trimmed);
278      if (atPrev) atPrev.push(t);
279      else byPrev.set(trimmed, [t]);
280    }
281  }
282  return (ref: string): Resolution => {
283    const hit = byId.get(ref);
284    if (hit) return { kind: "found", item: hit };
285    const display = byDisplay.get(ref);
286    if (display && display.length === 1) return { kind: "found", item: display[0]! };
287    if (display && display.length > 1) return { kind: "ambiguous" };
288    const prev = byPrev.get(ref);
289    if (prev && prev.length === 1) return { kind: "found", item: prev[0]! };
290    if (prev && prev.length > 1) return { kind: "ambiguous" };
291    return { kind: "missing" };
292  };
293}
294
295/**
296 * Parent references, resolved as `ProjectState` resolves them when it builds
297 * its graph, through `ticketRefNormalizer` in src/core/ticket-counts.ts:
298 *
299 *   if (byId.has(ref)) return ref;
300 *   const display = byDisplay.get(ref);
301 *   if (display !== undefined) return display.length === 1 ? display[0] : ref;
302 *   const prev = byPrev.get(ref);
303 *   if (prev?.length === 1) return prev[0];
304 *   return ref;
305 *
306 * This is NOT the blocker resolver above: a missing or ambiguous parent ref
307 * comes back unchanged rather than as a verdict.
308 *
309 * T-486 U2-3a changed the precedence: a ref that any ticket holds as its
310 * CURRENT displayId resolves only when exactly one does, and an ambiguous one
311 * ends the search there. A previous displayId is consulted only
312 * when no ticket currently holds the ref. So a ref that two tickets currently
313 * answer to and one ticket used to answer to stays unresolved, and the one
314 * that used to answer to it is not made an umbrella by it.
315 *
316 * This file cannot import that function (see the top of the file), so this
317 * is a copy, and `test/plugin/sidebar-projection.test.ts` holds the two equal
318 * ref by ref over one fixture (mutant M-PARENT-AMBIGUOUS restores the old
319 * fall-through here and that test goes red).
320 *
321 * One thing still differs from the blocker resolver above: the index is built
322 * on the RAW displayId only where a record carries one, with no trimming and
323 * no falling back to the id, so a ticket with no displayId is not indexed
324 * under its own id here even though `buildDisplayIndex` does index it that
325 * way.
326 *
327 * A ref that resolves to nothing comes back unchanged, which is what makes an
328 * unresolvable parent name an umbrella id nothing matches: no ticket is
329 * excluded from the leaves by it, and none should be.
330 */
331export function buildParentResolver(tickets: readonly SidebarTicket[]): (ref: string) => string {
332  const byId = new Set<string>();
333  const byDisplay = new Map<string, string[]>();
334  const byPrev = new Map<string, string[]>();
335  for (const t of tickets) {
336    byId.add(t.id);
337    if (t.displayId !== null && t.displayId !== "") {
338      const at = byDisplay.get(t.displayId);
339      if (at) at.push(t.id);
340      else byDisplay.set(t.displayId, [t.id]);
341    }
342    for (const prev of t.previousDisplayIds) {
343      const at = byPrev.get(prev);
344      if (at) at.push(t.id);
345      else byPrev.set(prev, [t.id]);
346    }
347  }
348  return (ref: string): string => {
349    if (byId.has(ref)) return ref;
350    const display = byDisplay.get(ref);
351    if (display !== undefined) return display.length === 1 ? display[0]! : ref;
352    const prev = byPrev.get(ref);
353    if (prev && prev.length === 1) return prev[0]!;
354    return ref;
355  };
356}
357
358/**
359 * T-486 U2-3b: the canonical ids claimed by more than one record. The CLI's
360 * applyTicketIdentityPolicy (src/core/ticket-counts.ts) leaves every such
361 * record out of every count and out of dependency release, so the sidebar
362 * does the same: none of them is a leaf, an umbrella or a parent, and a
363 * blocker that names one never releases.
364 */
365function duplicatedTicketIds(tickets: readonly SidebarTicket[]): Set<string> {
366  const seen = new Set<string>();
367  const duplicated = new Set<string>();
368  for (const t of tickets) {
369    if (seen.has(t.id)) duplicated.add(t.id);
370    else seen.add(t.id);
371  }
372  return duplicated;
373}
374
375/** The counted population: every record whose canonical id no other record claims. */
376export function countedSidebarTickets(tickets: readonly SidebarTicket[]): SidebarTicket[] {
377  const duplicated = duplicatedTicketIds(tickets);
378  return tickets.filter((t) => !duplicated.has(t.id));
379}
380
381function aggregateStatus(leaves: readonly SidebarTicket[]): PhaseStatus {
382  if (leaves.length === 0) return "notstarted";
383  if (leaves.every((t) => t.status === "complete")) return "complete";
384  const anyProgress = leaves.some((t) => t.status === "inprogress");
385  const anyComplete = leaves.some((t) => t.status === "complete");
386  return anyProgress || anyComplete ? "inprogress" : "notstarted";
387}
388
389/**
390 * The numbers the pane draws, from records the caller already read.
391 *
392 * Held equal to `buildCompactStatusData` by the repo's own test; change a rule
393 * here without changing it there and that test goes red.
394 */
395export function projectSidebar(input: SidebarInput): SidebarProjection {
396  const resolve = buildResolver(input.tickets);
397  // Blockers resolve over every record, as the CLI's isBlocked does; parents
398  // and leaves come from the counted population, as its umbrellaIdsOf does.
399  const duplicated = duplicatedTicketIds(input.tickets);
400  const counted = input.tickets.filter((t) => !duplicated.has(t.id));
401  const resolveParent = buildParentResolver(counted);
402
403  const activeTickets = counted.filter(isActive);
404  const activeIssues = input.issues.filter(isActive);
405
406  // An umbrella is any ticket another active ticket names as its parent. The
407  // reference is normalized first, so a parent named by displayId still makes
408  // that ticket an umbrella.
409  const umbrellaIds = new Set<string>();
410  for (const t of activeTickets) {
411    if (t.parentTicket === null) continue;
412    umbrellaIds.add(resolveParent(t.parentTicket));
413  }
414
415  const leaves = activeTickets.filter((t) => !umbrellaIds.has(t.id));
416  const completeTickets = leaves.filter((t) => t.status === "complete").length;
417
418  const isBlocked = (t: SidebarTicket): boolean => {
419    for (const ref of t.blockedBy) {
420      const resolved = resolve(ref);
421      if (resolved.kind === "missing" || resolved.kind === "ambiguous") return true;
422      if (duplicated.has(resolved.item.id)) return true;
423      if (resolved.item.lifecycle !== "deleted" && resolved.item.status !== "complete") return true;
424    }
425    return false;
426  };
427
428  const phases: SidebarPhase[] = input.phases.map((p) => {
429    const ofPhase = leaves.filter((t) => t.phase === p.id);
430    return { id: p.id, name: p.name, label: p.label, status: aggregateStatus(ofPhase), leafCount: ofPhase.length };
431  });
432
433  const issuesBySeverity: Record<string, number> = {};
434  for (const severity of SEVERITIES) issuesBySeverity[severity] = 0;
435  let openIssues = 0;
436  for (const i of activeIssues) {
437    if (i.status === "resolved") continue;
438    openIssues += 1;
439    issuesBySeverity[i.severity] = (issuesBySeverity[i.severity] ?? 0) + 1;
440  }
441
442  const inProgressTickets = leaves
443    .filter((t) => t.status === "inprogress")
444    .sort((a, b) => a.order - b.order)
445    .map((t) => ({ id: t.displayId ?? t.id, title: t.title, phase: t.phase }));
446
447  const currentPhase =
448    phases.find((p) => p.status === "inprogress")
449    ?? phases.find((p) => p.status === "notstarted")
450    ?? null;
451
452  // The whole project, every phase: the owner wants the board to be the
453  // ledger's shape, so its counts are the ones `storybloq status` prints. The
454  // column cap is what keeps a thousand complete tickets off the screen, not
455  // a filter that changes what the numbers mean.
456  const boardLeaves = leaves;
457  const card = (t: SidebarTicket): SidebarBoardCard => ({
458    key: `ticket:${t.id}`,
459    id: t.displayId ?? t.id,
460    title: t.title,
461    blocked: t.status !== "complete" && isBlocked(t),
462    kind: "ticket",
463    severity: null,
464    withdrawn: t.withdrawn === true,
465  });
466  const issueCard = (i: SidebarIssue): SidebarBoardCard => ({
467    key: `issue:${i.id}`,
468    id: i.displayId ?? i.id,
469    title: i.title,
470    // An issue is never blocked: it carries no blockedBy for anything to
471    // point at.
472    blocked: false,
473    kind: "issue",
474    severity: i.severity,
475    withdrawn: false,
476  });
477  // Ticket order, then the id as the tie-break, so the columns are stable
478  // between renders and between sessions. Order alone is not enough: the
479  // ledger hands out the same order number freely (every ticket filed without
480  // one shares a default), and Array.prototype.sort is only stable with
481  // respect to INPUT order, which here is directory read order. Done sorts
482  // the other way, newest first, and breaks its ties the other way too.
483  const byOrderAscending = (a: SidebarTicket, b: SidebarTicket): number =>
484    (a.order - b.order)
485    || (a.displayId ?? a.id).localeCompare(b.displayId ?? b.id)
486    // The canonical id has the last word: two tickets can share an order AND
487    // a display id (`storybloq reconcile` exists for exactly that), and
488    // without this they still compare equal and swap on read order.
489    || a.id.localeCompare(b.id);
490  // Issues sort by how much they matter and then by id, in every column: an
491  // issue has no order field to sort on, and severity is the only ranking the
492  // ledger gives. The id is the tie-break for the same reason it is on a
493  // ticket, and the canonical id has the last word for the same reason again.
494  const bySeverityThenId = (a: SidebarIssue, b: SidebarIssue): number =>
495    (severityRank(a.severity) - severityRank(b.severity))
496    || (a.displayId ?? a.id).localeCompare(b.displayId ?? b.id)
497    || a.id.localeCompare(b.id);
498  // Tickets first, then issues: the two are different things and a column
499  // that interleaves them reads as one list of neither.
500  const withIssues = (
501    tickets: readonly SidebarBoardCard[],
502    belongs: (i: SidebarIssue) => boolean,
503  ): readonly SidebarBoardCard[] => [
504    ...tickets,
505    ...activeIssues.filter(belongs).slice().sort(bySeverityThenId).map(issueCard),
506  ];
507  const board: SidebarBoard = {
508    // Open is the REMAINDER, not a status match, on both sides of the ledger.
509    // It is hand-editable JSON and this reads it raw, so a leaf can carry a
510    // status the CLI's enum does not have ("blocked" and "deferred" both
511    // occur) and so can an issue ("closed", "wontfix"); matching on "open"
512    // drops those records out of every column while they still count in
513    // leafCount and openIssues, and the board then does not add up.
514    open: withIssues(
515      boardLeaves
516        .filter((t) => t.status !== "complete" && t.status !== "inprogress")
517        .sort(byOrderAscending)
518        .map(card),
519      (i) => i.status !== "inprogress" && i.status !== "resolved",
520    ),
521    inProgress: withIssues(
522      boardLeaves.filter((t) => t.status === "inprogress").sort(byOrderAscending).map(card),
523      (i) => i.status === "inprogress",
524    ),
525    // Newest first: the last thing finished is the useful one to see, and the
526    // rest is history the ledger already keeps.
527    done: withIssues(
528      boardLeaves
529        .filter((t) => t.status === "complete")
530        .sort(
531          (a, b) =>
532            (b.order - a.order)
533            || (b.displayId ?? b.id).localeCompare(a.displayId ?? a.id)
534            || b.id.localeCompare(a.id),
535        )
536        .map(card),
537      (i) => i.status === "resolved",
538    ),
539  };
540
541  // Names are date-led, so a reverse sort is newest first. It is exact
542  // between two names of the same shape and deterministic always, but it
543  // cannot order a date-only name against a timestamped one from the same day
544  // (2026-01-02-x sorts before 2026-01-02-193000-y whatever their real order).
545  // Reading each file's mtime to do better would cost a $.fs.stat per
546  // handover on every refresh, which is not worth the tie.
547  const latestHandovers = [...input.handoverFilenames].sort().reverse().slice(0, HANDOVERS_SHOWN);
548
549  return {
550    project: input.project,
551    totalTickets: leaves.length,
552    completeTickets,
553    openTickets: leaves.length - completeTickets,
554    blockedTickets: leaves.filter((t) => t.status !== "complete" && isBlocked(t)).length,
555    openIssues,
556    issuesBySeverity,
557    phases,
558    currentPhase,
559    inProgressTickets,
560    board,
561    latestHandovers,
562  };
563}
564