shapewall 0.3.3 → 0.7.0

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 (157) hide show
  1. package/CHANGELOG.md +501 -0
  2. package/README.md +87 -4
  3. package/THIRD_PARTY.md +15 -0
  4. package/dist/agent/package-lock.json +594 -2
  5. package/dist/client/assets/BoardChat-WS8mAIW6.js +27 -0
  6. package/dist/client/assets/{BuzzDialog-Ix9qJvim.js → BuzzDialog-B-Mlr6wF.js} +1 -1
  7. package/dist/client/assets/ChecksPanel-Dx1ZLBR6.js +1 -0
  8. package/dist/client/assets/History-sXhmpcvs.js +1 -0
  9. package/dist/client/assets/LocalSettings-CtGzz9F3.js +1 -0
  10. package/dist/client/assets/{Management-NVFDtSZB.js → Management-CGHe8Wt9.js} +1 -1
  11. package/dist/client/assets/QuickConnect-DEk84E4A.js +1 -0
  12. package/dist/client/assets/{ShareDialog-By0ETg6n.js → ShareDialog-CGGx-AYK.js} +2 -2
  13. package/dist/client/assets/{app-update-dialog-RGIw24iZ.js → app-update-dialog-_bVp8hnC.js} +1 -1
  14. package/dist/client/assets/{circle-x-B6KfScOE.js → circle-x-BWoHA1QY.js} +1 -1
  15. package/dist/client/assets/index-Bm-Xd3YN.js +37 -0
  16. package/dist/client/assets/index-C7Ostk3K.css +2 -0
  17. package/dist/client/assets/play-BzQ3tRTv.js +1 -0
  18. package/dist/client/assets/{refresh-cw-vkgvCCfR.js → refresh-cw-BYBMyDKT.js} +1 -1
  19. package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
  20. package/dist/client/assets/ui-CAY6myRA.js +41 -0
  21. package/dist/client/build-info.json +1 -1
  22. package/dist/client/index.html +3 -3
  23. package/dist/client/sw.js +2 -2
  24. package/dist/console/assets/LocalAccountsApp-CWRguRRL.js +1 -0
  25. package/dist/console/assets/LocalConsole--I2SaIpr.js +2 -0
  26. package/dist/console/assets/LocalUsage-BmpCZ36k.js +1 -0
  27. package/dist/console/assets/app-update-dialog-B8M9plaT.js +1 -0
  28. package/dist/console/assets/app-version-muKrQqbL.js +1 -0
  29. package/dist/console/assets/console-CrhTsPbz.css +2 -0
  30. package/dist/console/assets/console-DxapQ9oS.js +11 -0
  31. package/dist/console/assets/local-agents-CoqD4dSN.js +6 -0
  32. package/dist/console/assets/minus-DDQ0ydcM.js +1 -0
  33. package/dist/console/assets/tailnet-C5Flwl_d.js +2 -0
  34. package/dist/console/assets/theme-DCd0qh4l.js +1 -0
  35. package/dist/console/assets/ui-Bry1mnCY.js +41 -0
  36. package/dist/console/assets/usage-CIPWnHaS.css +1 -0
  37. package/dist/console/assets/usage-CqVjMb7m.js +5 -0
  38. package/dist/console/assets/usage-D1G0GgE3.js +56 -0
  39. package/dist/console/build-info.json +1 -1
  40. package/dist/console/console.html +8 -2
  41. package/docs/agent-hardening.md +206 -0
  42. package/docs/agent-recipes.md +23 -0
  43. package/docs/agent-workflow.md +5 -0
  44. package/docs/agents.md +49 -6
  45. package/docs/architecture.md +10 -4
  46. package/docs/deployment.md +39 -17
  47. package/docs/design-system.md +56 -7
  48. package/docs/hosted.md +1800 -46
  49. package/docs/images/board-overview.png +0 -0
  50. package/docs/images/journeys.png +0 -0
  51. package/docs/images/live-comment.png +0 -0
  52. package/docs/images/phone-comment.png +0 -0
  53. package/docs/images/phone-overview.png +0 -0
  54. package/docs/images/variant.png +0 -0
  55. package/docs/where-to-run.md +119 -3
  56. package/package.json +6 -1
  57. package/src/agent/backends/acp.js +745 -44
  58. package/src/agent/branch-mode.js +17 -3
  59. package/src/agent/card-hash.js +65 -0
  60. package/src/agent/context.js +41 -0
  61. package/src/agent/exec-paths.js +105 -0
  62. package/src/agent/routes.js +23 -2
  63. package/src/agent/run.js +11 -1
  64. package/src/agent/starter/cli.js +6 -1
  65. package/src/agent/turn-changes.js +70 -8
  66. package/src/agent/usage-log.js +251 -0
  67. package/src/cli/args.js +37 -0
  68. package/src/cli/commands.js +20 -5
  69. package/src/cli/help.js +73 -3
  70. package/src/cli/init.js +2 -1
  71. package/src/cli/serve.js +20 -5
  72. package/src/cli.js +8 -0
  73. package/src/hosted/access-cli.js +99 -1
  74. package/src/hosted/activity-core.js +232 -0
  75. package/src/hosted/activity.js +375 -0
  76. package/src/hosted/admin.js +87 -13
  77. package/src/hosted/agent-approvals.js +29 -4
  78. package/src/hosted/agent-link.js +1032 -0
  79. package/src/hosted/agent-policy.js +10 -3
  80. package/src/hosted/agent-presence.js +63 -22
  81. package/src/hosted/agent-routes.js +135 -0
  82. package/src/hosted/agents.js +1206 -0
  83. package/src/hosted/app.js +178 -12
  84. package/src/hosted/auth-routes.js +156 -15
  85. package/src/hosted/auth.js +123 -20
  86. package/src/hosted/board-routes.js +129 -5
  87. package/src/hosted/chat-frames.js +246 -0
  88. package/src/hosted/chat-job.js +204 -0
  89. package/src/hosted/chat-permissions.js +203 -0
  90. package/src/hosted/chat-routes.js +1009 -0
  91. package/src/hosted/chat-store.js +477 -0
  92. package/src/hosted/comment-files.js +11 -1
  93. package/src/hosted/comments.js +1 -1
  94. package/src/hosted/console-routes.js +122 -34
  95. package/src/hosted/db.js +295 -0
  96. package/src/hosted/device-routes.js +88 -10
  97. package/src/hosted/device.js +54 -4
  98. package/src/hosted/events.js +3 -3
  99. package/src/hosted/guards.js +337 -27
  100. package/src/hosted/http.js +6 -3
  101. package/src/hosted/membership.js +22 -3
  102. package/src/hosted/operator-access.js +418 -0
  103. package/src/hosted/operator.js +106 -2
  104. package/src/hosted/ops-cli.js +238 -3
  105. package/src/hosted/pages.js +4 -0
  106. package/src/hosted/passkey-policy.js +75 -0
  107. package/src/hosted/permissions.js +52 -6
  108. package/src/hosted/presence.js +31 -2
  109. package/src/hosted/projects.js +30 -2
  110. package/src/hosted/public.js +4 -4
  111. package/src/hosted/ratelimit.js +14 -0
  112. package/src/hosted/retention.js +18 -2
  113. package/src/hosted/server.js +123 -11
  114. package/src/hosted/shares.js +2 -2
  115. package/src/hosted/step-up-settings.js +68 -0
  116. package/src/hosted/step-up.js +135 -0
  117. package/src/hosted/tailnet-identity.js +24 -0
  118. package/src/hosted/tailnet-routes.js +342 -0
  119. package/src/hosted/tailnet.js +220 -0
  120. package/src/hosted/tailscale.js +75 -9
  121. package/src/hosted/tenancy.js +66 -1
  122. package/src/hosted/token-api.js +119 -20
  123. package/src/hosted/token-events.js +5 -3
  124. package/src/hosted/tokens.js +29 -6
  125. package/src/hosted/turn-hub.js +152 -0
  126. package/src/hosted/usage-core.js +624 -0
  127. package/src/hosted/usage-layout.js +724 -0
  128. package/src/hosted/usage-routes.js +375 -0
  129. package/src/hosted/usage-shape.js +256 -0
  130. package/src/hosted/usage.js +641 -0
  131. package/src/remote/agent-connect.js +469 -0
  132. package/src/remote/agent-fix.js +611 -0
  133. package/src/remote/agent-job.js +216 -0
  134. package/src/remote/agent-link.js +906 -0
  135. package/src/remote/agent-policy-file.js +242 -0
  136. package/src/remote/credentials.js +35 -1
  137. package/src/remote/login.js +44 -9
  138. package/src/remote/share.js +1 -1
  139. package/src/remote/tailnet-cli.js +547 -0
  140. package/src/server/app.js +15 -0
  141. package/src/server/pairing-routes.js +34 -5
  142. package/src/server/tailscale.js +97 -4
  143. package/src/tailnet-proof.js +766 -0
  144. package/dist/client/assets/BoardChat-DxehB761.js +0 -27
  145. package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +0 -1
  146. package/dist/client/assets/History-C9N32kCv.js +0 -1
  147. package/dist/client/assets/LocalSettings-DawtKnvi.js +0 -1
  148. package/dist/client/assets/QuickConnect-DO1TjS1t.js +0 -1
  149. package/dist/client/assets/index-CsqLdqGx.css +0 -2
  150. package/dist/client/assets/index-DnZarJ3U.js +0 -37
  151. package/dist/client/assets/shield-check-QzwGhz0t.js +0 -1
  152. package/dist/client/assets/ui-DI-FGWWK.js +0 -41
  153. package/dist/console/assets/LocalAccountsApp-VOCZg8dg.js +0 -1
  154. package/dist/console/assets/LocalConsole-CMwAW-mF.js +0 -1
  155. package/dist/console/assets/app-update-dialog-Cw6UDqNP.js +0 -1
  156. package/dist/console/assets/console-DgfWmUEx.js +0 -55
  157. package/dist/console/assets/console-Lis0vvdL.css +0 -2
