Skip to content

Commit e71c048

Browse files
longtngoclaude
andcommitted
feat: account usage readout + thinking-token/notification surfacing
Squash-merge of fix/context-window-calculation. Two related features plus the SDK-noise cleanup that motivated them. Usage / context-window meter: - Base the context-window meter on point-in-time assistant-message usage, excluding subagent totals (which previously pinned the meter), and reset to post_tokens on compaction. - Add a footer account-usage readout (5h / 7d / extra-credits) sourced from the Anthropic OAuth usage API, mirroring statusline.sh token resolution. Thinking tokens, notifications, SDK noise: - Silence the per-turn "Runtime warning" spam: unhandled Claude SDK message types/subtypes are dropped to debug logs instead of user-facing warnings (already captured by logNativeSdkMessage). Removed emitRuntimeWarning. - Surface SDK 'notification' messages as in-app toasts (priority-mapped, deduped by activity id). - Render 'thinking_tokens' as a dedicated work-log row that updates in place (stable per-turn id), with emission throttled to one event per visible display bucket so per-delta updates don't flood projection/UI. - Work log: coalesce consecutive identical rows with a xN count badge; show runtime warning/error payload.message as the row detail. - account.usage.updated upserts one row per thread (stable id) instead of appending every poll tick. - contracts: add runtime.notification + runtime.thinking-tokens events. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 92be60e commit e71c048

22 files changed

Lines changed: 2176 additions & 107 deletions

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts‎

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,159 @@ describe("ProviderRuntimeIngestion", () => {
807807
expect(rawOutput?.content).toBe('import * as Effect from "effect/Effect"\n');
808808
});
809809

