1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
import { describe, it, expect, afterEach } from "bun:test";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import {
invocationFloorCents,
extractUsageFromStdout,
LOOP_EXEC_TIMEOUT_MS,
} from "../lib/hosted-claude-loop";
const ENV_MIN = "HOSTED_LOOP_MIN_CENTS_PER_RUN";
const ENV_RATE = "HOSTED_LOOP_MS_PER_CENT";
afterEach(() => {
delete process.env[ENV_MIN];
delete process.env[ENV_RATE];
});
describe("invocationFloorCents", () => {
it("charges at least one cent even for an instant run", () => {
expect(invocationFloorCents(0)).toBe(1);
expect(invocationFloorCents(1)).toBe(1);
});
it("scales with measured wall clock", () => {
expect(invocationFloorCents(10_000)).toBe(1);
expect(invocationFloorCents(10_001)).toBe(2);
expect(invocationFloorCents(25_000)).toBe(3);
});
it("bounds a maximum-length run sensibly", () => {
const maxFloor = invocationFloorCents(LOOP_EXEC_TIMEOUT_MS);
expect(maxFloor).toBeGreaterThan(0);
expect(maxFloor).toBeLessThanOrEqual(5);
});
it("never returns a negative or non-finite charge", () => {
for (const bad of [-1, -100_000, NaN, Infinity, -Infinity]) {
const got = invocationFloorCents(bad as number);
expect(Number.isFinite(got)).toBe(true);
expect(got).toBeGreaterThanOrEqual(1);
}
});
it("honours the env knobs at access time", () => {
process.env[ENV_MIN] = "5";
process.env[ENV_RATE] = "1000";
expect(invocationFloorCents(0)).toBe(5);
expect(invocationFloorCents(9_000)).toBe(9);
});
it("falls back to safe defaults when the env is nonsense", () => {
process.env[ENV_MIN] = "not-a-number";
process.env[ENV_RATE] = "0";
expect(invocationFloorCents(0)).toBe(1);
expect(invocationFloorCents(10_000)).toBe(1);
});
it("allows the floor to be disabled deliberately", () => {
process.env[ENV_MIN] = "0";
expect(invocationFloorCents(0)).toBe(0);
});
});
describe("extractUsageFromStdout is the untrusted input", () => {
it("reports nothing for a snippet that prints nothing", () => {
expect(extractUsageFromStdout("")).toEqual({
inputTokens: 0,
outputTokens: 0,
model: null,
});
expect(extractUsageFromStdout("hello, no json here")).toEqual({
inputTokens: 0,
outputTokens: 0,
model: null,
});
});
it("still reads a well-formed usage block", () => {
const got = extractUsageFromStdout(
JSON.stringify({ usage: { input_tokens: 100, output_tokens: 50 } })
);
expect(got.inputTokens).toBe(100);
expect(got.outputTokens).toBe(50);
});
});
describe("invokeLoop applies the floor to what it charges", () => {
const src = (() => {
const raw = readFileSync(
join(import.meta.dir, "..", "lib", "hosted-claude-loop.ts"),
"utf8"
);
const stripped = raw
.split("\n")
.filter((l) => !l.trim().startsWith("//") && !l.trim().startsWith("*"))
.join("\n");
expect(stripped.length).toBeGreaterThan(0);
return stripped;
})();
it("takes the max of self-reported cost and the measured floor", () => {
expect(src).toContain("invocationFloorCents(execResult.durationMs)");
expect(src).toContain("Math.max(");
});
it("charges the floor before the run row is written", () => {
const at = src.indexOf("invocationFloorCents(execResult.durationMs)");
expect(at).toBeGreaterThan(-1);
const after = src.slice(at);
expect(after.length).toBeGreaterThan(0);
expect(after).toContain("centsEstimate: cents");
expect(after).toContain("bumpLoopTotals(loop.id, cents)");
});
it("still short-circuits over-budget loops before executing", () => {
const at = src.indexOf("getLoopMonthlySpendCents(loop.id)");
expect(at).toBeGreaterThan(-1);
const window = src.slice(at, at + 400);
expect(window.length).toBeGreaterThan(0);
expect(window).toContain("loop.monthlyBudgetCents");
expect(window).toContain("budget_exceeded");
expect(at).toBeLessThan(src.indexOf("await exec({"));
});
});
|