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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .fallowrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
"packages/shift-manager/src/components/ui/dialog.tsx",
// The Teammates lab's copy of the registry's tool grouping.
"labs/teammates/src/components/flow-state/tool-grouping.ts",
// The Teammates lab's byte copy of the registry's spend: its card, popup and panel
// draw the line and the totals; the copy's other formatters are for other apps.
"labs/teammates/src/components/flow-state/spend.tsx",
// and of the registry's review card, which the task popup draws.
"labs/teammates/src/components/flow-state/review-hold.tsx",
// and of the registry's error, whose `ErrorDisplay` export its task popup doesn't draw.
Expand Down
5 changes: 4 additions & 1 deletion labs/teammates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ does, with no question first. Each agent is a worker of its own, off the list an
messaged, whose `WORKER.md` names its `harness:`. The rest of that file is the agent's
instructions: every run on it is told them before the task. A line in Chief of Staff's conversation and a
row in Chief of Staff's panel show the task, tagged with the agent it runs on (and the model, once the
run reports one), and open its run in a popup. The result lands in Chief of Staff's conversation as
run reports one), and open its run in a popup. Once the run reports what it spent, the line adds its
tokens and, when the vendor reported a charge, the cost; the popup splits input from output tokens, and
the panel totals Chief of Staff's coding tasks. A run billed by subscription shows tokens and no cost.
The result lands in Chief of Staff's conversation as
the coding task's line, *Coding task finished: …*. To change a coding task, tell Chief of Staff, who starts the next one,
or use the task's buttons.

