@cursor/july 0.1.7 → 0.1.9

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 (210) hide show
  1. package/dist/bin/agent-serve.js +31 -2
  2. package/dist/channels/slack/channel-watch.d.ts +6 -1
  3. package/dist/channels/slack/channel-watch.d.ts.map +1 -1
  4. package/dist/channels/slack/channel-watch.js +22 -1
  5. package/dist/channels/slack/cursor-account.d.ts +4 -0
  6. package/dist/channels/slack/cursor-account.d.ts.map +1 -1
  7. package/dist/channels/slack/cursor-account.js +12 -2
  8. package/dist/channels/slack/defaults.d.ts +10 -5
  9. package/dist/channels/slack/defaults.d.ts.map +1 -1
  10. package/dist/channels/slack/defaults.js +39 -17
  11. package/dist/channels/slack/dispatch.d.ts +18 -0
  12. package/dist/channels/slack/dispatch.d.ts.map +1 -1
  13. package/dist/channels/slack/dispatch.js +141 -38
  14. package/dist/channels/slack/inbound.d.ts +13 -3
  15. package/dist/channels/slack/inbound.d.ts.map +1 -1
  16. package/dist/channels/slack/inbound.js +17 -9
  17. package/dist/channels/slack/index.d.ts +4 -0
  18. package/dist/channels/slack/index.d.ts.map +1 -1
  19. package/dist/channels/slack/index.js +4 -0
  20. package/dist/channels/slack/live-delivery.d.ts.map +1 -1
  21. package/dist/channels/slack/live-delivery.js +1 -0
  22. package/dist/channels/slack/manifest.d.ts.map +1 -1
  23. package/dist/channels/slack/manifest.js +3 -0
  24. package/dist/channels/slack/nudge-carrier.d.ts +19 -0
  25. package/dist/channels/slack/nudge-carrier.d.ts.map +1 -0
  26. package/dist/channels/slack/nudge-carrier.js +166 -0
  27. package/dist/channels/slack/nudge-store.d.ts +61 -0
  28. package/dist/channels/slack/nudge-store.d.ts.map +1 -0
  29. package/dist/channels/slack/nudge-store.js +266 -0
  30. package/dist/channels/slack/post-update-delivery.js +318 -0
  31. package/dist/channels/slack/slack-channel.d.ts.map +1 -1
  32. package/dist/channels/slack/slack-channel.js +74 -25
  33. package/dist/channels/slack/stop.d.ts +14 -0
  34. package/dist/channels/slack/stop.d.ts.map +1 -0
  35. package/dist/channels/slack/stop.js +22 -0
  36. package/dist/channels/slack/thread-context.d.ts +81 -0
  37. package/dist/channels/slack/thread-context.d.ts.map +1 -0
  38. package/dist/channels/slack/thread-context.js +470 -0
  39. package/dist/channels/slack/types.d.ts +27 -0
  40. package/dist/channels/slack/types.d.ts.map +1 -1
  41. package/dist/docs/404.html +2 -2
  42. package/dist/docs/ab.html +4 -4
  43. package/dist/docs/assets/{app.COTN7wgo.js → app.Ciosf24M.js} +1 -1
  44. package/dist/docs/assets/chunks/@localSearchIndexroot.D154Detj.js +1 -0
  45. package/dist/docs/assets/chunks/{VPLocalSearchBox.BW3TBdT0.js → VPLocalSearchBox.BcRDly7n.js} +1 -1
  46. package/dist/docs/assets/chunks/{theme.BEJW0vE7.js → theme.CMpbjdfx.js} +2 -2
  47. package/dist/docs/assets/{deployment.md.BtfEsc9S.js → deployment.md.DC5-ImcM.js} +1 -1
  48. package/dist/docs/assets/{example-agents_approval-buddy.md.8R5phXb5.js → example-agents_approval-buddy.md.DFGBYLcc.js} +2 -2
  49. package/dist/docs/assets/{example-agents_approval-buddy.md.8R5phXb5.lean.js → example-agents_approval-buddy.md.DFGBYLcc.lean.js} +1 -1
  50. package/dist/docs/assets/example-agents_index.md.BPXjUtRB.js +2 -0
  51. package/dist/docs/assets/example-agents_index.md.BPXjUtRB.lean.js +1 -0
  52. package/dist/docs/assets/example-agents_oncall.md.BG_sUMly.js +10 -0
  53. package/dist/docs/assets/example-agents_oncall.md.BG_sUMly.lean.js +1 -0
  54. package/dist/docs/assets/{example-agents_security-reviewer.md.D2rtwDTO.js → example-agents_security-reviewer.md.CE9v-aKm.js} +2 -2
  55. package/dist/docs/assets/{example-agents_security-reviewer.md.D2rtwDTO.lean.js → example-agents_security-reviewer.md.CE9v-aKm.lean.js} +1 -1
  56. package/dist/docs/assets/{guides_slack.md.CCwqHvSV.js → guides_slack.md.FWale6Ip.js} +4 -3
  57. package/dist/docs/assets/{guides_slack.md.CCwqHvSV.lean.js → guides_slack.md.FWale6Ip.lean.js} +1 -1
  58. package/dist/docs/assets/{reference_sessions.md.DdjWabnN.js → reference_sessions.md.DLd6mvbv.js} +2 -2
  59. package/dist/docs/assets/{reference_sessions.md.DdjWabnN.lean.js → reference_sessions.md.DLd6mvbv.lean.js} +1 -1
  60. package/dist/docs/assets/{storage.md.CVnInNiN.js → storage.md.DRTdnFvd.js} +9 -2
  61. package/dist/docs/assets/{storage.md.CVnInNiN.lean.js → storage.md.DRTdnFvd.lean.js} +1 -1
  62. package/dist/docs/building-with-agents.html +4 -4
  63. package/dist/docs/concepts.html +4 -4
  64. package/dist/docs/deployment.html +6 -6
  65. package/dist/docs/evals.html +4 -4
  66. package/dist/docs/example-agents/approval-buddy.html +5 -5
  67. package/dist/docs/example-agents/benny.html +5 -5
  68. package/dist/docs/example-agents/bugbot.html +5 -5
  69. package/dist/docs/example-agents/codebase-wiki.html +4 -4
  70. package/dist/docs/example-agents/codeowners-review.html +4 -4
  71. package/dist/docs/example-agents/concierge.html +4 -4
  72. package/dist/docs/example-agents/fsd.html +4 -4
  73. package/dist/docs/example-agents/index.html +6 -6
  74. package/dist/docs/example-agents/knowledge-base.html +4 -4
  75. package/dist/docs/example-agents/oncall.html +34 -0
  76. package/dist/docs/example-agents/security-reviewer.html +6 -6
  77. package/dist/docs/example-agents/slack-agent.html +4 -4
  78. package/dist/docs/example-agents/weather-agent.html +4 -4
  79. package/dist/docs/guides/agent-to-agent.html +4 -4
  80. package/dist/docs/guides/cloud-runtime.html +4 -4
  81. package/dist/docs/guides/github.html +4 -4
  82. package/dist/docs/guides/human-in-the-loop.html +4 -4
  83. package/dist/docs/guides/slack.html +7 -6
  84. package/dist/docs/guides/webhooks.html +4 -4
  85. package/dist/docs/hashmap.json +1 -1
  86. package/dist/docs/hillclimbing.html +4 -4
  87. package/dist/docs/index.html +4 -4
  88. package/dist/docs/quickstart.html +4 -4
  89. package/dist/docs/reference/agent-config.html +4 -4
  90. package/dist/docs/reference/channels.html +4 -4
  91. package/dist/docs/reference/cli.html +4 -4
  92. package/dist/docs/reference/connections.html +4 -4
  93. package/dist/docs/reference/hooks.html +4 -4
  94. package/dist/docs/reference/http-api.html +4 -4
  95. package/dist/docs/reference/instructions.html +4 -4
  96. package/dist/docs/reference/playground.html +4 -4
  97. package/dist/docs/reference/project-layout.html +4 -4
  98. package/dist/docs/reference/schedules.html +4 -4
  99. package/dist/docs/reference/sessions.html +5 -5
  100. package/dist/docs/reference/skills.html +4 -4
  101. package/dist/docs/reference/subagents.html +4 -4
  102. package/dist/docs/reference/tools.html +4 -4
  103. package/dist/docs/scaffolding-agents.html +4 -4
  104. package/dist/docs/storage.html +13 -6
  105. package/dist/docs/troubleshooting.html +4 -4
  106. package/dist/internal/cli-deploy.d.ts.map +1 -1
  107. package/dist/internal/cli-deploy.js +45 -18
  108. package/dist/internal/cli-mcp.d.ts +64 -0
  109. package/dist/internal/cli-mcp.d.ts.map +1 -0
  110. package/dist/internal/cli-mcp.js +570 -0
  111. package/dist/internal/deploy-client.d.ts +7 -1
  112. package/dist/internal/deploy-client.d.ts.map +1 -1
  113. package/dist/internal/deploy-client.js +3 -0
  114. package/dist/internal/deploy-source.d.ts +3 -0
  115. package/dist/internal/deploy-source.d.ts.map +1 -1
  116. package/dist/internal/deploy-source.js +25 -7
  117. package/dist/internal/discovery.js +1 -1
  118. package/dist/internal/distribution.d.ts.map +1 -1
  119. package/dist/internal/distribution.js +3 -0
  120. package/dist/internal/host-kv.d.ts +17 -0
  121. package/dist/internal/host-kv.d.ts.map +1 -0
  122. package/dist/internal/host-kv.js +77 -0
  123. package/dist/internal/hosting.d.ts +12 -2
  124. package/dist/internal/hosting.d.ts.map +1 -1
  125. package/dist/internal/hosting.js +21 -4
  126. package/dist/internal/json-dir-store.js +100 -0
  127. package/dist/internal/logs-client.d.ts +16 -2
  128. package/dist/internal/logs-client.d.ts.map +1 -1
  129. package/dist/internal/logs-client.js +51 -16
  130. package/dist/internal/server.d.ts.map +1 -1
  131. package/dist/internal/server.js +8 -0
  132. package/dist/internal/session-engine.d.ts +42 -7
  133. package/dist/internal/session-engine.d.ts.map +1 -1
  134. package/dist/internal/session-engine.js +276 -66
  135. package/dist/internal/storage-coordinator.d.ts +18 -1
  136. package/dist/internal/storage-coordinator.d.ts.map +1 -1
  137. package/dist/internal/storage-coordinator.js +43 -0
  138. package/dist/internal/workspace.d.ts +19 -0
  139. package/dist/internal/workspace.d.ts.map +1 -1
  140. package/dist/internal/workspace.js +47 -13
  141. package/dist/playground/assets/{index-72vCOBWO.js → index-C0-QEG4O.js} +42 -42
  142. package/dist/playground/assets/index-z3JY9O2x.css +1 -0
  143. package/dist/playground/index.html +2 -2
  144. package/dist/storage-backends/cursor-hosted.d.ts +65 -0
  145. package/dist/storage-backends/cursor-hosted.d.ts.map +1 -0
  146. package/dist/storage-backends/cursor-hosted.js +203 -0
  147. package/dist/storage-backends/file-kv.d.ts +42 -0
  148. package/dist/storage-backends/file-kv.d.ts.map +1 -0
  149. package/dist/storage-backends/file-kv.js +190 -0
  150. package/dist/storage-backends/postgres-kv.d.ts +82 -0
  151. package/dist/storage-backends/postgres-kv.d.ts.map +1 -0
  152. package/dist/storage-backends/postgres-kv.js +185 -0
  153. package/dist/storage.d.ts +10 -1
  154. package/dist/storage.d.ts.map +1 -1
  155. package/dist/storage.js +10 -1
  156. package/dist/types.d.ts +79 -0
  157. package/dist/types.d.ts.map +1 -1
  158. package/docs/.vitepress/cache/deps/package.json +3 -0
  159. package/docs/deployment.md +7 -3
  160. package/docs/example-agents/approval-buddy.md +1 -1
  161. package/docs/example-agents/index.md +15 -10
  162. package/docs/example-agents/oncall.md +216 -0
  163. package/docs/example-agents/security-reviewer.md +5 -5
  164. package/docs/guides/slack.md +11 -2
  165. package/docs/reference/sessions.md +13 -4
  166. package/docs/storage.md +41 -0
  167. package/package.json +22 -1
  168. package/skills/framework-map/SKILL.md +6 -4
  169. package/src/bin/agent-serve.ts +32 -2
  170. package/src/channels/slack/channel-watch.ts +28 -1
  171. package/src/channels/slack/cursor-account.ts +20 -0
  172. package/src/channels/slack/defaults.ts +51 -20
  173. package/src/channels/slack/dispatch.ts +215 -40
  174. package/src/channels/slack/inbound.ts +30 -10
  175. package/src/channels/slack/index.ts +4 -0
  176. package/src/channels/slack/live-delivery.ts +1 -0
  177. package/src/channels/slack/manifest.ts +3 -0
  178. package/src/channels/slack/nudge-carrier.ts +182 -0
  179. package/src/channels/slack/nudge-store.ts +308 -0
  180. package/src/channels/slack/slack-channel.ts +106 -31
  181. package/src/channels/slack/stop.ts +25 -0
  182. package/src/channels/slack/thread-context.ts +572 -0
  183. package/src/channels/slack/types.ts +27 -0
  184. package/src/internal/cli-deploy.ts +58 -20
  185. package/src/internal/cli-mcp.ts +708 -0
  186. package/src/internal/deploy-client.ts +11 -0
  187. package/src/internal/deploy-source.ts +24 -6
  188. package/src/internal/discovery.ts +1 -1
  189. package/src/internal/distribution.ts +3 -0
  190. package/src/internal/host-kv.ts +85 -0
  191. package/src/internal/hosting.ts +24 -4
  192. package/src/internal/logs-client.ts +67 -21
  193. package/src/internal/server.ts +31 -0
  194. package/src/internal/session-engine.ts +379 -61
  195. package/src/internal/storage-coordinator.ts +41 -0
  196. package/src/internal/workspace.ts +48 -11
  197. package/src/storage-backends/cursor-hosted.ts +246 -0
  198. package/src/storage-backends/file-kv.ts +221 -0
  199. package/src/storage-backends/postgres-kv.ts +253 -0
  200. package/src/storage.ts +11 -1
  201. package/src/types.ts +83 -0
  202. package/dist/docs/assets/chunks/@localSearchIndexroot.B7UcKvIn.js +0 -1
  203. package/dist/docs/assets/example-agents_index.md.CRqJlnIf.js +0 -2
  204. package/dist/docs/assets/example-agents_index.md.CRqJlnIf.lean.js +0 -1
  205. package/dist/internal/persistence-coordinator.d.ts +0 -127
  206. package/dist/internal/persistence-coordinator.d.ts.map +0 -1
  207. package/dist/persistence.d.ts +0 -184
  208. package/dist/persistence.d.ts.map +0 -1
  209. package/dist/playground/assets/index-BjnMwYoR.css +0 -1
  210. /package/dist/docs/assets/{deployment.md.BtfEsc9S.lean.js → deployment.md.DC5-ImcM.lean.js} +0 -0
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Postgres `defineStorage` helper on the agent-serve tables
3
+ * (backend/packages/prisma-bugbot):
4
+ *
5
+ * agent_serve_kv(deployment_id, key, value JSONB, updated_at)
6
+ * PRIMARY KEY (deployment_id, key)
7
+ * agent_serve_eval_runs(deployment_id, agent, run_id, run JSONB, updated_at)
8
+ * PRIMARY KEY (deployment_id, agent, run_id)
9
+ * agent_serve_ab(deployment_id, agent, kind, ref, value JSONB, updated_at)
10
+ * PRIMARY KEY (deployment_id, agent, kind, ref)
11
+ *
12
+ * For Cursor-managed hosting prefer `cursorHostedStorage` from
13
+ * `@cursor/july/storage/cursor-hosted` — same tables via a control-plane
14
+ * HTTP proxy (no pod-visible DSN). Use this helper when you supply
15
+ * `BUGBOTDB_URL` / `AGENT_SERVE_DEPLOYMENT_ID` yourself (self-host).
16
+ *
17
+ * `postgresStorage` implements the whole `defineStorage` surface as plain
18
+ * SQL: the four KV methods against the last-write-wins `agent_serve_kv`
19
+ * table (sessions, event chunks, continuations, reminders), the `evals`
20
+ * table against `agent_serve_eval_runs` (one row per playground eval
21
+ * batch), and the `abs` table against `agent_serve_ab` (`kind = 'sample'`
22
+ * rows per metric sample plus one `kind = 'snapshot'` row holding the
23
+ * latest aggregate).
24
+ *
25
+ * - KV `put` upserts on the primary key (LWW matches the framework contract).
26
+ * - KV `list` is a prefix range scan on the PK btree (`key >= p AND key < p||…`
27
+ * spelled as LIKE with escaped wildcards, ordered by key).
28
+ * - `get` returning no row is a definitive miss (`undefined`); a connection
29
+ * error throws, so lazy restore fails the follow-up instead of forking
30
+ * the session.
31
+ *
32
+ * Connection comes from `BUGBOTDB_URL` — the repo-wide convention for the
33
+ * bugbot Postgres database, whose Prisma package owns these tables' schema
34
+ * and migrations; locally it falls back to the dev bugbot DB. Non-local
35
+ * URLs enable TLS (`ssl.rejectUnauthorized`) because PlanetScale requires
36
+ * SSL even when the vault URL omits `sslmode`.
37
+ * `deploymentId` scopes rows per deployment (`AGENT_SERVE_DEPLOYMENT_ID`);
38
+ * the local default keeps dev runs of different checkouts from colliding
39
+ * only by agent name.
40
+ *
41
+ * Callers must pass `Pool` from their own `import { Pool } from "pg"`.
42
+ * This module must not import `pg` itself: Node resolves modules from
43
+ * *this* file's directory tree, so a `pg` dep declared only on the
44
+ * consuming agent package would never be found after a hosted install.
45
+ */
46
+
47
+ import type { ABSnapshot } from "../ab.js";
48
+ import type { EvalRunSnapshot } from "../evals.js";
49
+ import type {
50
+ StorageABTable,
51
+ StorageConfig,
52
+ StorageEntry,
53
+ StorageEvalsTable,
54
+ } from "../storage.js";
55
+ import type { JsonValue } from "../types.js";
56
+
57
+ /** Minimal `pg.Pool` surface used here (avoids importing `pg` from this path). */
58
+ export interface PostgresQueryable {
59
+ query<T extends Record<string, unknown> = Record<string, unknown>>(
60
+ text: string,
61
+ values?: readonly unknown[]
62
+ ): Promise<{ rows: T[] }>;
63
+ }
64
+
65
+ /** `pg.Pool` constructor supplied by the consuming example package. */
66
+ export type PostgresPoolConstructor = new (config?: {
67
+ connectionString?: string;
68
+ max?: number;
69
+ ssl?: boolean | { rejectUnauthorized?: boolean };
70
+ }) => PostgresQueryable;
71
+
72
+ export interface PostgresStorageOptions {
73
+ /**
74
+ * Pass `Pool` from `import { Pool } from "pg"` in the example that declares
75
+ * the `pg` dependency (hosted installs put it in that package's
76
+ * `node_modules` only).
77
+ */
78
+ Pool: PostgresPoolConstructor;
79
+ /** Postgres connection string (default: env, then local bugbot dev DB). */
80
+ databaseUrl?: string;
81
+ /** Value for the tables' deployment_id column (default: env or "local-dev"). */
82
+ deploymentId?: string;
83
+ }
84
+
85
+ type PostgresStorageMethods = Required<
86
+ Pick<StorageConfig, "put" | "get" | "delete" | "list">
87
+ > & {
88
+ evals: StorageEvalsTable;
89
+ abs: StorageABTable;
90
+ };
91
+
92
+ /** True for local bugbot/dev URLs that do not speak TLS. */
93
+ export function isLocalPostgresUrl(connectionString: string): boolean {
94
+ try {
95
+ const host = new URL(connectionString).hostname.toLowerCase();
96
+ return (
97
+ host === "localhost" ||
98
+ host === "127.0.0.1" ||
99
+ host === "::1" ||
100
+ host === "[::1]"
101
+ );
102
+ } catch {
103
+ return /@(localhost|127\.0\.0\.1|\[::1\])([:/]|$)/i.test(connectionString);
104
+ }
105
+ }
106
+
107
+ /** Postgres-backed `defineStorage` methods, ready to spread into `defineStorage({...})`. */
108
+ export function postgresStorage(
109
+ options: PostgresStorageOptions
110
+ ): PostgresStorageMethods {
111
+ const connectionString =
112
+ options.databaseUrl ??
113
+ process.env.BUGBOTDB_URL ??
114
+ "postgresql://prisma:prisma@localhost:5432/bugbot";
115
+ // Prod Bugbot is PlanetScale Postgres: TLS is required and the vault
116
+ // `BUGBOTDB_URL` often omits `sslmode`. Without an explicit `ssl` option,
117
+ // `pg` opens plaintext and PlanetScale rejects with "SSL connection is
118
+ // required" — sessions stay in-memory. Skip TLS only for local hosts.
119
+ const pool = new options.Pool({
120
+ connectionString,
121
+ // Small fixed pool per agent, mirroring the backend's connection_limit
122
+ // clamp on BUGBOTDB_URL consumers (backend/server/src/prisma.ts).
123
+ max: 3,
124
+ ...(isLocalPostgresUrl(connectionString)
125
+ ? {}
126
+ : { ssl: { rejectUnauthorized: true } }),
127
+ });
128
+ const deploymentId =
129
+ options.deploymentId ??
130
+ process.env.AGENT_SERVE_DEPLOYMENT_ID ??
131
+ "local-dev";
132
+
133
+ return {
134
+ async put(key, value): Promise<void> {
135
+ await pool.query(
136
+ `INSERT INTO agent_serve_kv (deployment_id, key, value, updated_at)
137
+ VALUES ($1, $2, $3::jsonb, now())
138
+ ON CONFLICT ON CONSTRAINT pk_agent_serve_kv
139
+ DO UPDATE SET value = EXCLUDED.value, updated_at = now()`,
140
+ [deploymentId, key, JSON.stringify(value)]
141
+ );
142
+ },
143
+
144
+ async get(key): Promise<JsonValue | undefined> {
145
+ const result = await pool.query<{ value: JsonValue }>(
146
+ `SELECT value FROM agent_serve_kv
147
+ WHERE deployment_id = $1 AND key = $2`,
148
+ [deploymentId, key]
149
+ );
150
+ return result.rows[0]?.value; // no row ⇒ definitive miss
151
+ },
152
+
153
+ async delete(key): Promise<void> {
154
+ await pool.query(
155
+ `DELETE FROM agent_serve_kv WHERE deployment_id = $1 AND key = $2`,
156
+ [deploymentId, key]
157
+ );
158
+ },
159
+
160
+ async list(prefix): Promise<StorageEntry[]> {
161
+ // Escape LIKE wildcards in the prefix so keys containing % or _
162
+ // (URI-encoded segments cannot, but be safe) match literally.
163
+ const escaped = prefix.replace(/([\\%_])/g, "\\$1");
164
+ const result = await pool.query<{ key: string; value: JsonValue }>(
165
+ `SELECT key, value FROM agent_serve_kv
166
+ WHERE deployment_id = $1 AND key LIKE $2 ESCAPE '\\'
167
+ ORDER BY key ASC`,
168
+ [deploymentId, `${escaped}%`]
169
+ );
170
+ return result.rows;
171
+ },
172
+
173
+ // Playground eval batches → agent_serve_eval_runs (one row per run).
174
+ evals: {
175
+ async put(run, ctx): Promise<void> {
176
+ await pool.query(
177
+ `INSERT INTO agent_serve_eval_runs (deployment_id, agent, run_id, run, updated_at)
178
+ VALUES ($1, $2, $3, $4::jsonb, now())
179
+ ON CONFLICT ON CONSTRAINT pk_agent_serve_eval_runs
180
+ DO UPDATE SET run = EXCLUDED.run, updated_at = now()`,
181
+ [deploymentId, ctx.agentName, run.runId, JSON.stringify(run)]
182
+ );
183
+ },
184
+ async delete(runId, ctx): Promise<void> {
185
+ await pool.query(
186
+ `DELETE FROM agent_serve_eval_runs
187
+ WHERE deployment_id = $1 AND agent = $2 AND run_id = $3`,
188
+ [deploymentId, ctx.agentName, runId]
189
+ );
190
+ },
191
+ async list(ctx): Promise<EvalRunSnapshot[]> {
192
+ const result = await pool.query<{ run: EvalRunSnapshot }>(
193
+ `SELECT run FROM agent_serve_eval_runs
194
+ WHERE deployment_id = $1 AND agent = $2`,
195
+ [deploymentId, ctx.agentName]
196
+ );
197
+ return result.rows.map((row) => row.run);
198
+ },
199
+ },
200
+
201
+ // A/B metrics → agent_serve_ab: sample rows plus one snapshot row.
202
+ abs: {
203
+ async putSample(sample, ctx): Promise<void> {
204
+ await upsertAbRow(pool, {
205
+ deploymentId,
206
+ agent: ctx.agentName,
207
+ kind: "sample",
208
+ // One boundary event emits a sample per enrolled experiment with
209
+ // the same sessionId/at — the experiment must be in the row key
210
+ // or those samples overwrite each other.
211
+ ref: `${sample.experiment}/${sample.sessionId}/${sample.at}`,
212
+ value: sample,
213
+ });
214
+ },
215
+ async putSnapshot(snapshot, ctx): Promise<void> {
216
+ await upsertAbRow(pool, {
217
+ deploymentId,
218
+ agent: ctx.agentName,
219
+ kind: "snapshot",
220
+ ref: "",
221
+ value: snapshot,
222
+ });
223
+ },
224
+ async getSnapshot(ctx): Promise<ABSnapshot | undefined> {
225
+ const result = await pool.query<{ value: JsonValue }>(
226
+ `SELECT value FROM agent_serve_ab
227
+ WHERE deployment_id = $1 AND agent = $2 AND kind = 'snapshot' AND ref = ''`,
228
+ [deploymentId, ctx.agentName]
229
+ );
230
+ return result.rows[0]?.value as unknown as ABSnapshot | undefined;
231
+ },
232
+ },
233
+ };
234
+ }
235
+
236
+ async function upsertAbRow(
237
+ pool: PostgresQueryable,
238
+ row: {
239
+ deploymentId: string;
240
+ agent: string;
241
+ kind: "sample" | "snapshot";
242
+ ref: string;
243
+ value: unknown;
244
+ }
245
+ ): Promise<void> {
246
+ await pool.query(
247
+ `INSERT INTO agent_serve_ab (deployment_id, agent, kind, ref, value, updated_at)
248
+ VALUES ($1, $2, $3, $4, $5::jsonb, now())
249
+ ON CONFLICT ON CONSTRAINT pk_agent_serve_ab
250
+ DO UPDATE SET value = EXCLUDED.value, updated_at = now()`,
251
+ [row.deploymentId, row.agent, row.kind, row.ref, JSON.stringify(row.value)]
252
+ );
253
+ }
package/src/storage.ts CHANGED
@@ -33,7 +33,9 @@
33
33
  *
