shapewall 0.3.3 → 0.6.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 (98) hide show
  1. package/CHANGELOG.md +249 -0
  2. package/README.md +49 -3
  3. package/dist/agent/package-lock.json +2 -2
  4. package/dist/client/assets/BoardChat-DdwBcb2N.js +27 -0
  5. package/dist/client/assets/{BuzzDialog-Ix9qJvim.js → BuzzDialog-DpUcnU6S.js} +1 -1
  6. package/dist/client/assets/ChecksPanel-CngYV4yz.js +1 -0
  7. package/dist/client/assets/History-CD0saQA5.js +1 -0
  8. package/dist/client/assets/LocalSettings-MZy2baig.js +1 -0
  9. package/dist/client/assets/{Management-NVFDtSZB.js → Management-CSvZ-SHO.js} +1 -1
  10. package/dist/client/assets/QuickConnect-foLh2QUm.js +1 -0
  11. package/dist/client/assets/{ShareDialog-By0ETg6n.js → ShareDialog-DPWDftoL.js} +2 -2
  12. package/dist/client/assets/{app-update-dialog-RGIw24iZ.js → app-update-dialog-CtiXpTRD.js} +1 -1
  13. package/dist/client/assets/{circle-x-B6KfScOE.js → circle-x-CteRBtXC.js} +1 -1
  14. package/dist/client/assets/index-Cxz1UVcV.css +2 -0
  15. package/dist/client/assets/index-uUZVBYUS.js +37 -0
  16. package/dist/client/assets/play-BjRNIGSf.js +1 -0
  17. package/dist/client/assets/{refresh-cw-vkgvCCfR.js → refresh-cw-r_tZiFfB.js} +1 -1
  18. package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
  19. package/dist/client/assets/ui-zQpn-PdL.js +41 -0
  20. package/dist/client/build-info.json +1 -1
  21. package/dist/client/index.html +3 -3
  22. package/dist/client/sw.js +2 -2
  23. package/dist/console/assets/{LocalAccountsApp-VOCZg8dg.js → LocalAccountsApp-DIdy1KnN.js} +1 -1
  24. package/dist/console/assets/{LocalConsole-CMwAW-mF.js → LocalConsole-DfcQztF-.js} +1 -1
  25. package/dist/console/assets/{app-update-dialog-Cw6UDqNP.js → app-update-dialog-ttl9lF5A.js} +1 -1
  26. package/dist/console/assets/console-OqAjD0kV.js +55 -0
  27. package/dist/console/assets/console-kvjrAArQ.css +2 -0
  28. package/dist/console/build-info.json +1 -1
  29. package/dist/console/console.html +2 -2
  30. package/docs/agent-hardening.md +192 -0
  31. package/docs/agent-recipes.md +23 -0
  32. package/docs/agent-workflow.md +5 -0
  33. package/docs/agents.md +14 -6
  34. package/docs/hosted.md +804 -11
  35. package/docs/where-to-run.md +37 -2
  36. package/package.json +1 -1
  37. package/src/agent/backends/acp.js +528 -25
  38. package/src/agent/branch-mode.js +17 -3
  39. package/src/agent/card-hash.js +65 -0
  40. package/src/agent/context.js +41 -0
  41. package/src/agent/exec-paths.js +105 -0
  42. package/src/agent/routes.js +2 -2
  43. package/src/agent/run.js +11 -1
  44. package/src/agent/starter/cli.js +6 -1
  45. package/src/agent/turn-changes.js +70 -8
  46. package/src/cli/args.js +13 -0
  47. package/src/cli/commands.js +20 -5
  48. package/src/cli/help.js +36 -1
  49. package/src/hosted/admin.js +7 -0
  50. package/src/hosted/agent-approvals.js +28 -3
  51. package/src/hosted/agent-link.js +1032 -0
  52. package/src/hosted/agent-policy.js +10 -3
  53. package/src/hosted/agent-presence.js +63 -22
  54. package/src/hosted/agent-routes.js +135 -0
  55. package/src/hosted/agents.js +1199 -0
  56. package/src/hosted/app.js +89 -5
  57. package/src/hosted/board-routes.js +127 -3
  58. package/src/hosted/chat-frames.js +256 -0
  59. package/src/hosted/chat-job.js +204 -0
  60. package/src/hosted/chat-permissions.js +191 -0
  61. package/src/hosted/chat-routes.js +1009 -0
  62. package/src/hosted/chat-store.js +460 -0
  63. package/src/hosted/comment-files.js +8 -1
  64. package/src/hosted/comments.js +1 -1
  65. package/src/hosted/console-routes.js +11 -0
  66. package/src/hosted/db.js +165 -0
  67. package/src/hosted/device-routes.js +64 -8
  68. package/src/hosted/device.js +35 -4
  69. package/src/hosted/events.js +3 -3
  70. package/src/hosted/membership.js +9 -1
  71. package/src/hosted/pages.js +2 -0
  72. package/src/hosted/permissions.js +38 -0
  73. package/src/hosted/presence.js +17 -0
  74. package/src/hosted/projects.js +29 -2
  75. package/src/hosted/ratelimit.js +8 -0
  76. package/src/hosted/tenancy.js +18 -0
  77. package/src/hosted/token-api.js +118 -19
  78. package/src/hosted/token-events.js +5 -3
  79. package/src/hosted/tokens.js +21 -5
  80. package/src/hosted/turn-hub.js +152 -0
  81. package/src/remote/agent-connect.js +433 -0
  82. package/src/remote/agent-fix.js +611 -0
  83. package/src/remote/agent-job.js +216 -0
  84. package/src/remote/agent-link.js +904 -0
  85. package/src/remote/agent-policy-file.js +242 -0
  86. package/src/remote/credentials.js +35 -1
  87. package/src/remote/login.js +12 -2
  88. package/dist/client/assets/BoardChat-DxehB761.js +0 -27
  89. package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +0 -1
  90. package/dist/client/assets/History-C9N32kCv.js +0 -1
  91. package/dist/client/assets/LocalSettings-DawtKnvi.js +0 -1
  92. package/dist/client/assets/QuickConnect-DO1TjS1t.js +0 -1
  93. package/dist/client/assets/index-CsqLdqGx.css +0 -2
  94. package/dist/client/assets/index-DnZarJ3U.js +0 -37
  95. package/dist/client/assets/shield-check-QzwGhz0t.js +0 -1
  96. package/dist/client/assets/ui-DI-FGWWK.js +0 -41
  97. package/dist/console/assets/console-DgfWmUEx.js +0 -55
  98. package/dist/console/assets/console-Lis0vvdL.css +0 -2
