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,23 @@
|
|
|
1
|
+
// Text for an approval card with nothing hidden: control characters,
|
|
2
|
+
// zero-width characters and bidirectional overrides (which can make text
|
|
3
|
+
// read differently from what runs) are shown as escapes. Used for command
|
|
4
|
+
// and path details (src/agent/backends/acp.js) and for every diff line
|
|
5
|
+
// (src/agent/text-diff.js).
|
|
6
|
+
|
|
7
|
+
// eslint-disable-next-line no-control-regex
|
|
8
|
+
const HIDDEN_RE = /[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2066-\u2069\ufeff]/g
|
|
9
|
+
// The same without the tab: a diff of indented code stays readable (a tab
|
|
10
|
+
// hides nothing; it shows as space).
|
|
11
|
+
// eslint-disable-next-line no-control-regex
|
|
12
|
+
const HIDDEN_KEEP_TAB_RE = /[\u0000-\u0008\u000a-\u001f\u007f-\u009f\u200b-\u200f\u2028-\u202e\u2066-\u2069\ufeff]/g
|
|
13
|
+
|
|
14
|
+
const escape = (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`
|
|
15
|
+
|
|
16
|
+
export function showable(text) {
|
|
17
|
+
return String(text).replace(HIDDEN_RE, escape)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// For a line of a file: as showable(), but tabs stay tabs.
|
|
21
|
+
export function showableLine(text) {
|
|
22
|
+
return String(text).replace(HIDDEN_KEEP_TAB_RE, escape)
|
|
23
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// Approving the starter agent's shell commands from the board.
|
|
2
|
+
//
|
|
3
|
+
// `shapewall agent run` and `shapewall serve` are separate processes, so an
|
|
4
|
+
// approval is a file: <board>/agent/approvals/<id>.json, written by the
|
|
5
|
+
// agent run with the exact command, and answered by the board's
|
|
6
|
+
// permission route (POST /api/agent/boards/:id/threads/:thread/permissions/:id,
|
|
7
|
+
// the same card flow as an ACP agent's requests). The card shows in the
|
|
8
|
+
// comment's chat thread. Only the person at this machine may answer: the
|
|
9
|
+
// route needs the settings token from the `shapewall serve` link and a
|
|
10
|
+
// local, direct, same-origin request (src/agent/settings-routes.js), so a
|
|
11
|
+
// visitor of a shared board cannot approve a command.
|
|
12
|
+
//
|
|
13
|
+
// The agent run polls the file; no answer before it expires is a no. An
|
|
14
|
+
// answer is bound to the command: the run checks the stored hash before it
|
|
15
|
+
// lets the command run.
|
|
16
|
+
|
|
17
|
+
import fs from 'node:fs'
|
|
18
|
+
import path from 'node:path'
|
|
19
|
+
import { createHash, randomBytes } from 'node:crypto'
|
|
20
|
+
import { withFileLock } from '../../lock.js'
|
|
21
|
+
import { boardDir, readJson, writeJsonAtomic } from '../../store.js'
|
|
22
|
+
import { THREAD_RE, changeMessage } from '../threads.js'
|
|
23
|
+
import { boardUrlFor, commentLink } from '../../notify/messages.js'
|
|
24
|
+
import { pause } from './pause.js'
|
|
25
|
+
import { processAlive } from './worktree.js'
|
|
26
|
+
|
|
27
|
+
export const APPROVAL_ID_RE = /^sa_[a-f0-9]{20}$/
|
|
28
|
+
export const APPROVAL_MS = 2 * 60 * 1000
|
|
29
|
+
export const RUN_OPTION = { optionId: 'run', name: 'Run', kind: 'allow_once' }
|
|
30
|
+
export const DENY_OPTION = { optionId: 'deny', name: "Don't run", kind: 'reject_once' }
|
|
31
|
+
|
|
32
|
+
export const commandHash = (command) => createHash('sha256').update(String(command)).digest('hex')
|
|
33
|
+
|
|
34
|
+
function approvalFile(config, boardId, id) {
|
|
35
|
+
if (!APPROVAL_ID_RE.test(String(id))) return null
|
|
36
|
+
return path.join(boardDir(config, boardId), 'agent', 'approvals', `${id}.json`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// What the card shows: the whole command with hidden characters escaped.
|
|
40
|
+
export function showCommand(command) {
|
|
41
|
+
return String(command).replace(
|
|
42
|
+
// eslint-disable-next-line no-control-regex
|
|
43
|
+
/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u200b-\u200f\u202a-\u202e\u2066-\u2069]/g,
|
|
44
|
+
(char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`,
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function permissionCard(record) {
|
|
49
|
+
return {
|
|
50
|
+
id: record.id,
|
|
51
|
+
kind: 'permission',
|
|
52
|
+
name: 'execute',
|
|
53
|
+
label: 'Run a command in the sandbox',
|
|
54
|
+
detail: `${showCommand(record.command)}\n\nWhy: ${showCommand(record.reason || '')}\nIn the agent's container (worktree only, no network beyond the allow list).`,
|
|
55
|
+
state: record.state,
|
|
56
|
+
options: [RUN_OPTION, DENY_OPTION],
|
|
57
|
+
chosen: record.state === 'approved' ? RUN_OPTION.name : record.state === 'denied' ? DENY_OPTION.name : null,
|
|
58
|
+
expiresAt: record.expiresAt,
|
|
59
|
+
answeredBy: record.answeredBy || null,
|
|
60
|
+
// Answerable from the stored thread (no answer is being streamed).
|
|
61
|
+
answerable: true,
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// The agent run asks: writes the record. Returns it.
|
|
66
|
+
export function createApproval(config, boardId, { threadId, messageId, command, reason, now = new Date(), ms = APPROVAL_MS }) {
|
|
67
|
+
if (!THREAD_RE.test(threadId)) throw new Error('bad thread')
|
|
68
|
+
const id = `sa_${randomBytes(10).toString('hex')}`
|
|
69
|
+
const record = {
|
|
70
|
+
id,
|
|
71
|
+
threadId,
|
|
72
|
+
messageId,
|
|
73
|
+
command,
|
|
74
|
+
reason,
|
|
75
|
+
hash: commandHash(command),
|
|
76
|
+
state: 'pending',
|
|
77
|
+
createdAt: now.toISOString(),
|
|
78
|
+
expiresAt: new Date(now.getTime() + ms).toISOString(),
|
|
79
|
+
answeredAt: null,
|
|
80
|
+
}
|
|
81
|
+
const file = approvalFile(config, boardId, id)
|
|
82
|
+
fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
|
|
83
|
+
writeJsonAtomic(file, record)
|
|
84
|
+
return record
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function readApproval(config, boardId, id) {
|
|
88
|
+
const file = approvalFile(config, boardId, id)
|
|
89
|
+
return file ? readJson(file) : null
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Sets a pending record's state (approved, denied, expired, cancelled) once.
|
|
93
|
+
// Returns the record after the change, or null when there is none.
|
|
94
|
+
export async function settleApproval(config, boardId, id, state, { now = new Date(), threadId = null, answeredBy = null } = {}) {
|
|
95
|
+
const file = approvalFile(config, boardId, id)
|
|
96
|
+
if (!file || !fs.existsSync(file)) return null
|
|
97
|
+
const record = await withFileLock(file, () => {
|
|
98
|
+
const current = readJson(file)
|
|
99
|
+
if (!current || (threadId && current.threadId !== threadId)) return null
|
|
100
|
+
if (current.state !== 'pending') return current
|
|
101
|
+
current.state = Date.parse(current.expiresAt) <= now.getTime() && state !== 'cancelled' ? 'expired' : state
|
|
102
|
+
current.answeredAt = now.toISOString()
|
|
103
|
+
current.answeredBy = answeredBy
|
|
104
|
+
writeJsonAtomic(file, current)
|
|
105
|
+
return current
|
|
106
|
+
})
|
|
107
|
+
if (record) await updateCard(config, boardId, record)
|
|
108
|
+
return record
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The board's answer (POST .../permissions/:id): 'approved' | 'denied' |
|
|
112
|
+
// 'expired', false for an option the card does not have, null when there is
|
|
113
|
+
// no such pending request in this thread.
|
|
114
|
+
export async function answerApproval(config, boardId, threadId, id, optionId, { now = new Date(), answeredBy = null } = {}) {
|
|
115
|
+
const record = readApproval(config, boardId, id)
|
|
116
|
+
if (!record || record.threadId !== threadId || record.state !== 'pending') return null
|
|
117
|
+
if (optionId !== null && optionId !== RUN_OPTION.optionId && optionId !== DENY_OPTION.optionId) return false
|
|
118
|
+
const settled = await settleApproval(config, boardId, id, optionId === RUN_OPTION.optionId ? 'approved' : 'denied', {
|
|
119
|
+
now,
|
|
120
|
+
threadId,
|
|
121
|
+
answeredBy,
|
|
122
|
+
})
|
|
123
|
+
return settled?.state || null
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// The board link the terminal prints while a command waits: the comment
|
|
127
|
+
// selected and its chat thread (where the card is) open. The running
|
|
128
|
+
// board's own URL from <outputDir>/serve.json ({ url, pid }, written by
|
|
129
|
+
// `shapewall serve`) when that server is still running, else the usual
|
|
130
|
+
// board URL (SHAPEWALL_BOARD_URL, notifications.boardUrl, the default).
|
|
131
|
+
export function approvalLink(config, boardId, comment, { env = process.env, isAlive = processAlive } = {}) {
|
|
132
|
+
let base = null
|
|
133
|
+
try {
|
|
134
|
+
const serve = JSON.parse(fs.readFileSync(path.join(config.outputDir, 'serve.json'), 'utf8'))
|
|
135
|
+
if (typeof serve?.url === 'string' && /^https?:\/\//.test(serve.url) && isAlive(Number(serve.pid))) base = serve.url
|
|
136
|
+
} catch {
|
|
137
|
+
/* no running board recorded */
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const url = new URL(commentLink(config, base || boardUrlFor(config, { env }), boardId, comment))
|
|
141
|
+
url.searchParams.set('chat', `c${comment.id}`)
|
|
142
|
+
return url.toString()
|
|
143
|
+
} catch {
|
|
144
|
+
return null
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function updateCard(config, boardId, record) {
|
|
149
|
+
if (!record.messageId) return
|
|
150
|
+
await changeMessage(config, boardId, record.threadId, record.messageId, (message) => {
|
|
151
|
+
const cards = Array.isArray(message.cards) ? message.cards : []
|
|
152
|
+
const card = permissionCard(record)
|
|
153
|
+
const index = cards.findIndex((item) => item.id === card.id)
|
|
154
|
+
if (index >= 0) cards[index] = card
|
|
155
|
+
else cards.push(card)
|
|
156
|
+
message.cards = cards
|
|
157
|
+
}).catch(() => {})
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Waits for the answer: resolves { approved, state }. Expires, and follows
|
|
161
|
+
// `signal` (the run ending) as a cancel.
|
|
162
|
+
export async function waitForApproval(config, boardId, record, { signal = null, pollMs = 500, now = () => Date.now() } = {}) {
|
|
163
|
+
for (;;) {
|
|
164
|
+
const current = readApproval(config, boardId, record.id)
|
|
165
|
+
if (current && current.state !== 'pending') {
|
|
166
|
+
return {
|
|
167
|
+
approved: current.state === 'approved' && current.hash === record.hash && current.command === record.command,
|
|
168
|
+
state: current.state,
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (signal?.aborted) {
|
|
172
|
+
const settled = await settleApproval(config, boardId, record.id, 'cancelled')
|
|
173
|
+
return { approved: false, state: settled?.state || 'cancelled' }
|
|
174
|
+
}
|
|
175
|
+
if (now() >= Date.parse(record.expiresAt)) {
|
|
176
|
+
const settled = await settleApproval(config, boardId, record.id, 'expired')
|
|
177
|
+
return { approved: settled?.state === 'approved' && settled.hash === record.hash, state: settled?.state || 'expired' }
|
|
178
|
+
}
|
|
179
|
+
await pause(pollMs, signal)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// `shapewall agent build-image`: builds the starter agent's image from the
|
|
2
|
+
// Dockerfile.agent that ships in this package, so an npm install is enough
|
|
3
|
+
// (no clone of the source). The image is built on this machine and never
|
|
4
|
+
// pulled or pushed; `agent run` starts it with --pull never.
|
|
5
|
+
//
|
|
6
|
+
// The build context is a folder made for the build with only what the
|
|
7
|
+
// Dockerfile copies: package.json, the lockfile, LICENSE, NOTICE, bin and
|
|
8
|
+
// src. npm never puts package-lock.json in a package, so the package ships
|
|
9
|
+
// a copy in dist/agent/ (the prepack script); a clone has its own. The
|
|
10
|
+
// image always installs the locked versions: the Playwright in it must
|
|
11
|
+
// match the browsers of its base image.
|
|
12
|
+
|
|
13
|
+
import fs from 'node:fs'
|
|
14
|
+
import os from 'node:os'
|
|
15
|
+
import path from 'node:path'
|
|
16
|
+
import { spawn } from 'node:child_process'
|
|
17
|
+
import { randomBytes } from 'node:crypto'
|
|
18
|
+
import { fileURLToPath } from 'node:url'
|
|
19
|
+
|
|
20
|
+
// The installed package's own folder (or the clone's).
|
|
21
|
+
export const PACKAGE_DIR = path.resolve(fileURLToPath(new URL('../../../', import.meta.url)))
|
|
22
|
+
export const DOCKERFILE = 'Dockerfile.agent'
|
|
23
|
+
export const CONTEXT_FILES = ['package.json', 'LICENSE', 'NOTICE', DOCKERFILE]
|
|
24
|
+
export const CONTEXT_DIRS = ['bin', 'src']
|
|
25
|
+
// Where the lockfile is: a clone's own, else the package's copy.
|
|
26
|
+
export const LOCKFILES = ['package-lock.json', path.join('dist', 'agent', 'package-lock.json')]
|
|
27
|
+
|
|
28
|
+
export function buildImageArgs({ image, context }) {
|
|
29
|
+
return ['build', '-f', path.join(context, DOCKERFILE), '-t', image, context]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// A fresh build context under the cache folder (on disk), from `dir`.
|
|
33
|
+
export function prepareContext({ dir = PACKAGE_DIR, env = process.env } = {}) {
|
|
34
|
+
if (!fs.existsSync(path.join(dir, DOCKERFILE))) {
|
|
35
|
+
throw new Error(`${DOCKERFILE} is not in ${dir}: reinstall shapewall, or build from a clone of the repository`)
|
|
36
|
+
}
|
|
37
|
+
const lock = LOCKFILES.map((rel) => path.join(dir, rel)).find((file) => fs.existsSync(file))
|
|
38
|
+
if (!lock) throw new Error(`no package-lock.json in ${dir} (nor its dist/agent copy): reinstall shapewall, or build from a clone`)
|
|
39
|
+
const cache =
|
|
40
|
+
env.XDG_CACHE_HOME || (process.platform === 'darwin' ? path.join(os.homedir(), 'Library', 'Caches') : path.join(os.homedir(), '.cache'))
|
|
41
|
+
const context = path.join(cache, 'shapewall', `agent-image-${randomBytes(4).toString('hex')}`)
|
|
42
|
+
fs.mkdirSync(context, { recursive: true, mode: 0o700 })
|
|
43
|
+
try {
|
|
44
|
+
for (const name of CONTEXT_FILES) fs.copyFileSync(path.join(dir, name), path.join(context, name))
|
|
45
|
+
for (const name of CONTEXT_DIRS) fs.cpSync(path.join(dir, name), path.join(context, name), { recursive: true, verbatimSymlinks: true })
|
|
46
|
+
fs.copyFileSync(lock, path.join(context, 'package-lock.json'))
|
|
47
|
+
} catch (error) {
|
|
48
|
+
fs.rmSync(context, { recursive: true, force: true })
|
|
49
|
+
throw error
|
|
50
|
+
}
|
|
51
|
+
return context
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Runs `<runtime> build` without a shell, its output streamed as it comes,
|
|
55
|
+
// then removes the context. Resolves the exit code.
|
|
56
|
+
export async function buildImage({
|
|
57
|
+
runtime,
|
|
58
|
+
image,
|
|
59
|
+
dir = PACKAGE_DIR,
|
|
60
|
+
env = process.env,
|
|
61
|
+
stdout = process.stdout,
|
|
62
|
+
stderr = process.stderr,
|
|
63
|
+
spawnImpl = spawn,
|
|
64
|
+
onArgs = () => {},
|
|
65
|
+
}) {
|
|
66
|
+
const context = prepareContext({ dir, env })
|
|
67
|
+
const args = buildImageArgs({ image, context })
|
|
68
|
+
onArgs(args)
|
|
69
|
+
try {
|
|
70
|
+
return await new Promise((resolve) => {
|
|
71
|
+
let child
|
|
72
|
+
try {
|
|
73
|
+
child = spawnImpl(runtime.bin, args, { cwd: context, env, stdio: ['ignore', 'pipe', 'pipe'], shell: false })
|
|
74
|
+
} catch (error) {
|
|
75
|
+
stderr.write(`could not start ${runtime.name}: ${error.message}\n`)
|
|
76
|
+
resolve(1)
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
child.stdout.on('data', (chunk) => stdout.write(chunk))
|
|
80
|
+
child.stderr.on('data', (chunk) => stderr.write(chunk))
|
|
81
|
+
child.once('error', (error) => {
|
|
82
|
+
stderr.write(`could not start ${runtime.name}: ${error.message}\n`)
|
|
83
|
+
resolve(1)
|
|
84
|
+
})
|
|
85
|
+
child.once('close', (code) => resolve(code ?? 1))
|
|
86
|
+
})
|
|
87
|
+
} finally {
|
|
88
|
+
fs.rmSync(context, { recursive: true, force: true })
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// `shapewall agent run | watch | doctor | build-image` (and the internal `agent sandbox`).
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { loadConfig, findConfigFile } from '../../config.js'
|
|
6
|
+
import { githubSettings, linkPull, openPullRequest } from '../../github.js'
|
|
7
|
+
import { createRuntime, detectRuntime, agentImage } from './runtime.js'
|
|
8
|
+
import { starterSettings } from './settings.js'
|
|
9
|
+
import { localSource, remoteSource } from './source.js'
|
|
10
|
+
import { git, workRoot } from './worktree.js'
|
|
11
|
+
|
|
12
|
+
// The config, or (with --remote and no config here) an empty one: the
|
|
13
|
+
// endpoint and trust list then come from the environment and flags.
|
|
14
|
+
function configFor(flags) {
|
|
15
|
+
const file = findConfigFile(flags.config)
|
|
16
|
+
if (flags.remote !== undefined && !fs.existsSync(file)) {
|
|
17
|
+
return { file: null, dir: process.cwd(), outputDir: null, agent: null, starterAgent: null, boards: [], github: null }
|
|
18
|
+
}
|
|
19
|
+
return loadConfig(flags.config)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function sourceFor(config, flags) {
|
|
23
|
+
if (flags.remote === undefined) return localSource(config, flags.board || null)
|
|
24
|
+
const { createRemote, remoteSettings } = await import('../../remote/remote.js')
|
|
25
|
+
const { remoteBoard } = await import('../../remote/cli.js')
|
|
26
|
+
const remote = createRemote(remoteSettings(flags))
|
|
27
|
+
const { project, boardId } = await remoteBoard(remote, flags.board)
|
|
28
|
+
const info = (project.boards || []).find((item) => item.id === boardId) || {}
|
|
29
|
+
return remoteSource(remote, boardId, info)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function runtimeFor(flags, env) {
|
|
33
|
+
const found = detectRuntime({ flag: flags.runtime || null, env })
|
|
34
|
+
if (found.problem) return { runtime: null, problem: found.problem }
|
|
35
|
+
return { runtime: createRuntime({ name: found.name, bin: found.bin, env }) }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The pull request step (local boards with GitHub connect): open it from the
|
|
39
|
+
// pushed branch into the base, then link it to the comment.
|
|
40
|
+
function pullRequester(config, settings, source, env) {
|
|
41
|
+
return async ({ branch, comment, remote, fork }) => {
|
|
42
|
+
const github = githubSettings(config, env)
|
|
43
|
+
if (!github.ready) throw new Error('GitHub connect is not set up (github.repo and a token, docs/github.md)')
|
|
44
|
+
let base = settings.prBase
|
|
45
|
+
if (!base) {
|
|
46
|
+
const head = await git(['symbolic-ref', '--short', `refs/remotes/${settings.remote}/HEAD`], { cwd: config.dir }).catch(() => '')
|
|
47
|
+
base = head.trim().split('/').slice(1).join('/') || 'main'
|
|
48
|
+
}
|
|
49
|
+
const first = String(comment.body || '')
|
|
50
|
+
.split('\n')[0]
|
|
51
|
+
.trim()
|
|
52
|
+
const title = `Shapewall #${comment.id}: ${first.length > 70 ? `${first.slice(0, 67)}...` : first}`
|
|
53
|
+
const body = [
|
|
54
|
+
`Made by the Shapewall starter agent for comment shapewall:${source.board.id}/${comment.id}, in a sandbox.`,
|
|
55
|
+
'',
|
|
56
|
+
'The request came from a board comment (untrusted text). Review the whole diff before merging, especially changes to',
|
|
57
|
+
'scripts, workflows, dependencies and config.',
|
|
58
|
+
].join('\n')
|
|
59
|
+
// From a fork: the head is owner:branch, the owner from the fork remote's GitHub URL.
|
|
60
|
+
let head = branch
|
|
61
|
+
if (fork) {
|
|
62
|
+
const url = (await git(['remote', 'get-url', remote], { cwd: config.dir })).trim()
|
|
63
|
+
const owner = /github\.com[:/]([A-Za-z0-9-]{1,39})\//.exec(url)?.[1]
|
|
64
|
+
if (!owner) throw new Error(`the fork remote ${remote} is not a GitHub repository`)
|
|
65
|
+
head = `${owner}:${branch}`
|
|
66
|
+
}
|
|
67
|
+
const pull = await openPullRequest(config, { head, base, title, body, env })
|
|
68
|
+
await linkPull(config, { boardId: source.board.id, commentId: comment.id, pr: String(pull.number), env }).catch(() => {})
|
|
69
|
+
return pull
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function agentCommand(positional, flags, out, err, { env = process.env } = {}) {
|
|
74
|
+
const action = positional[0]
|
|
75
|
+
if (action === 'sandbox') {
|
|
76
|
+
// Inside the container only.
|
|
77
|
+
const { runSandbox } = await import('./sandbox-main.js')
|
|
78
|
+
const outcome = await runSandbox()
|
|
79
|
+
// stdin stays open (the host may still write): end here, once stdout is flushed.
|
|
80
|
+
await new Promise((resolve) => process.stdout.write('', resolve))
|
|
81
|
+
process.exit(outcome.stop === 'error' ? 1 : 0)
|
|
82
|
+
}
|
|
83
|
+
if (action === 'build-image') {
|
|
84
|
+
// No config needed: the image is the same for every project.
|
|
85
|
+
const image = agentImage({ flag: flags.image || null, env })
|
|
86
|
+
const { runtime, problem } = runtimeFor(flags, env)
|
|
87
|
+
if (!runtime) throw new Error(problem)
|
|
88
|
+
const { buildImage } = await import('./build-image.js')
|
|
89
|
+
const code = await buildImage({
|
|
90
|
+
runtime,
|
|
91
|
+
image,
|
|
92
|
+
env,
|
|
93
|
+
onArgs: (args) => err(`Building ${image} with ${runtime.name}: ${[runtime.bin, ...args].join(' ')}`),
|
|
94
|
+
})
|
|
95
|
+
if (code !== 0) {
|
|
96
|
+
err(`The build failed (exit ${code}).`)
|
|
97
|
+
return 1
|
|
98
|
+
}
|
|
99
|
+
out(`Built ${image}. Check the sandbox with: shapewall agent doctor${flags.image ? ` --image ${image}` : ''}`)
|
|
100
|
+
return 0
|
|
101
|
+
}
|
|
102
|
+
if (!['run', 'watch', 'doctor'].includes(action))
|
|
103
|
+
throw new Error('usage: shapewall agent run --comment ID | agent watch | agent doctor | agent build-image')
|
|
104
|
+
const config = configFor(flags)
|
|
105
|
+
const settings = starterSettings(config, { env, flags })
|
|
106
|
+
const image = agentImage({ flag: flags.image || null, env })
|
|
107
|
+
const { runtime, problem } = runtimeFor(flags, env)
|
|
108
|
+
|
|
109
|
+
if (action === 'doctor') {
|
|
110
|
+
const { formatStarterDoctor, starterDoctor } = await import('./doctor.js')
|
|
111
|
+
const report = await starterDoctor({ config, settings, runtime, runtimeProblem: problem, image, egress: !flags['no-egress'] })
|
|
112
|
+
out(flags.json ? JSON.stringify(report, null, 2) : formatStarterDoctor(report))
|
|
113
|
+
return report.ok ? 0 : 1
|
|
114
|
+
}
|
|
115
|
+
if (!runtime) throw new Error(problem)
|
|
116
|
+
const workDir = typeof flags['work-dir'] === 'string' ? path.resolve(flags['work-dir']) : null
|
|
117
|
+
// Containers, networks and folders that runs which died left behind.
|
|
118
|
+
const { sweepLeftovers } = await import('./sweep.js')
|
|
119
|
+
await sweepLeftovers({ runtime, root: workRoot(config.dir, { flag: workDir, env }), log: err }).catch((error) =>
|
|
120
|
+
err(`sweep: ` + error.message),
|
|
121
|
+
)
|
|
122
|
+
const source = await sourceFor(config, flags)
|
|
123
|
+
const controller = new AbortController()
|
|
124
|
+
const stop = () => controller.abort()
|
|
125
|
+
process.once('SIGINT', stop)
|
|
126
|
+
process.once('SIGTERM', stop)
|
|
127
|
+
const options = {
|
|
128
|
+
source,
|
|
129
|
+
settings,
|
|
130
|
+
runtime,
|
|
131
|
+
image,
|
|
132
|
+
projectDir: config.dir,
|
|
133
|
+
outputDir: config.outputDir,
|
|
134
|
+
workDir,
|
|
135
|
+
env,
|
|
136
|
+
pr: flags.pr && config.file ? pullRequester(config, settings, source, env) : null,
|
|
137
|
+
allowGuardedPush: Boolean(flags['allow-ci-push']),
|
|
138
|
+
signal: controller.signal,
|
|
139
|
+
log: err,
|
|
140
|
+
}
|
|
141
|
+
if (flags.pr && !options.pr) err('--pr needs a local GitHub connect configuration; the branch is still made.')
|
|
142
|
+
try {
|
|
143
|
+
if (action === 'run') {
|
|
144
|
+
const id = Number(flags.comment)
|
|
145
|
+
if (!Number.isSafeInteger(id) || id < 1) throw new Error('usage: shapewall agent run --comment ID')
|
|
146
|
+
const { runStarter } = await import('./supervisor.js')
|
|
147
|
+
const result = await runStarter({
|
|
148
|
+
...options,
|
|
149
|
+
commentId: id,
|
|
150
|
+
claimFrom: flags.retry ? [null, 'needs-human', 'done'] : [null],
|
|
151
|
+
overrideGate: Boolean(flags['trust-comment']),
|
|
152
|
+
})
|
|
153
|
+
out(flags.json ? JSON.stringify(result, null, 2) : result.reply)
|
|
154
|
+
return result.failure ? 1 : 0
|
|
155
|
+
}
|
|
156
|
+
const { watchStarter } = await import('./watch.js')
|
|
157
|
+
await watchStarter({ ...options, once: Boolean(flags.once) })
|
|
158
|
+
return 0
|
|
159
|
+
} finally {
|
|
160
|
+
process.removeListener('SIGINT', stop)
|
|
161
|
+
process.removeListener('SIGTERM', stop)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// The disk cap on the folder the container has (/work). A bind mount has no
|
|
2
|
+
// size limit of its own on podman or docker, so the host measures the folder
|
|
3
|
+
// while the run goes and stops it over the limit (starterAgent.limits.disk,
|
|
4
|
+
// --max-disk; default 2g). /tmp in the container is a tmpfs with its own size.
|
|
5
|
+
|
|
6
|
+
import { execFile } from 'node:child_process'
|
|
7
|
+
import { absoluteProgram } from '../../which.js'
|
|
8
|
+
|
|
9
|
+
export const sizeBytes = (size) => {
|
|
10
|
+
const match = /^([1-9]\d{0,5})([mg])$/.exec(String(size))
|
|
11
|
+
if (!match) throw new Error(`not a size: ${size}`)
|
|
12
|
+
return Number(match[1]) * (match[2] === 'g' ? 1024 ** 3 : 1024 ** 2)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const formatBytes = (bytes) => (bytes >= 1024 ** 3 ? `${(bytes / 1024 ** 3).toFixed(1)} GB` : `${Math.round(bytes / 1024 ** 2)} MB`)
|
|
16
|
+
|
|
17
|
+
// What the folder takes on disk: `du -sk -x` (one file system, no symlink
|
|
18
|
+
// followed), in bytes; null when it cannot tell.
|
|
19
|
+
// `du` from absolute PATH entries only, like host git (worktree.js): a
|
|
20
|
+
// relative entry would let a `du` in the folder shapewall runs in decide what runs.
|
|
21
|
+
export function folderBytes(dir, { timeoutMs = 60000, env = process.env } = {}) {
|
|
22
|
+
const program = absoluteProgram('du', env)
|
|
23
|
+
if (!program) return Promise.resolve(null)
|
|
24
|
+
return new Promise((resolve) => {
|
|
25
|
+
execFile(program, ['-sk', '-x', dir], { timeout: timeoutMs }, (error, stdout) => {
|
|
26
|
+
const kb = Number(String(stdout || '').split(/\s/)[0])
|
|
27
|
+
// du exits non-zero for a file it could not read but still prints a total.
|
|
28
|
+
resolve(Number.isFinite(kb) && String(stdout).trim() ? kb * 1024 : error ? null : 0)
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Measures every `everyMs` until stopped; calls onOver({ bytes, limit }) once.
|
|
34
|
+
export function watchDisk({ dir, limitBytes, everyMs = 5000, measure = folderBytes, onOver }) {
|
|
35
|
+
let stopped = false
|
|
36
|
+
let timer = null
|
|
37
|
+
const tick = async () => {
|
|
38
|
+
if (stopped) return
|
|
39
|
+
const bytes = await measure(dir)
|
|
40
|
+
if (stopped) return
|
|
41
|
+
if (bytes !== null && bytes > limitBytes) {
|
|
42
|
+
stopped = true
|
|
43
|
+
onOver({ bytes, limit: limitBytes })
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
timer = setTimeout(tick, everyMs)
|
|
47
|
+
}
|
|
48
|
+
timer = setTimeout(tick, everyMs)
|
|
49
|
+
return () => {
|
|
50
|
+
stopped = true
|
|
51
|
+
clearTimeout(timer)
|
|
52
|
+
}
|
|
53
|
+
}
|