810+
it("projects account.usage.updated into an account usage activity", async () => {
811+
const harness = await createHarness();
812+
const now = "2026-01-01T00:00:00.000Z";
813+
const usagePayload = {
814+
fiveHour: { utilization: 45, resetsAt: "2026-06-04T19:30:00Z" },
815+
sevenDay: { utilization: 24, resetsAt: "2026-06-08T09:00:00Z" },
816+
extra: {
817+
isEnabled: true,
818+
usedCredits: 43540,
819+
monthlyLimit: 200000,
820+
utilization: 21.77,
821+
currency: "CAD",
822+
},
823+
};
824+
825+
harness.emit({
826+
type: "account.usage.updated",
827+
eventId: asEventId("evt-account-usage"),
828+
provider: ProviderDriverKind.make("claudeAgent"),
829+
createdAt: now,
830+
threadId: asThreadId("thread-1"),
831+
payload: usagePayload,
832+
});
833+
834+
const thread = await waitForThread(harness.readModel, (entry) =>
835+
entry.activities.some(
836+
(activity: ProviderRuntimeTestActivity) => activity.kind === "account.usage.updated",
837+
),
838+
);
839+
const activity = thread.activities.find(
840+
(entry: ProviderRuntimeTestActivity) => entry.kind === "account.usage.updated",
841+
);
842+
843+
// Stable per-thread id so successive poll snapshots upsert one row.
844+
expect(activity?.id).toBe("account-usage:thread-1");
845+
expect(activity?.kind).toBe("account.usage.updated");
846+
expect(activity?.summary).toBe("Account usage updated");
847+
expect(activity?.payload).toStrictEqual(usagePayload);
848+
});
849+
850+
it("upserts successive account.usage.updated snapshots into one row per thread", async () => {
851+
const harness = await createHarness();
852+
853+
harness.emit({
854+
type: "account.usage.updated",
855+
eventId: asEventId("evt-usage-1"),
856+
provider: ProviderDriverKind.make("claudeAgent"),
857+
createdAt: "2026-01-01T00:00:00.000Z",
858+
threadId: asThreadId("thread-1"),
859+
payload: { fiveHour: { utilization: 10, resetsAt: null }, sevenDay: null, extra: null },
860+
});
861+
harness.emit({
862+
type: "account.usage.updated",
863+
eventId: asEventId("evt-usage-2"),
864+
provider: ProviderDriverKind.make("claudeAgent"),
865+
createdAt: "2026-01-01T00:01:00.000Z",
866+
threadId: asThreadId("thread-1"),
867+
payload: { fiveHour: { utilization: 25, resetsAt: null }, sevenDay: null, extra: null },
868+
});
869+
870+
const thread = await waitForThread(harness.readModel, (entry) =>
871+
entry.activities.some(
872+
(activity: ProviderRuntimeTestActivity) =>
873+
activity.kind === "account.usage.updated" &&
874+
(activity.payload as { fiveHour?: { utilization?: number } }).fiveHour?.utilization ===
875+
25,
876+
),
877+
);
878+
const usageActivities = thread.activities.filter(
879+
(entry: ProviderRuntimeTestActivity) => entry.kind === "account.usage.updated",
880+
);
881+
882+
expect(usageActivities).toHaveLength(1);
883+
expect(usageActivities[0]?.id).toBe("account-usage:thread-1");
884+
});
885+
886+
it("projects runtime.notification into a notification activity", async () => {
887+
const harness = await createHarness();
888+
const now = "2026-01-01T00:00:00.000Z";
889+
890+
harness.emit({
891+
type: "runtime.notification",
892+
eventId: asEventId("evt-notification"),
893+
provider: ProviderDriverKind.make("claudeAgent"),
894+
createdAt: now,
895+
threadId: asThreadId("thread-1"),
896+
payload: {
897+
key: "compaction-warning",
898+
text: "Context is getting full",
899+
priority: "high",
900+
timeoutMs: 5000,
901+
},
902+
});
903+
904+
const thread = await waitForThread(harness.readModel, (entry) =>
905+
entry.activities.some(
906+
(activity: ProviderRuntimeTestActivity) => activity.id === "evt-notification",
907+
),
908+
);
909+
const activity = thread.activities.find(
910+
(entry: ProviderRuntimeTestActivity) => entry.id === "evt-notification",
911+
);
912+
913+
expect(activity?.kind).toBe("runtime.notification");
914+
expect(activity?.summary).toBe("Context is getting full");
915+
expect(activity?.payload).toStrictEqual({
916+
key: "compaction-warning",
917+
priority: "high",
918+
timeoutMs: 5000,
919+
});
920+
});
921+
922+
it("collapses runtime.thinking-tokens deltas into one in-place row per turn", async () => {
923+
const harness = await createHarness();
924+
const now = "2026-01-01T00:00:00.000Z";
925+
const later = "2026-01-01T00:00:01.000Z";
926+
927+
harness.emit({
928+
type: "runtime.thinking-tokens",
929+
eventId: asEventId("evt-thinking-1"),
930+
provider: ProviderDriverKind.make("claudeAgent"),
931+
createdAt: now,
932+
threadId: asThreadId("thread-1"),
933+
turnId: asTurnId("turn-1"),
934+
payload: { estimatedTokens: 512, estimatedTokensDelta: 512 },
935+
});
936+
harness.emit({
937+
type: "runtime.thinking-tokens",
938+
eventId: asEventId("evt-thinking-2"),
939+
provider: ProviderDriverKind.make("claudeAgent"),
940+
createdAt: later,
941+
threadId: asThreadId("thread-1"),
942+
turnId: asTurnId("turn-1"),
943+
payload: { estimatedTokens: 2048, estimatedTokensDelta: 1536 },
944+
});
945+
946+
const thread = await waitForThread(harness.readModel, (entry) =>
947+
entry.activities.some(
948+
(activity: ProviderRuntimeTestActivity) =>
949+
activity.kind === "thinking.tokens" &&
950+
(activity.payload as { estimatedTokens?: number }).estimatedTokens === 2048,
951+
),
952+
);
953+
const thinkingActivities = thread.activities.filter(
954+
(entry: ProviderRuntimeTestActivity) => entry.kind === "thinking.tokens",
955+
);
956+
957+
// Both deltas share a stable per-turn id, so they upsert into a single row.
958+
expect(thinkingActivities).toHaveLength(1);
959+
expect(thinkingActivities[0]?.id).toBe("thinking-tokens:turn-1");
960+
expect(thinkingActivities[0]?.summary).toBe("Thinking");
961+
});
962+
810963
it("normalizes command execution activities to ran-command summaries", async () => {
811964
const harness = await createHarness();
812965
const now = "2026-01-01T00:00:00.000Z";

‎apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
ApprovalRequestId,
33
type AssistantDeliveryMode,
44
CommandId,
5+
EventId,
56
MessageId,
67
type OrchestrationEvent,
78
type OrchestrationMessage,
@@ -344,6 +345,50 @@ function runtimeEventToActivities(
344345
];
345346
}
346347

348+
case "runtime.notification": {
349+
return [
350+
{
351+
id: event.eventId,
352+
createdAt: event.createdAt,
353+
tone: "info",
354+
kind: "runtime.notification",
355+
summary: event.payload.text,
356+
payload: {
357+
key: event.payload.key,
358+
priority: event.payload.priority,
359+
...(event.payload.timeoutMs !== undefined
360+
? { timeoutMs: event.payload.timeoutMs }
361+
: {}),
362+
},
363+
turnId: toTurnId(event.turnId) ?? null,
364+
...maybeSequence,
365+
},
366+
];
367+
}
368+
369+
case "runtime.thinking-tokens": {
370+
// Stable per-turn id so successive token updates replace the same work-log
371+
// row in place (the projector upserts activities by id) instead of
372+
// appending a new row per delta.
373+
const turnKey = toTurnId(event.turnId) ?? null;
374+
const stableId = EventId.make(`thinking-tokens:${turnKey ?? event.threadId}`);
375+
return [
376+
{
377+
id: stableId,
378+
createdAt: event.createdAt,
379+
tone: "info",
380+
kind: "thinking.tokens",
381+
summary: "Thinking",
382+
payload: {
383+
estimatedTokens: event.payload.estimatedTokens,
384+
estimatedTokensDelta: event.payload.estimatedTokensDelta,
385+
},
386+
turnId: turnKey,
387+
...maybeSequence,
388+
},
389+
];
390+
}
391+
347392
case "runtime.warning": {
348393
return [
349394
{
@@ -598,6 +643,30 @@ function runtimeEventToActivities(
598643
];
599644
}
600645

646+
case "account.usage.updated": {
647+
// NOTE: account.rate-limits.updated stays dropped (no consumer); fold it
648+
// in here if a future rate-limit-detail refactor needs it.
649+
//
650+
// Stable per-thread id so each 60s poll snapshot upserts the same row
651+
// (the projector replaces activities by id) instead of appending a new
652+
// one every tick — which would grow the projection unbounded for the
653+
// session's life. Only the latest snapshot is ever read
654+
// (deriveLatestUsageSnapshot), so one in-place row is sufficient.
655+
const stableId = EventId.make(`account-usage:${event.threadId}`);
656+
return [
657+
{
658+
id: stableId,
659+
createdAt: event.createdAt,
660+
tone: "info",
661+
kind: "account.usage.updated",
662+
summary: "Account usage updated",
663+
payload: event.payload,
664+
turnId: toTurnId(event.turnId) ?? null,
665+
...maybeSequence,
666+
},
667+
];
668+
}
669+
601670
default:
602671
break;
603672
}

0 commit comments

Comments
 (0)