fix(server): preserve tool lifecycle identity (#7151)
This commit is contained in:
1 parent
792a1404f6
commit
b2e2ccfdb4
4 files changed
+96
-18
No files matched your search
@@ -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({
|
||||
|
||||
@@ -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}`;
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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 }
|
||||
|
||||
Reference in new issue
Block a user