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
4 changes: 2 additions & 2 deletions src/components/thread/DelegationSubTraceBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { TokenUsage, TraceRound } from "../../types/thread";
// settled by issue #1355, replacing the #934 modal): the row head carries
// the fold toggle (TraceRow owns it), and the panel renders full width
// beneath the row -- the sub-agent's rounds in the main trace's folding
// posture (each round's thinking fold + connective prose + its "Trace ·
// N calls" step fold, stacked flush exactly as the main trace stacks its
// posture (each round's thinking fold + connective prose + its per-tool
// count step fold, stacked flush exactly as the main trace stacks its
// rounds), the running face's task head above them, the usage subtotal as
// the closing receipt -- so a sub-round reads exactly like a main round.
// The rows reuse the main trace's row list, whose nesting is physically
Expand Down
76 changes: 53 additions & 23 deletions src/components/thread/TraceRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useState, type ReactNode } from "react";
import { FormattedMessage, useIntl, type IntlShape } from "react-intl";
import { Check, TriangleAlert } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { cn } from "@/lib/utils";
import { FoldToggle } from "./FoldToggle";
import { TraceList } from "./TraceList";
import { TraceSummaryFold } from "./TraceSummaryFold";
Expand Down Expand Up @@ -81,13 +80,36 @@ function SuccessGlyph({ success }: { success: boolean }) {
);
}

// One round's step fold (the "Trace · N calls" tier, ADR-0078's folding
// discipline): the round's call count reads on the fold head so a rail scan
// shows which rounds made multiple calls without expanding; the rows mount
// only while expanded. Shared by the settled main trace's round adapter
// (TurnCard) and the delegation sub-trace's rounds (issue #1355) so the two
// faces cannot drift; `renderSubTrace` is the delegation panel injection
// (issue #934) -- the sub-face passes none.
// The step fold's head label (issue #1604, recalibrated on the device):
// per-tool call counts -- "explore ×3 · describe ×1". The tool name is
// the call's identity; the operation category alone collapsed to noise
// on a single-category round (a data-exploration turn of all reads read
// as an undifferentiated 读 ×N). Order is first appearance -- the trace
// is append-only within a round, so re-renders never reshuffle it, and
// tool names are untranslated data, so locale switches cannot either.
// The category stays on the row's OperationBadge.
function foldLabel(intl: IntlShape, calls: ReadonlyArray<TraceEntry>): string {
// A Map keeps its insertion order, which over an append-only trace is
// the first-appearance order above.
const counts = new Map<string, number>();
for (const call of calls) {
counts.set(call.name, (counts.get(call.name) ?? 0) + 1);
}
return Array.from(counts, ([tool, count]) =>
intl.formatMessage(
{ id: "thread.trace.toolCount", defaultMessage: "{tool} ×{count}" },
{ tool, count },
),
).join(" · ");
}