Expand Down
3 changes: 2 additions & 1 deletion labs/teammates/host.mts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ const HARNESSES: Record<string, Harness> = {
client: { env: harnessEnv({ pass: CODEX_PASS }) },
thread: { sandboxMode: "workspace-write" },
} as never) as unknown as HarnessBlock,
cursor: ({ cwd, resume, onSession, onSteerable }) =>
cursor: ({ cwd, resume, onSession, onSteerable, onActivity }) =>
cursorAgent({
cwd,
resume,
onSession,
onSteerable,
onActivity,
env: harnessEnv({ pass: CURSOR_PASS }),
// A local Cursor run must name its model; Kitchen Sink's default.
model: "composer-2.5",
Expand Down
39 changes: 16 additions & 23 deletions labs/teammates/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
* | `watchTasks` | the board's rows, as the conversation's stream carries each one (`task-change`) |
* | | and the board's `listTasks` read (`taskActionName`), on each notice of the |
* | | conversation's board |
* | | and `taskSpendView` on a row, for the harness and model its run reported |
* | | and each linked run's session, where the run writes its row (its holds) |
* | | and `taskRowView` with the Inbox's holds (`inboxItems`), for what it waits on |
* | | you for |
* | | you for, and what its runs spent (the harness and model they reported) |
* | `answerTask` | the Inbox client's `answerOne`, on the task's open question or review |
* | `taskActions` | the Inbox client's `board(at)` on the conversation that filed the task, its |
* | | `"tasks"` board: cancel, steer, release, retry, and `refile` for a re-run |
Expand Down Expand Up @@ -68,7 +67,7 @@ import {
type RosterEntry,
type WorkforceClient,
} from "@flow-state-dev/workforce/browser";
import { isTerminalStatus, parseNotice, taskSpendView, taskSpendViewSchema } from "@flow-state-dev/orchestration/tasks";
import { isTerminalStatus, parseNotice, type TaskSpendView } from "@flow-state-dev/orchestration/tasks";
import {
createInboxClient,
inboxItems,
Expand All @@ -79,6 +78,7 @@ import {
type InboxRow,
type RefileOverrides,
type RefileRow,
type TaskRowView,
} from "@flow-state-dev/orchestration/inbox";
import { reviewDetail, type ReviewDetail } from "@flow-state-dev/harness-manager/browser";
import { CODER_FLOW } from "./lib/coder-flow";
Expand Down Expand Up @@ -160,6 +160,8 @@ export interface BoardTask {
harness?: string;
/** The model its run reported, when it reported one; never guessed. */
model?: string;
/** What its runs spent, as orchestration's task-row view folds it (`taskRowView`); absent before any ran. */
spend?: TaskSpendView;
/** What it waits on you for, while something does: its oldest open hold. */
waiting?: TaskWaiting;
}
Expand Down Expand Up @@ -591,21 +593,9 @@ function boardRows(items: readonly Item[]): Map<string, Record<string, any>> {
return rows;
}

/**
* What a row's spend says ran it: the harness and model its run reported, or nothing.
*
* A row reaches here in one of two shapes: the stored ledger (`spend.requests`), as a
* `task-change` item carries it, or the view the board's `listTasks` read already folded
* (`taskSpendViewSchema`). Folding a view again would read as nothing spent.
*/
function spendOf(row: Record<string, any>): { harness?: string; model?: string } {
try {
const folded = taskSpendViewSchema.safeParse(row.spend);
const view = folded.success ? folded.data : taskSpendView(row as never);
return { ...(view?.harness == null ? {} : { harness: view.harness }), ...(view?.model == null ? {} : { model: view.model }) };
} catch {
return {};
}
/** What a row's spend says ran it: the harness and model its run reported, or nothing. */
function spendOf(view: TaskSpendView | null): { harness?: string; model?: string } {
return { ...(view?.harness == null ? {} : { harness: view.harness }), ...(view?.model == null ? {} : { model: view.model }) };
}

/**
Expand All @@ -622,6 +612,9 @@ export function toBoardTask(
const status: BoardTask["status"] =
row.status === "completed" ? "done" : row.status === "errored" ? "failed" : row.status === "cancelled" ? "cancelled" : "running";
const answer = typeof row.output?.answer === "string" ? row.output.answer : undefined;
// One read of the row as orchestration's task-row view: its folded spend, and its oldest open hold.
const view = typeof row.id === "string" && typeof row.status === "string" ? taskRowView({ ...row, board: row.board ?? "" } as InboxRow, holds) : null;
const spend = view?.spend ?? null;
return {
id: row.id,
to: row.assignee ?? "",
Expand All @@ -635,8 +628,9 @@ export function toBoardTask(
...(Array.isArray(row.labels) ? { labels: row.labels.filter((label: unknown): label is string => typeof label === "string") } : {}),
...(typeof row.priority === "number" ? { priority: row.priority } : {}),
...(typeof row.run?.sessionId === "string" ? { runSessionId: row.run.sessionId } : {}),
...harnessAndModel(spendOf(row), harnessOf(row.assignee ?? "")),
...waitingOf(row, holds),
...harnessAndModel(spendOf(spend), harnessOf(row.assignee ?? "")),
...(spend === null ? {} : { spend }),
...waitingOf(view),
};
}

Expand All @@ -657,9 +651,8 @@ export function rerunOverrides(task: BoardTask, worker: string): RefileOverrides
}

/** What a row waits on you for, as `taskRowView` reads it: its oldest open hold, or nothing. */
function waitingOf(row: Record<string, any>, holds: readonly InboxHold[] | undefined): { waiting?: TaskWaiting } {
if (typeof row.id !== "string" || typeof row.status !== "string") return {};
const hold = taskRowView({ ...row, board: row.board ?? "" } as InboxRow, holds).hold;
function waitingOf(view: TaskRowView | null): { waiting?: TaskWaiting } {
const hold = view?.hold ?? null;
if (hold === null) return {};
const kind = hold.ask.kind === "question" || hold.ask.kind === "review" ? hold.ask.kind : "other";
return { waiting: { kind, prompt: hold.ask.prompt, unanswerable: hold.unanswerable } };
Expand Down
7 changes: 7 additions & 0 deletions labs/teammates/src/components/Popups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useWatch } from "../lib/use-watch";
import { Avatar } from "./Avatar";
import { ErrorBody } from "./flow-state/error";
import { ReviewHold } from "./flow-state/review-hold";
import { Spend } from "./flow-state/spend";
import { TaskActions, type TaskRerunOption } from "./flow-state/task-actions";
import { SessionPopup, Steps } from "./SessionPopup";

Expand Down Expand Up @@ -126,6 +127,12 @@ function TaskPopup({ adapter, popup, nameOf, lookOf, isCoding, coders, onClose,
return (
<SessionPopup label={line} title={title} status={task?.status} onClose={onClose}>
{task === undefined ? null : <TaskActions key={task.id} id={task.id} status={task.boardStatus} actions={actions} steer rerun={rerun} />}
{/* What a coding task's runs spent, in and out apart, with the vendor's charge when it reported one. */}
{task?.spend && isCoding(task.to) ? (
<div data-task-spend>
<Spend spend={task.spend} split className="text-[11px]" />
</div>
) : null}
{/* Why it failed, as an error; the result leads otherwise. How the run got there is below. */}
{task?.error ? (
<section aria-label="Error" role="alert" data-step="error" className="flex flex-col gap-1 rounded-md border border-destructive/40 bg-destructive/10 px-3 py-2 text-sm text-destructive">
Expand Down
5 changes: 4 additions & 1 deletion labs/teammates/src/components/TaskCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import type { BoardTask } from "../adapter";
import { cn } from "../lib/utils";
import type { Look } from "../lib/looks";
import { Avatar } from "./Avatar";
import { Spend } from "./flow-state/spend";
import { RunsOn, runsOn, taskLabel, taskLine } from "./Popups";

/** What a task's line draws: a board row's task, or a filed task's stand-in before its row is read. */
export type TaskLine = Pick<BoardTask, "id" | "to" | "title" | "status" | "harness" | "model">;
export type TaskLine = Pick<BoardTask, "id" | "to" | "title" | "status" | "harness" | "model" | "spend">;

/**
* A task a conversation filed, as one line in the thread: what was asked of
Expand Down Expand Up @@ -47,6 +48,8 @@ export const TaskCard = memo(function TaskCard({
)}
<span className="min-w-0 flex-1 truncate">{line}</span>
{on === undefined ? null : <RunsOn on={on} />}
{/* What its runs spent: tokens, and the charge when the vendor reported one. The tag above names the model. */}
{coding && task.spend ? <Spend spend={{ ...task.spend, model: null }} className="w-auto shrink-0" /> : null}
Comment thread
jhoffner marked this conversation as resolved.
Resolved
<span className={cn("shrink-0 text-xs", task.status === "failed" ? "text-destructive" : "text-muted-foreground")} data-task-status>
{task.status}
</span>
Expand Down
33 changes: 33 additions & 0 deletions labs/teammates/src/components/TeammatePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useWatch } from "../lib/use-watch";
import { cn } from "../lib/utils";
import { Avatar } from "./Avatar";
import { STALE_CLASS } from "./flow-state/connection-banner";
import { costAmount, tokenCount } from "./flow-state/spend";
import { runsOn, taskLabel, taskLine, type Popup } from "./Popups";
import { InstructionsForm } from "./TeammateForms";
import { useStale } from "./WorkersList";
Expand Down Expand Up @@ -135,6 +136,7 @@ export function TeammatePanel({
Tasks
</div>
{tasks.length === 0 ? <p className="px-4 text-xs text-muted-foreground">{worker.name} hasn't started any tasks yet.</p> : null}
<CodingSpend tasks={tasks.filter((t) => isCoding(t.to))} />
{active.length > 0 ? <ul aria-label="Active">{active.map(row)}</ul> : null}
{ended.map((group) => {
const open = opened[group.status] ?? false;
Expand Down Expand Up @@ -168,3 +170,34 @@ export function TeammatePanel({
</aside>
);
}

/**
* What this conversation's coding tasks spent, all together: their tokens, and
* the charges vendors reported. Each task's own count is its row's folded view;
* a task whose runs reported nothing adds nothing, and a subscription run adds
* tokens but no charge. A total missing a count reads as partial. Nothing shows
* until a coding task has reported spend. (The registry's line never sums; this
* total is the panel's own.)
*/
function CodingSpend({ tasks }: { tasks: readonly BoardTask[] }) {
const line = codingSpendLine(tasks);
if (line === "") return null;
return (
<p className="px-4 pb-2 font-mono text-[10px] text-muted-foreground" data-coding-spend>
{line}
</p>
);
}

/** {@link CodingSpend}'s line, or `""` until some task has reported a count or a charge. Exported for its tests. */
export function codingSpendLine(tasks: readonly BoardTask[]): string {
const spent = tasks.flatMap((t) => (t.spend === undefined ? [] : [t.spend]));
// Nothing measured yet, only runs that started: no line, never a measured-looking zero.
if (!spent.some((s) => s.inputTokens !== null || s.outputTokens !== null || s.costUsd != null)) return "";
const tokens = spent.reduce((sum, s) => sum + (s.inputTokens ?? 0) + (s.outputTokens ?? 0), 0);
const charged = spent.filter((s) => s.costUsd != null);
const cost = charged.reduce((sum, s) => sum + (s.costUsd ?? 0), 0);
// Some count still missing, or a source not yet reported: the total is a floor, and says so, as each task's line does.
const partial = spent.some((s) => s.unreported || s.inputTokens === null || s.outputTokens === null) ? " (partial)" : "";
return [`Coding tasks: ${tokenCount(tokens)} tokens${partial}`, charged.length === 0 ? null : costAmount(cost)].filter(Boolean).join(" · ");
}
92 changes: 92 additions & 0 deletions labs/teammates/src/components/flow-state/spend.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use client";

import { cn } from "@/lib/utils";

/*
* What a run spent, as one line: the model, the tokens, the vendor's charge
* when it reported one, and optionally the attempts and how long it took. The
* numbers are read from what the server folded (a task row's or a harness
* turn's `spend`), never summed here. A count nobody reported reads as such,
* never as 0; one still missing a source is marked partial. A run billed by
Comment thread
jhoffner marked this conversation as resolved.
Resolved
* subscription reports no charge and shows none: there is no dollar figure
* to show, and an estimate would be one nobody billed.
*/

/**
* What the line reads: the fields of orchestration's `TaskSpendView`, which a
* task row and a harness session's turn both carry. `costUsd` absent reads as
* none reported.
*/
export interface SpendSummary {
harness: string | null;
model: string | null;
inputTokens: number | null;
outputTokens: number | null;
costUsd?: number | null;
unreported: boolean;
live: boolean;
}

/** What else the line can say. */
export interface SpendLineOptions {
/** Input and output apart ("10.1k in · 2.3k out") rather than one total. */
split?: boolean;
/** The harness's name, first, for a view with no harness tag of its own. */
harnessLabel?: string;
/** The task's attempts; shown from the second on. */
attempts?: number;
/** How long it took, in ms. */
durationMs?: number | null;
}

/** 980, 12.4k, 1.2M. */
export function tokenCount(n: number): string {
if (n < 1000) return String(n);
if (n < 1_000_000) return `${(n / 1000).toFixed(1)}k`;
return `${(n / 1_000_000).toFixed(1)}M`;
}

/** $0.0042, $0.42, $12.40. */
export function costAmount(usd: number): string {
return usd < 0.01 ? `$${usd.toFixed(4)}` : `$${usd.toFixed(2)}`;
}

/** 42s, 4m 12s, 1h 3m. */
export function durationText(ms: number): string {
const seconds = Math.max(0, Math.round(ms / 1000));
if (seconds < 60) return `${seconds}s`;
const minutes = Math.floor(seconds / 60);
if (minutes < 60) return `${minutes}m ${seconds % 60}s`;
return `${Math.floor(minutes / 60)}h ${minutes % 60}m`;
}

/**
* "claude-sonnet-4-5 · 12.4k tokens · $0.42 · 2 attempts · 4m 12s". Empty
* when there is no spend to show.
*/
export function spendLine(spend: SpendSummary | null | undefined, options: SpendLineOptions = {}): string {
if (spend == null) return "";
const partial = spend.unreported ? " (partial)" : "";
const side = (n: number | null) => (n === null ? "not reported" : tokenCount(n));
const tokens =
spend.inputTokens === null && spend.outputTokens === null
? "no tokens reported yet"
: options.split === true
? `${side(spend.inputTokens)} in · ${side(spend.outputTokens)} out${partial}`
: `${tokenCount((spend.inputTokens ?? 0) + (spend.outputTokens ?? 0))} tokens${partial}`;
const cost = spend.costUsd == null ? null : costAmount(spend.costUsd);
const attempts = options.attempts !== undefined && options.attempts > 1 ? `${options.attempts} attempts` : null;
const duration = options.durationMs == null ? null : durationText(options.durationMs);
return [options.harnessLabel, spend.model, tokens, cost, attempts, duration].filter(Boolean).join(" · ");
}

/** The spend line, or nothing when there is no spend to show. */
export function Spend({ spend, className, ...options }: SpendLineOptions & { spend: SpendSummary | null | undefined; className?: string }) {
const line = spendLine(spend, options);
if (!line) return null;
return (
<span className={cn("block truncate font-mono text-[10px] text-muted-foreground", className)} data-spend={spend?.live ? "live" : "final"}>
{line}
</span>
);
}
15 changes: 11 additions & 4 deletions labs/teammates/test/fake-lab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ export interface FixtureLab extends LabAdapter {
setConnection(state: SessionConnectionState): void;
}

/** Each coding worker's harness, by name, and the model its run reports, if it reports one. */
const HARNESS: Record<string, { name: string; model?: string }> = {
"claude-code": { name: "Claude Code" },
codex: { name: "Codex", model: "gpt-5.5-codex" },
/**
* Each coding worker's harness, by name, the model its run reports, if it
* reports one, and what its run spent once it has run its check: Codex on an
* API key reports a charge; Claude Code on a subscription reports tokens only.
*/
const HARNESS: Record<string, { name: string; model?: string; spent?: { inputTokens: number; outputTokens: number; costUsd: number | null } }> = {
"claude-code": { name: "Claude Code", spent: { inputTokens: 8_000, outputTokens: 1_200, costUsd: null } },
codex: { name: "Codex", model: "gpt-5.5-codex", spent: { inputTokens: 12_400, outputTokens: 2_300, costUsd: 0.42 } },
cursor: { name: "Cursor" },
};

Expand Down Expand Up @@ -276,6 +280,7 @@ export function createFixtureLab(options: FixtureOptions = {}): FixtureLab {
return;
}
const model = HARNESS[harness]!.model;
const spent = HARNESS[harness]!.spent;
const calls = RUN_TOOLS[harness]!;
const call = (which: "read" | "edit" | "check") => report(task.id, harness, tool(...calls[which]));
const sleep = async (ms: number) => {
Expand Down Expand Up @@ -308,6 +313,8 @@ export function createFixtureLab(options: FixtureOptions = {}): FixtureLab {
call("edit")();
await sleep(800);
call("check")();
// What the run spent, as the row's folded view reads it once the run has reported.
if (spent !== undefined) updateTask(task.id, { spend: { harness, model: model ?? null, ...spent, unreported: false, live: false } });
for (let commits = 1; options.review; commits++) {
reviews.set(task.runSessionId!, {
branch: `teammates/${task.id}--change`,
Expand Down
Loading
Loading