Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions apps/mobile/src/lib/threadActivity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,49 @@ describe("buildThreadFeed", () => {
expect(nextRows.some((row) => row.type === "activity-group")).toBe(true);
});

it("keeps thinking activities in their own feed group between tool calls", () => {
const turnId = TurnId.make("turn-1");
const thread = makeThread({
id: ThreadId.make("feed-thinking"),
projectId: ProjectId.make("project-1"),
title: "Feed thinking",
messages: [],
activities: [
makeActivity({
id: EventId.make("tool-1"),
kind: "tool.completed",
summary: "Ran command",
tone: "tool",
createdAt: "2026-04-01T00:00:01.000Z",
turnId,
payload: { itemType: "command_execution", status: "completed" },
}),
makeActivity({
id: EventId.make("thinking-1"),
kind: "thinking",
summary: "Thinking",
tone: "info",
createdAt: "2026-04-01T00:00:02.000Z",
turnId,
payload: { detail: "Checking the checklist first." },
}),
makeActivity({
id: EventId.make("tool-2"),
kind: "tool.completed",
summary: "Ran command",
tone: "tool",
createdAt: "2026-04-01T00:00:03.000Z",
turnId,
payload: { itemType: "command_execution", status: "completed" },
}),
],
});

const feed = buildThreadFeed(thread);
expect(feed.map((row) => row.id)).toEqual(["tool-1", "thinking-1", "tool-2"]);
expect(feed[1]).toMatchObject({ type: "activity-group" });
});