// One round's step fold (ADR-0078's folding discipline): the fold head
// speaks the round's work mix (the per-tool counts above) so a rail scan
// shows which tools a round ran without expanding; the rows mount only
// while expanded. Shared by the settled main trace's round adapter
// (TurnCard) and the delegation sub-trace's rounds (issue #1355) so the
// two faces cannot drift; `renderSubTrace` is the delegation panel
// injection (issue #934) -- the sub-face passes none.
export function TraceStepFold({
calls,
renderSubTrace,
Expand All @@ -96,6 +118,7 @@ export function TraceStepFold({
renderSubTrace?: (entry: TraceEntry) => ReactNode;
}) {
const [stepsExpanded, setStepsExpanded] = useState(false);
const intl = useIntl();
if (calls.length === 0) return null;
return (
<>
Expand All @@ -104,11 +127,7 @@ export function TraceStepFold({
expanded={stepsExpanded}
onToggle={() => setStepsExpanded((v) => !v)}
>
<FormattedMessage
id="thread.trace.toggle"
defaultMessage="Trace · {count} {count, plural, one {call} other {calls}}"
values={{ count: calls.length }}
/>
{foldLabel(intl, calls)}
</FoldToggle>
{stepsExpanded && (
<TraceRowList entries={calls} renderSubTrace={renderSubTrace} />
Expand Down Expand Up @@ -137,6 +156,7 @@ export function TraceRow({
subTrace?: ReactNode;
}) {
const [subOpen, setSubOpen] = useState(false);
const intl = useIntl();
// The gate lives here, once: a row whose entry carries no sub-rounds (or
// whose caller supplied no body) renders neither the toggle nor the panel,
// so the callers inject the body unconditionally over their row lists.
Expand All @@ -153,6 +173,9 @@ export function TraceRow({
<TraceSummaryFold
summary={entry.summary}
summaryClassName="trace-summary"
resultExcerpt={
entry.success && entry.result_excerpt !== "" ? entry.result_excerpt : undefined
}
head={(
<>
{/* Tool name truncates with the row's summary family
Expand Down Expand Up @@ -185,17 +208,24 @@ export function TraceRow({
</>
)}
/>
{/* The excerpt is the cross-turn retrospection anchor (ADR-0078).
* The one SUCCESS that carries one is a capped delegation report's
* truncation notice (issue #1047) -- the projections keep exactly
* that success excerpt -- rendered muted under the check glyph: the
* call completed, the answer was just cut. */}
{entry.result_excerpt !== "" && (
{/* The failure excerpt is the cross-turn retrospection anchor
* (ADR-0078): visible without a click, destructive-styled, and
* height-capped since #1604 so a multi-KB error cannot push the
* whole screen out of view (the thinking fold's cap face and its
* #1363 keyboard-reachable region posture). A success never
* renders here anymore (#1604) -- the one success excerpt the
* projections keep, the capped delegation report's truncation
* notice (issue #1047), folds into the summary expand block
* above. */}
{!entry.success && entry.result_excerpt !== "" && (
<span
className={cn(
"trace-excerpt block whitespace-pre-wrap break-words text-xs",
entry.success ? "text-muted-foreground" : "text-destructive",
)}
className="trace-excerpt block whitespace-pre-wrap break-words text-xs max-h-48 overflow-y-auto text-destructive"
tabIndex={0}
role="region"
aria-label={intl.formatMessage({
id: "thread.trace.excerptAria",
defaultMessage: "Error excerpt",
})}
>
{entry.result_excerpt}
</span>
Expand Down
21 changes: 20 additions & 1 deletion src/components/thread/TraceSummaryFold.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function TraceSummaryFold({
summary,
summaryClassName,
head = null,
resultExcerpt,
}: {
summary: string;
/** The hook class for the truncated summary span -- the visual
Expand All @@ -48,6 +49,12 @@ export function TraceSummaryFold({
/** The site-specific line head (spinner / shield, tool name, badges)
* rendered inside the line before the summary. */
head?: ReactNode;
/** The call's result excerpt, folded into the expand block beside the
* argument summary as a second column (issue #1604). Absent keeps the
* single-column block -- the trace row passes it only for the one
* success whose excerpt survives the projection (the capped
* delegation's truncation notice, issue #1047). */
resultExcerpt?: string;
}) {
const [expanded, setExpanded] = useState(false);
const intl = useIntl();
Expand Down Expand Up @@ -95,7 +102,19 @@ export function TraceSummaryFold({
defaultMessage: "Full summary",
})}
>
{summary}
{resultExcerpt === undefined ? (
summary
) : (
// Both faces of the call in one capped block (issue #1604):
// the argument summary left, the result excerpt right of the
// divider -- each column free to wrap, the cap bounds the pair.
<span className="flex gap-2">
<span className="min-w-0 flex-1">{summary}</span>
<span className="min-w-0 flex-1 border-l border-border pl-2">
{resultExcerpt}
</span>
</span>
)}
</p>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("DelegationSubTraceBody inline panel (issue #1355)", () => {
// step fold (the main trace's posture), collapsed by default.
expect(screen.getByText("Checking the sheet first.")).toBeInTheDocument();
expect(screen.queryByText("SELECT count(*) AS n FROM result_1")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: /trace · 2 calls/i }));
fireEvent.click(screen.getByRole("button", { name: /explore ×1 · materialize ×1/i }));
expect(screen.getByText("SELECT count(*) AS n FROM result_1")).toBeInTheDocument();
// The failed sub-call keeps its bounded excerpt -- the cross-turn
// failure anchor.
Expand Down Expand Up @@ -123,9 +123,9 @@ describe("DelegationSubTraceBody inline panel (issue #1355)", () => {
const toggles = container.querySelectorAll(".thinking-toggle");
expect(toggles).toHaveLength(1);
expect(toggles[0].getAttribute("aria-expanded")).toBe("false");
// A sub-round with no calls renders no step fold at all -- the "Trace
// · 0 calls" head must not appear (TraceStepFold's empty guard, the
// only line of defense on this face).
// A sub-round with no calls renders no step fold at all -- no count
// head must appear (TraceStepFold's empty guard, the only line of
// defense on this face).
expect(container.querySelectorAll(".trace-toggle")).toHaveLength(0);
expect(screen.queryByText("inner reasoning")).not.toBeInTheDocument();
fireEvent.click(toggles[0]);
Expand Down
24 changes: 12 additions & 12 deletions src/components/thread/__tests__/Thread.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ describe("Thread", () => {
}

it("defaults COLLAPSED: question + outcome visible, the process behind the turn head", () => {
renderThread(
const { container } = renderThread(
<Thread
entries={[turnEntry(tracedRecord())]}
selectedResult={null}
Expand All @@ -1060,7 +1060,7 @@ describe("Thread", () => {
expect(screen.getByText("多少行")).toBeInTheDocument();
const head = screen.getByRole("button", { name: /已取消/ });
expect(head).toHaveAttribute("aria-expanded", "false");
expect(screen.queryByRole("button", { name: /轨迹 · 2 次调用/ })).not.toBeInTheDocument();
expect(container.querySelector(".trace-toggle")).toBeNull();
expect(screen.queryByText("no such table")).not.toBeInTheDocument();
expect(screen.queryByText("SELECT count(*) FROM people")).not.toBeInTheDocument();
});
Expand All @@ -1075,8 +1075,8 @@ describe("Thread", () => {
);
// Issue #1282: the head opens first; the per-round folds live inside it.
fireEvent.click(screen.getByRole("button", { name: /已取消/ }));
fireEvent.click(screen.getByRole("button", { name: /轨迹 · 2 次调用/ }));
const toggle = screen.getByRole("button", { name: /轨迹 · 2 次调用/ });
fireEvent.click(screen.getByRole("button", { name: "explore ×1 · materialize ×1" }));
const toggle = screen.getByRole("button", { name: "explore ×1 · materialize ×1" });
expect(toggle).toHaveAttribute("aria-expanded", "true");
// Both calls render with their summaries + operation badges.
expect(screen.getByText("explore")).toBeInTheDocument();
Expand All @@ -1089,14 +1089,14 @@ describe("Thread", () => {
});

it("omits the toggle for a zero-call turn (no trace to expand)", () => {
renderThread(
const { container } = renderThread(
<Thread
entries={[turnEntry({ question: "q", outcome: { kind: "Cancelled", data: null }, trace: [], provenance: { skills: [] } })]}
selectedResult={null}
onSelectResult={() => {}}
/>,
);
expect(screen.queryByRole("button", { name: /轨迹/ })).not.toBeInTheDocument();
expect(container.querySelector(".trace-toggle")).toBeNull();
});
});

Expand Down Expand Up @@ -1309,8 +1309,8 @@ describe("Thread", () => {
// Thinking + steps folds are per round, both default collapsed.
const thinking = screen.getByRole("button", { name: "思考 · 1.5s" });
expect(thinking).toHaveAttribute("aria-expanded", "false");
const round1 = screen.getByRole("button", { name: "轨迹 · 2 次调用" });
const round2 = screen.getByRole("button", { name: "轨迹 · 1 次调用" });
const round1 = screen.getByRole("button", { name: "explore ×1 · materialize ×1" });
const round2 = screen.getByRole("button", { name: "explore ×1" });
expect(round1).toHaveAttribute("aria-expanded", "false");
expect(round2).toHaveAttribute("aria-expanded", "false");
expect(screen.queryByText("先推理一下")).not.toBeInTheDocument();
Expand All @@ -1323,17 +1323,17 @@ describe("Thread", () => {
fireEvent.click(screen.getByRole("button", { name: /已回答/ }));
fireEvent.click(screen.getByRole("button", { name: "思考 · 1.5s" }));
expect(screen.getByText("先推理一下")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "轨迹 · 2 次调用" }));
fireEvent.click(screen.getByRole("button", { name: "explore ×1 · materialize ×1" }));
expect(screen.getByText("SELECT 1")).toBeInTheDocument();
expect(screen.getByText("SELECT 2")).toBeInTheDocument();
// Round 2's fold stays collapsed -- its call is still hidden.
expect(screen.queryByText("SELECT 3")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "轨迹 · 1 次调用" }));
fireEvent.click(screen.getByRole("button", { name: "explore ×1" }));
expect(screen.getByText("SELECT 3")).toBeInTheDocument();
});

it("folds an all-prose round (no calls, no thinking) behind the head, with no inner fold chrome", () => {
renderChat(
const { container } = renderChat(
chatRecord({
trace: [
{ text: "直接答复", calls: [] },
Expand All @@ -1344,7 +1344,7 @@ describe("Thread", () => {
// (issue #1282) and carries no per-round fold chrome of its own.
fireEvent.click(screen.getByRole("button", { name: /已回答/ }));
expect(screen.getByText("直接答复")).toBeInTheDocument();
expect(screen.queryByRole("button", { name: /轨迹/ })).not.toBeInTheDocument();
expect(container.querySelector(".trace-toggle")).toBeNull();
expect(screen.queryByRole("button", { name: /思考/ })).not.toBeInTheDocument();
});

Expand Down
39 changes: 39 additions & 0 deletions src/components/thread/__tests__/TraceSummaryFold.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,42 @@ describe("TraceSummaryFold scroll container (issue #1363)", () => {
expect(screen.queryByText(/Keyboard-reachable/)).not.toBeInTheDocument();
});
});

// Issue #1604: a success row's result excerpt folds into the expand block
// beside the argument summary -- same capped block, two columns.
describe("TraceSummaryFold result excerpt column (issue #1604)", () => {
it("shows the summary and the result excerpt as columns of one block", () => {
const { container } = renderWithProviders(
<TraceSummaryFold
summary="SELECT * FROM result_1"
summaryClassName="trace-summary"
resultExcerpt="42 rows returned"
/>,
);
fireEvent.click(container.querySelector(".summary-fold-toggle") as Element);
const block = document.querySelector(".summary-fold-block");
expect(block).not.toBeNull();
expect(block).toHaveTextContent("SELECT * FROM result_1");
expect(block).toHaveTextContent("42 rows returned");
// The columnar shape itself: the excerpt rides the divider-bearing
// right column, not a stacked second paragraph.
const divider = block?.querySelector(".border-l");
expect(divider).not.toBeNull();
expect(divider).toHaveTextContent("42 rows returned");
});

it("keeps the single-column block when no excerpt is given", () => {
const { container } = renderWithProviders(
<TraceSummaryFold
summary="SELECT * FROM result_1"
summaryClassName="trace-summary"
/>,
);
fireEvent.click(container.querySelector(".summary-fold-toggle") as Element);
const block = document.querySelector(".summary-fold-block");
expect(block?.textContent).toBe("SELECT * FROM result_1");
// No excerpt means no divider column -- the absent face must not
// regress into an empty right column with a stray hairline.
expect(block?.querySelector(".border-l")).toBeNull();
});
});
Loading
Loading