Merge feat/ui-polish: three-phase session model, plan graph, layout restructure

chat.tsx woven by hand: UI's session-surface layout (transcript column +
right sidebar as full-height siblings, h-10 header with PhaseChip) wrapped
in sub-agents' JobsContext.Provider; the message map carries both the
steer ternary (steers bypass the Message wrapper — the model-facing glue
text never renders) and UI's PhaseDivider emission (steers are verified
phase-boundary no-ops). Right sidebar = sub-agents' fleet panel column
(SubAgentsPanel above TreePanel, xl-only) carried onto UI's skeleton.
contracts.ts auto-merged (ChatSummary.phase/pendingQuestion + ChatListItem,
additive above the async-protocol section).

Smoke-verified on the merged tree: steer renders bare as its artifact
badge with no learner bubble, no rendered glue text, and no spurious
phase divider at the steer boundary.
This commit is contained in:
amosblomqvist
2026-09-02 16:40:47 +03:00
17 changed files with 1576 additions and 183 deletions
+24 -1
View File
@@ -5,6 +5,7 @@
*/
import type { UIMessage } from "ai";
import type { Phase3 } from "./phases.ts";
// ─── Plan tree (vision §2 "formal plan tree") ────────────────────────────────
// A session-scoped dependency tree authored by the teacher via plan tools.
@@ -257,13 +258,35 @@ export interface ChatRecord {
tree?: PlanTree;
}
/** The list view's shape (messageCount instead of full messages). */
/** The list view's shape (messageCount instead of full messages) + the
* per-session status the picker renders. `phase` and `pendingQuestion`
* derive from the stored record (tree + messages); the store computes
* them in listChats. Liveness (`busy`) is NOT record-derived — the API
* route merges it in (see ChatListItem).
*
* Phase semantics (three phases, data-only): probe = no plan yet, plan =
* tree exists but no node occupied, teach = a node is occupied
* (core/phases.ts phaseOfTree).
*/
export interface ChatSummary {
id: string;
title: string;
createdAt: string;
updatedAt: string;
messageCount: number;
/** The session's current phase, derived from the stored plan tree. */
phase: Phase3;
/** An unanswered quiz/ask question is waiting for the learner. */
pendingQuestion: boolean;
}
/** The sidebar's session row: a summary + live liveness. The chats list
* route attaches `busy` from the turn-liveness registry and the job store
* (a running turn OR a background job = the session is working). The store
* itself can't see liveness (import law: store never imports server),
* hence the split at exactly the record/liveness seam. */
export interface ChatListItem extends ChatSummary {
busy: boolean;
}
// ─── Async sub-agent jobs (vision §2.5; plan.md Wave 0 protocol) ────────────
+107
View File
@@ -45,3 +45,110 @@ export function phaseAt(messages: readonly PhaseMessage[], upToIndex?: number):
}
return "probe";
}
// ─── The three-phase view (probe / plan / teach) ──────────────────────────────
//
// The session picker's color strips, the header chip, and the transcript
// dividers all speak THREE phases, derived data-only from the plan tree:
//
// probe — no plan output yet (the tree is empty or absent);
// plan — a tree exists but no node is occupied (plan_set_current not fired);
// teach — a node is occupied.
//
// The teacher's invisible PRE-plan planning (reasoning before plan_init
// fires) reads as probe until the plan lands — an accepted limitation: no
// observable signal marks "I'm now planning" (a teacher-side marker would be
// an agent change; see phases.md discussion in the transcript divider work).
/** The three phases of a session, as the picker / header / dividers see it. */
export type Phase3 = "probe" | "plan" | "teach";
/** The advance-only order of phases: a session never visibly regresses. */
const PHASE3_RANK: Record<Phase3, number> = { probe: 0, plan: 1, teach: 2 };
/** Structural slice of a plan tree — anything with nodes + a current node. */
export interface PhaseTree {
nodes: ReadonlyArray<unknown>;
currentNodeId: string | null;
}
/**
* The phase of a tree, point-in-time: no nodes → probe, no current node →
* plan, an occupied node → teach. Used for the chats summary (the stored
* tree IS the session state there). Empty trees (emptyTree(), a failed
* plan_init) read as probe — a seam only counts once a plan exists.
*/
export function phaseOfTree(tree: PhaseTree | null | undefined): Phase3 {
if (!tree || tree.nodes.length === 0) return "probe";
return tree.currentNodeId === null ? "plan" : "teach";
}
/**
* Minimal structural slice of a UI message for the three-phase scan: parts
* with a state and (when a plan op completed) its output.
*/
export interface Phase3Message {
parts: ReadonlyArray<{ type: string; state?: string; output?: unknown }>;
}
/** A successful plan-op output as it appears on the part (`PlanToolResult`). */
interface PlanOutput {
tree?: { nodes?: unknown };
}
/**
* The three-phase boundary semantics over the TRANSCRIPT (the same data the
* tree panel reads): scanning forward from 0 to `upToIndex` (default: the
* last message), the phase advances probe → plan at the first plan part
* with a NON-EMPTY tree available (a failed plan_init carries an empty tree
* and never marks the seam), and plan → teach at the first tree output with
* an occupied node. MONOTONIC by design: the phase only ever advances — a
* node removal mid-teaching (currentNodeId → null) is an edit, not a phase
* regression, so no "Plan" divider ever appears below teaching content.
*
* `phaseAt` (two-phase) stays as-is: the plan_init call is its seam and the
* question dock's reveal logic leans on it. The two views differ only in
* the window between plan_init and the first plan_set_current, where
* three-phase reads "plan".
*/
export function phaseAt3(messages: readonly Phase3Message[], upToIndex?: number): Phase3 {
let phase: Phase3 = "probe";
const last = Math.min(upToIndex ?? messages.length - 1, messages.length - 1);
for (let i = 0; i <= last; i++) {
const parts = messages[i]?.parts;
if (!parts) continue;
for (const part of parts) {
if (!part.type.startsWith("tool-plan_") || part.state !== "output-available") continue;
const tree = (part.output as PlanOutput | undefined)?.tree as PhaseTree | undefined;
if (!tree || !Array.isArray(tree.nodes)) continue;
const candidate = phaseOfTree(tree);
if (PHASE3_RANK[candidate] > PHASE3_RANK[phase]) phase = candidate;
}
}
return phase;
}
/**
* The phase at EVERY message index, in one forward fold — the transcript
* dividers render one per boundary, so phaseAt3(m, i) per index would
* rescan 0..i each time (quadratic); this is the same derivation, linear.
* out[i] === phaseAt3(messages, i) for every index.
*/
export function phaseTimeline(messages: readonly Phase3Message[]): Phase3[] {
const out: Phase3[] = [];
let phase: Phase3 = "probe";
for (let i = 0; i < messages.length; i++) {
const parts = messages[i]?.parts;
if (parts) {
for (const part of parts) {
if (!part.type.startsWith("tool-plan_") || part.state !== "output-available") continue;
const tree = (part.output as PlanOutput | undefined)?.tree as PhaseTree | undefined;
if (!tree || !Array.isArray(tree.nodes)) continue;
const candidate = phaseOfTree(tree);
if (PHASE3_RANK[candidate] > PHASE3_RANK[phase]) phase = candidate;
}
}
out.push(phase);
}
return out;
}
+12 -1
View File
@@ -13,6 +13,8 @@ import { PATHS } from "../../config.ts";
import type { ChatStore } from "../core/ports.ts";
import type { ChatRecord, ChatSummary, PlanTree } from "../core/contracts.ts";
import type { UIMessage } from "ai";
import { findPendingToolCall } from "../core/pending.ts";
import { phaseOfTree } from "../core/phases.ts";
function chatFile(id: string): string {
if (!/^[A-Za-z0-9_-]+$/.test(id)) throw new Error(`Invalid chat id: ${id}`);
@@ -49,7 +51,16 @@ function listChats(): ChatSummary[] {
.filter((f) => f.endsWith(".json"))
.map((f) => getChat(f.slice(0, -5)))
.filter((r): r is ChatRecord => r !== null)
.map(({ messages, ...summary }) => ({ ...summary, messageCount: messages.length }))
.map(({ messages, tree, ...summary }) => ({
...summary,
messageCount: messages.length,
// Record-derived status for the session picker: phase from the
// stored plan tree, pending question from the transcript's last
// unanswered quiz/ask part. Liveness (busy) is NOT record-derived —
// the API route merges it (contracts.ts ChatListItem).
phase: phaseOfTree(tree),
pendingQuestion: findPendingToolCall(messages) !== null,
}))
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
} catch {
return [];