shapewall 0.3.2 → 0.3.3

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 (75) hide show
  1. package/CHANGELOG.md +108 -0
  2. package/NOTICE +7 -0
  3. package/README.md +1 -1
  4. package/THIRD_PARTY.md +39 -0
  5. package/dist/agent/package-lock.json +2 -2
  6. package/dist/client/assets/BoardChat-DxehB761.js +27 -0
  7. package/dist/client/assets/BuzzDialog-Ix9qJvim.js +1 -0
  8. package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +1 -0
  9. package/dist/client/assets/History-C9N32kCv.js +1 -0
  10. package/dist/client/assets/LocalSettings-DawtKnvi.js +1 -0
  11. package/dist/client/assets/{Management-DmTh1-tA.js → Management-NVFDtSZB.js} +1 -1
  12. package/dist/client/assets/QuickConnect-DO1TjS1t.js +1 -0
  13. package/dist/client/assets/ShareDialog-By0ETg6n.js +2 -0
  14. package/dist/client/assets/app-update-dialog-RGIw24iZ.js +1 -0
  15. package/dist/client/assets/{circle-x-3UxAvczM.js → circle-x-B6KfScOE.js} +1 -1
  16. package/dist/client/assets/index-CsqLdqGx.css +2 -0
  17. package/dist/client/assets/index-DnZarJ3U.js +37 -0
  18. package/dist/client/assets/{refresh-cw-BRHNyCnY.js → refresh-cw-vkgvCCfR.js} +1 -1
  19. package/dist/client/assets/rolldown-runtime-XKyaHtl9.js +1 -0
  20. package/dist/client/assets/{shield-check-DMpniYP6.js → shield-check-QzwGhz0t.js} +1 -1
  21. package/dist/client/assets/ui-DI-FGWWK.js +41 -0
  22. package/dist/client/build-info.json +1 -1
  23. package/dist/client/index.html +4 -3
  24. package/dist/client/sw.js +2 -2
  25. package/dist/console/assets/{LocalAccountsApp-IvHd9zWN.js → LocalAccountsApp-VOCZg8dg.js} +1 -1
  26. package/dist/console/assets/{LocalConsole-DOyLou56.js → LocalConsole-CMwAW-mF.js} +1 -1
  27. package/dist/console/assets/{app-update-dialog-Da46CJpv.js → app-update-dialog-Cw6UDqNP.js} +1 -1
  28. package/dist/console/assets/console-DgfWmUEx.js +55 -0
  29. package/dist/console/assets/console-Lis0vvdL.css +2 -0
  30. package/dist/console/build-info.json +1 -1
  31. package/dist/console/console.html +2 -2
  32. package/docs/agents.md +62 -2
  33. package/docs/config.md +1 -1
  34. package/docs/deployment.md +2 -0
  35. package/docs/design-system.md +20 -15
  36. package/docs/hosted.md +19 -0
  37. package/docs/where-to-run.md +88 -0
  38. package/package.json +2 -1
  39. package/src/agent/agent-logos.js +81 -0
  40. package/src/agent/config.js +27 -2
  41. package/src/agent/icon-store.js +100 -0
  42. package/src/agent/identity.js +77 -0
  43. package/src/agent/owner-mcp.js +19 -3
  44. package/src/agent/routes.js +80 -20
  45. package/src/agent/run.js +3 -1
  46. package/src/agent/settings-routes.js +233 -14
  47. package/src/agent/test-timing.js +14 -0
  48. package/src/agent/tools.js +4 -2
  49. package/src/agent/user-settings.js +51 -3
  50. package/src/capture/element-map.js +61 -4
  51. package/src/hosted/agent-presence.js +40 -0
  52. package/src/hosted/app.js +6 -0
  53. package/src/hosted/board-routes.js +7 -0
  54. package/src/hosted/console-routes.js +47 -1
  55. package/src/hosted/events.js +6 -4
  56. package/src/hosted/presence.js +1 -1
  57. package/src/hosted/projects.js +32 -1
  58. package/src/hosted/token-api.js +5 -1
  59. package/src/hosted/token-events.js +5 -2
  60. package/src/hosted/tokens.js +3 -2
  61. package/src/mcp.js +10 -2
  62. package/src/store.js +5 -0
  63. package/dist/client/assets/BoardChat-BeRp6007.js +0 -27
  64. package/dist/client/assets/BuzzDialog-CrYy48hh.js +0 -1
  65. package/dist/client/assets/ChecksPanel-CB820mhj.js +0 -1
  66. package/dist/client/assets/History-Ba909bGJ.js +0 -1
  67. package/dist/client/assets/LocalSettings-ChMi9Zir.js +0 -1
  68. package/dist/client/assets/QuickConnect-CrVweWZB.js +0 -1
  69. package/dist/client/assets/ShareDialog-DO2HhY6g.js +0 -2
  70. package/dist/client/assets/app-update-dialog-C09ODdUQ.js +0 -1
  71. package/dist/client/assets/index-BIjfZKnp.js +0 -37
  72. package/dist/client/assets/index-CXtuJi1K.css +0 -2
  73. package/dist/client/assets/ui-mhL9gsne.js +0 -41
  74. package/dist/console/assets/console-CIBv7cXU.js +0 -55
  75. package/dist/console/assets/console-Fp4k9iyx.css +0 -2
