Three-phase session status: derived summaries for the picker

- core/phases.ts: Phase3 (probe/plan/teach) + pure derivations —
  phaseOfTree (point-in-time, from the stored plan tree: no nodes → probe,
  no occupied node → plan, occupied → teach) and phaseAt3 (monotonic
  transcript fold for the dividers: failed plan_init never marks a seam;
  phase only advances).
- contracts.ts: ChatSummary gains record-derived phase + pendingQuestion;
  ChatListItem adds liveness-merged busy.
- store/chats.ts listChats computes the record-derived fields (tree +
  pending question scan).
- api/chats route merges busy = turn running || running background job.

Lane crossing granted by master: contracts/store/api (schema arbitration).
Data-only inference: pre-plan planning reads as probe until the plan lands.
This commit is contained in:
Amos
2026-09-01 18:24:13 +03:00
parent cf3225fb23
commit 1c3d288d08
5 changed files with 241 additions and 5 deletions
+112 -1
View File
@@ -8,7 +8,13 @@
*/
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { phaseAt, type PhaseMessage } from "../src/core/phases.ts";
import {
phaseAt,
phaseAt3,
phaseOfTree,
type Phase3Message,
type PhaseMessage,
} from "../src/core/phases.ts";
/** A minimal message: just typed parts, exactly the structural slice phaseAt reads. */
function msg(...partTypes: string[]): PhaseMessage {
@@ -83,3 +89,108 @@ describe("phaseAt", () => {
assert.equal(phaseAt([]), "probe");
});
});
// ─── The three-phase view (picker strips, header chip, transcript dividers) ────
/** A minimal tree: nodes as opaque ids is all phaseOfTree reads. */
function tree(nodes: string[], currentNodeId: string | null): { nodes: unknown[]; currentNodeId: string | null } {
return { nodes: nodes.map((id) => ({ id })), currentNodeId };
}
/** A message whose parts are full plan-op tool parts with outputs. */
function planMsg(
...ops: { tool: string; ok: boolean; tree: ReturnType<typeof tree> }[]
): Phase3Message {
return {
parts: ops.map((op, i) => ({
type: `tool-plan_${op.tool}`,
state: "output-available",
toolCallId: `p${i}`,
output: { ok: op.ok, tree: op.tree },
})),
};
}
describe("phaseOfTree", () => {
it("no tree / empty tree → probe", () => {
assert.equal(phaseOfTree(undefined), "probe");
assert.equal(phaseOfTree(null), "probe");
assert.equal(phaseOfTree(tree([], null)), "probe", "emptyTree() (a failed init) reads probe");
});
it("tree with no current node → plan", () => {
assert.equal(phaseOfTree(tree(["a", "b"], null)), "plan");
});
it("occupied node → teach", () => {
assert.equal(phaseOfTree(tree(["a", "b"], "a")), "teach");
});
});
describe("phaseAt3", () => {
it("no plan parts anywhere → probe at every index", () => {
const messages: Phase3Message[] = [msg("text"), msg("tool-quiz"), msg("text")];
for (let i = 0; i < messages.length; i++) {
assert.equal(phaseAt3(messages, i), "probe", `index ${i}`);
}
assert.equal(phaseAt3([]), "probe");
});
it("the probe→plan seam: the first plan output with a non-empty tree", () => {
const messages: Phase3Message[] = [
msg("text"), // probe chatter
planMsg({ tool: "plan_init", ok: false, tree: tree([], null) }), // failed init: NOT a seam
msg("text"),
planMsg({ tool: "plan_init", ok: true, tree: tree(["a", "b"], null) }), // ← the seam
msg("text"),
];
assert.equal(phaseAt3(messages, 0), "probe");
assert.equal(phaseAt3(messages, 1), "probe", "a failed init carries an empty tree — no seam");
assert.equal(phaseAt3(messages, 2), "probe");
assert.equal(phaseAt3(messages, 3), "plan");
assert.equal(phaseAt3(messages, 4), "plan");
assert.equal(phaseAt3(messages), "plan", "default = current phase");
});
it("the plan→teach seam: the first tree output with an occupied node", () => {
const messages: Phase3Message[] = [
msg("text"),
planMsg({ tool: "plan_init", ok: true, tree: tree(["a", "b"], null) }),
msg("text"),
planMsg({ tool: "plan_set_current", ok: true, tree: tree(["a", "b"], "a") }), // ← the seam
msg("text"),
];
assert.equal(phaseAt3(messages, 2), "plan");
assert.equal(phaseAt3(messages, 3), "teach");
assert.equal(phaseAt3(messages, 4), "teach");
});
it("collapsed seams: init and set_current in one message → straight to teach", () => {
const messages: Phase3Message[] = [
msg("text"),
planMsg(
{ tool: "plan_init", ok: true, tree: tree(["a"], null) },
{ tool: "plan_set_current", ok: true, tree: tree(["a"], "a") },
),
];
assert.equal(phaseAt3(messages, 0), "probe");
assert.equal(phaseAt3(messages, 1), "teach");
});
it("monotonic: a mid-teach node removal (current → null) never regresses the phase", () => {
const messages: Phase3Message[] = [
planMsg({ tool: "plan_init", ok: true, tree: tree(["a", "b"], null) }),
planMsg({ tool: "plan_set_current", ok: true, tree: tree(["a", "b"], "a") }),
msg("text"),
planMsg({ tool: "plan_remove_node", ok: true, tree: tree(["b"], null) }), // edit, not a regression
];
assert.equal(phaseAt3(messages, 2), "teach");
assert.equal(phaseAt3(messages, 3), "teach", "phase only ever advances");
});
it("upToIndex past the end / negative → clamped, never throws", () => {
const messages: Phase3Message[] = [planMsg({ tool: "plan_init", ok: true, tree: tree(["a"], null) })];
assert.equal(phaseAt3(messages, 99), "plan");
assert.equal(phaseAt3(messages, -1), "probe", "empty scan range → probe");
});
});