@@ -8,13 +8,14 @@ import { ID_PATTERNS } from './crypto.js'
8
8
  import { transaction } from './db.js'
9
9
  import { HttpError, fail } from './http.js'
10
10
  import { workspaceSuspended } from './operator.js'
11
- import { NEEDS } from './permissions.js'
11
+ import { cleanAgentName, isAgentTint, isBuiltInIcon } from '../agent/identity.js'
12
+ import { AGENT_AUDIENCES, NEEDS } from './permissions.js'
12
13
  import { createProjectStore } from './projects.js'
13
14
  import { roleAtLeast, roleIn, tenant, workspacesForUser } from './tenancy.js'
14
15
  import { TOKEN_SCOPES } from './tokens.js'
15
16
 
16
17
  export function mountDeviceApprovals(consoleApi, deps) {
17
- const { db, dataDir, now, quotas, deviceLogins, clientIp, limit, handle, needFresh, auditOf, createApiToken } = deps
18
+ const { db, dataDir, now, quotas, deviceLogins, clientIp, limit, handle, needFresh, auditOf, createApiToken, agentsFor } = deps
18
19
 
19
20
  // ---- `shapewall login`: approving a login in the console ----
20
21
  // The CLI side (start, poll) is on /api/v1/device, without a session.
@@ -27,6 +28,8 @@ export function mountDeviceApprovals(consoleApi, deps) {
27
28
  return {
28
29
  code: formatUserCode(found.code),
29
30
  purpose: found.entry.purpose,
31
+ // An agent connection: what the machine says it runs, and the name it suggests.
32
+ ...(found.entry.purpose === 'agent' ? { agent: found.entry.agent } : {}),
30
33
  name: found.entry.name,
31
34
  ip: found.entry.ip,
32
35
  approverIp,
@@ -59,7 +62,12 @@ export function mountDeviceApprovals(consoleApi, deps) {
59
62
  res.json({
60
63
  login: devicePublic(found, req),
61
64
  workspaces,
62
- scopes: TOKEN_SCOPES.filter((scope) => scope !== 'import' || found.entry.purpose === 'import'),
65
+ // An agent connection gets a fixed set (AGENT_LOGIN_SCOPES): nothing to tick.
66
+ scopes:
67
+ found.entry.purpose === 'agent'
68
+ ? AGENT_LOGIN_SCOPES
69
+ : TOKEN_SCOPES.filter((scope) => scope !== 'agent' && (scope !== 'import' || found.entry.purpose === 'import')),
70
+ ...(found.entry.purpose === 'agent' ? { audiences: AGENT_AUDIENCES } : {}),
63
71
  })
64
72
  }),
65
73
  )
@@ -91,15 +99,42 @@ export function mountDeviceApprovals(consoleApi, deps) {
91
99
  req.project = projectStore.create({ name: req.body.newProjectName, userId: req.user.id, now: now() })
92
100
  auditOf(req, 'project_created', req.project.id)
93
101
  }
102
+ const agentLogin = found.entry.purpose === 'agent'
103
+ // Connecting an agent: the approver names it, picks its icon and who
104
+ // may use it. The scopes are fixed, whatever the request says: what
105
+ // an agent works with, plus the link.
106
+ const look = agentLogin ? agentLook(req.body?.agent, found.entry) : null
107
+ if (!agentLogin && Array.isArray(req.body?.scopes) && req.body.scopes.includes('agent'))
108
+ throw new HttpError(403, 'agent_login_required', 'The agent scope comes from "shapewall agent connect".')
94
109
  const token = createApiToken(
95
110
  req,
96
111
  {
97
- name: `${found.entry.name} (shapewall login)`,
98
- scopes: req.body?.scopes,
112
+ name: agentLogin ? look.name : `${found.entry.name} (shapewall login)`,
113
+ scopes: agentLogin ? AGENT_LOGIN_SCOPES : req.body?.scopes,
99
114
  expiresInDays: req.body?.expiresInDays ?? null,
100
115
  },
101
- { via: found.entry.purpose === 'import' ? 'import' : 'login' },
116
+ {
117
+ via: agentLogin ? 'agent' : found.entry.purpose === 'import' ? 'import' : 'login',
118
+ machine: found.entry.name,
119
+ ...(agentLogin ? { agent: { kind: found.entry.agent?.kind || 'custom', icon: look.icon, tint: look.tint } } : {}),
120
+ },
102
121
  )
122
+ let agent = null
123
+ if (agentLogin) {
124
+ const actor = { userId: req.user.id, role: req.role, audit: (action, target, detail) => auditOf(req, action, target, detail) }
125
+ // A new agent is its connector's own; widening it is said here, on
126
+ // the approval, and written to the audit log like any other change.
127
+ if (look.audience !== 'owner')
128
+ agentsFor(req.t).setAudience(actor, req.project.id, token.agentId, { audience: look.audience, users: look.users })
129
+ auditOf(req, 'agent_connected', token.agentId, {
130
+ project: req.project.id,
131
+ name: look.name,
132
+ kind: found.entry.agent?.kind || 'custom',
133
+ machine: found.entry.name,
134
+ audience: look.audience,
135
+ })
136
+ agent = { id: token.agentId, name: look.name, audience: look.audience }
137
+ }
103
138
  const approved = deviceLogins.approve(found.code, {
104
139
  token: token.token,
105
140
  tokenName: token.info.name,
@@ -107,15 +142,17 @@ export function mountDeviceApprovals(consoleApi, deps) {
107
142
  expiresAt: token.info.expiresAt,
108
143
  workspace: { id: ws, name: workspace.name },
109
144
  project: { id: req.project.id, name: req.project.name },
145
+ ...(agent ? { agent } : {}),
110
146
  })
111
147
  if (!approved) throw new HttpError(404, 'unknown_code', 'That code was already used, or it expired. Run shapewall login again.')
112
- return token
148
+ return { ...token, agent }
113
149
  })
