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
1 change: 1 addition & 0 deletions apps/desktop/src/ui/ThreadDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ function TurnsSection({
<span className="truncate text-callout">{turn.prompt}</span>
<span className="truncate text-caption text-fg-muted tabular">
{[
turn.origin,
turn.elapsedMs === undefined ? undefined : formatDuration(turn.elapsedMs),
turnPrice(turn, harness, rawModel),
]
Expand Down
38 changes: 38 additions & 0 deletions apps/desktop/src/ui/overview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
pullBranches,
pullPill,
pullsForBranches,
replyHeadline,
toggleFolded,
turnEntries,
turnHasTrace,
Expand Down Expand Up @@ -181,6 +182,43 @@ describe("the thread's turns", () => {
expect(turnEntries(items).map((turn) => turn.prompt)).toEqual(["Do the work", "Woken for a delegate"]);
});

test("a turn the app started is named by its last reply, and says what started it", () => {
const items: TranscriptItem[] = [
{ kind: "user", id: "u1", text: "Fix it all" },
turnEnd("e1"),
{
kind: "user",
id: "u2",
text: "Continued after compaction\n\nContinue.\n\n<!-- rustcode:continued -->",
continued: true,
},
{ kind: "assistant", id: "a1", text: "Looking at the inbox first." },
{ kind: "assistant", id: "a2", text: "## Done\n\nI opened **PR 14**: the inbox approves. More here." },
turnEnd("e2"),
{
kind: "user",
id: "u3",
text: "Resumed after restart.\n\n<!-- rustcode:resumed -->",
continued: true,
},
turnEnd("e3"),
];
const turns = turnEntries(items);
expect(turns.map((turn) => turn.prompt)).toEqual([
"Fix it all",
"I opened PR 14: the inbox approves.",
"Resumed after restart.",
]);
expect(turns.map((turn) => turn.origin)).toEqual([undefined, "After compaction", undefined]);
});

test("a reply's headline is its first plain sentence", () => {
expect(replyHeadline("I opened [PR 13](https://x/13). Next the inbox.")).toBe("I opened PR 13.");
expect(replyHeadline("```ts\ncode\n```\n- **Merged** twelve PRs")).toBe("Merged twelve PRs");
expect(replyHeadline("Version 1.2 ships")).toBe("Version 1.2 ships");
expect(replyHeadline("## Only a heading")).toBe("Only a heading");
});

test("a turn's cost is its last sample minus what the session had already spent", () => {
const items: TranscriptItem[] = [
{ kind: "user", id: "u1", text: "Go" },
Expand Down
65 changes: 61 additions & 4 deletions apps/desktop/src/ui/overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import { readStorage, writeStorage } from "../lib/storage";
import { rowTokens, type TraceUsage, usageDelta } from "../lib/trace";
import { type DelegateNode, delegateTree } from "../state/delegates";
import type { Link } from "../state/orchestration";
import { diffCounts, stripHarnessMarkup, type TranscriptItem, type TurnUsage } from "../state/thread";
import {
CONTINUE_MARK,
diffCounts,
RESUME_MARK,
stripHarnessMarkup,
type TranscriptItem,
type TurnUsage,
} from "../state/thread";

// ---------------------------------------------------------------- folded sections

Expand Down Expand Up @@ -273,8 +280,13 @@ export function pullBranches(own: string | undefined, delegated: readonly (strin
/** One finished turn, as the Execution log lists it. */
export interface TurnEntry {
id: string;
/** The first line of the message that opened it, or what woke the thread. */
/**
* The first line of the message that opened it. A turn the app started on its own (a wake, the
* continue after a compaction or a restart) is named by the first sentence of its last reply.
*/
prompt: string;
/** What started a turn the app began on its own ("After compaction"), shown beside its numbers. */
origin?: string;
/** When the message that opened it was sent, so a stored turn can be matched to it. */
promptAt?: number;
at?: number;
Expand Down Expand Up @@ -324,6 +336,45 @@ function promptLine(text: string): string {
return line?.trim() ?? "";
}

/** What started a turn the app began on its own, or nothing for a message someone sent. */
export function turnOrigin(item: Extract<TranscriptItem, { kind: "user" }>): string | undefined {
if (item.wake) return "Woken for a delegate";
if (item.text.includes(CONTINUE_MARK)) return "After compaction";
if (item.text.includes(RESUME_MARK)) return "After restart";
return undefined;
}

/** A reply's first sentence, as plain words on one line. */
export function replyHeadline(text: string): string {
const lines = stripHarnessMarkup(text)
.replace(/```[\s\S]*?(```|$)/g, "\n")
.split("\n")
.map((part) => ({ heading: /^\s{0,3}#{1,6}\s/.test(part), words: plainLine(part) }))
.filter((part) => part.words);
const line = (lines.find((part) => !part.heading) ?? lines[0])?.words ?? "";
const end = line.search(/[.!?](\s|$)/);
return end < 0 ? line : line.slice(0, end + 1);
}

function plainLine(line: string): string {
return line
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
.replace(/^\s{0,3}(#{1,6}\s+|>\s?|[-*+]\s+|\d+\.\s+)/, "")
.replace(/[`*_~]/g, "")
.trim();
}

/** A turn's row title: an app-started turn is named by what it did, once it replied. */
function turnHeading(
prompt: string,
origin: string | undefined,
reply: string,
): Pick<TurnEntry, "prompt" | "origin"> {
const headline = origin ? replyHeadline(reply) : "";
if (headline) return { prompt: headline, origin };
return { prompt: prompt || "No message" };
}

/**
* The thread's turns, oldest first, each with the message that opened it. A
* turn the app woke the thread for has no message of its own, so its row says
Expand All @@ -335,6 +386,8 @@ export function turnEntries(items: readonly TranscriptItem[], running = false):
const entries: TurnEntry[] = [];
let from = 0;
let prompt = "";
let origin: string | undefined;
let reply = "";
let userId: string | undefined;
let promptAt: number | undefined;
let worked = false;
Expand All @@ -351,7 +404,7 @@ export function turnEntries(items: readonly TranscriptItem[], running = false):
const end = items[i - 1] as Extract<TranscriptItem, { kind: "turnEnd" }>;
entries.push({
id: end.id,
prompt: prompt || "No message",
...turnHeading(prompt, origin, reply),
promptAt,
at: end.at,
startedAt: end.startedAt,
Expand All @@ -364,10 +417,11 @@ export function turnEntries(items: readonly TranscriptItem[], running = false):
userId = undefined;
promptAt = undefined;
worked = false;
reply = "";
} else if (nextTurn) {
entries.push({
id: userId ?? items[from]!.id,
prompt: prompt || "No message",
...turnHeading(prompt, origin, reply),
promptAt,
outcome: "completed",
});
Expand All @@ -376,14 +430,17 @@ export function turnEntries(items: readonly TranscriptItem[], running = false):
userId = undefined;
promptAt = undefined;
worked = false;
reply = "";
}
if (!item) break;
if (item.kind === "user" && !item.steered) {
origin = turnOrigin(item);
prompt = item.wake ? "Woken for a delegate" : promptLine(item.text);
if (userId === undefined) promptAt = item.wake ? undefined : item.at;
userId ??= item.id;
} else if (item.kind === "assistant" || item.kind === "tool" || item.kind === "thought") {
worked = true;
if (item.kind === "assistant" && item.text.trim()) reply = item.text;
}
}
return entries;
Expand Down
Loading