shapewall 0.3.2 → 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 (119) hide show
  1. package/CHANGELOG.md +357 -0
  2. package/NOTICE +7 -0
  3. package/README.md +50 -4
  4. package/THIRD_PARTY.md +39 -0
  5. package/dist/agent/package-lock.json +2 -2
  6. package/dist/client/assets/BoardChat-DdwBcb2N.js +27 -0
  7. package/dist/client/assets/BuzzDialog-DpUcnU6S.js +1 -0
  8. package/dist/client/assets/ChecksPanel-CngYV4yz.js +1 -0
  9. package/dist/client/assets/History-CD0saQA5.js +1 -0
  10. package/dist/client/assets/LocalSettings-MZy2baig.js +1 -0
  11. package/dist/client/assets/{Management-DmTh1-tA.js → Management-CSvZ-SHO.js} +1 -1
  12. package/dist/client/assets/QuickConnect-foLh2QUm.js +1 -0
  13. package/dist/client/assets/ShareDialog-DPWDftoL.js +2 -0
  14. package/dist/client/assets/app-update-dialog-CtiXpTRD.js +1 -0
  15. package/dist/client/assets/{circle-x-3UxAvczM.js → circle-x-CteRBtXC.js} +1 -1
  16. package/dist/client/assets/index-Cxz1UVcV.css +2 -0
  17. package/dist/client/assets/index-uUZVBYUS.js +37 -0
  18. package/dist/client/assets/play-BjRNIGSf.js +1 -0
  19. package/dist/client/assets/{refresh-cw-BRHNyCnY.js → refresh-cw-r_tZiFfB.js} +1 -1
  20. package/dist/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  21. package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
  22. package/dist/client/assets/ui-zQpn-PdL.js +41 -0
  23. package/dist/client/build-info.json +1 -1
  24. package/dist/client/index.html +4 -3
  25. package/dist/client/sw.js +2 -2
  26. package/dist/console/assets/{LocalAccountsApp-IvHd9zWN.js → LocalAccountsApp-DIdy1KnN.js} +1 -1
  27. package/dist/console/assets/{LocalConsole-DOyLou56.js → LocalConsole-DfcQztF-.js} +1 -1
  28. package/dist/console/assets/{app-update-dialog-Da46CJpv.js → app-update-dialog-ttl9lF5A.js} +1 -1
  29. package/dist/console/assets/console-OqAjD0kV.js +55 -0
  30. package/dist/console/assets/console-kvjrAArQ.css +2 -0
  31. package/dist/console/build-info.json +1 -1
  32. package/dist/console/console.html +2 -2
  33. package/docs/agent-hardening.md +192 -0
  34. package/docs/agent-recipes.md +23 -0
  35. package/docs/agent-workflow.md +5 -0
  36. package/docs/agents.md +74 -6
  37. package/docs/config.md +1 -1
  38. package/docs/deployment.md +2 -0
  39. package/docs/design-system.md +20 -15
  40. package/docs/hosted.md +815 -3
  41. package/docs/where-to-run.md +123 -0
  42. package/package.json +2 -1
  43. package/src/agent/agent-logos.js +81 -0
  44. package/src/agent/backends/acp.js +528 -25
  45. package/src/agent/branch-mode.js +17 -3
  46. package/src/agent/card-hash.js +65 -0
  47. package/src/agent/config.js +27 -2
  48. package/src/agent/context.js +41 -0
  49. package/src/agent/exec-paths.js +105 -0
  50. package/src/agent/icon-store.js +100 -0
  51. package/src/agent/identity.js +77 -0
  52. package/src/agent/owner-mcp.js +19 -3
  53. package/src/agent/routes.js +82 -22
  54. package/src/agent/run.js +14 -2
  55. package/src/agent/settings-routes.js +233 -14
  56. package/src/agent/starter/cli.js +6 -1
  57. package/src/agent/test-timing.js +14 -0
  58. package/src/agent/tools.js +4 -2
  59. package/src/agent/turn-changes.js +70 -8
  60. package/src/agent/user-settings.js +51 -3
  61. package/src/capture/element-map.js +61 -4
  62. package/src/cli/args.js +13 -0
  63. package/src/cli/commands.js +20 -5
  64. package/src/cli/help.js +36 -1
  65. package/src/hosted/admin.js +7 -0
  66. package/src/hosted/agent-approvals.js +28 -3
  67. package/src/hosted/agent-link.js +1032 -0
  68. package/src/hosted/agent-policy.js +10 -3
  69. package/src/hosted/agent-presence.js +81 -0
  70. package/src/hosted/agent-routes.js +135 -0
  71. package/src/hosted/agents.js +1199 -0
  72. package/src/hosted/app.js +94 -4
  73. package/src/hosted/board-routes.js +134 -3
  74. package/src/hosted/chat-frames.js +256 -0
  75. package/src/hosted/chat-job.js +204 -0
  76. package/src/hosted/chat-permissions.js +191 -0
  77. package/src/hosted/chat-routes.js +1009 -0
  78. package/src/hosted/chat-store.js +460 -0
  79. package/src/hosted/comment-files.js +8 -1
  80. package/src/hosted/comments.js +1 -1
  81. package/src/hosted/console-routes.js +58 -1
  82. package/src/hosted/db.js +165 -0
  83. package/src/hosted/device-routes.js +64 -8
  84. package/src/hosted/device.js +35 -4
  85. package/src/hosted/events.js +8 -6
  86. package/src/hosted/membership.js +9 -1
  87. package/src/hosted/pages.js +2 -0
  88. package/src/hosted/permissions.js +38 -0
  89. package/src/hosted/presence.js +18 -1
  90. package/src/hosted/projects.js +60 -2
  91. package/src/hosted/ratelimit.js +8 -0
  92. package/src/hosted/tenancy.js +18 -0
  93. package/src/hosted/token-api.js +118 -15
  94. package/src/hosted/token-events.js +7 -2
  95. package/src/hosted/tokens.js +24 -7
  96. package/src/hosted/turn-hub.js +152 -0
  97. package/src/mcp.js +10 -2
  98. package/src/remote/agent-connect.js +433 -0
  99. package/src/remote/agent-fix.js +611 -0
  100. package/src/remote/agent-job.js +216 -0
  101. package/src/remote/agent-link.js +904 -0
  102. package/src/remote/agent-policy-file.js +242 -0
  103. package/src/remote/credentials.js +35 -1
  104. package/src/remote/login.js +12 -2
  105. package/src/store.js +5 -0
  106. package/dist/client/assets/BoardChat-BeRp6007.js +0 -27
  107. package/dist/client/assets/BuzzDialog-CrYy48hh.js +0 -1
  108. package/dist/client/assets/ChecksPanel-CB820mhj.js +0 -1
  109. package/dist/client/assets/History-Ba909bGJ.js +0 -1
  110. package/dist/client/assets/LocalSettings-ChMi9Zir.js +0 -1
  111. package/dist/client/assets/QuickConnect-CrVweWZB.js +0 -1
  112. package/dist/client/assets/ShareDialog-DO2HhY6g.js +0 -2
  113. package/dist/client/assets/app-update-dialog-C09ODdUQ.js +0 -1
  114. package/dist/client/assets/index-BIjfZKnp.js +0 -37
  115. package/dist/client/assets/index-CXtuJi1K.css +0 -2
  116. package/dist/client/assets/shield-check-DMpniYP6.js +0 -1
  117. package/dist/client/assets/ui-mhL9gsne.js +0 -41
  118. package/dist/console/assets/console-CIBv7cXU.js +0 -55
  119. package/dist/console/assets/console-Fp4k9iyx.css +0 -2
