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
@@ -15,8 +15,15 @@
15
15
  // An occupied port is never taken over, even if its target happens to match.
16
16
 
17
17
  import { execFile } from 'node:child_process'
18
+ import { SECRET_PREFIX, machineFacts, selfFromStatus, serveProblem, serveTarget, tailscaledProblem, whoisAnswer } from '../tailnet-proof.js'
19
+
20
+ export { reachesLocalPort, serveProblem } from '../tailnet-proof.js'
18
21
 
19
22
  export class TailscaleError extends Error {}
23
+ // Tailnet sign-in cannot be switched on with this tailscaled (the message says why).
24
+ export class TailnetSignInRefused extends Error {}
25
+ const NO_SELF =
26
+ 'tailscale status does not say which node this machine is (its id and own tailnet addresses), so a request from this machine itself could not be told apart'
20
27
 
21
28
  // Funnel only publishes these HTTPS ports.
22
29
  export const FUNNEL_PORTS = [443, 8443, 10000]
@@ -83,7 +90,7 @@ export async function tailscaleStatus({ run = runCommand } = {}) {
83
90
  throw new TailscaleError(
84
91
  `HTTPS certificates are off for this tailnet, so ${dnsName} cannot serve https. Turn on "HTTPS Certificates" in the admin console, DNS page.`,
85
92
  )
86
- return { dnsName: dnsName.toLowerCase(), version: status.Version || null }
93
+ return { dnsName: dnsName.toLowerCase(), version: status.Version || null, status }
87
94
  }
88
95
 
89
96
  // What `tailscale serve` already does on `port`: null (free), or the local
