@cursor/july 0.1.26 → 0.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. package/dist/ab.d.ts +6 -0
  2. package/dist/ab.d.ts.map +1 -1
  3. package/dist/ab.js +1 -0
  4. package/dist/artifacts.d.ts +34 -0
  5. package/dist/artifacts.d.ts.map +1 -0
  6. package/dist/artifacts.js +57 -0
  7. package/dist/bin/agent-serve.js +31 -9
  8. package/dist/channels/github/github-channel.d.ts +1 -1
  9. package/dist/channels/github/github-channel.d.ts.map +1 -1
  10. package/dist/channels/github/github-channel.js +14 -1
  11. package/dist/channels/github/types.d.ts +6 -1
  12. package/dist/channels/github/types.d.ts.map +1 -1
  13. package/dist/channels/slack/api.d.ts +2 -1
  14. package/dist/channels/slack/api.d.ts.map +1 -1
  15. package/dist/channels/slack/api.js +1 -0
  16. package/dist/channels/slack/cursor-account.d.ts +2 -1
  17. package/dist/channels/slack/cursor-account.d.ts.map +1 -1
  18. package/dist/channels/slack/cursor-account.js +1 -1
  19. package/dist/channels/slack/dispatch.d.ts +4 -1
  20. package/dist/channels/slack/dispatch.d.ts.map +1 -1
  21. package/dist/channels/slack/dispatch.js +3 -3
  22. package/dist/channels/slack/interactive.d.ts +2 -1
  23. package/dist/channels/slack/interactive.d.ts.map +1 -1
  24. package/dist/channels/slack/interactive.js +1 -0
  25. package/dist/channels/slack/slack-channel.d.ts.map +1 -1
  26. package/dist/channels/slack/slack-channel.js +4 -3
  27. package/dist/channels/slack/types.d.ts +6 -1
  28. package/dist/channels/slack/types.d.ts.map +1 -1
  29. package/dist/docs/404.html +1 -1
  30. package/dist/docs/ab.html +2 -2
  31. package/dist/docs/assets/{app.DYcC9FY-.js → app.BICLoJYF.js} +1 -1
  32. package/dist/docs/assets/chunks/@localSearchIndexroot.CQJytDwU.js +1 -0
  33. package/dist/docs/assets/chunks/{VPLocalSearchBox.o4N_knTV.js → VPLocalSearchBox.wN4Ko9dT.js} +1 -1
  34. package/dist/docs/assets/chunks/{theme.DQ-njyo0.js → theme.dnnTE-Co.js} +2 -2
  35. package/dist/docs/building-with-agents.html +2 -2
  36. package/dist/docs/concepts.html +2 -2
  37. package/dist/docs/deployment.html +2 -2
  38. package/dist/docs/evals.html +2 -2
  39. package/dist/docs/example-agents/approval-buddy.html +2 -2
  40. package/dist/docs/example-agents/benny.html +2 -2
  41. package/dist/docs/example-agents/bugbot.html +2 -2
  42. package/dist/docs/example-agents/codebase-wiki.html +2 -2
  43. package/dist/docs/example-agents/codeowners-review.html +2 -2
  44. package/dist/docs/example-agents/concierge.html +2 -2
  45. package/dist/docs/example-agents/fsd.html +2 -2
  46. package/dist/docs/example-agents/index.html +2 -2
  47. package/dist/docs/example-agents/knowledge-base.html +2 -2
  48. package/dist/docs/example-agents/oncall.html +2 -2
  49. package/dist/docs/example-agents/security-reviewer.html +2 -2
  50. package/dist/docs/example-agents/slack-agent.html +2 -2
  51. package/dist/docs/example-agents/weather-agent.html +2 -2
  52. package/dist/docs/guides/agent-to-agent.html +2 -2
  53. package/dist/docs/guides/cloud-runtime.html +2 -2
  54. package/dist/docs/guides/github.html +2 -2
  55. package/dist/docs/guides/human-in-the-loop.html +2 -2
  56. package/dist/docs/guides/mcp-oauth.html +2 -2
  57. package/dist/docs/guides/slack.html +2 -2
  58. package/dist/docs/guides/webhooks.html +2 -2
  59. package/dist/docs/hillclimbing.html +2 -2
  60. package/dist/docs/index.html +2 -2
  61. package/dist/docs/quickstart.html +2 -2
  62. package/dist/docs/reference/agent-config.html +2 -2
  63. package/dist/docs/reference/channels.html +2 -2
  64. package/dist/docs/reference/cli.html +2 -2
  65. package/dist/docs/reference/connections.html +2 -2
  66. package/dist/docs/reference/hooks.html +2 -2
  67. package/dist/docs/reference/http-api.html +2 -2
  68. package/dist/docs/reference/instructions.html +2 -2
  69. package/dist/docs/reference/playground.html +2 -2
  70. package/dist/docs/reference/project-layout.html +2 -2
  71. package/dist/docs/reference/prompt.html +2 -2
  72. package/dist/docs/reference/schedules.html +2 -2
  73. package/dist/docs/reference/sessions.html +2 -2
  74. package/dist/docs/reference/skills.html +2 -2
  75. package/dist/docs/reference/subagents.html +2 -2
  76. package/dist/docs/reference/tools.html +2 -2
  77. package/dist/docs/scaffolding-agents.html +2 -2
  78. package/dist/docs/storage.html +2 -2
  79. package/dist/docs/troubleshooting.html +2 -2
  80. package/dist/evals/assertions.d.ts +10 -1
  81. package/dist/evals/assertions.d.ts.map +1 -1
  82. package/dist/evals/assertions.js +12 -0
  83. package/dist/evals.d.ts +1 -1
  84. package/dist/evals.d.ts.map +1 -1
  85. package/dist/index.d.ts +1 -0
  86. package/dist/index.d.ts.map +1 -1
  87. package/dist/index.js +1 -0
  88. package/dist/internal/ab-fold.d.ts.map +1 -1
  89. package/dist/internal/ab-fold.js +4 -0
  90. package/dist/internal/ab-snapshot.d.ts.map +1 -1
  91. package/dist/internal/ab-snapshot.js +1 -0
  92. package/dist/internal/artifacts-store.d.ts +45 -0
  93. package/dist/internal/artifacts-store.d.ts.map +1 -0
  94. package/dist/internal/artifacts-store.js +250 -0
  95. package/dist/internal/builtin-tools/artifacts.d.ts +10 -0
  96. package/dist/internal/builtin-tools/artifacts.d.ts.map +1 -0
  97. package/dist/internal/builtin-tools/artifacts.js +73 -0
  98. package/dist/internal/builtin-tools/index.d.ts +8 -1
  99. package/dist/internal/builtin-tools/index.d.ts.map +1 -1
  100. package/dist/internal/builtin-tools/index.js +15 -0
  101. package/dist/internal/cli-ax.d.ts +11 -0
  102. package/dist/internal/cli-ax.d.ts.map +1 -1
  103. package/dist/internal/cli-ax.js +80 -0
  104. package/dist/internal/cli-slack.d.ts.map +1 -1
  105. package/dist/internal/cli-slack.js +14 -6
  106. package/dist/internal/discovery.d.ts.map +1 -1
  107. package/dist/internal/discovery.js +83 -2
  108. package/dist/internal/distribution.d.ts.map +1 -1
  109. package/dist/internal/distribution.js +1 -0
  110. package/dist/internal/model-pricing.d.ts +49 -0
  111. package/dist/internal/model-pricing.d.ts.map +1 -0
  112. package/dist/internal/model-pricing.js +377 -0
  113. package/dist/internal/reminder-runner.d.ts.map +1 -1
  114. package/dist/internal/reminder-runner.js +1 -0
  115. package/dist/internal/schedule-runner.d.ts.map +1 -1
  116. package/dist/internal/schedule-runner.js +1 -0
  117. package/dist/internal/sdk-runner.d.ts.map +1 -1
  118. package/dist/internal/sdk-runner.js +9 -1
  119. package/dist/internal/server.d.ts.map +1 -1
  120. package/dist/internal/server.js +39 -0
  121. package/dist/internal/session-cost.d.ts +49 -0
  122. package/dist/internal/session-cost.d.ts.map +1 -0
  123. package/dist/internal/session-cost.js +175 -0
  124. package/dist/internal/session-engine.d.ts +35 -1
  125. package/dist/internal/session-engine.d.ts.map +1 -1
  126. package/dist/internal/session-engine.js +128 -7
  127. package/dist/internal/storage-coordinator.d.ts +15 -0
  128. package/dist/internal/storage-coordinator.d.ts.map +1 -1
  129. package/dist/internal/storage-coordinator.js +52 -0
  130. package/dist/internal/trajectory.d.ts +9 -1
  131. package/dist/internal/trajectory.d.ts.map +1 -1
  132. package/dist/internal/trajectory.js +32 -5
  133. package/dist/playground/assets/index-BDCeW4uc.js +86 -0
  134. package/dist/playground/assets/index-Bul1WfrB.css +1 -0
  135. package/dist/playground/index.html +2 -2
  136. package/dist/storage.d.ts +4 -0
  137. package/dist/storage.d.ts.map +1 -1
  138. package/dist/storage.js +4 -0
  139. package/dist/types.d.ts +164 -1
  140. package/dist/types.d.ts.map +1 -1
  141. package/package.json +26 -26
  142. package/src/ab.ts +7 -0
  143. package/src/bin/agent-serve.ts +30 -2
  144. package/src/internal/ab-fold.ts +4 -0
  145. package/src/internal/ab-snapshot.ts +1 -0
  146. package/src/internal/cli-ax.ts +95 -0
  147. package/src/internal/model-pricing.ts +426 -0
  148. package/src/internal/sdk-runner.ts +10 -2
  149. package/src/internal/session-cost.ts +231 -0
  150. package/src/internal/session-engine.ts +28 -0
  151. package/src/internal/trajectory.ts +45 -3
  152. package/src/types.ts +24 -1
  153. package/dist/channels/github/instrument.d.ts +0 -20
  154. package/dist/channels/github/instrument.d.ts.map +0 -1
  155. package/dist/docs/assets/chunks/@localSearchIndexroot.BQTzJjR_.js +0 -1
  156. package/dist/internal/json-dir-store.d.ts +0 -32
  157. package/dist/internal/json-dir-store.d.ts.map +0 -1
  158. package/dist/internal/json-dir-store.js +0 -100
  159. package/dist/playground/assets/index-CjOQ4hN9.css +0 -1
  160. package/dist/playground/assets/index-DqXdAFGa.js +0 -85
  161. package/src/bin/agent-serve.version.test.ts +0 -64
  162. package/src/channels/github/api.test.ts +0 -64
  163. package/src/channels/github/auth.test.ts +0 -105
  164. package/src/channels/github/cursor-account.test.ts +0 -204
  165. package/src/channels/github/forward.test.ts +0 -457
  166. package/src/channels/github/github.test.ts +0 -985
  167. package/src/channels/github/replay.test.ts +0 -179
  168. package/src/channels/slack/api.post-message.test.ts +0 -148
  169. package/src/channels/slack/approvals.test.ts +0 -328
  170. package/src/channels/slack/block-actions.test.ts +0 -469
  171. package/src/channels/slack/bot-mentions.test.ts +0 -267
  172. package/src/channels/slack/channel-watch.test.ts +0 -363
  173. package/src/channels/slack/cursor-account.test.ts +0 -301
  174. package/src/channels/slack/defaults.final-post.test.ts +0 -182
  175. package/src/channels/slack/dispatch.test.ts +0 -875
  176. package/src/channels/slack/eval-directive.test.ts +0 -273
  177. package/src/channels/slack/message-body.test.ts +0 -54
  178. package/src/channels/slack/nudge-store.test.ts +0 -143
  179. package/src/channels/slack/slack.test.ts +0 -391
  180. package/src/channels/slack/stop.test.ts +0 -23
  181. package/src/channels/slack/thread-context.test.ts +0 -202
  182. package/src/evals/assertions.test.ts +0 -617
  183. package/src/evals/expect.test.ts +0 -144
  184. package/src/evals/judge.test.ts +0 -181
  185. package/src/evals/loaders.test.ts +0 -132
  186. package/src/evals/matchers.test.ts +0 -95
  187. package/src/evals/reporters.test.ts +0 -303
  188. package/src/evals/run-facts.test.ts +0 -259
  189. package/src/internal/ab-snapshot.test.ts +0 -325
  190. package/src/internal/approval-gate.test.ts +0 -49
  191. package/src/internal/approvals.integration.test.ts +0 -383
  192. package/src/internal/artifacts-store.test.ts +0 -283
  193. package/src/internal/authored-loaders.test.ts +0 -31
  194. package/src/internal/builtin-tools/artifacts.test.ts +0 -247
  195. package/src/internal/builtin-tools/reminders.test.ts +0 -201
  196. package/src/internal/channel-route-schema.test.ts +0 -294
  197. package/src/internal/chat-attach.test.ts +0 -262
  198. package/src/internal/cli-deploy.test.ts +0 -1991
  199. package/src/internal/cli-docs.test.ts +0 -161
  200. package/src/internal/cli-mcp.test.ts +0 -789
  201. package/src/internal/cli-skills.test.ts +0 -133
  202. package/src/internal/cli-slack.test.ts +0 -1654
  203. package/src/internal/cloud-merge.test.ts +0 -74
  204. package/src/internal/cron.test.ts +0 -22
  205. package/src/internal/cursor/account-mcp.test.ts +0 -807
  206. package/src/internal/cursor/backend-client.test.ts +0 -591
  207. package/src/internal/cursor/credentials.test.ts +0 -351
  208. package/src/internal/cursor/github-credentials.test.ts +0 -136
  209. package/src/internal/cursor-account-mcp-auth.test.ts +0 -310
  210. package/src/internal/cursor-account.integration.test.ts +0 -441
  211. package/src/internal/cursor-event-relay.test.ts +0 -746
  212. package/src/internal/cursor-github-credentials.integration.test.ts +0 -271
  213. package/src/internal/cursor-slack-relay.test.ts +0 -525
  214. package/src/internal/deploy-source.test.ts +0 -111
  215. package/src/internal/discovery.artifact-tool.test.ts +0 -136
  216. package/src/internal/discovery.artifacts.test.ts +0 -119
  217. package/src/internal/discovery.builtin-tools.test.ts +0 -94
  218. package/src/internal/discovery.concurrency.test.ts +0 -60
  219. package/src/internal/discovery.cursor-account.test.ts +0 -133
  220. package/src/internal/discovery.cwd.test.ts +0 -83
  221. package/src/internal/discovery.hosting.test.ts +0 -80
  222. package/src/internal/discovery.identity.test.ts +0 -44
  223. package/src/internal/docs-site.test.ts +0 -66
  224. package/src/internal/duration.test.ts +0 -29
  225. package/src/internal/eval-judge-model.test.ts +0 -187
  226. package/src/internal/eval-run-store.cancel.test.ts +0 -142
  227. package/src/internal/eval-run-store.storage.test.ts +0 -211
  228. package/src/internal/eval-runner.http.test.ts +0 -403
  229. package/src/internal/eval-runner.run.test.ts +0 -928
  230. package/src/internal/evals-client.test.ts +0 -307
  231. package/src/internal/event-mapper.test.ts +0 -243
  232. package/src/internal/github-fanout.test.ts +0 -213
  233. package/src/internal/handleAgentServeTrigger.test.ts +0 -185
  234. package/src/internal/host-kv.test.ts +0 -82
  235. package/src/internal/host-platforms.test.ts +0 -126
  236. package/src/internal/http-channel.test.ts +0 -402
  237. package/src/internal/init-project.test.ts +0 -270
  238. package/src/internal/install-cursor-skills.test.ts +0 -262
  239. package/src/internal/local-env.test.ts +0 -120
  240. package/src/internal/log-ring.test.ts +0 -31
  241. package/src/internal/logs-client.test.ts +0 -350
  242. package/src/internal/mcp-endpoint.test.ts +0 -436
  243. package/src/internal/mcp-host.test.ts +0 -298
  244. package/src/internal/mcp-oauth.test.ts +0 -148
  245. package/src/internal/net.test.ts +0 -17
  246. package/src/internal/peer-connections.test.ts +0 -128
  247. package/src/internal/peer-mcp.integration.test.ts +0 -289
  248. package/src/internal/playground/toolchain.test.ts +0 -53
  249. package/src/internal/playground-cli.test.ts +0 -187
  250. package/src/internal/playground-proxy.test.ts +0 -376
  251. package/src/internal/prompt-context.integration.test.ts +0 -232
  252. package/src/internal/prompt-context.test.ts +0 -127
  253. package/src/internal/reminder-runner.test.ts +0 -432
  254. package/src/internal/reminder-store.test.ts +0 -53
  255. package/src/internal/request-headers.test.ts +0 -27
  256. package/src/internal/resolve-prod-target.test.ts +0 -787
  257. package/src/internal/resolved-connections.test.ts +0 -295
  258. package/src/internal/router.test.ts +0 -57
  259. package/src/internal/sdk-runner.test.ts +0 -290
  260. package/src/internal/server.artifacts.test.ts +0 -334
  261. package/src/internal/session-engine.artifacts.test.ts +0 -293
  262. package/src/internal/session-engine.coalesce.test.ts +0 -169
  263. package/src/internal/session-engine.concurrency.test.ts +0 -250
  264. package/src/internal/session-engine.host-oauth-mcp.test.ts +0 -110
  265. package/src/internal/session-engine.interrupt.test.ts +0 -577
  266. package/src/internal/session-engine.storage.test.ts +0 -547
  267. package/src/internal/session-urls.test.ts +0 -28
  268. package/src/internal/sessions-client.test.ts +0 -518
  269. package/src/internal/storage-coordinator.test.ts +0 -517
  270. package/src/internal/tool-call.test.ts +0 -458
  271. package/src/internal/tool-result.test.ts +0 -52
  272. package/src/internal/trajectory.approvals.test.ts +0 -83
  273. package/src/internal/trajectory.subagents.test.ts +0 -198
  274. package/src/internal/turn-governor.test.ts +0 -137
  275. package/src/internal/update-check.test.ts +0 -485
  276. package/src/internal/workspace.test.ts +0 -207
  277. package/src/storage-backends/cursor-hosted.test.ts +0 -121
