opencode-any-sandbox 0.0.0-stage → 0.1.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/package.json CHANGED
@@ -1,6 +1,69 @@
1
1
  {
2
2
  "name": "opencode-any-sandbox",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "OpenCode plugin that runs the agent's shell commands inside the sandbox you choose: tartaros or sandbox-runtime (srt)",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "author": "Carlos Tapia",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/carlostapiacl/opencode-any-sandbox.git"
11
+ },
12
+ "keywords": [
13
+ "opencode",
14
+ "opencode-plugin",
15
+ "sandbox",
16
+ "tartaros",
17
+ "sandbox-runtime",
18
+ "seatbelt",
19
+ "landlock",
20
+ "bubblewrap"
21
+ ],
22
+ "oc-plugin": [
23
+ "server",
24
+ "tui"
25
+ ],
26
+ "main": "./dist/index.js",
27
+ "types": "./dist/index.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "default": "./dist/index.js"
32
+ },
33
+ "./tui": "./tui.jsx",
34
+ "./rpc": {
35
+ "types": "./dist/rpc.d.ts",
36
+ "default": "./dist/rpc.js"
37
+ }
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "tui.jsx",
42
+ "README.md",
43
+ "LICENSE"
44
+ ],
45
+ "engines": {
46
+ "node": ">=20"
47
+ },
48
+ "scripts": {
49
+ "build": "tsc -p tsconfig.json",
50
+ "test": "npm run build && node --test --test-concurrency=1 test/*.test.js",
51
+ "prepublishOnly": "npm test"
52
+ },
53
+ "peerDependencies": {
54
+ "@opencode/plugin": ">=2.2.6",
55
+ "solid-js": ">=1.9.0"
56
+ },
57
+ "peerDependenciesMeta": {
58
+ "@opencode/plugin": {
59
+ "optional": true
60
+ },
61
+ "solid-js": {
62
+ "optional": true
63
+ }
64
+ },
65
+ "devDependencies": {
66
+ "typescript": "^5.9.0",
67
+ "@types/node": "^24.0.0"
68
+ }
69
+ }
package/tui.jsx ADDED
@@ -0,0 +1,165 @@
1
+ // The TUI part. OpenCode compiles this file with Solid when it loads it, so it ships as source.
2
+ // It only shows what the server part reports: the sandbox works the same without it.
3
+ import { createSignal } from "solid-js"
4
+ import { STATUS } from "./dist/rpc.js"
5
+
6
+ const ID = "opencode-any-sandbox"
7
+
8
+ function label(status) {
9
+ if (!status) return "sandbox: ..."
10
+ if (status.state === "unknown") return "sandbox: status unavailable"
11
+ if (status.state === "blocked") return `sandbox: ${status.engine} failed, commands blocked`
12
+ if (status.state === "off") return `sandbox: off (${status.engine} failed)`
13
+ const network = status.network === "none" ? "no network" : status.network === "open" ? "network open" : "allowlist"
14
+ return `sandbox: ${status.engine} · ${network}`
15
+ }
16
+
17
+ function details(status, format) {
18
+ if (!status || status.state === "unknown") return "The server part of opencode-any-sandbox did not answer, so the current state is unknown."
19
+ if (status.state !== "on") return `${label(status)}\n\n${status.reason ?? ""}`
20
+ const place = (item) =>
21
+ item === status.project ? "the project" : item === status.scratch ? "$TMPDIR" : format(item)
22
+ // Protected paths are a few names repeated under each writable root: show the names once.
23
+ const names = [...new Set((status.protected ?? []).map((item) => {
24
+ const root = (status.writable ?? []).find((dir) => item.startsWith(dir + "/"))
25
+ return root ? item.slice(root.length + 1) : format(item)
26
+ }))]
27
+ // OpenTUI wraps long lines at dots, so lists go in two columns, one path per cell.
28
+ const columns = (items) => {
29
+ const rows = []
30
+ for (let i = 0; i < items.length; i += 2) rows.push(` ${items[i].padEnd(26)}${items[i + 1] ?? ""}`.trimEnd())
31
+ return rows.join("\n")
32
+ }
33
+ return [
34
+ `Engine ${status.engine}`,
35
+ `Network ${status.network === "none" ? "none" : status.network === "open" ? "open" : `only ${status.network}`}`,
36
+ `Writable ${(status.writable ?? []).map(place).join(", ")}`,
37
+ `Blocked ${status.blocked} so far`,
38
+ "",
39
+ "Read-only inside the writable places:",
40
+ columns(names),
41
+ "",
42
+ "Unreadable:",
43
+ columns((status.denyRead ?? []).map(format)),
44
+ ].join("\n")
45
+ }
46
+
47
+ export default {
48
+ id: ID,
49
+ async setup(ctx) {
50
+ const server = ctx.client.rpc(STATUS)
51
+ const fallback = () => (ctx.location ?? ctx.data?.location?.default?.())?.directory
52
+ // Each session belongs to a Location (a project directory) with its own sandbox: state is kept per directory.
53
+ const directoryOf = (sessionID) => (sessionID && ctx.data.session.get(sessionID)?.location?.directory) || fallback()
54
+ const visible = () => {
55
+ const route = ctx.ui.router.current()
56
+ return route.type === "session" ? directoryOf(route.sessionID) : fallback()
57
+ }
58
+ const states = new Map()
59
+ const entry = (directory) => {
60
+ let item = states.get(directory)
61
+ if (!item) {
62
+ const [get, set] = createSignal(undefined)
63
+ item = { get, set }
64
+ states.set(directory, item)
65
+ queueMicrotask(() => refresh(directory))
66
+ }
67
+ return item
68
+ }
69
+ let disposed = false
70
+
71
+ const refresh = async (directory) => {
72
+ if (!directory || disposed) return false
73
+ const item = entry(directory)
74
+ const before = item.get()?.state
75
+ try {
76
+ const next = await server.status({}, { location: { directory } })
77
+ if (disposed) return false
78
+ item.set(next)
79
+ // Warn when the sandbox stops protecting this directory, not on every refresh.
80
+ if (next.state !== "on" && before !== next.state && directory === visible())
81
+ ctx.ui.toast.show({
82
+ variant: next.state === "blocked" ? "error" : "warning",
83
+ title: "Sandbox",
84
+ message: next.state === "blocked" ? `Shell commands are blocked: ${next.reason}` : `Shell commands run unconfined: ${next.reason}`,
85
+ })
86
+ return true
87
+ } catch {
88
+ // Never keep showing a state that may no longer be true.
89
+ if (!disposed && item.get()) item.set({ state: "unknown" })
90
+ return false
91
+ }
92
+ }
93
+
94
+ let stopEvents = () => {}
95
+ const listen = () => {
96
+ stopEvents()
97
+ stopEvents = server.events.on("blocked", (event) => {
98
+ const directory = event.location?.directory
99
+ void refresh(directory)
100
+ // Only the project on screen: another project's blocks show when you open it, in its counter.
101
+ if (directory && directory !== visible()) return
102
+ ctx.ui.toast.show({ variant: "warning", title: "Sandbox blocked", message: event.data.message, sessionID: event.data.sessionID })
103
+ })
104
+ }
105
+ listen()
106
+ // After the server restarts, subscribe again and ask again.
107
+ let stopConnected = () => {}
108
+ try {
109
+ stopConnected = ctx.data.on("server.connected", () => {
110
+ listen()
111
+ for (const directory of states.keys()) void refresh(directory)
112
+ })
113
+ } catch {}
114
+ const poll = setInterval(() => void refresh(visible()), 30_000)
115
+
116
+ const color = (state) => {
117
+ const text = ctx.theme.text
118
+ if (state === "on") return text.feedback?.success?.base ?? text.muted
119
+ if (state === "blocked") return text.feedback?.error?.base ?? text.base
120
+ if (state === "off") return text.feedback?.warning?.base ?? text.base
121
+ return text.faint ?? text.muted
122
+ }
123
+ const unclaim = ctx.ui.slot({
124
+ append: "prompt.footer.status",
125
+ render: (input) => {
126
+ const status = () => {
127
+ const directory = directoryOf(input.sessionID)
128
+ return directory ? entry(directory).get() : undefined
129
+ }
130
+ return <text fg={color(status()?.state)}>{label(status())}</text>
131
+ },
132
+ })
133
+
134
+ ctx.keymap.layer(() => ({
135
+ mode: "global",
136
+ commands: [
137
+ {
138
+ id: `${ID}.status`,
139
+ title: "Sandbox status",
140
+ description: "What the agent's shell can write, read and reach",
141
+ group: "Sandbox",
142
+ palette: true,
143
+ slash: { name: "sandbox" },
144
+ run: async () => {
145
+ const directory = visible()
146
+ // Show this query's answer, not an older one.
147
+ const fresh = await refresh(directory)
148
+ const status = fresh ? entry(directory).get() : undefined
149
+ await ctx.ui.dialog.alert({ title: "Sandbox", message: details(status, (value) => ctx.ui.format.path(value)) })
150
+ },
151
+ },
152
+ ],
153
+ }))
154
+
155
+ const first = fallback()
156
+ if (first) await refresh(first)
157
+ return () => {
158
+ disposed = true
159
+ clearInterval(poll)
160
+ stopEvents()
161
+ stopConnected()
162
+ unclaim()
163
+ }
164
+ },
165
+ }