@yaks/harness 0.0.0-stage → 0.2.36

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 (230) hide show
  1. package/FRONTEND.md +170 -0
  2. package/LICENSE +202 -0
  3. package/MCPAuthPanel.js +166 -0
  4. package/MCPAuthPanel.js.map +1 -0
  5. package/MCPAuthPanel.ts +184 -0
  6. package/ModelPanel.js +124 -0
  7. package/ModelPanel.js.map +1 -0
  8. package/ModelPanel.ts +143 -0
  9. package/PersonaPanel.js +106 -0
  10. package/PersonaPanel.js.map +1 -0
  11. package/PersonaPanel.ts +110 -0
  12. package/README.md +1570 -2
  13. package/RuntimePanel.js +180 -0
  14. package/RuntimePanel.js.map +1 -0
  15. package/RuntimePanel.ts +205 -0
  16. package/STREAMING.md +101 -0
  17. package/WINDOWS.md +78 -0
  18. package/WORKER.md +221 -0
  19. package/_dist/agent.d.ts +197 -0
  20. package/_dist/agent.d.ts.map +1 -0
  21. package/_dist/cli.d.ts +10 -0
  22. package/_dist/cli.d.ts.map +1 -0
  23. package/_dist/conversations.d.ts +45 -0
  24. package/_dist/conversations.d.ts.map +1 -0
  25. package/_dist/default.d.ts +32 -0
  26. package/_dist/default.d.ts.map +1 -0
  27. package/_dist/detail.d.ts +16 -0
  28. package/_dist/detail.d.ts.map +1 -0
  29. package/_dist/diagnostics.d.ts +5 -0
  30. package/_dist/diagnostics.d.ts.map +1 -0
  31. package/_dist/effects.d.ts +35 -0
  32. package/_dist/effects.d.ts.map +1 -0
  33. package/_dist/http.d.ts +12 -0
  34. package/_dist/http.d.ts.map +1 -0
  35. package/_dist/isolated.d.ts +6 -0
  36. package/_dist/isolated.d.ts.map +1 -0
  37. package/_dist/local.d.ts +74 -0
  38. package/_dist/local.d.ts.map +1 -0
  39. package/_dist/machine.d.ts +3 -0
  40. package/_dist/machine.d.ts.map +1 -0
  41. package/_dist/mcp_auth.d.ts +8 -0
  42. package/_dist/mcp_auth.d.ts.map +1 -0
  43. package/_dist/mod.d.ts +43 -0
  44. package/_dist/mod.d.ts.map +1 -0
  45. package/_dist/model_selection.d.ts +8 -0
  46. package/_dist/model_selection.d.ts.map +1 -0
  47. package/_dist/routes.d.ts +11 -0
  48. package/_dist/routes.d.ts.map +1 -0
  49. package/_dist/runs.d.ts +7 -0
  50. package/_dist/runs.d.ts.map +1 -0
  51. package/_dist/runtime.d.ts +2 -0
  52. package/_dist/runtime.d.ts.map +1 -0
  53. package/_dist/session_machines.d.ts +18 -0
  54. package/_dist/session_machines.d.ts.map +1 -0
  55. package/_dist/skills.d.ts +11 -0
  56. package/_dist/skills.d.ts.map +1 -0
  57. package/_dist/store.d.ts +38 -0
  58. package/_dist/store.d.ts.map +1 -0
  59. package/_dist/tools.d.ts +28 -0
  60. package/_dist/tools.d.ts.map +1 -0
  61. package/_dist/view.d.ts +5 -0
  62. package/_dist/view.d.ts.map +1 -0
  63. package/_dist/vocab.d.ts +7 -0
  64. package/_dist/vocab.d.ts.map +1 -0
  65. package/_dist/worn.d.ts +52 -0
  66. package/_dist/worn.d.ts.map +1 -0
  67. package/agent.js +773 -0
  68. package/agent.js.map +1 -0
  69. package/agent.ts +959 -0
  70. package/app.js +811 -0
  71. package/app.js.map +1 -0
  72. package/app.ts +868 -0
  73. package/artifact_tools.js +186 -0
  74. package/artifact_tools.js.map +1 -0
  75. package/artifact_tools.ts +190 -0
  76. package/backend_worker.ts +286 -0
  77. package/cli.js +43 -0
  78. package/cli.js.map +1 -0
  79. package/cli.ts +48 -0
  80. package/conversations.js +526 -0
  81. package/conversations.js.map +1 -0
  82. package/conversations.ts +576 -0
  83. package/conversations_testing.ts +162 -0
  84. package/default.js +162 -0
  85. package/default.js.map +1 -0
  86. package/default.ts +168 -0
  87. package/deferred_model.ts +1 -0
  88. package/deno.json +1 -0
  89. package/detail.js +50 -0
  90. package/detail.js.map +1 -0
  91. package/detail.ts +75 -0
  92. package/diagnostics.js +132 -0
  93. package/diagnostics.js.map +1 -0
  94. package/diagnostics.ts +150 -0
  95. package/draft_vault.js +149 -0
  96. package/draft_vault.js.map +1 -0
  97. package/draft_vault.ts +176 -0
  98. package/effects.js +233 -0
  99. package/effects.js.map +1 -0
  100. package/effects.ts +250 -0
  101. package/frontend/vocab.json +376 -0
  102. package/frontend.js +511 -0
  103. package/frontend.js.map +1 -0
  104. package/frontend.ts +389 -0
  105. package/http.js +107 -0
  106. package/http.js.map +1 -0
  107. package/http.ts +105 -0
  108. package/input_text.js +91 -0
  109. package/input_text.js.map +1 -0
  110. package/input_text.ts +99 -0
  111. package/instructions.js +111 -0
  112. package/instructions.js.map +1 -0
  113. package/instructions.ts +145 -0
  114. package/isolated.js +512 -0
  115. package/isolated.js.map +1 -0
  116. package/isolated.ts +502 -0
  117. package/keyboard.js +544 -0
  118. package/keyboard.js.map +1 -0
  119. package/keyboard.ts +403 -0
  120. package/legacy_instructions.js +3 -0
  121. package/legacy_instructions.js.map +1 -0
  122. package/legacy_instructions.ts +13 -0
  123. package/local.js +181 -0
  124. package/local.js.map +1 -0
  125. package/local.ts +282 -0
  126. package/machine.js +3 -0
  127. package/machine.js.map +1 -0
  128. package/machine.ts +8 -0
  129. package/mcp.js +123 -0
  130. package/mcp.js.map +1 -0
  131. package/mcp.ts +141 -0
  132. package/mcp_auth.js +63 -0
  133. package/mcp_auth.js.map +1 -0
  134. package/mcp_auth.ts +91 -0
  135. package/mcp_authorize.js +17 -0
  136. package/mcp_authorize.js.map +1 -0
  137. package/mcp_authorize.ts +18 -0
  138. package/mcp_registry.js +178 -0
  139. package/mcp_registry.js.map +1 -0
  140. package/mcp_registry.ts +193 -0
  141. package/mod.js +43 -0
  142. package/mod.js.map +1 -0
  143. package/mod.ts +45 -0
  144. package/model_selection.js +72 -0
  145. package/model_selection.js.map +1 -0
  146. package/model_selection.ts +86 -0
  147. package/numbers.js +6 -0
  148. package/numbers.js.map +1 -0
  149. package/numbers.ts +8 -0
  150. package/package.json +106 -4
  151. package/panels.js +119 -0
  152. package/panels.js.map +1 -0
  153. package/panels.ts +259 -0
  154. package/patch.js +329 -0
  155. package/patch.js.map +1 -0
  156. package/patch.ts +325 -0
  157. package/perf.ts +65 -0
  158. package/providers.js +36 -0
  159. package/providers.js.map +1 -0
  160. package/providers.ts +53 -0
  161. package/remote.js +441 -0
  162. package/remote.js.map +1 -0
  163. package/remote.ts +446 -0
  164. package/results.js +49 -0
  165. package/results.js.map +1 -0
  166. package/results.ts +39 -0
  167. package/routes.js +64 -0
  168. package/routes.js.map +1 -0
  169. package/routes.ts +87 -0
  170. package/runs.js +478 -0
  171. package/runs.js.map +1 -0
  172. package/runs.ts +479 -0
  173. package/runtime/vocab.json +36 -0
  174. package/runtime.js +105 -0
  175. package/runtime.js.map +1 -0
  176. package/runtime.ts +112 -0
  177. package/session_machines.js +203 -0
  178. package/session_machines.js.map +1 -0
  179. package/session_machines.ts +216 -0
  180. package/sheet.js +55 -0
  181. package/sheet.js.map +1 -0
  182. package/sheet.ts +31 -0
  183. package/shutdown.js +7 -0
  184. package/shutdown.js.map +1 -0
  185. package/shutdown.ts +7 -0
  186. package/skills.js +116 -0
  187. package/skills.js.map +1 -0
  188. package/skills.ts +114 -0
  189. package/startup_bench.ts +140 -0
  190. package/status.js +82 -0
  191. package/status.js.map +1 -0
  192. package/status.ts +97 -0
  193. package/steps.baseline.json +28 -0
  194. package/steps_bench.ts +155 -0
  195. package/store.js +31 -0
  196. package/store.js.map +1 -0
  197. package/store.ts +82 -0
  198. package/streaming.js +3 -0
  199. package/streaming.js.map +1 -0
  200. package/streaming.ts +8 -0
  201. package/streaming_bench.ts +76 -0
  202. package/switch_bench.ts +97 -0
  203. package/testing.ts +177 -0
  204. package/tools.js +287 -0
  205. package/tools.js.map +1 -0
  206. package/tools.ts +313 -0
  207. package/transcript.js +149 -0
  208. package/transcript.js.map +1 -0
  209. package/transcript.ts +187 -0
  210. package/tree.js +97 -0
  211. package/tree.js.map +1 -0
  212. package/tree.ts +114 -0
  213. package/unfinished.js +186 -0
  214. package/unfinished.js.map +1 -0
  215. package/unfinished.ts +198 -0
  216. package/view.js +92 -0
  217. package/view.js.map +1 -0
  218. package/view.ts +102 -0
  219. package/vocab.js +23 -0
  220. package/vocab.js.map +1 -0
  221. package/vocab.json +494 -0
  222. package/vocab.ts +22 -0
  223. package/window_bench.ts +114 -0
  224. package/worker_bench.ts +72 -0
  225. package/workspace.js +21 -0
  226. package/workspace.js.map +1 -0
  227. package/workspace.ts +17 -0
  228. package/worn.js +194 -0
  229. package/worn.js.map +1 -0
  230. package/worn.ts +246 -0
