shapewall 0.6.0 → 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 (127) hide show
  1. package/CHANGELOG.md +252 -0
  2. package/README.md +39 -2
  3. package/THIRD_PARTY.md +15 -0
  4. package/dist/agent/package-lock.json +594 -2
  5. package/dist/client/assets/{BoardChat-DdwBcb2N.js → BoardChat-WS8mAIW6.js} +1 -1
  6. package/dist/client/assets/{BuzzDialog-DpUcnU6S.js → BuzzDialog-B-Mlr6wF.js} +1 -1
  7. package/dist/client/assets/{ChecksPanel-CngYV4yz.js → ChecksPanel-Dx1ZLBR6.js} +1 -1
  8. package/dist/client/assets/{History-CD0saQA5.js → History-sXhmpcvs.js} +1 -1
  9. package/dist/client/assets/{LocalSettings-MZy2baig.js → LocalSettings-CtGzz9F3.js} +1 -1
  10. package/dist/client/assets/{Management-CSvZ-SHO.js → Management-CGHe8Wt9.js} +1 -1
  11. package/dist/client/assets/{QuickConnect-foLh2QUm.js → QuickConnect-DEk84E4A.js} +1 -1
  12. package/dist/client/assets/{ShareDialog-DPWDftoL.js → ShareDialog-CGGx-AYK.js} +1 -1
  13. package/dist/client/assets/{app-update-dialog-CtiXpTRD.js → app-update-dialog-_bVp8hnC.js} +1 -1
  14. package/dist/client/assets/{circle-x-CteRBtXC.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-BjRNIGSf.js → play-BzQ3tRTv.js} +1 -1
  18. package/dist/client/assets/{refresh-cw-r_tZiFfB.js → refresh-cw-BYBMyDKT.js} +1 -1
  19. package/dist/client/assets/{ui-zQpn-PdL.js → ui-CAY6myRA.js} +1 -1
  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-CWRguRRL.js +1 -0
  24. package/dist/console/assets/LocalConsole--I2SaIpr.js +2 -0
  25. package/dist/console/assets/LocalUsage-BmpCZ36k.js +1 -0
  26. package/dist/console/assets/app-update-dialog-B8M9plaT.js +1 -0
  27. package/dist/console/assets/app-version-muKrQqbL.js +1 -0
  28. package/dist/console/assets/console-CrhTsPbz.css +2 -0
  29. package/dist/console/assets/console-DxapQ9oS.js +11 -0
  30. package/dist/console/assets/local-agents-CoqD4dSN.js +6 -0
  31. package/dist/console/assets/minus-DDQ0ydcM.js +1 -0
  32. package/dist/console/assets/tailnet-C5Flwl_d.js +2 -0
  33. package/dist/console/assets/theme-DCd0qh4l.js +1 -0
  34. package/dist/console/assets/ui-Bry1mnCY.js +41 -0
  35. package/dist/console/assets/usage-CIPWnHaS.css +1 -0
  36. package/dist/console/assets/usage-CqVjMb7m.js +5 -0
  37. package/dist/console/assets/usage-D1G0GgE3.js +56 -0
  38. package/dist/console/build-info.json +1 -1
  39. package/dist/console/console.html +8 -2
  40. package/docs/agent-hardening.md +31 -17
  41. package/docs/agents.md +35 -0
  42. package/docs/architecture.md +10 -4
  43. package/docs/deployment.md +39 -17
  44. package/docs/design-system.md +56 -7
  45. package/docs/hosted.md +1004 -43
  46. package/docs/images/board-overview.png +0 -0
  47. package/docs/images/journeys.png +0 -0
  48. package/docs/images/live-comment.png +0 -0
  49. package/docs/images/phone-comment.png +0 -0
  50. package/docs/images/phone-overview.png +0 -0
  51. package/docs/images/variant.png +0 -0
  52. package/docs/where-to-run.md +82 -1
  53. package/package.json +6 -1
  54. package/src/agent/backends/acp.js +224 -26
  55. package/src/agent/routes.js +21 -0
  56. package/src/agent/usage-log.js +251 -0
  57. package/src/cli/args.js +24 -0
  58. package/src/cli/help.js +37 -2
  59. package/src/cli/init.js +2 -1
  60. package/src/cli/serve.js +20 -5
  61. package/src/cli.js +8 -0
  62. package/src/hosted/access-cli.js +99 -1
  63. package/src/hosted/activity-core.js +232 -0
  64. package/src/hosted/activity.js +375 -0
  65. package/src/hosted/admin.js +80 -13
  66. package/src/hosted/agent-approvals.js +2 -2
  67. package/src/hosted/agent-routes.js +3 -3
  68. package/src/hosted/agents.js +11 -4
  69. package/src/hosted/app.js +89 -7
  70. package/src/hosted/auth-routes.js +156 -15
  71. package/src/hosted/auth.js +123 -20
  72. package/src/hosted/board-routes.js +3 -3
  73. package/src/hosted/chat-frames.js +5 -15
  74. package/src/hosted/chat-permissions.js +12 -0
  75. package/src/hosted/chat-routes.js +8 -8
  76. package/src/hosted/chat-store.js +20 -3
  77. package/src/hosted/comment-files.js +5 -2
  78. package/src/hosted/console-routes.js +111 -34
  79. package/src/hosted/db.js +130 -0
  80. package/src/hosted/device-routes.js +25 -3
  81. package/src/hosted/device.js +23 -4
  82. package/src/hosted/guards.js +337 -27
  83. package/src/hosted/http.js +6 -3
  84. package/src/hosted/membership.js +13 -2
  85. package/src/hosted/operator-access.js +418 -0
  86. package/src/hosted/operator.js +106 -2
  87. package/src/hosted/ops-cli.js +238 -3
  88. package/src/hosted/pages.js +2 -0
  89. package/src/hosted/passkey-policy.js +75 -0
  90. package/src/hosted/permissions.js +14 -6
  91. package/src/hosted/presence.js +14 -2
  92. package/src/hosted/projects.js +2 -1
  93. package/src/hosted/public.js +4 -4
  94. package/src/hosted/ratelimit.js +6 -0
  95. package/src/hosted/retention.js +18 -2
  96. package/src/hosted/server.js +123 -11
  97. package/src/hosted/shares.js +2 -2
  98. package/src/hosted/step-up-settings.js +68 -0
  99. package/src/hosted/step-up.js +135 -0
  100. package/src/hosted/tailnet-identity.js +24 -0
  101. package/src/hosted/tailnet-routes.js +342 -0
  102. package/src/hosted/tailnet.js +220 -0
  103. package/src/hosted/tailscale.js +75 -9
  104. package/src/hosted/tenancy.js +48 -1
  105. package/src/hosted/token-api.js +2 -2
  106. package/src/hosted/tokens.js +9 -2
  107. package/src/hosted/usage-core.js +624 -0
  108. package/src/hosted/usage-layout.js +724 -0
  109. package/src/hosted/usage-routes.js +375 -0
  110. package/src/hosted/usage-shape.js +256 -0
  111. package/src/hosted/usage.js +641 -0
  112. package/src/remote/agent-connect.js +37 -1
  113. package/src/remote/agent-link.js +2 -0
  114. package/src/remote/login.js +33 -8
  115. package/src/remote/share.js +1 -1
  116. package/src/remote/tailnet-cli.js +547 -0
  117. package/src/server/app.js +15 -0
  118. package/src/server/pairing-routes.js +34 -5
  119. package/src/server/tailscale.js +97 -4
  120. package/src/tailnet-proof.js +766 -0
  121. package/dist/client/assets/index-Cxz1UVcV.css +0 -2
  122. package/dist/client/assets/index-uUZVBYUS.js +0 -37
  123. package/dist/console/assets/LocalAccountsApp-DIdy1KnN.js +0 -1
  124. package/dist/console/assets/LocalConsole-DfcQztF-.js +0 -1
  125. package/dist/console/assets/app-update-dialog-ttl9lF5A.js +0 -1
  126. package/dist/console/assets/console-OqAjD0kV.js +0 -55
  127. package/dist/console/assets/console-kvjrAArQ.css +0 -2
