feat(headroom): report effective payload savings
Break out tool schema and tool-history bytes in the size snapshot and add an effective byte-savings percentage so token-saved logs reflect the actual outbound payload reduction, not just processed content.
This commit is contained in:
@@ -25,9 +25,17 @@ function messagePayload(body) {
|
|||||||
|
|
||||||
function captureSizeSnapshot(body) {
|
function captureSizeSnapshot(body) {
|
||||||
const messages = messagePayload(body);
|
const messages = messagePayload(body);
|
||||||
|
const toolHistory = messages?.filter((message) =>
|
||||||
|
message?.role === "tool"
|
||||||
|
|| message?.role === "function"
|
||||||
|
|| message?.tool_calls?.length
|
||||||
|
|| message?.content?.some?.((part) => part?.type === "tool_use" || part?.type === "tool_result")
|
||||||
|
) || [];
|
||||||
return {
|
return {
|
||||||
bodyBytes: jsonBytes(body),
|
bodyBytes: jsonBytes(body),
|
||||||
messageBytes: messages ? jsonBytes(messages) : 0,
|
messageBytes: messages ? jsonBytes(messages) : 0,
|
||||||
|
toolSchemaBytes: jsonBytes(body?.tools || []),
|
||||||
|
toolHistoryBytes: jsonBytes(toolHistory),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -336,7 +344,10 @@ export function formatHeadroomSizeLog(diagnostics) {
|
|||||||
const before = diagnostics?.before;
|
const before = diagnostics?.before;
|
||||||
const after = diagnostics?.after;
|
const after = diagnostics?.after;
|
||||||
if (!before || !after) return "";
|
if (!before || !after) return "";
|
||||||
return `body=${before.bodyBytes}B→${after.bodyBytes}B messages=${before.messageBytes}B→${after.messageBytes}B`;
|
const effective = before.bodyBytes > 0
|
||||||
|
? (((before.bodyBytes - after.bodyBytes) / before.bodyBytes) * 100).toFixed(1)
|
||||||
|
: "0.0";
|
||||||
|
return `body=${before.bodyBytes}B→${after.bodyBytes}B messages=${before.messageBytes}B→${after.messageBytes}B tools=${before.toolSchemaBytes || 0}B→${after.toolSchemaBytes || 0}B toolHistory=${before.toolHistoryBytes || 0}B→${after.toolHistoryBytes || 0}B effective=${effective}%`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isHeadroomPhantomSavings(stats, diagnostics, minShrinkRatio = 0.05) {
|
export function isHeadroomPhantomSavings(stats, diagnostics, minShrinkRatio = 0.05) {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, it, expect, vi, afterEach } from "vitest";
|
import { describe, it, expect, vi, afterEach } from "vitest";
|
||||||
import { compressWithHeadroom, formatHeadroomLog } from "../../open-sse/rtk/headroom.js";
|
import { compressWithHeadroom, formatHeadroomLog, formatHeadroomSizeLog } from "../../open-sse/rtk/headroom.js";
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
@@ -209,4 +209,11 @@ describe("formatHeadroomLog", () => {
|
|||||||
expect(formatHeadroomLog({ tokens_before: 100, tokens_after: 25, tokens_saved: 75 }))
|
expect(formatHeadroomLog({ tokens_before: 100, tokens_after: 25, tokens_saved: 75 }))
|
||||||
.toBe("reported token delta=75 before=100 after=25 (75.0%)");
|
.toBe("reported token delta=75 before=100 after=25 (75.0%)");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("reports effective payload, tool-schema, and tool-history sizes", () => {
|
||||||
|
expect(formatHeadroomSizeLog({
|
||||||
|
before: { bodyBytes: 1000, messageBytes: 800, toolSchemaBytes: 100, toolHistoryBytes: 500 },
|
||||||
|
after: { bodyBytes: 900, messageBytes: 700, toolSchemaBytes: 100, toolHistoryBytes: 400 },
|
||||||
|
})).toContain("tools=100B→100B toolHistory=500B→400B effective=10.0%");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user