@@ -0,0 +1,231 @@
1
+ /**
2
+ * Per-session cost metrics folded from a durable session event stream.
3
+ *
4
+ * Powers `agent-serve cost [sessionId]`: each `turn.completed` event carries
5
+ * the turn's token usage and the estimated dollar cost recorded by the
6
+ * engine (see `model-pricing.ts`), so a session's spend is a pure fold over
7
+ * its `events.ndjson` — no server-side aggregation required.
8
+ */
9
+
10
+ import type { SessionEvent, TurnCost, TurnUsage } from "../types.js";
11
+ import { formatCostUsd, roundCostUsd } from "./trajectory.js";
12
+
13
+ /** One agent loop pass (turn) with its usage and estimated cost. */
14
+ export interface SessionCostTurn {
15
+ turnId: string;
16
+ failed: boolean;
17
+ usage?: TurnUsage;
18
+ cost?: TurnCost;
19
+ }
20
+
21
+ export interface SessionCostTotals {
22
+ turns: number;
23
+ turnFailures: number;
24
+ inputTokens: number;
25
+ outputTokens: number;
26
+ cacheReadTokens: number;
27
+ cacheWriteTokens: number;
28
+ /** Sum of per-turn estimated costs (USD). */
29
+ costUsd: number;
30
+ /** Turns that reported usage but had no known model rates. */
31
+ unpricedTurns: number;
32
+ }
33
+
34
+ export interface SessionCostReport {
35
+ sessionId: string;
36
+ turns: SessionCostTurn[];
37
+ totals: SessionCostTotals;
38
+ }
39
+
40
+ export function emptySessionCostTotals(): SessionCostTotals {
41
+ return {
42
+ turns: 0,
43
+ turnFailures: 0,
44
+ inputTokens: 0,
45
+ outputTokens: 0,
46
+ cacheReadTokens: 0,
47
+ cacheWriteTokens: 0,
48
+ costUsd: 0,
49
+ unpricedTurns: 0,
50
+ };
51
+ }
52
+
53
+ export function addSessionCostTotals(
54
+ target: SessionCostTotals,
55
+ delta: SessionCostTotals
56
+ ): void {
57
+ target.turns += delta.turns;
58
+ target.turnFailures += delta.turnFailures;
59
+ target.inputTokens += delta.inputTokens;
60
+ target.outputTokens += delta.outputTokens;
61
+ target.cacheReadTokens += delta.cacheReadTokens;
62
+ target.cacheWriteTokens += delta.cacheWriteTokens;
63
+ target.costUsd = roundCostUsd(target.costUsd + delta.costUsd);
64
+ target.unpricedTurns += delta.unpricedTurns;
65
+ }
66
+
67
+ /** Fold a session event stream into per-turn and total cost metrics. */
68
+ export function buildSessionCostReport(
69
+ events: readonly SessionEvent[]
70
+ ): SessionCostReport {
71
+ let sessionId = "";
72
+ const turns: SessionCostTurn[] = [];
73
+ const totals = emptySessionCostTotals();
74
+
75
+ for (const event of events) {
76
+ if (sessionId === "" && typeof event.sessionId === "string") {
77
+ sessionId = event.sessionId;
78
+ }
79
+ if (event.type !== "turn.completed" && event.type !== "turn.failed") {
80
+ continue;
81
+ }
82
+ const turn: SessionCostTurn = {
83
+ turnId: event.turnId ?? `turn_${turns.length + 1}`,
84
+ failed: event.type === "turn.failed",
85
+ };
86
+ totals.turns += 1;
87
+ if (event.type === "turn.failed") {
88
+ totals.turnFailures += 1;
89
+ turns.push(turn);
90
+ continue;
91
+ }
92
+ const usage = event.data.usage;
93
+ if (usage !== undefined) {
94
+ turn.usage = usage;
95
+ totals.inputTokens += usage.inputTokens;
96
+ totals.outputTokens += usage.outputTokens;
97
+ totals.cacheReadTokens += usage.cacheReadTokens;
98
+ totals.cacheWriteTokens += usage.cacheWriteTokens;
99
+ }
100
+ const cost = event.data.cost;
101
+ if (cost !== undefined) {
102
+ turn.cost = cost;
103
+ totals.costUsd = roundCostUsd(totals.costUsd + cost.totalUsd);
104
+ } else if (usage !== undefined) {
105
+ totals.unpricedTurns += 1;
106
+ }
107
+ turns.push(turn);
108
+ }
109
+
110
+ return { sessionId, turns, totals };
111
+ }
112
+
113
+ function formatCount(value: number): string {
114
+ return String(value);
115
+ }
116
+
117
+ /** Per-turn table for `agent-serve cost <sessionId>`. */
118
+ export function formatSessionCostText(report: SessionCostReport): string {
119
+ const lines: string[] = [];
120
+ lines.push(
121
+ `session ${report.sessionId || "(unknown)"} est. cost: ${formatCostUsd(report.totals.costUsd)}`
122
+ );
123
+ if (report.turns.length === 0) {
124
+ lines.push("No turns recorded.");
125
+ return `${lines.join("\n")}\n`;
126
+ }
127
+ const header = [
128
+ "TURN",
129
+ "IN",
130
+ "OUT",
131
+ "CACHE R",
132
+ "CACHE W",
133
+ "EST COST",
134
+ "MODEL",
135
+ "STATUS",
136
+ ];
137
+ const rows = report.turns.map((turn) => [
138
+ turn.turnId,
139
+ turn.usage === undefined ? "—" : formatCount(turn.usage.inputTokens),
140
+ turn.usage === undefined ? "—" : formatCount(turn.usage.outputTokens),
141
+ turn.usage === undefined ? "—" : formatCount(turn.usage.cacheReadTokens),
142
+ turn.usage === undefined ? "—" : formatCount(turn.usage.cacheWriteTokens),
143
+ turn.cost === undefined ? "—" : formatCostUsd(turn.cost.totalUsd),
144
+ turn.cost?.model ?? "—",
145
+ turn.failed ? "failed" : "ok",
146
+ ]);
147
+ rows.push([
148
+ "total",
149
+ formatCount(report.totals.inputTokens),
150
+ formatCount(report.totals.outputTokens),
151
+ formatCount(report.totals.cacheReadTokens),
152
+ formatCount(report.totals.cacheWriteTokens),
153
+ formatCostUsd(report.totals.costUsd),
154
+ "",
155
+ report.totals.turnFailures > 0
156
+ ? `${report.totals.turnFailures} failed`
157
+ : "",
158
+ ]);
159
+ lines.push(renderTable(header, rows));
160
+ appendUnpricedNote(lines, report.totals.unpricedTurns);
161
+ return `${lines.join("\n")}\n`;
162
+ }
163
+
164
+ export interface SessionCostRow {
165
+ sessionId: string;
166
+ channelId: string;
167
+ title?: string;
168
+ updatedAt?: string;
169
+ totals: SessionCostTotals;
170
+ }
171
+
172
+ /** All-sessions table for `agent-serve cost` (no session id). */
173
+ export function formatSessionsCostText(rows: SessionCostRow[]): string {
174
+ if (rows.length === 0) {
175
+ return "No sessions.\n";
176
+ }
177
+ const header = [
178
+ "SESSION",
179
+ "CHANNEL",
180
+ "TURNS",
181
+ "IN",
182
+ "OUT",
183
+ "EST COST",
184
+ "UPDATED",
185
+ ];
186
+ const totals = emptySessionCostTotals();
187
+ const body = rows.map((row) => {
188
+ addSessionCostTotals(totals, row.totals);
189
+ return [
190
+ row.sessionId,
191
+ row.channelId,
192
+ formatCount(row.totals.turns),
193
+ formatCount(row.totals.inputTokens),
194
+ formatCount(row.totals.outputTokens),
195
+ formatCostUsd(row.totals.costUsd),
196
+ row.updatedAt ?? "",
197
+ ];
198
+ });
199
+ body.push([
200
+ "total",
201
+ "",
202
+ formatCount(totals.turns),
203
+ formatCount(totals.inputTokens),
204
+ formatCount(totals.outputTokens),
205
+ formatCostUsd(totals.costUsd),
206
+ "",
207
+ ]);
208
+ const lines = [renderTable(header, body)];
209
+ appendUnpricedNote(lines, totals.unpricedTurns);
210
+ return `${lines.join("\n")}\n`;
211
+ }
212
+
213
+ function appendUnpricedNote(lines: string[], unpricedTurns: number): void {
214
+ if (unpricedTurns > 0) {
215
+ lines.push(
216
+ `note: ${unpricedTurns} turn(s) reported usage for a model with no known rates; est. cost excludes them.`
217
+ );
218
+ }
219
+ }
220
+
221
+ function renderTable(header: string[], rows: string[][]): string {
222
+ const widths = header.map((h, i) =>
223
+ Math.max(h.length, ...rows.map((row) => (row[i] ?? "").length))
224
+ );
225
+ const renderRow = (row: string[]): string =>
226
+ row
227
+ .map((cell, i) => cell.padEnd(widths[i] ?? 0))
228
+ .join(" ")
229
+ .trimEnd();
230
+ return [renderRow(header), ...rows.map(renderRow)].join("\n");
231
+ }
@@ -34,6 +34,7 @@ import {
34
34
  type ChannelEventHandler,
35
35
  type ChannelSession,
36
36
  type ConnectionTransport,
37
+ DEFAULT_MODEL,
37
38
  DIRECT_TOOL_CALL_CHANNEL_ID,
38
39
  type HookHandler,
39
40
  type HostContext,
@@ -80,6 +81,7 @@ import {
80
81
  } from "./ids.js";
81
82
  import { validateToolInput } from "./json-schema.js";
82
83
  import { McpHost } from "./mcp-host.js";
84
+ import { estimateTurnCost } from "./model-pricing.js";
83
85
  import {
84
86
  buildSessionContextBlock,
85
87
  SCHEDULE_CHANNEL_PREFIX,
@@ -535,6 +537,19 @@ export class SessionEngine {
535
537
  pendingWorkspaceFiles: options.workspaceFiles,
536
538
  }));