package/effects.js ADDED
@@ -0,0 +1,233 @@
1
+ // The code behind `session_run` where a `yak` host lists the harness,
2
+ // exported as `@yaks/harness/effects`: the runner (@yaks/session `running`)
3
+ // lent this machine, as the harness on its own lends it (./local.ts `here`).
4
+ // @yaks/session declares the effect and knows nothing of a machine; the
5
+ // harness handles it because the machine is the harness's to lend. What the
6
+ // box opened is let go after the host's admitted work drains.
7
+ //
8
+ // `session_start` is handled here too (@yaks/session `starting`): a session
9
+ // started on a person's comment wears the persona of the task's lineage and
10
+ // asks what the task's project says its work runs on (./default.ts
11
+ // `usingFor`): the project's own `using{provider, model}`, and the harness's
12
+ // default model where the project names none.
13
+ import { promptEntry } from "@yaks/context";
14
+ import { derivedEid, Refused } from "@yaks/graph";
15
+ import { line, sub } from "@yaks/query";
16
+ import { openingFor } from "./instructions.js";
17
+ import { recordedWorkspace } from "@yaks/machine/recorded";
18
+ import { SandboxReleased } from "@yaks/sandbox";
19
+ import { recall, running, starting } from "@yaks/session";
20
+ import { usingFor } from "./default.js";
21
+ import { lend } from "./agent.js";
22
+ import { here } from "./local.js";
23
+ import { hosted } from "./store.js";
24
+ import { machines } from "./session_machines.js";
25
+ import { exposed, isolate, snapshot } from "./isolated.js";
26
+ // The isolation config names. A sandbox needs a directory, limits and a
27
+ // source; without them an exposed session has its host tools alone.
28
+ let isolation = async (config = {})=>{
29
+ let { dir, limits, source } = config;
30
+ if (!dir || !limits || !source) {
31
+ return {
32
+ tools: config.tools,
33
+ landing: config.landing
34
+ };
35
+ }
36
+ let { sandboxProvider } = await import("@yaks/sandbox/bubblewrap");
37
+ return {
38
+ tools: config.tools,
39
+ landing: config.landing,
40
+ sandbox: sandboxProvider({
41
+ dir,
42
+ archive: config.archive,
43
+ limits,
44
+ binds: config.binds,
45
+ env: ()=>({
46
+ ...config.env
47
+ }),
48
+ ...snapshot(source, {
49
+ setup: config.setup,
50
+ env: config.env,
51
+ dir: `${dir}/.layers`
52
+ })
53
+ })
54
+ };
55
+ };
56
+ /** The runner, lent this machine, over the host's graph. The plugin's options
57
+ * may name `compactAt`, the share of its model's context window a transcript
58
+ * fills before it is compacted (half where absent), and `starts`, the most
59
+ * sessions started on comments at work at once (two where absent); the model
60
+ * a session asks by default is graph data (./default.ts `defaultUsing`), and
61
+ * so is the one a project's work runs on (`usingFor`). `isolation`
62
+ * ({@link IsolationConfig}) is what a session outside words started works
63
+ * with instead of the host's tools. */ export let effects = (host, options = {})=>{
64
+ // The providers the config names load before the first run is answered.
65
+ let made = here(hosted(host), {
66
+ env: options.env,
67
+ http: options.http,
68
+ compactAt: typeof options.compactAt == 'number' ? options.compactAt : undefined
69
+ });
70
+ let lent = made.then((here)=>here.lent);
71
+ host.defer(()=>lent.then((lent)=>lent.release?.(), ()=>{}));
72
+ let binding = machines(host.graph, host.sandboxes, host.resolveHome, host.workspaces);
73
+ let g = host.graph;
74
+ // A sandbox config that cannot be built is reported, and exposed sessions
75
+ // keep their host tools alone; every other session goes on as it was.
76
+ let isolationOf = isolation(options.isolation).catch(async (error)=>{
77
+ await made.then((h)=>h.report(error, {
78
+ phase: 'isolation'
79
+ }), ()=>{});
80
+ return {
81
+ tools: options.isolation?.tools
82
+ };
83
+ });
84
+ let workspaces = host.workspaces;
85
+ if (workspaces && options.isolation?.dir) {
86
+ host.workspaces = {
87
+ connect: async (id)=>{
88
+ let [row] = await g.get([
89
+ id
90
+ ], [
91
+ 'isolated_sandbox'
92
+ ]);
93
+ if (!row?.isolated_sandbox) return await workspaces.connect(id);
94
+ let { sandbox } = await isolationOf;
95
+ if (!sandbox) {
96
+ throw new Refused('The isolated workspace is unavailable.');
97
+ }
98
+ // A released sandbox refuses, as a released workspace row does.
99
+ let connection = await sandbox.connect({
100
+ id
101
+ }).catch((error)=>{
102
+ if (!(error instanceof SandboxReleased)) throw error;
103
+ throw new Refused('Workspace has been released: ' + id);
104
+ });
105
+ return recordedWorkspace(g, id, connection.workspace);
106
+ }
107
+ };
108
+ }
109
+ let isolated = isolationOf.then((i)=>isolate(g, i, host.fx.slots().some((s)=>s.id == 'shell' && !!s.run)));
110
+ let scoped = async (session, runner)=>(await isolated)(session, runner);
111
+ let entries = (session, q)=>g.read(line`.entry.session=${session}&${sub(q)}&.limit=1&.fields=entity.eid`);
112
+ // What a transcript opens with, admitted once before it is answered, when
113
+ // whoever began it wrote none: an HTTP fixer's instruction files (its home,
114
+ // its claimed task or project), and those of the workspace a session's home
115
+ // names, as any door writes one (a browser's chat names the persona, the
116
+ // topic and its first message, no more). The persona itself is not
117
+ // admitted: every ask is told its text and the topic's tree afresh, and
118
+ // offered the tools it contains then (./local.ts `wear`).
119
+ let opening = async (session)=>{
120
+ let [owner] = await g.get([
121
+ session
122
+ ]);
123
+ let s = owner?.session;
124
+ if (!s || s.ended || [
125
+ 'stopped',
126
+ 'failed'
127
+ ].includes(String(s.status))) return;
128
+ let fixer = !!owner.fixer && !owner.process && !owner.stop;
129
+ let persona = typeof s.persona == 'string' ? s.persona : undefined;
130
+ let topic = typeof s.topic == 'string' ? s.topic : undefined;
131
+ if (!fixer && !persona && !topic) return;
132
+ try {
133
+ if (fixer) {
134
+ // HTTP fixer requests use the native runner, not an outside CLI. An
135
+ // existing managed run keeps its original owner across config
136
+ // changes.
137
+ if (!owner.native) {
138
+ await g.apply([
139
+ {
140
+ entity: {
141
+ eid: session
142
+ },
143
+ native: {}
144
+ }
145
+ ], {
146
+ trusted: true
147
+ });
148
+ }
149
+ // An exposed fixer works in its sandbox, never a checkout of the
150
+ // host's.
151
+ if (!await exposed(g, session)) await binding.cwd(session);
152
+ [owner] = await g.get([
153
+ session
154
+ ]);
155
+ } else if ((await entries(session, line`.ask`)).length) return;
156
+ // Preparation is restartable. Admit each immutable source only once.
157
+ if ((await entries(session, line`.prompt`)).length) return;
158
+ let work = fixer ? (await g.read(line`.claim.session=${session}&.task&.limit=1`))[0]?.entity.eid ?? owner.filed?.project : undefined;
159
+ let { files, persona: worn } = await openingFor(g, {
160
+ ...owner.home
161
+ }, host.workspaces, {
162
+ persona,
163
+ work: work,
164
+ topic
165
+ });
166
+ let writes = [
167
+ // A persona its work chose is recorded, so each ask is told it.
168
+ ...worn && !persona ? [
169
+ {
170
+ entity: {
171
+ eid: session
172
+ },
173
+ session: {
174
+ persona: worn
175
+ }
176
+ }
177
+ ] : [],
178
+ ...files.map((file)=>promptEntry(session, undefined, file.body, file.source, 'shared', file.revision))
179
+ ];
180
+ if (writes.length) await g.apply(writes, {
181
+ trusted: true
182
+ });
183
+ } catch (error) {
184
+ // Configuration cannot improve by repeating the same preparation.
185
+ // Keep a terminal answer in the transcript, also seen after restart.
186
+ if (!(error instanceof Refused)) throw error;
187
+ let [last] = await g.read(line`.entry.session=${session}&.order=-entry.seq&.limit=1`);
188
+ await g.apply([
189
+ {
190
+ entity: {
191
+ eid: derivedEid(`${session}:binding-refusal:${last.entity.eid}`)
192
+ },
193
+ entry: {
194
+ session
195
+ },
196
+ refusal: {
197
+ code: 'context'
198
+ },
199
+ content: {
200
+ body: error.message
201
+ }
202
+ }
203
+ ], {
204
+ trusted: true
205
+ });
206
+ }
207
+ };
208
+ return {
209
+ ...running(g, lent.then((lent)=>{
210
+ let runner = lend(lent);
211
+ return {
212
+ ...runner,
213
+ answers: async (using, session)=>{
214
+ if (!await runner.answers?.(using, session)) return false;
215
+ if (session) await opening(session);
216
+ return true;
217
+ },
218
+ stopping: host.stopping
219
+ };
220
+ }), scoped),
221
+ ...starting(g, {
222
+ beside: recall(host).beside,
223
+ limit: typeof options.starts == 'number' ? options.starts : undefined,
224
+ opening: async (work)=>({
225
+ persona: (await openingFor(g, {}, host.workspaces, {
226
+ work
227
+ })).persona,
228
+ using: await usingFor(g, work)
229
+ })
230
+ })
231
+ };
232
+ };
233
+ //# sourceMappingURL=effects.js.map
package/effects.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"effects.js","sources":["./effects.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,4EAA4E;AAC5E,6EAA6E;AAC7E,wEAAwE;AACxE,4EAA4E;AAC5E,8DAA8D;AAC9D,EAAE;AACF,4EAA4E;AAC5E,4EAA4E;AAC5E,mEAAmE;AACnE,6EAA6E;AAC7E,8CAA8C;AAE9C,SAAS,WAAW,6BAAkC;AACtD,SAAoB,UAAU,EAAE,OAAO,2BAAgC;AACvE,SAAqB,IAAI,EAAE,GAAG,2BAAgC;AAC9D,SAAS,UAAU,4BAA2B;AAG9C,SAAS,iBAAiB,sCAA2C;AACrE,SAAS,eAAe,6BAAkC;AAE1D,SAAS,MAAM,EAAe,OAAO,EAAE,QAAQ,6BAAkC;AACjF,SAAS,QAAQ,uBAAsB;AACvC,SAAS,IAAI,qBAAoB;AACjC,SAAS,IAAI,qBAA+B;AAC5C,SAAS,MAAM,qBAAoB;AACnC,SAAS,QAAQ,gCAA+B;AAChD,SACE,OAAO,EACP,OAAO,EAGP,QAAQ,wBACY;AAoBtB,wEAAwE;AACxE,oEAAoE;AACpE,IAAI,YAAY,OAAO,SAA0B,CAAC,CAAC;EACjD,IAAI,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG;EAC9B,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ;IAC9B,OAAO;MAAE,OAAO,OAAO,KAAK;MAAE,SAAS,OAAO,OAAO;IAAC;EACxD;EACA,IAAI,EAAE,eAAe,EAAE,GAAG,MAAM,MAAM,CAAC;EACvC,OAAO;IACL,OAAO,OAAO,KAAK;IACnB,SAAS,OAAO,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ts ADDED
@@ -0,0 +1,250 @@
1
+ // The code behind `session_run` where a `yak` host lists the harness,
2
+ // exported as `@yaks/harness/effects`: the runner (@yaks/session `running`)
3
+ // lent this machine, as the harness on its own lends it (./local.ts `here`).
4
+ // @yaks/session declares the effect and knows nothing of a machine; the
5
+ // harness handles it because the machine is the harness's to lend. What the
6
+ // box opened is let go after the host's admitted work drains.
7
+ //
8
+ // `session_start` is handled here too (@yaks/session `starting`): a session
9
+ // started on a person's comment wears the persona of the task's lineage and
10
+ // asks what the task's project says its work runs on (./default.ts
11
+ // `usingFor`): the project's own `using{provider, model}`, and the harness's
12
+ // default model where the project names none.
13
+
14
+ import { promptEntry } from '@yaks/context'
15
+ import { type Comp, derivedEid, Refused } from '@yaks/graph'
16
+ import { type Built, line, sub } from '@yaks/query'
17
+ import { openingFor } from './instructions.js'
18
+ import type { Handlers } from 'jsr:@yaks/effects@0.2.36'
19
+ import type { Host } from 'jsr:@yaks/host@0.2.36'
20
+ import { recordedWorkspace } from '@yaks/machine/recorded'
21
+ import { SandboxReleased } from '@yaks/sandbox'
22
+
23
+ import { recall, type Runner, running, starting } from '@yaks/session'
24
+ import { usingFor } from './default.js'
25
+ import { lend } from './agent.js'
26
+ import { here, type Opts } from './local.js'
27
+ import { hosted } from './store.js'
28
+ import { machines } from './session_machines.js'
29
+ import {
30
+ exposed,
31
+ isolate,
32
+ type Isolation,
33
+ type Setup,
34
+ snapshot,
35
+ } from './isolated.js'
36
+
37
+ /** What config says an exposed session's sandbox is (./isolated.ts): a
38
+ * bubblewrap sandbox under `dir` with `limits`, the read-only `binds` and
39
+ * the command `env`, holding the committed HEAD of `source` over what `setup`
40
+ * made of it, shared read-only (kept under `dir`'s `.layers`); the host
41
+ * `tools` it is offered by name; and the tools its lander is offered
42
+ * (`landing`). */
43
+ export type IsolationConfig = {
44
+ dir?: string
45
+ archive?: string
46
+ limits?: { cpuQuota: number; memoryMax: number; tasksMax: number }
47
+ binds?: string[]
48
+ env?: Record<string, string>
49
+ source?: string
50
+ setup?: Setup
51
+ tools?: string[]
52
+ landing?: string[]
53
+ }
54
+
55
+ // The isolation config names. A sandbox needs a directory, limits and a
56
+ // source; without them an exposed session has its host tools alone.
57
+ let isolation = async (config: IsolationConfig = {}): Promise<Isolation> => {
58
+ let { dir, limits, source } = config
59
+ if (!dir || !limits || !source) {
60
+ return { tools: config.tools, landing: config.landing }
61
+ }
62
+ let { sandboxProvider } = await import('@yaks/sandbox/bubblewrap')
63
+ return {
64
+ tools: config.tools,
65
+ landing: config.landing,
66
+ sandbox: sandboxProvider({
67
+ dir,
68
+ archive: config.archive,
69
+ limits,
70
+ binds: config.binds,
71
+ env: () => ({ ...config.env }),
72
+ ...snapshot(source, {
73
+ setup: config.setup,
74
+ env: config.env,
75
+ dir: `${dir}/.layers`,
76
+ }),
77
+ }),
78
+ }
79
+ }
80
+
81
+ /** The runner, lent this machine, over the host's graph. The plugin's options
82
+ * may name `compactAt`, the share of its model's context window a transcript
83
+ * fills before it is compacted (half where absent), and `starts`, the most
84
+ * sessions started on comments at work at once (two where absent); the model
85
+ * a session asks by default is graph data (./default.ts `defaultUsing`), and
86
+ * so is the one a project's work runs on (`usingFor`). `isolation`
87
+ * ({@link IsolationConfig}) is what a session outside words started works
88
+ * with instead of the host's tools. */
89
+ export let effects = (
90
+ host: Host,
91
+ options: Record<string, unknown> & Pick<Opts, 'env' | 'http'> & {
92
+ isolation?: IsolationConfig
93
+ } = {},
94
+ ): Handlers => {
95
+ // The providers the config names load before the first run is answered.
96
+ let made = here(hosted(host), {
97
+ env: options.env,
98
+ http: options.http,
99
+ compactAt: typeof options.compactAt == 'number'
100
+ ? options.compactAt
101
+ : undefined,
102
+ })
103
+ let lent = made.then((here) => here.lent)
104
+ host.defer(() => lent.then((lent) => lent.release?.(), () => {}))
105
+ let binding = machines(
106
+ host.graph,
107
+ host.sandboxes,
108
+ host.resolveHome,
109
+ host.workspaces,
110
+ )
111
+ let g = host.graph
112
+ // A sandbox config that cannot be built is reported, and exposed sessions
113
+ // keep their host tools alone; every other session goes on as it was.
114
+ let isolationOf = isolation(options.isolation).catch(async (error) => {
115
+ await made.then((h) => h.report(error, { phase: 'isolation' }), () => {})
116
+ return { tools: options.isolation?.tools }
117
+ }) as Promise<Isolation>
118
+ let workspaces = host.workspaces
119
+ if (workspaces && options.isolation?.dir) {
120
+ host.workspaces = {
121
+ connect: async (id) => {
122
+ let [row] = await g.get([id], ['isolated_sandbox'])
123
+ if (!row?.isolated_sandbox) return await workspaces.connect(id)
124
+ let { sandbox } = await isolationOf
125
+ if (!sandbox) {
126
+ throw new Refused('The isolated workspace is unavailable.')
127
+ }
128
+ // A released sandbox refuses, as a released workspace row does.
129
+ let connection = await sandbox.connect({ id }).catch((error) => {
130
+ if (!(error instanceof SandboxReleased)) throw error
131
+ throw new Refused('Workspace has been released: ' + id)
132
+ })
133
+ return recordedWorkspace(g, id, connection.workspace)
134
+ },
135
+ }
136
+ }
137
+ let isolated = isolationOf.then((i) =>
138
+ isolate(g, i, host.fx.slots().some((s) => s.id == 'shell' && !!s.run))
139
+ )
140
+ let scoped = async (session: string, runner: Runner) =>
141
+ (await isolated)(session, runner)
142
+ let entries = (session: string, q: Built) =>
143
+ g.read(
144
+ line`.entry.session=${session}&${sub(q)}&.limit=1&.fields=entity.eid`,
145
+ )
146
+ // What a transcript opens with, admitted once before it is answered, when
147
+ // whoever began it wrote none: an HTTP fixer's instruction files (its home,
148
+ // its claimed task or project), and those of the workspace a session's home
149
+ // names, as any door writes one (a browser's chat names the persona, the
150
+ // topic and its first message, no more). The persona itself is not
151
+ // admitted: every ask is told its text and the topic's tree afresh, and
152
+ // offered the tools it contains then (./local.ts `wear`).
153
+ let opening = async (session: string) => {
154
+ let [owner] = await g.get([session])
155
+ let s = owner?.session as Comp | undefined
156
+ if (
157
+ !s || s.ended || ['stopped', 'failed'].includes(String(s.status))
158
+ ) return
159
+ let fixer = !!owner.fixer && !owner.process && !owner.stop
160
+ let persona = typeof s.persona == 'string' ? s.persona : undefined
161
+ let topic = typeof s.topic == 'string' ? s.topic : undefined
162
+ if (!fixer && !persona && !topic) return
163
+ try {
164
+ if (fixer) {
165
+ // HTTP fixer requests use the native runner, not an outside CLI. An
166
+ // existing managed run keeps its original owner across config
167
+ // changes.
168
+ if (!owner.native) {
169
+ await g.apply([{ entity: { eid: session }, native: {} }], {
170
+ trusted: true,
171
+ })
172
+ }
173
+ // An exposed fixer works in its sandbox, never a checkout of the
174
+ // host's.
175
+ if (!await exposed(g, session)) await binding.cwd(session)
176
+ ;[owner] = await g.get([session])
177
+ } else if ((await entries(session, line`.ask`)).length) return
178
+ // Preparation is restartable. Admit each immutable source only once.
179
+ if ((await entries(session, line`.prompt`)).length) return
180
+ let work = fixer
181
+ ? (await g.read(line`.claim.session=${session}&.task&.limit=1`))[0]
182
+ ?.entity.eid ?? (owner.filed as Comp | undefined)?.project
183
+ : undefined
184
+ let { files, persona: worn } = await openingFor(
185
+ g,
186
+ { ...owner.home as Comp | undefined },
187
+ host.workspaces,
188
+ { persona, work: work as string | undefined, topic },
189
+ )
190
+ let writes = [
191
+ // A persona its work chose is recorded, so each ask is told it.
192
+ ...worn && !persona
193
+ ? [{ entity: { eid: session }, session: { persona: worn } }]
194
+ : [],
195
+ ...files.map((file) =>
196
+ promptEntry(
197
+ session,
198
+ undefined,
199
+ file.body,
200
+ file.source,
201
+ 'shared',
202
+ file.revision,
203
+ )
204
+ ),
205
+ ]
206
+ if (writes.length) await g.apply(writes, { trusted: true })
207
+ } catch (error) {
208
+ // Configuration cannot improve by repeating the same preparation.
209
+ // Keep a terminal answer in the transcript, also seen after restart.
210
+ if (!(error instanceof Refused)) throw error
211
+ let [last] = await g.read(
212
+ line`.entry.session=${session}&.order=-entry.seq&.limit=1`,
213
+ )
214
+ await g.apply([{
215
+ entity: {
216
+ eid: derivedEid(`${session}:binding-refusal:${last.entity.eid}`),
217
+ },
218
+ entry: { session },
219
+ refusal: { code: 'context' },
220
+ content: { body: error.message },
221
+ }], { trusted: true })
222
+ }
223
+ }
224
+ return {
225
+ ...running(
226
+ g,
227
+ lent.then((lent) => {
228
+ let runner = lend(lent)
229
+ return {
230
+ ...runner,
231
+ answers: async (using, session) => {
232
+ if (!await runner.answers?.(using, session)) return false
233
+ if (session) await opening(session)
234
+ return true
235
+ },
236
+ stopping: host.stopping,
237
+ }
238
+ }),
239
+ scoped,
240
+ ),
241
+ ...starting(g, {
242
+ beside: recall(host).beside,
243
+ limit: typeof options.starts == 'number' ? options.starts : undefined,
244
+ opening: async (work) => ({
245
+ persona: (await openingFor(g, {}, host.workspaces, { work })).persona,
246
+ using: await usingFor(g, work),
247
+ }),
248
+ }),
249
+ }
250
+ }