@@ -0,0 +1,477 @@
1
+ // Chat on a hosted board, in the database (migration 13): threads, their
2
+ // messages, and turns. Every query goes through the tenant handle `t`
3
+ // (src/hosted/tenancy.js).
4
+ //
5
+ // thread belongs to one board. "board" and "comment-<n>" are shared by
6
+ // the members of the workspace; a "chat-..." belongs to the person
7
+ // who started it and nobody else reads it. A thread names the
8
+ // agent it talks to (its "profile" on a local board).
9
+ // message the person's text (with what they mentioned and attached) or
10
+ // the agent's answer with its cards in their final state.
11
+ // turn one question on its way through an agent: queued, running, then
12
+ // done, stopped, error or interrupted. Its live events are in
13
+ // memory (src/hosted/turn-hub.js).
14
+ //
15
+ // A usage record (agent_runs) is written for every turn: who asked, which
16
+ // agent, where, when, how it ended, and the numbers the agent reported.
17
+ // Never message text.
18
+ //
19
+ // The pure thread rules (titles, who sees a thread, search) are the local
20
+ // board's: src/agent/thread-history.js.
21
+
22
+ import {
23
+ CHAT_RE,
24
+ THREAD_RE,
25
+ autoTitle,
26
+ newestFirst,
27
+ renameThread,
28
+ searchNeedle,
29
+ threadKind,
30
+ threadSummary,
31
+ } from '../agent/thread-history.js'
32
+ import { newId } from './crypto.js'
33
+ import { HttpError } from './http.js'
34
+
35
+ export const CHAT_LIMITS = {
36
+ // Per thread: the oldest messages go.
37
+ messages: 200,
38
+ // A member's own chats on one board (the shared threads are apart).
39
+ privateChats: 50,
40
+ // Threads read for a snippet in one search.
41
+ searchRead: 40,
42
+ text: 8000,
43
+ }
44
+ export const TURN_ENDED = ['done', 'stopped', 'error', 'interrupted']
45
+ export const INTERRUPTED_MESSAGE = "The agent's machine went away before it finished. Ask again."
46
+
47
+ const parse = (text) => {
48
+ try {
49
+ const value = JSON.parse(text)
50
+ return value && typeof value === 'object' ? value : {}
51
+ } catch {
52
+ return {}
53
+ }
54
+ }
55
+
56
+ export function createChatStore(t, { now = () => new Date() } = {}) {
57
+ const iso = () => now().toISOString()
58
+ const KEY = 'workspace_id = :ws AND project_id = :project AND board_id = :board AND thread_id = :thread'
59
+
60
+ // ---- Threads ----
61
+
62
+ const validThread = (threadId) => typeof threadId === 'string' && THREAD_RE.test(threadId)
63
+ const thread = (projectId, boardId, threadId) =>
64
+ (validThread(threadId) && t.get(`SELECT * FROM chat_threads WHERE ${KEY}`, { project: projectId, board: boardId, thread: threadId })) ||
65
+ null
66
+ // Who may read a thread: the shared ones every member; a chat its owner
67
+ // (a chat nobody wrote in yet has no row and is anyone's to start).
68
+ const visible = (row, threadId, userId) => !CHAT_RE.test(threadId) || !row || !row.owner_user_id || row.owner_user_id === userId
69
+ // The thread's row, made on its first write. A chat belongs to `userId`.
70
+ // Returns { row, created }.
71
+ function ensure(projectId, boardId, threadId, { userId, agentId = null }) {
72
+ const found = thread(projectId, boardId, threadId)
73
+ if (found) return { row: found, created: false }
74
+ // A chat this person opened and never wrote in is forgotten after an
75
+ // hour, so opening chats never fills the board's allowance.
76
+ if (CHAT_RE.test(threadId) && userId)
77
+ t.run(
78
+ `DELETE FROM chat_threads WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND owner_user_id = :user
79
+ AND created_at < :cutoff AND NOT EXISTS (SELECT 1 FROM chat_messages m WHERE m.workspace_id = chat_threads.workspace_id
80
+ AND m.project_id = chat_threads.project_id AND m.board_id = chat_threads.board_id AND m.thread_id = chat_threads.thread_id)
81
+ AND NOT EXISTS (SELECT 1 FROM chat_turns n WHERE n.workspace_id = chat_threads.workspace_id
82
+ AND n.project_id = chat_threads.project_id AND n.board_id = chat_threads.board_id AND n.thread_id = chat_threads.thread_id)`,
83
+ { project: projectId, board: boardId, user: userId, cutoff: new Date(now().getTime() - 60 * 60_000).toISOString() },
84
+ )
85
+ // A member's own chats on a board are capped apart from the shared
86
+ // threads (the board's and one per comment), so one person cannot use
87
+ // up what everyone shares.
88
+ if (CHAT_RE.test(threadId)) {
89
+ const mine = t.get(
90
+ "SELECT COUNT(*) AS n FROM chat_threads WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND owner_user_id = :user AND thread_id LIKE 'chat-%'",
91
+ { project: projectId, board: boardId, user: userId || '' },
92
+ ).n
93
+ if (mine >= CHAT_LIMITS.privateChats)
94
+ throw new HttpError(
95
+ 409,
96
+ 'too_many_threads',
97
+ `You have ${CHAT_LIMITS.privateChats} chats of your own on this board. Delete some from the chat history first.`,
98
+ )
99
+ }
100
+ const at = iso()
101
+ t.run(
102
+ `INSERT INTO chat_threads (workspace_id, project_id, board_id, thread_id, agent_id, owner_user_id, created_by, created_at, updated_at)
103
+ VALUES (:ws, :project, :board, :thread, :agent, :owner, :user, :at, :at)`,
104
+ {
105
+ project: projectId,
106
+ board: boardId,
107
+ thread: threadId,
108
+ agent: agentId,
109
+ owner: CHAT_RE.test(threadId) ? userId : null,
110
+ user: userId,
111
+ at,
112
+ },
113
+ )
114
+ return { row: thread(projectId, boardId, threadId), created: true }
115
+ }
116
+ function setAgent(projectId, boardId, threadId, agentId) {
117
+ t.run(`UPDATE chat_threads SET agent_id = :agent WHERE ${KEY}`, {
118
+ project: projectId,
119
+ board: boardId,
120
+ thread: threadId,
121
+ agent: agentId,
122
+ })
123
+ }
124
+ function change(projectId, boardId, threadId, { title, archived }) {
125
+ const row = thread(projectId, boardId, threadId)
126
+ if (!row) throw new HttpError(404, 'unknown_thread', 'No such chat.')
127
+ if (title !== undefined) {
128
+ const shape = { id: threadId, messages: messages(projectId, boardId, threadId), title: row.title, titleSource: row.title_source }
129
+ renameThread(shape, title)
130
+ t.run(`UPDATE chat_threads SET title = :title, title_source = :source WHERE ${KEY}`, {
131
+ project: projectId,
132
+ board: boardId,
133
+ thread: threadId,
134
+ title: shape.title || null,
135
+ source: shape.titleSource || null,
136
+ })
137
+ }
138
+ if (archived !== undefined)
139
+ t.run(`UPDATE chat_threads SET archived = :archived WHERE ${KEY}`, {
140
+ project: projectId,
141
+ board: boardId,
142
+ thread: threadId,
143
+ archived: archived ? 1 : 0,
144
+ })
145
+ return thread(projectId, boardId, threadId)
146
+ }
147
+ // The thread and everything in it. Its usage records stay (they are history).
148
+ function remove(projectId, boardId, threadId) {
149
+ const key = { project: projectId, board: boardId, thread: threadId }
150
+ t.run(`DELETE FROM chat_permissions WHERE ${KEY}`, key)
151
+ t.run(`DELETE FROM chat_messages WHERE ${KEY}`, key)
152
+ t.run(`DELETE FROM chat_turns WHERE ${KEY}`, key)
153
+ t.run(`DELETE FROM chat_threads WHERE ${KEY}`, key)
154
+ }
155
+
156
+ // ---- Messages ----
157
+
158
+ const view = (row) => ({ ...parse(row.body), id: row.id, role: row.role, createdAt: row.created_at })
159
+ const rows = (projectId, boardId, threadId) =>
160
+ t.all(`SELECT * FROM chat_messages WHERE ${KEY} ORDER BY created_at, rowid`, { project: projectId, board: boardId, thread: threadId })
161
+ const messages = (projectId, boardId, threadId) => rows(projectId, boardId, threadId).map(view)
162
+ function append(projectId, boardId, threadId, { role, body, userId = null, agentId = null, turnId = null }) {
163
+ const id = newId('msg')
164
+ const text = JSON.stringify(body)
165
+ const at = iso()
166
+ t.run(
167
+ `INSERT INTO chat_messages (workspace_id, project_id, board_id, thread_id, id, role, author_user_id, agent_id, turn_id, body, bytes, created_at)
168
+ VALUES (:ws, :project, :board, :thread, :id, :role, :user, :agent, :turn, :body, :bytes, :at)`,
169
+ {
170
+ project: projectId,
171
+ board: boardId,
172
+ thread: threadId,
173
+ id,
174
+ role,
175
+ user: userId,
176
+ agent: agentId,
177
+ turn: turnId,
178
+ body: text,
179
+ bytes: Buffer.byteLength(text),
180
+ at,
181
+ },
182
+ )
183
+ // The oldest messages make room.
184
+ const extra = t.get(`SELECT COUNT(*) AS n FROM chat_messages WHERE ${KEY}`, { project: projectId, board: boardId, thread: threadId }).n
185
+ if (extra > CHAT_LIMITS.messages)
186
+ t.run(
187
+ `DELETE FROM chat_messages WHERE ${KEY} AND id IN (SELECT id FROM chat_messages WHERE ${KEY} ORDER BY created_at, rowid LIMIT :n)`,
188
+ { project: projectId, board: boardId, thread: threadId, n: extra - CHAT_LIMITS.messages },
189
+ )
190
+ const row = thread(projectId, boardId, threadId)
191
+ if (row && role === 'user' && !row.title && row.title_source !== 'user') {
192
+ const title = autoTitle(body.text)
193
+ if (title)
194
+ t.run(`UPDATE chat_threads SET title = :title, title_source = 'auto' WHERE ${KEY}`, {
195
+ project: projectId,
196
+ board: boardId,
197
+ thread: threadId,
198
+ title,
199
+ })
200
+ }
201
+ t.run(`UPDATE chat_threads SET updated_at = :at WHERE ${KEY}`, { project: projectId, board: boardId, thread: threadId, at })
202
+ return { ...body, id, role, createdAt: at }
203
+ }
204
+
205
+ // Every thread of the board this person may read, newest first, as the
206
+ // chat history lists them (thread-history.js threadSummary). `query`
207
+ // searches titles and messages.
208
+ function list(projectId, boardId, { userId, query = '' }) {
209
+ const needle = searchNeedle(query)
210
+ const key = { project: projectId, board: boardId }
211
+ // Titles, counts and last activity only: no message is loaded for the list.
212
+ const all = t
213
+ .all(
214
+ `SELECT c.thread_id, c.agent_id, c.owner_user_id, c.title, c.archived, c.created_at,
215
+ (SELECT COUNT(*) FROM chat_messages m WHERE m.workspace_id = c.workspace_id AND m.project_id = c.project_id
216
+ AND m.board_id = c.board_id AND m.thread_id = c.thread_id) AS n,
217
+ (SELECT MAX(m.created_at) FROM chat_messages m WHERE m.workspace_id = c.workspace_id AND m.project_id = c.project_id
218
+ AND m.board_id = c.board_id AND m.thread_id = c.thread_id) AS last_at
219
+ FROM chat_threads c WHERE c.workspace_id = :ws AND c.project_id = :project AND c.board_id = :board ORDER BY c.updated_at DESC`,
220
+ key,
221
+ )
222
+ .filter((row) => row.n > 0 && visible(row, row.thread_id, userId))
223
+ const entry = (row, match = null) => {
224
+ const kind = threadKind(row.thread_id)
225
+ return {
226
+ id: row.thread_id,
227
+ kind,
228
+ ...(kind === 'comment' ? { commentId: Number(row.thread_id.slice(8)) } : {}),
229
+ title: row.title || null,
230
+ count: row.n,
231
+ createdAt: row.created_at,
232
+ updatedAt: row.last_at,
233
+ archived: row.archived === 1,
234
+ shared: kind !== 'chat',
235
+ ...(match ? { match } : {}),
236
+ agentId: row.agent_id || null,
237
+ }
238
+ }
239
+ if (!needle) return all.map((row) => entry(row)).sort(newestFirst)
240
+ // A search: titles first; then only the threads whose stored text holds
241
+ // the words are read for a snippet (a bounded number of them).
242
+ const like = `%${needle.replace(/[\\%_]/g, (char) => `\\${char}`)}%`
243
+ const holding = new Set(
244
+ t
245
+ .all(
246
+ "SELECT DISTINCT thread_id FROM chat_messages WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND lower(body) LIKE :like ESCAPE '\\'",
247
+ { ...key, like },
248
+ )
249
+ .map((row) => row.thread_id),
250
+ )
251
+ const out = []
252
+ let read = 0
253
+ for (const row of all) {
254
+ if (
255
+ String(row.title || '')
256
+ .toLowerCase()
257
+ .includes(needle)
258
+ ) {
259
+ out.push(entry(row))
260
+ continue
261
+ }
262
+ if (!holding.has(row.thread_id) || read >= CHAT_LIMITS.searchRead) continue
263
+ read += 1
264
+ const summary = threadSummary(
265
+ { id: row.thread_id, messages: messages(projectId, boardId, row.thread_id), title: row.title || undefined, branches: [] },
266
+ needle,
267
+ )
268
+ if (summary?.match) out.push(entry(row, summary.match))
269
+ }
270
+ return out.sort(newestFirst)
271
+ }
272
+
273
+ // ---- Turns ----
274
+
275
+ const turn = (turnId) => t.get('SELECT * FROM chat_turns WHERE workspace_id = :ws AND id = :id', { id: String(turnId || '') }) || null
276
+ // `kind`: "chat", "comment" (a comment sent to the agent, which it answers)
277
+ // or "fix" (Fix with agent: the server took the comment in the agent's
278
+ // name, `claimId`, and the agent may ask to edit).
279
+ function createTurn({
280
+ projectId,
281
+ boardId,
282
+ threadId,
283
+ agentId,
284
+ kind = 'chat',
285
+ userId = null,
286
+ userMessageId = null,
287
+ commentNumber = null,
288
+ claimId = null,
289
+ }) {
290
+ const id = newId('turn')
291
+ const runId = newId('run')
292
+ const at = iso()
293
+ t.run(
294
+ `INSERT INTO chat_turns (workspace_id, project_id, id, board_id, thread_id, agent_id, kind, state, started_by, user_message_id, run_id,
295
+ comment_number, created_at)
296
+ VALUES (:ws, :project, :id, :board, :thread, :agent, :kind, 'queued', :user, :message, :run, :number, :at)`,
297
+ {
298
+ project: projectId,
299
+ id,
300
+ board: boardId,
301
+ thread: threadId,
302
+ agent: agentId,
303
+ kind,
304
+ user: userId,
305
+ message: userMessageId,
306
+ run: runId,
307
+ number: commentNumber,
308
+ at,
309
+ },
310
+ )
311
+ // The usage record: who asked which agent, where and when. No text.
312
+ // `agent_owner`: the person who connected the agent (they may read
313
+ // everything that ran on their machine, src/hosted/usage.js).
314
+ t.run(
315
+ `INSERT INTO agent_runs (workspace_id, project_id, id, kind, agent_id, claim_id, board_id, comment_number, thread_id, asked_by, started_at,
316
+ agent_owner)
317
+ VALUES (:ws, :project, :id, :kind, :agent, :claim, :board, :number, :thread, :asked, :at,
318
+ (SELECT a.created_by FROM agents a WHERE a.id = :agent))`,
319
+ {
320
+ project: projectId,
321
+ id: runId,
322
+ claim: claimId,
323
+ kind: kind === 'fix' ? 'fix' : kind === 'push' ? 'push' : kind === 'comment' ? 'review' : 'chat',
324
+ agent: agentId,
325
+ board: boardId,
326
+ number: commentNumber,
327
+ thread: threadId,
328
+ asked: userId,
329
+ at,
330
+ },
331
+ )
332
+ return turn(id)
333
+ }
334
+ const openTurn = (projectId, boardId, threadId) =>
335
+ t.get(`SELECT * FROM chat_turns WHERE ${KEY} AND state IN ('queued', 'running') ORDER BY created_at DESC LIMIT 1`, {
336
+ project: projectId,
337
+ board: boardId,
338
+ thread: threadId,
339
+ }) || null
340
+ const openTurnsOf = (agentId) =>
341
+ t.all(
342
+ "SELECT * FROM chat_turns WHERE workspace_id = :ws AND agent_id = :agent AND state IN ('queued', 'running') ORDER BY created_at",
343
+ {
344
+ agent: agentId,
345
+ },
346
+ )
347
+ // Today's turns of a project, from the usage records: they stay when a
348
+ // thread is deleted, so deleting one never gives turns back.
349
+ const turnsToday = (projectId) =>
350
+ t.get(
351
+ "SELECT COUNT(*) AS n FROM agent_runs WHERE workspace_id = :ws AND project_id = :project AND kind IN ('chat', 'review') AND started_at >= :start",
352
+ { project: projectId, start: `${iso().slice(0, 10)}T00:00:00.000Z` },
353
+ ).n
354
+ // Every turn the database still calls open (after a restart: none is).
355
+ const openTurns = () => t.all("SELECT * FROM chat_turns WHERE workspace_id = :ws AND state IN ('queued', 'running') ORDER BY created_at")
356
+ function setRunning(turnId) {
357
+ t.run("UPDATE chat_turns SET state = 'running' WHERE workspace_id = :ws AND id = :id AND state = 'queued'", { id: turnId })
358
+ }
359
+ function ack(turnId, seq) {
360
+ t.run('UPDATE chat_turns SET acked_seq = :seq WHERE workspace_id = :ws AND id = :id', { id: turnId, seq })
361
+ }
362
+ // Ends a turn once (a second end changes nothing): its state, and its
363
+ // usage record with what the agent reported. Returns whether it ended now.
364
+ function endTurn(row, state, usage = {}) {
365
+ const at = iso()
366
+ const result = t.run(
367
+ "UPDATE chat_turns SET state = :state, ended_at = :at WHERE workspace_id = :ws AND id = :id AND state IN ('queued', 'running')",
368
+ { id: row.id, state, at },
369
+ )
370
+ if (!Number(result.changes)) return false
371
+ const started = t.get('SELECT started_at FROM agent_runs WHERE workspace_id = :ws AND id = :run', { run: row.run_id })?.started_at
372
+ const tools = usage.tools && Object.keys(usage.tools).length ? JSON.stringify(usage.tools) : null
373
+ const by = usage.approvals?.by && Object.keys(usage.approvals.by).length ? JSON.stringify(usage.approvals.by) : null
374
+ t.run(
375
+ `UPDATE agent_runs SET ended_at = :at, outcome = :outcome, model = :model, effort = :effort, input_tokens = :input,
376
+ output_tokens = :output, cached_tokens = :cached, cost_micros = :cost, steps = :steps,
377
+ approvals_asked = :asked, approvals_allowed = :allowed, approvals_denied = :denied, approvals_auto = :auto,
378
+ approvals_by = :by, duration_ms = :duration, agent_ms = :agent_ms, tools = :tools, window_used = :window_used,
379
+ window_size = :window_size
380
+ WHERE workspace_id = :ws AND id = :run AND ended_at IS NULL`,
381
+ {
382
+ run: row.run_id,
383
+ at,
384
+ outcome: usage.outcome || state,
385
+ model: usage.model ?? null,
386
+ effort: usage.effort ?? null,
387
+ input: usage.inputTokens ?? null,
388
+ output: usage.outputTokens ?? null,
389
+ cached: usage.cachedTokens ?? null,
390
+ cost: usage.costMicros ?? null,
391
+ steps: usage.steps ?? null,
392
+ asked: usage.approvals?.asked ?? null,
393
+ allowed: usage.approvals?.allowed ?? null,
394
+ denied: usage.approvals?.denied ?? null,
395
+ auto: usage.approvals?.auto ?? null,
396
+ by,
397
+ // The server's own clock, from the question to the end of the turn.
398
+ duration: started ? Math.max(0, Date.parse(at) - Date.parse(started)) : null,
399
+ agent_ms: usage.agentMs ?? null,
400
+ tools,
401
+ window_used: usage.contextUsed ?? null,
402
+ window_size: usage.contextSize ?? null,
403
+ },
404
+ )
405
+ return true
406
+ }
407
+ // A turn that ended without an answer from the machine (it went away, the
408
+ // server restarted, its agent was revoked): the thread says so.
409
+ function interrupt(row, { message = INTERRUPTED_MESSAGE, agentName = null, state = 'interrupted', partial = null } = {}) {
410
+ if (!endTurn(row, state, { outcome: state })) return null
411
+ return append(row.project_id, row.board_id, row.thread_id, {
412
+ role: 'assistant',
413
+ agentId: row.agent_id,
414
+ turnId: row.id,
415
+ body: {
416
+ text: partial?.text || '',
417
+ cards: partial?.cards || [],
418
+ status: state === 'stopped' ? 'stopped' : 'error',
419
+ ...(state === 'stopped' ? {} : { error: { code: 'interrupted', message } }),
420
+ ...(agentName ? { agent: { id: row.agent_id, name: agentName } } : {}),
421
+ },
422
+ })
423
+ }
424
+
425
+ // The shared threads of a board (the board's and its comments') with
426
+ // their messages, for a workspace export. A member's own chats are theirs:
427
+ // only how many there are.
428
+ function shared(projectId, boardId) {
429
+ const all = t.all(
430
+ 'SELECT thread_id, agent_id, owner_user_id, title, created_at, updated_at FROM chat_threads WHERE workspace_id = :ws AND project_id = :project AND board_id = :board ORDER BY created_at, thread_id',
431
+ { project: projectId, board: boardId },
432
+ )
433
+ const threads = all
434
+ .filter((row) => !CHAT_RE.test(row.thread_id))
435
+ .map((row) => ({
436
+ id: row.thread_id,
437
+ title: row.title || null,
438
+ agentId: row.agent_id || null,
439
+ createdAt: row.created_at,
440
+ updatedAt: row.updated_at,
441
+ messages: messages(projectId, boardId, row.thread_id),
442
+ }))
443
+ .filter((thread) => thread.messages.length)
444
+ return { threads, privateChats: all.filter((row) => CHAT_RE.test(row.thread_id)).length }
445
+ }
446
+
447
+ // Bytes of every stored chat message of the workspace, for the storage quota.
448
+ const bytes = () =>
449
+ t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM chat_messages WHERE workspace_id = :ws').n +
450
+ t.get('SELECT COALESCE(SUM(length(card)), 0) AS n FROM chat_permissions WHERE workspace_id = :ws').n
451
+
452
+ return {
453
+ validThread,
454
+ thread,
455
+ visible,
456
+ ensure,
457
+ setAgent,
458
+ change,
459
+ remove,
460
+ messages,
461
+ append,
462
+ list,
463
+ turn,
464
+ createTurn,
465
+ openTurn,
466
+ openTurnsOf,
467
+ openTurns,
468
+ turnsToday,
469
+ setRunning,
470
+ ack,
471
+ endTurn,
472
+ interrupt,
473
+ shared,
474
+ bytes,
475
+ kindOf: threadKind,
476
+ }
477
+ }
@@ -11,6 +11,7 @@
11
11
  // copy step. They are small and capped (4 per comment or reply, 5 MB each,
