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
@@ -0,0 +1,251 @@
1
+ // Usage of agents on a local board: one line per agent turn in
2
+ // <outputDir>/agent-usage.jsonl, the same record a hosted server keeps
3
+ // (src/hosted/usage-shape.js usageRecordOf): when, which board and thread
4
+ // kind, who asked (the owner or a viewer, never a name), which agent profile,
5
+ // what kind of turn, how it ended, how long it took, its tool calls by type,
6
+ // and the numbers the agent reported, each null when it reported none.
7
+ // Never the question, the answer, a file or a diff.
8
+ //
9
+ // - Appended after each turn, to a file only its owner reads (0600), under
10
+ // the same kind of file lock as the event log; a line that cannot be
11
+ // written is dropped (a turn never fails over its usage record).
12
+ // - Bounded: past LOG_MAX_BYTES the file is renamed to agent-usage.1.jsonl
13
+ // (the one older file kept) and a new one starts.
14
+ // - Read by the owner's Usage view (Console > Usage on a local board).
15
+
16
+ import fs from 'node:fs'
17
+ import path from 'node:path'
18
+ import { randomBytes } from 'node:crypto'
19
+ import { withFileLock } from '../lock.js'
20
+ import { OUTCOME_GROUP_IDS, outcomeGroup, usageRecordOf } from '../hosted/usage-shape.js'
21
+
22
+ export const USAGE_FILE = 'agent-usage.jsonl'
23
+ export const USAGE_ROTATED = 'agent-usage.1.jsonl'
24
+ export const LOG_MAX_BYTES = 5 * 1024 * 1024
25
+
26
+ const fileOf = (config, name = USAGE_FILE) => path.join(config.outputDir, name)
27
+
28
+ // What one finished turn leaves in the log. `reply`: the answer as stored
29
+ // (its text is not read). `kind`: chat, review (a comment's thread) or fix.
30
+ export function localUsageRow({
31
+ reply,
32
+ boardId,
33
+ threadId,
34
+ commentId = null,
35
+ kind,
36
+ viewer = 'owner',
37
+ agent = {},
38
+ startedAt,
39
+ now = Date.now(),
40
+ }) {
41
+ const record = usageRecordOf(reply)
42
+ const permissions = (Array.isArray(reply.cards) ? reply.cards : []).filter((card) => card.kind === 'permission')
43
+ const allowed = permissions.filter((card) => card.state === 'approved').length
44
+ return {
45
+ id: `run_${randomBytes(12).toString('hex')}`,
46
+ kind,
47
+ startedAt: new Date(startedAt).toISOString(),
48
+ endedAt: new Date(now).toISOString(),
49
+ durationMs: Math.max(0, now - startedAt),
50
+ boardId,
51
+ // The kind of thread, and the comment for a comment's thread. A private
52
+ // chat's id stays out of the log.
53
+ thread: threadId === 'board' ? 'board' : commentId ? 'comment' : 'chat',
54
+ commentNumber: commentId,
55
+ // "owner" or "viewer": a local board has no accounts to name.
56
+ askedBy: viewer === 'owner' ? 'owner' : 'viewer',
57
+ agent: {
58
+ id: String(agent.id || 'default').slice(0, 80),
59
+ name: String(agent.label || agent.id || 'Agent').slice(0, 80),
60
+ backend: agent.backend || null,
61
+ },
62
+ outcome: record.outcome,
63
+ model: record.model || agent.model || null,
64
+ effort: record.effort,
65
+ inputTokens: record.inputTokens,
66
+ outputTokens: record.outputTokens,
67
+ cachedTokens: record.cachedTokens,
68
+ costMicros: record.costMicros,
69
+ contextUsed: record.contextUsed,
70
+ contextSize: record.contextSize,
71
+ steps: record.steps,
72
+ tools: Object.keys(record.tools).length ? record.tools : null,
73
+ approvals: permissions.length ? { asked: permissions.length, allowed, denied: permissions.length - allowed } : null,
74
+ }
75
+ }
76
+
77
+ // Appends one row. The file is the owner's alone (0600, like the event
78
+ // log), and the size check, the rename and the append happen under the
79
+ // file's lock, so two processes never rotate or write at once. Never throws.
80
+ export async function recordLocalTurn(config, row, { maxBytes = LOG_MAX_BYTES } = {}) {
81
+ try {
82
+ const file = fileOf(config)
83
+ const data = Buffer.from(`${JSON.stringify(row)}\n`)
84
+ fs.mkdirSync(config.outputDir, { recursive: true })
85
+ await withFileLock(file, () => {
86
+ let size = 0
87
+ try {
88
+ size = fs.statSync(file).size
89
+ } catch {
90
+ /* a new log */
91
+ }
92
+ if (size > 0 && size + data.length > maxBytes) fs.renameSync(file, fileOf(config, USAGE_ROTATED))
93
+ const fd = fs.openSync(file, 'a', 0o600)
94
+ try {
95
+ // A log that was made wider is narrowed again.
96
+ fs.fchmodSync(fd, 0o600)
97
+ fs.writeSync(fd, data)
98
+ } finally {
99
+ fs.closeSync(fd)
100
+ }
101
+ })
102
+ return true
103
+ } catch {
104
+ return false
105
+ }
106
+ }
107
+
108
+ // Every row newer than `since` (ms), oldest first. Unreadable lines are skipped.
109
+ export function readLocalUsage(config, { since = 0 } = {}) {
110
+ const rows = []
111
+ for (const name of [USAGE_ROTATED, USAGE_FILE]) {
112
+ let text
113
+ try {
114
+ text = fs.readFileSync(fileOf(config, name), 'utf8')
115
+ } catch {
116
+ continue
117
+ }
118
+ for (const line of text.split('\n')) {
119
+ if (!line) continue
120
+ try {
121
+ const row = JSON.parse(line)
122
+ if (row && typeof row.startedAt === 'string' && Date.parse(row.startedAt) >= since) rows.push(row)
123
+ } catch {
124
+ /* a torn line */
125
+ }
126
+ }
127
+ }
128
+ return rows
129
+ }
130
+
131
+ const pad = (value) => String(value).padStart(2, '0')
132
+ // The local day of an instant, in this machine's time zone.
133
+ const dayOf = (ms) => {
134
+ const date = new Date(ms)
135
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
136
+ }
137
+ const sum = (a, b) => (a === null || a === undefined ? (b ?? null) : b === null || b === undefined ? a : a + b)
138
+
139
+ // A set of rows as one group of numbers, in the hosted server's shape
140
+ // (src/hosted/usage.js aggregate), so the same tiles and charts draw it.
141
+ function aggregate(rows) {
142
+ const out = {
143
+ calls: rows.length,
144
+ tokens: null,
145
+ input: null,
146
+ output: null,
147
+ cached: null,
148
+ cost: null,
149
+ duration: null,
150
+ steps: null,
151
+ reported: { tokens: 0, cost: 0, duration: 0 },
152
+ approvals: { asked: null, allowed: null, denied: null, auto: null },
153
+ outcomes: Object.fromEntries(OUTCOME_GROUP_IDS.map((id) => [id, 0])),
154
+ fixes: 0,
155
+ fixesDone: 0,
156
+ }
157
+ for (const row of rows) {
158
+ const tokens = row.inputTokens === null && row.outputTokens === null ? null : (row.inputTokens || 0) + (row.outputTokens || 0)
159
+ out.tokens = sum(out.tokens, tokens)
160
+ out.input = sum(out.input, row.inputTokens)
161
+ out.output = sum(out.output, row.outputTokens)
162
+ out.cached = sum(out.cached, row.cachedTokens)
163
+ out.cost = sum(out.cost, row.costMicros)
164
+ out.duration = sum(out.duration, row.durationMs)
165
+ out.steps = sum(out.steps, row.steps)
166
+ if (tokens !== null) out.reported.tokens += 1
167
+ if (row.costMicros !== null && row.costMicros !== undefined) out.reported.cost += 1
168
+ if (row.durationMs !== null && row.durationMs !== undefined) out.reported.duration += 1
169
+ if (row.approvals) for (const key of ['asked', 'allowed', 'denied']) out.approvals[key] = sum(out.approvals[key], row.approvals[key])
170
+ const group = outcomeGroup(row.outcome)
171
+ out.outcomes[group] += 1
172
+ if (row.kind === 'fix') {
173
+ out.fixes += 1
174
+ if (group === 'completed') out.fixesDone += 1
175
+ }
176
+ }
177
+ return out
178
+ }
179
+ function latency(rows) {
180
+ const values = rows
181
+ .map((row) => row.durationMs)
182
+ .filter((value) => Number.isFinite(value))
183
+ .sort((a, b) => a - b)
184
+ const rank = (share) => (values.length ? values[Math.max(1, Math.ceil(values.length * share)) - 1] : null)
185
+ return { turns: values.length, p50: rank(0.5), p95: rank(0.95), longest: values.at(-1) ?? null }
186
+ }
187
+
188
+ // What the local Usage view shows: the last `days` days against the days
189
+ // before, a series by day split by agent, and the newest turns.
190
+ export function summarizeLocalUsage(config, { days = 30, now = Date.now(), rowLimit = 200 } = {}) {
191
+ const today = new Date(now)
192
+ const end = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1).getTime()
193
+ const start = new Date(today.getFullYear(), today.getMonth(), today.getDate() - days + 1).getTime()
194
+ const before = start - (end - start)
195
+ const all = readLocalUsage(config, { since: before })
196
+ const current = all.filter((row) => Date.parse(row.startedAt) >= start && Date.parse(row.startedAt) < end)
197
+ const previous = all.filter((row) => Date.parse(row.startedAt) < start)
198
+ const buckets = []
199
+ for (let at = start; at < end; at = new Date(new Date(at).getFullYear(), new Date(at).getMonth(), new Date(at).getDate() + 1).getTime())
200
+ buckets.push(dayOf(at))
201
+ const byAgent = new Map()
202
+ const points = new Map()
203
+ for (const row of current) {
204
+ const key = row.agent?.id || 'default'
205
+ if (!byAgent.has(key)) byAgent.set(key, { key, label: row.agent?.name || key, rows: [] })
206
+ byAgent.get(key).rows.push(row)
207
+ const id = `${dayOf(Date.parse(row.startedAt))}\u0000${key}`
208
+ if (!points.has(id)) points.set(id, [])
209
+ points.get(id).push(row)
210
+ }
211
+ const groups = [...byAgent.values()]
212
+ .map((group) => ({ key: group.key, label: group.label, total: aggregate(group.rows) }))
213
+ .sort((a, b) => b.total.calls - a.total.calls)
214
+ const pointsOf = (map) =>
215
+ [...map.entries()].map(([id, rows]) => {
216
+ const [bucket, key] = id.split('\u0000')
217
+ return { bucket, key, ...aggregate(rows) }
218
+ })
219
+ const plain = new Map()
220
+ for (const row of current) {
221
+ const id = `${dayOf(Date.parse(row.startedAt))}\u0000`
222
+ if (!plain.has(id)) plain.set(id, [])
223
+ plain.get(id).push(row)
224
+ }
225
+ return {
226
+ days,
227
+ summary: {
228
+ period: { from: new Date(start).toISOString(), to: new Date(end).toISOString() },
229
+ current: { ...aggregate(current), latency: latency(current) },
230
+ previous: { ...aggregate(previous), latency: latency(previous) },
231
+ },
232
+ series: { bucket: 'day', groupBy: 'agent', buckets, groups, points: pointsOf(points) },
233
+ plain: {
234
+ bucket: 'day',
235
+ groupBy: 'none',
236
+ buckets,
237
+ groups: [{ key: '', label: '', total: aggregate(current) }],
238
+ points: pointsOf(plain),
239
+ },
240
+ rows: current
241
+ .slice()
242
+ .sort((a, b) => b.startedAt.localeCompare(a.startedAt))
243
+ .slice(0, rowLimit)
244
+ .map((row) => ({
245
+ ...row,
246
+ outcomeGroup: outcomeGroup(row.outcome),
247
+ totalTokens: row.inputTokens === null && row.outputTokens === null ? null : (row.inputTokens || 0) + (row.outputTokens || 0),
248
+ })),
249
+ total: current.length,
250
+ }
251
+ }
package/src/cli/args.js CHANGED
@@ -54,6 +54,10 @@ const BOOLEAN_FLAGS = new Set([
54
54
  'no-claim',
55
55
  'tailscale',
56
56
  'funnel',
57
+ 'tailnet-sign-in',
58
+ 'tailnet-passkeys',
59
+ 'yes',
60
+ 'link',
57
61
  'setup',
58
62
  'start',
59
63
  'print',
@@ -175,6 +179,11 @@ export const COMMAND_FLAGS = {
175
179
  'concurrency',
176
180
  'takeover',
177
181
  'approvals',
182
+ 'project',
183
+ 'workspace',
184
+ 'used-by',
185
+ 'icon',
186
+ 'tint',
178
187
  'allow-command',
179
188
  'allow-path',
180
189
  'auto',
@@ -209,6 +218,9 @@ export const COMMAND_FLAGS = {
209
218
  export: ['out', 'no-comments', 'browser-comments', 'noindex', 'include-checks'],
210
219
  push: ['remote', 'token-file', 'board', 'include-checks'],
211
220
  share: ['remote', 'token-file', 'board', 'dry-run', 'no-comments', 'include-checks'],
221
+ login: ['remote', 'project', 'workspace'],
222
+ join: ['remote', 'invite'],
223
+ admin: ['remote', 'workspace', 'name', 'login', 'link', 'role', 'days', 'json', 'yes'],
212
224
  hosted: [
213
225
  'data',
214
226
  'origin',
@@ -224,10 +236,22 @@ export const COMMAND_FLAGS = {
224
236
  'tailscale',
225
237
  'tailscale-port',
226
238
  'funnel',
239
+ 'tailnet-sign-in',
240
+ 'tailnet-passkeys',
241
+ 'yes',
242
+ 'role',
243
+ 'workspace',
244
+ 'link',
245
+ 'workspace-name',
246
+ 'login',
247
+ 'handle',
248
+ 'setup-link',
227
249
  'funnel-port',
228
250
  'quotas',
229
251
  'retention-hours',
230
252
  'retention-keep',
253
+ 'usage-days',
254
+ 'owners-see-sign-ins',
231
255
  'setup',
232
256
  'domain',
233
257
  'usercontent-domain',
package/src/cli/help.js CHANGED
@@ -140,6 +140,10 @@ export const SECTIONS = {
140
140
  project's agent, else Claude Code when found). Codex, goose and Gemini CLI are not
141
141
  offered in this release. Never a command from the server
142
142
  --name "Builder" the name to suggest on the approval page (the approver has the last word)
143
+ --project SLUG on a server with tailnet sign-in, run on a tailnet machine: no browser, your tailnet
144
+ user approves it. --project (and --workspace NAME) pick the project when there are
145
+ several; --used-by owner|trusted who may send it work (default owner: only you);
146
+ --icon NAME and --tint NAME as on the approval page
143
147
  --as NAME a second connection from this machine (its own token; default "default")
144
148
  --audience owner|server "owner": the server keeps it to the person who connects it even if its audience
145
149
  there is widened, and this machine refuses a job the server says is someone else's
@@ -238,7 +242,18 @@ export const SECTIONS = {
238
242
  --board ID move only this board (default all)
239
243
  --dry-run show counts; sends nothing
240
244
  --no-comments captures, history and journeys only`,
241
- login: ` login --remote URL get a project token through the browser; saved for the --remote commands`,
245
+ login: ` login --remote URL get a project token through the browser; saved for the --remote commands
246
+ --project SLUG on a server with tailnet sign-in, run on a tailnet machine: no code and no browser,
247
+ your tailnet user approves it (if its role may). --project, and --workspace NAME
248
+ when you are in several, pick the project; with one project neither is needed`,
249
+ join: ` join --remote URL join a workspace on a server with tailnet sign-in, from a tailnet machine: takes the
250
+ invite an admin made for your tailnet login. The role is the invite's
251
+ --invite LINK an invite link (or its code) instead`,
252
+ admin: ` admin projects list|create NAME | members list | agents list | invites create
253
+ a few admin commands on a server with tailnet sign-in, as your tailnet user (the
254
+ server checks your role). --remote URL; --workspace NAME when you are in several
255
+ --login LOGIN invites create: for one tailnet login (ada@example.com); or --link for a link
256
+ --role ROLE viewer, member (default), admin or owner; --days N until it expires; --name; --json`,
242
257
  push: ` push upload the latest captures to a hosted project
243
258
  --include-checks explicitly include private diagnostic reports
244
259
  --remote URL the hosted Shapewall (or $SHAPEWALL_REMOTE)
@@ -258,12 +273,28 @@ export const SECTIONS = {
258
273
  --usercontent-origin URL serve live HTML only from this other origin (or $SHAPEWALL_USERCONTENT_ORIGIN)
259
274
  --previous-origin URL while moving to a new origin: the old one (sign-in moves, up to 30 days)
260
275
  --report-email ADDR where "Report this board" on public projects goes
276
+ --owners-see-sign-ins workspace owners see their members' sign-ins and devices in Activity
277
+ (off: each person sees only their own; for a server with one team)
261
278
  --tailscale on your tailnet through tailscale serve (https://<machine>.<tailnet>.ts.net)
262
279
  --tailscale-port N default 443; --funnel: share links public through Tailscale Funnel
263
280
  (share pages only, never the console), on --funnel-port 8443|443|10000
281
+ --tailnet-sign-in with --tailscale: a tailnet user is signed in as the account linked to their tailnet
282
+ login, no passkey (or $SHAPEWALL_HOSTED_TAILNET_SIGN_IN=1). An admin still decides
283
+ who is in a workspace. Needs Linux and Tailscale 1.98+ in its normal (TUN) mode, not
284
+ in a container; refuses to start where it cannot prove that a request came through
285
+ tailscale serve. An identity is per device: what runs as you there can act as you.
286
+ Tailscale is then the only sign-in: no passkeys, and the few things that always ask
287
+ take a typed confirmation
288
+ --tailnet-passkeys with --tailnet-sign-in: also allow passkeys ($SHAPEWALL_HOSTED_TAILNET_PASSKEYS=1, or
289
+ hosted admin passkeys on|off). An account that adds one confirms with it
264
290
  --quotas FILE per-workspace quotas (default <data>/quotas.json when it exists)
265
291
  --retention-hours N prune old versions every N hours (default 6, 0 = off); --retention-keep N (2)
266
292
  hosted setup-link print the first account's one-time link (it is not in the log)
293
+ hosted claim --data DIR --login LOGIN
294
+ with --tailnet-sign-in: make the first account on the server's own machine, no
295
+ browser. Whoever can read the data folder says whose server it is: --login is the
296
+ owner's tailnet login (ada@example.com); --workspace-name NAME. From another tailnet
297
+ device: hosted claim --setup-link URL (the server then sees who asks)
267
298
  --data DIR | --new --new: make a new link (the old one stops working)
268
299
  hosted move-origin --to URL move the server to a new origin (passkeys move with a rebind)
269
300
  --data DIR | --days N | --status window 1 to 30 days (default 30)
@@ -271,11 +302,13 @@ export const SECTIONS = {
271
302
  --usercontent-domain HOST live HTML on its own domain; --email (TLS); --dir (./shapewall-hosted)
272
303
  --source DIR a Shapewall checkout to build from; --dry-run; --force; --start (build, start, setup link)
273
304
  hosted admin list|suspend|resume|delete|export|reports|retention|log the operator's commands
305
+ hosted admin members|invites|projects|passkeys ... who is in a workspace and how people sign in, from the
306
+ server's own machine (shapewall hosted admin help lists them)
274
307
  hosted backup --to DIR [--keep-days N] a consistent copy while the server runs
275
308
  hosted restore --from BACKUP --data NEW | --from BACKUP --check (the restore drill)`,
276
309
  }
277
310
 
278
- const LOCAL_COMMANDS = Object.keys(SECTIONS).filter((name) => !['login', 'push', 'hosted'].includes(name))
311
+ const LOCAL_COMMANDS = Object.keys(SECTIONS).filter((name) => !['login', 'join', 'admin', 'push', 'hosted'].includes(name))
279
312
 
280
313
  const GLOBAL_HELP = `Global
281
314
  --config FILE default ./${CONFIG_FILE} (or $SHAPEWALL_CONFIG)
@@ -290,6 +323,8 @@ ${LOCAL_COMMANDS.map((name) => SECTIONS[name]).join('\n')}
290
323
 
291
324
  Hosted Shapewall (docs/hosted.md)
292
325
  ${SECTIONS.login}
326
+ ${SECTIONS.join}
327
+ ${SECTIONS.admin}
293
328
  ${SECTIONS.push}
294
329
  ${SECTIONS.hosted}
295
330
 
package/src/cli/init.js CHANGED
@@ -142,7 +142,8 @@ export async function init(flags, out, { fetch }) {
142
142
  const gitignore = path.join(path.dirname(file), '.gitignore')
143
143
  // .shapewall/agent-worktrees/ is inside .shapewall/; named too so it stays
144
144
  // ignored if someone narrows the first line (agent branch mode's checkouts).
145
- const wanted = ['.shapewall/', 'shapewall-export/', '*.auth.json', '.shapewall/agent-worktrees/']
145
+ // The agent usage log (who used which agent, when) is named too, for the same reason.
146
+ const wanted = ['.shapewall/', 'shapewall-export/', '*.auth.json', '.shapewall/agent-worktrees/', '.shapewall/agent-usage*.jsonl']
146
147
  const current = fs.existsSync(gitignore) ? fs.readFileSync(gitignore, 'utf8') : ''
147
148
  const missing = wanted.filter((line) => !current.split(/\r?\n/).includes(line))
148
149
  if (missing.length) {
package/src/cli/serve.js CHANGED
@@ -3,6 +3,7 @@
3
3
  import fs from 'node:fs'
4
4
  import { randomBytes } from 'node:crypto'
5
5
  import { isLoopback } from '../net.js'
6
+ import http from 'node:http'
6
7
  import https from 'node:https'
7
8
  import { lanAccess, tailscaleAccess } from '../server/tailscale.js'
8
9
  import { loadConfig } from '../config.js'
@@ -94,7 +95,7 @@ export async function serve(flags, out) {
94
95
  const port = flags.port === undefined ? DEFAULT_PORT : Number(flags.port)
95
96
  if (!Number.isInteger(port) || port < 0 || port > 65535) throw new Error('--port must be a port number')
96
97
  if (flags.tailscale && port === 0) throw new Error('--tailscale needs a fixed --port, not 0.')
97
- const tail = flags.tailscale ? await tailscaleAccess({ port, noIdentity: Boolean(flags['no-identity']) }) : null
98
+ const tail = flags.tailscale ? await tailscaleAccess({ port, noIdentity: Boolean(flags['no-identity']), log: out }) : null
98
99
  if (flags.tailscale === 'print') {
99
100
  out(tail.command)
100
101
  out(tail.url)
@@ -150,7 +151,17 @@ export async function serve(flags, out) {
150
151
  log: (line) => out(line),
151
152
  })
152
153
  const tls = flags['tls-cert'] ? { cert: fs.readFileSync(flags['tls-cert']), key: fs.readFileSync(flags['tls-key']) } : null
153
- const server = tls ? https.createServer(tls, app).listen(port, host) : app.listen(port, host)
154
+ // With `--tailscale` and the identity proof, each request is first asked how it came in: the serve
155
+ // entry's secret comes off its path here, before Express and before anything can echo the URL.
156
+ const handler = tail?.gate
157
+ ? (req, res) => {
158
+ tail.gate.admit(req, res).then(
159
+ (ok) => ok && app(req, res),
160
+ () => res.writeHead(500).end(),
161
+ )
162
+ }
163
+ : app
164
+ const server = tls ? https.createServer(tls, handler).listen(port, host) : http.createServer(handler).listen(port, host)
154
165
  watchConfig(config, { log: out, server })
155
166
  await new Promise((resolve, reject) => {
156
167
  server.once('listening', resolve)
@@ -159,8 +170,8 @@ export async function serve(flags, out) {
159
170
  let hostServer = null
160
171
  if (lan && host !== '0.0.0.0') {
161
172
  hostServer = tls
162
- ? https.createServer(tls, app).listen(server.address().port, '127.0.0.1')
163
- : app.listen(server.address().port, '127.0.0.1')
173
+ ? https.createServer(tls, handler).listen(server.address().port, '127.0.0.1')
174
+ : http.createServer(handler).listen(server.address().port, '127.0.0.1')
164
175
  try {
165
176
  await new Promise((resolve, reject) => {
166
177
  hostServer.once('listening', resolve)
@@ -173,7 +184,7 @@ export async function serve(flags, out) {
173
184
  }
174
185
  }
175
186
  try {
176
- await tail?.start()
187
+ await tail?.start(server)
177
188
  } catch (error) {
178
189
  server.close()
179
190
  hostServer?.close()
@@ -188,6 +199,10 @@ export async function serve(flags, out) {
188
199
  `Shapewall is serving ${config.boards.length} board${config.boards.length === 1 ? '' : 's'} at ${accountUrl || `${tls ? 'https' : 'http'}://${shown}:${address.port}/`}`,
189
200
  )
190
201
  if (tail) out(`Private Tailscale board: ${tail.url}${tail.ownerLogin ? '' : ' (pairing required; node has no owner identity)'}`)
202
+ if (tail?.ownerLogin && !tail.gate)
203
+ out(
204
+ `Your other tailnet devices pair like any other device: this machine cannot prove that a request came through tailscale serve (${tail.identityOff}).`,
205
+ )
191
206
  if (lan)
192
207
  out(
193
208
  'LAN pairing is on. Plain HTTP on Wi-Fi exposes the device cookie to sniffing and replay; use --tailscale or --tls-cert/--tls-key. HTTP installs only as a bookmark.',
package/src/cli.js CHANGED
@@ -95,6 +95,14 @@ const COMMANDS = {
95
95
  await login(flags, out)
96
96
  return 0
97
97
  },
98
+ join: async ({ flags, out, fetch }) => {
99
+ const { joinCommand } = await import('./remote/tailnet-cli.js')
100
+ return joinCommand(flags, out, { fetchImpl: fetch })
101
+ },
102
+ admin: async ({ positional, flags, out, fetch }) => {
103
+ const { adminCommand } = await import('./remote/tailnet-cli.js')
104
+ return adminCommand(positional, flags, out, { fetchImpl: fetch })
105
+ },
98
106
  share: async ({ flags, out }) => {
99
107
  const { share } = await import('./remote/share.js')
100
108
  return (await share(flags, out)).code
@@ -13,9 +13,23 @@
13
13
  import fs from 'node:fs'
14
14
  import path from 'node:path'
15
15
  import { MIN_NODE, nodeSupportsSqlite, openDatabase, quietSqliteWarning } from './db.js'
16
- import { mintSetupLink, readSetupLink, setupFile, setupLinkUsable, userCount, writeSetupLink } from './setup-link.js'
16
+ import { mintSetupLink, readSetupLink, removeSetupLink, setupFile, setupLinkUsable, userCount, writeSetupLink } from './setup-link.js'
17
+ import { sha256 } from './crypto.js'
18
+ import { transaction } from './db.js'
19
+ import { createMembership } from './membership.js'
20
+ import { createQuotas } from './quotas.js'
21
+ import { createTailnetAccounts } from './tailnet.js'
22
+ import { accounts, auditAccount, tenant } from './tenancy.js'
23
+ import { cleanTailnetLogin } from '../tailnet-proof.js'
24
+
17
25
  import { MOVE_DAYS_MAX, getState, readMove, recordMove } from './rebind.js'
18
26
 
27
+ // A value as an error message may show it: printable ASCII only.
28
+ const show = (value) =>
29
+ String(value ?? '')
30
+ .replace(/[^ -~]/g, '')
31
+ .slice(0, 80)
32
+
19
33
  async function openData(dataDir) {
20
34
  if (!nodeSupportsSqlite())
21
35
  throw new Error(`shapewall hosted needs Node ${MIN_NODE.join('.')} or newer (for node:sqlite); this is ${process.versions.node}`)
@@ -29,6 +43,7 @@ async function openData(dataDir) {
29
43
  export async function operatorCommand(action, { dataDir, flags, env = process.env, out = console.log }) {
30
44
  const { dir, db } = await openData(dataDir)
31
45
  try {
46
+ if (action === 'claim') return claimCommand({ dir, db, flags, out })
32
47
  if (action === 'setup-link') return setupLinkCommand({ dir, db, flags, env, out })
33
48
  if (action === 'move-origin') return moveOriginCommand({ db, flags, out })
34
49
  } finally {
@@ -36,6 +51,89 @@ export async function operatorCommand(action, { dataDir, flags, env = process.en
36
51
  }
37
52
  }
38
53
 
54
+ // `shapewall hosted claim --data DIR --login LOGIN [--workspace-name NAME]`:
55
+ // the first account of a server with tailnet sign-in, made on the server's
56
+ // own machine without a browser.
57
+ //
58
+ // The proof is the data folder. Whoever can read it has the setup link (the
59
+ // one-time secret that makes the first account), the database and the
60
+ // signing secret: they control the server already. Who the owner is cannot
61
+ // be asked of the tailnet from here: a request from the server's own machine
62
+ // names the machine's owner whoever sends it, so the server never takes an
63
+ // identity from this machine (src/tailnet-proof.js). So the owner's tailnet
64
+ // login is said with --login, and it is the operator's word: the account is
65
+ // linked to that login, and whoever arrives through the tailnet as that
66
+ // login from another device is its owner. (From another tailnet device, the
67
+ // setup link opened in a browser claims by identity instead.)
68
+ function claimCommand({ dir, db, flags, out }) {
69
+ if (userCount(db) > 0)
70
+ throw new Error(
71
+ 'This server is claimed already: it has accounts. The setup link is only for the first one; admins invite everyone else.',
72
+ )
73
+ const link = readSetupLink(dir)
74
+ if (!link || !setupLinkUsable(db, link))
75
+ throw new Error(
76
+ `No usable setup link in ${dir}. Start the server (it writes one when it has no accounts), or run "shapewall hosted setup-link --new".`,
77
+ )
78
+ if (typeof flags.login !== 'string' || !flags.login.trim())
79
+ throw new Error(
80
+ 'claim: say whose server this is with --login <tailnet login> (like ada@example.com: the name the owner signs in to Tailscale with; "tailscale whois $(tailscale ip -4)" on one of their devices shows it).',
81
+ )
82
+ const login = cleanTailnetLogin(flags.login)
83
+ if (!login) throw new Error(`--login: "${show(flags.login)}" is not a tailnet login (user@domain, plain letters and digits).`)
84
+ const iso = () => new Date().toISOString()
85
+ const q = accounts(db)
86
+ const code = new URLSearchParams(new URL(link).hash.slice(1)).get('setup') || ''
87
+ const { createWorkspace } = createMembership({ db, quotas: createQuotas(), iso, auditOf: () => {} })
88
+ const made = transaction(db, () => {
89
+ const used = q.run('UPDATE setup_codes SET used_at = :at WHERE code_hash = :hash AND used_at IS NULL AND expires_at > :at', {
90
+ at: iso(),
91
+ hash: sha256(code),
92
+ })
93
+ if (!used.changes) throw new Error('The setup link was used or expired meanwhile. Nothing was claimed.')
94
+ const user = createTailnetAccounts({ db, iso, now: () => new Date() }).createUser({ login, name: login.split('@')[0] }, {})
95
+ const workspaceName =
96
+ typeof flags['workspace-name'] === 'string' && flags['workspace-name'].trim()
97
+ ? flags['workspace-name']
98
+ : `${user.display_name}'s workspace`
99
+ const workspaceId = createWorkspace(user, workspaceName, null)
100
+ auditAccount(db, {
101
+ userId: user.id,
102
+ action: 'account_created',
103
+ ip: null,
104
+ at: iso(),
105
+ detail: { via: 'setup', signIn: 'tailnet', tailnet: login, by: 'hosted claim on the server' },
106
+ })
107
+ auditAccount(db, {
108
+ userId: user.id,
109
+ action: 'tailnet_linked',
110
+ ip: null,
111
+ at: iso(),
112
+ detail: { tailnet: login, by: 'hosted claim on the server' },
113
+ })
114
+ const workspace = tenant(db, workspaceId).get('SELECT name FROM workspaces WHERE workspace_id = :ws')
115
+ return { user, workspaceName: workspace.name }
116
+ })
117
+ removeSetupLink(dir)
118
+ const origin = new URL(link).origin
119
+ out(`Claimed ${origin}.`)
120
+ out(` Owner account: @${made.user.handle}, linked to the tailnet login ${login}.`)
121
+ out(` Workspace: ${made.workspaceName} (that account owns it).`)
122
+ out(' The setup link is used up.')
123
+ out(`Whoever arrives through the tailnet as ${login}, from another device than this server, is signed in as the owner.`)
124
+ out(
125
+ 'That login was taken from --login on your word; it was not checked against the tailnet. If it is wrong, delete the data folder and start again.',
126
+ )
127
+ out('This machine itself is never signed in by its tailnet identity. Here, use another tailnet device of yours, or')
128
+ out('shapewall hosted admin with this data folder (members, invites, projects, passkeys; shapewall hosted admin help).')
129
+ out('Your Tailscale account is the sign-in: no passkey was made and none is asked for. Anything running as you on a tailnet')
130
+ out('device of yours can act as you there, so lock your devices and keep the tailnet to people and machines you trust.')
131
+ out('Deleting a workspace, changing owners and a few other things ask you to type a name to confirm.')
132
+ out('To also allow passkeys on this server: shapewall hosted admin passkeys on --data <dir>.')
133
+ out('The server must run with --tailscale --tailnet-sign-in for that login to sign in.')
134
+ return 0
135
+ }
136
+
39
137
  function setupLinkCommand({ dir, db, flags, env, out }) {
40
138
  if (userCount(db) > 0)
41
139
  throw new Error('This server has accounts already. The setup link is only for the first one; admins invite everyone else.')