@@ -123,12 +123,24 @@ export function createPermissionStore(t, { now = () => new Date() } = {}) {
123
123
  AND expires_at > :at ORDER BY created_at DESC LIMIT 50`,
124
124
  { project: projectId, board: boardId, at: iso() },
125
125
  )
126
+ // How the requests of a turn ended, for its usage record. `allowed` counts
127
+ // every request that went ahead (a person's "allow" and the automatic
128
+ // ones); `auto` is the part nobody was asked about; `by` says who answered
129
+ // the rest (user id to how many they allowed and denied). Ids and counts only.
126
130
  const counts = (turnId) => {
127
131
  const all = ofTurn(turnId)
132
+ const by = {}
133
+ for (const row of all) {
134
+ if (!row.answered_by || !['approved', 'denied'].includes(row.state)) continue
135
+ by[row.answered_by] ||= { allowed: 0, denied: 0 }
136
+ by[row.answered_by][row.state === 'approved' ? 'allowed' : 'denied'] += 1
137
+ }
128
138
  return {
129
139
  asked: all.length,
130
140
  allowed: all.filter((row) => ['approved', 'auto'].includes(row.state)).length,
131
141
  denied: all.filter((row) => ['denied', 'expired', 'cancelled'].includes(row.state)).length,
142
+ auto: all.filter((row) => row.state === 'auto').length,
143
+ by,
132
144
  }
133
145
  }
134
146
  const removeThread = (projectId, boardId, threadId) =>
@@ -41,7 +41,7 @@
41
41
  // - answering a card: the role the agent's answer setting names (admin by
42
42
  // default), in the agent's audience and trusted by the project, and the
43
43
  // person who connected it alone when its machine says "owner"
44
- // (src/hosted/agents.js answerCheck). Allow needs a fresh passkey check.
44
+ // (src/hosted/agents.js answerCheck). Allow asks for the passkey again when the workspace says so.
45
45
  // The answer must carry the hash of the card as it was shown, before the
46
46
  // card expires (2 minutes), once.
47
47
 
@@ -95,7 +95,7 @@ export function mountChatRoutes(
95
95
  quotas = null,
96
96
  workspaceQuota = undefined,
97
97
  policyFor = null,
98
- freshOrThrow = () => {},
98
+ stepUpOrThrow = () => {},
99
99
  now = () => new Date(),
100
100
  },
101
101
  ) {
@@ -463,7 +463,7 @@ export function mountChatRoutes(
463
463
  const author = { name: req.user.display_name, handle: req.user.handle }
464
464
  // A run that still needs an admin's approval (the project's agent policy
465
465
  // asks for one for a non-admin's comment). Either the person pressing Fix
466
- // may give it: then Fix gives it, with their passkey, and starts. Or they
466
+ // may give it: then Fix gives it and starts. Or they
467
467
  // may not: then the comment is really sent for approval (the vouch stays,
468
468
  // addressed to this agent, and admins see "Approve agent run" on it), and
469
469
  // the answer says so. Never a message about an approval nobody can find.
@@ -476,8 +476,8 @@ export function mountChatRoutes(
476
476
  policy.vouch(req.project.id, ctx.board.id, comment, req.user.id, { role: req.role, approve: false, expect })
477
477
  let now2 = ctx.comments.get(number)
478
478
  if (!policy.state(req.project.id, ctx.board.id, now2).dispatchable && admin) {
479
- // An admin's Fix is also their approval of the run: the same passkey check as "Approve agent run".
480
- freshOrThrow(req)
479
+ // An admin's Fix is also their approval of the run: the passkey again when "Approve agent run" asks for it.
480
+ stepUpOrThrow(req, 'cards')
481
481
  policy.vouch(req.project.id, ctx.board.id, now2, req.user.id, { role: req.role, approve: true, expect })
482
482
  now2 = ctx.comments.get(number)
483
483
  }
@@ -913,8 +913,8 @@ export function mountChatRoutes(
913
913
  // transaction that writes the answer: the card is this thread's running
914
914
  // turn's, it still waits and has not expired, the hash is the one of the
915
915
  // card as it was shown, and this person may answer this agent's cards.
916
- // Allow needs a passkey check in the last minutes; Deny does not (it only
917
- // ever stops something). Then the machine hears it, and checks again.
916
+ // Allow asks for the passkey again when the workspace switched that on
917
+ // ("Agent edits and pushes"); Deny never does (it only ever stops something). Then the machine hears it, and checks again.
918
918
  router.post(
919
919
  '/boards/:bid/threads/:thread/permissions/:permission',
920
920
  handle((req, res) => {
@@ -941,7 +941,7 @@ export function mountChatRoutes(
941
941
  throw new HttpError(409, 'no_permission', 'This request is no longer waiting for an answer.')
942
942
  if (typeof hash !== 'string' || !CARD_HASH_RE.test(hash) || hash !== card.card_hash)
943
943
  throw new HttpError(409, 'permission_changed', 'This is not the request you were shown. Reload and read it again.')
944
- if (allow) freshOrThrow(req)
944
+ if (allow) stepUpOrThrow(req, 'cards')
945
945
  if (!perms.answer(live.id, id, { state: allow ? 'approved' : 'denied', userId: req.user.id }))
946
946
  throw new HttpError(409, 'no_permission', 'This request is no longer waiting for an answer.')
947
947
  const shown = perms.cardOf(card)
@@ -309,9 +309,13 @@ export function createChatStore(t, { now = () => new Date() } = {}) {
309
309
  },
310
310
  )
311
311
  // The usage record: who asked which agent, where and when. No text.
312
+ // `agent_owner`: the person who connected the agent (they may read
313
+ // everything that ran on their machine, src/hosted/usage.js).
312
314
  t.run(
313
- `INSERT INTO agent_runs (workspace_id, project_id, id, kind, agent_id, claim_id, board_id, comment_number, thread_id, asked_by, started_at)
314
- VALUES (:ws, :project, :id, :kind, :agent, :claim, :board, :number, :thread, :asked, :at)`,
315
+ `INSERT INTO agent_runs (workspace_id, project_id, id, kind, agent_id, claim_id, board_id, comment_number, thread_id, asked_by, started_at,
316
+ agent_owner)
317
+ VALUES (:ws, :project, :id, :kind, :agent, :claim, :board, :number, :thread, :asked, :at,
318
+ (SELECT a.created_by FROM agents a WHERE a.id = :agent))`,
315
319
  {
316
320
  project: projectId,
317
321
  id: runId,
@@ -364,10 +368,15 @@ export function createChatStore(t, { now = () => new Date() } = {}) {
364
368
  { id: row.id, state, at },
365
369
  )
366
370
  if (!Number(result.changes)) return false
371
+ const started = t.get('SELECT started_at FROM agent_runs WHERE workspace_id = :ws AND id = :run', { run: row.run_id })?.started_at
372
+ const tools = usage.tools && Object.keys(usage.tools).length ? JSON.stringify(usage.tools) : null
373
+ const by = usage.approvals?.by && Object.keys(usage.approvals.by).length ? JSON.stringify(usage.approvals.by) : null
367
374
  t.run(
368
375
  `UPDATE agent_runs SET ended_at = :at, outcome = :outcome, model = :model, effort = :effort, input_tokens = :input,
369
376
  output_tokens = :output, cached_tokens = :cached, cost_micros = :cost, steps = :steps,
370
- approvals_asked = :asked, approvals_allowed = :allowed, approvals_denied = :denied
377
+ approvals_asked = :asked, approvals_allowed = :allowed, approvals_denied = :denied, approvals_auto = :auto,
378
+ approvals_by = :by, duration_ms = :duration, agent_ms = :agent_ms, tools = :tools, window_used = :window_used,
379
+ window_size = :window_size
371
380
  WHERE workspace_id = :ws AND id = :run AND ended_at IS NULL`,
372
381
  {
373
382
  run: row.run_id,
@@ -383,6 +392,14 @@ export function createChatStore(t, { now = () => new Date() } = {}) {
383
392
  asked: usage.approvals?.asked ?? null,
384
393
  allowed: usage.approvals?.allowed ?? null,
385
394
  denied: usage.approvals?.denied ?? null,
395
+ auto: usage.approvals?.auto ?? null,
396
+ by,
397
+ // The server's own clock, from the question to the end of the turn.
398
+ duration: started ? Math.max(0, Date.parse(at) - Date.parse(started)) : null,
399
+ agent_ms: usage.agentMs ?? null,
400
+ tools,
401
+ window_used: usage.contextUsed ?? null,
402
+ window_size: usage.contextSize ?? null,
386
403
  },
387
404
  )
388
405
  return true
@@ -11,6 +11,7 @@
11
11
  // copy step. They are small and capped (4 per comment or reply, 5 MB each,
12
12
  // the workspace storage quota over all of them).
13
13
 
14
+ import { USAGE_ROW_BYTES } from './usage-shape.js'
14
15
  import { COMMENT_FILE_RE, CommentError, readImages } from '../comment-fields.js'
15
16
  import { cleanCommentImage, newImageFile, renderMarkup } from '../comment-media.js'
16
17
  import { allWorkspaces, tenant } from './tenancy.js'
@@ -37,14 +38,16 @@ export async function cleanImages(cleaner, inputs) {
37
38
  }
38
39
 
39
40
  // Bytes of every comment image the workspace holds (attached or staged).
40
- // Stored chat messages (src/hosted/chat-store.js) count with them: every
41
+ // Stored chat messages (src/hosted/chat-store.js) and usage records count with them: every
41
42
  // caller adds this to the boards for the workspace's storage.
42
43
  export function commentFileBytes(t) {
43
44
  return (
44
45
  t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM comment_files WHERE workspace_id = :ws').n +
45
46
  t.get('SELECT COALESCE(SUM(bytes), 0) AS n FROM chat_messages WHERE workspace_id = :ws').n +
46
47
  // The permission cards of connected agents (their diffs): src/hosted/chat-permissions.js.
47
- t.get('SELECT COALESCE(SUM(length(card)), 0) AS n FROM chat_permissions WHERE workspace_id = :ws').n
48
+ t.get('SELECT COALESCE(SUM(length(card)), 0) AS n FROM chat_permissions WHERE workspace_id = :ws').n +
49
+ // Usage records (src/hosted/usage.js): small rows, counted at a fixed size each.
50
+ t.get('SELECT COUNT(*) AS n FROM agent_runs WHERE workspace_id = :ws').n * USAGE_ROW_BYTES
48
51
  )
49
52
  }
50
53
 
@@ -11,19 +11,14 @@ import { transaction } from './db.js'
11
11
  import { mountDeviceApprovals } from './device-routes.js'
12
12
  import { HttpError, fail, cleanInviteNote, cleanName, cleanWorkspaceName } from './http.js'
13
13
  import { cleanEmail } from './mail.js'
14
- import {
15
- NEEDS,
16
- canInvite,
17
- canManage,
18
- canRemoveMember,
19
- inviteNeedsFresh,
20
- leavesNoOwner,
21
- removalNeedsFresh,
22
- roleChangeNeedsFresh,
23
- } from './permissions.js'
14
+ import { NEEDS, canInvite, canManage, canRemoveMember, leavesNoOwner } from './permissions.js'
15
+ import { roleChangeTouchesAdmin, roleChangeTouchesOwner } from './step-up.js'
16
+ import { stepUpSettings } from './step-up-settings.js'
17
+ import { cleanTailnetLogin } from './tailnet-identity.js'
24
18
  import { projectPublic } from './public.js'
25
19
  import { LIMITS } from './ratelimit.js'
26
20
  import { SHARE_DAYS } from './shares.js'
21
+ import { createViews } from './usage.js'
27
22
  import { ROLES, findInviteByHash, inviteUsable, roleAtLeast, roleIn, tenant, workspacesForUser } from './tenancy.js'
28
23
 
29
24
  export const INVITE_DAYS = [1, 3, 7, 14, 30]
@@ -33,7 +28,9 @@ const DAY = 86_400_000
33
28
 
34
29
  // Invite links: preview needs no session, accepting one does.
35
30
  export function mountInviteLinks(consoleApi, deps) {
36
- const { db, iso, limit, handle, sessionOf, needSession, clientIp, acceptInvite } = deps
31
+ const { db, iso, limit, handle, sessionOf, needSession, clientIp, acceptInvite, tailnet = null } = deps
32
+ // The tailnet login a request arrives with (an invite can be for one login only).
33
+ const arrivesAs = (req) => (tailnet ? tailnet.identityOf(req)?.login || null : null)
37
34
 
38
35
  // Invite preview needs no session (the link is the secret). Invite and
39
36
  // setup links carry their token in the URL fragment, which browsers never
@@ -56,7 +53,9 @@ export function mountInviteLinks(consoleApi, deps) {
56
53
  usable: inviteUsable(invite, iso()),
57
54
  })
58
55
  }
59
- if (!inviteUsable(invite, iso())) return fail(res, 404, 'invalid_invite', 'This invite link is used, expired or revoked.')
56
+ // An invite made for a tailnet login reads as invalid to everyone else.
57
+ if (!inviteUsable(invite, iso()) || (invite.tailnet_login && invite.tailnet_login !== arrivesAs(req)))
58
+ return fail(res, 404, 'invalid_invite', 'This invite link is used, expired or revoked.')
60
59
  res.json({ workspace: { name: invite.workspace_name }, role: invite.role, expiresAt: invite.expires_at })
61
60
  }),
62
61
  )
@@ -67,7 +66,11 @@ export function mountInviteLinks(consoleApi, deps) {
67
66
  handle(async (req, res) => {
68
67
  const invite = typeof req.body?.token === 'string' ? findInviteByHash(db, sha256(req.body.token)) : null
69
68
  if (!invite) throw new AuthError(403, 'invalid_invite', 'This invite link is used, expired or revoked.')
70
- acceptInvite(tenant(db, invite.workspace_id), invite, req.user.id, clientIp(req))
69
+ // The account must be the one linked to the login it arrives with.
70
+ const login = arrivesAs(req)
71
+ acceptInvite(tenant(db, invite.workspace_id), invite, req.user.id, clientIp(req), {
72
+ tailnetLogin: login && req.user.tailnet_login === login ? login : null,
73
+ })
71
74
  res.json({ workspaceId: invite.workspace_id })
72
75
  }),
73
76
  )
@@ -88,15 +91,32 @@ export function mountConsoleRoutes(consoleApi, deps) {
88
91
  handle,
89
92
  inWorkspace,
90
93
  inProject,
91
- needFresh,
92
- freshOrThrow,
94
+ coreFreshOrThrow,
95
+ machineTokensOrThrow,
96
+ adminMakingOrThrow,
97
+ stepUp,
98
+ stepUpOrThrow,
93
99
  auditOf,
94
100
  createWorkspace,
95
101
  revokeGrantsOf,
96
102
  ownerCount,
97
103
  createApiToken,
98
104
  agentsFor,
105
+ tailnet = null,
106
+ tailnetAccounts = null,
107
+ passkeys = null,
99
108
  } = deps
109
+ // Making, changing or removing an owner always asks for the passkey; the
110
+ // same for an admin when the workspace asks for it ("Members and roles").
111
+ // makesAdmin: the request gives someone the admin role (an invite, or a
112
+ // change of role). With "Approve command line logins in the browser" on,
113
+ // a session that only a tailnet identity stands behind may not: the new
114
+ // admin could make the tokens that switch keeps from it (guards.js).
115
+ const roleStepUp = (req, { owner, admin, makesAdmin = false }) => {
116
+ if (owner) coreFreshOrThrow(req)
117
+ else if (admin) stepUpOrThrow(req, 'members')
118
+ if (makesAdmin && !owner) adminMakingOrThrow(req)
119
+ }
100
120
 
101
121
  consoleApi.get(
102
122
  '/workspaces',
@@ -120,6 +140,7 @@ export function mountConsoleRoutes(consoleApi, deps) {
120
140
  const workspace = t.get(
121
141
  'SELECT id, name, created_at, share_links, public_projects, suspended_at, suspended_reason FROM workspaces WHERE workspace_id = :ws',
122
142
  )
143
+ const stepUpNow = stepUpSettings(t).get()
123
144
  const members = t
124
145
  .all(
125
146
  `SELECT m.user_id, m.role, m.created_at, m.last_active_at, u.handle, u.display_name FROM memberships m JOIN users u ON u.id = m.user_id
@@ -140,17 +161,29 @@ export function mountConsoleRoutes(consoleApi, deps) {
140
161
  const invites = roleAtLeast(req.role, NEEDS.invites)
141
162
  ? t
142
163
  .all(
143
- 'SELECT id, role, created_at, expires_at, note FROM invites WHERE workspace_id = :ws AND used_at IS NULL AND revoked_at IS NULL AND expires_at > :now ORDER BY created_at DESC',
164
+ 'SELECT id, role, created_at, expires_at, note, tailnet_login FROM invites WHERE workspace_id = :ws AND used_at IS NULL AND revoked_at IS NULL AND expires_at > :now ORDER BY created_at DESC',
144
165
  { now: iso() },
145
166
  )
146
- .map((row) => ({ id: row.id, role: row.role, createdAt: row.created_at, expiresAt: row.expires_at, note: row.note || null }))
167
+ .map((row) => ({
168
+ id: row.id,
169
+ role: row.role,
170
+ createdAt: row.created_at,
171
+ expiresAt: row.expires_at,
172
+ note: row.note || null,
173
+ tailnetLogin: row.tailnet_login || null,
174
+ }))
147
175
  : []
148
176
  res.json({
149
177
  workspace: {
150
178
  id: workspace.id,
151
179
  name: workspace.name,
152
180
  createdAt: workspace.created_at,
153
- settings: { shareLinks: Boolean(workspace.share_links), publicProjects: Boolean(workspace.public_projects) },
181
+ settings: {
182
+ shareLinks: Boolean(workspace.share_links),
183
+ publicProjects: Boolean(workspace.public_projects),
184
+ // When this workspace asks for the passkey again (src/hosted/step-up.js).
185
+ stepUp: stepUpNow,
186
+ },
154
187
  // Set by the server operator (shapewall hosted admin suspend).
155
188
  suspended: workspace.suspended_at ? { at: workspace.suspended_at, reason: workspace.suspended_reason || null } : null,
156
189
  },
@@ -160,7 +193,17 @@ export function mountConsoleRoutes(consoleApi, deps) {
160
193
  projects,
161
194
  invites,
162
195
  // What the console can offer: invite emails only when the server can send them.
163
- features: { inviteEmail: inviteEmailOn, inviteDays: INVITE_DAYS, shareDays: SHARE_DAYS, publicProjects: true, guestSessions: true },
196
+ features: {
197
+ inviteEmail: inviteEmailOn,
198
+ inviteDays: INVITE_DAYS,
199
+ shareDays: SHARE_DAYS,
200
+ publicProjects: true,
201
+ guestSessions: true,
202
+ // Invites for a tailnet login, and the list of tailnet visitors.
203
+ tailnetSignIn: Boolean(tailnet),
204
+ // false: Tailscale is the only sign-in here (no passkeys, so nothing in "Ask for a passkey again" applies).
205
+ passkeys: passkeys ? passkeys.on() : true,
206
+ },
164
207
  })
165
208
  }),
166
209
  )