537
539
  }
540
+ // Refresh the cloud attach the same way: a follow-up send carries the
541
+ // caller's CURRENT cloud options (e.g. a PR attach re-derived per wake),
542
+ // and the first send's snapshot must not pin this session's turns to a
543
+ // stale repo/prUrl for the session's lifetime.
544
+ if (
545
+ options.cloud !== undefined &&
546
+ JSON.stringify(options.cloud) !== JSON.stringify(record.cloudOverride)
547
+ ) {
548
+ record = await this.updateRecord(record.sessionId, (current) => ({
549
+ ...current,
550
+ cloudOverride: options.cloud,
551
+ }));
552
+ }
538
553
  const coalesced = await this.startTurn(record.sessionId, message, {
539
554
  admission: options.admission ?? "preempt",
540
555
  ...(options.coalesceSourceTs === undefined
@@ -1252,6 +1267,18 @@ export class SessionEngine {
1252
1267
 
1253
1268
  const finalResult = outcome.result ?? mapper.finalText;
1254
1269
  const finalUsage = outcome.usage ?? mapper.usage;
1270
+ // Estimated dollar cost of this loop pass, persisted alongside usage so
1271
+ // traces / A/B metrics / the CLI can aggregate spend without re-pricing.
1272
+ const finalCost =
1273
+ finalUsage === undefined
1274
+ ? undefined
1275
+ : estimateTurnCost(
1276
+ this.project.agent.model ?? DEFAULT_MODEL,
1277
+ finalUsage
1278
+ );
1279
+ if (finalCost !== undefined) {
1280
+ outcome = { ...outcome, cost: finalCost };
1281
+ }
1255
1282
 
1256
1283
  if (outcome.status === "finished") {
1257
1284
  await this.appendEvent(sessionId, {
@@ -1260,6 +1287,7 @@ export class SessionEngine {
1260
1287
  data: {
1261
1288
  ...(finalResult === undefined ? {} : { result: finalResult }),
1262
1289
  ...(finalUsage === undefined ? {} : { usage: finalUsage }),
1290
+ ...(finalCost === undefined ? {} : { cost: finalCost }),
1263
1291
  },
1264
1292
  });
1265
1293
  } else {
@@ -7,7 +7,7 @@
7
7
  */
8
8
 
9
9
  import { readFile } from "node:fs/promises";
10
- import type { SessionEvent, TurnUsage } from "../types.js";
10
+ import type { SessionEvent, TurnCost, TurnUsage } from "../types.js";
11
11
 
12
12
  export interface TrajectoryToolCall {
13
13
  callId: string;
@@ -37,6 +37,8 @@ export interface TrajectoryTurn {
37
37
  toolCalls: TrajectoryToolCall[];
38
38
  subagents: TrajectorySubagent[];
39
39
  usage?: TurnUsage;
40
+ /** Estimated dollar cost of this turn (from the `turn.completed` event). */
41
+ cost?: TurnCost;
40
42
  result?: string;
41
43
  failed: boolean;
42
44
  error?: string;
@@ -51,6 +53,8 @@ export interface Trajectory {
51
53
  finalText: string;
52
54
  turnsFailed: number;
53
55
  toolCallCount: number;
56
+ /** Sum of per-turn estimated costs; present when any turn reported cost. */
57
+ totalCostUsd?: number;
54
58
  turns: TrajectoryTurn[];
55
59
  }
56
60
 
@@ -228,6 +232,9 @@ export function buildTrajectory(events: SessionEvent[]): Trajectory {
228
232
  if (event.data.usage !== undefined) {
229
233
  turn.usage = event.data.usage;
230
234
  }
235
+ if (event.data.cost !== undefined) {
236
+ turn.cost = event.data.cost;
237
+ }
231
238
  if (event.data.result !== undefined) {
232
239
  turn.result = event.data.result;
233
240
  if (turn.assistantText === "") {
@@ -267,6 +274,13 @@ export function buildTrajectory(events: SessionEvent[]): Trajectory {
267
274
  0
268
275
  );
269
276
  const lastGood = [...turns].reverse().find((t) => t.assistantText !== "");
277
+ const costedTurns = turns.filter((t) => t.cost !== undefined);
278
+ const totalCostUsd =
279
+ costedTurns.length === 0
280
+ ? undefined
281
+ : roundCostUsd(
282
+ costedTurns.reduce((sum, t) => sum + (t.cost?.totalUsd ?? 0), 0)
283
+ );
270
284
  return {
271
285
  sessionId,
272
286
  ...(sdkAgentId === undefined ? {} : { sdkAgentId }),
@@ -276,10 +290,30 @@ export function buildTrajectory(events: SessionEvent[]): Trajectory {
276
290
  finalText: lastGood?.assistantText ?? "",
277
291
  turnsFailed,
278
292
  toolCallCount,
293
+ ...(totalCostUsd === undefined ? {} : { totalCostUsd }),
279
294
  turns,
280
295
  };
281
296
  }
282
297
 
298
+ /** Micro-dollar precision so summed floats stay readable in JSON. */
299
+ export function roundCostUsd(usd: number): number {
300
+ return Math.round(usd * 1e6) / 1e6;
301
+ }
302
+
303
+ /** Compact dollar display for estimated costs (`$0.0132`, `$1.25`). */
304
+ export function formatCostUsd(usd: number): string {
305
+ if (!Number.isFinite(usd)) {
306
+ return "—";
307
+ }
308
+ if (usd === 0) {
309
+ return "$0.00";
310
+ }
311
+ if (usd >= 0.1) {
312
+ return `$${usd.toFixed(2)}`;
313
+ }
314
+ return `$${usd.toFixed(4)}`;
315
+ }
316
+
283
317
  /** Parse an `events.ndjson` file (or any NDJSON blob of SessionEvents). */
284
318
  export function parseEventsNdjson(text: string): SessionEvent[] {
285
319
  const events: SessionEvent[] = [];
@@ -318,7 +352,11 @@ export function formatTrajectoryText(trajectory: Trajectory): string {
318
352
  lines.push(`url: ${trajectory.agentUrl}`);
319
353
  }
320
354
  lines.push(
321
- `turns: ${trajectory.turns.length} tools=${trajectory.toolCallCount} failed: ${trajectory.turnsFailed}`
355
+ `turns: ${trajectory.turns.length} tools=${trajectory.toolCallCount} failed: ${trajectory.turnsFailed}${
356
+ trajectory.totalCostUsd === undefined
357
+ ? ""
358
+ : ` est. cost: ${formatCostUsd(trajectory.totalCostUsd)}`
359
+ }`
322
360
  );
323
361
  for (const turn of trajectory.turns) {
324
362
  lines.push("");
@@ -356,7 +394,11 @@ export function formatTrajectoryText(trajectory: Trajectory): string {
356
394
  }
357
395
  if (turn.usage !== undefined) {
358
396
  lines.push(
359
- ` usage: in ${turn.usage.inputTokens} out ${turn.usage.outputTokens}`
397
+ ` usage: in ${turn.usage.inputTokens} out ${turn.usage.outputTokens}${
398
+ turn.cost === undefined
399
+ ? ""
400
+ : ` est. cost: ${formatCostUsd(turn.cost.totalUsd)}`
401
+ }`
360
402
  );
361
403
  }
362
404
  }
package/src/types.ts CHANGED
@@ -942,7 +942,7 @@ export type SessionEventPayload =
942
942
  }
943
943
  | {
944
944
  type: "turn.completed";
945
- data: { result?: string; usage?: TurnUsage };
945
+ data: { result?: string; usage?: TurnUsage; cost?: TurnCost };
946
946
  }
947
947
  | { type: "turn.failed"; data: { message: string } }
948
948
  | { type: "session.waiting"; data: Record<string, never> }
@@ -960,6 +960,27 @@ export interface TurnUsage {
960
960
  reasoningTokens?: number;
961
961
  }
962
962
 
963
+ /**
964
+ * Estimated dollar cost of one turn's token usage (one agent loop pass).
965
+ * Recorded on `turn.completed` events so traces, the playground, A/B
966
+ * metrics, and the CLI can aggregate spend. Estimates price the turn's
967
+ * {@link TurnUsage} against the framework's snapshot of Cursor's published
968
+ * per-model rates — the billed amount on the Cursor dashboard is the source
969
+ * of truth.
970
+ */
971
+ export interface TurnCost {
972
+ /** Estimated total for the turn, in USD. */
973
+ totalUsd: number;
974
+ inputUsd: number;
975
+ outputUsd: number;
976
+ cacheReadUsd: number;
977
+ cacheWriteUsd: number;
978
+ /** Normalized model id the estimate was priced against. */
979
+ model: string;
980
+ /** Always `"estimate"`: framework rate table, not the billing pipeline. */
981
+ source: "estimate";
982
+ }
983
+
963
984
  // ============================================================================
964
985
  // Sessions
965
986
  // ============================================================================
@@ -1061,6 +1082,8 @@ export interface TurnOutcome {
1061
1082
  result?: string;
1062
1083
  errorMessage?: string;
1063
1084
  usage?: TurnUsage;
1085
+ /** Estimated cost of {@link usage} when the model has known rates. */
1086
+ cost?: TurnCost;
1064
1087
  }
1065
1088
 
1066
1089
  /**
@@ -1,20 +0,0 @@
1
- /**
2
- * Per-request observability for GitHub channel Octokit clients.
3
- *
4
- * Every Octokit built by `createGitHubOctokit` / `callGitHubApi` gets one
5
- * structured `[agentkit/github]` line per GitHub API request (REST and
6
- * GraphQL), including rate-limit headers so installation-quota pressure can
7
- * be attributed to agentkit deployments from hosted-runtime logs.
8
- */
9
- import type { Octokit } from "@octokit/rest";
10
- export type GitHubRequestLogWrite = (line: string) => void;
11
- /** Best-effort GraphQL operation name (`query Foo` / `mutation Bar`). */
12
- export declare function parseGraphQLOperationName(query: unknown): string | undefined;
13
- /**
14
- * Wrap an Octokit so every request emits one structured log line. Returns
15
- * the same instance for call-through convenience. Idempotent per instance.
16
- */
17
- export declare function instrumentGitHubOctokit(octokit: Octokit, input?: {
18
- write?: GitHubRequestLogWrite;
19
- }): Octokit;
20
- //# sourceMappingURL=instrument.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"instrument.d.ts","sourceRoot":"","sources":["../../../src/channels/github/instrument.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAI7C,MAAM,MAAM,qBAAqB,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;AA4C3D,yEAAyE;AACzE,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAQ5E;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,OAAO,EAChB,KAAK,CAAC,EAAE;IAAE,KAAK,CAAC,EAAE,qBAAqB,CAAA;CAAE,GACxC,OAAO,CAmDT"}