114
150
  res.json({
115
151
  ok: true,
116
152
  info: made.info,
117
153
  workspace: { id: ws, name: workspace.name },
118
154
  project: { id: req.project.id, name: req.project.name },
155
+ ...(made.agent ? { agent: made.agent } : {}),
119
156
  })
120
157
  }),
121
158
  )
@@ -130,6 +167,25 @@ export function mountDeviceApprovals(consoleApi, deps) {
130
167
  )
131
168
  }
132
169
 
170
+ // What a connected agent's token may do: read dispatched comments, answer
171
+ // them as itself, set statuses, take work, and hold the link. Never resolve
172
+ // or push: those stay separate tokens.
173
+ export const AGENT_LOGIN_SCOPES = ['read', 'reply', 'status', 'claim', 'agent']
174
+
175
+ // The name, icon, tint and audience the approver chose for a new agent.
176
+ function agentLook(input, entry) {
177
+ const wanted = typeof input?.name === 'string' && input.name.trim() ? input.name : entry.agent?.name || entry.name
178
+ const { name, problem } = cleanAgentName(wanted)
179
+ if (problem || !name) throw new HttpError(400, 'invalid_name', problem ? `Agent ${problem}.` : 'Agent name: 1 to 40 characters.')
180
+ const icon = input?.icon ?? null
181
+ if (icon !== null && !isBuiltInIcon(icon)) throw new HttpError(400, 'invalid_icon', 'Choose one of the built-in icons.')
182
+ const tint = input?.tint ?? null
183
+ if (tint !== null && !isAgentTint(tint)) throw new HttpError(400, 'invalid_tint', 'Choose one of the tints.')
184
+ const audience = input?.audience ?? 'owner'
185
+ if (!AGENT_AUDIENCES.includes(audience)) throw new HttpError(400, 'invalid_audience', `Audience: ${AGENT_AUDIENCES.join(', ')}.`)
186
+ return { name, icon, tint, audience, users: Array.isArray(input?.users) ? input.users : [] }
187
+ }
188
+
133
189
  export function mountDeviceApi(app, deps) {
134
190
  const { deviceLogins, publicOrigin, clientIp, limit, handle } = deps
135
191
 
@@ -146,7 +202,7 @@ export function mountDeviceApi(app, deps) {
146
202
  '/start',
147
203
  limit('deviceStart'),
148
204
  handle(async (req, res) => {
149
- const started = deviceLogins.start({ name: req.body?.name, ip: clientIp(req), purpose: req.body?.purpose })
205
+ const started = deviceLogins.start({ name: req.body?.name, ip: clientIp(req), purpose: req.body?.purpose, agent: req.body?.agent })
150
206
  if (started.refused)
151
207
  throw new HttpError(429, 'too_many_logins', 'Too many logins in progress from this address. Finish or wait out one (10 minutes).')
152
208
  res.status(201).json({
@@ -70,6 +70,34 @@ export function cleanMachineName(value) {
70
70
  return name || 'a computer'
71
71
  }
72
72
 
73
+ // What a device login is for: a plain token (null), moving a local project
74
+ // ("import"), or connecting an agent ("agent", `shapewall agent connect`).
75
+ export const DEVICE_PURPOSES = ['import', 'agent']
76
+ const AGENT_META_KINDS = ['claude', 'codex', 'goose', 'gemini', 'custom']
77
+ // What `shapewall agent connect` says about the agent it will run: its kind
78
+ // and the name it suggests. The machine's own claim, shown on the approval
79
+ // page and never trusted for anything else.
80
+ export function cleanAgentMeta(value) {
81
+ const kind = AGENT_META_KINDS.includes(value?.kind) ? value.kind : 'custom'
82
+ const name =
83
+ typeof value?.name === 'string'
84
+ ? value.name
85
+ // eslint-disable-next-line no-control-regex
86
+ .replace(/[\u0000-\u001f\u007f<>]/g, '')
87
+ .replace(/\s+/g, ' ')
88
+ .trim()
89
+ .slice(0, 40)
90
+ : ''
91
+ return { kind, name: name || null }
92
+ }
93
+ const parseMeta = (text) => {
94
+ try {
95
+ return cleanAgentMeta(JSON.parse(text || '{}'))
96
+ } catch {
97
+ return cleanAgentMeta(null)
98
+ }
99
+ }
100
+
73
101
  // ---- Sealing the token to the device code ----
74
102
 
75
103
  // PKCS#8 header of a raw 32-byte X25519 private key.
@@ -125,11 +153,13 @@ export function createDeviceLogins({ db, now = () => new Date(), maxPending = MA
125
153
  expiresAt: row.expires_at,
126
154
  status: row.status,
127
155
  purpose: row.purpose || null,
156
+ // What an agent login said it runs: { kind, name } (cleaned at start).
157
+ agent: row.purpose === 'agent' ? parseMeta(row.meta) : null,
128
158
  }
129
159
 
130
160
  // A new login, or { refused: 'per_ip' } when this address has too many
131
161
  // pending already.
132
- function start({ name, ip, purpose }) {
162
+ function start({ name, ip, purpose, agent = null }) {
133
163
  return transaction(db, () => {
134
164
  sweep()
135
165
  const from = ip || null
@@ -154,11 +184,12 @@ export function createDeviceLogins({ db, now = () => new Date(), maxPending = MA
154
184
  const deviceCode = randomToken(32)
155
185
  const created = time()
156
186
  q.run(
157
- `INSERT INTO device_logins (device_hash, user_code, name, ip, seal_key, created_at, expires_at, status, purpose)
158
- VALUES (:hash, :code, :name, :ip, :seal, :created, :expires, 'pending', :purpose)`,
187
+ `INSERT INTO device_logins (device_hash, user_code, name, ip, seal_key, created_at, expires_at, status, purpose, meta)
188
+ VALUES (:hash, :code, :name, :ip, :seal, :created, :expires, 'pending', :purpose, :meta)`,
159
189
  {
160
190
  hash: sha256(deviceCode),
161
- purpose: purpose === 'import' ? 'import' : null,
191
+ purpose: DEVICE_PURPOSES.includes(purpose) ? purpose : null,
192
+ meta: purpose === 'agent' ? JSON.stringify(cleanAgentMeta(agent)) : null,
162
193
  code: userCode,
163
194
  name: cleanMachineName(name),
164
195
  ip: from,
@@ -21,7 +21,7 @@ export function createEventBus() {
21
21
 
22
22
  // The change event a successful write publishes (the SSE stream and board
23
23
  // presence refetch on it), or null. Only comment, approval, import, agent
24
- // policy and push-commit writes change what watchers see, and a board
24
+ // policy, agent setting and push-commit writes change what watchers see, and a board
25
25
  // removed in the console (`method` DELETE on the board itself); a push's
26
26
  // file uploads, token and device approvals publish nothing.
27
27
  export function changeEventType(pathname, method = 'POST') {
@@ -31,10 +31,10 @@ export function changeEventType(pathname, method = 'POST') {
31
31
  if (/\/comments\/[1-9][0-9]*\/resolve$/.test(pathname)) return 'comment.resolved'
32
32
  if (/\/boards\/[^/]+\/comments$/.test(pathname)) return 'comment.created'
33
33
  if (
34
- /\/boards\/[^/]+\/(?:comments\/[1-9][0-9]*(?:\/(?:replies|reactions|reopen|agent-status|approvals))?|approvals\/[^/]+|import)$/.test(
34
+ /\/boards\/[^/]+\/(?:comments\/[1-9][0-9]*(?:\/(?:replies|reactions|reopen|agent-status|approvals|release-agent))?|approvals\/[^/]+|import)$/.test(
35
35
  pathname,
36
36
  ) ||
37
- /\/projects\/[^/]+\/agent$/.test(pathname)
37
+ /\/projects\/[^/]+\/(?:agent|default-agent|agents\/[^/]+(?:\/(?:audience|approval))?)$/.test(pathname)
38
38
  )
39
39
  return 'comment.updated'
40
40
  return null
@@ -13,7 +13,7 @@ import { audit, inviteUsable, tenant, workspacesForUser } from './tenancy.js'
13
13
 
14
14
  const WORKSPACES_PER_USER = 10
15
15
 
16
- export function createMembership({ db, quotas, iso, auditOf }) {
16
+ export function createMembership({ db, quotas, iso, auditOf, agentsFor = null }) {
17
17
  function publicUser(user) {
18
18
  return { id: user.id, handle: user.handle, displayName: user.display_name, avatarUrl: avatarUrl(db, user.id) }
19
19
  }
@@ -102,6 +102,14 @@ export function createMembership({ db, quotas, iso, auditOf }) {
102
102
  }).changes
103
103
  : 0
104
104
  if (invites || shares || tokens) auditOf(req, 'member_grants_revoked', userId, { invites, shares, tokens }, t)
105
+ // A revoked token that was an agent lets go of what it held and what
106
+ // waited for it (src/hosted/agents.js releaseGone).
107
+ if (tokens && agentsFor) {
108
+ const agents = agentsFor(t)
109
+ agents.forget()
110
+ for (const project of t.all('SELECT id FROM projects WHERE workspace_id = :ws'))
111
+ agents.releaseGone(project.id, (action, target, detail) => auditOf(req, action, target, detail, t))
112
+ }
105
113
  return { invites, shares, tokens }
106
114
  }
107
115
 
@@ -63,6 +63,8 @@ export function mountPages(app, { db, dataDir, consoleDir, clientDir, turnstile,
63
63
  '/w/:ws',
64
64
  '/w/:ws/:section',
65
65
  '/w/:ws/projects/:pid',
66
+ // A project's Agents page.
67
+ '/w/:ws/agents/:pid',
66
68
  ])
67
69
  app.get(route, (req, res) => sendConsole(req, res))
68
70
 
@@ -61,6 +61,44 @@ export function guestPermissions(mode) {
61
61
  export const canEditComment = ({ isAuthor }) => Boolean(isAuthor)
62
62
  export const canDeleteComment = ({ role, isAuthor }) => roleAtLeast(role, 'member') && (Boolean(isAuthor) || roleAtLeast(role, 'admin'))
63
63
 
64
+ // ---- Agents on a hosted server (src/hosted/agents.js) ----
65
+ //
66
+ // Every role rule about agents is here, so the console, the board, a later
67
+ // CLI command or admin tool, and the approval cards of connected agents all
68
+ // ask the same functions.
69
+ //
70
+ // Who may send an agent work (its audience): only the person who connected
71
+ // it, named members, or everyone the project's agent policy trusts.
72
+ export const AGENT_AUDIENCES = ['owner', 'members', 'trusted']
73
+ // Whether an agent's edits and commands always wait for a person, or are
74
+ // approved automatically.
75
+ export const APPROVAL_RULES = ['ask', 'auto']
76
+ // The least role that may change an agent's approval rule, or answer its
77
+ // approvals: each is a setting per agent, admin by default.
78
+ export const APPROVAL_ROLES = ['admin', 'owner']
79
+ // Rename, icon, default agent, audience, revoke: the people who manage tokens.
80
+ export const canManageAgents = (role) => roleAtLeast(role, NEEDS.tokens)
81
+ export const canChangeApprovalRule = (role, agent) => canManageAgents(role) && roleAtLeast(role, agent?.approvalChangeRole || 'admin')
82
+ // Which role those two take is itself an owner's decision.
83
+ export const canChangeApprovalRoles = (role) => role === 'owner'
84
+ export const canAnswerApproval = (role, agent) => roleAtLeast(role, agent?.approvalAnswerRole || 'admin')
85
+ // Automatic approval needs the agent's own machine to have opted in twice:
86
+ // to approvals from the board at all (`agent.machineApprovals`, what a
87
+ // connected agent reported: "none", "owner" or "admins"), and to "allowed
88
+ // without asking" for at least one kind of request (`agent.machineAuto`:
89
+ // "edits", "commands"). Without both, "always ask" is the only rule in
90
+ // effect, whatever is stored. Even with both, the machine keeps its own
91
+ // limits: the rule never widens what may be edited or which commands run.
92
+ export const APPROVAL_KINDS = ['edits', 'commands']
93
+ const machineAutoKinds = (agent) =>
94
+ ['owner', 'admins'].includes(agent?.machineApprovals)
95
+ ? APPROVAL_KINDS.filter((kind) => Array.isArray(agent?.machineAuto) && agent.machineAuto.includes(kind))
96
+ : []
97
+ export const approvalAutoAvailable = (agent) => machineAutoKinds(agent).length > 0
98
+ export const effectiveApprovalRule = (agent) => (agent?.approvalRule === 'auto' && approvalAutoAvailable(agent) ? 'auto' : 'ask')
99
+ // The rule in effect for one kind of request: "auto" only for a kind the machine named.
100
+ export const approvalRuleFor = (agent, kind) => (agent?.approvalRule === 'auto' && machineAutoKinds(agent).includes(kind) ? 'auto' : 'ask')
101
+
64
102
  // Roles: admins manage members and viewers (and may make admins); only
65
103
  // owners change owners or admins. `newRole` null asks about removing.
66
104
  export function canManage(actorRole, targetRole, newRole = null) {
@@ -171,6 +171,23 @@ export function createPresenceHub({ events = null, limits = {}, now = () => Date
171
171
  }
172
172
  const unsubscribe = events?.subscribe((event) => {
173
173
  if (event.type === 'close') return close()
174
+ // Chat with connected agents (src/hosted/chat-routes.js) and agents
175
+ // coming and going: a nudge for signed-in members only, never a share
176
+ // link's guest. Ids of shared threads only; the board refetches.
177
+ if (event.type === 'chat.turn' || event.type === 'agent.changed') {
178
+ if (!event.workspace || !event.project) return
179
+ const prefix = `${event.workspace}:${event.project}:`
180
+ for (const [key, room] of rooms) {
181
+ if (!key.startsWith(prefix) || (event.board && key !== `${prefix}${event.board}`)) continue
182
+ for (const peer of [...room.peers])
183
+ if (peer.access.kind === 'member' && live(peer))
184
+ send(peer, {
185
+ type: 'agents',
186
+ ...(event.type === 'chat.turn' ? { thread: typeof event.thread === 'string' ? event.thread : null } : {}),
187
+ })
188
+ }
189
+ return
190
+ }
174
191
  if (!event.board || !event.workspace || !event.project) return
175
192
  const room = rooms.get(`${event.workspace}:${event.project}:${event.board}`)
176
193
  if (!room) return
@@ -105,7 +105,17 @@ export function createProjectStore(t, { db, dataDir, maxProjects = PROJECTS_MAX
105
105
  const project = get(projectId)
106
106
  if (!project) throw new ProjectError(404, 'unknown_project', 'No such project.')
107
107
  transaction(db, () => {
108
- for (const table of ['comment_files', 'comments', 'boards', 'api_tokens', 'pushes']) {
108
+ for (const table of [
109
+ 'chat_permissions',
110
+ 'chat_turns',
111
+ 'chat_messages',
112
+ 'chat_threads',
113
+ 'comment_files',
114
+ 'comments',
115
+ 'boards',
116
+ 'api_tokens',
117
+ 'pushes',
118
+ ]) {
109
119
  t.run(`DELETE FROM ${table} WHERE workspace_id = :ws AND project_id = :id`, { id: project.id })
110
120
  }
111
121
  t.run('DELETE FROM projects WHERE workspace_id = :ws AND id = :id', { id: project.id })
@@ -130,7 +140,19 @@ export function createProjectStore(t, { db, dataDir, maxProjects = PROJECTS_MAX
130
140
  // board id starts a new board (comment numbers from 1 again). The
131
141
  // workspace's storage count is a sum over these rows, so it drops by
132
142
  // itself. Returns what went: { board, comments, bytes }.
133
- const BOARD_TABLES = ['comment_files', 'comment_imports', 'agent_approvals', 'agent_claims', 'comments', 'pushes', 'boards']
143
+ const BOARD_TABLES = [
144
+ 'chat_permissions',
145
+ 'chat_turns',
146
+ 'chat_messages',
147
+ 'chat_threads',
148
+ 'comment_files',
149
+ 'comment_imports',
150
+ 'agent_approvals',
151
+ 'agent_claims',
152
+ 'comments',
153
+ 'pushes',
154
+ 'boards',
155
+ ]
134
156
  function removeBoard(projectId, boardId) {
135
157
  const id = String(boardId || '')
136
158
  const project = get(projectId)
@@ -142,6 +164,11 @@ export function createProjectStore(t, { db, dataDir, maxProjects = PROJECTS_MAX
142
164
  const comments = t.get(`SELECT COUNT(*) AS n FROM comments ${where}`, key).n
143
165
  const images = t.get(`SELECT COALESCE(SUM(bytes), 0) AS n FROM comment_files ${where}`, key).n
144
166
  const open = t.all(`SELECT id FROM pushes ${where} AND status = 'open'`, key).map((push) => push.id)
167
+ // The usage record of a run on this board stays (it is history), closed.
168
+ t.run(`UPDATE agent_runs SET ended_at = :at, outcome = 'removed' ${where} AND ended_at IS NULL`, {
169
+ ...key,
170
+ at: new Date().toISOString(),
171
+ })
145
172
  for (const table of BOARD_TABLES) t.run(`DELETE FROM ${table} ${where}`, key)
146
173
  return { comments, bytes: row.bytes + images, open }
147
174
  })
@@ -68,6 +68,14 @@ export const LIMITS = {
68
68
  sessionFiles: { limit: 6000, windowMs: 60_000 },
69
69
  token: { limit: 1200, windowMs: 60_000 },
70
70
  tokenBadAuth: { limit: 20, windowMs: 60_000 },
71
+ // A connected agent's link (src/hosted/agent-link.js): streamed answers
72
+ // arrive in small batches, in a bucket of their own so they never use up
73
+ // the token's budget for comments and pushes.
74
+ agentLink: { limit: 6000, windowMs: 60_000 },
75
+ // How often one agent's link may open (each open is a write and a broadcast).
76
+ agentLinkOpen: { limit: 6, windowMs: 60_000 },
77
+ // Chat messages a person sends to connected agents (src/hosted/chat-routes.js).
78
+ chatSend: { limit: 20, windowMs: 60_000 },
71
79
  push: { limit: 20, windowMs: 60 * 60_000 },
72
80
  // Invite emails per workspace (the link can always be copied instead).
73
81
  inviteEmail: { limit: 20, windowMs: 60 * 60_000 },
@@ -30,6 +30,12 @@ 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',
33
39
  ]
34
40
  export { ROLES, roleAtLeast } from './permissions.js'
35
41
 
@@ -115,6 +121,18 @@ export function tenant(db, workspaceId) {
115
121
 
116
122
  // ---- Entry points (global lookups that yield a workspace) ----------------------
117
123
 
124
+ // The workspaces that hold a chat turn the database still calls open. At
125
+ // server start none is really running (live turns are memory): the caller
126
+ // ends each one, through its own tenant handle (src/hosted/agent-link.js).
127
+ export function workspacesWithOpenTurns(db) {
128
+ const has = db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'chat_turns'").get()
129
+ if (!has) return []
130
+ return db
131
+ .prepare("SELECT DISTINCT workspace_id FROM chat_turns WHERE state IN ('queued', 'running')")
132
+ .all()
133
+ .map((row) => row.workspace_id)
134
+ }
135
+
118
136
  // The token row for a SHA-256 hash, if it is live: not revoked, not
119
137
  // expired, and its maker is still a member who may hold tokens (an admin or
120
138
  // owner here; tokenCreatorOk). Callers then scope by its workspace_id.