package/src/hosted/app.js CHANGED
@@ -60,6 +60,7 @@ import { mountDeviceApi } from './device-routes.js'
60
60
  import { createBoardContext } from './board-context.js'
61
61
  import { mountBoardRoutes } from './board-routes.js'
62
62
  import { mountTokenApi } from './token-api.js'
63
+ import { createAgentPresence } from './agent-presence.js'
63
64
  import { mountPages } from './pages.js'
64
65
 
65
66
  export { changeEventType, publicBoardView, TOKEN_SCOPES, INVITE_DAYS }
@@ -131,6 +132,8 @@ export function createHostedApp({
131
132
  deviceLogins ||= createDeviceLogins({ db, now })
132
133
  const ownsEvents = !eventBus
133
134
  const events = eventBus || createEventBus()
135
+ // Which projects have an agent listening right now (in memory only).
136
+ const agentPresence = createAgentPresence({ now })
134
137
  const streams = new Set()
135
138
  const policyFor = (t) =>
136
139
  createAgentPolicy(t, { now, handleFor: (user) => q.get('SELECT handle FROM users WHERE id = :id', { id: user })?.handle || null })
@@ -373,6 +376,7 @@ export function createHostedApp({
373
376
  mountPublicAdmin(consoleApi, deps)
374
377
 
375
378
  const { approvalApi } = mountBoardRoutes(app, {
379
+ agentPresence,
376
380
  db,
377
381
  dataDir,
378
382
  iso,
@@ -401,6 +405,7 @@ export function createHostedApp({
401
405
  mountDeviceApi(app, consoleDeps)
402
406
 
403
407
  const { v1, tokenAuth } = mountTokenApi({
408
+ agentPresence,
404
409
  db,
405
410
  dataDir,
406
411
  iso,
@@ -502,6 +507,7 @@ export function createHostedApp({
502
507
  const presence = hub.listener(presenceAuthorizer({ db, dataDir, origin: publicOrigin, auth, now, guestOnly }), { clientIp: upgradeIp })
503
508
  app.locals.presence = { attach: presence.attach, close: presence.close, hub }
504
509
  app.locals.events = events
510
+ app.locals.agentPresence = agentPresence
505
511
  app.locals.sweep = sweep
506
512
  app.locals.close = async () => {
507
513
  presence.close()
@@ -29,6 +29,7 @@ import { roleAtLeast, tenant } from './tenancy.js'
29
29
  export function mountBoardRoutes(
30
30
  app,
31
31
  {
32
+ agentPresence = null,
32
33
  db,
33
34
  dataDir,
34
35
  iso,
@@ -340,6 +341,12 @@ export function mountBoardRoutes(
340
341
  ...hostedBoardView(ctx),
341
342
  agentDispatch: Boolean(trusted),
342
343
  agentApprove: Boolean(trusted && roleAtLeast(req.role, 'admin')),
344
+ // Whether an agent is listening for this project, for members
345
+ // only (never a share link guest or a public viewer): the token's
346
+ // display name and when it was last heard from.
347
+ ...(agentPresence && !req.guest && !req.publicViewer
348
+ ? { agentListening: agentPresence.of(req.t.workspaceId, req.project.id) }
349
+ : {}),
343
350
  })
344
351
  }),
345
352
  )
@@ -9,7 +9,7 @@ import { agentPolicy, cleanPolicy } from './agent-policy.js'
9
9
  import { newId, randomToken, sha256 } from './crypto.js'
10
10
  import { transaction } from './db.js'
11
11
  import { mountDeviceApprovals } from './device-routes.js'
12
- import { HttpError, fail, cleanInviteNote, cleanWorkspaceName } from './http.js'
12
+ import { HttpError, fail, cleanInviteNote, cleanName, cleanWorkspaceName } from './http.js'
13
13
  import { cleanEmail } from './mail.js'
14
14
  import {
15
15
  NEEDS,
@@ -343,6 +343,23 @@ export function mountConsoleRoutes(consoleApi, deps) {
343
343
  }),
344
344
  )
345
345
 
346
+ // One board off the server (pushed by mistake, or retired): the same role
347
+ // and the same fresh passkey check as deleting the project it is in. An
348
+ // unknown board, a malformed id and a board of another workspace all
349
+ // answer the same 404. The write publishes `board.removed`
350
+ // (src/hosted/events.js), so open boards and token streams hear of it.
351
+ consoleApi.delete(
352
+ '/workspaces/:ws/projects/:pid/boards/:bid',
353
+ inWorkspace(NEEDS.projects),
354
+ needFresh,
355
+ inProject,
356
+ handle(async (req, res) => {
357
+ const gone = req.projects.removeBoard(req.project.id, req.params.bid)
358
+ auditOf(req, 'board_removed', `${req.project.id}/${gone.board}`, { project: req.project.name, comments: gone.comments })
359
+ res.json({ ok: true, board: gone.board, comments: gone.comments, bytes: gone.bytes })
360
+ }),
361
+ )
362
+
346
363
  consoleApi.get(
347
364
  '/workspaces/:ws/projects/:pid/tokens',
348
365
  inWorkspace(NEEDS.tokens),
@@ -384,6 +401,35 @@ export function mountConsoleRoutes(consoleApi, deps) {
384
401
  )
385
402
 
386
403
  mountDeviceApprovals(consoleApi, deps)
404
+ // Renames a token: { name }. A watcher's comments and replies are signed
405
+ // with its token's name, so this is what an agent is called on the board
406
+ // from now on (what it already wrote keeps the name it was written with).
407
+ // The same people who may revoke it; the secret and scopes do not change.
408
+ consoleApi.patch(
409
+ '/workspaces/:ws/projects/:pid/tokens/:tid',
410
+ inWorkspace(NEEDS.tokens),
411
+ inProject,
412
+ handle(async (req, res) => {
413
+ // A name too long is refused, not cut: it is what people will see.
414
+ const name = cleanName(req.body?.name, 61)
415
+ if (!name || name.length > 60) throw new HttpError(400, 'invalid_name', 'Token name: 1 to 60 characters.')
416
+ const id = String(req.params.tid)
417
+ const token = req.t.get(
418
+ 'SELECT name FROM api_tokens WHERE workspace_id = :ws AND project_id = :project AND id = :id AND revoked_at IS NULL',
419
+ { project: req.project.id, id },
420
+ )
421
+ if (!token) throw new HttpError(404, 'unknown_token', 'No such token.')
422
+ if (token.name !== name) {
423
+ req.t.run('UPDATE api_tokens SET name = :name WHERE workspace_id = :ws AND project_id = :project AND id = :id', {
424
+ name,
425
+ project: req.project.id,
426
+ id,
427
+ })
428
+ auditOf(req, 'token_renamed', id, { from: token.name, to: name })
429
+ }
430
+ res.json({ token: listTokens(req.t, { projectId: req.project.id, now: iso() }).find((item) => item.id === id) })
431
+ }),
432
+ )
387
433
  consoleApi.delete(
388
434
  '/workspaces/:ws/projects/:pid/tokens/:tid',
389
435
  inWorkspace(NEEDS.tokens),
@@ -21,9 +21,11 @@ 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 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'
@@ -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({
@@ -175,7 +175,7 @@ export function createPresenceHub({ events = null, limits = {}, now = () => Date
175
175
  const room = rooms.get(`${event.workspace}:${event.project}:${event.board}`)
176
176
  if (!room) return
177
177
  const comment = Number.isSafeInteger(event.comment) && event.comment > 0 ? event.comment : null
178
- if (!comment && !['push.committed', 'comment.created'].includes(event.type)) return
178
+ if (!comment && !['push.committed', 'comment.created', 'board.removed'].includes(event.type)) return
179
179
  for (const peer of [...room.peers])
180
180
  if (peer.access.kind !== 'public' && live(peer)) send(peer, { type: 'invalidate', comment, sequence: event.sequence || 0 })
181
181
  })
@@ -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
 
@@ -121,5 +122,35 @@ export function createProjectStore(t, { db, dataDir, maxProjects = PROJECTS_MAX
121
122
  board: String(boardId || ''),
122
123
  }) || null
123
124
 
124
- return { get, list, create, remove, boards, board }
125
+ // One board of a project, gone from this server: its rows in one
126
+ // transaction (comment images before the comments they hang on, the board
127
+ // row last), then its folder (captures, live HTML, versions, variants,
128
+ // checks) and the staging folders of pushes still open for it. The
129
+ // project, its tokens and its share links stay; a later push of the same
130
+ // board id starts a new board (comment numbers from 1 again). The
131
+ // workspace's storage count is a sum over these rows, so it drops by
132
+ // itself. Returns what went: { board, comments, bytes }.
133
+ const BOARD_TABLES = ['comment_files', 'comment_imports', 'agent_approvals', 'agent_claims', 'comments', 'pushes', 'boards']
134
+ function removeBoard(projectId, boardId) {
135
+ const id = String(boardId || '')
136
+ const project = get(projectId)
137
+ const row = project && ID_RE.test(id) ? board(project.id, id) : null
138
+ if (!row) throw new ProjectError(404, 'unknown_board', 'No such board.')
139
+ const key = { project: project.id, board: row.board_id }
140
+ const gone = transaction(db, () => {
141
+ const where = 'WHERE workspace_id = :ws AND project_id = :project AND board_id = :board'
142
+ const comments = t.get(`SELECT COUNT(*) AS n FROM comments ${where}`, key).n
143
+ const images = t.get(`SELECT COALESCE(SUM(bytes), 0) AS n FROM comment_files ${where}`, key).n
144
+ const open = t.all(`SELECT id FROM pushes ${where} AND status = 'open'`, key).map((push) => push.id)
145
+ for (const table of BOARD_TABLES) t.run(`DELETE FROM ${table} ${where}`, key)
146
+ return { comments, bytes: row.bytes + images, open }
147
+ })
148
+ fs.rmSync(path.join(projectDir(dataDir, t.workspaceId, project.id), row.board_id), { recursive: true, force: true })
149
+ for (const pushId of gone.open) {
150
+ if (/^[a-z0-9_]+$/i.test(pushId)) fs.rmSync(path.join(dataDir, 'staging', pushId), { recursive: true, force: true })
151
+ }
152
+ return { board: row.board_id, comments: gone.comments, bytes: gone.bytes }
153
+ }
154
+
155
+ return { get, list, create, remove, removeBoard, boards, board }
125
156
  }
@@ -31,6 +31,7 @@ import { findApiToken, tenant } from './tenancy.js'
31
31
  import { mountTokenEvents } from './token-events.js'
32
32
 
33
33
  export function mountTokenApi({
34
+ agentPresence = null,
34
35
  db,
35
36
  dataDir,
36
37
  iso,
@@ -97,6 +98,9 @@ export function mountTokenApi({
97
98
  req.t.run('UPDATE api_tokens SET last_used_at = :at WHERE workspace_id = :ws AND id = :id', { at: iso(), id: row.id })
98
99
  }
99
100
  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)
100
104
  next()
101
105
  }
102
106
  v1.use(tokenAuth)
@@ -274,7 +278,7 @@ export function mountTokenApi({
274
278
  // Any `author` in the request is ignored, so a token cannot pose as a person.
275
279
  const tokenAuthor = (req) => ({ name: cleanAuthor(req.token.name, 'agent'), agent: true })
276
280
 
277
- mountTokenEvents(v1, { db, iso, events, streams, scope })
281
+ mountTokenEvents(v1, { db, iso, events, streams, scope, agentPresence })
278
282
 
279
283
  v1.get(
280
284
  '/project',
@@ -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 }) {
11
+ export function mountTokenEvents(v1, { db, iso, events, streams, scope, agentPresence = 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.')
@@ -51,7 +51,10 @@ export function mountTokenEvents(v1, { db, iso, events, streams, scope }) {
51
51
  })
52
52
  timer = setInterval(() => {
53
53
  check()
54
- if (!closed) res.write(': keepalive\n\n')
54
+ if (closed) return
55
+ 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)
55
58
  }, 15000)
56
59
  timer.unref()
57
60
  res.write(': connected\n\n')
@@ -14,8 +14,9 @@ export function createApiTokens({ iso, auditOf }) {
14
14
  // the token (shown once) and its listing. `via` marks how it was made
15
15
  // ("login" for `shapewall login`) in the audit log.
16
16
  function createApiToken(req, body, { via = null } = {}) {
17
- const name = cleanName(body.name, 60)
18
- if (!name) throw new HttpError(400, 'invalid_name', 'Token name: 1 to 60 characters.')
17
+ // A name too long is refused, not cut (as on a rename): it is what people will see.
18
+ const name = cleanName(body.name, 61)
19
+ if (!name || name.length > 60) throw new HttpError(400, 'invalid_name', 'Token name: 1 to 60 characters.')
19
20
  const scopes = Array.isArray(body.scopes) ? expandScopes(body.scopes) : ['read']
20
21
  if (!scopes.length || scopes.some((scope) => !TOKEN_SCOPES.includes(scope))) {
21
22
  throw new HttpError(400, 'invalid_scopes', `Scopes: ${TOKEN_SCOPES.join(', ')}.`)
package/src/mcp.js CHANGED
@@ -146,7 +146,15 @@ export function createMcpServer({
146
146
  fetchImpl = undefined,
147
147
  boardId = null,
148
148
  toolNames = null,
149
+ // The board's own agent (src/agent/owner-mcp.js): a function giving the
150
+ // name its owner gave it. Its replies are signed with it and marked as an
151
+ // agent's. null: replies are signed "agent", as for any outside agent.
152
+ agentAuthor = null,
149
153
  }) {
154
+ const signed = (author) => {
155
+ const name = author ? null : agentAuthor?.()
156
+ return name ? { author: name, agent: true } : { author: author || 'agent' }
157
+ }
150
158
  const server = new McpServer({ name: 'shapewall', version: VERSION }, { instructions: INSTRUCTIONS })
151
159
  const boardFor = (id) => {
152
160
  if (boardId && id && id !== boardId) throw new Error('This agent session is scoped to one board.')
@@ -300,7 +308,7 @@ export function createMcpServer({
300
308
  },
301
309
  tool(async ({ board, id, body, author }) => {
302
310
  const target = boardFor(board)
303
- const { comment, reply } = await addReply(config, target.id, id, { body, author: author || 'agent' })
311
+ const { comment, reply } = await addReply(config, target.id, id, { body, ...signed(author) })
304
312
  return json({
305
313
  reply,
306
314
  comment: { id: comment.id, status: comment.status, agentStatus: comment.agentStatus, replies: comment.replies.length },
@@ -323,7 +331,7 @@ export function createMcpServer({
323
331
  },
324
332
  tool(async ({ board, id, agentStatus, note }) => {
325
333
  const target = boardFor(board)
326
- if (note && note.trim()) await addReply(config, target.id, id, { body: note, author: 'agent' })
334
+ if (note && note.trim()) await addReply(config, target.id, id, { body: note, ...signed(null) })
327
335
  const { comment } = await setAgentStatus(config, target.id, id, agentStatus === 'clear' ? null : agentStatus, { actor: 'agent' })
328
336
  return json({ id: comment.id, status: comment.status, agentStatus: comment.agentStatus })
329
337
  }),
package/src/store.js CHANGED
@@ -825,6 +825,7 @@ export function getComment(config, boardId, commentId) {
825
825
 
826
826
  // Adds a reply. Returns { comment, reply }.
827
827
  // `images` and `drawing` as on a new comment (validateCommentMedia).
828
+ // `agent: true` marks a reply the board's agent wrote under its own name.
828
829
  export async function addReply(config, boardId, commentId, input, { now = new Date() } = {}) {
829
830
  const body = cleanCommentBody(input?.body, { media: hasCommentMedia(input) })
830
831
  const author = cleanAuthor(input?.author)
@@ -846,6 +847,10 @@ export async function addReply(config, boardId, commentId, input, { now = new Da
846
847
  body,
847
848
  createdAt: now.toISOString(),
848
849
  }
850
+ // Written by the board's own agent under the name its owner gave it
851
+ // ("Harry"): the flag says it is an agent, since the name no longer
852
+ // does. Only the server's own agent tools set it, never a request body.
853
+ if (input?.agent === true) reply.agent = true
849
854
  if (images.length) reply.images = images
850
855
  if (media.drawing) {
851
856
  const version = String(target.pageId).startsWith('variant:')