34
34
  * Because keys are opaque strings to the sink, new kinds of durable state
35
35
  * (channel cursors, thread affinity, …) are new key prefixes — existing
36
- * sinks store them with no code changes.
36
+ * sinks store them with no code changes. Authors can also store their own
37
+ * JSON under `agentkit/v1/{agent}/kv/…` via {@link HostContext.kv}; the
38
+ * framework still mints the full key from the author-supplied segment.
37
39
  *
38
40
  * Delivery semantics: writes are **serialized** (one sink call in flight
39
41
  * per agent, in order), **bounded** (a sink that falls behind sheds writes
@@ -274,6 +276,7 @@ export const STORAGE_KEY_ROOT = "agentkit/v1" as const;
274
276
  * | `agentkit/v1/{agent}/session-events/{sessionId}/{index}` | `SessionEvent[]` chunk (index = first event's index, zero-padded) |
275
277
  * | `agentkit/v1/{agent}/continuation/{channelId}/{token}` | `{ sessionId }` |
276
278
  * | `agentkit/v1/{agent}/reminder/{reminderId}` | `ReminderRecord` |
279
+ * | `agentkit/v1/{agent}/kv/{key}` | Author JSON via {@link HostContext.kv} |
277
280
  *
278
281
  * Eval-run and A/B history do not flow through this KV scheme — they have
279
282
  * dedicated tables ({@link StorageConfig.evals} / {@link StorageConfig.abs}).
@@ -301,6 +304,13 @@ export const storageKeys = {
301
304
  `${STORAGE_KEY_ROOT}/${agent}/reminder/${reminderId}`,
302
305
  reminderPrefix: (agent: string): string =>
303
306
  `${STORAGE_KEY_ROOT}/${agent}/reminder/`,
307
+ /**
308
+ * Author-facing opaque JSON (see {@link HostContext.kv}). `key` is
309
+ * URI-encoded / digested like other caller-controlled segments.
310
+ */
311
+ kv: (agent: string, key: string): string =>
312
+ `${STORAGE_KEY_ROOT}/${agent}/kv/${keySegment(key)}`,
313
+ kvPrefix: (agent: string): string => `${STORAGE_KEY_ROOT}/${agent}/kv/`,
304
314
  } as const;
305
315
 
306
316
  /**
package/src/types.ts CHANGED
@@ -316,6 +316,28 @@ export interface HostMcpCallResult {
316
316
  structuredContent?: JsonObject;
317
317
  }
318
318
 
319
+ /**
320
+ * Durable Slack follow-up nudge + ask-claim API attached to {@link HostContext}.
321
+ * Used for mid-turn inject, boundary resume skip, and delayed-parent dedupe.
322
+ */
323
+ export interface SlackNudgeHostApi {
324
+ append(input: {
325
+ continuationKey: string;
326
+ sourceTs: string;
327
+ text: string;
328
+ author?: string;
329
+ }): Promise<boolean>;
330
+ /** Drain pending nudges and return a `<slack_followup>` block, if any. */
331
+ drainFormatted(continuationKey: string): Promise<string | undefined>;
332
+ clear(continuationKey: string): Promise<void>;
333
+ hasUndelivered(continuationKey: string): Promise<boolean>;
334
+ isDelivered(continuationKey: string, sourceTs: string): Promise<boolean>;
335
+ /** Claim `channelId:message.ts` exactly once (false = already seen). */
336
+ tryClaimAsk(channelId: string, messageTs: string): Promise<boolean>;
337
+ /** Release a claim after a failed send so Slack redelivery can retry. */
338
+ releaseAsk(channelId: string, messageTs: string): Promise<void>;
339
+ }
340
+
319
341
  /**
320
342
  * Shared host services available on every tool / channel / schedule surface.
321
343
  * Channel packs attach this as `ctx.host` so handlers can tap GitHub, Slack,
@@ -339,6 +361,29 @@ export interface HostContext {
339
361
  * the serve host has attached a {@link ReminderRunner}.
340
362
  */
341
363
  reminders?: ReminderHostApi;
364
+ /**
365
+ * Slack ask-dedupe / optional nudge helpers. Always present on a live
366
+ * {@link SessionEngine} host. Mid-turn nudge inject is not used; busy
367
+ * Slack follow-ups coalesce and drain at turn boundaries.
368
+ */
369
+ slackNudges?: SlackNudgeHostApi;
370
+ /**
371
+ * Author-facing durable JSON KV (`agentkit/v1/{agent}/kv/…`). Backed by
372
+ * `agent/storage.ts` (`defineStorage`) when authored; otherwise files
373
+ * under `--state-root/kv`. Prefer this over ad-hoc files under the
374
+ * project tree so state survives host replacement.
375
+ */
376
+ kv: HostKvApi;
377
+ }
378
+
379
+ /** Host-facing JSON key-value API attached to {@link HostContext.kv}. */
380
+ export interface HostKvApi {
381
+ /** Read one value. Miss → `undefined`. */
382
+ get(key: string): Promise<JsonValue | undefined>;
383
+ /** Write or replace one value. */
384
+ put(key: string, value: JsonValue): Promise<void>;
385
+ /** Remove one key. No-op when missing. */
386
+ delete(key: string): Promise<void>;
342
387
  }
343
388
 
344
389
  /** Runtime context handed to a tool's `execute`. */
@@ -851,6 +896,11 @@ export interface ChannelSession {
851
896
  readonly continuationToken: string | null;
852
897
  /** True when this `send()` created the session. */
853
898
  readonly isNew: boolean;
899
+ /**
900
+ * True when `admission: "coalesce"` enqueued this message behind an
901
+ * in-flight turn instead of starting immediately (Slack busy path).
902
+ */
903
+ readonly coalesced?: boolean;
854
904
  readonly info: SessionInfo;
855
905
  /** Replay recorded events from `startIndex`, then follow live (NDJSON). */
856
906
  getEventStream(options?: {
@@ -919,6 +969,17 @@ export interface SendMessageOptions {
919
969
  * store is gone.
920
970
  */
921
971
  sdkAgentId?: string;
972
+ /**
973
+ * Turn admission when a session is already busy.
974
+ * - `"preempt"` (default) — interrupt the in-flight turn (HTTP / MCP).
975
+ * - `"coalesce"` — enqueue and return without interrupting (Slack).
976
+ */
977
+ admission?: "preempt" | "coalesce";
978
+ /**
979
+ * Optional source message timestamp for coalesce queue items. When a
980
+ * mid-turn nudge already delivered this `sourceTs`, boundary resume skips it.
981
+ */
982
+ coalesceSourceTs?: string;
922
983
  }
923
984
 
924
985
  export type SendMessageFn = (
@@ -1183,6 +1244,28 @@ export interface ChannelStartArgs {
1183
1244
  /** Shared host services (MCP / GitHub / Slack). Same as {@link ChannelHandlerArgs.host}. */
1184
1245
  host: HostContext;
1185
1246
  waitUntil(promise: Promise<unknown>): void;
1247
+ /**
1248
+ * Whether a live session already exists for a continuation token on this
1249
+ * channel (includes lazy restore from storage).
1250
+ */
1251
+ hasContinuationSession(continuationToken: string): Promise<boolean>;
1252
+ /**
1253
+ * Whether a model turn is in flight (or being admitted) for the session
1254
+ * addressed by this continuation token.
1255
+ */
1256
+ isContinuationBusy(continuationToken: string): Promise<boolean>;
1257
+ /**
1258
+ * Slack warm-delta watermark: last agent reply ts recorded on the session's
1259
+ * channel state (`lastBotMessageTs`), when present.
1260
+ */
1261
+ getContinuationLastBotMessageTs(
1262
+ continuationToken: string
1263
+ ): Promise<string | undefined>;
1264
+ /**
1265
+ * Interrupt the in-flight turn for a continuation token and clear any
1266
+ * coalesced follow-ups queued behind it. Returns whether work was active.
1267
+ */
1268
+ interruptContinuation(continuationToken: string): Promise<boolean>;
1186
1269
  /**
1187
1270
  * Approve or deny a parked {@link ToolConfig.needsApproval} tool call
1188
1271
  * for a session on this channel. Channel packs (e.g. Slack Block Kit