|
| 1 | +import { describe, expect } from "bun:test" |
| 2 | +import { Effect, Layer } from "effect" |
| 3 | +import { eq } from "drizzle-orm" |
| 4 | +import { Database } from "@opencode-ai/core/database/database" |
| 5 | +import { SessionProjector } from "@opencode-ai/core/session/projector" |
| 6 | +import { SessionTable } from "@opencode-ai/core/session/sql" |
| 7 | +import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" |
| 8 | +import { Session as SessionNs } from "@/session/session" |
| 9 | +import { SubagentLimits } from "@/session/subagent-limits" |
| 10 | +import { Storage } from "@/storage/storage" |
| 11 | +import { RuntimeFlags } from "@/effect/runtime-flags" |
| 12 | +import { BackgroundJob } from "@/background/job" |
| 13 | +import { EventV2Bridge } from "@/event-v2-bridge" |
| 14 | +import type { SessionID } from "@/session/schema" |
| 15 | +import { testInstanceStoreLayer } from "../fixture/fixture" |
| 16 | +import { testEffect } from "../lib/effect" |
| 17 | + |
| 18 | +const it = testEffect( |
| 19 | + Layer.mergeAll( |
| 20 | + SessionNs.layer.pipe( |
| 21 | + Layer.provide(Storage.defaultLayer), |
| 22 | + Layer.provide(Database.defaultLayer), |
| 23 | + Layer.provideMerge(EventV2Bridge.defaultLayer), |
| 24 | + Layer.provide(SessionProjector.defaultLayer), |
| 25 | + Layer.provide(RuntimeFlags.layer({ experimentalWorkspaces: false })), |
| 26 | + Layer.provide(BackgroundJob.defaultLayer), |
| 27 | + ), |
| 28 | + Database.defaultLayer, |
| 29 | + CrossSpawnSpawner.defaultLayer, |
| 30 | + testInstanceStoreLayer, |
| 31 | + ), |
| 32 | +) |
| 33 | + |
| 34 | +/** |
| 35 | + * Seeds the per-session cost/token aggregate directly in the database — the |
| 36 | + * same DB-seam pattern session-lineage.test.ts uses to corrupt parent rows. |
| 37 | + * Mirrors what the projector accumulates from step-finish parts, without |
| 38 | + * having to drive a full prompt loop. |
| 39 | + */ |
| 40 | +const seedUsage = ( |
| 41 | + sessionID: SessionID, |
| 42 | + usage: { cost: number; input?: number; output?: number; reasoning?: number; cacheRead?: number; cacheWrite?: number }, |
| 43 | +) => |
| 44 | + Database.Service.use(({ db }) => |
| 45 | + db |
| 46 | + .update(SessionTable) |
| 47 | + .set({ |
| 48 | + cost: usage.cost, |
| 49 | + tokens_input: usage.input ?? 0, |
| 50 | + tokens_output: usage.output ?? 0, |
| 51 | + tokens_reasoning: usage.reasoning ?? 0, |
| 52 | + tokens_cache_read: usage.cacheRead ?? 0, |
| 53 | + tokens_cache_write: usage.cacheWrite ?? 0, |
| 54 | + }) |
| 55 | + .where(eq(SessionTable.id, sessionID)) |
| 56 | + .run() |
| 57 | + .pipe(Effect.orDie), |
| 58 | + ) |
| 59 | + |
| 60 | +describe("SubagentLimits.aggregateCost", () => { |
| 61 | + it.effect("sums cost and every token bucket across the subtree", () => { |
| 62 | + const rollup = SubagentLimits.aggregateCost([ |
| 63 | + { |
| 64 | + cost: 1.5, |
| 65 | + tokens: { input: 10, output: 20, reasoning: 5, cache: { read: 3, write: 1 } }, |
| 66 | + }, |
| 67 | + { |
| 68 | + cost: 0.25, |
| 69 | + tokens: { input: 4, output: 6, reasoning: 0, cache: { read: 2, write: 7 } }, |
| 70 | + }, |
| 71 | + ]) |
| 72 | + expect(rollup).toEqual({ |
| 73 | + cost: 1.75, |
| 74 | + tokens: { input: 14, output: 26, reasoning: 5, cache: { read: 5, write: 8 } }, |
| 75 | + }) |
| 76 | + return Effect.void |
| 77 | + }) |
| 78 | + |
| 79 | + it.effect("treats missing cost/tokens as zero", () => { |
| 80 | + const rollup = SubagentLimits.aggregateCost([{}, { cost: 2 }, { tokens: undefined }]) |
| 81 | + expect(rollup).toEqual({ |
| 82 | + cost: 2, |
| 83 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 84 | + }) |
| 85 | + return Effect.void |
| 86 | + }) |
| 87 | + |
| 88 | + it.effect("is an empty zero rollup for no nodes", () => { |
| 89 | + expect(SubagentLimits.aggregateCost([])).toEqual({ |
| 90 | + cost: 0, |
| 91 | + tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, |
| 92 | + }) |
| 93 | + return Effect.void |
| 94 | + }) |
| 95 | +}) |
| 96 | + |
| 97 | +describe("Session-tree cost rollup", () => { |
| 98 | + it.instance("rolls a child's own usage plus its 2-level descendant subtree", () => |
| 99 | + Effect.gen(function* () { |
| 100 | + const sessions = yield* SessionNs.Service |
| 101 | + // Tree: root → child → grandchild (a 3-level chain, the subagent task is |
| 102 | + // the `child`, so the rollup covers child + grandchild). |
| 103 | + const root = yield* sessions.create({}) |
| 104 | + const child = yield* sessions.create({ parentID: root.id }) |
| 105 | + const grandchild = yield* sessions.create({ parentID: child.id }) |
| 106 | + |
| 107 | + yield* seedUsage(root.id, { cost: 10, input: 100 }) |
| 108 | + yield* seedUsage(child.id, { cost: 2, input: 20, output: 5, cacheRead: 3 }) |
| 109 | + yield* seedUsage(grandchild.id, { cost: 0.5, input: 7, reasoning: 4, cacheWrite: 9 }) |
| 110 | + |
| 111 | + // The rollup of the task that spawned `child`: the child itself plus its |
| 112 | + // descendants — root (the spawner) is deliberately excluded. |
| 113 | + const subtree = yield* sessions.descendants(child.id) |
| 114 | + const childInfo = yield* sessions.get(child.id) |
| 115 | + const rollup = SubagentLimits.aggregateCost([childInfo, ...subtree]) |
| 116 | + |
| 117 | + expect(rollup.cost).toBeCloseTo(2.5, 10) |
| 118 | + expect(rollup.tokens).toEqual({ |
| 119 | + input: 27, |
| 120 | + output: 5, |
| 121 | + reasoning: 4, |
| 122 | + cache: { read: 3, write: 9 }, |
| 123 | + }) |
| 124 | + }), |
| 125 | + ) |
| 126 | + |
| 127 | + it.instance("rolls a fan-out subtree (root → A,B → A1) without double counting the spawner", () => |
| 128 | + Effect.gen(function* () { |
| 129 | + const sessions = yield* SessionNs.Service |
| 130 | + const root = yield* sessions.create({}) |
| 131 | + const a = yield* sessions.create({ parentID: root.id }) |
| 132 | + const b = yield* sessions.create({ parentID: root.id }) |
| 133 | + const a1 = yield* sessions.create({ parentID: a.id }) |
| 134 | + |
| 135 | + yield* seedUsage(root.id, { cost: 99, input: 999 }) |
| 136 | + yield* seedUsage(a.id, { cost: 1, input: 1 }) |
| 137 | + yield* seedUsage(b.id, { cost: 2, input: 2 }) |
| 138 | + yield* seedUsage(a1.id, { cost: 4, input: 4 }) |
| 139 | + |
| 140 | + // Rollup for the task that spawned the whole subtree from `root`: every |
| 141 | + // descendant, but NOT root's own spend (the parent session still bills |
| 142 | + // its own cost separately — this is display-only, no double counting). |
| 143 | + const subtree = yield* sessions.descendants(root.id) |
| 144 | + const rollup = SubagentLimits.aggregateCost(subtree) |
| 145 | + |
| 146 | + expect(rollup.cost).toBe(7) |
| 147 | + expect(rollup.tokens.input).toBe(7) |
| 148 | + |
| 149 | + // The root session's own per-session accounting is unchanged by the |
| 150 | + // rollup read — the real bill is not touched. |
| 151 | + const rootInfo = yield* sessions.get(root.id) |
| 152 | + expect(rootInfo.cost).toBe(99) |
| 153 | + expect(rootInfo.tokens?.input).toBe(999) |
| 154 | + }), |
| 155 | + ) |
| 156 | +}) |
0 commit comments