@@ -183,8 +226,7 @@ export function mountConsoleRoutes(consoleApi, deps) {
183
226
  handle(async (req, res) => {
184
227
  const role = req.body?.role
185
228
  if (!ROLES.includes(role)) throw new HttpError(400, 'invalid_role', `Role: ${ROLES.join(', ')}.`)
186
- // Granting admin or owner needs a recent passkey check.
187
- if (inviteNeedsFresh(role)) freshOrThrow(req)
229
+ roleStepUp(req, { owner: role === 'owner', admin: role === 'admin', makesAdmin: role === 'admin' })
188
230
  let revoked = { invites: 0, shares: 0, tokens: 0 }
189
231
  transaction(db, () => {
190
232
  const target = req.t.get('SELECT role FROM memberships WHERE workspace_id = :ws AND user_id = :user', {
@@ -194,8 +236,8 @@ export function mountConsoleRoutes(consoleApi, deps) {
194
236
  if (!canManage(req.role, target.role, role)) throw new HttpError(403, 'forbidden', 'Only an owner can change owners and admins.')
195
237
  if (leavesNoOwner({ targetRole: target.role, newRole: role, owners: ownerCount(req.t) }))
196
238
  throw new HttpError(409, 'last_owner', 'A workspace needs an owner.')
197
- // Taking power away from an owner or admin needs a recent passkey check too.
198
- if (roleChangeNeedsFresh(target.role, role)) freshOrThrow(req)
239
+ // Taking power away from an owner (always) or an admin (when the workspace asks).
240
+ roleStepUp(req, { owner: roleChangeTouchesOwner(target.role, role), admin: roleChangeTouchesAdmin(target.role, role) })
199
241
  req.t.run('UPDATE memberships SET role = :role WHERE workspace_id = :ws AND user_id = :user', {
200
242
  role,
201
243
  user: String(req.params.uid),
@@ -221,8 +263,12 @@ export function mountConsoleRoutes(consoleApi, deps) {
221
263
  if (!canRemoveMember(req.role, target.role, { self })) throw new HttpError(403, 'forbidden', 'You cannot remove this member.')
222
264
  if (leavesNoOwner({ targetRole: target.role, owners: ownerCount(req.t) }))
223
265
  throw new HttpError(409, 'last_owner', 'A workspace needs an owner.')
224
- if (removalNeedsFresh(target.role, { self })) freshOrThrow(req)
266
+ // An owner who leaves is an owner removed, like any other: the fixed core asks. Leaving with a
267
+ // lesser role is one's own business and asks nothing.
268
+ roleStepUp(req, { owner: target.role === 'owner', admin: !self && target.role === 'admin' })
225
269
  req.t.run('DELETE FROM memberships WHERE workspace_id = :ws AND user_id = :user', { user: userId })
270
+ // Their saved views on the Usage and Activity pages go with them.
271
+ createViews(req.t).removeOf(userId)
226
272
  auditOf(req, self ? 'member_left' : 'member_removed', userId, { role: target.role })
227
273
  // Their API tokens are revoked too, unless an admin asks to keep them
228
274
  // ({ revokeTokens: false }). Kept or not, they stop working: a token
@@ -242,9 +288,22 @@ export function mountConsoleRoutes(consoleApi, deps) {
242
288
  const role = req.body?.role || 'member'
243
289
  if (!ROLES.includes(role)) throw new HttpError(400, 'invalid_role', `Role: ${ROLES.join(', ')}.`)
244
290
  if (!canInvite(req.role, role)) throw new HttpError(403, 'forbidden', 'Only an owner can invite an owner.')
245
- if (inviteNeedsFresh(role)) freshOrThrow(req)
291
+ roleStepUp(req, { owner: role === 'owner', admin: role === 'admin', makesAdmin: role === 'admin' })
246
292
  const days = req.body?.expiresInDays ?? INVITE_DAYS_DEFAULT
247
293
  if (!INVITE_DAYS.includes(days)) throw new HttpError(400, 'invalid_expiry', `expiresInDays: one of ${INVITE_DAYS.join(', ')}.`)
294
+ // An invite for one tailnet login: only an account linked to that login
295
+ // (or a first visit that arrives as it) can take it.
296
+ let tailnetLogin = null
297
+ if (req.body?.tailnetLogin !== undefined && req.body?.tailnetLogin !== null && req.body?.tailnetLogin !== '') {
298
+ if (!tailnet)
299
+ throw new HttpError(409, 'tailnet_off', 'Tailnet sign-in is off on this server, so an invite cannot be for a tailnet login.')
300
+ tailnetLogin = cleanTailnetLogin(req.body.tailnetLogin)
301
+ if (!tailnetLogin)
302
+ throw new HttpError(400, 'invalid_login', 'Tailnet login: the name the person signs in to Tailscale with, like ada@example.com.')
303
+ const holder = tailnetAccounts.userFor(tailnetLogin)
304
+ if (holder && roleIn(db, holder.id, req.t.workspaceId))
305
+ throw new HttpError(409, 'already_member', `${tailnetLogin} is already in this workspace.`)
306
+ }
248
307
  let email = null
249
308
  if (req.body?.email !== undefined && req.body?.email !== null && req.body?.email !== '') {
250
309
  if (!inviteEmailOn) throw new HttpError(409, 'email_off', 'This server does not send invite emails. Copy the link instead.')
@@ -275,10 +334,16 @@ export function mountConsoleRoutes(consoleApi, deps) {
275
334
  const id = newId('iv')
276
335
  const expiresAt = iso(days * DAY)
277
336
  req.t.run(
278
- 'INSERT INTO invites (workspace_id, id, token_hash, role, created_by, created_at, expires_at, email, note) VALUES (:ws, :id, :hash, :role, :user, :at, :expires, :email, :note)',
279
- { id, hash: sha256(token), role, user: req.user.id, at: iso(), expires: expiresAt, email, note },
337
+ 'INSERT INTO invites (workspace_id, id, token_hash, role, created_by, created_at, expires_at, email, note, tailnet_login) VALUES (:ws, :id, :hash, :role, :user, :at, :expires, :email, :note, :tailnet)',
338
+ { id, hash: sha256(token), role, user: req.user.id, at: iso(), expires: expiresAt, email, note, tailnet: tailnetLogin },
280
339
  )
281
- auditOf(req, 'invite_created', id, { role, days, ...(email ? { emailed: true } : {}), ...(note ? { note } : {}) })
340
+ auditOf(req, 'invite_created', id, {
341
+ role,
342
+ days,
343
+ ...(email ? { emailed: true } : {}),
344
+ ...(note ? { note } : {}),
345
+ ...(tailnetLogin ? { tailnet: tailnetLogin } : {}),
346
+ })
282
347
  const url = `${publicOrigin}/invite#${token}`
283
348
  let emailed = null
284
349
  if (email) {
@@ -294,7 +359,7 @@ export function mountConsoleRoutes(consoleApi, deps) {
294
359
  emailed = { ok: result.ok, error: result.ok ? null : result.error }
295
360
  if (result.ok) req.t.run('UPDATE invites SET emailed_at = :at WHERE workspace_id = :ws AND id = :id', { at: iso(), id })
296
361
  }
297
- res.status(201).json({ invite: { id, role, expiresAt, email, note }, url, emailed })
362
+ res.status(201).json({ invite: { id, role, expiresAt, email, note, tailnetLogin }, url, emailed })
298
363
  }),
299
364
  )
300
365
  consoleApi.delete(
@@ -335,7 +400,7 @@ export function mountConsoleRoutes(consoleApi, deps) {
335
400
  consoleApi.delete(
336
401
  '/workspaces/:ws/projects/:pid',
337
402
  inWorkspace(NEEDS.projects),
338
- needFresh,
403
+ stepUp('data'),
339
404
  inProject,
340
405
  handle(async (req, res) => {
341
406
  req.projects.remove(req.project.id)
@@ -352,7 +417,7 @@ export function mountConsoleRoutes(consoleApi, deps) {
352
417
  consoleApi.delete(
353
418
  '/workspaces/:ws/projects/:pid/boards/:bid',
354
419
  inWorkspace(NEEDS.projects),
355
- needFresh,
420
+ stepUp('data'),
356
421
  inProject,
357
422
  handle(async (req, res) => {
358
423
  const gone = req.projects.removeBoard(req.project.id, req.params.bid)
@@ -372,9 +437,11 @@ export function mountConsoleRoutes(consoleApi, deps) {
372
437
  consoleApi.post(
373
438
  '/workspaces/:ws/projects/:pid/tokens',
374
439
  inWorkspace(NEEDS.tokens),
375
- needFresh,
440
+ stepUp('tokens'),
376
441
  inProject,
377
442
  handle(async (req, res) => {
443
+ // A token is what a command line or an agent works with: where the workspace wants a passkey for those, here too.
444
+ machineTokensOrThrow(req)
378
445
  res.status(201).json(createApiToken(req, req.body || {}))
379
446
  }),
380
447
  )
@@ -388,15 +455,25 @@ export function mountConsoleRoutes(consoleApi, deps) {
388
455
  consoleApi.put(
389
456
  '/workspaces/:ws/projects/:pid/agent',
390
457
  inWorkspace('admin'),
391
- needFresh,
458
+ stepUp('agents'),
392
459
  inProject,
393
460
  handle((req, res) => {
394
461
  const policy = cleanPolicy(req.body)
462
+ const before = agentPolicy(req.t, req.project.id)
395
463
  req.t.run('UPDATE projects SET agent_policy = :policy WHERE workspace_id = :ws AND id = :project', {
396
464
  project: req.project.id,
397
465
  policy: JSON.stringify(policy),
398
466
  })
399
- auditOf(req, 'agent_policy_changed', req.project.id)
467
+ // What changed, setting by setting (names of settings and their values; the board overrides as a count).
468
+ const shown = (value) =>
469
+ Array.isArray(value) ? value.join(', ') || 'none' : value && typeof value === 'object' ? Object.keys(value).length : value
470
+ const changed = Object.keys(policy).filter((key) => JSON.stringify(policy[key]) !== JSON.stringify(before[key]))
471
+ auditOf(req, 'agent_policy_changed', req.project.id, {
472
+ name: req.project.name,
473
+ changed,
474
+ before: Object.fromEntries(changed.map((key) => [key, shown(before[key])])),
475
+ after: Object.fromEntries(changed.map((key) => [key, shown(policy[key])])),
476
+ })
400
477
  res.json({ policy })
401
478
  }),
402
479
  )
package/src/hosted/db.js CHANGED
@@ -627,6 +627,105 @@ const MIGRATIONS = [
627
627
  ALTER TABLE agent_runs ADD COLUMN approvals_denied INTEGER;
628
628
  ALTER TABLE agents ADD COLUMN approval_rule_by TEXT;
629
629
  `,
630
+ // 15: the Usage dashboard and people's activity (0.7.0). The usage record
631
+ // of a turn gains what the dashboard shows and nothing else: how long it
632
+ // took (duration_ms: the server's own clock; agent_ms: what the agent said),
633
+ // its tool calls counted by type (tools, JSON of name to count), requests
634
+ // allowed without asking and who answered the others (approvals_auto,
635
+ // approvals_by: JSON of user id to counts), the context window as the agent
636
+ // reported it, and the person who connected the agent (agent_owner: they
637
+ // may read everything that ran on their machine). Never text.
638
+ // saved_views: a person's named filters on the Usage and Activity pages
639
+ // (a name and a query string; a view never widens what its reader may see).
640
+ // retention_usage_days: how long a workspace keeps usage records (NULL: the
641
+ // server default). Indexes for the activity filters. Additive; an older
642
+ // build ignores all of it (docs/hosted.md, "Rolling back").
643
+ `
644
+ ALTER TABLE agent_runs ADD COLUMN agent_owner TEXT;
645
+ ALTER TABLE agent_runs ADD COLUMN duration_ms INTEGER;
646
+ ALTER TABLE agent_runs ADD COLUMN agent_ms INTEGER;
647
+ ALTER TABLE agent_runs ADD COLUMN tools TEXT;
648
+ ALTER TABLE agent_runs ADD COLUMN approvals_auto INTEGER;
649
+ ALTER TABLE agent_runs ADD COLUMN approvals_by TEXT;
650
+ ALTER TABLE agent_runs ADD COLUMN window_used INTEGER;
651
+ ALTER TABLE agent_runs ADD COLUMN window_size INTEGER;
652
+ UPDATE agent_runs SET agent_owner = (SELECT a.created_by FROM agents a WHERE a.id = agent_runs.agent_id);
653
+ UPDATE agent_runs SET duration_ms = MAX(0, CAST(ROUND((julianday(ended_at) - julianday(started_at)) * 86400000) AS INTEGER))
654
+ WHERE ended_at IS NOT NULL;
655
+ CREATE INDEX agent_runs_owner ON agent_runs(workspace_id, agent_owner, started_at);
656
+ CREATE INDEX agent_runs_thread ON agent_runs(workspace_id, project_id, board_id, thread_id);
657
+ CREATE TABLE saved_views (
658
+ workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
659
+ id TEXT PRIMARY KEY,
660
+ user_id TEXT NOT NULL,
661
+ page TEXT NOT NULL CHECK (page IN ('usage', 'activity')),
662
+ name TEXT NOT NULL,
663
+ query TEXT NOT NULL,
664
+ created_at TEXT NOT NULL,
665
+ updated_at TEXT NOT NULL
666
+ );
667
+ CREATE INDEX saved_views_user ON saved_views(workspace_id, user_id, page);
668
+ ALTER TABLE workspaces ADD COLUMN retention_usage_days INTEGER;
669
+ CREATE INDEX audit_workspace_at ON audit_log(workspace_id, at);
670
+ CREATE INDEX audit_workspace_user ON audit_log(workspace_id, user_id, id);
671
+ CREATE INDEX audit_account_user ON audit_log(user_id, id) WHERE workspace_id IS NULL;
672
+ `,
673
+ // 16: layouts of the Usage dashboard (0.7.0). One row per layout: a
674
+ // person's own (kind 'person', subject their user id), the workspace's
675
+ // default that people start from (kind 'default', subject ''), and the
676
+ // layout a saved view carries (kind 'view', subject the view's id). The
677
+ // layout is JSON, checked on the way in and again on the way out
678
+ // (src/hosted/usage-layout.js). One new table and nothing else: an older
679
+ // build ignores it (docs/hosted.md, "Migration 16 and rollback").
680
+ // The ledger below runs 16 and 17 in whichever order they reach a database: one made on the tailnet
681
+ // sign-in branch (17 ran, 16 absent) runs 16 later, and one that ran 16 first runs 17 next.
682
+ `
683
+ CREATE TABLE usage_layouts (
684
+ workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
685
+ kind TEXT NOT NULL CHECK (kind IN ('person', 'default', 'view')),
686
+ subject TEXT NOT NULL,
687
+ layout TEXT NOT NULL,
688
+ updated_by TEXT NOT NULL,
689
+ updated_at TEXT NOT NULL,
690
+ PRIMARY KEY (workspace_id, kind, subject)
691
+ );
692
+ `,
693
+ // 17. Step-up as a workspace's choice (step_up: a JSON object, '{}' is all off;
694
+ // src/hosted/step-up.js), and tailnet sign-in (src/hosted/tailnet.js): the
695
+ // tailnet login an account is linked to, the one a session came in with
696
+ // (such a session works only on requests that carry that identity), the
697
+ // one an invite is for, and the logins that arrived and belong to nobody
698
+ // yet. users.tailnet_user_id: Tailscale's own id of the user behind the
699
+ // linked login (a login string can be given to someone else later; the id
700
+ // cannot). passkeys.tailnet_added_from: set (to the device's label) on a
701
+ // passkey that a tailnet session added with no passkey check, the first
702
+ // of its account; with recovery_added_at as its 24 hour clock.
703
+ // server_settings: what the operator switched for the whole server
704
+ // (src/hosted/passkey-policy.js: passkeys on a tailnet sign-in server).
705
+ // Additive; an older build ignores all of it (docs/hosted.md,
706
+ // "Rolling back").
707
+ `
708
+ ALTER TABLE workspaces ADD COLUMN step_up TEXT NOT NULL DEFAULT '{}';
709
+ ALTER TABLE users ADD COLUMN tailnet_login TEXT;
710
+ ALTER TABLE users ADD COLUMN tailnet_linked_at TEXT;
711
+ ALTER TABLE users ADD COLUMN tailnet_user_id TEXT;
712
+ ALTER TABLE passkeys ADD COLUMN tailnet_added_from TEXT;
713
+ CREATE TABLE server_settings (
714
+ key TEXT PRIMARY KEY,
715
+ value TEXT NOT NULL,
716
+ updated_at TEXT NOT NULL
717
+ );
718
+ CREATE UNIQUE INDEX users_tailnet_login ON users(tailnet_login) WHERE tailnet_login IS NOT NULL;
719
+ ALTER TABLE sessions ADD COLUMN tailnet_login TEXT;
720
+ ALTER TABLE invites ADD COLUMN tailnet_login TEXT;
721
+ CREATE TABLE tailnet_visitors (
722
+ login TEXT PRIMARY KEY,
723
+ name TEXT NOT NULL DEFAULT '',
724
+ first_seen_at TEXT NOT NULL,
725
+ last_seen_at TEXT NOT NULL,
726
+ visits INTEGER NOT NULL DEFAULT 1
727
+ );
728
+ `,
630
729
  ]
631
730
 
632
731
  export async function openDatabase(file) {
@@ -638,6 +737,36 @@ export async function openDatabase(file) {
638
737
  return db
639
738
  }
640
739
 
740
+ // A database made by a build of 0.7.0 from before it was released, when the
741
+ // sign-in migration still had the number 16 (now 17; 16 is the saved Usage
742
+ // layouts'). Its ledger says "16 ran", which now means something else: the
743
+ // real 16 would never run, and 17 would run on top of its own tables and
744
+ // fail. Told apart by what is there: the ledger has 16 and not 17, the
745
+ // sign-in tables exist and usage_layouts does not. Repaired in place: what
746
+ // that early build did not have yet is added (each piece only if missing),
747
+ // and the ledger is corrected to "17 ran, 16 did not", so that both end
748
+ // applied once. A database where 16 is really the layouts' is left alone.
749
+ function repairEarlySignIn(db) {
750
+ const has = (version) => Boolean(db.prepare('SELECT 1 FROM schema_migrations WHERE version = ?').get(version))
751
+ const table = (name) => Boolean(db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(name))
752
+ const column = (name, col) =>
753
+ db
754
+ .prepare(`PRAGMA table_info(${name})`)
755
+ .all()
756
+ .some((row) => row.name === col)
757
+ if (!has(16) || has(17) || table('usage_layouts') || !table('tailnet_visitors') || !column('users', 'tailnet_login')) return
758
+ if (!column('workspaces', 'step_up')) db.exec("ALTER TABLE workspaces ADD COLUMN step_up TEXT NOT NULL DEFAULT '{}'")
759
+ if (!column('users', 'tailnet_linked_at')) db.exec('ALTER TABLE users ADD COLUMN tailnet_linked_at TEXT')
760
+ if (!column('users', 'tailnet_user_id')) db.exec('ALTER TABLE users ADD COLUMN tailnet_user_id TEXT')
761
+ if (!column('passkeys', 'tailnet_added_from')) db.exec('ALTER TABLE passkeys ADD COLUMN tailnet_added_from TEXT')
762
+ if (!column('sessions', 'tailnet_login')) db.exec('ALTER TABLE sessions ADD COLUMN tailnet_login TEXT')
763
+ if (!column('invites', 'tailnet_login')) db.exec('ALTER TABLE invites ADD COLUMN tailnet_login TEXT')
764
+ db.exec('CREATE UNIQUE INDEX IF NOT EXISTS users_tailnet_login ON users(tailnet_login) WHERE tailnet_login IS NOT NULL')
765
+ db.exec('CREATE TABLE IF NOT EXISTS server_settings (key TEXT PRIMARY KEY, value TEXT NOT NULL, updated_at TEXT NOT NULL)')
766
+ db.prepare('DELETE FROM schema_migrations WHERE version = 16').run()
767
+ db.prepare('INSERT INTO schema_migrations(version) VALUES (17)').run()
768
+ }
769
+
641
770
  // A ledger allows reserved workstreams to land out of order. user_version is
642
771
  // still the highest applied number, but an absent reserved migration is never
643
772
  // marked applied. Legacy databases (before the ledger) have contiguous 1..4.
@@ -651,6 +780,7 @@ export function migrate(db, migrations = MIGRATIONS) {
651
780
  throw new Error('Database has untracked migrations newer than version 4; restore its pre-upgrade backup before continuing.')
652
781
  for (let version = 1; version <= current; version += 1) db.prepare('INSERT INTO schema_migrations(version) VALUES (?)').run(version)
653
782
  }
783
+ if (migrations === MIGRATIONS) repairEarlySignIn(db)
654
784
  for (let index = 0; index < migrations.length; index += 1) {
655
785
  if (!migrations[index] || db.prepare('SELECT 1 FROM schema_migrations WHERE version = ?').get(index + 1)) continue
656
786
  db.exec(migrations[index])