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,469 @@
|
|
|
1
|
+
// `shapewall agent connect --remote URL`: makes the coding agent on this
|
|
2
|
+
// machine (Claude Code, Codex, goose, Gemini CLI) an agent of a hosted
|
|
3
|
+
// project. Run it in the project's folder, on the machine that has the code.
|
|
4
|
+
//
|
|
5
|
+
// The first run asks for an approval in the browser (the device login, for
|
|
6
|
+
// an agent): an admin types the code, names the agent, picks its icon, the
|
|
7
|
+
// project and who may use it. The server makes the agent's own token, which
|
|
8
|
+
// is saved here (credentials.js, 0600) and never printed. Then this starts
|
|
9
|
+
// nothing but a link (agent-link.js): members chat with the agent from the
|
|
10
|
+
// board, it reads the project and answers. It does not edit and it runs no
|
|
11
|
+
// command: this machine refuses both, whatever the server sends.
|
|
12
|
+
//
|
|
13
|
+
// The command that runs is this machine's own: a saved agent, or one of the
|
|
14
|
+
// known agents found on PATH. Nothing from the server names it.
|
|
15
|
+
|
|
16
|
+
import fs from 'node:fs'
|
|
17
|
+
import os from 'node:os'
|
|
18
|
+
import path from 'node:path'
|
|
19
|
+
import { KNOWN_AGENTS, detectAgents } from '../agent/acp/detect.js'
|
|
20
|
+
import { acpAgentKind } from '../agent/acp/command.js'
|
|
21
|
+
import { ACP_DEFAULTS, acpHandshake, adapterName, adapterWarning } from '../agent/backends/acp.js'
|
|
22
|
+
import { createBackend } from '../agent/backends/index.js'
|
|
23
|
+
import { agentSettings } from '../agent/config.js'
|
|
24
|
+
import { REMOTE_FIX_PREAMBLE, REMOTE_PREAMBLE } from '../agent/context.js'
|
|
25
|
+
import { authFiles, findConfigFile, loadConfig } from '../config.js'
|
|
26
|
+
import { AGENT_SLUG_RE, forgetAgentCredential, readAgentCredential, saveAgentCredential } from './credentials.js'
|
|
27
|
+
import { fixRoot, sweepFixes } from './agent-fix.js'
|
|
28
|
+
import { fatalLinkError, runAgentLink } from './agent-link.js'
|
|
29
|
+
import {
|
|
30
|
+
POLICY_APPROVALS,
|
|
31
|
+
POLICY_AUDIENCES,
|
|
32
|
+
POLICY_AUTO_KINDS,
|
|
33
|
+
cleanAllowPath,
|
|
34
|
+
cleanBase,
|
|
35
|
+
cleanCommand,
|
|
36
|
+
cleanRemote,
|
|
37
|
+
describeMachinePolicy,
|
|
38
|
+
policyFile,
|
|
39
|
+
readMachinePolicy,
|
|
40
|
+
saveMachinePolicy,
|
|
41
|
+
} from './agent-policy-file.js'
|
|
42
|
+
import { createRemote, remoteOrigin } from './remote.js'
|
|
43
|
+
import { safeOut, terminalSafe } from './terminal.js'
|
|
44
|
+
|
|
45
|
+
const VERSION = JSON.parse(fs.readFileSync(new URL('../../package.json', import.meta.url), 'utf8')).version
|
|
46
|
+
const TOKEN_RE = /^swp_[A-Za-z0-9_-]{43}$/
|
|
47
|
+
|
|
48
|
+
// The project folder: where the config is, else where the command runs.
|
|
49
|
+
function projectFor(flags, cwd) {
|
|
50
|
+
const file = findConfigFile(flags.config, cwd)
|
|
51
|
+
if (fs.existsSync(file)) return loadConfig(flags.config, { cwd })
|
|
52
|
+
return { file: null, dir: cwd, outputDir: path.join(cwd, '.shapewall'), agent: null, boards: [] }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Which local agent runs: --agent names a known one (claude, codex, goose,
|
|
56
|
+
// gemini) or a saved agent's id; without it, this project's saved agent,
|
|
57
|
+
// else the first known agent found on PATH. Always this machine's command.
|
|
58
|
+
// The agents `agent connect` offers: the ones whose own controls were
|
|
59
|
+
// proven, with the real adapter, to keep a read-only session from editing,
|
|
60
|
+
// running commands and reading outside the project (docs/agent-hardening.md
|
|
61
|
+
// says how). An agent that could not be proven that way is not offered:
|
|
62
|
+
// refusing what it asks for is not enough when it does not ask.
|
|
63
|
+
export const CONNECT_PROVEN = ['claude']
|
|
64
|
+
const NOT_PROVEN = {
|
|
65
|
+
codex:
|
|
66
|
+
'Codex cannot be connected in this release. Started read-only it changes no files, but its sandbox still runs shell commands and reads files outside the project without asking, and what it reads can leave this machine in an answer.',
|
|
67
|
+
goose: 'goose cannot be connected in this release: its chat-only mode could not be verified to keep every tool off.',
|
|
68
|
+
gemini: 'Gemini CLI cannot be connected in this release: its read-only behaviour has not been verified.',
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function connectSettings(config, flags, env = process.env) {
|
|
72
|
+
const wanted = typeof flags.agent === 'string' ? flags.agent.trim() : ''
|
|
73
|
+
const known = KNOWN_AGENTS.find((agent) => agent.id === wanted)
|
|
74
|
+
const found = detectAgents(env)
|
|
75
|
+
const fromKnown = (agent) => {
|
|
76
|
+
if (!CONNECT_PROVEN.includes(agent.id))
|
|
77
|
+
throw new Error(`${NOT_PROVEN[agent.id]} Connect Claude Code (--agent claude), or use "shapewall watch --remote" for this agent.`)
|
|
78
|
+
const here = found.find((item) => item.id === agent.id)
|
|
79
|
+
if (!here)
|
|
80
|
+
throw new Error(
|
|
81
|
+
`${agent.label} is not installed on this machine (no "${agent.adapter}" on PATH).${agent.install ? ` Install it: ${agent.install}` : ''}`,
|
|
82
|
+
)
|
|
83
|
+
if (here.state !== 'ready')
|
|
84
|
+
throw new Error(
|
|
85
|
+
`${agent.label} is here, but its ACP adapter is not.${here.install ? ` Install it: ${here.install}` : ''} (docs/agents.md)`,
|
|
86
|
+
)
|
|
87
|
+
return agentSettings(config, env, { acp: agent.command })
|
|
88
|
+
}
|
|
89
|
+
let settings
|
|
90
|
+
if (known) settings = fromKnown(known)
|
|
91
|
+
else if (wanted) settings = agentSettings(config, env, { profileId: wanted })
|
|
92
|
+
else {
|
|
93
|
+
settings = agentSettings(config, env)
|
|
94
|
+
if (!(settings.configured && settings.backend === 'acp')) {
|
|
95
|
+
// Only an agent that is offered: another one found first is passed over.
|
|
96
|
+
const first = found.find((item) => item.state === 'ready' && CONNECT_PROVEN.includes(item.id))
|
|
97
|
+
if (!first)
|
|
98
|
+
throw new Error(
|
|
99
|
+
'No agent that can be connected was found on this machine. Install Claude Code with its ACP adapter (npm install -g @agentclientprotocol/claude-agent-acp, docs/agents.md). Codex, goose and Gemini CLI are not offered by agent connect in this release.',
|
|
100
|
+
)
|
|
101
|
+
settings = fromKnown(KNOWN_AGENTS.find((agent) => agent.id === first.id))
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (settings.backend !== 'acp')
|
|
105
|
+
throw new Error('agent connect runs a coding agent on this machine (Claude Code, Codex, goose, Gemini CLI), not a model endpoint.')
|
|
106
|
+
if (!settings.configured) throw new Error(settings.problem || 'That agent is not set up on this machine.')
|
|
107
|
+
// A saved agent or this project's own: the same rule, by what its command starts.
|
|
108
|
+
const kind = acpAgentKind(settings.acp.argv)
|
|
109
|
+
if (!CONNECT_PROVEN.includes(kind))
|
|
110
|
+
throw new Error(
|
|
111
|
+
`${NOT_PROVEN[kind] || `${settings.label} cannot be connected in this release: Shapewall cannot make an agent it does not know read-only by the agent's own controls.`} Connect Claude Code (--agent claude).`,
|
|
112
|
+
)
|
|
113
|
+
return settings
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// What stands in the way of a read-only session, in the agent's own
|
|
117
|
+
// settings. A connected Claude Code session is started without the person's
|
|
118
|
+
// and the project's settings files (their allow rules, hooks and MCP servers
|
|
119
|
+
// do not apply) and with only its reading tools. What Shapewall cannot leave
|
|
120
|
+
// out is the machine's managed settings: when those allow acting tools or
|
|
121
|
+
// run hooks, the agent is not started. Returns { refuse: [...], notes: [...] }.
|
|
122
|
+
const ACTING_RULE_RE = /^(Bash|Edit|Write|MultiEdit|NotebookEdit|WebFetch|WebSearch|Task|Agent|mcp__|Read\(\s*(\/|~))/
|
|
123
|
+
export function profileProblems(kind, root, env = process.env, { managedFile = '/etc/claude-code/managed-settings.json' } = {}) {
|
|
124
|
+
const out = { refuse: [], notes: [] }
|
|
125
|
+
if (kind !== 'claude') return out
|
|
126
|
+
const read = (file) => {
|
|
127
|
+
try {
|
|
128
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
129
|
+
} catch {
|
|
130
|
+
return null
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const acting = (data) =>
|
|
134
|
+
Array.isArray(data?.permissions?.allow) ? data.permissions.allow.filter((rule) => ACTING_RULE_RE.test(String(rule))) : []
|
|
135
|
+
const hooks = (data) => (data?.hooks && typeof data.hooks === 'object' ? Object.keys(data.hooks).length : 0)
|
|
136
|
+
const managed = read(managedFile)
|
|
137
|
+
if (managed) {
|
|
138
|
+
if (acting(managed).length)
|
|
139
|
+
out.refuse.push(
|
|
140
|
+
`${managedFile} lets Claude Code use ${acting(managed).length} acting tool rule${acting(managed).length === 1 ? '' : 's'} without asking (permissions.allow), and a session cannot leave managed settings out.`,
|
|
141
|
+
)
|
|
142
|
+
if (hooks(managed))
|
|
143
|
+
out.refuse.push(`${managedFile} runs hooks (commands) around every session, and a session cannot leave managed settings out.`)
|
|
144
|
+
if (/bypass|accept|dontask|auto/i.test(String(managed.permissions?.defaultMode || '')))
|
|
145
|
+
out.notes.push(
|
|
146
|
+
`${managedFile} sets the default mode "${managed.permissions.defaultMode}": this session is switched to one that asks, or it does not start.`,
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
const home = env.HOME || os.homedir()
|
|
150
|
+
const files = [
|
|
151
|
+
path.join(env.CLAUDE_CONFIG_DIR || path.join(home, '.claude'), 'settings.json'),
|
|
152
|
+
path.join(root, '.claude', 'settings.json'),
|
|
153
|
+
path.join(root, '.claude', 'settings.local.json'),
|
|
154
|
+
]
|
|
155
|
+
const found = files.filter((file) => {
|
|
156
|
+
const data = read(file)
|
|
157
|
+
return data && (acting(data).length || hooks(data) || data.permissions?.defaultMode)
|
|
158
|
+
})
|
|
159
|
+
if (found.length)
|
|
160
|
+
out.notes.push(
|
|
161
|
+
`Left out of this session: the allow rules, hooks and default mode in ${found.map((file) => (file.startsWith(root) ? path.relative(root, file) : file.replace(home, '~'))).join(', ')}.`,
|
|
162
|
+
)
|
|
163
|
+
return out
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// What the flags change in this machine's policy for the connection:
|
|
167
|
+
// { changes } to save, or throws with how to say it. Lists grow one entry a
|
|
168
|
+
// run ("none" empties one), so a service file carries no policy: the file does.
|
|
169
|
+
// Agents whose listed commands were proven, with the real adapter, to be
|
|
170
|
+
// the only commands they run. None yet: Claude Code, started with its shell
|
|
171
|
+
// tool, runs commands it takes to be read-only without asking (acp.js).
|
|
172
|
+
export const COMMANDS_PROVEN = []
|
|
173
|
+
const NO_COMMANDS =
|
|
174
|
+
'Commands cannot be allowed for Claude Code in this release: started with its shell tool it runs commands it takes to be read-only (ls, cat) without asking, and those can read outside the project. Edits work without it.'
|
|
175
|
+
|
|
176
|
+
export function policyChanges(flags, saved, kind = null) {
|
|
177
|
+
const changes = {}
|
|
178
|
+
const commandsOk = kind === null || COMMANDS_PROVEN.includes(kind)
|
|
179
|
+
if (flags.audience !== undefined) {
|
|
180
|
+
if (!POLICY_AUDIENCES.includes(flags.audience))
|
|
181
|
+
throw new Error(
|
|
182
|
+
'--audience: "owner" (ask the server to keep it to the person who connects it, and refuse other jobs here) or "server" (as set on the server)',
|
|
183
|
+
)
|
|
184
|
+
changes.audience = flags.audience
|
|
185
|
+
}
|
|
186
|
+
if (flags.approvals !== undefined) {
|
|
187
|
+
if (!POLICY_APPROVALS.includes(flags.approvals))
|
|
188
|
+
throw new Error(
|
|
189
|
+
'--approvals: "none" (read and answer only, the default), "owner" (the person who connected it may allow edits from the board) or "admins" (whoever the server lets answer, admins by default)',
|
|
190
|
+
)
|
|
191
|
+
changes.approvals = flags.approvals
|
|
192
|
+
}
|
|
193
|
+
const approvals = changes.approvals ?? saved.approvals
|
|
194
|
+
const needsApprovals = (flag) => {
|
|
195
|
+
if (approvals === 'none') throw new Error(`--${flag} needs --approvals owner or --approvals admins: this agent reads and answers only.`)
|
|
196
|
+
}
|
|
197
|
+
if (flags['allow-command'] !== undefined) {
|
|
198
|
+
needsApprovals('allow-command')
|
|
199
|
+
if (flags['allow-command'] === 'none') changes.commands = []
|
|
200
|
+
else if (!commandsOk) throw new Error(`--allow-command: ${NO_COMMANDS}`)
|
|
201
|
+
else {
|
|
202
|
+
const command = cleanCommand(flags['allow-command'])
|
|
203
|
+
if (!command)
|
|
204
|
+
throw new Error(
|
|
205
|
+
'--allow-command: one exact command on one line, at most 200 characters, such as "npm test" ("none" removes them all)',
|
|
206
|
+
)
|
|
207
|
+
changes.commands = [...new Set([...saved.commands, command])]
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (flags['allow-path'] !== undefined) {
|
|
211
|
+
needsApprovals('allow-path')
|
|
212
|
+
if (flags['allow-path'] === 'none') changes.allowPaths = []
|
|
213
|
+
else {
|
|
214
|
+
const file = cleanAllowPath(flags['allow-path'])
|
|
215
|
+
if (!file) throw new Error('--allow-path: a file path inside the project, such as package.json ("none" removes them all)')
|
|
216
|
+
changes.allowPaths = [...new Set([...saved.allowPaths, file])]
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (flags.auto !== undefined) {
|
|
220
|
+
const kinds = flags.auto === 'none' ? [] : String(flags.auto).split(',')
|
|
221
|
+
if (kinds.some((kind) => !POLICY_AUTO_KINDS.includes(kind)))
|
|
222
|
+
throw new Error('--auto: "none" (every request waits for a person, the default), "edits", "commands" or "edits,commands"')
|
|
223
|
+
if (kinds.length) needsApprovals('auto')
|
|
224
|
+
if (kinds.includes('commands') && !(changes.commands ?? saved.commands).length)
|
|
225
|
+
throw new Error('--auto commands needs at least one --allow-command: there is no command to allow.')
|
|
226
|
+
changes.auto = kinds
|
|
227
|
+
}
|
|
228
|
+
if (flags.base !== undefined) {
|
|
229
|
+
if (flags.base !== 'head' && !cleanBase(flags.base))
|
|
230
|
+
throw new Error('--base: a branch name, such as main ("head": the commit the checkout is on)')
|
|
231
|
+
changes.base = flags.base === 'head' ? null : flags.base
|
|
232
|
+
}
|
|
233
|
+
if (flags['git-remote'] !== undefined) {
|
|
234
|
+
if (flags['git-remote'] !== 'none' && !cleanRemote(flags['git-remote']))
|
|
235
|
+
throw new Error('--git-remote: a remote name, such as origin ("none": never push)')
|
|
236
|
+
changes.remote = flags['git-remote'] === 'none' ? null : flags['git-remote']
|
|
237
|
+
}
|
|
238
|
+
return changes
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// What this machine's rules come to, for the approval page: who may allow
|
|
242
|
+
// edits from the board (or "none"), whether edits may be allowed without
|
|
243
|
+
// asking, and the branch a fix starts from. Commands are left out: none is
|
|
244
|
+
// in effect for an agent they were not proven with.
|
|
245
|
+
export function askedOf(policy, kind = null) {
|
|
246
|
+
const approvals = POLICY_APPROVALS.includes(policy?.approvals) ? policy.approvals : 'none'
|
|
247
|
+
const auto = (Array.isArray(policy?.auto) ? policy.auto : []).filter((item) => item !== 'commands' || COMMANDS_PROVEN.includes(kind))
|
|
248
|
+
return { approvals, auto: approvals !== 'none' && auto.includes('edits'), base: approvals !== 'none' ? policy?.base || null : null }
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export async function agentConnect(
|
|
252
|
+
flags,
|
|
253
|
+
print,
|
|
254
|
+
err,
|
|
255
|
+
{ env = process.env, cwd = process.cwd(), fetchImpl = fetch, signal = null, sleep } = {},
|
|
256
|
+
) {
|
|
257
|
+
const out = safeOut(print)
|
|
258
|
+
const log = safeOut(err)
|
|
259
|
+
const origin = remoteOrigin(flags, env)
|
|
260
|
+
const slug = flags.as === undefined ? 'default' : String(flags.as)
|
|
261
|
+
if (!AGENT_SLUG_RE.test(slug)) throw new Error('--as: a short name for this connection (lower-case letters, digits and dashes)')
|
|
262
|
+
const concurrency = flags.concurrency === undefined ? 2 : Number(flags.concurrency)
|
|
263
|
+
if (!Number.isInteger(concurrency) || concurrency < 1 || concurrency > 4) throw new Error('--concurrency must be 1 to 4')
|
|
264
|
+
const config = projectFor(flags, cwd)
|
|
265
|
+
const settings = connectSettings(config, flags, env)
|
|
266
|
+
const kind = acpAgentKind(settings.acp.argv) || 'custom'
|
|
267
|
+
const label = terminalSafe(settings.label)
|
|
268
|
+
// Checked before anything is asked of anyone: a wrong flag changes nothing.
|
|
269
|
+
// A value in the file that is not understood reads as the narrowest setting, and is said.
|
|
270
|
+
const policyNow = readMachinePolicy(origin, slug, env, {
|
|
271
|
+
warn: (text) => log(`Not understood, so read as the narrowest setting: ${text}`),
|
|
272
|
+
})
|
|
273
|
+
const changes = policyChanges(flags, policyNow, kind)
|
|
274
|
+
|
|
275
|
+
// ---- The agent's token: its own, never the saved login's ----
|
|
276
|
+
let token = env.SHAPEWALL_TOKEN || ''
|
|
277
|
+
let source = token ? 'env' : null
|
|
278
|
+
if (typeof flags['token-file'] === 'string') {
|
|
279
|
+
token = fs.readFileSync(flags['token-file'], 'utf8').trim()
|
|
280
|
+
source = 'file'
|
|
281
|
+
}
|
|
282
|
+
if (!token) {
|
|
283
|
+
const saved = readAgentCredential(origin, slug, env)
|
|
284
|
+
if (saved) {
|
|
285
|
+
token = saved.token
|
|
286
|
+
source = 'agent'
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (!token) {
|
|
290
|
+
const { login } = await import('./login.js')
|
|
291
|
+
const suggested = typeof flags.name === 'string' && flags.name.trim() ? flags.name.trim() : settings.label
|
|
292
|
+
out(`Connecting ${label} in ${config.dir} to ${origin}.`)
|
|
293
|
+
out('An admin of the workspace approves it in the browser: they name the agent, pick its project and who may use it.')
|
|
294
|
+
token = await login(flags, print, {
|
|
295
|
+
env,
|
|
296
|
+
fetchImpl,
|
|
297
|
+
signal,
|
|
298
|
+
...(sleep ? { sleep } : {}),
|
|
299
|
+
purpose: 'agent',
|
|
300
|
+
// What this machine will take from the board, as it will be once this run's flags are saved:
|
|
301
|
+
// the approval page shows it, as this terminal's report.
|
|
302
|
+
agent: { kind, name: suggested, asks: askedOf({ ...policyNow, ...changes }, kind) },
|
|
303
|
+
command: 'shapewall agent connect',
|
|
304
|
+
onApproved: (result) => {
|
|
305
|
+
const file = saveAgentCredential(
|
|
306
|
+
origin,
|
|
307
|
+
slug,
|
|
308
|
+
{
|
|
309
|
+
token: result.token,
|
|
310
|
+
tokenName: result.tokenName || null,
|
|
311
|
+
workspace: result.workspace || null,
|
|
312
|
+
project: result.project || null,
|
|
313
|
+
agent: result.agent || null,
|
|
314
|
+
scopes: Array.isArray(result.scopes) ? result.scopes : [],
|
|
315
|
+
expiresAt: result.expiresAt || null,
|
|
316
|
+
savedAt: new Date().toISOString(),
|
|
317
|
+
},
|
|
318
|
+
env,
|
|
319
|
+
)
|
|
320
|
+
out(
|
|
321
|
+
`Approved as "${result.agent?.name || result.tokenName}" for ${result.workspace?.name || 'the workspace'} / ${result.project?.name || 'the project'}.`,
|
|
322
|
+
)
|
|
323
|
+
out(`Its token is saved in ${file} (only you can read it). Run the same command to connect again.`)
|
|
324
|
+
return result.token
|
|
325
|
+
},
|
|
326
|
+
})
|
|
327
|
+
source = 'agent'
|
|
328
|
+
}
|
|
329
|
+
if (!TOKEN_RE.test(token))
|
|
330
|
+
throw new Error('That is not a project token (swp_...). Run "shapewall agent connect" without it to get one approved.')
|
|
331
|
+
|
|
332
|
+
// ---- This machine's own rules ----
|
|
333
|
+
const saved = Object.keys(changes).length ? saveMachinePolicy(origin, slug, changes, env) : policyNow
|
|
334
|
+
// Commands in the file (written by hand, or for another agent) are not in effect for an agent they were not proven with.
|
|
335
|
+
const policy = COMMANDS_PROVEN.includes(kind) ? saved : { ...saved, commands: [], auto: saved.auto.filter((item) => item !== 'commands') }
|
|
336
|
+
if (saved.commands.length && !policy.commands.length) log(`Not in effect: the commands in ${policyFile(env)}. ${NO_COMMANDS}`)
|
|
337
|
+
const edits = policy.approvals !== 'none'
|
|
338
|
+
if (edits) {
|
|
339
|
+
// Said now rather than at the first fix: where fixes are made must be outside the project.
|
|
340
|
+
try {
|
|
341
|
+
fixRoot(config.dir, env)
|
|
342
|
+
} catch (error) {
|
|
343
|
+
throw new Error(error.message.replace(/^On the agent's machine, t/, 'T').replace(/ there /, ' '), { cause: error })
|
|
344
|
+
}
|
|
345
|
+
// What an earlier run left behind (it was killed mid-fix): removed, and its branch names are free again.
|
|
346
|
+
const swept = sweepFixes(config.dir, env)
|
|
347
|
+
if (swept.length) log(`Removed ${swept.length} unfinished fix cop${swept.length === 1 ? 'y' : 'ies'} an earlier run left behind.`)
|
|
348
|
+
}
|
|
349
|
+
const profile = profileProblems(kind, config.dir, env)
|
|
350
|
+
if (profile.refuse.length)
|
|
351
|
+
throw new Error(`${label} is not started read-only on this machine. ${profile.refuse.join(' ')} See docs/agent-hardening.md.`)
|
|
352
|
+
for (const line of profile.notes) log(line)
|
|
353
|
+
|
|
354
|
+
// ---- Can a session be opened at all? ----
|
|
355
|
+
// Asked of the agent now, the way a question from the board would start it
|
|
356
|
+
// (no prompt is sent, so nothing is spent): an adapter that cannot open the
|
|
357
|
+
// session, or that this release was not tested with, is said here and not
|
|
358
|
+
// at somebody's first question.
|
|
359
|
+
const opens = async (what, how) => {
|
|
360
|
+
const result = await acpHandshake({ ...settings, ...how }, { env, root: config.dir, timeoutMs: ACP_DEFAULTS.startMs })
|
|
361
|
+
if (result.ok) return result
|
|
362
|
+
const said = adapterName(result.agentInfo)
|
|
363
|
+
if (result.stderr) log(`${label} last output: ${terminalSafe(result.stderr)}`)
|
|
364
|
+
throw new Error(
|
|
365
|
+
`${label} could not open ${what} on this machine, so it is not connected${said && !result.message.includes(said) ? ` (adapter: ${terminalSafe(said)})` : ''}. ${terminalSafe(result.message)}`,
|
|
366
|
+
)
|
|
367
|
+
}
|
|
368
|
+
const opened = await opens('a read-only session', { readOnly: true })
|
|
369
|
+
if (edits)
|
|
370
|
+
await opens('a session for edits', { readOnly: false, remoteEdit: { commands: policy.commands, allowPaths: policy.allowPaths } })
|
|
371
|
+
const adapter = terminalSafe(adapterName(opened.agentInfo) || 'an adapter that does not say its version')
|
|
372
|
+
const untested = adapterWarning(kind, opened.agentInfo, label)
|
|
373
|
+
if (untested) log(`Warning: ${terminalSafe(untested)}`)
|
|
374
|
+
else log(`${label} answers through ${adapter}, a version this release was tested with.`)
|
|
375
|
+
|
|
376
|
+
const backend = createBackend(
|
|
377
|
+
// Read and answer only (src/agent/backends/acp.js readOnly), with the
|
|
378
|
+
// connected agent's own first words. Images are not sent to it yet.
|
|
379
|
+
{ ...settings, readOnly: true, preamble: REMOTE_PREAMBLE, vision: false },
|
|
380
|
+
{
|
|
381
|
+
env,
|
|
382
|
+
root: config.dir,
|
|
383
|
+
protect: [config.outputDir],
|
|
384
|
+
sensitivePaths: () => authFiles(config),
|
|
385
|
+
limits: { processesMax: Math.max(concurrency, 2) },
|
|
386
|
+
log: (line) => log(line),
|
|
387
|
+
},
|
|
388
|
+
)
|
|
389
|
+
if (!backend) throw new Error('That agent could not be set up on this machine.')
|
|
390
|
+
// The same agent, started for fixes: it may ask to edit files of the
|
|
391
|
+
// project (and to run the listed commands), each request checked here and
|
|
392
|
+
// answered on the board (acp.js remoteEdit). Only when this machine said so.
|
|
393
|
+
const editBackend = edits
|
|
394
|
+
? createBackend(
|
|
395
|
+
{
|
|
396
|
+
...settings,
|
|
397
|
+
readOnly: false,
|
|
398
|
+
remoteEdit: { commands: policy.commands, allowPaths: policy.allowPaths },
|
|
399
|
+
preamble: REMOTE_FIX_PREAMBLE,
|
|
400
|
+
vision: false,
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
env,
|
|
404
|
+
// Each fix gives its own folder (a worktree) as the turn's workdir; this root is never written.
|
|
405
|
+
root: config.dir,
|
|
406
|
+
protect: [config.outputDir],
|
|
407
|
+
sensitivePaths: () => authFiles(config),
|
|
408
|
+
limits: { processesMax: Math.max(concurrency, 2) },
|
|
409
|
+
log: (line) => log(line),
|
|
410
|
+
},
|
|
411
|
+
)
|
|
412
|
+
: null
|
|
413
|
+
|
|
414
|
+
const controller = new AbortController()
|
|
415
|
+
const stop = () => controller.abort()
|
|
416
|
+
signal?.addEventListener('abort', stop, { once: true })
|
|
417
|
+
process.once('SIGINT', stop)
|
|
418
|
+
process.once('SIGTERM', stop)
|
|
419
|
+
const remote = createRemote({ origin, token, source, fetchImpl })
|
|
420
|
+
let ended = null
|
|
421
|
+
try {
|
|
422
|
+
ended = await runAgentLink({
|
|
423
|
+
origin,
|
|
424
|
+
token,
|
|
425
|
+
source,
|
|
426
|
+
backend,
|
|
427
|
+
editBackend,
|
|
428
|
+
fix: edits ? { config, env, fetchImpl } : null,
|
|
429
|
+
hello: { kind, machine: os.hostname().split('.')[0] || 'this computer', version: VERSION, maxTurns: concurrency },
|
|
430
|
+
policy,
|
|
431
|
+
remote,
|
|
432
|
+
takeover: Boolean(flags.takeover),
|
|
433
|
+
signal: controller.signal,
|
|
434
|
+
fetchImpl,
|
|
435
|
+
log: (line) => log(line),
|
|
436
|
+
onReady: (said) => {
|
|
437
|
+
const agent = said?.agent || {}
|
|
438
|
+
log(
|
|
439
|
+
`Connected: ${terminalSafe(agent.name || label)} (${label}) is online for ${terminalSafe(agent.workspace?.name || 'the workspace')} / ${terminalSafe(agent.project?.name || 'the project')}.`,
|
|
440
|
+
)
|
|
441
|
+
log(`Works in ${config.dir}. Used by: ${terminalSafe(agent.audience || 'the person who connected it')}.`)
|
|
442
|
+
for (const line of describeMachinePolicy(policy)) log(terminalSafe(line))
|
|
443
|
+
if (policy.audience === 'owner')
|
|
444
|
+
log(`This machine refuses questions the server says are not from the person who connected it (${policyFile(env)}).`)
|
|
445
|
+
log('Members chat with it from the Agents panel of the board. Ctrl+C stops it.')
|
|
446
|
+
},
|
|
447
|
+
})
|
|
448
|
+
} catch (error) {
|
|
449
|
+
if (!fatalLinkError(error)) throw error
|
|
450
|
+
// Another process holds this agent's link: the server says which machine, since when.
|
|
451
|
+
if (error.code === 'already_connected') throw new Error(terminalSafe(error.message), { cause: error })
|
|
452
|
+
// A saved token the server refuses is of no more use: the next run asks for a new approval.
|
|
453
|
+
const forgot = error.status === 401 && source === 'agent' && forgetAgentCredential(origin, slug, env)
|
|
454
|
+
throw new Error(
|
|
455
|
+
error.status === 401
|
|
456
|
+
? `This agent's token was revoked or has expired, or the person who connected it is no longer an admin there.${forgot ? ' The saved token was removed: run the same command again for a new approval.' : ''}`
|
|
457
|
+
: `This token is not a connected agent's (${error.message}). Run "shapewall agent connect --remote ${origin}" without SHAPEWALL_TOKEN or --token-file to get one approved.`,
|
|
458
|
+
{ cause: error },
|
|
459
|
+
)
|
|
460
|
+
} finally {
|
|
461
|
+
process.removeListener('SIGINT', stop)
|
|
462
|
+
process.removeListener('SIGTERM', stop)
|
|
463
|
+
signal?.removeEventListener('abort', stop)
|
|
464
|
+
backend.close?.()
|
|
465
|
+
editBackend?.close?.()
|
|
466
|
+
}
|
|
467
|
+
log(ended?.takenOver ? 'Another "shapewall agent connect" took over this agent (--takeover). This one stopped.' : 'Disconnected.')
|
|
468
|
+
return 0
|
|
469
|
+
}
|