@@ -109,17 +116,29 @@ export async function servedOn(port, { run = runCommand, dnsName } = {}) {
109
116
 
110
117
  const localTarget = (port) => `http://127.0.0.1:${port}`
111
118
 
112
- async function claim(kind, httpsPort, localPort, { run, dnsName }) {
119
+ // A target as an error message may show it: never the secret path of a serve entry.
120
+ const shown = (target) => {
121
+ const at = String(target).indexOf(SECRET_PREFIX)
122
+ return at < 0 ? target : `${String(target).slice(0, at + SECRET_PREFIX.length)}...`
123
+ }
124
+
125
+ async function claim(kind, httpsPort, localPort, { run, dnsName, secret = null }) {
113
126
  const current = await servedOn(httpsPort, { run, dnsName })
114
- const target = localTarget(localPort)
127
+ // With tailnet sign-in the entry points at a secret path (src/tailnet-proof.js).
128
+ const target = secret ? serveTarget(localPort, secret) : localTarget(localPort)
115
129
  if (current) {
116
130
  throw new TailscaleError(
117
- `Tailscale already serves port ${httpsPort} on this machine (${current.targets.join(', ') || 'another service'}${current.funnel ? ', public through Funnel' : ''}). ` +
131
+ `Tailscale already serves port ${httpsPort} on this machine (${current.targets.map(shown).join(', ') || 'another service'}${current.funnel ? ', public through Funnel' : ''}). ` +
118
132
  `Pick another port (${kind === 'funnel' ? `--funnel-port, one of ${FUNNEL_PORTS.join(', ')}` : '--tailscale-port 8443'}) or remove it first: tailscale ${kind} --https=${httpsPort} off`,
119
133
  )
120
134
  }
121
135
  const result = await run('tailscale', [kind, '--bg', `--https=${httpsPort}`, target])
122
- if (result.code !== 0) throw explain(result, `tailscale ${kind} --https=${httpsPort}`)
136
+ if (result.code !== 0) {
137
+ const error = explain(result, `tailscale ${kind} --https=${httpsPort}`)
138
+ // tailscale may quote its arguments back: the secret never reaches a log.
139
+ if (secret) error.message = error.message.split(secret).join('...')
140
+ throw error
141
+ }
123
142
  return async () => {
124
143
  const active = await servedOn(httpsPort, { run, dnsName })
125
144
  if (!active) return true
@@ -131,8 +150,19 @@ async function claim(kind, httpsPort, localPort, { run, dnsName }) {
131
150
 
132
151
  // Puts the server on the tailnet. Returns { origin, shareOrigin, stop }:
133
152
  // `stop()` removes what was added.
134
- export async function startTailscale({ localPort, httpsPort = 443, funnel = null, run = runCommand }) {
135
- const { dnsName } = await tailscaleStatus({ run })
153
+ // `secret`: tailnet sign-in; the serve entry then carries it, and `verify`
154
+ // and `whois` are what the identity proof asks while the server runs.
155
+ // `facts`: what the machine says about tailscaled (tests).
156
+ export async function startTailscale({ localPort, httpsPort = 443, funnel = null, run = runCommand, secret = null, facts = machineFacts }) {
157
+ const { dnsName, status } = await tailscaleStatus({ run })
158
+ // This node (its stable id and own tailnet addresses): an identity that comes from it is never honoured.
159
+ let here = selfFromStatus(status)
160
+ if (secret) {
161
+ // Before anything is put on the tailnet.
162
+ const problem = tailscaledProblem(status, await facts())
163
+ if (problem) throw new TailnetSignInRefused(problem)
164
+ if (!here) throw new TailnetSignInRefused(NO_SELF)
165
+ }
136
166
  if (!Number.isInteger(httpsPort) || httpsPort < 1 || httpsPort > 65535) throw new TailscaleError('--tailscale-port: a port number')
137
167
  if (funnel) {
138
168
  if (!FUNNEL_PORTS.includes(funnel.httpsPort))
@@ -147,13 +177,47 @@ export async function startTailscale({ localPort, httpsPort = 443, funnel = null
147
177
  return results.every(Boolean)
148
178
  }
149
179
  try {
150
- undo.push(await claim('serve', httpsPort, localPort, { run, dnsName }))
180
+ undo.push(await claim('serve', httpsPort, localPort, { run, dnsName, secret }))
151
181
  let shareOrigin = null
152
182
  if (funnel) {
153
183
  undo.push(await claim('funnel', funnel.httpsPort, funnel.localPort, { run, dnsName }))
154
184
  shareOrigin = originFor(dnsName, funnel.httpsPort)
155
185
  }
156
- return { origin: originFor(dnsName, httpsPort), shareOrigin, dnsName, stop }
186
+ // For tailnet sign-in: why the identity headers on this server's port
187
+ // cannot be believed right now, or null (see serveProblem).
188
+ const verify = async () => {
189
+ const result = await run('tailscale', ['serve', 'status', '--json'])
190
+ if (result.missing || result.code !== 0) return 'tailscale serve status could not be read'
191
+ let problem
192
+ try {
193
+ problem = serveProblem(JSON.parse(result.stdout || '{}') || {}, {
194
+ dnsName,
195
+ httpsPort,
196
+ localPort,
197
+ ...(secret ? { target: serveTarget(localPort, secret) } : {}),
198
+ })
199
+ } catch {
200
+ return 'tailscale serve status did not answer JSON'
201
+ }
202
+ if (problem || !secret) return problem
203
+ // And tailscaled itself: still one whose connections can be believed.
204
+ const now = await run('tailscale', ['status', '--json'])
205
+ if (now.missing || now.code !== 0) return 'tailscale status could not be read'
206
+ try {
207
+ const status = JSON.parse(now.stdout)
208
+ here = selfFromStatus(status)
209
+ return tailscaledProblem(status, await facts()) || (here ? null : NO_SELF)
210
+ } catch {
211
+ here = null
212
+ return 'tailscale status did not answer JSON'
213
+ }
214
+ }
215
+ // tailscaled's own word on who owns a tailnet address (LocalAPI whois, through the CLI).
216
+ const whois = async (address) => {
217
+ const result = await run('tailscale', ['whois', '--json', address])
218
+ return result.missing || result.code !== 0 ? null : whoisAnswer(result.stdout)
219
+ }
220
+ return { origin: originFor(dnsName, httpsPort), shareOrigin, dnsName, stop, verify, whois, self: () => here }
157
221
  } catch (error) {
158
222
  await stop()
159
223
  throw error
@@ -199,6 +263,8 @@ export function shareOnly(app) {
199
263
  .filter((part) => !/^\s*(__Host-)?sw_(session|ceremony)=/.test(part))
200
264
  .join(';')
201
265
  }
266
+ // Funnel traffic never carries a tailnet identity; whatever claims one is dropped here too.
267
+ for (const name of Object.keys(req.headers)) if (name.startsWith('tailscale-')) delete req.headers[name]
202
268
  return app(req, res)
203
269
  }
204
270
  }
@@ -30,6 +30,14 @@ export const TENANT_TABLES = [
30
30
  'import_batches',
31
31
  'comment_imports',
32
32
  'comment_files',
33
+ 'agents',
34
+ 'agent_runs',
35
+ 'chat_threads',
36
+ 'chat_messages',
37
+ 'chat_turns',
38
+ 'chat_permissions',
39
+ 'saved_views',
40
+ 'usage_layouts',
33
41
  ]
34
42
  export { ROLES, roleAtLeast } from './permissions.js'
35
43
 
@@ -115,6 +123,18 @@ export function tenant(db, workspaceId) {
115
123
 
116
124
  // ---- Entry points (global lookups that yield a workspace) ----------------------
117
125
 
126
+ // The workspaces that hold a chat turn the database still calls open. At
127
+ // server start none is really running (live turns are memory): the caller
128
+ // ends each one, through its own tenant handle (src/hosted/agent-link.js).
129
+ export function workspacesWithOpenTurns(db) {
130
+ const has = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'chat_turns'").get()
131
+ if (!has) return []
132
+ return db
133
+ .prepare("SELECT DISTINCT workspace_id FROM chat_turns WHERE state IN ('queued', 'running')")
134
+ .all()
135
+ .map((row) => row.workspace_id)
136
+ }
137
+
118
138
  // The token row for a SHA-256 hash, if it is live: not revoked, not
119
139
  // expired, and its maker is still a member who may hold tokens (an admin or
120
140
  // owner here; tokenCreatorOk). Callers then scope by its workspace_id.
@@ -194,6 +214,17 @@ export function findInviteByHash(db, tokenHash) {
194
214
  )
195
215
  }
196
216
 
217
+ // The invites made for one tailnet login (src/hosted/tailnet.js), in any
218
+ // workspace, with what inviteUsable needs. The caller keeps the usable ones.
219
+ export function findInvitesForTailnetLogin(db, login) {
220
+ return prepare(
221
+ db,
222
+ `SELECT i.*, w.name AS workspace_name, m.role AS creator_role FROM invites i JOIN workspaces w ON w.id = i.workspace_id
223
+ LEFT JOIN memberships m ON m.workspace_id = i.workspace_id AND m.user_id = i.created_by
224
+ WHERE i.tailnet_login = :login AND i.used_at IS NULL AND i.revoked_at IS NULL ORDER BY i.created_at`,
225
+ ).all({ login })
226
+ }
227
+
197
228
  // Whether an invite row (from findInviteByHash) still works: unused, not
198
229
  // revoked, not expired, and its creator can still invite to that role (an
199
230
  // admin up to admin, an owner for owners). A removed or demoted admin's
@@ -225,7 +256,7 @@ export function roleIn(db, userId, workspaceId) {
225
256
  export function allWorkspaces(db) {
226
257
  return prepare(
227
258
  db,
228
- 'SELECT id, name, created_at, created_by, share_links, suspended_at, suspended_reason, retention_versions FROM workspaces ORDER BY created_at, id',
259
+ 'SELECT id, name, created_at, created_by, share_links, suspended_at, suspended_reason, retention_versions, retention_usage_days FROM workspaces ORDER BY created_at, id',
229
260
  ).all()
230
261
  }
231
262
 
@@ -283,6 +314,40 @@ export function seenDevice(db, userId, device) {
283
314
  )
284
315
  }
285
316
 
317
+ // The account events of a workspace's members (sign-ins, passkeys, sessions:
318
+ // rows with no workspace), for the workspace's Activity page: only of people
319
+ // who are members now, only since each joined, and only the actions listed
320
+ // (src/hosted/usage-shape.js ACCOUNT_ACTIONS). Newest first, `before` an
321
+ // entry id. `userId`: one member (a person reading their own activity).
322
+ export function memberAccountEvents(
323
+ db,
324
+ workspaceId,
325
+ { userId = null, actions, from = null, to = null, q = null, before = Number.MAX_SAFE_INTEGER, limit = 50 },
326
+ ) {
327
+ if (!ID_PATTERNS.workspace.test(String(workspaceId || ''))) return []
328
+ return prepare(
329
+ db,
330
+ `SELECT a.id, a.action, a.target, a.ip, a.detail, a.at, a.user_id, u.handle, u.display_name
331
+ FROM memberships m JOIN audit_log a ON a.user_id = m.user_id AND a.workspace_id IS NULL JOIN users u ON u.id = a.user_id
332
+ WHERE m.workspace_id = :ws AND a.at >= m.created_at AND a.id < :before
333
+ AND a.action IN (SELECT value FROM json_each(:actions))
334
+ AND (:user IS NULL OR a.user_id = :user) AND (:from IS NULL OR a.at >= :from) AND (:to IS NULL OR a.at < :to)
335
+ AND (:q IS NULL OR instr(lower(replace(a.action, '_', ' ') || ' ' || coalesce(a.ip, '') || ' ' || coalesce(a.detail, '') || ' ' ||
336
+ u.display_name || ' ' || u.handle), :q) > 0)
337
+ ORDER BY a.id DESC LIMIT :limit`,
338
+ ).all({ ws: workspaceId, actions: JSON.stringify(actions), user: userId, from, to, q, before, limit })
339
+ }
340
+
341
+ // Account deletion: the person's saved views, in every workspace, go with them.
342
+ export function deleteSavedViewsOf(db, userId) {
343
+ // With the layouts those views carry, and the person's own dashboard layouts.
344
+ prepare(db, "DELETE FROM usage_layouts WHERE kind = 'view' AND subject IN (SELECT id FROM saved_views WHERE user_id = :user)").run({
345
+ user: userId,
346
+ })
347
+ prepare(db, "DELETE FROM usage_layouts WHERE kind = 'person' AND subject = :user").run({ user: userId })
348
+ prepare(db, 'DELETE FROM saved_views WHERE user_id = :user').run({ user: userId })
349
+ }
350
+
286
351
  // Account deletion: the person's own account-level log goes with them.
287
352
  export function deleteAccountEvents(db, userId) {
288
353
  prepare(db, 'DELETE FROM audit_log WHERE workspace_id IS NULL AND user_id = :user').run({ user: userId })
@@ -15,7 +15,9 @@ import { joinUrl } from '../config.js'
15
15
  import { MENTION_HANDLE_RE } from '../mentions.js'
16
16
  import { captureFiles, listVersions, readManifest, resolveFile, variantsWithFiles } from '../store.js'
17
17
  import { agentPolicy, expandScopes, trustedMember } from './agent-policy.js'
18
+ import { COMMENT_ROUTING } from './agents.js'
18
19
  import { mountHostedApprovals } from './agent-approvals.js'
20
+ import { mountAgentLink } from './agent-link.js'
19
21
  import { sendPng } from './board-context.js'
20
22
  import { markupName } from '../comment-media.js'
21
23
  import { commentListsFile, createMarkupCache, readCommentFile, renderMarkupPng, sendImage } from './comment-files.js'
@@ -31,7 +33,7 @@ import { findApiToken, tenant } from './tenancy.js'
31
33
  import { mountTokenEvents } from './token-events.js'
32
34
 
33
35
  export function mountTokenApi({
34
- agentPresence = null,
36
+ agentsFor,
35
37
  db,
36
38
  dataDir,
37
39
  iso,
@@ -51,8 +53,15 @@ export function mountTokenApi({
51
53
  policyFor,
52
54
  boardContext,
53
55
  dispatchView,
54
- needFresh,
56
+ stepUp,
55
57
  approvalApi,
58
+ // Connected agents (src/hosted/agent-link.js): the chat store, the live
59
+ // turns, the open links and what ends a turn.
60
+ agentPresence = null,
61
+ chatFor = null,
62
+ hub = null,
63
+ links = null,
64
+ chat = null,
56
65
  }) {
57
66
  // ---- Token API ----
58
67
  const pushOptions = (t) => {
@@ -83,12 +92,16 @@ export function mountTokenApi({
83
92
  'This token was revoked or has expired, its maker is no longer an admin of the workspace, or it is wrong. Send a live project token: Authorization: Bearer swp_...',
84
93
  )
85
94
  }
86
- const hits = rateLimiter.hit('token', row.id, LIMITS.token)
95
+ // A connected agent's link has a bucket of its own: an answer streams
96
+ // in many small batches, and must not use up the token's budget.
97
+ const scopes = expandScopes(JSON.parse(row.scopes))
98
+ const onLink = scopes.includes('agent') && /^\/agent\//.test(req.path)
99
+ const hits = onLink ? rateLimiter.hit('agentLink', row.id, LIMITS.agentLink) : rateLimiter.hit('token', row.id, LIMITS.token)
87
100
  if (!hits.ok) {
88
101
  res.setHeader('Retry-After', String(hits.retryAfter))
89
102
  return fail(res, 429, 'rate_limited', 'Too many requests for this token.')
90
103
  }
91
- req.token = { id: row.id, name: row.name, scopes: expandScopes(JSON.parse(row.scopes)) }
104
+ req.token = { id: row.id, name: row.name, scopes }
92
105
  req.tokenHash = sha256(token)
93
106
  req.authTokenHash = row.token_hash
94
107
  req.t = tenant(db, row.workspace_id)
@@ -98,9 +111,18 @@ export function mountTokenApi({
98
111
  req.t.run('UPDATE api_tokens SET last_used_at = :at WHERE workspace_id = :ws AND id = :id', { at: iso(), id: row.id })
99
112
  }
100
113
  req.workspaceName = row.workspace_name
101
- // A token that may take work was heard from: an agent is listening for
102
- // this project (src/hosted/agent-presence.js; the name only).
103
- if (req.token.scopes.includes('claim')) agentPresence?.seen(row.workspace_id, row.project_id, row.name)
114
+ // A token that may take work is an agent (src/hosted/agents.js), and it
115
+ // was just heard from: it is online. A claim token made while an older
116
+ // build ran on this database becomes an agent here, on first sight.
117
+ if (req.token.scopes.includes('claim')) {
118
+ const agents = agentsFor(req.t)
119
+ const said = (action, target, detail) => auditOf(req, action, target, detail)
120
+ req.agent = agents.forToken(row.project_id, row.id, { audit: said })
121
+ // An agent of this project that is gone (revoked, expired, its maker
122
+ // no longer an admin) lets go of what it held before this one looks.
123
+ transaction(db, () => agents.releaseGone(row.project_id, said))
124
+ } else req.agent = null
125
+ if (req.agent) agentsFor(req.t).seen(req.agent)
104
126
  next()
105
127
  }
106
128
  v1.use(tokenAuth)
@@ -126,13 +148,20 @@ export function mountTokenApi({
126
148
  // Guest comments stay away from agents unless asked for, on every route
127
149
  // and not only in lists: reading one, replying, statuses, resolving and
128
150
  // reopening need the same opt-in (?guests=1) as listing them.
151
+ // A claim token works only on comments that are its agent's to take: sent
152
+ // to it or to any agent, by someone in its audience (agents.js claimCheck).
153
+ // A comment sent to another agent does not exist for it: 404, like one
154
+ // that was never dispatched, on every route.
155
+ // The token that holds a comment keeps it until it is done, whatever
156
+ // changed since (agents.js workCheck).
157
+ const mine = (req, row) => agentsFor(req.t).workCheck(req.agent, req.project.id, row).ok
129
158
  const v1Comment = (fn) =>
130
159
  handle(async (req, res) => {
131
160
  req.ctx = boardContext(req.t, req.project, req.params.bid)
132
161
  const row = req.ctx.comments.get(req.params.cid)
133
162
  if (
134
163
  req.token.scopes.includes('claim') &&
135
- !policyFor(req.t).state(req.project.id, req.ctx.board.id, { ...row, status: 'open' }).dispatchable
164
+ (!policyFor(req.t).state(req.project.id, req.ctx.board.id, { ...row, status: 'open' }).dispatchable || !mine(req, row))
136
165
  )
137
166
  throw new HttpError(404, 'unknown_comment', 'No dispatched comment at that id.')
138
167
  if (row.author_share_id && req.query.guests !== '1') {
@@ -149,7 +178,12 @@ export function mountTokenApi({
149
178
  // member vouched for the comment, not for what others added after, so only
150
179
  // replies by members the agent policy trusts and the token's own replies.
151
180
  // Guests, other tokens, untrusted members and imported replies never reach it.
152
- function agentReplies(req, ctx, replies = []) {
181
+ // And of those, only people in this agent's audience: someone it does not
182
+ // take work from cannot steer it through a reply (agents.js replyVisible).
183
+ function agentReplies(req, ctx, replies = [], commentId = null) {
184
+ if (!replies.length) return replies
185
+ const agents = agentsFor(req.t)
186
+ const row = commentId ? ctx.comments.get(commentId) : null
153
187
  const policy = agentPolicy(req.t, req.project.id, ctx.board.id)
154
188
  const handleFor = (user) => q.get('SELECT handle FROM users WHERE id = :id', { id: user })?.handle || null
155
189
  return replies.filter((reply) => {
@@ -157,15 +191,15 @@ export function mountTokenApi({
157
191
  if (reply.agent) return Boolean(by.token) && by.token === req.token.id
158
192
  if (reply.guest || reply.imported) return false
159
193
  const user = by.user || (reply.handle ? q.get('SELECT id FROM users WHERE handle = :handle', { handle: reply.handle })?.id : null)
160
- return trustedMember({ ...req.t, handleFor }, policy, user)
194
+ return trustedMember({ ...req.t, handleFor }, policy, user) && agents.replyVisible(req.agent, req.project.id, row, user)
161
195
  })
162
196
  }
163
197
 
164
198
  // The replies a token reads. A claim token: agentReplies above. Any other
165
199
  // token: everything but share link guests' replies, which (like guest
166
200
  // comments) come only when asked for (?guests=1), on every route.
167
- function tokenReplies(req, ctx, replies = []) {
168
- if (req.token.scopes.includes('claim')) return agentReplies(req, ctx, replies)
201
+ function tokenReplies(req, ctx, replies = [], commentId = null) {
202
+ if (req.token.scopes.includes('claim')) return agentReplies(req, ctx, replies, commentId)
169
203
  return req.query.guests === '1' ? replies : replies.filter((reply) => !reply.guest)
170
204
  }
171
205
 
@@ -188,9 +222,12 @@ export function mountTokenApi({
188
222
  // Feedback items with download URLs instead of server paths.
189
223
  function remoteItem(req, ctx, context, comment) {
190
224
  const item = {
191
- ...feedbackItem(context, { ...comment, replies: tokenReplies(req, ctx, comment.replies) }),
225
+ ...feedbackItem(context, { ...comment, replies: tokenReplies(req, ctx, comment.replies, comment.id) }),
192
226
  dispatchable: Boolean(comment.dispatchable),
193
227
  needsRunApproval: Boolean(comment.needsRunApproval),
228
+ // The agent it was sent to, for a token that may take work: its own
229
+ // agent's id, or null for any agent (one sent elsewhere is never listed).
230
+ ...(req.token.scopes.includes('claim') ? { agentId: comment.agentId || null } : {}),
194
231
  }
195
232
  // The capture a comment was made on can be an archived version (versions/).
196
233
  const url = (file) =>
@@ -232,12 +269,12 @@ export function mountTokenApi({
232
269
  const row = ctx.comments.get(number)
233
270
  if (
234
271
  req.token.scopes.includes('claim') &&
235
- !policyFor(req.t).state(req.project.id, ctx.board.id, { ...row, status: 'open' }).dispatchable
272
+ (!policyFor(req.t).state(req.project.id, ctx.board.id, { ...row, status: 'open' }).dispatchable || !mine(req, row))
236
273
  )
237
274
  return null
238
275
  const comment = dispatchView(req.t, req.project.id, ctx.board.id, rowToComment(row))
239
276
  if (comment.authorGuest && req.query.guests !== '1') return null
240
- const replies = tokenReplies(req, ctx, comment.replies)
277
+ const replies = tokenReplies(req, ctx, comment.replies, comment.id)
241
278
  if (replyId && !replies.some((reply) => Number(reply.id) === Number(replyId))) return null
242
279
  return { ...comment, replies }
243
280
  }
@@ -251,7 +288,7 @@ export function mountTokenApi({
251
288
  const comments = ctx.comments
252
289
  .list({ status: status === 'all' ? null : status, mention })
253
290
  .map((comment) => dispatchView(req.t, req.project.id, ctx.board.id, comment))
254
- .filter((comment) => !req.token.scopes.includes('claim') || comment.dispatchable)
291
+ .filter((comment) => !req.token.scopes.includes('claim') || (comment.dispatchable && mine(req, comment[COMMENT_ROUTING])))
255
292
  .filter((comment) => !pageId || comment.pageId === pageId || context.variants.get(comment.pageId)?.pageId === pageId)
256
293
  .filter((comment) => {
257
294
  if (guests || !comment.authorGuest) return true
@@ -278,7 +315,31 @@ export function mountTokenApi({
278
315
  // Any `author` in the request is ignored, so a token cannot pose as a person.
279
316
  const tokenAuthor = (req) => ({ name: cleanAuthor(req.token.name, 'agent'), agent: true })
280
317
 
281
- mountTokenEvents(v1, { db, iso, events, streams, scope, agentPresence })
318
+ mountTokenEvents(v1, { db, iso, events, streams, scope, agentsFor })
319
+ if (chatFor && hub && links && chat)
320
+ mountAgentLink(v1, {
321
+ db,
322
+ iso,
323
+ now,
324
+ events,
325
+ streams,
326
+ scope,
327
+ handle,
328
+ agentsFor,
329
+ agentPresence,
330
+ rateLimiter,
331
+ chatFor,
332
+ hub,
333
+ links,
334
+ chat,
335
+ auditOf,
336
+ boardContext,
337
+ policyFor,
338
+ dispatchView,
339
+ remoteItem,
340
+ contextFor,
341
+ quotaOf: (t) => workspaceQuota ?? quotas.of(t.workspaceId).storageBytes,
342
+ })
282
343
 
283
344
  v1.get(
284
345
  '/project',
@@ -495,14 +556,36 @@ export function mountTokenApi({
495
556
  v1Json,
496
557
  v1Comment((req, res, ctx) => {
497
558
  const { agentStatus = null, onlyFrom = null } = req.body || {}
559
+ // While a fix the server opened runs on this comment, where the comment
560
+ // stands is written with that turn, by the server (src/hosted/agent-link.js
561
+ // releaseFix). A write from the token, late from an earlier fix or not,
562
+ // would end the running fix's claim: it is not taken.
563
+ const fixing = hub?.ofThread(req.t.workspaceId, req.project.id, ctx.board.id, `comment-${Number(req.params.cid)}`)
564
+ if (fixing?.kind === 'fix')
565
+ throw new HttpError(409, 'fix_running', 'A fix is running on this comment: its status is written when that fix ends.')
498
566
  const result = transaction(db, () => {
499
567
  const row = ctx.comments.get(req.params.cid)
500
568
  const taking = ['queued', 'working'].includes(agentStatus) && !['queued', 'working'].includes(row.agent_status)
501
569
  if (taking && !req.token.scopes.includes('claim')) throw new HttpError(403, 'missing_scope', 'Taking work needs the claim scope.')
502
570
  // Only open comments are claimed (and counted against the budget).
503
571
  if (taking && row.status !== 'open') throw new HttpError(409, 'not_open', 'Only an open comment can be taken.')
504
- if (taking && row.status === 'open' && (!onlyFrom || onlyFrom.includes(row.agent_status || null)))
505
- policyFor(req.t).claim(req.project.id, ctx.board.id, row, req.token.id)
572
+ const agents = agentsFor(req.t)
573
+ if (taking && row.status === 'open' && (!onlyFrom || onlyFrom.includes(row.agent_status || null))) {
574
+ // Checked again on the row this transaction reads: a comment sent
575
+ // to another agent, or by someone outside this agent's audience,
576
+ // is never taken.
577
+ const check = agents.claimCheck(req.agent, req.project.id, row)
578
+ if (!check.ok) throw new HttpError(403, check.code, check.message)
579
+ const claimId = policyFor(req.t).claim(req.project.id, ctx.board.id, row, req.token.id, req.agent.id)
580
+ agents.startRun({
581
+ projectId: req.project.id,
582
+ boardId: ctx.board.id,
583
+ number: row.number,
584
+ agentId: req.agent.id,
585
+ claimId,
586
+ askedBy: row.dispatch_by,
587
+ })
588
+ }
506
589
  if (['queued', 'working'].includes(row.agent_status)) {
507
590
  const claim = req.t.get(
508
591
  'SELECT token_id FROM agent_claims WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND comment_number = :number AND ended_at IS NULL ORDER BY created_at DESC LIMIT 1',
@@ -516,6 +599,9 @@ export function mountTokenApi({
516
599
  'UPDATE agent_claims SET ended_at = :at WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND comment_number = :number AND ended_at IS NULL',
517
600
  { project: req.project.id, board: ctx.board.id, number: row.number, at: iso() },
518
601
  )
602
+ // The usage record of every run whose claim just ended (this one, or
603
+ // stale ones the claim above swept).
604
+ agents.closeRuns(req.project.id)
519
605
  return result
520
606
  })
521
607
  res.json({ comment: v1Item(req, ctx, result.comment), changed: result.changed })
@@ -553,7 +639,20 @@ export function mountTokenApi({
553
639
  }),
554
640
  )
555
641
  v1.use(v1Json)
556
- mountHostedApprovals({ v1, boardApi: approvalApi, handle, scope, db, transaction, policyFor, boardContext, needFresh, auditOf, now })
642
+ mountHostedApprovals({
643
+ v1,
644
+ boardApi: approvalApi,
645
+ handle,
646
+ scope,
647
+ db,
648
+ transaction,
649
+ policyFor,
650
+ agentsFor,
651
+ boardContext,
652
+ stepUp,
653
+ auditOf,
654
+ now,
655
+ })
557
656
  v1.use((req, res) => fail(res, 404, 'not_found', 'No such API route.'))
558
657
 
559
658
  return { v1, tokenAuth }
@@ -8,7 +8,7 @@ import { findApiToken } from './tenancy.js'
8
8
 
9
9
  const STREAMS_PER_WORKSPACE = 100
10
10
 
11
- export function mountTokenEvents(v1, { db, iso, events, streams, scope, agentPresence = null }) {
11
+ export function mountTokenEvents(v1, { db, iso, events, streams, scope, agentsFor = null }) {
12
12
  v1.get('/events', scope('read'), (req, res) => {
13
13
  if (streams.size >= 1000 || [...streams].filter((check) => check.tokenId === req.token.id).length >= 5)
14
14
  return fail(res, 429, 'stream_budget', 'Too many event streams for this token.')
@@ -41,6 +41,8 @@ export function mountTokenEvents(v1, { db, iso, events, streams, scope, agentPre
41
41
  // lookup; revocation is still caught here, by the keepalive and after
42
42
  // every mutating request.
43
43
  if (closed || event.workspace !== req.t.workspaceId || event.project !== req.project.id) return
44
+ // Chat turns and agents coming and going are the board's business, not a watcher's.
45
+ if (/^(chat|agent)\./.test(event.type)) return
44
46
  check()
45
47
  if (!closed) {
46
48
  if (
@@ -53,8 +55,8 @@ export function mountTokenEvents(v1, { db, iso, events, streams, scope, agentPre
53
55
  check()
54
56
  if (closed) return
55
57
  res.write(': keepalive\n\n')
56
- // An open stream of a token that may take work is an agent listening.
57
- if (req.token.scopes.includes('claim')) agentPresence?.seen(req.t.workspaceId, req.project.id, req.token.name)
58
+ // An open stream of a token that may take work is its agent listening.
59
+ if (req.agent) agentsFor?.(req.t).seen(req.agent)
58
60
  }, 15000)
59
61
  timer.unref()
60
62
  res.write(': connected\n\n')
@@ -6,14 +6,20 @@ import { expandScopes } from './agent-policy.js'
6
6
  import { newApiToken, newId } from './crypto.js'
7
7
  import { HttpError, cleanName } from './http.js'
8
8
 
9
- export const TOKEN_SCOPES = ['read', 'reply', 'status', 'claim', 'resolve', 'push', 'import']
9
+ // "agent": may hold the link of a connected agent (`shapewall agent connect`,
10
+ // src/hosted/agent-link.js). It opens the link routes only, and is granted
11
+ // only by approving an agent connection (or by an admin, on purpose, for a
12
+ // service's token); "claim" alone still means "may take dispatched comments".
13
+ export const TOKEN_SCOPES = ['read', 'reply', 'status', 'claim', 'resolve', 'push', 'import', 'agent']
10
14
  const DAY = 86_400_000
11
15
 
12
- export function createApiTokens({ iso, auditOf }) {
16
+ export function createApiTokens({ iso, auditOf, agentsFor = null }) {
13
17
  // A new project token for req.t / req.project, made by req.user. Returns
14
18
  // the token (shown once) and its listing. `via` marks how it was made
15
- // ("login" for `shapewall login`) in the audit log.
16
- function createApiToken(req, body, { via = null } = {}) {
19
+ // ("login" for `shapewall login`) in the audit log. A token that may take
20
+ // work (the claim scope) is an agent from the start (src/hosted/agents.js),
21
+ // for the person who made it only; `machine` is the label a login sent.
22
+ function createApiToken(req, body, { via = null, machine = null, agent = null, approvedBy = null } = {}) {
17
23
  // A name too long is refused, not cut (as on a rename): it is what people will see.
18
24
  const name = cleanName(body.name, 61)
19
25
  if (!name || name.length > 60) throw new HttpError(400, 'invalid_name', 'Token name: 1 to 60 characters.')
@@ -21,6 +27,10 @@ export function createApiTokens({ iso, auditOf }) {
21
27
  if (!scopes.length || scopes.some((scope) => !TOKEN_SCOPES.includes(scope))) {
22
28
  throw new HttpError(400, 'invalid_scopes', `Scopes: ${TOKEN_SCOPES.join(', ')}.`)
23
29
  }
30
+ // The link scope is an agent's: it never stands without the scopes an
31
+ // agent works with (it reads comments and answers them as itself).
32
+ if (scopes.includes('agent') && !['read', 'reply', 'status', 'claim'].every((scope) => scopes.includes(scope)))
33
+ throw new HttpError(400, 'invalid_scopes', 'The agent scope goes with read, reply, status and claim.')
24
34
  if (scopes.includes('import') && via !== 'import')
25
35
  throw new HttpError(403, 'import_login_required', 'Import is a one-time grant from a moving-project device login.')
26
36
  const days = body.expiresInDays
@@ -49,9 +59,22 @@ export function createApiTokens({ iso, auditOf }) {
49
59
  expires: days ? iso(days * DAY) : null,
50
60
  },
51
61
  )
52
- auditOf(req, 'token_created', id, { project: req.project.id, scopes, ...(days ? { days } : {}), ...(via ? { via } : {}) })
62
+ auditOf(req, 'token_created', id, {
63
+ project: req.project.id,
64
+ scopes,
65
+ ...(days ? { days } : {}),
66
+ ...(via ? { via } : {}),
67
+ // A login approved from the command line by tailnet identity says so, with the machine.
68
+ ...(approvedBy ? { approvedBy, machine } : {}),
69
+ })
70
+ let agentId = null
71
+ if (scopes.includes('claim') && agentsFor)
72
+ agentId = agentsFor(req.t).create(
73
+ { userId: req.user.id, role: req.role, audit: (action, target, detail) => auditOf(req, action, target, detail) },
74
+ { projectId: req.project.id, tokenId: id, name, machine, ...(agent || {}) },
75
+ )
53
76
  const info = listTokens(req.t, { projectId: req.project.id, now: iso() }).find((item) => item.id === id)
54
- return { token, info }
77
+ return { token, info, agentId }
55
78
  }
56
79
 
57
80
  return createApiToken