shapewall 0.3.3 → 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.
- package/CHANGELOG.md +501 -0
- package/README.md +87 -4
- package/THIRD_PARTY.md +15 -0
- package/dist/agent/package-lock.json +594 -2
- package/dist/client/assets/BoardChat-WS8mAIW6.js +27 -0
- package/dist/client/assets/{BuzzDialog-Ix9qJvim.js → BuzzDialog-B-Mlr6wF.js} +1 -1
- package/dist/client/assets/ChecksPanel-Dx1ZLBR6.js +1 -0
- package/dist/client/assets/History-sXhmpcvs.js +1 -0
- package/dist/client/assets/LocalSettings-CtGzz9F3.js +1 -0
- package/dist/client/assets/{Management-NVFDtSZB.js → Management-CGHe8Wt9.js} +1 -1
- package/dist/client/assets/QuickConnect-DEk84E4A.js +1 -0
- package/dist/client/assets/{ShareDialog-By0ETg6n.js → ShareDialog-CGGx-AYK.js} +2 -2
- package/dist/client/assets/{app-update-dialog-RGIw24iZ.js → app-update-dialog-_bVp8hnC.js} +1 -1
- package/dist/client/assets/{circle-x-B6KfScOE.js → circle-x-BWoHA1QY.js} +1 -1
- package/dist/client/assets/index-Bm-Xd3YN.js +37 -0
- package/dist/client/assets/index-C7Ostk3K.css +2 -0
- package/dist/client/assets/play-BzQ3tRTv.js +1 -0
- package/dist/client/assets/{refresh-cw-vkgvCCfR.js → refresh-cw-BYBMyDKT.js} +1 -1
- package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
- package/dist/client/assets/ui-CAY6myRA.js +41 -0
- package/dist/client/build-info.json +1 -1
- package/dist/client/index.html +3 -3
- package/dist/client/sw.js +2 -2
- package/dist/console/assets/LocalAccountsApp-CWRguRRL.js +1 -0
- package/dist/console/assets/LocalConsole--I2SaIpr.js +2 -0
- package/dist/console/assets/LocalUsage-BmpCZ36k.js +1 -0
- package/dist/console/assets/app-update-dialog-B8M9plaT.js +1 -0
- package/dist/console/assets/app-version-muKrQqbL.js +1 -0
- package/dist/console/assets/console-CrhTsPbz.css +2 -0
- package/dist/console/assets/console-DxapQ9oS.js +11 -0
- package/dist/console/assets/local-agents-CoqD4dSN.js +6 -0
- package/dist/console/assets/minus-DDQ0ydcM.js +1 -0
- package/dist/console/assets/tailnet-C5Flwl_d.js +2 -0
- package/dist/console/assets/theme-DCd0qh4l.js +1 -0
- package/dist/console/assets/ui-Bry1mnCY.js +41 -0
- package/dist/console/assets/usage-CIPWnHaS.css +1 -0
- package/dist/console/assets/usage-CqVjMb7m.js +5 -0
- package/dist/console/assets/usage-D1G0GgE3.js +56 -0
- package/dist/console/build-info.json +1 -1
- package/dist/console/console.html +8 -2
- package/docs/agent-hardening.md +206 -0
- package/docs/agent-recipes.md +23 -0
- package/docs/agent-workflow.md +5 -0
- package/docs/agents.md +49 -6
- package/docs/architecture.md +10 -4
- package/docs/deployment.md +39 -17
- package/docs/design-system.md +56 -7
- package/docs/hosted.md +1800 -46
- package/docs/images/board-overview.png +0 -0
- package/docs/images/journeys.png +0 -0
- package/docs/images/live-comment.png +0 -0
- package/docs/images/phone-comment.png +0 -0
- package/docs/images/phone-overview.png +0 -0
- package/docs/images/variant.png +0 -0
- package/docs/where-to-run.md +119 -3
- package/package.json +6 -1
- package/src/agent/backends/acp.js +745 -44
- package/src/agent/branch-mode.js +17 -3
- package/src/agent/card-hash.js +65 -0
- package/src/agent/context.js +41 -0
- package/src/agent/exec-paths.js +105 -0
- package/src/agent/routes.js +23 -2
- package/src/agent/run.js +11 -1
- package/src/agent/starter/cli.js +6 -1
- package/src/agent/turn-changes.js +70 -8
- package/src/agent/usage-log.js +251 -0
- package/src/cli/args.js +37 -0
- package/src/cli/commands.js +20 -5
- package/src/cli/help.js +73 -3
- package/src/cli/init.js +2 -1
- package/src/cli/serve.js +20 -5
- package/src/cli.js +8 -0
- package/src/hosted/access-cli.js +99 -1
- package/src/hosted/activity-core.js +232 -0
- package/src/hosted/activity.js +375 -0
- package/src/hosted/admin.js +87 -13
- package/src/hosted/agent-approvals.js +29 -4
- package/src/hosted/agent-link.js +1032 -0
- package/src/hosted/agent-policy.js +10 -3
- package/src/hosted/agent-presence.js +63 -22
- package/src/hosted/agent-routes.js +135 -0
- package/src/hosted/agents.js +1206 -0
- package/src/hosted/app.js +178 -12
- package/src/hosted/auth-routes.js +156 -15
- package/src/hosted/auth.js +123 -20
- package/src/hosted/board-routes.js +129 -5
- package/src/hosted/chat-frames.js +246 -0
- package/src/hosted/chat-job.js +204 -0
- package/src/hosted/chat-permissions.js +203 -0
- package/src/hosted/chat-routes.js +1009 -0
- package/src/hosted/chat-store.js +477 -0
- package/src/hosted/comment-files.js +11 -1
- package/src/hosted/comments.js +1 -1
- package/src/hosted/console-routes.js +122 -34
- package/src/hosted/db.js +295 -0
- package/src/hosted/device-routes.js +88 -10
- package/src/hosted/device.js +54 -4
- package/src/hosted/events.js +3 -3
- package/src/hosted/guards.js +337 -27
- package/src/hosted/http.js +6 -3
- package/src/hosted/membership.js +22 -3
- package/src/hosted/operator-access.js +418 -0
- package/src/hosted/operator.js +106 -2
- package/src/hosted/ops-cli.js +238 -3
- package/src/hosted/pages.js +4 -0
- package/src/hosted/passkey-policy.js +75 -0
- package/src/hosted/permissions.js +52 -6
- package/src/hosted/presence.js +31 -2
- package/src/hosted/projects.js +30 -2
- package/src/hosted/public.js +4 -4
- package/src/hosted/ratelimit.js +14 -0
- package/src/hosted/retention.js +18 -2
- package/src/hosted/server.js +123 -11
- package/src/hosted/shares.js +2 -2
- package/src/hosted/step-up-settings.js +68 -0
- package/src/hosted/step-up.js +135 -0
- package/src/hosted/tailnet-identity.js +24 -0
- package/src/hosted/tailnet-routes.js +342 -0
- package/src/hosted/tailnet.js +220 -0
- package/src/hosted/tailscale.js +75 -9
- package/src/hosted/tenancy.js +66 -1
- package/src/hosted/token-api.js +119 -20
- package/src/hosted/token-events.js +5 -3
- package/src/hosted/tokens.js +29 -6
- package/src/hosted/turn-hub.js +152 -0
- package/src/hosted/usage-core.js +624 -0
- package/src/hosted/usage-layout.js +724 -0
- package/src/hosted/usage-routes.js +375 -0
- package/src/hosted/usage-shape.js +256 -0
- package/src/hosted/usage.js +641 -0
- package/src/remote/agent-connect.js +469 -0
- package/src/remote/agent-fix.js +611 -0
- package/src/remote/agent-job.js +216 -0
- package/src/remote/agent-link.js +906 -0
- package/src/remote/agent-policy-file.js +242 -0
- package/src/remote/credentials.js +35 -1
- package/src/remote/login.js +44 -9
- package/src/remote/share.js +1 -1
- package/src/remote/tailnet-cli.js +547 -0
- package/src/server/app.js +15 -0
- package/src/server/pairing-routes.js +34 -5
- package/src/server/tailscale.js +97 -4
- package/src/tailnet-proof.js +766 -0
- package/dist/client/assets/BoardChat-DxehB761.js +0 -27
- package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +0 -1
- package/dist/client/assets/History-C9N32kCv.js +0 -1
- package/dist/client/assets/LocalSettings-DawtKnvi.js +0 -1
- package/dist/client/assets/QuickConnect-DO1TjS1t.js +0 -1
- package/dist/client/assets/index-CsqLdqGx.css +0 -2
- package/dist/client/assets/index-DnZarJ3U.js +0 -37
- package/dist/client/assets/shield-check-QzwGhz0t.js +0 -1
- package/dist/client/assets/ui-DI-FGWWK.js +0 -41
- package/dist/console/assets/LocalAccountsApp-VOCZg8dg.js +0 -1
- package/dist/console/assets/LocalConsole-CMwAW-mF.js +0 -1
- package/dist/console/assets/app-update-dialog-Cw6UDqNP.js +0 -1
- package/dist/console/assets/console-DgfWmUEx.js +0 -55
- package/dist/console/assets/console-Lis0vvdL.css +0 -2
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
// The console API for the Usage and Activity pages (src/hosted/app.js).
|
|
2
|
+
// Thin on purpose: every answer is one function of src/hosted/usage.js or
|
|
3
|
+
// src/hosted/activity.js called with the request's filter and the reader's
|
|
4
|
+
// scope, so a CLI command or an admin tool can call the same functions.
|
|
5
|
+
//
|
|
6
|
+
// Who reads what:
|
|
7
|
+
// usage owners and admins: the workspace. A member: the turns they
|
|
8
|
+
// asked for and every turn of an agent they connected. Viewers:
|
|
9
|
+
// nothing (403). No session: 401. Another workspace: 404.
|
|
10
|
+
// activity owners and admins: the workspace's log. Anyone else in the
|
|
11
|
+
// workspace: their own entries.
|
|
12
|
+
// access review, another person's page, an agent's page: admins and owners.
|
|
13
|
+
//
|
|
14
|
+
// Exports obey the same filter and scope as the page, are capped, and leave
|
|
15
|
+
// an audit entry. There are no scheduled or emailed exports.
|
|
16
|
+
|
|
17
|
+
import { exportFileName } from './admin.js'
|
|
18
|
+
import { ACTIVITY_LIMITS, activityCsv, activityViewer, cleanActivityFilter, createActivity, reviewCsv } from './activity.js'
|
|
19
|
+
import { deviceLabel } from './auth.js'
|
|
20
|
+
import { transaction } from './db.js'
|
|
21
|
+
import { HttpError } from './http.js'
|
|
22
|
+
import { NEEDS, roleAtLeast } from './permissions.js'
|
|
23
|
+
import {
|
|
24
|
+
USAGE_LIMITS,
|
|
25
|
+
cleanBucket,
|
|
26
|
+
cleanDimension,
|
|
27
|
+
cleanMetric,
|
|
28
|
+
cleanTopLimit,
|
|
29
|
+
cleanUsageFilter,
|
|
30
|
+
createLayouts,
|
|
31
|
+
createUsage,
|
|
32
|
+
createViews,
|
|
33
|
+
usageCsvHeader,
|
|
34
|
+
usageCsvLine,
|
|
35
|
+
usageRetentionDays,
|
|
36
|
+
usageScope,
|
|
37
|
+
} from './usage.js'
|
|
38
|
+
|
|
39
|
+
export function mountUsageRoutes(
|
|
40
|
+
consoleApi,
|
|
41
|
+
{ db, auth, now, iso, handle, limit, inWorkspace, auditOf, agentsFor, ownersSeeSignIns = false },
|
|
42
|
+
) {
|
|
43
|
+
const usageOf = (req) => createUsage(req.t, { now })
|
|
44
|
+
const scopeOf = (req) => usageScope(req.role, req.user.id)
|
|
45
|
+
const filterOf = (req) => cleanUsageFilter(req.query, { now })
|
|
46
|
+
const workspaceName = (req) => req.t.get('SELECT name FROM workspaces WHERE workspace_id = :ws').name
|
|
47
|
+
// Waits for the socket to drain between pages of an export.
|
|
48
|
+
const writer = (res) => (text) => {
|
|
49
|
+
if (res.destroyed) throw new Error('the download was cancelled')
|
|
50
|
+
if (res.write(text)) return null
|
|
51
|
+
return new Promise((resolve) => {
|
|
52
|
+
const done = () => {
|
|
53
|
+
res.off('drain', done)
|
|
54
|
+
res.off('close', done)
|
|
55
|
+
resolve()
|
|
56
|
+
}
|
|
57
|
+
res.on('drain', done)
|
|
58
|
+
res.on('close', done)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ---- Usage ----
|
|
63
|
+
|
|
64
|
+
const usage = (path, answer) =>
|
|
65
|
+
consoleApi.get(
|
|
66
|
+
`/workspaces/:ws/usage${path}`,
|
|
67
|
+
inWorkspace(NEEDS.usage),
|
|
68
|
+
handle((req, res) => res.json(answer(req, usageOf(req), scopeOf(req)))),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
usage('/options', (req, store, scope) => ({
|
|
72
|
+
options: store.options(scope),
|
|
73
|
+
scope: scope.all ? 'workspace' : 'own',
|
|
74
|
+
retentionDays: usageRetentionDays(req.t),
|
|
75
|
+
limits: { days: USAGE_LIMITS.days, exportRows: USAGE_LIMITS.exportRows },
|
|
76
|
+
}))
|
|
77
|
+
usage('/summary', (req, store, scope) => store.summary(filterOf(req), scope))
|
|
78
|
+
usage('/series', (req, store, scope) =>
|
|
79
|
+
store.series(filterOf(req), scope, {
|
|
80
|
+
bucket: cleanBucket(req.query.bucket),
|
|
81
|
+
groupBy: cleanDimension(req.query.groupBy, 'none'),
|
|
82
|
+
metric: cleanMetric(req.query.metric),
|
|
83
|
+
}),
|
|
84
|
+
)
|
|
85
|
+
// ?previous=1: the same split over the period to compare with.
|
|
86
|
+
usage('/breakdown', (req, store, scope) => {
|
|
87
|
+
const groupBy = cleanDimension(req.query.groupBy)
|
|
88
|
+
if (groupBy === 'none') throw new HttpError(400, 'invalid_option', 'groupBy: a dimension.')
|
|
89
|
+
const previous = req.query.previous === '1'
|
|
90
|
+
return { groupBy, previous, groups: store.breakdown(filterOf(req), scope, groupBy, { previous }) }
|
|
91
|
+
})
|
|
92
|
+
usage('/latency', (req, store, scope) => {
|
|
93
|
+
const bucket = cleanBucket(req.query.bucket)
|
|
94
|
+
return { bucket, buckets: store.latency(filterOf(req), scope, bucket) }
|
|
95
|
+
})
|
|
96
|
+
// ?limit=: how many entries each list holds (5 unless asked, at most 10).
|
|
97
|
+
usage('/top', (req, store, scope) => store.top(filterOf(req), scope, { limit: cleanTopLimit(req.query.limit) }))
|
|
98
|
+
usage('/budgets', (req, store, scope) => ({ projects: store.budgets(scope) }))
|
|
99
|
+
usage('/rows', (req, store, scope) =>
|
|
100
|
+
store.rows(filterOf(req), scope, {
|
|
101
|
+
sort: req.query.sort || 'started',
|
|
102
|
+
dir: req.query.dir || 'desc',
|
|
103
|
+
cursor: req.query.cursor,
|
|
104
|
+
limit: req.query.limit,
|
|
105
|
+
}),
|
|
106
|
+
)
|
|
107
|
+
consoleApi.get(
|
|
108
|
+
'/workspaces/:ws/usage/rows/:id',
|
|
109
|
+
inWorkspace(NEEDS.usage),
|
|
110
|
+
handle((req, res) => {
|
|
111
|
+
// A row the reader may not see answers like one that does not exist.
|
|
112
|
+
const row = usageOf(req).row(String(req.params.id), scopeOf(req))
|
|
113
|
+
if (!row) throw new HttpError(404, 'unknown_row', 'No such usage record.')
|
|
114
|
+
res.json({ row })
|
|
115
|
+
}),
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
// The filtered set as a file: ?format=csv (the default) or json. Streamed
|
|
119
|
+
// a page at a time, at most USAGE_LIMITS.exportRows rows, newest first.
|
|
120
|
+
consoleApi.get(
|
|
121
|
+
'/workspaces/:ws/usage/export',
|
|
122
|
+
inWorkspace(NEEDS.usage),
|
|
123
|
+
limit('usageExport', (req) => req.user.id),
|
|
124
|
+
handle(async (req, res) => {
|
|
125
|
+
const format = req.query.format === 'json' ? 'json' : 'csv'
|
|
126
|
+
if (req.query.format && !['csv', 'json'].includes(req.query.format))
|
|
127
|
+
throw new HttpError(400, 'invalid_format', 'format: csv or json.')
|
|
128
|
+
const filter = filterOf(req)
|
|
129
|
+
const scope = scopeOf(req)
|
|
130
|
+
const store = usageOf(req)
|
|
131
|
+
const total = store.totals(filter, scope).calls
|
|
132
|
+
auditOf(req, 'usage_exported', null, {
|
|
133
|
+
format,
|
|
134
|
+
rows: Math.min(total, USAGE_LIMITS.exportRows),
|
|
135
|
+
scope: scope.all ? 'workspace' : 'own',
|
|
136
|
+
from: new Date(filter.fromMs).toISOString(),
|
|
137
|
+
to: new Date(filter.toMs).toISOString(),
|
|
138
|
+
})
|
|
139
|
+
res.setHeader('Content-Type', format === 'json' ? 'application/json; charset=utf-8' : 'text/csv; charset=utf-8')
|
|
140
|
+
res.setHeader(
|
|
141
|
+
'Content-Disposition',
|
|
142
|
+
`attachment; filename="${exportFileName(workspaceName(req))}-usage-${iso().slice(0, 10)}.${format}"`,
|
|
143
|
+
)
|
|
144
|
+
res.setHeader('X-Usage-Rows', String(Math.min(total, USAGE_LIMITS.exportRows)))
|
|
145
|
+
if (total > USAGE_LIMITS.exportRows) res.setHeader('X-Usage-Truncated', String(total))
|
|
146
|
+
const write = writer(res)
|
|
147
|
+
try {
|
|
148
|
+
if (format === 'csv') await write(usageCsvHeader())
|
|
149
|
+
else await write('[')
|
|
150
|
+
let sent = 0
|
|
151
|
+
for (const page of store.pages(filter, scope)) {
|
|
152
|
+
let text = ''
|
|
153
|
+
for (const row of page) text += format === 'csv' ? usageCsvLine(row) : `${sent++ ? ',' : ''}\n ${JSON.stringify(row)}`
|
|
154
|
+
await write(text)
|
|
155
|
+
}
|
|
156
|
+
if (format === 'json') await write(sent ? '\n]\n' : ']\n')
|
|
157
|
+
res.end()
|
|
158
|
+
} catch (error) {
|
|
159
|
+
res.destroy(error)
|
|
160
|
+
}
|
|
161
|
+
}),
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
// ---- Saved views (a person's own, per page) ----
|
|
165
|
+
|
|
166
|
+
// Views of the Activity page are for everyone who has that page (viewers
|
|
167
|
+
// too); views of the Usage page for those who have Usage.
|
|
168
|
+
const viewsFor = (req, page) => {
|
|
169
|
+
if (page !== 'activity' && !roleAtLeast(req.role, NEEDS.usage))
|
|
170
|
+
throw new HttpError(403, 'forbidden', 'Usage is for members, admins and owners.')
|
|
171
|
+
}
|
|
172
|
+
consoleApi.get(
|
|
173
|
+
'/workspaces/:ws/views',
|
|
174
|
+
inWorkspace(NEEDS.activity),
|
|
175
|
+
handle((req, res) => {
|
|
176
|
+
const page = req.query.page === 'activity' ? 'activity' : 'usage'
|
|
177
|
+
viewsFor(req, page)
|
|
178
|
+
res.json({ views: createViews(req.t, { now }).list(req.user.id, page) })
|
|
179
|
+
}),
|
|
180
|
+
)
|
|
181
|
+
consoleApi.post(
|
|
182
|
+
'/workspaces/:ws/views',
|
|
183
|
+
inWorkspace(NEEDS.activity),
|
|
184
|
+
handle((req, res) => {
|
|
185
|
+
viewsFor(req, req.body?.page)
|
|
186
|
+
const view = transaction(db, () => createViews(req.t, { now }).save(req.user.id, req.body || {}))
|
|
187
|
+
res.status(201).json({ view })
|
|
188
|
+
}),
|
|
189
|
+
)
|
|
190
|
+
consoleApi.delete(
|
|
191
|
+
'/workspaces/:ws/views/:id',
|
|
192
|
+
inWorkspace(NEEDS.activity),
|
|
193
|
+
handle((req, res) => {
|
|
194
|
+
createViews(req.t, { now }).remove(req.user.id, req.params.id)
|
|
195
|
+
res.json({ ok: true })
|
|
196
|
+
}),
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
// The layout a view carries, for whoever was handed the view's link:
|
|
200
|
+
// anyone in this workspace who has the Usage page. A view that carries
|
|
201
|
+
// none, a view of another workspace and an unknown id all answer 404.
|
|
202
|
+
consoleApi.get(
|
|
203
|
+
'/workspaces/:ws/views/:id/layout',
|
|
204
|
+
inWorkspace(NEEDS.usage),
|
|
205
|
+
handle((req, res) => {
|
|
206
|
+
const view = createViews(req.t, { now }).layoutOf(req.params.id, req.user.id)
|
|
207
|
+
if (!view) throw new HttpError(404, 'unknown_view', 'No such saved view, or it carries no layout.')
|
|
208
|
+
res.json({ view })
|
|
209
|
+
}),
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
// ---- The layout of the Usage dashboard ----
|
|
213
|
+
|
|
214
|
+
// A person reads and writes their own layout and nobody else's: there is
|
|
215
|
+
// no user id in these addresses, only the session's. `default`: what the
|
|
216
|
+
// workspace's owners set for everyone to start from (null: the built-in
|
|
217
|
+
// layout). Saving checks the layout (src/hosted/usage-layout.js): unknown
|
|
218
|
+
// widgets and sizes out of range are dropped, never stored.
|
|
219
|
+
const layoutsOf = (req) => createLayouts(req.t, { now })
|
|
220
|
+
const layoutAnswer = (req, store = layoutsOf(req)) => ({
|
|
221
|
+
layout: store.mine(req.user.id),
|
|
222
|
+
default: store.shared(),
|
|
223
|
+
canSetDefault: roleAtLeast(req.role, NEEDS.usageDefaultLayout),
|
|
224
|
+
})
|
|
225
|
+
consoleApi.get(
|
|
226
|
+
'/workspaces/:ws/usage/layout',
|
|
227
|
+
inWorkspace(NEEDS.usage),
|
|
228
|
+
handle((req, res) => res.json(layoutAnswer(req))),
|
|
229
|
+
)
|
|
230
|
+
consoleApi.put(
|
|
231
|
+
'/workspaces/:ws/usage/layout',
|
|
232
|
+
inWorkspace(NEEDS.usage),
|
|
233
|
+
handle((req, res) => {
|
|
234
|
+
const layout = transaction(db, () => layoutsOf(req).save(req.user.id, req.body?.layout))
|
|
235
|
+
res.json({ layout })
|
|
236
|
+
}),
|
|
237
|
+
)
|
|
238
|
+
// "Reset to default": forgets the person's own layout.
|
|
239
|
+
consoleApi.delete(
|
|
240
|
+
'/workspaces/:ws/usage/layout',
|
|
241
|
+
inWorkspace(NEEDS.usage),
|
|
242
|
+
handle((req, res) => {
|
|
243
|
+
layoutsOf(req).reset(req.user.id)
|
|
244
|
+
res.json(layoutAnswer(req))
|
|
245
|
+
}),
|
|
246
|
+
)
|
|
247
|
+
// "Set as default for everyone": owners. Audited.
|
|
248
|
+
consoleApi.put(
|
|
249
|
+
'/workspaces/:ws/usage/layout/default',
|
|
250
|
+
inWorkspace(NEEDS.usageDefaultLayout),
|
|
251
|
+
handle((req, res) => {
|
|
252
|
+
const layout = transaction(db, () =>
|
|
253
|
+
layoutsOf(req).setDefault(req.user.id, req.body?.layout, {
|
|
254
|
+
audit: (action, target, detail) => auditOf(req, action, target, detail),
|
|
255
|
+
}),
|
|
256
|
+
)
|
|
257
|
+
res.json({ default: layout })
|
|
258
|
+
}),
|
|
259
|
+
)
|
|
260
|
+
consoleApi.delete(
|
|
261
|
+
'/workspaces/:ws/usage/layout/default',
|
|
262
|
+
inWorkspace(NEEDS.usageDefaultLayout),
|
|
263
|
+
handle((req, res) => {
|
|
264
|
+
transaction(db, () => layoutsOf(req).clearDefault({ audit: (action, target, detail) => auditOf(req, action, target, detail) }))
|
|
265
|
+
res.json({ default: null })
|
|
266
|
+
}),
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
// ---- Activity ----
|
|
270
|
+
|
|
271
|
+
const activityOf = (req) => createActivity(req.t, { db, now })
|
|
272
|
+
const viewerOf = (req) => activityViewer(req.role, req.user.id, { ownersSeeSignIns })
|
|
273
|
+
// A search, or a filter by agent or project, reads the log entry by entry: a per-person limit, as on the exports.
|
|
274
|
+
const heavy = limit('activitySearch', (req) => req.user.id)
|
|
275
|
+
const limitHeavy = (req, res, next) => (req.query.q || req.query.agent || req.query.project ? heavy(req, res, next) : next())
|
|
276
|
+
|
|
277
|
+
consoleApi.get(
|
|
278
|
+
'/workspaces/:ws/activity',
|
|
279
|
+
inWorkspace(NEEDS.activity),
|
|
280
|
+
limitHeavy,
|
|
281
|
+
handle((req, res) => {
|
|
282
|
+
const viewer = viewerOf(req)
|
|
283
|
+
res.json({
|
|
284
|
+
...activityOf(req).list(cleanActivityFilter(req.query), viewer, {
|
|
285
|
+
before: req.query.before,
|
|
286
|
+
limit: req.query.limit || ACTIVITY_LIMITS.page,
|
|
287
|
+
}),
|
|
288
|
+
scope: viewer.all ? 'workspace' : 'own',
|
|
289
|
+
})
|
|
290
|
+
}),
|
|
291
|
+
)
|
|
292
|
+
consoleApi.get(
|
|
293
|
+
'/workspaces/:ws/activity/filters',
|
|
294
|
+
inWorkspace(NEEDS.activity),
|
|
295
|
+
handle((req, res) => res.json(activityOf(req).filters(viewerOf(req)))),
|
|
296
|
+
)
|
|
297
|
+
// The same filters as a CSV file (at most ACTIVITY_LIMITS.exportRows rows).
|
|
298
|
+
consoleApi.get(
|
|
299
|
+
'/workspaces/:ws/activity/export',
|
|
300
|
+
inWorkspace(NEEDS.activity),
|
|
301
|
+
limit('usageExport', (req) => req.user.id),
|
|
302
|
+
handle((req, res) => {
|
|
303
|
+
const viewer = viewerOf(req)
|
|
304
|
+
const entries = activityOf(req).all(cleanActivityFilter(req.query), viewer, { max: ACTIVITY_LIMITS.exportRows })
|
|
305
|
+
auditOf(req, 'audit_exported', null, { rows: entries.length, scope: viewer.all ? 'workspace' : 'own', view: 'activity' })
|
|
306
|
+
res.setHeader('Content-Type', 'text/csv; charset=utf-8')
|
|
307
|
+
res.setHeader(
|
|
308
|
+
'Content-Disposition',
|
|
309
|
+
`attachment; filename="${exportFileName(workspaceName(req))}-activity-${iso().slice(0, 10)}.csv"`,
|
|
310
|
+
)
|
|
311
|
+
res.send(activityCsv(entries))
|
|
312
|
+
}),
|
|
313
|
+
)
|
|
314
|
+
// One person: admins and owners, or the person themselves. Anyone else
|
|
315
|
+
// gets the same 404 as for someone who is not in the workspace.
|
|
316
|
+
consoleApi.get(
|
|
317
|
+
'/workspaces/:ws/people/:uid',
|
|
318
|
+
inWorkspace(NEEDS.activity),
|
|
319
|
+
handle((req, res) => {
|
|
320
|
+
const person = activityOf(req).person(String(req.params.uid), viewerOf(req), {
|
|
321
|
+
// A label ("Chrome on macOS"), not the browser's whole user agent string.
|
|
322
|
+
sessionsOf: (userId) =>
|
|
323
|
+
auth.listSessions(userId, null).map((session) => ({ ...session, device: session.device ? deviceLabel(session.device) : null })),
|
|
324
|
+
})
|
|
325
|
+
if (!person) throw new HttpError(404, 'unknown_member', 'No such member.')
|
|
326
|
+
res.json({ person })
|
|
327
|
+
}),
|
|
328
|
+
)
|
|
329
|
+
// One agent, for its page (activity and usage are fetched with ?agent=).
|
|
330
|
+
consoleApi.get(
|
|
331
|
+
'/workspaces/:ws/agents/:aid/profile',
|
|
332
|
+
inWorkspace(NEEDS.accessReview),
|
|
333
|
+
handle((req, res) => {
|
|
334
|
+
const agent = transaction(db, () =>
|
|
335
|
+
agentsFor(req.t).consoleList(null, { audit: (action, target, detail) => auditOf(req, action, target, detail) }),
|
|
336
|
+
).find((item) => item.id === String(req.params.aid))
|
|
337
|
+
if (!agent) throw new HttpError(404, 'unknown_agent', 'No such agent.')
|
|
338
|
+
res.json({ agent })
|
|
339
|
+
}),
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
// ---- Access review ----
|
|
343
|
+
|
|
344
|
+
const reviewOf = (req) =>
|
|
345
|
+
activityOf(req).review({
|
|
346
|
+
agents: transaction(db, () =>
|
|
347
|
+
agentsFor(req.t).consoleList(null, { audit: (action, target, detail) => auditOf(req, action, target, detail) }),
|
|
348
|
+
),
|
|
349
|
+
})
|
|
350
|
+
consoleApi.get(
|
|
351
|
+
'/workspaces/:ws/access-review',
|
|
352
|
+
inWorkspace(NEEDS.accessReview),
|
|
353
|
+
handle((req, res) => res.json(reviewOf(req))),
|
|
354
|
+
)
|
|
355
|
+
consoleApi.get(
|
|
356
|
+
'/workspaces/:ws/access-review/export',
|
|
357
|
+
inWorkspace(NEEDS.accessReview),
|
|
358
|
+
limit('usageExport', (req) => req.user.id),
|
|
359
|
+
handle((req, res) => {
|
|
360
|
+
const data = reviewOf(req)
|
|
361
|
+
auditOf(req, 'access_exported', null, {
|
|
362
|
+
people: data.people.length,
|
|
363
|
+
tokens: data.tokens.length,
|
|
364
|
+
agents: data.agents.length,
|
|
365
|
+
shares: data.shares.length,
|
|
366
|
+
})
|
|
367
|
+
res.setHeader('Content-Type', 'text/csv; charset=utf-8')
|
|
368
|
+
res.setHeader(
|
|
369
|
+
'Content-Disposition',
|
|
370
|
+
`attachment; filename="${exportFileName(workspaceName(req))}-access-review-${iso().slice(0, 10)}.csv"`,
|
|
371
|
+
)
|
|
372
|
+
res.send(reviewCsv(data))
|
|
373
|
+
}),
|
|
374
|
+
)
|
|
375
|
+
}
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// The shape of usage and activity data, as plain values with no imports, so
|
|
2
|
+
// the server (usage.js, activity.js), the console and the local board's
|
|
3
|
+
// Usage view all name things the same way.
|
|
4
|
+
//
|
|
5
|
+
// A usage record is one agent turn: a chat answer, a comment an agent
|
|
6
|
+
// answered (review), a fix, or the push of a fix. It holds numbers and ids
|
|
7
|
+
// only. Never prompt text, answers, file contents or diffs.
|
|
8
|
+
|
|
9
|
+
// What a row can be grouped and filtered by.
|
|
10
|
+
export const USAGE_DIMENSIONS = ['agent', 'project', 'person', 'kind', 'model', 'outcome']
|
|
11
|
+
// What a chart or a column can measure. "tokens" is input plus output.
|
|
12
|
+
export const USAGE_METRICS = ['calls', 'tokens', 'input', 'output', 'cached', 'duration', 'cost']
|
|
13
|
+
export const USAGE_BUCKETS = ['day', 'week', 'month']
|
|
14
|
+
export const USAGE_KINDS = ['chat', 'review', 'fix', 'push']
|
|
15
|
+
export const USAGE_SORTS = ['started', 'duration', 'tokens', 'input', 'output', 'cached', 'cost', 'steps']
|
|
16
|
+
|
|
17
|
+
// How a turn ended, in the four groups the dashboard draws, plus "other"
|
|
18
|
+
// (the agent let go, or the comment went away) and "running".
|
|
19
|
+
export const OUTCOME_GROUPS = {
|
|
20
|
+
completed: ['answered', 'fix_ready', 'done', 'resolved', 'branch-ready'],
|
|
21
|
+
needs_person: ['needs-human'],
|
|
22
|
+
failed: ['failed', 'error', 'interrupted'],
|
|
23
|
+
stopped: ['stopped'],
|
|
24
|
+
other: ['released', 'removed', 'stale', 'revoked', 'expired', 'inactive'],
|
|
25
|
+
}
|
|
26
|
+
export const OUTCOME_GROUP_IDS = ['completed', 'needs_person', 'failed', 'stopped', 'other', 'running']
|
|
27
|
+
export const OUTCOME_GROUP_LABEL = {
|
|
28
|
+
completed: 'Completed',
|
|
29
|
+
needs_person: 'Needs a person',
|
|
30
|
+
failed: 'Failed',
|
|
31
|
+
stopped: 'Stopped',
|
|
32
|
+
other: 'Let go',
|
|
33
|
+
running: 'Running',
|
|
34
|
+
}
|
|
35
|
+
export function outcomeGroup(outcome) {
|
|
36
|
+
if (!outcome) return 'running'
|
|
37
|
+
for (const [group, list] of Object.entries(OUTCOME_GROUPS)) if (list.includes(outcome)) return group
|
|
38
|
+
return 'other'
|
|
39
|
+
}
|
|
40
|
+
export const OUTCOME_LABEL = {
|
|
41
|
+
answered: 'Answered',
|
|
42
|
+
fix_ready: 'Fix ready',
|
|
43
|
+
done: 'Done',
|
|
44
|
+
resolved: 'Resolved',
|
|
45
|
+
'branch-ready': 'Branch ready',
|
|
46
|
+
'needs-human': 'Needs a person',
|
|
47
|
+
failed: 'Failed',
|
|
48
|
+
error: 'Failed',
|
|
49
|
+
interrupted: 'Interrupted',
|
|
50
|
+
stopped: 'Stopped',
|
|
51
|
+
released: 'Released',
|
|
52
|
+
removed: 'Comment removed',
|
|
53
|
+
stale: 'Taken again',
|
|
54
|
+
revoked: 'Agent revoked',
|
|
55
|
+
expired: 'Agent expired',
|
|
56
|
+
inactive: 'Agent inactive',
|
|
57
|
+
}
|
|
58
|
+
export const KIND_LABEL = { chat: 'Chat', review: 'Comment answer', fix: 'Fix', push: 'Push' }
|
|
59
|
+
export const DIMENSION_LABEL = { agent: 'Agent', project: 'Project', person: 'Person', kind: 'Kind', model: 'Model', outcome: 'Outcome' }
|
|
60
|
+
export const METRIC_LABEL = {
|
|
61
|
+
calls: 'Turns',
|
|
62
|
+
tokens: 'Tokens',
|
|
63
|
+
input: 'Input tokens',
|
|
64
|
+
output: 'Output tokens',
|
|
65
|
+
cached: 'Cached tokens',
|
|
66
|
+
duration: 'Time',
|
|
67
|
+
cost: 'Reported cost',
|
|
68
|
+
}
|
|
69
|
+
// A metric an agent may not report. Turns and time are the server's own.
|
|
70
|
+
export const METRIC_REPORTED = { tokens: true, input: true, output: true, cached: true, cost: true }
|
|
71
|
+
|
|
72
|
+
// Tool call types kept per turn: the name the agent gave the step, when it
|
|
73
|
+
// is a plain word; anything else counts as "other". Never a label or detail.
|
|
74
|
+
const TOOL_NAME = /^[a-z][a-z0-9_.-]{0,31}$/
|
|
75
|
+
export const TOOL_TYPES_MAX = 24
|
|
76
|
+
export function toolCounts(cards) {
|
|
77
|
+
const out = {}
|
|
78
|
+
for (const card of Array.isArray(cards) ? cards : []) {
|
|
79
|
+
if (card?.kind !== 'tool') continue
|
|
80
|
+
const raw = String(card.name || '').toLowerCase()
|
|
81
|
+
const name = TOOL_NAME.test(raw) ? raw : 'other'
|
|
82
|
+
const key = Object.hasOwn(out, name) || Object.keys(out).length < TOOL_TYPES_MAX ? name : 'other'
|
|
83
|
+
out[key] = (out[key] || 0) + 1
|
|
84
|
+
}
|
|
85
|
+
return out
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const count = (value) => (Number.isFinite(Number(value)) && Number(value) >= 0 && value !== null ? Math.round(Number(value)) : null)
|
|
89
|
+
|
|
90
|
+
// What a usage record keeps of a finished answer: how it ended and the
|
|
91
|
+
// numbers the agent reported, each null when it reported none. Shared by the
|
|
92
|
+
// hosted server (chat-frames.js usageOf) and the local board (usage-log.js).
|
|
93
|
+
export function usageRecordOf(reply) {
|
|
94
|
+
const find = (category) => reply.options?.find((item) => item.category === category)?.value || null
|
|
95
|
+
const cards = Array.isArray(reply.cards) ? reply.cards : []
|
|
96
|
+
const tools = toolCounts(cards)
|
|
97
|
+
return {
|
|
98
|
+
// A fix that left a branch is "fix_ready".
|
|
99
|
+
outcome: reply.status === 'done' ? (reply.summary ? 'fix_ready' : 'answered') : reply.status === 'stopped' ? 'stopped' : 'failed',
|
|
100
|
+
model: find('model') || reply.model || null,
|
|
101
|
+
effort: find('thought_level'),
|
|
102
|
+
inputTokens: count(reply.usage?.input ?? null),
|
|
103
|
+
outputTokens: count(reply.usage?.output ?? null),
|
|
104
|
+
cachedTokens: count(reply.usage?.cached ?? null),
|
|
105
|
+
// Only a cost the agent or its endpoint reported. Never worked out here.
|
|
106
|
+
costMicros: count(reply.usage?.costMicros ?? null),
|
|
107
|
+
contextUsed: count(reply.usage?.contextUsed ?? null),
|
|
108
|
+
contextSize: count(reply.usage?.contextSize ?? null),
|
|
109
|
+
steps: cards.filter((card) => card.kind === 'tool').length,
|
|
110
|
+
tools,
|
|
111
|
+
// How long the agent says it worked (the server measures its own time too).
|
|
112
|
+
agentMs: count(reply.durationMs ?? null),
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// What one usage record counts toward the storage quota.
|
|
117
|
+
export const USAGE_ROW_BYTES = 400
|
|
118
|
+
|
|
119
|
+
// SQL: a usage record that ends without its agent's own report still says how
|
|
120
|
+
// long it ran, by the server's clock (`:at` is the end).
|
|
121
|
+
export const RUN_DURATION = 'duration_ms = MAX(0, CAST(ROUND((julianday(:at) - julianday(started_at)) * 86400000) AS INTEGER))'
|
|
122
|
+
|
|
123
|
+
// ---- Activity (the audit log, read as what people did) ----
|
|
124
|
+
|
|
125
|
+
// Every audit action belongs to one group, for the "what" filter.
|
|
126
|
+
export const ACTION_GROUPS = {
|
|
127
|
+
signin: [
|
|
128
|
+
'signed_in',
|
|
129
|
+
'signed_out',
|
|
130
|
+
'sign_in_failed',
|
|
131
|
+
'recovery_failed',
|
|
132
|
+
'account_created',
|
|
133
|
+
'passkey_added',
|
|
134
|
+
'passkey_removed',
|
|
135
|
+
'recovery_codes_created',
|
|
136
|
+
'session_revoked',
|
|
137
|
+
'sessions_revoked',
|
|
138
|
+
'tailnet_linked',
|
|
139
|
+
'tailnet_unlinked',
|
|
140
|
+
],
|
|
141
|
+
members: [
|
|
142
|
+
'invite_created',
|
|
143
|
+
'invite_revoked',
|
|
144
|
+
'invite_accepted',
|
|
145
|
+
'member_role_changed',
|
|
146
|
+
'member_removed',
|
|
147
|
+
'member_left',
|
|
148
|
+
'member_grants_revoked',
|
|
149
|
+
'tailnet_visitor_dismissed',
|
|
150
|
+
],
|
|
151
|
+
settings: [
|
|
152
|
+
'workspace_created',
|
|
153
|
+
'workspace_renamed',
|
|
154
|
+
'share_links_enabled',
|
|
155
|
+
'share_links_disabled',
|
|
156
|
+
'agent_policy_changed',
|
|
157
|
+
'usage_retention_changed',
|
|
158
|
+
'usage_layout_default_set',
|
|
159
|
+
'usage_layout_default_removed',
|
|
160
|
+
'project_made_public',
|
|
161
|
+
'project_made_private',
|
|
162
|
+
'step_up_changed',
|
|
163
|
+
],
|
|
164
|
+
projects: ['project_created', 'project_deleted', 'board_removed', 'push_committed'],
|
|
165
|
+
sharing: ['share_created', 'share_revoked', 'share_opened', 'share_passcode_failed', 'guest_commented', 'guest_replied', 'guest_ended'],
|
|
166
|
+
tokens: ['token_created', 'token_renamed', 'token_revoked'],
|
|
167
|
+
agents: [
|
|
168
|
+
'agent_created',
|
|
169
|
+
'agent_connected',
|
|
170
|
+
'agent_first_seen',
|
|
171
|
+
'agent_renamed',
|
|
172
|
+
'agent_identity_changed',
|
|
173
|
+
'agent_default_changed',
|
|
174
|
+
'agent_audience_changed',
|
|
175
|
+
'agent_approval_rule_changed',
|
|
176
|
+
'agent_edit_mode_changed',
|
|
177
|
+
'agent_revoked',
|
|
178
|
+
'agent_queue_released',
|
|
179
|
+
'agent_link_connected',
|
|
180
|
+
'agent_link_disconnected',
|
|
181
|
+
],
|
|
182
|
+
approvals: [
|
|
183
|
+
'agent_permission_asked',
|
|
184
|
+
'agent_permission_approved',
|
|
185
|
+
'agent_permission_denied',
|
|
186
|
+
'agent_permission_auto',
|
|
187
|
+
'agent_permission_expired',
|
|
188
|
+
'agent_permission_not_taken',
|
|
189
|
+
'agent_command_approved',
|
|
190
|
+
'agent_command_denied',
|
|
191
|
+
'comment_vouched',
|
|
192
|
+
],
|
|
193
|
+
work: [
|
|
194
|
+
'agent_turn_started',
|
|
195
|
+
'agent_turn_stopped',
|
|
196
|
+
'agent_fix_branch_ready',
|
|
197
|
+
'agent_branch_pushed',
|
|
198
|
+
'agent_pull_request_opened',
|
|
199
|
+
'chat_thread_created',
|
|
200
|
+
'chat_thread_deleted',
|
|
201
|
+
'chat_thread_agent_changed',
|
|
202
|
+
],
|
|
203
|
+
comments: ['comment_resolved', 'comment_deleted'],
|
|
204
|
+
exports: ['workspace_exported', 'audit_exported', 'usage_exported', 'access_exported'],
|
|
205
|
+
}
|
|
206
|
+
export const ACTION_GROUP_IDS = Object.keys(ACTION_GROUPS)
|
|
207
|
+
export const ACTION_GROUP_LABEL = {
|
|
208
|
+
signin: 'Sign-in and passkeys',
|
|
209
|
+
members: 'Members and invites',
|
|
210
|
+
settings: 'Settings',
|
|
211
|
+
projects: 'Projects and pushes',
|
|
212
|
+
sharing: 'Share links and guests',
|
|
213
|
+
tokens: 'API tokens',
|
|
214
|
+
agents: 'Agents',
|
|
215
|
+
approvals: 'Approvals',
|
|
216
|
+
work: 'Agent work',
|
|
217
|
+
comments: 'Comments',
|
|
218
|
+
exports: 'Exports',
|
|
219
|
+
}
|
|
220
|
+
export function actionGroup(action) {
|
|
221
|
+
for (const [group, list] of Object.entries(ACTION_GROUPS)) if (list.includes(action)) return group
|
|
222
|
+
return 'other'
|
|
223
|
+
}
|
|
224
|
+
// Account events (they belong to a person, not to a workspace) that a
|
|
225
|
+
// workspace's admins see for their members: sign-ins and what protects them.
|
|
226
|
+
export const ACCOUNT_ACTIONS = [
|
|
227
|
+
'signed_in',
|
|
228
|
+
'signed_out',
|
|
229
|
+
'account_created',
|
|
230
|
+
'passkey_added',
|
|
231
|
+
'passkey_removed',
|
|
232
|
+
'recovery_codes_created',
|
|
233
|
+
'session_revoked',
|
|
234
|
+
'sessions_revoked',
|
|
235
|
+
'tailnet_linked',
|
|
236
|
+
'tailnet_unlinked',
|
|
237
|
+
]
|
|
238
|
+
// Changes worth a second look in a security review.
|
|
239
|
+
export const SENSITIVE_ACTIONS = [
|
|
240
|
+
'member_role_changed',
|
|
241
|
+
'agent_audience_changed',
|
|
242
|
+
'agent_approval_rule_changed',
|
|
243
|
+
'agent_policy_changed',
|
|
244
|
+
'share_links_enabled',
|
|
245
|
+
'project_made_public',
|
|
246
|
+
'token_created',
|
|
247
|
+
'workspace_exported',
|
|
248
|
+
'usage_exported',
|
|
249
|
+
'audit_exported',
|
|
250
|
+
'access_exported',
|
|
251
|
+
'passkey_added',
|
|
252
|
+
'passkey_removed',
|
|
253
|
+
// When the workspace asks for a passkey again, and who a tailnet login signs in as.
|
|
254
|
+
'step_up_changed',
|
|
255
|
+
'tailnet_linked',
|
|
256
|
+
]
|