package/src/hosted/db.js CHANGED
@@ -462,6 +462,171 @@ const MIGRATIONS = [
462
462
  CREATE INDEX comment_files_comment ON comment_files(workspace_id, project_id, board_id, comment_number);
463
463
  CREATE INDEX comment_files_import ON comment_files(workspace_id, project_id, import_session, import_ref);
464
464
  `,
465
+ // 12: agents (0.4.0). A project API token that may take work (the claim
466
+ // scope) is an agent: this row gives it a face (kind, icon, tint, machine),
467
+ // an audience (who may send it work), an approval rule and who may change
468
+ // or answer it. The name stays the token's. A project has a default
469
+ // agent; a comment can be addressed to one agent (NULL: any agent); a
470
+ // claim remembers which agent took it. agent_runs is the usage record of
471
+ // each comment an agent took (who asked, which agent, when, how it ended;
472
+ // never comment or answer text), shaped for the usage dashboard, whose
473
+ // token and cost columns stay NULL until an agent reports them.
474
+ //
475
+ // Every token that already has the claim scope becomes an agent of kind
476
+ // "watcher" with the audience it had in effect before: everyone the
477
+ // project's agent policy trusts (audience_inherited marks that it was
478
+ // never chosen). Agents made from now on start with "owner".
479
+ `
480
+ CREATE TABLE agents (
481
+ workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
482
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
483
+ id TEXT PRIMARY KEY,
484
+ token_id TEXT NOT NULL UNIQUE REFERENCES api_tokens(id) ON DELETE CASCADE,
485
+ kind TEXT NOT NULL DEFAULT 'watcher',
486
+ icon TEXT,
487
+ tint TEXT,
488
+ machine TEXT,
489
+ capabilities TEXT NOT NULL DEFAULT '{}',
490
+ audience TEXT NOT NULL DEFAULT 'owner' CHECK (audience IN ('owner', 'members', 'trusted')),
491
+ audience_users TEXT NOT NULL DEFAULT '[]',
492
+ audience_inherited INTEGER NOT NULL DEFAULT 0,
493
+ approval_rule TEXT NOT NULL DEFAULT 'ask' CHECK (approval_rule IN ('ask', 'auto')),
494
+ approval_change_role TEXT NOT NULL DEFAULT 'admin' CHECK (approval_change_role IN ('admin', 'owner')),
495
+ approval_answer_role TEXT NOT NULL DEFAULT 'admin' CHECK (approval_answer_role IN ('admin', 'owner')),
496
+ created_by TEXT,
497
+ created_at TEXT NOT NULL,
498
+ approved_by TEXT,
499
+ approved_at TEXT,
500
+ last_seen_at TEXT
501
+ );
502
+ CREATE INDEX agents_project ON agents(workspace_id, project_id);
503
+ ALTER TABLE projects ADD COLUMN default_agent_id TEXT;
504
+ ALTER TABLE comments ADD COLUMN agent_id TEXT;
505
+ ALTER TABLE agent_claims ADD COLUMN agent_id TEXT;
506
+ INSERT INTO agents (workspace_id, project_id, id, token_id, kind, audience, audience_inherited, created_by, created_at,
507
+ approved_by, approved_at, last_seen_at)
508
+ SELECT workspace_id, project_id, 'ag_' || lower(hex(randomblob(12))), id, 'watcher', 'trusted', 1, created_by, created_at,
509
+ created_by, created_at, last_used_at
510
+ FROM api_tokens WHERE scopes LIKE '%"claim"%';
511
+ UPDATE agent_claims SET agent_id = (SELECT a.id FROM agents a WHERE a.token_id = agent_claims.token_id);
512
+ CREATE TABLE agent_runs (
513
+ workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
514
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
515
+ id TEXT PRIMARY KEY,
516
+ kind TEXT NOT NULL DEFAULT 'fix',
517
+ agent_id TEXT,
518
+ claim_id TEXT,
519
+ board_id TEXT,
520
+ comment_number INTEGER,
521
+ thread_id TEXT,
522
+ asked_by TEXT,
523
+ started_at TEXT NOT NULL,
524
+ ended_at TEXT,
525
+ outcome TEXT,
526
+ model TEXT,
527
+ effort TEXT,
528
+ input_tokens INTEGER,
529
+ output_tokens INTEGER,
530
+ cached_tokens INTEGER,
531
+ cost_micros INTEGER,
532
+ steps INTEGER
533
+ );
534
+ CREATE INDEX agent_runs_time ON agent_runs(workspace_id, started_at);
535
+ CREATE INDEX agent_runs_project ON agent_runs(workspace_id, project_id, started_at);
536
+ CREATE INDEX agent_runs_agent ON agent_runs(workspace_id, agent_id, started_at);
537
+ CREATE INDEX agent_runs_person ON agent_runs(workspace_id, asked_by, started_at);
538
+ CREATE INDEX agent_runs_claim ON agent_runs(workspace_id, claim_id);
539
+ `,
540
+ // 13: connected agents and chat (0.5.0). `shapewall agent connect` holds a
541
+ // link to the server and members chat with the agent on the board. A
542
+ // thread belongs to a board ("board" and "comment-<n>" are shared, a
543
+ // "chat-..." is its starter's own) and names the agent it talks to. Its
544
+ // messages are stored here (the person's text, the agent's answer with its
545
+ // cards in their final state); a turn is one question on its way through
546
+ // an agent, whose live events stay in memory. A device login remembers
547
+ // what the machine said it runs (meta), for the approval page.
548
+ `
549
+ ALTER TABLE device_logins ADD COLUMN meta TEXT;
550
+ CREATE TABLE chat_threads (
551
+ workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
552
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
553
+ board_id TEXT NOT NULL,
554
+ thread_id TEXT NOT NULL,
555
+ agent_id TEXT,
556
+ owner_user_id TEXT,
557
+ title TEXT,
558
+ title_source TEXT,
559
+ archived INTEGER NOT NULL DEFAULT 0,
560
+ created_by TEXT,
561
+ created_at TEXT NOT NULL,
562
+ updated_at TEXT NOT NULL,
563
+ PRIMARY KEY (workspace_id, project_id, board_id, thread_id)
564
+ );
565
+ CREATE TABLE chat_messages (
566
+ workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
567
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
568
+ board_id TEXT NOT NULL,
569
+ thread_id TEXT NOT NULL,
570
+ id TEXT PRIMARY KEY,
571
+ role TEXT NOT NULL CHECK (role IN ('user', 'assistant')),
572
+ author_user_id TEXT,
573
+ agent_id TEXT,
574
+ turn_id TEXT,
575
+ body TEXT NOT NULL,
576
+ bytes INTEGER NOT NULL DEFAULT 0,
577
+ created_at TEXT NOT NULL
578
+ );
579
+ CREATE INDEX chat_messages_thread ON chat_messages(workspace_id, project_id, board_id, thread_id, created_at);
580
+ CREATE TABLE chat_turns (
581
+ workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
582
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
583
+ id TEXT PRIMARY KEY,
584
+ board_id TEXT NOT NULL,
585
+ thread_id TEXT NOT NULL,
586
+ agent_id TEXT NOT NULL,
587
+ kind TEXT NOT NULL DEFAULT 'chat',
588
+ state TEXT NOT NULL DEFAULT 'queued',
589
+ acked_seq INTEGER NOT NULL DEFAULT 0,
590
+ started_by TEXT,
591
+ user_message_id TEXT,
592
+ run_id TEXT,
593
+ comment_number INTEGER,
594
+ created_at TEXT NOT NULL,
595
+ ended_at TEXT
596
+ );
597
+ CREATE INDEX chat_turns_thread ON chat_turns(workspace_id, project_id, board_id, thread_id, created_at);
598
+ CREATE INDEX chat_turns_agent ON chat_turns(workspace_id, agent_id, state);
599
+ `,
600
+ // 14: approvals and edits from a hosted board (0.6.0). chat_permissions is
601
+ // one row per permission card a connected agent asked during a fix: what
602
+ // it asked (the card, as hashed), its state, who answered and when. The
603
+ // usage record of a run gains how many requests it asked and how they
604
+ // ended. Additive; an older build ignores all of it.
605
+ `
606
+ CREATE TABLE chat_permissions (
607
+ workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
608
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
609
+ id TEXT NOT NULL,
610
+ turn_id TEXT NOT NULL,
611
+ agent_id TEXT NOT NULL,
612
+ board_id TEXT NOT NULL,
613
+ thread_id TEXT NOT NULL,
614
+ kind TEXT NOT NULL,
615
+ card TEXT NOT NULL,
616
+ card_hash TEXT NOT NULL,
617
+ state TEXT NOT NULL DEFAULT 'pending',
618
+ created_at TEXT NOT NULL,
619
+ expires_at TEXT NOT NULL,
620
+ answered_by TEXT,
621
+ answered_at TEXT,
622
+ PRIMARY KEY (workspace_id, turn_id, id)
623
+ );
624
+ CREATE INDEX chat_permissions_board ON chat_permissions(workspace_id, project_id, board_id, state);
625
+ ALTER TABLE agent_runs ADD COLUMN approvals_asked INTEGER;
626
+ ALTER TABLE agent_runs ADD COLUMN approvals_allowed INTEGER;
627
+ ALTER TABLE agent_runs ADD COLUMN approvals_denied INTEGER;
628
+ ALTER TABLE agents ADD COLUMN approval_rule_by TEXT;
629
+ `,
465
630
  ]
466
631
 
467
632
  export async function openDatabase(file) {
@@ -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,18 +21,20 @@ 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; a push's file
25
- // uploads, token and device approvals publish nothing.
26
- export function changeEventType(pathname) {
24
+ // policy, agent setting and push-commit writes change what watchers see, and a board
25
+ // removed in the console (`method` DELETE on the board itself); a push's
26
+ // file uploads, token and device approvals publish nothing.
27
+ export function changeEventType(pathname, method = 'POST') {
28
+ if (/\/projects\/[^/]+\/boards\/[^/]+$/.test(pathname)) return method === 'DELETE' ? 'board.removed' : null
27
29
  if (/\/pushes\/[^/]+\/commit$/.test(pathname)) return 'push.committed'
28
30
  if (/\/comments\/[1-9][0-9]*\/send-to-agent$/.test(pathname)) return 'comment.vouched'
29
31
  if (/\/comments\/[1-9][0-9]*\/resolve$/.test(pathname)) return 'comment.resolved'
30
32
  if (/\/boards\/[^/]+\/comments$/.test(pathname)) return 'comment.created'
31
33
  if (
32
- /\/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(
33
35
  pathname,
34
36
  ) ||
35
- /\/projects\/[^/]+\/agent$/.test(pathname)
37
+ /\/projects\/[^/]+\/(?:agent|default-agent|agents\/[^/]+(?:\/(?:audience|approval))?)$/.test(pathname)
36
38
  )
37
39
  return 'comment.updated'
38
40
  return null
@@ -47,7 +49,7 @@ export const publishMutations =
47
49
  res.once('finish', () => {
48
50
  for (const check of [...streams]) check()
49
51
  const pathname = req.originalUrl.split('?')[0]
50
- const type = res.statusCode < 400 && req.t && req.project ? changeEventType(pathname) : null
52
+ const type = res.statusCode < 400 && req.t && req.project ? changeEventType(pathname, req.method) : null
51
53
  if (type) {
52
54
  const comment = /\/comments\/([1-9][0-9]*)/.exec(pathname)?.[1]
53
55
  events.publish({
@@ -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,11 +171,28 @@ 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
177
194
  const comment = Number.isSafeInteger(event.comment) && event.comment > 0 ? event.comment : null
178
- if (!comment && !['push.committed', 'comment.created'].includes(event.type)) return
195
+ if (!comment && !['push.committed', 'comment.created', 'board.removed'].includes(event.type)) return
179
196
  for (const peer of [...room.peers])
180
197
  if (peer.access.kind !== 'public' && live(peer)) send(peer, { type: 'invalidate', comment, sequence: event.sequence || 0 })
181
198
  })
@@ -7,6 +7,7 @@
7
7
 
8
8
  import fs from 'node:fs'
9
9
  import path from 'node:path'
10
+ import { ID_RE } from '../config.js'
10
11
  import { ID_PATTERNS, newId } from './crypto.js'
11
12
  import { transaction } from './db.js'
12
13
 
@@ -104,7 +105,17 @@ export function createProjectStore(t, { db, dataDir, maxProjects = PROJECTS_MAX
104
105
  const project = get(projectId)
105
106
  if (!project) throw new ProjectError(404, 'unknown_project', 'No such project.')
106
107
  transaction(db, () => {
107
- 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
+ ]) {
108
119
  t.run(`DELETE FROM ${table} WHERE workspace_id = :ws AND project_id = :id`, { id: project.id })
109
120
  }
110
121
  t.run('DELETE FROM projects WHERE workspace_id = :ws AND id = :id', { id: project.id })
@@ -121,5 +132,52 @@ export function createProjectStore(t, { db, dataDir, maxProjects = PROJECTS_MAX
121
132
  board: String(boardId || ''),
122
133
  }) || null
123
134
 
124
- return { get, list, create, remove, boards, board }
135
+ // One board of a project, gone from this server: its rows in one
136
+ // transaction (comment images before the comments they hang on, the board
137
+ // row last), then its folder (captures, live HTML, versions, variants,
138
+ // checks) and the staging folders of pushes still open for it. The
139
+ // project, its tokens and its share links stay; a later push of the same
140
+ // board id starts a new board (comment numbers from 1 again). The
141
+ // workspace's storage count is a sum over these rows, so it drops by
142
+ // itself. Returns what went: { board, comments, bytes }.
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
+ ]
156
+ function removeBoard(projectId, boardId) {
157
+ const id = String(boardId || '')
158
+ const project = get(projectId)
159
+ const row = project && ID_RE.test(id) ? board(project.id, id) : null
160
+ if (!row) throw new ProjectError(404, 'unknown_board', 'No such board.')
161
+ const key = { project: project.id, board: row.board_id }
162
+ const gone = transaction(db, () => {
163
+ const where = 'WHERE workspace_id = :ws AND project_id = :project AND board_id = :board'
164
+ const comments = t.get(`SELECT COUNT(*) AS n FROM comments ${where}`, key).n
165
+ const images = t.get(`SELECT COALESCE(SUM(bytes), 0) AS n FROM comment_files ${where}`, key).n
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
+ })
172
+ for (const table of BOARD_TABLES) t.run(`DELETE FROM ${table} ${where}`, key)
173
+ return { comments, bytes: row.bytes + images, open }
174
+ })
175
+ fs.rmSync(path.join(projectDir(dataDir, t.workspaceId, project.id), row.board_id), { recursive: true, force: true })
176
+ for (const pushId of gone.open) {
177
+ if (/^[a-z0-9_]+$/i.test(pushId)) fs.rmSync(path.join(dataDir, 'staging', pushId), { recursive: true, force: true })
178
+ }
179
+ return { board: row.board_id, comments: gone.comments, bytes: gone.bytes }
180
+ }
181
+
182
+ return { get, list, create, remove, removeBoard, boards, board }
125
183
  }
@@ -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.