it("regroups cached activities for message changes and pagination", () => {
const messages = [2, 4].map((second) => ({
id: MessageId.make(`message-${second}`),
Expand Down
14 changes: 11 additions & 3 deletions apps/mobile/src/lib/threadActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
requestKindFromRequestType,
type PendingApproval,
} from "@t3tools/client-runtime/pending-requests";
import { UserInputAttachmentAnswerPayload, isToolLifecycleItemType } from "@t3tools/contracts";
import {
THINKING_ACTIVITY_KIND,
UserInputAttachmentAnswerPayload,
isToolLifecycleItemType,
} from "@t3tools/contracts";
import type {
OrchestrationLatestTurn,
OrchestrationThread,
Expand Down Expand Up @@ -1555,12 +1559,16 @@ function groupAdjacentActivities(entries: ReadonlyArray<RawThreadFeedEntry>): Th
}

const isCompaction = entry.activity.workEntry.sourceActivityKind === "context-compaction";
if (isCompaction || firstActivityEntry?.turnId !== entry.turnId) {
// Thinking rows stay visible outside collapsed tool groups (mirrors web):
// they break the run on both sides so a reasoning-narrated turn keeps its
// narrative without expanding dozens of tool calls.
const isThinking = entry.activity.workEntry.sourceActivityKind === THINKING_ACTIVITY_KIND;
if (isCompaction || isThinking || firstActivityEntry?.turnId !== entry.turnId) {
flushGroup();
}
firstActivityEntry ??= entry;
openGroupActivities.push(entry.activity);
if (isCompaction) {
if (isCompaction || isThinking) {
flushGroup();
}
}
Expand Down
207 changes: 206 additions & 1 deletion apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,9 @@ describe("ProviderRuntimeIngestion", () => {
const harness = await createHarness();
const initial = await harness.readModel();

for (const streamKind of ["reasoning_text", "command_output", "file_change_output"] as const) {
// Reasoning streams are persisted as thinking activities (tested below);
// tool-output streams stay ignored because tool rows already show them.
for (const streamKind of ["command_output", "file_change_output", "unknown"] as const) {
harness.emit({
type: "content.delta",
eventId: asEventId(`evt-ignored-${streamKind}`),
Expand All @@ -1341,6 +1343,209 @@ describe("ProviderRuntimeIngestion", () => {
expect(await harness.readModel()).toEqual(initial);
});

it("persists reasoning deltas as thinking activities ordered before tool rows", async () => {
const harness = await createHarness();
const thoughtAt = "2026-01-01T00:00:00.000Z";
const toolAt = "2026-01-01T00:00:01.000Z";

harness.emit({
type: "content.delta",
eventId: asEventId("evt-reason-1"),
provider: ProviderDriverKind.make("antigravity"),
createdAt: thoughtAt,
threadId: asThreadId("thread-1"),
turnId: asTurnId("turn-thinking"),
payload: {
streamKind: "reasoning_text",
delta: "Checking the ",
},
});
harness.emit({
type: "content.delta",
eventId: asEventId("evt-reason-2"),
provider: ProviderDriverKind.make("antigravity"),
createdAt: thoughtAt,
threadId: asThreadId("thread-1"),
turnId: asTurnId("turn-thinking"),
payload: {
streamKind: "reasoning_text",
delta: "checklist first.",
},
});
harness.emit({
type: "item.completed",
eventId: asEventId("evt-thinking-tool"),
provider: ProviderDriverKind.make("antigravity"),
createdAt: toolAt,
threadId: asThreadId("thread-1"),
turnId: asTurnId("turn-thinking"),
itemId: asItemId("tool-1"),
payload: {
itemType: "command_execution",
status: "completed",
title: "Ran command",
detail: "git status",
},
});

await harness.drain();
const thread = await waitForThread(harness.readModel, (entry) =>
entry.activities.some(
(activity: ProviderRuntimeTestActivity) => activity.kind === "thinking",
),
);
const thinking = thread.activities.find(
(activity: ProviderRuntimeTestActivity) => activity.kind === "thinking",
);
expect(thinking?.tone).toBe("info");
expect(thinking?.summary).toBe("Thinking");
expect(thinking?.turnId).toBe("turn-thinking");
expect(thinking?.createdAt).toBe(thoughtAt);
expect((thinking?.payload as Record<string, unknown> | undefined)?.detail).toBe(
"Checking the checklist first.",
);
const kinds = thread.activities.map(
(activity: ProviderRuntimeTestActivity) => activity.kind,
);
expect(kinds.indexOf("thinking")).toBeLessThan(kinds.indexOf("tool.completed"));
});

it("flushes buffered thinking when the turn completes", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";

harness.emit({
type: "content.delta",
eventId: asEventId("evt-summary-reason"),
provider: ProviderDriverKind.make("codex"),
createdAt: now,
threadId: asThreadId("thread-1"),
turnId: asTurnId("turn-9"),
payload: {
streamKind: "reasoning_summary_text",
delta: "ci, websearch",
},
});
harness.emit({
type: "turn.completed",
eventId: asEventId("evt-turn-9-completed"),
provider: ProviderDriverKind.make("codex"),
threadId: asThreadId("thread-1"),
createdAt: now,
turnId: asTurnId("turn-9"),
payload: { state: "completed" },
});

await harness.drain();
const thread = await waitForThread(
harness.readModel,
(entry) =>
entry.session?.status === "ready" &&
entry.activities.some(
(activity: ProviderRuntimeTestActivity) => activity.kind === "thinking",
),
);
const thinking = thread.activities.filter(
(activity: ProviderRuntimeTestActivity) => activity.kind === "thinking",
);
expect(thinking).toHaveLength(1);
expect((thinking[0]?.payload as Record<string, unknown> | undefined)?.detail).toBe(
"ci, websearch",
);
expect(thinking[0]?.turnId).toBe("turn-9");
});

it("splits long thinking buffers across activities without losing text", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
const longThought = "x".repeat(9_000);

harness.emit({
type: "content.delta",
eventId: asEventId("evt-long-reason"),
provider: ProviderDriverKind.make("opencode"),
createdAt: now,
threadId: asThreadId("thread-1"),
turnId: asTurnId("turn-long"),
payload: {
streamKind: "reasoning_text",
delta: longThought,
},
});
harness.emit({
type: "turn.completed",
eventId: asEventId("evt-turn-long-completed"),
provider: ProviderDriverKind.make("opencode"),
threadId: asThreadId("thread-1"),
createdAt: now,
turnId: asTurnId("turn-long"),
payload: { state: "completed" },
});

await harness.drain();
const thread = await waitForThread(harness.readModel, (entry) =>
entry.activities.some(
(activity: ProviderRuntimeTestActivity) => activity.kind === "thinking",
),
);
const thinking = thread.activities.filter(
(activity: ProviderRuntimeTestActivity) => activity.kind === "thinking",
);
expect(thinking.length).toBeGreaterThan(1);
const thinkingText = (activity: ProviderRuntimeTestActivity): string => {
const detail = (activity.payload as Record<string, unknown> | undefined)?.detail;
return typeof detail === "string" ? detail : "";
};
expect(thinking.map(thinkingText).join("")).toBe(longThought);
for (const activity of thinking) {
expect(thinkingText(activity).length).toBeLessThanOrEqual(8_000);
}
});

it("skips whitespace-only thinking buffers", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";

harness.emit({
type: "content.delta",
eventId: asEventId("evt-blank-reason"),
provider: ProviderDriverKind.make("claudeAgent"),
createdAt: now,
threadId: asThreadId("thread-1"),
turnId: asTurnId("turn-blank"),
payload: {
streamKind: "reasoning_text",
delta: " \n ",
},
});
harness.emit({
type: "item.completed",
eventId: asEventId("evt-blank-tool"),
provider: ProviderDriverKind.make("claudeAgent"),
createdAt: now,
threadId: asThreadId("thread-1"),
turnId: asTurnId("turn-blank"),
itemId: asItemId("tool-blank"),
payload: {
itemType: "dynamic_tool_call",
status: "completed",
title: "Tool call",
},
});

await harness.drain();
const thread = await waitForThread(harness.readModel, (entry) =>
entry.activities.some(
(activity: ProviderRuntimeTestActivity) => activity.kind === "tool.completed",
),
);
expect(
thread.activities.some(
(activity: ProviderRuntimeTestActivity) => activity.kind === "thinking",
),
).toBe(false);
});

it("maps canonical content delta/item completed into finalized assistant messages", async () => {
const harness = await createHarness();
const now = "2026-01-01T00:00:00.000Z";
Expand Down
Loading
Loading