GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge omp/20 (#20): tokenUsage mapping + compaction projection · dioskuroi/t3code@3561f39

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/provider-omp/src/server
    • adapter.test.ts
    • adapter.ts
    • projection.ts
    • status.test.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/provider-omp/src/server/adapter.test.ts‎

Copy file name to clipboardExpand all lines: packages/provider-omp/src/server/adapter.test.ts
+341-1Lines changed: 341 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,6 @@ describe("omp adapter session runtime", () => {
802802
delayMs: 20,
803803
frame: { type: "subagent_lifecycle", subagentId: "sub-1", status: "running" },
804804
},
805-
{ delayMs: 20, frame: { type: "auto_compaction_start" } },
806805
{ delayMs: 20, frame: { type: "notice", message: "hi" } },
807806
{ delayMs: 20, frame: { type: "agent_end", isTerminal: true } },
808807
{
@@ -818,6 +817,347 @@ describe("omp adapter session runtime", () => {
818817
}).pipe(Effect.provide(NodeServices.layer)),
819818
);
820819

820+
live("carries turn tokenUsage from usage frames and refreshes the meter at terminal (D9)", () =>
821+
Effect.gen(function* () {
822+
const { harness } = yield* startDrivenTurn(
823+
{
824+
responses: {
825+
open_session: {
826+
data: { sessionId: "s-usage", sessionFile: "/tmp/fake-usage.jsonl" },
827+
},
828+
get_state: {
829+
data: {
830+
isSettled: true,
831+
contextUsage: { tokens: 140, contextWindow: 2048, percent: 6.8 },
832+
},
833+
},
834+
},
835+
events: [
836+
messageUpdate(20, { type: "text_start", contentIndex: 0 }),
837+
messageUpdate(20, { type: "text_delta", delta: "answer", contentIndex: 0 }),
838+
messageUpdate(20, { type: "text_end", contentIndex: 0 }),
839+
{
840+
delayMs: 20,
841+
frame: {
842+
type: "message_end",
843+
message: {
844+
role: "assistant",
845+
usage: {
846+
input: 100,
847+
output: 20,
848+
cacheRead: 10,
849+
cacheWrite: 0,
850+
totalTokens: 130,
851+
contextTokens: 150,
852+
reasoningTokens: 8,
853+
},
854+
},
855+
},
856+
},
857+
{ delayMs: 20, frame: { type: "agent_end", isTerminal: true } },
858+
{
859+
delayMs: 20,
860+
frame: { type: "prompt_result", agentInvoked: true, status: "completed" },
861+
},
862+
],
863+
},
864+
"usage",
865+
);
866+
yield* waitFor(harness, "turn.terminal");
867+
const turns = eventsOf(harness, "provider_turn.updated").map((event) => event.providerTurn);
868+
// Live usage from message_end: omp Usage → turn tokenUsage (contextTokens drives usedTokens).
869+
const liveUsage = turns[1]?.tokenUsage;
870+
expect(liveUsage).toEqual({
871+
usedTokens: 150,
872+
inputTokens: 100,
873+
cachedInputTokens: 10,
874+
outputTokens: 20,
875+
reasoningOutputTokens: 8,
876+
updatedAt: expect.any(String),
877+
});
878+
// Terminal get_state reading wins on usedTokens/maxTokens and lands on the final turn.
879+
expect(turns.at(-1)?.status).toBe("completed");
880+
expect(turns.at(-1)?.tokenUsage).toEqual({
881+
usedTokens: 140,
882+
maxTokens: 2048,
883+
inputTokens: 100,
884+
cachedInputTokens: 10,
885+
outputTokens: 20,
886+
reasoningOutputTokens: 8,
887+
updatedAt: expect.any(String),
888+
});
889+
// Meter: the thread contextUsage snapshot mirrors the get_state reading with the
890+
// truth-table bit for native auto-compaction set (spec §7).
891+
const thread = eventsOf(harness, "provider_thread.updated").at(-1)?.providerThread;
892+
expect(thread?.contextUsage).toEqual({
893+
usedTokens: 140,
894+
maxTokens: 2048,
895+
compactsAutomatically: true,
896+
});
897+
}).pipe(Effect.provide(NodeServices.layer)),
898+
);
899+
900+
live(
901+
"projects compaction frames into hint lines and meter updates without rewriting the transcript (D9)",
902+
() =>
903+
Effect.gen(function* () {
904+
const { harness } = yield* startDrivenTurn(
905+
{
906+
responses: {
907+
open_session: {
908+
data: { sessionId: "s-compact", sessionFile: "/tmp/fake-compact.jsonl" },
909+
},
910+
get_state: {
911+
data: {
912+
isSettled: true,
913+
contextUsage: { tokens: 120, contextWindow: 1000, percent: 12 },
914+
},
915+
},
916+
},
917+
events: [
918+
{
919+
delayMs: 20,
920+
frame: {
921+
type: "available_commands_update",
922+
commands: [
923+
{
924+
name: "compact",
925+
description: "Summarize the conversation so far",
926+
input: { hint: "Optional focus" },
927+
},
928+
{ name: "plan", description: "Toggle plan mode" },
929+
],
930+
},
931+
},
932+
messageUpdate(20, { type: "text_start", contentIndex: 0 }),
933+
messageUpdate(20, { type: "text_delta", delta: "pre-compaction", contentIndex: 0 }),
934+
messageUpdate(20, { type: "text_end", contentIndex: 0 }),
935+
{
936+
delayMs: 20,
937+
frame: {
938+
type: "message_end",
939+
message: {
940+
role: "assistant",
941+
usage: { input: 90, output: 15, cacheRead: 5, cacheWrite: 0, totalTokens: 110 },
942+
},
943+
},
944+
},
945+
// Auto compaction: full lifecycle with a committed result.
946+
{
947+
delayMs: 20,
948+
frame: {
949+
type: "auto_compaction_start",
950+
reason: "threshold",
951+
action: "context-full",
952+
},
953+
},
954+
{
955+
delayMs: 20,
956+
frame: {
957+
type: "auto_compaction_end",
958+
action: "context-full",
959+
result: { summary: "Old context summarized", tokensBefore: 900 },
960+
aborted: false,
961+
willRetry: false,
962+
},
963+
},
964+
// Manual compaction: the /compact RPC path, same projection.
965+
{ delayMs: 20, frame: { type: "manual_compaction_start", mode: "accurate" } },
966+
{
967+
delayMs: 20,
968+
frame: {
969+
type: "manual_compaction_end",
970+
result: { summary: "Manual summary", tokensBefore: 300 },
971+
aborted: false,
972+
},
973+
},
974+
{ delayMs: 20, frame: { type: "agent_end", isTerminal: true } },
975+
{
976+
delayMs: 20,
977+
frame: { type: "prompt_result", agentInvoked: true, status: "completed" },
978+
},
979+
],
980+
},
981+
"compact",
982+
);
983+
yield* waitFor(harness, "turn.terminal");
984+
const items = eventsOf(harness, "turn_item.updated").map((event) => event.turnItem);
985+
const compactions = items.filter((item) => item.type === "compaction");
986+
// Hint lines: running → completed per compaction, contract shape with omp details.
987+
expect(
988+
compactions.map((item) => (item.type === "compaction" ? item.status : null)),
989+
).toEqual(["running", "completed", "running", "completed"]);
990+
expect(compactions[0]?.title).toBe("Compacting context...");
991+
const firstDone = compactions[1];
992+
expect(firstDone?.type === "compaction" ? firstDone.title : null).toBe("Context compacted");
993+
expect(firstDone?.type === "compaction" ? firstDone.summary : null).toBe(
994+
"Old context summarized",
995+
);
996+
expect(firstDone?.type === "compaction" ? firstDone.beforeTokenCount : null).toBe(900);
997+
expect(firstDone?.type === "compaction" ? firstDone.driver : null).toBe(OMP_PROVIDER);
998+
const secondDone = compactions[3];
999+
expect(secondDone?.type === "compaction" ? secondDone.summary : null).toBe(
1000+
"Manual summary",
1001+
);
1002+
expect(secondDone?.type === "compaction" ? secondDone.beforeTokenCount : null).toBe(300);
1003+
// Hint lines own system nodes that never count for the run.
1004+
const systemNodes = eventsOf(harness, "node.updated").filter(
1005+
(event) => event.node.kind === "system",
1006+
);
1007+
expect(systemNodes.map((event) => event.node.status)).toEqual([
1008+
"running",
1009+
"completed",
1010+
"running",
1011+
"completed",
1012+
]);
1013+
expect(systemNodes.every((event) => event.node.countsForRun === false)).toBe(true);
1014+
// Transcript invariant: after the first compaction hint line no assistant message is
1015+
// re-emitted, and the final message history is byte-identical (append-only store).
1016+
const firstCompactionIndex = harness.events.findIndex(
1017+
(event) => event.type === "turn_item.updated" && event.turnItem.type === "compaction",
1018+
);
1019+
expect(
1020+
harness.events
1021+
.slice(firstCompactionIndex)
1022+
.filter(
1023+
(event) =>
1024+
event.type === "turn_item.updated" && event.turnItem.type === "assistant_message",
1025+
),
1026+
).toHaveLength(0);
1027+
const finalMessageTexts = (events: Harness["events"]) =>
1028+
events.flatMap((event) =>
1029+
event.type === "message.updated" && !event.message.streaming
1030+
? [event.message.text]
1031+
: [],
1032+
);
1033+
expect(finalMessageTexts(harness.events)).toEqual(
1034+
finalMessageTexts(harness.events.slice(0, firstCompactionIndex)),
1035+
);
1036+
expect(finalMessageTexts(harness.events)).toEqual(["pre-compaction"]);
1037+
// Meter: refreshed from get_state after compaction and again at terminal; the snapshot
1038+
// carries the compactsAutomatically truth-table bit.
1039+
const thread = eventsOf(harness, "provider_thread.updated").at(-1)?.providerThread;
1040+
expect(thread?.contextUsage).toEqual({
1041+
usedTokens: 120,
1042+
maxTokens: 1000,
1043+
compactsAutomatically: true,
1044+
});
1045+
// Terminal tokenUsage merges the live usage with the fresh get_state reading.
1046+
const turns = eventsOf(harness, "provider_turn.updated").map((event) => event.providerTurn);
1047+
expect(turns.at(-1)?.tokenUsage).toEqual({
1048+
usedTokens: 120,
1049+
maxTokens: 1000,
1050+
inputTokens: 90,
1051+
cachedInputTokens: 5,
1052+
outputTokens: 15,
1053+
updatedAt: expect.any(String),
1054+
});
1055+
// §7: the declared slash-command directory carries the compact entry (passthrough, #19).
1056+
const directory = eventsOf(harness, "provider_slash_commands.updated").at(-1);
1057+
expect(directory?.slashCommands).toContainEqual({
1058+
name: "compact",
1059+
description: "Summarize the conversation so far",
1060+
input: { hint: "Optional focus" },
1061+
});
1062+
}).pipe(Effect.provide(NodeServices.layer)),
1063+
);
1064+
1065+
live(
1066+
"projects aborted, skipped, and failed compaction outcomes without inventing meter readings",
1067+
() =>
1068+
Effect.gen(function* () {
1069+
const { harness } = yield* startDrivenTurn(
1070+
{
1071+
responses: {
1072+
open_session: {
1073+
data: {
1074+
sessionId: "s-compact-outcome",
1075+
sessionFile: "/tmp/fake-compact-outcome.jsonl",
1076+
},
1077+
},
1078+
// No contextUsage in get_state: no meter reading may be fabricated.
1079+
get_state: { data: { isSettled: true } },
1080+
},
1081+
events: [
1082+
{ delayMs: 20, frame: { type: "manual_compaction_start", mode: "fast" } },
1083+
{ delayMs: 20, frame: { type: "manual_compaction_end", aborted: true } },
1084+
{ delayMs: 20, frame: { type: "manual_compaction_start" } },
1085+
{
1086+
delayMs: 20,
1087+
frame: {
1088+
type: "manual_compaction_end",
1089+
aborted: false,
1090+
skipped: true,
1091+
errorMessage: "Nothing to compact yet",
1092+
},
1093+
},
1094+
{
1095+
delayMs: 20,
1096+
frame: { type: "auto_compaction_start", reason: "overflow", action: "remote" },
1097+
},
1098+
{
1099+
delayMs: 20,
1100+
frame: {
1101+
type: "auto_compaction_end",
1102+
action: "remote",
1103+
result: undefined,
1104+
aborted: false,
1105+
willRetry: true,
1106+
errorMessage: "remote compaction unavailable",
1107+
},
1108+
},
1109+
// Left open on purpose: the turn terminal must close it as cancelled.
1110+
{
1111+
delayMs: 20,
1112+
frame: {
1113+
type: "auto_compaction_start",
1114+
reason: "threshold",
1115+
action: "context-full",
1116+
},
1117+
},
1118+
{ delayMs: 20, frame: { type: "agent_end", isTerminal: true } },
1119+
{
1120+
delayMs: 20,
1121+
frame: { type: "prompt_result", agentInvoked: true, status: "completed" },
1122+
},
1123+
],
1124+
},
1125+
"compact-outcome",
1126+
);
1127+
yield* waitFor(harness, "turn.terminal");
1128+
const compactions = eventsOf(harness, "turn_item.updated")
1129+
.map((event) => event.turnItem)
1130+
.filter((item) => item.type === "compaction");
1131+
expect(
1132+
compactions.map((item) => (item.type === "compaction" ? item.status : null)),
1133+
).toEqual([
1134+
"running",
1135+
"cancelled",
1136+
"running",
1137+
"completed",
1138+
"running",
1139+
"failed",
1140+
"running",
1141+
"cancelled",
1142+
]);
1143+
expect(compactions[1]?.title).toBe("Context compaction stopped");
1144+
expect(compactions[2]?.title).toBe("Compacting context...");
1145+
const skipped = compactions[3];
1146+
expect(skipped?.type === "compaction" ? skipped.summary : null).toBe(
1147+
"Nothing to compact yet",
1148+
);
1149+
expect(compactions[5]?.title).toBe("Context compaction failed");
1150+
expect(compactions[5]?.type === "compaction" ? compactions[5].summary : null).toBe(
1151+
"remote compaction unavailable",
1152+
);
1153+
// No contextUsage anywhere in the frames: the meter stays null, never fabricated.
1154+
const thread = eventsOf(harness, "provider_thread.updated").at(-1)?.providerThread;
1155+
expect(thread?.contextUsage ?? null).toBeNull();
1156+
const turns = eventsOf(harness, "provider_turn.updated").map((event) => event.providerTurn);
1157+
expect(turns.at(-1)?.tokenUsage).toBeUndefined();
1158+
}).pipe(Effect.provide(NodeServices.layer)),
1159+
);
1160+
8211161
live("exposes the capability truth table and one shared capabilities object", () =>
8221162
Effect.gen(function* () {
8231163
const ids = makeIds("cap");
Collapse file

‎packages/provider-omp/src/server/adapter.ts‎

Copy file name to clipboardExpand all lines: packages/provider-omp/src/server/adapter.ts
+7-3Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,6 @@ export const makeOmpAdapterV2 = Effect.fn("makeOmpAdapterV2")(function* (input:
359359
runtimeRequests: new Map<string, OmpPendingRuntimeRequest>(),
360360
appliedThinking: null,
361361
};
362-
// Late-bound: the projection outlives this closure's construction and the thread state
363-
// is assembled right below.
364-
let threadState: OmpThreadState | undefined;
365362
const projection = createOmpThreadProjection({
366363
driver: OMP_PROVIDER,
367364
instanceId: input.instanceId,
@@ -384,6 +381,13 @@ export const makeOmpAdapterV2 = Effect.fn("makeOmpAdapterV2")(function* (input:
384381
const self = threadState;
385382
return self === undefined ? Effect.void : applyQueuedApprovalMode(self);
386383
},
384+
// Transport only: meter reads (D9) cap at 2s and swallow failures inside the
385+
// projection — a dead process must never fail a turn finalization.
386+
readSessionState: Effect.suspend(() => {
387+
const self = threadState;
388+
const rpc = self === undefined ? null : self.client.client;
389+
return rpc === null ? Effect.void : rpc.request("get_state");
390+
}),
387391
});
388392
const thread: OmpThreadState = {
389393
projection,

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.