@culture.dev/agents 0.0.0-stage → 0.15.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.
Files changed (290) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +59 -2
  3. package/dist/contract-schemas/agent.json +128 -0
  4. package/dist/contract-schemas/allowlist.json +25 -0
  5. package/dist/contract-schemas/api.json +501 -0
  6. package/dist/contract-schemas/capabilities.json +208 -0
  7. package/dist/contract-schemas/common.json +123 -0
  8. package/dist/contract-schemas/control-action.json +279 -0
  9. package/dist/contract-schemas/error.json +269 -0
  10. package/dist/contract-schemas/event.json +316 -0
  11. package/dist/contract-schemas/run.json +336 -0
  12. package/dist/contract-schemas/session.json +77 -0
  13. package/dist/contract-schemas/version.json +22 -0
  14. package/dist/src/contract/api.d.ts +41 -0
  15. package/dist/src/contract/api.js +35 -0
  16. package/dist/src/contract/api.js.map +1 -0
  17. package/dist/src/contract/errors.d.ts +21 -0
  18. package/dist/src/contract/errors.js +25 -0
  19. package/dist/src/contract/errors.js.map +1 -0
  20. package/dist/src/contract/generated/agent.d.ts +31 -0
  21. package/dist/src/contract/generated/agent.js +3 -0
  22. package/dist/src/contract/generated/agent.js.map +1 -0
  23. package/dist/src/contract/generated/allowlist.d.ts +10 -0
  24. package/dist/src/contract/generated/allowlist.js +3 -0
  25. package/dist/src/contract/generated/allowlist.js.map +1 -0
  26. package/dist/src/contract/generated/capabilities.d.ts +46 -0
  27. package/dist/src/contract/generated/capabilities.js +3 -0
  28. package/dist/src/contract/generated/capabilities.js.map +1 -0
  29. package/dist/src/contract/generated/common.d.ts +40 -0
  30. package/dist/src/contract/generated/common.js +3 -0
  31. package/dist/src/contract/generated/common.js.map +1 -0
  32. package/dist/src/contract/generated/control-action.d.ts +73 -0
  33. package/dist/src/contract/generated/control-action.js +3 -0
  34. package/dist/src/contract/generated/control-action.js.map +1 -0
  35. package/dist/src/contract/generated/error.d.ts +25 -0
  36. package/dist/src/contract/generated/error.js +3 -0
  37. package/dist/src/contract/generated/error.js.map +1 -0
  38. package/dist/src/contract/generated/event.d.ts +80 -0
  39. package/dist/src/contract/generated/event.js +3 -0
  40. package/dist/src/contract/generated/event.js.map +1 -0
  41. package/dist/src/contract/generated/index.d.ts +81 -0
  42. package/dist/src/contract/generated/index.js +3 -0
  43. package/dist/src/contract/generated/index.js.map +1 -0
  44. package/dist/src/contract/generated/run.d.ts +63 -0
  45. package/dist/src/contract/generated/run.js +3 -0
  46. package/dist/src/contract/generated/run.js.map +1 -0
  47. package/dist/src/contract/generated/session.d.ts +12 -0
  48. package/dist/src/contract/generated/session.js +3 -0
  49. package/dist/src/contract/generated/session.js.map +1 -0
  50. package/dist/src/contract/generated/version.d.ts +9 -0
  51. package/dist/src/contract/generated/version.js +3 -0
  52. package/dist/src/contract/generated/version.js.map +1 -0
  53. package/dist/src/contract/ids.d.ts +36 -0
  54. package/dist/src/contract/ids.js +85 -0
  55. package/dist/src/contract/ids.js.map +1 -0
  56. package/dist/src/contract/index.d.ts +17 -0
  57. package/dist/src/contract/index.js +13 -0
  58. package/dist/src/contract/index.js.map +1 -0
  59. package/dist/src/contract/run-state.d.ts +13 -0
  60. package/dist/src/contract/run-state.js +24 -0
  61. package/dist/src/contract/run-state.js.map +1 -0
  62. package/dist/src/contract/schemas.d.ts +18 -0
  63. package/dist/src/contract/schemas.js +37 -0
  64. package/dist/src/contract/schemas.js.map +1 -0
  65. package/dist/src/contract/validate.d.ts +17 -0
  66. package/dist/src/contract/validate.js +68 -0
  67. package/dist/src/contract/validate.js.map +1 -0
  68. package/dist/src/daemon/allowlist.d.ts +23 -0
  69. package/dist/src/daemon/allowlist.js +57 -0
  70. package/dist/src/daemon/allowlist.js.map +1 -0
  71. package/dist/src/daemon/daemon.d.ts +31 -0
  72. package/dist/src/daemon/daemon.js +141 -0
  73. package/dist/src/daemon/daemon.js.map +1 -0
  74. package/dist/src/daemon/errors.d.ts +14 -0
  75. package/dist/src/daemon/errors.js +26 -0
  76. package/dist/src/daemon/errors.js.map +1 -0
  77. package/dist/src/daemon/harness-catalog.d.ts +11 -0
  78. package/dist/src/daemon/harness-catalog.js +21 -0
  79. package/dist/src/daemon/harness-catalog.js.map +1 -0
  80. package/dist/src/daemon/http.d.ts +15 -0
  81. package/dist/src/daemon/http.js +76 -0
  82. package/dist/src/daemon/http.js.map +1 -0
  83. package/dist/src/daemon/lock.d.ts +47 -0
  84. package/dist/src/daemon/lock.js +167 -0
  85. package/dist/src/daemon/lock.js.map +1 -0
  86. package/dist/src/daemon/main.d.ts +32 -0
  87. package/dist/src/daemon/main.js +204 -0
  88. package/dist/src/daemon/main.js.map +1 -0
  89. package/dist/src/daemon/paths.d.ts +29 -0
  90. package/dist/src/daemon/paths.js +26 -0
  91. package/dist/src/daemon/paths.js.map +1 -0
  92. package/dist/src/daemon/ports.d.ts +58 -0
  93. package/dist/src/daemon/ports.js +2 -0
  94. package/dist/src/daemon/ports.js.map +1 -0
  95. package/dist/src/daemon/routes.d.ts +15 -0
  96. package/dist/src/daemon/routes.js +259 -0
  97. package/dist/src/daemon/routes.js.map +1 -0
  98. package/dist/src/daemon/sse.d.ts +8 -0
  99. package/dist/src/daemon/sse.js +56 -0
  100. package/dist/src/daemon/sse.js.map +1 -0
  101. package/dist/src/harness/availability.d.ts +9 -0
  102. package/dist/src/harness/availability.js +52 -0
  103. package/dist/src/harness/availability.js.map +1 -0
  104. package/dist/src/harness/claude-code/agent.d.ts +4 -0
  105. package/dist/src/harness/claude-code/agent.js +33 -0
  106. package/dist/src/harness/claude-code/agent.js.map +1 -0
  107. package/dist/src/harness/claude-code/backend.d.ts +48 -0
  108. package/dist/src/harness/claude-code/backend.js +83 -0
  109. package/dist/src/harness/claude-code/backend.js.map +1 -0
  110. package/dist/src/harness/claude-code/capabilities.d.ts +7 -0
  111. package/dist/src/harness/claude-code/capabilities.js +11 -0
  112. package/dist/src/harness/claude-code/capabilities.js.map +1 -0
  113. package/dist/src/harness/claude-code/index.d.ts +15 -0
  114. package/dist/src/harness/claude-code/index.js +20 -0
  115. package/dist/src/harness/claude-code/index.js.map +1 -0
  116. package/dist/src/harness/claude-code/map.d.ts +30 -0
  117. package/dist/src/harness/claude-code/map.js +108 -0
  118. package/dist/src/harness/claude-code/map.js.map +1 -0
  119. package/dist/src/harness/common/capabilities.d.ts +34 -0
  120. package/dist/src/harness/common/capabilities.js +60 -0
  121. package/dist/src/harness/common/capabilities.js.map +1 -0
  122. package/dist/src/harness/common/guard.d.ts +18 -0
  123. package/dist/src/harness/common/guard.js +31 -0
  124. package/dist/src/harness/common/guard.js.map +1 -0
  125. package/dist/src/harness/common/processes.d.ts +21 -0
  126. package/dist/src/harness/common/processes.js +46 -0
  127. package/dist/src/harness/common/processes.js.map +1 -0
  128. package/dist/src/harness/common/queue.d.ts +20 -0
  129. package/dist/src/harness/common/queue.js +76 -0
  130. package/dist/src/harness/common/queue.js.map +1 -0
  131. package/dist/src/harness/common/run-driver.d.ts +88 -0
  132. package/dist/src/harness/common/run-driver.js +317 -0
  133. package/dist/src/harness/common/run-driver.js.map +1 -0
  134. package/dist/src/harness/common/sandbox-workspace.d.ts +72 -0
  135. package/dist/src/harness/common/sandbox-workspace.js +155 -0
  136. package/dist/src/harness/common/sandbox-workspace.js.map +1 -0
  137. package/dist/src/harness/common/text.d.ts +9 -0
  138. package/dist/src/harness/common/text.js +49 -0
  139. package/dist/src/harness/common/text.js.map +1 -0
  140. package/dist/src/harness/common/types.d.ts +111 -0
  141. package/dist/src/harness/common/types.js +2 -0
  142. package/dist/src/harness/common/types.js.map +1 -0
  143. package/dist/src/harness/config.d.ts +43 -0
  144. package/dist/src/harness/config.js +200 -0
  145. package/dist/src/harness/config.js.map +1 -0
  146. package/dist/src/harness/index.d.ts +21 -0
  147. package/dist/src/harness/index.js +89 -0
  148. package/dist/src/harness/index.js.map +1 -0
  149. package/dist/src/harness/opencode/backend.d.ts +37 -0
  150. package/dist/src/harness/opencode/backend.js +114 -0
  151. package/dist/src/harness/opencode/backend.js.map +1 -0
  152. package/dist/src/harness/opencode/capabilities.d.ts +8 -0
  153. package/dist/src/harness/opencode/capabilities.js +11 -0
  154. package/dist/src/harness/opencode/capabilities.js.map +1 -0
  155. package/dist/src/harness/opencode/client.d.ts +70 -0
  156. package/dist/src/harness/opencode/client.js +189 -0
  157. package/dist/src/harness/opencode/client.js.map +1 -0
  158. package/dist/src/harness/opencode/index.d.ts +13 -0
  159. package/dist/src/harness/opencode/index.js +19 -0
  160. package/dist/src/harness/opencode/index.js.map +1 -0
  161. package/dist/src/harness/opencode/lines.d.ts +14 -0
  162. package/dist/src/harness/opencode/lines.js +99 -0
  163. package/dist/src/harness/opencode/lines.js.map +1 -0
  164. package/dist/src/harness/opencode/map.d.ts +13 -0
  165. package/dist/src/harness/opencode/map.js +84 -0
  166. package/dist/src/harness/opencode/map.js.map +1 -0
  167. package/dist/src/harness/opencode/turn.d.ts +23 -0
  168. package/dist/src/harness/opencode/turn.js +143 -0
  169. package/dist/src/harness/opencode/turn.js.map +1 -0
  170. package/dist/src/harness/reclaim.d.ts +13 -0
  171. package/dist/src/harness/reclaim.js +18 -0
  172. package/dist/src/harness/reclaim.js.map +1 -0
  173. package/dist/src/harness/stub/backend.d.ts +11 -0
  174. package/dist/src/harness/stub/backend.js +125 -0
  175. package/dist/src/harness/stub/backend.js.map +1 -0
  176. package/dist/src/harness/stub/index.d.ts +18 -0
  177. package/dist/src/harness/stub/index.js +21 -0
  178. package/dist/src/harness/stub/index.js.map +1 -0
  179. package/dist/src/harness/stub/workspace.d.ts +29 -0
  180. package/dist/src/harness/stub/workspace.js +92 -0
  181. package/dist/src/harness/stub/workspace.js.map +1 -0
  182. package/dist/src/index.d.ts +2 -0
  183. package/dist/src/index.js +3 -0
  184. package/dist/src/index.js.map +1 -0
  185. package/dist/src/runs/agents.d.ts +10 -0
  186. package/dist/src/runs/agents.js +49 -0
  187. package/dist/src/runs/agents.js.map +1 -0
  188. package/dist/src/runs/control.d.ts +31 -0
  189. package/dist/src/runs/control.js +105 -0
  190. package/dist/src/runs/control.js.map +1 -0
  191. package/dist/src/runs/index.d.ts +8 -0
  192. package/dist/src/runs/index.js +7 -0
  193. package/dist/src/runs/index.js.map +1 -0
  194. package/dist/src/runs/ports.d.ts +31 -0
  195. package/dist/src/runs/ports.js +5 -0
  196. package/dist/src/runs/ports.js.map +1 -0
  197. package/dist/src/runs/scan.d.ts +38 -0
  198. package/dist/src/runs/scan.js +149 -0
  199. package/dist/src/runs/scan.js.map +1 -0
  200. package/dist/src/runs/service.d.ts +70 -0
  201. package/dist/src/runs/service.js +239 -0
  202. package/dist/src/runs/service.js.map +1 -0
  203. package/dist/src/sandbox/harness-session.d.ts +11 -0
  204. package/dist/src/sandbox/harness-session.js +170 -0
  205. package/dist/src/sandbox/harness-session.js.map +1 -0
  206. package/dist/src/sandbox/host-command.d.ts +19 -0
  207. package/dist/src/sandbox/host-command.js +41 -0
  208. package/dist/src/sandbox/host-command.js.map +1 -0
  209. package/dist/src/sandbox/index.d.ts +11 -0
  210. package/dist/src/sandbox/index.js +15 -0
  211. package/dist/src/sandbox/index.js.map +1 -0
  212. package/dist/src/sandbox/naming.d.ts +49 -0
  213. package/dist/src/sandbox/naming.js +106 -0
  214. package/dist/src/sandbox/naming.js.map +1 -0
  215. package/dist/src/sandbox/openshell-client.d.ts +81 -0
  216. package/dist/src/sandbox/openshell-client.js +49 -0
  217. package/dist/src/sandbox/openshell-client.js.map +1 -0
  218. package/dist/src/sandbox/policy.d.ts +26 -0
  219. package/dist/src/sandbox/policy.js +100 -0
  220. package/dist/src/sandbox/policy.js.map +1 -0
  221. package/dist/src/sandbox/run-sandbox.d.ts +38 -0
  222. package/dist/src/sandbox/run-sandbox.js +247 -0
  223. package/dist/src/sandbox/run-sandbox.js.map +1 -0
  224. package/dist/src/sandbox/run-sandboxes.d.ts +18 -0
  225. package/dist/src/sandbox/run-sandboxes.js +225 -0
  226. package/dist/src/sandbox/run-sandboxes.js.map +1 -0
  227. package/dist/src/sandbox/timeout.d.ts +12 -0
  228. package/dist/src/sandbox/timeout.js +39 -0
  229. package/dist/src/sandbox/timeout.js.map +1 -0
  230. package/dist/src/sandbox/types.d.ts +125 -0
  231. package/dist/src/sandbox/types.js +15 -0
  232. package/dist/src/sandbox/types.js.map +1 -0
  233. package/dist/src/sandbox/worktree.d.ts +72 -0
  234. package/dist/src/sandbox/worktree.js +157 -0
  235. package/dist/src/sandbox/worktree.js.map +1 -0
  236. package/dist/src/store/derive.d.ts +39 -0
  237. package/dist/src/store/derive.js +68 -0
  238. package/dist/src/store/derive.js.map +1 -0
  239. package/dist/src/store/event-log.d.ts +79 -0
  240. package/dist/src/store/event-log.js +202 -0
  241. package/dist/src/store/event-log.js.map +1 -0
  242. package/dist/src/store/files.d.ts +51 -0
  243. package/dist/src/store/files.js +103 -0
  244. package/dist/src/store/files.js.map +1 -0
  245. package/dist/src/store/index.d.ts +15 -0
  246. package/dist/src/store/index.js +11 -0
  247. package/dist/src/store/index.js.map +1 -0
  248. package/dist/src/store/liveness.d.ts +61 -0
  249. package/dist/src/store/liveness.js +150 -0
  250. package/dist/src/store/liveness.js.map +1 -0
  251. package/dist/src/store/run-store.d.ts +67 -0
  252. package/dist/src/store/run-store.js +211 -0
  253. package/dist/src/store/run-store.js.map +1 -0
  254. package/dist/src/util/sequence.d.ts +58 -0
  255. package/dist/src/util/sequence.js +119 -0
  256. package/dist/src/util/sequence.js.map +1 -0
  257. package/dist/src/worker/control.d.ts +60 -0
  258. package/dist/src/worker/control.js +205 -0
  259. package/dist/src/worker/control.js.map +1 -0
  260. package/dist/src/worker/delivery.d.ts +17 -0
  261. package/dist/src/worker/delivery.js +43 -0
  262. package/dist/src/worker/delivery.js.map +1 -0
  263. package/dist/src/worker/driver.d.ts +88 -0
  264. package/dist/src/worker/driver.js +6 -0
  265. package/dist/src/worker/driver.js.map +1 -0
  266. package/dist/src/worker/fake-driver.d.ts +65 -0
  267. package/dist/src/worker/fake-driver.js +123 -0
  268. package/dist/src/worker/fake-driver.js.map +1 -0
  269. package/dist/src/worker/index.d.ts +15 -0
  270. package/dist/src/worker/index.js +11 -0
  271. package/dist/src/worker/index.js.map +1 -0
  272. package/dist/src/worker/main.d.ts +1 -0
  273. package/dist/src/worker/main.js +57 -0
  274. package/dist/src/worker/main.js.map +1 -0
  275. package/dist/src/worker/retained.d.ts +13 -0
  276. package/dist/src/worker/retained.js +40 -0
  277. package/dist/src/worker/retained.js.map +1 -0
  278. package/dist/src/worker/run-loop.d.ts +63 -0
  279. package/dist/src/worker/run-loop.js +325 -0
  280. package/dist/src/worker/run-loop.js.map +1 -0
  281. package/dist/src/worker/spawn.d.ts +68 -0
  282. package/dist/src/worker/spawn.js +201 -0
  283. package/dist/src/worker/spawn.js.map +1 -0
  284. package/image/Dockerfile +81 -0
  285. package/image/build.sh +62 -0
  286. package/image/pnpm-shim.sh +25 -0
  287. package/package.json +54 -4
  288. package/policy/profiles/culture-agents-claude-subscription.yaml +59 -0
  289. package/policy/profiles/culture-agents-local-gateway.yaml +49 -0
  290. package/policy/run-sandbox.policy.json +10 -0
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Starting a run's worker and reaching it later.
3
+ *
4
+ * `spawnWorker` resolves only after the durability condition holds: the worker's identity
5
+ * (pid), its event log with event 1 (`null` -> `queued`) and its cursor are on disk, owner-only,
6
+ * and its control socket answers. Only then may the daemon acknowledge the submit (README,
7
+ * order of checks for runs.submit, step 7).
8
+ *
9
+ * `reconnectWorker` finds a surviving worker after a daemon restart and reports whether its
10
+ * control socket answers. It never signals or terminates the worker.
11
+ */
12
+ import { spawn } from "node:child_process";
13
+ import { closeSync, openSync, statSync, writeFileSync } from "node:fs";
14
+ import { fileURLToPath } from "node:url";
15
+ import { readCursorFile, readLog } from "../store/event-log.js";
16
+ import { FILE_MODE, runDir, runFile } from "../store/files.js";
17
+ import { checkLiveness, readWorkerIdentity } from "../store/liveness.js";
18
+ import { createRun } from "../store/run-store.js";
19
+ import { again, finish, stepUntil } from "../util/sequence.js";
20
+ import { ControlUnavailableError, controlRequest } from "./control.js";
21
+ import { settleFromLog } from "./delivery.js";
22
+ /** The compiled worker entry point. */
23
+ export const WORKER_ENTRY = fileURLToPath(new URL("./main.js", import.meta.url));
24
+ /** Live control of one worker over its control socket. */
25
+ export class WorkerControl {
26
+ runDir;
27
+ timeoutMs;
28
+ steerTimeoutMs;
29
+ constructor(runDir, timeoutMs = 2000,
30
+ /**
31
+ * A steer is answered only after the harness took the message, which can take seconds. Giving up
32
+ * earlier would tell the caller `unavailable` for a steer that is then delivered.
33
+ */
34
+ steerTimeoutMs = 8000) {
35
+ this.runDir = runDir;
36
+ this.timeoutMs = timeoutMs;
37
+ this.steerTimeoutMs = steerTimeoutMs;
38
+ }
39
+ ping() {
40
+ return controlRequest(this.runDir, { op: "ping" }, this.timeoutMs);
41
+ }
42
+ steer(action, message) {
43
+ return this.deliver({ op: "steer", action, message }, "run.control", this.steerTimeoutMs);
44
+ }
45
+ cancel(action, reason) {
46
+ const req = reason === undefined ? { op: "cancel", action } : { op: "cancel", action, reason };
47
+ return this.deliver(req, "run.control");
48
+ }
49
+ /** Have the worker record a refusal the core decided (`run.refused`). */
50
+ refuse(action) {
51
+ return this.deliver({ op: "refuse", action }, "run.refused");
52
+ }
53
+ /** Send an action; with no reply, settle its outcome from the run's log (see `settleFromLog`). */
54
+ async deliver(request, recordedAs, timeoutMs = this.timeoutMs) {
55
+ try {
56
+ return await controlRequest(this.runDir, request, timeoutMs);
57
+ }
58
+ catch (err) {
59
+ if (err instanceof ControlUnavailableError)
60
+ return settleFromLog(this.runDir, request.action, recordedAs, err);
61
+ throw err;
62
+ }
63
+ }
64
+ }
65
+ /** The worker did not reach the durability condition; nothing may be acknowledged. */
66
+ export class WorkerSpawnError extends Error {
67
+ constructor(message) {
68
+ super(message);
69
+ this.name = "WorkerSpawnError";
70
+ }
71
+ }
72
+ const DURABLE_FILES = ["record", "events", "cursor", "worker"];
73
+ /** Whether pid, event 1 and cursor are on disk for `pid`; throws if a file is not owner-only. */
74
+ function filesDurable(dir, runId, pid) {
75
+ if (readWorkerIdentity(dir)?.pid !== pid)
76
+ return false;
77
+ const first = readLog(dir, runId).events[0];
78
+ if (first?.cursor !== 1)
79
+ return false;
80
+ if ((readCursorFile(dir) ?? 0) < 1)
81
+ return false;
82
+ for (const file of DURABLE_FILES) {
83
+ const mode = statSync(runFile(dir, file)).mode & 0o777;
84
+ if (mode !== FILE_MODE)
85
+ throw new WorkerSpawnError(`${file} has mode ${mode.toString(8)}, not 600`);
86
+ }
87
+ return true;
88
+ }
89
+ async function controlReady(dir, pid) {
90
+ try {
91
+ const reply = await controlRequest(dir, { op: "ping" }, 500);
92
+ return reply.ok && reply.pid === pid;
93
+ }
94
+ catch (err) {
95
+ if (err instanceof ControlUnavailableError)
96
+ return false;
97
+ throw err;
98
+ }
99
+ }
100
+ /** The worker process's command line (after the node binary). */
101
+ function workerArgs(options) {
102
+ const args = [WORKER_ENTRY, "--state-dir", options.stateDir, "--run-id", options.run.id];
103
+ if (options.heartbeatMs !== undefined)
104
+ args.push("--heartbeat-ms", String(options.heartbeatMs));
105
+ if (options.cancelGraceMs !== undefined)
106
+ args.push("--cancel-grace-ms", String(options.cancelGraceMs));
107
+ return args;
108
+ }
109
+ function killQuietly(pid) {
110
+ try {
111
+ process.kill(pid, "SIGKILL");
112
+ }
113
+ catch {
114
+ // already gone
115
+ }
116
+ }
117
+ /**
118
+ * Poll until the run is durable on disk and its worker is either controllable or already finished
119
+ * (a very short run may end before the first ping; its files still hold, so the submit may be
120
+ * acknowledged). `exitedNow` says how the worker ended, or null while it runs.
121
+ */
122
+ function waitUntilDurable(dir, runId, pid, deadline, exitedNow) {
123
+ return stepUntil(async () => {
124
+ const exited = exitedNow();
125
+ const ended = exited !== null;
126
+ if (filesDurable(dir, runId, pid) && (ended || (await controlReady(dir, pid))))
127
+ return finish(undefined);
128
+ if (ended)
129
+ throw new WorkerSpawnError(`the worker exited before its run was durable (${exited}); see ${runFile(dir, "log")}`);
130
+ if (Date.now() > deadline)
131
+ throw new WorkerSpawnError(`the worker did not make its run durable in time; see ${runFile(dir, "log")}`);
132
+ return again(5);
133
+ });
134
+ }
135
+ /** Create the run, start its worker detached, and resolve once the worker's files are durable. */
136
+ export async function spawnWorker(options) {
137
+ const { stateDir, run } = options;
138
+ if (run.state !== "queued" || run.worker_pid !== null)
139
+ throw new RangeError("a new run must be queued with no worker");
140
+ const dir = createRun(stateDir, run);
141
+ writeFileSync(runFile(dir, "driver"), `${JSON.stringify(options.driver)}\n`, { mode: FILE_MODE, flag: "wx" });
142
+ const args = workerArgs(options);
143
+ const logFd = openSync(runFile(dir, "log"), "a", FILE_MODE);
144
+ let exited = null;
145
+ const child = (() => {
146
+ try {
147
+ // detached: own session and process group, so the daemon's death or terminal does not reach it.
148
+ return spawn(process.execPath, args, {
149
+ cwd: dir,
150
+ detached: true,
151
+ stdio: ["ignore", logFd, logFd],
152
+ env: options.env ?? process.env,
153
+ });
154
+ }
155
+ finally {
156
+ closeSync(logFd);
157
+ }
158
+ })();
159
+ const onExit = (code, signal) => (exited = signal ?? `exit ${String(code)}`);
160
+ child.once("exit", onExit);
161
+ child.once("error", (err) => (exited = err.message));
162
+ child.unref();
163
+ const pid = child.pid;
164
+ try {
165
+ if (pid === undefined)
166
+ throw new WorkerSpawnError(`the worker could not be started: ${exited ?? "no pid"}`);
167
+ const deadline = Date.now() + (options.readyTimeoutMs ?? 10_000);
168
+ await waitUntilDurable(dir, run.id, pid, deadline, () => exited);
169
+ }
170
+ catch (err) {
171
+ if (pid !== undefined && exited === null)
172
+ killQuietly(pid);
173
+ throw err;
174
+ }
175
+ finally {
176
+ child.off("exit", onExit);
177
+ }
178
+ return { runId: run.id, runDir: dir, pid, control: new WorkerControl(dir) };
179
+ }
180
+ /**
181
+ * Reach a surviving worker's control socket. `live: false` (with the reason) when the worker is
182
+ * not alive or does not answer. Note: in D1 a restarted core still reports such a run with
183
+ * `live_control: false` (README: after a core restart); this only establishes reachability.
184
+ */
185
+ export async function reconnectWorker(stateDir, runId, timeoutMs = 1000) {
186
+ const dir = runDir(stateDir, runId);
187
+ const liveness = checkLiveness(dir);
188
+ if (!liveness.alive)
189
+ return { live: false, liveness, reason: liveness.reason };
190
+ const control = new WorkerControl(dir, timeoutMs);
191
+ try {
192
+ const reply = await control.ping();
193
+ if (reply.ok && reply.pid === liveness.pid)
194
+ return { live: true, liveness, control };
195
+ return { live: false, liveness, reason: reply.ok ? "a different process answered the control socket" : reply.error.message };
196
+ }
197
+ catch (err) {
198
+ return { live: false, liveness, reason: err instanceof Error ? err.message : String(err) };
199
+ }
200
+ }
201
+ //# sourceMappingURL=spawn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"spawn.js","sourceRoot":"","sources":["../../../src/worker/spawn.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACvE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAGzC,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAgB,MAAM,mBAAmB,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAiB,MAAM,sBAAsB,CAAC;AACxF,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAa,MAAM,qBAAqB,CAAC;AAC1E,OAAO,EAAE,uBAAuB,EAAE,cAAc,EAA0C,MAAM,cAAc,CAAC;AAC/G,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAG9C,uCAAuC;AACvC,MAAM,CAAC,MAAM,YAAY,GAAG,aAAa,CAAC,IAAI,GAAG,CAAC,WAA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K,CAAC,EAAE,CAAC,CAAC,CAAC,iDAAiD,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;IAC/H,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;IAC7F,CAAC;AACH,CAAC"}
@@ -0,0 +1,81 @@
1
+ # Run image for culture-agents harness drivers (plan task t11), tag
2
+ # culture-agents-run:dev. Build it with core/image/build.sh, which stages the
3
+ # context: the AI SDK Claude Code bridge manifest and lockfile (copied from the
4
+ # installed @ai-sdk/harness-claude-code, so the baked dependencies match the
5
+ # adapter version core pins) and the OpenCode v2 binary.
6
+ #
7
+ # Derived from the D1 probe (docs/d0/probe.md, "What the image must contain"):
8
+ # - No USER line: OpenShell refuses a workload identity with UID or GID 0, and
9
+ # an image without USER runs as `ubuntu` (uid 1000) with HOME=/sandbox.
10
+ # - The sandbox has no npm registry, so bridge dependencies are installed here
11
+ # and a pnpm shim links them into the adapter's bootstrap directory.
12
+ # - Only /usr, /lib, /etc and /bin are readable in the sandbox (not /opt), so
13
+ # everything lives under /usr.
14
+ ARG BASE_IMAGE=nvcr.io/nvidia/base/ubuntu:24.04
15
+ FROM ${BASE_IMAGE}
16
+
17
+ ARG NODE_MAJOR=22
18
+ ARG PNPM_VERSION=10.33.0
19
+
20
+ # git and openssh-client for the working-tree handoff (clone from a bundle in,
21
+ # the host fetches over SSH out); procps for the process checks after cancel.
22
+ # Node.js comes from the NodeSource apt repository, added by hand: its signing
23
+ # key is fetched over HTTPS only and the repository is pinned to that key, so
24
+ # no downloaded script is executed. curl and gnupg are only needed for that and
25
+ # are purged again: the repo's local-gateway profile deliberately does not let
26
+ # curl reach the gateway.
27
+ RUN apt-get update \
28
+ && apt-get install -y --no-install-recommends ca-certificates curl git gnupg openssh-client procps \
29
+ && mkdir -p /etc/apt/keyrings \
30
+ && curl --proto '=https' --tlsv1.2 -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
31
+ | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
32
+ && echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_${NODE_MAJOR}.x nodistro main" \
33
+ > /etc/apt/sources.list.d/nodesource.list \
34
+ && apt-get update \
35
+ && apt-get install -y --no-install-recommends nodejs \
36
+ && npm install -g --ignore-scripts "pnpm@${PNPM_VERSION}" \
37
+ && apt-get purge -y curl gnupg && apt-get autoremove -y \
38
+ && rm -rf /var/lib/apt/lists/* /root/.npm
39
+
40
+ # Claude Code bridge dependencies at the location the repo's profile
41
+ # (core/policy/profiles/culture-agents-claude-subscription.yaml) allows:
42
+ # /usr/local/lib/ai-sdk-harness/**. The Agent SDK launches the native `claude`
43
+ # bundled in these node_modules, so this path is what the egress policy sees.
44
+ COPY claude-code-bridge/ /usr/local/lib/ai-sdk-harness/claude-code/
45
+ WORKDIR /usr/local/lib/ai-sdk-harness/claude-code
46
+ RUN pnpm install --frozen-lockfile \
47
+ && ./node_modules/.bin/claude --version
48
+ WORKDIR /
49
+
50
+ # Compatibility copy for the profile the running gateway actually has
51
+ # (`claude-code-subscription`, binaries /usr/lib/node_modules/@anthropic-ai/**),
52
+ # which does not list /usr/local/lib/ai-sdk-harness/**. A symlink would not do:
53
+ # the policy matches the resolved executable path. Build with
54
+ # CLAUDE_BRIDGE_COMPAT=0 once the gateway runs the repo's profile.
55
+ ARG CLAUDE_BRIDGE_COMPAT=1
56
+ RUN if [ "$CLAUDE_BRIDGE_COMPAT" = "1" ]; then \
57
+ mkdir -p /usr/lib/node_modules/@anthropic-ai \
58
+ && cp -a /usr/local/lib/ai-sdk-harness/claude-code \
59
+ /usr/lib/node_modules/@anthropic-ai/ai-sdk-harness-bridge; \
60
+ fi \
61
+ && npm uninstall -g pnpm
62
+ COPY pnpm-shim.sh /usr/local/bin/pnpm
63
+
64
+ # OpenCode v2, driven through its own server API (`opencode serve`). Both the
65
+ # repo's local-gateway profile and the gateway's `lobes-gateway` profile let
66
+ # exactly /usr/local/bin/opencode reach the model gateway. No AI SDK OpenCode
67
+ # bridge is installed: that adapter targets the OpenCode 1.x server API.
68
+ COPY opencode /usr/local/bin/opencode
69
+ # The last step drops the default user's extra groups (adm, sudo, ...), so the
70
+ # user declared below carries no more than the sandbox gives.
71
+ RUN chmod 0755 /usr/local/bin/pnpm /usr/local/bin/opencode && /usr/local/bin/opencode --version \
72
+ && if id ubuntu >/dev/null 2>&1; then usermod -G "" ubuntu; fi
73
+
74
+ # OpenShell runs every sandbox process as uid 1000 whatever the image says
75
+ # (measured: same uid, capabilities, seccomp, readable paths and blocked egress
76
+ # with and without this line). Declaring it keeps a plain `docker run` of the
77
+ # image from being root too.
78
+ USER 1000:1000
79
+
80
+ LABEL org.opencontainers.image.title="culture-agents run image" \
81
+ org.opencontainers.image.source="https://github.com/agentculture/culture-agents"
package/image/build.sh ADDED
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env bash
2
+ # Build the run image (default tag culture-agents-run:dev).
3
+ #
4
+ # core/image/build.sh [--tag NAME] [--opencode PATH] [--no-claude-compat]
5
+ #
6
+ # Run `npm ci` in core/ first: the Claude Code bridge manifest and lockfile are
7
+ # copied from the installed @ai-sdk/harness-claude-code so the baked bridge
8
+ # matches the adapter version core pins. The OpenCode binary comes from
9
+ # --opencode, $OPENCODE_BIN or `opencode` on PATH, and must report
10
+ # $OPENCODE_VERSION (default 2.3.2, the version the D1 probe measured).
11
+ set -euo pipefail
12
+
13
+ here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
14
+ core="$(dirname "$here")"
15
+ tag="culture-agents-run:dev"
16
+ opencode_bin="${OPENCODE_BIN:-}"
17
+ compat=1
18
+ want_version="${OPENCODE_VERSION:-2.3.2}"
19
+
20
+ while [[ $# -gt 0 ]]; do
21
+ case "$1" in
22
+ --tag) tag="$2"; shift 2 ;;
23
+ --opencode) opencode_bin="$2"; shift 2 ;;
24
+ --no-claude-compat) compat=0; shift ;;
25
+ *) echo "unknown argument: $1" >&2; exit 1 ;;
26
+ esac
27
+ done
28
+
29
+ bridge="$core/node_modules/@ai-sdk/harness-claude-code/dist/bridge"
30
+ [[ -f "$bridge/package.json" ]] || { echo "missing $bridge: run npm ci in core/ first" >&2; exit 2; }
31
+
32
+ if [[ -z "$opencode_bin" ]]; then
33
+ opencode_bin="$(command -v opencode || true)"
34
+ fi
35
+ [[ -n "$opencode_bin" ]] || { echo "no opencode binary: pass --opencode PATH" >&2; exit 2; }
36
+ opencode_bin="$(readlink -f "$opencode_bin")"
37
+ got_version="$("$opencode_bin" --version | sed 's/^opencode v//')"
38
+ if [[ "$got_version" != "$want_version" ]]; then
39
+ echo "opencode is $got_version, expected $want_version (set OPENCODE_VERSION to override)" >&2
40
+ exit 2
41
+ fi
42
+
43
+ ctx="$(mktemp -d "${TMPDIR:-/tmp}/culture-agents-image-XXXXXX")"
44
+ trap 'rm -rf "$ctx"' EXIT
45
+ mkdir -p "$ctx/claude-code-bridge"
46
+ cp "$bridge/package.json" "$ctx/claude-code-bridge/package.json"
47
+ # The adapter ships its lockfile as pnpm-lockfile.yaml (pnpm pack drops pnpm-lock.yaml).
48
+ cp "$bridge/pnpm-lockfile.yaml" "$ctx/claude-code-bridge/pnpm-lock.yaml"
49
+ cp "$bridge/pnpm-workspace.yaml" "$ctx/claude-code-bridge/pnpm-workspace.yaml"
50
+ cp "$here/Dockerfile" "$here/pnpm-shim.sh" "$ctx/"
51
+ cp "$opencode_bin" "$ctx/opencode"
52
+
53
+ # Versions as image labels, read by the harness registry's availability check.
54
+ claude_version="$(node -e 'const p=require(process.argv[1]);process.stdout.write(p.dependencies["@anthropic-ai/claude-code"])' "$bridge/package.json")"
55
+ adapter_version="$(node -e 'process.stdout.write(require(process.argv[1]).version)' "$core/node_modules/@ai-sdk/harness-claude-code/package.json")"
56
+
57
+ docker build --build-arg "CLAUDE_BRIDGE_COMPAT=$compat" \
58
+ --label "dev.culture.agents.claude-code=$claude_version" \
59
+ --label "dev.culture.agents.harness-claude-code=$adapter_version" \
60
+ --label "dev.culture.agents.opencode=$got_version" \
61
+ -t "$tag" "$ctx"
62
+ echo "built $tag (opencode $got_version, claude bridge compat=$compat)"
@@ -0,0 +1,25 @@
1
+ #!/bin/sh
2
+ # Stands in for pnpm inside the run sandbox. The AI SDK Claude Code adapter's
3
+ # bootstrap runs `pnpm install --frozen-lockfile --store-dir .pnpm-store` in
4
+ # $HOME/.ai-sdk-harness/.harness-bootstrap/claude-code, but the sandbox has no
5
+ # npm registry. The bridge dependencies were installed into the image at build
6
+ # time from the adapter's own lockfile; this shim links them instead.
7
+ #
8
+ # It prefers the compatibility copy under /usr/lib/node_modules/@anthropic-ai/
9
+ # when the image has one (the running gateway's profile only allows binaries
10
+ # there), and otherwise uses /usr/local/lib/ai-sdk-harness/claude-code (the
11
+ # location the repo's profile allows).
12
+ set -e
13
+ case "$(basename "$PWD")" in
14
+ claude-code)
15
+ src=/usr/local/lib/ai-sdk-harness/claude-code
16
+ compat=/usr/lib/node_modules/@anthropic-ai/ai-sdk-harness-bridge
17
+ [ -d "$compat/node_modules" ] && src=$compat
18
+ ;;
19
+ *)
20
+ echo "pnpm shim: no baked dependencies for $PWD" >&2
21
+ exit 1
22
+ ;;
23
+ esac
24
+ ln -sfn "$src/node_modules" node_modules
25
+ echo "pnpm shim: linked $src/node_modules"
package/package.json CHANGED
@@ -1,6 +1,56 @@
1
1
  {
2
2
  "name": "@culture.dev/agents",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.15.0",
4
+ "description": "TypeScript core of the culture-agents control plane",
5
+ "private": false,
6
+ "type": "module",
7
+ "license": "Apache-2.0",
8
+ "main": "./dist/src/index.js",
9
+ "types": "./dist/src/index.d.ts",
10
+ "bin": {
11
+ "culture-agents-core": "dist/src/daemon/main.js"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/src/index.d.ts",
16
+ "default": "./dist/src/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist/src",
21
+ "dist/contract-schemas",
22
+ "policy",
23
+ "image"
24
+ ],
25
+ "engines": {
26
+ "node": ">=22"
27
+ },
28
+ "scripts": {
29
+ "build": "rm -rf dist && tsc -p tsconfig.json && node scripts/copy-contract-schemas.mjs",
30
+ "typecheck": "tsc -p tsconfig.json --noEmit",
31
+ "lint": "eslint .",
32
+ "test": "npm run build && node --test --test-timeout=120000 'dist/test/**/*.test.js'",
33
+ "test:coverage": "npm run build && node --test --test-timeout=120000 --experimental-test-coverage --enable-source-maps --test-reporter=spec --test-reporter-destination=stdout --test-reporter=lcov --test-reporter-destination=coverage.lcov 'dist/test/**/*.test.js'",
34
+ "gen:contract": "node scripts/gen-contract-types.mjs",
35
+ "check:contract-types": "node scripts/gen-contract-types.mjs --check"
36
+ },
37
+ "devDependencies": {
38
+ "@eslint/js": "9.39.5",
39
+ "@types/node": "22.20.5",
40
+ "eslint": "9.39.5",
41
+ "typescript": "5.9.3",
42
+ "typescript-eslint": "8.74.0"
43
+ },
44
+ "dependencies": {
45
+ "@ai-openshell/sdk": "0.1.10",
46
+ "@ai-sdk/harness": "1.0.192",
47
+ "@ai-sdk/harness-claude-code": "1.0.196",
48
+ "ajv": "8.20.0"
49
+ },
50
+ "repository": {
51
+ "type": "git",
52
+ "url": "git+https://github.com/agentculture/culture-agents.git",
53
+ "directory": "core"
54
+ },
55
+ "homepage": "https://github.com/agentculture/culture-agents#readme"
56
+ }
@@ -0,0 +1,59 @@
1
+ # OpenShell provider profile: Claude Code on a Claude subscription token.
2
+ #
3
+ # Import (an operator step, not done by the core):
4
+ # openshell provider profile lint -f core/policy/profiles/culture-agents-claude-subscription.yaml
5
+ # openshell provider profile import -f core/policy/profiles/culture-agents-claude-subscription.yaml
6
+ # openshell provider create --name claude-sub-ca --type culture-agents-claude-subscription \
7
+ # --credential CLAUDE_CODE_OAUTH_TOKEN
8
+ # `--credential VAR` reads the value from the operator's environment at
9
+ # provider creation; this file names the variable only. Inside the sandbox
10
+ # the variable holds an `openshell:resolve:env:` placeholder; the proxy puts the
11
+ # real token on the `authorization` header at egress, for the endpoint below
12
+ # and nowhere else. Take the token from `claude setup-token` (durable), stored
13
+ # behind `grant`.
14
+ #
15
+ # Binaries (the D1 probe, docs/d0/probe.md, "Credentials and profiles"):
16
+ # - The stock `claude-code` profile lists /usr/bin/claude and
17
+ # /usr/local/bin/claude, but an npm global install connects from
18
+ # /usr/lib/node_modules/@anthropic-ai/claude-code/bin/claude.exe, so every
19
+ # call was denied. That exact path is listed here.
20
+ # - The AI SDK bridge launches the native `claude` bundled in the Claude Agent
21
+ # SDK, from the bridge's own node_modules. The probe had to bake the bridge
22
+ # under /usr/lib/node_modules/@anthropic-ai/ only to fit the stock glob. This
23
+ # profile instead names /usr/local/lib/ai-sdk-harness/**, the read-only
24
+ # location where the image bakes every adapter's bridge dependencies
25
+ # (claude-code/ and opencode/ side by side).
26
+ # - No path under /sandbox (the writable home, where the adapter's bootstrap
27
+ # directory lives) is listed: a binary a run can write must not reach the
28
+ # credentialed endpoint.
29
+ #
30
+ # Endpoints: api.anthropic.com only. The bundled profile also opens
31
+ # statsig.anthropic.com and sentry.io for telemetry; they are left out here.
32
+ # NOT VERIFIED: whether Claude Code runs cleanly without them (the probe ran
33
+ # with them allowed).
34
+
35
+ id: culture-agents-claude-subscription
36
+ display_name: Claude Code (subscription token, culture-agents)
37
+ description: Claude Code CLI and the AI SDK Claude Code bridge, on a Claude subscription OAuth token
38
+ category: agent
39
+ inference_capable: true
40
+ credentials:
41
+ - name: oauth_token
42
+ description: Claude subscription OAuth token (claude setup-token)
43
+ env_vars: [CLAUDE_CODE_OAUTH_TOKEN]
44
+ required: true
45
+ auth_style: bearer
46
+ header_name: authorization
47
+ discovery:
48
+ credentials: [oauth_token]
49
+ endpoints:
50
+ - host: api.anthropic.com
51
+ port: 443
52
+ protocol: rest
53
+ access: read-write
54
+ enforcement: enforce
55
+ binaries:
56
+ - /usr/bin/claude
57
+ - /usr/local/bin/claude
58
+ - /usr/lib/node_modules/@anthropic-ai/claude-code/bin/claude.exe
59
+ - /usr/local/lib/ai-sdk-harness/**
@@ -0,0 +1,49 @@
1
+ # OpenShell provider profile: the host-local OpenAI-compatible model gateway
2
+ # (lobes) on port 8001, as used by OpenCode in the D1 probe.
3
+ #
4
+ # Import (an operator step, not done by the core):
5
+ # openshell provider profile lint -f core/policy/profiles/culture-agents-local-gateway.yaml
6
+ # openshell provider profile import -f core/policy/profiles/culture-agents-local-gateway.yaml
7
+ # openshell provider create --name lobes-ca --type culture-agents-local-gateway \
8
+ # --credential LOBES_GATEWAY_KEY
9
+ # The key is named by its environment variable only; inside the sandbox it is
10
+ # an `openshell:resolve:env:` placeholder resolved by the proxy at egress.
11
+ # OpenCode reads it through the config reference `{env:LOBES_GATEWAY_KEY}` with
12
+ # baseURL http://host.openshell.internal:8001/v1.
13
+ #
14
+ # Binaries (docs/d0/probe.md, "Obstacles" item 6):
15
+ # - /usr/local/bin/opencode: the OpenCode CLI in the image.
16
+ # - The AI SDK OpenCode adapter pins its own OpenCode (opencode-ai 1.19.5) and
17
+ # runs it from the bridge's node_modules
18
+ # (.../node_modules/.pnpm/opencode-ai@1.19.5/.../bin/opencode.exe). The probe's
19
+ # `lobes` profile denied that path, so the probe copied the binary to
20
+ # /usr/local/bin/opencode. This profile names the bridge location instead:
21
+ # /usr/local/lib/ai-sdk-harness/**, where the image bakes the bridge
22
+ # dependencies (read-only in the sandbox; /opt is not readable there).
23
+ # - /usr/bin/curl from the probe's profile is dropped: it was a debugging aid
24
+ # and would let any script reach the credentialed gateway.
25
+ # - Nothing under the writable /sandbox home is listed.
26
+
27
+ id: culture-agents-local-gateway
28
+ display_name: Local model gateway (culture-agents)
29
+ description: Host-local OpenAI-compatible model gateway on port 8001, for OpenCode and its AI SDK bridge
30
+ category: inference
31
+ inference_capable: true
32
+ credentials:
33
+ - name: api_key
34
+ description: Local model gateway API key
35
+ env_vars: [LOBES_GATEWAY_KEY]
36
+ required: true
37
+ auth_style: bearer
38
+ header_name: authorization
39
+ discovery:
40
+ credentials: [api_key]
41
+ endpoints:
42
+ - host: host.openshell.internal
43
+ port: 8001
44
+ protocol: rest
45
+ access: read-write
46
+ enforcement: enforce
47
+ binaries:
48
+ - /usr/local/bin/opencode
49
+ - /usr/local/lib/ai-sdk-harness/**
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 1,
3
+ "filesystem_policy": {
4
+ "include_workdir": true,
5
+ "read_only": ["/bin", "/usr", "/lib", "/proc", "/dev/urandom", "/etc", "/var/log"],
6
+ "read_write": ["/tmp", "/dev/null"]
7
+ },
8
+ "landlock": { "compatibility": "hard_requirement" },
9
+ "network_policies": {}
10
+ }