Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

fix(server): preserve tool lifecycle identity (#7151)

mariab2e2ccfdb462
diff --git a/apps/server/src/orchestration/ActivityPayloadProjection.test.ts b/apps/server/src/orchestration/ActivityPayloadProjection.test.ts
index 229c7f8ab..2cdfef19f 100644
--- a/apps/server/src/orchestration/ActivityPayloadProjection.test.ts
+++ b/apps/server/src/orchestration/ActivityPayloadProjection.test.ts
@@ -20,7 +20,7 @@ function activity(payload: Record<string, unknown>): OrchestrationThreadActivity
* If slimming ever moves to an allowlist over the whole payload, these
* assertions are the tripwire.
*/
-describe("projectActivityPayload agent-field survival", () => {
+describe("projectActivityPayload", () => {
it("preserves tool attribution (agentId/parentToolUseId) through data slimming", () => {
const projected = projectActivityPayload(
activity({
@@ -97,6 +97,45 @@ describe("projectActivityPayload agent-field survival", () => {
expect(JSON.stringify(acp.payload).length).toBeLessThan(500);
});
+ it("normalizes Claude and OpenCode command inputs before slimming provider data", () => {
+ const claude = projectActivityPayload(
+ activity({
+ itemType: "command_execution",
+ toolCallId: "claude-call-1",
+ data: {
+ toolName: "Bash",
+ input: { command: "vp test run" },
+ result: { content: "x".repeat(5_000) },
+ },
+ }),
+ );
+ const openCode = projectActivityPayload(
+ activity({
+ itemType: "command_execution",
+ toolCallId: "opencode-call-1",
+ data: {
+ tool: "bash",
+ state: {
+ status: "running",
+ input: { command: "vp lint" },
+ output: "x".repeat(5_000),
+ },
+ },
+ }),
+ );
+
+ expect(claude.payload).toMatchObject({
+ toolCallId: "claude-call-1",
+ data: { command: "vp test run" },
+ });
+ expect(openCode.payload).toMatchObject({
+ toolCallId: "opencode-call-1",
+ data: { command: "vp lint" },
+ });
+ expect(JSON.stringify(claude.payload).length).toBeLessThan(200);
+ expect(JSON.stringify(openCode.payload).length).toBeLessThan(200);
+ });
+
it("slims Codex-shaped mcp_tool_call items to rendered fields plus a result summary", () => {
const projected = projectActivityPayload(
activity({
diff --git a/apps/server/src/orchestration/ActivityPayloadProjection.ts b/apps/server/src/orchestration/ActivityPayloadProjection.ts
index e6333a1ca..103b267d2 100644
--- a/apps/server/src/orchestration/ActivityPayloadProjection.ts
+++ b/apps/server/src/orchestration/ActivityPayloadProjection.ts
@@ -125,6 +125,24 @@ function projectCommandData(data: Record<string, unknown>): Record<string, unkno
return Object.keys(projectedItem).length > 0 ? projectedItem : undefined;
}
+function projectCommandValue(data: Record<string, unknown>): unknown {
+ if (data.command !== undefined) {
+ return data.command;
+ }
+
+ const input = asRecord(data.input);
+ if (input?.command !== undefined) {
+ return input.command;
+ }
+
+ const stateInput = asRecord(asRecord(data.state)?.input);
+ if (stateInput?.command !== undefined) {
+ return stateInput.command;
+ }
+
+ return undefined;
+}
+
function summarizeToolTextOutput(value: string): string | null {
const lines: string[] = [];
for (const rawLine of value.split(/\r?\n/u)) {
@@ -339,8 +357,9 @@ export function projectActivityPayload(
if (item) {
projectedData.item = item;
}
- if ("command" in data) {
- projectedData.command = data.command;
+ const command = projectCommandValue(data);
+ if (command !== undefined) {
+ projectedData.command = command;
}
const changedFiles: string[] = [];
@@ -418,12 +437,10 @@ function dropStaleContextWindowActivities(
}
/**
- * Identity both clients use to fold a tool lifecycle row into the call it
- * belongs to (`deriveToolLifecycleCollapseKey` in web's `session-logic` and
- * mobile's `threadActivity`): an explicit `data.toolCallId` when the adapter
- * emits one, otherwise the itemType/title/detail triple. Returns null for rows
- * with no identity at all — those never collapse on the client either, so they
- * must not be dropped here.
+ * Identity used to retain only the newest lifecycle row for each call in a
+ * thread snapshot. Prefer the runtime item id, then the legacy nested id, and
+ * finally the itemType/title/detail triple. Rows without any identity remain
+ * untouched.
*/
function toolLifecycleIdentity(activity: OrchestrationThreadActivity): string | null {
const payload = asRecord(activity.payload);
@@ -431,7 +448,8 @@ function toolLifecycleIdentity(activity: OrchestrationThreadActivity): string |
return null;
}
- const toolCallId = asTrimmedString(asRecord(payload.data)?.toolCallId);
+ const toolCallId =
+ asTrimmedString(payload.toolCallId) ?? asTrimmedString(asRecord(payload.data)?.toolCallId);
if (toolCallId) {
return `id:${toolCallId}`;
}
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
index 449b1fbf5..1e1374c96 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
@@ -2815,11 +2815,16 @@ describe("ProviderRuntimeIngestion", () => {
createdAt: now,
threadId: asThreadId("thread-1"),
turnId: asTurnId("turn-9"),
+ itemId: asItemId("tool-call-9"),
payload: {
itemType: "command_execution",
- status: "in_progress",
- title: "Read file",
- detail: "/tmp/file.ts",
+ status: "inProgress",
+ title: "Command run",
+ detail: "Bash: vp test run",
+ data: {
+ toolName: "Bash",
+ input: { command: "vp test run" },
+ },
},
});
@@ -2834,11 +2839,20 @@ describe("ProviderRuntimeIngestion", () => {
);
expect(thread.session?.status).toBe("ready");
- expect(
- thread.activities.some(
- (activity: ProviderRuntimeTestActivity) => activity.kind === "tool.started",
- ),
- ).toBe(true);
+ const activity = thread.activities.find(
+ (entry: ProviderRuntimeTestActivity) => entry.kind === "tool.started",
+ );
+ const payload = activity?.payload as Record<string, unknown> | undefined;
+ expect(payload).toMatchObject({
+ itemType: "command_execution",
+ toolCallId: "tool-call-9",
+ status: "inProgress",
+ detail: "Bash: vp test run",
+ data: {
+ toolName: "Bash",
+ input: { command: "vp test run" },
+ },
+ });
});
it("consumes P1 runtime events into thread metadata, diff checkpoints, and activities", async () => {
@@ -2956,6 +2970,7 @@ describe("ProviderRuntimeIngestion", () => {
expect(toolUpdate?.kind).toBe("tool.updated");
expect(toolUpdatePayload?.itemType).toBe("command_execution");
expect(toolUpdatePayload?.status).toBe("in_progress");
+ expect(toolUpdatePayload?.toolCallId).toBe("item-p1-tool");
const warning = thread.activities.find(
(activity: ProviderRuntimeTestActivity) => activity.id === "evt-runtime-warning",
diff --git a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
index e24825d3d..953ba1ec9 100644
--- a/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
+++ b/apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
@@ -803,6 +803,7 @@ export function runtimeEventToActivities(
summary: event.payload.title ?? "Tool updated",
payload: {
itemType: event.payload.itemType,
+ ...(event.itemId !== undefined ? { toolCallId: event.itemId } : {}),
...(event.payload.status ? { status: event.payload.status } : {}),
...(event.payload.detail ? { detail: truncateDetail(event.payload.detail) } : {}),
...(event.payload.data !== undefined ? { data: event.payload.data } : {}),
@@ -830,6 +831,8 @@ export function runtimeEventToActivities(
summary: event.payload.title ?? "Tool",
payload: {
itemType: event.payload.itemType,
+ ...(event.itemId !== undefined ? { toolCallId: event.itemId } : {}),
+ ...(event.payload.status ? { status: event.payload.status } : {}),
...(event.payload.detail ? { detail: truncateDetail(event.payload.detail) } : {}),
...(event.payload.data !== undefined ? { data: event.payload.data } : {}),
...(event.payload.agentId ? { agentId: event.payload.agentId } : {}),
@@ -856,7 +859,10 @@ export function runtimeEventToActivities(
summary: `${event.payload.title ?? "Tool"} started`,
payload: {
itemType: event.payload.itemType,
+ ...(event.itemId !== undefined ? { toolCallId: event.itemId } : {}),
+ ...(event.payload.status ? { status: event.payload.status } : {}),
...(event.payload.detail ? { detail: truncateDetail(event.payload.detail) } : {}),
+ ...(event.payload.data !== undefined ? { data: event.payload.data } : {}),
...(event.payload.agentId ? { agentId: event.payload.agentId } : {}),
...(event.payload.parentToolUseId
? { parentToolUseId: event.payload.parentToolUseId }