shapewall 0.0.0-stage → 0.2.2
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 +139 -0
- package/Dockerfile.agent +45 -0
- package/LICENSE +202 -0
- package/NOTICE +12 -0
- package/README.md +651 -2
- package/THIRD_PARTY.md +165 -0
- package/bin/shapewall.js +29 -0
- package/dist/agent/package-lock.json +8384 -0
- package/dist/client/assets/BoardChat-D5SFd8HE.js +28 -0
- package/dist/client/assets/BuzzDialog-HWXBy52W.js +1 -0
- package/dist/client/assets/ChecksPanel-DTdImWoD.js +1 -0
- package/dist/client/assets/History-CEv6b-av.js +1 -0
- package/dist/client/assets/LocalSettings-C4vriEVQ.js +1 -0
- package/dist/client/assets/Management-DCfhcEUY.js +1 -0
- package/dist/client/assets/QuickConnect-DQGgUsl1.js +1 -0
- package/dist/client/assets/ShareDialog-C_plIto0.js +2 -0
- package/dist/client/assets/app-update-dialog-2hPz_N9G.js +1 -0
- package/dist/client/assets/circle-x-BZMPi8Zf.js +1 -0
- package/dist/client/assets/index-ByQRKVit.css +2 -0
- package/dist/client/assets/index-DweQ8Qtu.js +29 -0
- package/dist/client/assets/refresh-cw-CRow0Nbq.js +1 -0
- package/dist/client/assets/runtime-Bft6x_6E.js +4 -0
- package/dist/client/assets/square-A8EnIYLR.js +1 -0
- package/dist/client/assets/tts-dist-DYlBQmwG.js +18 -0
- package/dist/client/assets/tts-en-g2p-BPUtBFGG.js +1 -0
- package/dist/client/assets/tts-ort-wasm-simd-threaded-qh98TCA_.wasm +0 -0
- package/dist/client/assets/tts-ort.wasm.bundle.min-rEkcs_ow.js +12 -0
- package/dist/client/assets/tts-rolldown-runtime-D8NudN84.js +1 -0
- package/dist/client/assets/tts-spacy-en-tokenizer-6SNMl--A.json +1 -0
- package/dist/client/assets/tts-tts.worker-Bzm6aWbv.js +1 -0
- package/dist/client/assets/tts-us_gold-DcjeNMH1.json +92589 -0
- package/dist/client/assets/tts-us_silver-C964mEB5.json +93363 -0
- package/dist/client/assets/ui-CLbws0rx.js +41 -0
- package/dist/client/build-info.json +1 -0
- package/dist/client/favicon.svg +7 -0
- package/dist/client/icons/apple-touch-icon.png +0 -0
- package/dist/client/icons/icon-192.png +0 -0
- package/dist/client/icons/icon-512.png +0 -0
- package/dist/client/icons/maskable-512.png +0 -0
- package/dist/client/index.html +25 -0
- package/dist/client/manifest.webmanifest +15 -0
- package/dist/client/sw.js +53 -0
- package/dist/client/theme-boot.js +27 -0
- package/dist/console/assets/LocalAccountsApp-BQCcmj3x.js +1 -0
- package/dist/console/assets/LocalConsole-Bb2FUDs7.js +1 -0
- package/dist/console/assets/app-update-dialog-DIeYepLZ.js +1 -0
- package/dist/console/assets/console-C7uJLLOn.js +51 -0
- package/dist/console/assets/console-R8IBY3Am.css +2 -0
- package/dist/console/build-info.json +1 -0
- package/dist/console/console.html +21 -0
- package/dist/console/favicon.svg +7 -0
- package/dist/console/icons/apple-touch-icon.png +0 -0
- package/dist/console/icons/icon-192.png +0 -0
- package/dist/console/icons/icon-512.png +0 -0
- package/dist/console/icons/maskable-512.png +0 -0
- package/dist/console/manifest.webmanifest +15 -0
- package/dist/console/theme-boot.js +27 -0
- package/docs/agent-hardening.md +342 -0
- package/docs/agent-recipes.md +187 -0
- package/docs/agent-workflow.md +320 -0
- package/docs/agents.md +894 -0
- package/docs/architecture.md +242 -0
- package/docs/buzz.md +140 -0
- package/docs/capture-warnings.md +57 -0
- package/docs/config.md +489 -0
- package/docs/deployment.md +289 -0
- package/docs/design-system.md +162 -0
- package/docs/github.md +107 -0
- package/docs/hosted.md +764 -0
- 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/interaction-audits.md +40 -0
- package/docs/journeys.md +137 -0
- package/docs/local-accounts.md +80 -0
- package/docs/notifications.md +237 -0
- package/docs/page-checks.md +69 -0
- package/docs/performance.md +174 -0
- package/docs/starter-agent.md +325 -0
- package/docs/updates.md +87 -0
- package/examples/audits/interaction.audit.json +17 -0
- package/package.json +132 -4
- package/src/accounts/avatar.js +121 -0
- package/src/accounts/local.js +464 -0
- package/src/agent/acp/command.js +204 -0
- package/src/agent/acp/detect.js +75 -0
- package/src/agent/acp/sandbox.js +160 -0
- package/src/agent/agent-health.js +75 -0
- package/src/agent/backends/acp.js +1297 -0
- package/src/agent/backends/index.js +61 -0
- package/src/agent/backends/openai.js +308 -0
- package/src/agent/composer.js +221 -0
- package/src/agent/config.js +338 -0
- package/src/agent/context.js +191 -0
- package/src/agent/control.js +122 -0
- package/src/agent/errors.js +17 -0
- package/src/agent/feedback-loop.js +137 -0
- package/src/agent/manage.js +94 -0
- package/src/agent/next-steps.js +51 -0
- package/src/agent/owner-mcp.js +38 -0
- package/src/agent/plain-text.js +52 -0
- package/src/agent/ratings-core.js +213 -0
- package/src/agent/ratings.js +100 -0
- package/src/agent/reply-text.js +67 -0
- package/src/agent/routes.js +1065 -0
- package/src/agent/run.js +326 -0
- package/src/agent/settings-routes.js +704 -0
- package/src/agent/showable.js +23 -0
- package/src/agent/starter/approvals.js +181 -0
- package/src/agent/starter/build-image.js +90 -0
- package/src/agent/starter/cli.js +163 -0
- package/src/agent/starter/disk.js +53 -0
- package/src/agent/starter/doctor.js +180 -0
- package/src/agent/starter/egress-proxy.js +351 -0
- package/src/agent/starter/gate.js +90 -0
- package/src/agent/starter/loop.js +114 -0
- package/src/agent/starter/model-relay.js +289 -0
- package/src/agent/starter/pause.js +14 -0
- package/src/agent/starter/probe.js +272 -0
- package/src/agent/starter/prompt.js +41 -0
- package/src/agent/starter/push-guard.js +146 -0
- package/src/agent/starter/rpc.js +164 -0
- package/src/agent/starter/runtime.js +287 -0
- package/src/agent/starter/sandbox-main.js +121 -0
- package/src/agent/starter/settings.js +290 -0
- package/src/agent/starter/source.js +69 -0
- package/src/agent/starter/supervisor.js +755 -0
- package/src/agent/starter/sweep.js +46 -0
- package/src/agent/starter/tools.js +524 -0
- package/src/agent/starter/watch.js +84 -0
- package/src/agent/starter/worktree.js +409 -0
- package/src/agent/text-diff.js +210 -0
- package/src/agent/thread-history.js +223 -0
- package/src/agent/threads.js +399 -0
- package/src/agent/tools.js +200 -0
- package/src/agent/turn-changes.js +424 -0
- package/src/agent/undo-state.js +26 -0
- package/src/agent/user-settings.js +308 -0
- package/src/auth/record.js +375 -0
- package/src/broken-links.js +164 -0
- package/src/buzz/client.js +75 -0
- package/src/buzz/config.js +151 -0
- package/src/buzz/environment.js +13 -0
- package/src/buzz/gateway.js +192 -0
- package/src/buzz/routes.js +61 -0
- package/src/buzz/signer-child.js +205 -0
- package/src/buzz/store.js +89 -0
- package/src/buzz/transport.js +60 -0
- package/src/buzz/verify.js +140 -0
- package/src/capture/actions.js +159 -0
- package/src/capture/capture.js +776 -0
- package/src/capture/dom-names.js +189 -0
- package/src/capture/element-map.js +509 -0
- package/src/capture/live.js +827 -0
- package/src/capture/media-scheme.js +317 -0
- package/src/capture/network-summary.js +86 -0
- package/src/capture/pacing.js +34 -0
- package/src/capture/requests.js +167 -0
- package/src/capture/sanitize.js +752 -0
- package/src/checks/accessibility.js +134 -0
- package/src/checks/budgets.js +58 -0
- package/src/checks/cli.js +55 -0
- package/src/checks/comparison.js +113 -0
- package/src/checks/environments.js +32 -0
- package/src/checks/flow-config.js +82 -0
- package/src/checks/flow-origin.js +53 -0
- package/src/checks/flows.js +398 -0
- package/src/checks/lighthouse-auth.js +145 -0
- package/src/checks/lighthouse-findings.js +130 -0
- package/src/checks/lighthouse.js +395 -0
- package/src/checks/robots.js +85 -0
- package/src/checks/routes.js +52 -0
- package/src/checks/run-control.js +259 -0
- package/src/checks/runner.js +724 -0
- package/src/checks/schema.js +133 -0
- package/src/checks/seo-analysis.js +443 -0
- package/src/checks/seo-probe.js +190 -0
- package/src/checks/seo-snapshot.js +99 -0
- package/src/checks/store.js +214 -0
- package/src/checks/transport.js +47 -0
- package/src/cli/args.js +319 -0
- package/src/cli/capture.js +79 -0
- package/src/cli/commands.js +92 -0
- package/src/cli/comments.js +84 -0
- package/src/cli/help.js +243 -0
- package/src/cli/init.js +214 -0
- package/src/cli/misc.js +64 -0
- package/src/cli/notify.js +87 -0
- package/src/cli/open.js +22 -0
- package/src/cli/serve.js +274 -0
- package/src/cli/variant.js +82 -0
- package/src/cli/watch.js +70 -0
- package/src/cli.js +130 -0
- package/src/comment-fields.js +76 -0
- package/src/comments-cli.js +208 -0
- package/src/config-reload.js +100 -0
- package/src/config.js +1061 -0
- package/src/devices-cli.js +21 -0
- package/src/diff.js +95 -0
- package/src/discover.js +284 -0
- package/src/doctor.js +295 -0
- package/src/event-log.js +268 -0
- package/src/events-cli.js +102 -0
- package/src/events.js +113 -0
- package/src/export.js +119 -0
- package/src/feedback.js +246 -0
- package/src/github.js +551 -0
- package/src/hosted/LICENSE.md +105 -0
- package/src/hosted/access-cli.js +78 -0
- package/src/hosted/admin.js +682 -0
- package/src/hosted/agent-approvals.js +163 -0
- package/src/hosted/agent-policy.js +189 -0
- package/src/hosted/app.js +510 -0
- package/src/hosted/auth-routes.js +540 -0
- package/src/hosted/auth.js +536 -0
- package/src/hosted/backup.js +483 -0
- package/src/hosted/board-context.js +35 -0
- package/src/hosted/board-routes.js +486 -0
- package/src/hosted/board-view.js +94 -0
- package/src/hosted/clean-worker.js +24 -0
- package/src/hosted/cleaner.js +89 -0
- package/src/hosted/comments.js +355 -0
- package/src/hosted/console-routes.js +401 -0
- package/src/hosted/cookies.js +18 -0
- package/src/hosted/crypto.js +80 -0
- package/src/hosted/db.js +496 -0
- package/src/hosted/device-routes.js +175 -0
- package/src/hosted/device.js +233 -0
- package/src/hosted/events.js +63 -0
- package/src/hosted/guards.js +172 -0
- package/src/hosted/guest-view.js +65 -0
- package/src/hosted/http.js +39 -0
- package/src/hosted/import.js +300 -0
- package/src/hosted/mail.js +73 -0
- package/src/hosted/mcp-http.js +71 -0
- package/src/hosted/membership.js +114 -0
- package/src/hosted/operator.js +478 -0
- package/src/hosted/ops-cli.js +345 -0
- package/src/hosted/pages.js +146 -0
- package/src/hosted/permissions.js +113 -0
- package/src/hosted/presence.js +372 -0
- package/src/hosted/projects.js +124 -0
- package/src/hosted/public.js +109 -0
- package/src/hosted/push.js +622 -0
- package/src/hosted/quotas.js +115 -0
- package/src/hosted/ratelimit.js +96 -0
- package/src/hosted/rebind.js +316 -0
- package/src/hosted/retention.js +128 -0
- package/src/hosted/server.js +260 -0
- package/src/hosted/setup-link.js +64 -0
- package/src/hosted/setup.js +323 -0
- package/src/hosted/shares.js +444 -0
- package/src/hosted/tailscale.js +204 -0
- package/src/hosted/tenancy.js +301 -0
- package/src/hosted/token-api.js +455 -0
- package/src/hosted/token-events.js +60 -0
- package/src/hosted/tokens.js +57 -0
- package/src/hosted/turnstile.js +49 -0
- package/src/hosted/usercontent.js +154 -0
- package/src/hosted/zip.js +155 -0
- package/src/invocation.js +58 -0
- package/src/journey-paths.js +282 -0
- package/src/journeys.js +189 -0
- package/src/links.js +106 -0
- package/src/local-projects.js +182 -0
- package/src/lock.js +193 -0
- package/src/mcp.js +417 -0
- package/src/mentions.js +151 -0
- package/src/net.js +15 -0
- package/src/notify/config.js +275 -0
- package/src/notify/index.js +301 -0
- package/src/notify/lease.js +73 -0
- package/src/notify/messages.js +212 -0
- package/src/notify/run.js +174 -0
- package/src/pixeldiff.js +49 -0
- package/src/png.js +143 -0
- package/src/reactions.js +111 -0
- package/src/remote/cli.js +133 -0
- package/src/remote/credentials.js +62 -0
- package/src/remote/ids.js +12 -0
- package/src/remote/login.js +155 -0
- package/src/remote/remote-mcp.js +237 -0
- package/src/remote/remote-watch.js +291 -0
- package/src/remote/remote.js +313 -0
- package/src/remote/share-state.js +78 -0
- package/src/remote/share.js +369 -0
- package/src/remote/terminal.js +13 -0
- package/src/serve-state.js +64 -0
- package/src/server/app.js +824 -0
- package/src/server/devices.js +186 -0
- package/src/server/local-console.js +28 -0
- package/src/server/local-user.js +59 -0
- package/src/server/pairing-routes.js +159 -0
- package/src/server/share-routes.js +89 -0
- package/src/server/tailscale.js +97 -0
- package/src/sitemap.js +431 -0
- package/src/store.js +983 -0
- package/src/tokens.js +11 -0
- package/src/update/routes.js +245 -0
- package/src/watch.js +403 -0
- package/src/which.js +58 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Command approval belongs to a live claim, exact comment text, exact
|
|
2
|
+
// command, and token. The runner cannot approve its own request.
|
|
3
|
+
import { randomBytes } from 'node:crypto'
|
|
4
|
+
import { sha256 } from './crypto.js'
|
|
5
|
+
import { dispatchHash, AgentPolicyError, agentPolicy } from './agent-policy.js'
|
|
6
|
+
import { findApiToken } from './tenancy.js'
|
|
7
|
+
import { roleAtLeast } from './permissions.js'
|
|
8
|
+
|
|
9
|
+
export function mountHostedApprovals({ v1, boardApi, handle, scope, db, transaction, policyFor, boardContext, needFresh, auditOf, now }) {
|
|
10
|
+
const error = (statusCode, code, message) => new AgentPolicyError(statusCode, code, message)
|
|
11
|
+
const keyOf = (req) => ({ project: req.project.id, board: req.params.bid })
|
|
12
|
+
function validate(req, row) {
|
|
13
|
+
if (!row) throw error(404, 'unknown_approval', 'No such approval.')
|
|
14
|
+
const token = req.t.get('SELECT token_hash FROM api_tokens WHERE workspace_id = :ws AND project_id = :project AND id = :id', {
|
|
15
|
+
project: req.project.id,
|
|
16
|
+
id: row.token_id,
|
|
17
|
+
})
|
|
18
|
+
if (!token || !findApiToken(db, token.token_hash, now().toISOString()))
|
|
19
|
+
throw error(409, 'approval_expired', 'The requesting token is no longer active.')
|
|
20
|
+
const ctx = boardContext(req.t, req.project, row.board_id)
|
|
21
|
+
const comment = ctx.comments.get(row.comment_number)
|
|
22
|
+
const claim = req.t.get(
|
|
23
|
+
'SELECT id, token_id FROM agent_claims WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND comment_number = :number AND ended_at IS NULL ORDER BY created_at DESC LIMIT 1',
|
|
24
|
+
{ project: req.project.id, board: row.board_id, number: row.comment_number },
|
|
25
|
+
)
|
|
26
|
+
if (
|
|
27
|
+
row.expires_at <= now().toISOString() ||
|
|
28
|
+
comment.status !== 'open' ||
|
|
29
|
+
comment.agent_status !== 'working' ||
|
|
30
|
+
row.comment_hash !== dispatchHash(comment) ||
|
|
31
|
+
!policyFor(req.t).state(req.project.id, row.board_id, comment).dispatchable ||
|
|
32
|
+
claim?.token_id !== row.token_id ||
|
|
33
|
+
claim?.id !== row.claim_id
|
|
34
|
+
)
|
|
35
|
+
throw error(409, 'approval_expired', 'This approval no longer matches a live run.')
|
|
36
|
+
return row
|
|
37
|
+
}
|
|
38
|
+
const byId = (req) =>
|
|
39
|
+
req.t.get('SELECT * FROM agent_approvals WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND id = :id', {
|
|
40
|
+
...keyOf(req),
|
|
41
|
+
id: req.params.aid,
|
|
42
|
+
})
|
|
43
|
+
v1.post(
|
|
44
|
+
'/boards/:bid/comments/:cid/approvals',
|
|
45
|
+
scope('claim'),
|
|
46
|
+
handle((req, res) => {
|
|
47
|
+
const command = req.body?.command
|
|
48
|
+
if (typeof command !== 'string' || !command.trim() || command.length > 8000)
|
|
49
|
+
throw error(400, 'invalid_command', 'Command: 1 to 8000 characters.')
|
|
50
|
+
const ctx = boardContext(req.t, req.project, req.params.bid)
|
|
51
|
+
const comment = ctx.comments.get(req.params.cid)
|
|
52
|
+
const at = now().toISOString()
|
|
53
|
+
const id = `sa_${randomBytes(10).toString('hex')}`
|
|
54
|
+
const claim = req.t.get(
|
|
55
|
+
'SELECT id FROM agent_claims WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND comment_number = :number AND token_id = :token AND ended_at IS NULL ORDER BY created_at DESC LIMIT 1',
|
|
56
|
+
{ project: req.project.id, board: req.params.bid, number: comment.number, token: req.token.id },
|
|
57
|
+
)
|
|
58
|
+
const record = {
|
|
59
|
+
claim_id: claim?.id || null,
|
|
60
|
+
workspace_id: req.t.workspaceId,
|
|
61
|
+
project_id: req.project.id,
|
|
62
|
+
id,
|
|
63
|
+
board_id: req.params.bid,
|
|
64
|
+
comment_number: comment.number,
|
|
65
|
+
token_id: req.token.id,
|
|
66
|
+
comment_hash: dispatchHash(comment),
|
|
67
|
+
command,
|
|
68
|
+
command_hash: sha256(command),
|
|
69
|
+
reason: typeof req.body?.reason === 'string' ? req.body.reason.slice(0, 500) : '',
|
|
70
|
+
state: 'pending',
|
|
71
|
+
created_at: at,
|
|
72
|
+
expires_at: new Date(now().getTime() + 120000).toISOString(),
|
|
73
|
+
}
|
|
74
|
+
validate(req, record)
|
|
75
|
+
const count = req.t.get(
|
|
76
|
+
'SELECT COUNT(*) AS n FROM agent_approvals WHERE workspace_id = :ws AND project_id = :project AND token_id = :token AND expires_at > :at',
|
|
77
|
+
{ project: req.project.id, token: req.token.id, at },
|
|
78
|
+
).n
|
|
79
|
+
if (count >= 20) throw error(429, 'approval_budget', 'Too many approval requests.')
|
|
80
|
+
req.t.run(
|
|
81
|
+
`INSERT INTO agent_approvals (workspace_id,project_id,id,board_id,comment_number,token_id,comment_hash,claim_id,command,command_hash,reason,state,created_at,expires_at) VALUES (:ws,:project,:id,:board,:number,:token,:hash,:claim,:command,:commandHash,:reason,'pending',:at,:expires)`,
|
|
82
|
+
{
|
|
83
|
+
...keyOf(req),
|
|
84
|
+
id,
|
|
85
|
+
number: comment.number,
|
|
86
|
+
token: req.token.id,
|
|
87
|
+
hash: record.comment_hash,
|
|
88
|
+
claim: record.claim_id,
|
|
89
|
+
command,
|
|
90
|
+
commandHash: record.command_hash,
|
|
91
|
+
reason: record.reason,
|
|
92
|
+
at,
|
|
93
|
+
expires: record.expires_at,
|
|
94
|
+
},
|
|
95
|
+
)
|
|
96
|
+
res.status(201).json({ approval: record })
|
|
97
|
+
}),
|
|
98
|
+
)
|
|
99
|
+
v1.get(
|
|
100
|
+
'/boards/:bid/approvals/:aid',
|
|
101
|
+
scope('claim'),
|
|
102
|
+
handle((req, res) => {
|
|
103
|
+
const row = byId(req)
|
|
104
|
+
if (!row || row.token_id !== req.token.id) throw error(404, 'unknown_approval', 'No such approval.')
|
|
105
|
+
validate(req, row)
|
|
106
|
+
res.json({ approval: row })
|
|
107
|
+
}),
|
|
108
|
+
)
|
|
109
|
+
boardApi.get(
|
|
110
|
+
'/boards/:bid/approvals',
|
|
111
|
+
handle((req, res) => {
|
|
112
|
+
if (!roleAtLeast(req.role, 'admin') || !policyFor(req.t).trusted(agentPolicy(req.t, req.project.id, req.params.bid), req.user.id))
|
|
113
|
+
throw error(403, 'forbidden', 'Only trusted admins review shell commands.')
|
|
114
|
+
const rows = req.t.all(
|
|
115
|
+
"SELECT * FROM agent_approvals WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND state = 'pending' AND expires_at > :at",
|
|
116
|
+
{ ...keyOf(req), at: now().toISOString() },
|
|
117
|
+
)
|
|
118
|
+
res.json({
|
|
119
|
+
approvals: rows.filter((row) => {
|
|
120
|
+
try {
|
|
121
|
+
validate(req, row)
|
|
122
|
+
return true
|
|
123
|
+
} catch {
|
|
124
|
+
return false
|
|
125
|
+
}
|
|
126
|
+
}),
|
|
127
|
+
})
|
|
128
|
+
}),
|
|
129
|
+
)
|
|
130
|
+
boardApi.post(
|
|
131
|
+
'/boards/:bid/approvals/:aid',
|
|
132
|
+
needFresh,
|
|
133
|
+
handle((req, res) => {
|
|
134
|
+
if (!roleAtLeast(req.role, 'admin') || !policyFor(req.t).trusted(agentPolicy(req.t, req.project.id, req.params.bid), req.user.id))
|
|
135
|
+
throw error(403, 'forbidden', 'Only trusted admins approve shell commands.')
|
|
136
|
+
if (!['approved', 'denied'].includes(req.body?.state)) throw error(400, 'invalid_approval', 'Choose approved or denied.')
|
|
137
|
+
const row = transaction(db, () => {
|
|
138
|
+
const row = validate(req, byId(req))
|
|
139
|
+
if (row.state !== 'pending' || req.body?.hash !== row.command_hash)
|
|
140
|
+
throw error(409, 'approval_changed', 'This exact command is no longer pending.')
|
|
141
|
+
req.t.run(
|
|
142
|
+
'UPDATE agent_approvals SET state = :state, answered_by = :user, answered_at = :at WHERE workspace_id = :ws AND project_id = :project AND id = :id',
|
|
143
|
+
{ project: req.project.id, id: row.id, state: req.body.state, user: req.user.id, at: now().toISOString() },
|
|
144
|
+
)
|
|
145
|
+
// In the workspace's audit log: who let which command run (its hash;
|
|
146
|
+
// the command itself stays on the approval).
|
|
147
|
+
auditOf(
|
|
148
|
+
req,
|
|
149
|
+
req.body.state === 'approved' ? 'agent_command_approved' : 'agent_command_denied',
|
|
150
|
+
`${row.board_id}#${row.comment_number}`,
|
|
151
|
+
{
|
|
152
|
+
project: req.project.id,
|
|
153
|
+
approval: row.id,
|
|
154
|
+
token: row.token_id,
|
|
155
|
+
commandHash: row.command_hash,
|
|
156
|
+
},
|
|
157
|
+
)
|
|
158
|
+
return byId(req)
|
|
159
|
+
})
|
|
160
|
+
res.json({ approval: row })
|
|
161
|
+
}),
|
|
162
|
+
)
|
|
163
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// Dispatch is an explicit server capability bound to the text a member saw.
|
|
2
|
+
// Neither a display name nor a token can vouch for work.
|
|
3
|
+
import { extractMentions } from '../mentions.js'
|
|
4
|
+
import { randomBytes } from 'node:crypto'
|
|
5
|
+
import { sha256 } from './crypto.js'
|
|
6
|
+
import { roleAtLeast } from './permissions.js'
|
|
7
|
+
|
|
8
|
+
export class AgentPolicyError extends Error {
|
|
9
|
+
constructor(statusCode, code, message) {
|
|
10
|
+
super(message)
|
|
11
|
+
Object.assign(this, { statusCode, code })
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export const DEFAULT_POLICY = {
|
|
15
|
+
trustedHandles: [],
|
|
16
|
+
trustedRoles: ['admin', 'owner'],
|
|
17
|
+
boards: {},
|
|
18
|
+
onlyMentions: false,
|
|
19
|
+
dailyClaims: 50,
|
|
20
|
+
maxOpenClaims: 3,
|
|
21
|
+
approveNonAdmin: true,
|
|
22
|
+
}
|
|
23
|
+
export const expandScopes = (scopes) => [...new Set(scopes.flatMap((scope) => (scope === 'comment' ? ['reply', 'status'] : [scope])))]
|
|
24
|
+
export const dispatchHash = (row) => sha256(JSON.stringify([row.body, row.anchor || null, row.page_id, row.viewport, row.x, row.y]))
|
|
25
|
+
export function cleanPolicy(input) {
|
|
26
|
+
const out = { ...DEFAULT_POLICY, boards: {} }
|
|
27
|
+
if (!input || typeof input !== 'object' || Array.isArray(input))
|
|
28
|
+
throw new AgentPolicyError(400, 'invalid_policy', 'Expected an agent policy.')
|
|
29
|
+
for (const key of ['trustedHandles', 'trustedRoles']) {
|
|
30
|
+
if (input[key] === undefined) continue
|
|
31
|
+
if (
|
|
32
|
+
!Array.isArray(input[key]) ||
|
|
33
|
+
input[key].length > 100 ||
|
|
34
|
+
input[key].some(
|
|
35
|
+
(value) =>
|
|
36
|
+
typeof value !== 'string' ||
|
|
37
|
+
(key === 'trustedRoles' ? !['member', 'admin', 'owner'].includes(value) : !/^[a-z][\w.-]{0,39}$/.test(value)),
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
throw new AgentPolicyError(400, 'invalid_policy', `Invalid ${key}.`)
|
|
41
|
+
out[key] = [...new Set(input[key])]
|
|
42
|
+
}
|
|
43
|
+
for (const key of ['onlyMentions', 'approveNonAdmin']) {
|
|
44
|
+
if (input[key] !== undefined && typeof input[key] !== 'boolean') throw new AgentPolicyError(400, 'invalid_policy', `Invalid ${key}.`)
|
|
45
|
+
if (input[key] !== undefined) out[key] = input[key]
|
|
46
|
+
}
|
|
47
|
+
for (const [key, max] of [
|
|
48
|
+
['dailyClaims', 10000],
|
|
49
|
+
['maxOpenClaims', 100],
|
|
50
|
+
]) {
|
|
51
|
+
if (input[key] !== undefined && (!Number.isInteger(input[key]) || input[key] < 0 || input[key] > max))
|
|
52
|
+
throw new AgentPolicyError(400, 'invalid_policy', `Invalid ${key}.`)
|
|
53
|
+
if (input[key] !== undefined) out[key] = input[key]
|
|
54
|
+
}
|
|
55
|
+
if (input.boards !== undefined) {
|
|
56
|
+
if (!input.boards || typeof input.boards !== 'object' || Array.isArray(input.boards) || Object.keys(input.boards).length > 100)
|
|
57
|
+
throw new AgentPolicyError(400, 'invalid_policy', 'Invalid board overrides.')
|
|
58
|
+
out.boards = {}
|
|
59
|
+
for (const [id, value] of Object.entries(input.boards)) {
|
|
60
|
+
if (!/^[a-zA-Z0-9][\w-]{0,63}$/.test(id) || value?.boards !== undefined)
|
|
61
|
+
throw new AgentPolicyError(400, 'invalid_policy', 'Invalid board override.')
|
|
62
|
+
const validated = cleanPolicy(value)
|
|
63
|
+
out.boards[id] = Object.fromEntries(
|
|
64
|
+
Object.keys(value)
|
|
65
|
+
.filter((key) => Object.hasOwn(DEFAULT_POLICY, key) && key !== 'boards')
|
|
66
|
+
.map((key) => [key, validated[key]]),
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return out
|
|
71
|
+
}
|
|
72
|
+
export function agentPolicy(t, project, board = null) {
|
|
73
|
+
const row = t.get('SELECT agent_policy FROM projects WHERE workspace_id = :ws AND id = :project', { project })
|
|
74
|
+
const policy = cleanPolicy(JSON.parse(row?.agent_policy || '{}'))
|
|
75
|
+
return board && policy.boards[board] ? { ...policy, ...policy.boards[board] } : policy
|
|
76
|
+
}
|
|
77
|
+
export function trustedMember(t, policy, user) {
|
|
78
|
+
if (!user) return false
|
|
79
|
+
const member = t.get('SELECT role FROM memberships WHERE workspace_id = :ws AND user_id = :user', { user })
|
|
80
|
+
if (!member || !roleAtLeast(member.role, 'member')) return false
|
|
81
|
+
// User handles are account data, looked up by caller through the injected account query.
|
|
82
|
+
return policy.trustedRoles.includes(member.role) || policy.trustedHandles.includes(t.handleFor?.(user))
|
|
83
|
+
}
|
|
84
|
+
export function createAgentPolicy(t, { handleFor = () => null, now = () => new Date() } = {}) {
|
|
85
|
+
const trusted = (policy, user) => trustedMember({ ...t, handleFor }, policy, user)
|
|
86
|
+
function state(project, board, row) {
|
|
87
|
+
const policy = agentPolicy(t, project, board)
|
|
88
|
+
const eligible =
|
|
89
|
+
!['agent', 'guest'].includes(row.import_author_kind) &&
|
|
90
|
+
!row.author_share_id &&
|
|
91
|
+
!row.author_token_id &&
|
|
92
|
+
Boolean(row.author_user_id || row.imported || row.imported_by)
|
|
93
|
+
const vouched = eligible && Boolean(row.dispatch_hash && row.dispatch_hash === dispatchHash(row) && trusted(policy, row.dispatch_by))
|
|
94
|
+
const mentions = !policy.onlyMentions || extractMentions(row.body).includes('agent')
|
|
95
|
+
return {
|
|
96
|
+
dispatchable: Boolean(vouched && mentions && row.run_approved && row.status === 'open'),
|
|
97
|
+
needsRunApproval: Boolean(vouched && !row.run_approved),
|
|
98
|
+
vouchedBy: vouched ? handleFor(row.dispatch_by) : null,
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// `expect` (when given): the dispatch hash the person saw. A comment edited
|
|
102
|
+
// since then is refused, so nobody vouches for text they did not read.
|
|
103
|
+
function vouch(project, board, row, user, { approve = false, role = null, expect = undefined } = {}) {
|
|
104
|
+
const policy = agentPolicy(t, project, board)
|
|
105
|
+
if (!trusted(policy, user)) throw new AgentPolicyError(403, 'untrusted_member', 'Only a trusted member may send work to an agent.')
|
|
106
|
+
if (
|
|
107
|
+
['agent', 'guest'].includes(row.import_author_kind) ||
|
|
108
|
+
row.author_share_id ||
|
|
109
|
+
row.author_token_id ||
|
|
110
|
+
(!row.author_user_id && !row.imported && !row.imported_by)
|
|
111
|
+
)
|
|
112
|
+
throw new AgentPolicyError(403, 'not_dispatchable', 'Guest and agent comments cannot be sent to agents.')
|
|
113
|
+
if (expect !== undefined) {
|
|
114
|
+
if (typeof expect !== 'string' || !expect)
|
|
115
|
+
throw new AgentPolicyError(
|
|
116
|
+
400,
|
|
117
|
+
'dispatch_hash_required',
|
|
118
|
+
'This page is out of date. Reload it, read the comment again, then send it.',
|
|
119
|
+
)
|
|
120
|
+
if (expect !== dispatchHash(row))
|
|
121
|
+
throw new AgentPolicyError(409, 'comment_changed', 'This comment changed since you opened it. Read it again, then send it.')
|
|
122
|
+
}
|
|
123
|
+
const authorRole = row.author_user_id
|
|
124
|
+
? t.get('SELECT role FROM memberships WHERE workspace_id = :ws AND user_id = :user', { user: row.author_user_id })?.role
|
|
125
|
+
: null
|
|
126
|
+
const approved =
|
|
127
|
+
approve || !policy.approveNonAdmin || (row.author_user_id === user && roleAtLeast(authorRole, 'admin') && roleAtLeast(role, 'admin'))
|
|
128
|
+
t.run(
|
|
129
|
+
`UPDATE comments SET dispatch_hash = :hash, dispatch_by = :user, dispatch_at = :at, run_approved = :approved WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND number = :number`,
|
|
130
|
+
{ project, board, number: row.number, hash: dispatchHash(row), user, at: now().toISOString(), approved: approved ? 1 : 0 },
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
function autoVouch(project, board, row, user, role) {
|
|
134
|
+
if (trusted(agentPolicy(t, project, board), user)) vouch(project, board, row, user, { role })
|
|
135
|
+
}
|
|
136
|
+
function claim(project, board, row, token) {
|
|
137
|
+
if (!state(project, board, row).dispatchable)
|
|
138
|
+
throw new AgentPolicyError(403, 'not_dispatchable', 'A trusted member must send the current comment to an agent first.')
|
|
139
|
+
const policy = agentPolicy(t, project, board)
|
|
140
|
+
const at = now().toISOString()
|
|
141
|
+
// Stale claims are closed before evaluating the open budget.
|
|
142
|
+
t.run(
|
|
143
|
+
`UPDATE agent_claims SET ended_at = :at WHERE workspace_id = :ws AND project_id = :project AND ended_at IS NULL AND NOT EXISTS (SELECT 1 FROM comments c WHERE c.workspace_id = :ws AND c.project_id = :project AND c.board_id = agent_claims.board_id AND c.number = agent_claims.comment_number AND c.agent_status IN ('queued','working') AND c.status = 'open' AND c.dispatch_hash = agent_claims.hash)`,
|
|
144
|
+
{ project, at },
|
|
145
|
+
)
|
|
146
|
+
for (const active of t.all('SELECT * FROM agent_claims WHERE workspace_id = :ws AND project_id = :project AND ended_at IS NULL', {
|
|
147
|
+
project,
|
|
148
|
+
})) {
|
|
149
|
+
const comment = t.get(
|
|
150
|
+
'SELECT * FROM comments WHERE workspace_id = :ws AND project_id = :project AND board_id = :board AND number = :number',
|
|
151
|
+
{ project, board: active.board_id, number: active.comment_number },
|
|
152
|
+
)
|
|
153
|
+
const token = t.get('SELECT * FROM api_tokens WHERE workspace_id = :ws AND project_id = :project AND id = :id', {
|
|
154
|
+
project,
|
|
155
|
+
id: active.token_id,
|
|
156
|
+
})
|
|
157
|
+
const creator = token?.created_by
|
|
158
|
+
? t.get('SELECT role FROM memberships WHERE workspace_id = :ws AND user_id = :user', { user: token.created_by })
|
|
159
|
+
: null
|
|
160
|
+
if (
|
|
161
|
+
!comment ||
|
|
162
|
+
!state(project, active.board_id, comment).dispatchable ||
|
|
163
|
+
!token ||
|
|
164
|
+
token.revoked_at ||
|
|
165
|
+
(token.expires_at && token.expires_at <= at) ||
|
|
166
|
+
!roleAtLeast(creator?.role, 'admin')
|
|
167
|
+
)
|
|
168
|
+
t.run('UPDATE agent_claims SET ended_at = :at WHERE workspace_id = :ws AND project_id = :project AND id = :id', {
|
|
169
|
+
project,
|
|
170
|
+
id: active.id,
|
|
171
|
+
at,
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
const daily = t.get(
|
|
175
|
+
'SELECT COUNT(*) AS n FROM agent_claims WHERE workspace_id = :ws AND project_id = :project AND created_at >= :start',
|
|
176
|
+
{ project, start: `${at.slice(0, 10)}T00:00:00.000Z` },
|
|
177
|
+
).n
|
|
178
|
+
const open = t.get('SELECT COUNT(*) AS n FROM agent_claims WHERE workspace_id = :ws AND project_id = :project AND ended_at IS NULL', {
|
|
179
|
+
project,
|
|
180
|
+
}).n
|
|
181
|
+
if (daily >= policy.dailyClaims || open >= policy.maxOpenClaims)
|
|
182
|
+
throw new AgentPolicyError(429, 'agent_budget', 'The project agent budget is exhausted. Ask an admin to review Agent policy.')
|
|
183
|
+
t.run(
|
|
184
|
+
'INSERT INTO agent_claims (workspace_id,id,project_id,board_id,comment_number,token_id,hash,created_at) VALUES (:ws,:id,:project,:board,:number,:token,:hash,:at)',
|
|
185
|
+
{ project, board, number: row.number, token, id: randomBytes(16).toString('hex'), hash: dispatchHash(row), at },
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
return { state, vouch, autoVouch, claim, trusted }
|
|
189
|
+
}
|