12
12
  // the workspace storage quota over all of them).
13
13
 
14
+ import { USAGE_ROW_BYTES } from './usage-shape.js'
14
15
  import { COMMENT_FILE_RE, CommentError, readImages } from '../comment-fields.js'
15
16
  import { cleanCommentImage, newImageFile, renderMarkup } from '../comment-media.js'
16
17
  import { allWorkspaces, tenant } from './tenancy.js'
@@ -37,8 +38,17 @@ export async function cleanImages(cleaner, inputs) {
37
38
  }
38
39
 
39
40
  // Bytes of every comment image the workspace holds (attached or staged).
41
+ // Stored chat messages (src/hosted/chat-store.js) and usage records count with them: every
42
+ // caller adds this to the boards for the workspace's storage.
40
43
  export function commentFileBytes(t) {
41
- return t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM comment_files WHERE workspace_id = :ws').n
44
+ return (
45
+ t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM comment_files WHERE workspace_id = :ws').n +
46
+ t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM chat_messages WHERE workspace_id = :ws').n +
47
+ // The permission cards of connected agents (their diffs): src/hosted/chat-permissions.js.
48
+ t.get('SELECT COALESCE(SUM(length(card)), 0) AS n FROM chat_permissions WHERE workspace_id = :ws').n +
49
+ // Usage records (src/hosted/usage.js): small rows, counted at a fixed size each.
50
+ t.get('SELECT COUNT(*) AS n FROM agent_runs WHERE workspace_id = :ws').n * USAGE_ROW_BYTES
51
+ )
42
52
  }
43
53
 
44
54
  // Everything the storage quota counts: pushed boards, open pushes and
@@ -268,7 +268,7 @@ export function createCommentStore(t, projectId, boardId) {
268
268
  const body = changes.body !== undefined ? cleanCommentBody(changes.body, { media: hasCommentMedia(before) }) : undefined
269
269
  if (body !== undefined && body !== row.body) {
270
270
  t.run(
271
- `UPDATE comments SET dispatch_hash = NULL, dispatch_by = NULL, dispatch_at = NULL, run_approved = 0 WHERE ${SCOPE} AND number = :number`,
271
+ `UPDATE comments SET dispatch_hash = NULL, dispatch_by = NULL, dispatch_at = NULL, run_approved = 0, agent_id = NULL WHERE ${SCOPE} AND number = :number`,
272
272
  { ...key, number: row.number },
273
273
  )
274
274
  row.body = body