@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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"input_text.js","sources":["./input_text.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAElF,SAAS,IAAI,2BAAgC;AAC7C,SAAS,KAAK,wBAA6B;AAC3C,SACE,YAAY,EAEZ,MAAM,EACN,KAAK,EACL,MAAM,EACN,MAAM,6BACyB;AAEjC,IAAI,OAAO,CAAC,IAAe,EAAE,OAAO,IAAI,CAAC;AACzC,IAAI,SAAS,CAAC,IAAe,EAAE,KAAK,EAAuB;AAE3D;;;;iEAIiE,GACjE,OAAO,IAAI,YAAY,CACrB,YACA,QAAQ,IAAI,KAAkB,EAC9B,aACA;EAEA,IAAI,OAAO,IAAI,IAAI,WAAW,GAAG,CAAC,CAAC,IAAM;MAAC,EAAE,MAAM,CAAC,GAAG;MAAE;KAAE;EAC1D,IAAI,UAAU,WAAW,OAAO,CAAC,CAAC;IAChC,IAAI,OAAO,MAAM,YAAY,CAAC,EAAE,OAAO,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE;IAC9D,IAAI,MAAM,KAAK,GAAG,CAAC,OAAQ,EAAE,MAAM,EAAW;IAC9C,IAAI,UAAU,OAAO,KAAK,GAAG,CAAC,OAAQ,IAAI,GAAG,EAAW;IACxD,OAAO,OAAO,UAAU;MAAC;QAAE;QAAK,SAAS,MAAM;MAAS;KAAE,GAAG,EAAE;EACjE;EACA,OAAO,CAAC;IACN,IAAI,WAAY,MAAM,KAAK,EAA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G,EAAE;EACxC;EACA,IAAI,WAAW,IAAI,IACjB,CAAC,KAAK,IAAI,GAAG,MAAM,MAAM,QAAQ,EAAE,EAAE,GAAG,CAAC,CAAC,IAAM;MAAC,EAAE,MAAM,CAAC,GAAG;MAAE;KAAE;EAEnE,IAAI,KAAK,MAAM,EAAE,KAAK;EACtB,IAAI,QAAQ,CAAC;IACX,IAAI,QAAS,EAAE,GAAG,EAAuB;IACzC,OAAO,QAAQ,GAAG,MAAM,EAAE,EAAE,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG;EAC5C;EACA,OAAO,IAAI,IAAI,KAAK,GAAG,CAAC,CAAC;IACvB,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO;MAAC,EAAE,MAAM,CAAC,GAAG;MAAE,MAAM;KAAG;IAC/C,IAAI,IAAI,SAAS,GAAG,CAAC,OAAO,AAAC,EAAE,OAAO,CAAU,OAAO;IACvD,OAAO;MAAC,EAAE,MAAM,CAAC,GAAG;MAAE,IAAI,GAAG,GAAG,GAAG,SAAS,EAAE,MAAM,IAAI,GAAG,GAAG;KAAG;EACnE;AACF,EAAC"}
package/input_text.ts ADDED
@@ -0,0 +1,99 @@
1
+ // Input prose, sender identities and timing, kept separate until prompt assembly.
2
+ import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
3
+ import { line } from '@yaks/query'
4
+ import { human } from '@yaks/id'
5
+ import {
6
+ attemptState,
7
+ type InputText,
8
+ kindOf,
9
+ seqOf,
10
+ source,
11
+ textOf,
12
+ } from '@yaks/session'
13
+
14
+ let made = (b: Bundle) => (b.created ?? {}) as Comp
15
+ let selfOf = (b: Bundle) => (b.entry as Comp | undefined)?.session
16
+
17
+ /** Each input of a transcript as it is sent: who sent it, where `names`
18
+ * ({@link senders}) knows them, and its sent time beside completion of the
19
+ * reply to the preceding input, even when that input arrived while the reply
20
+ * was streaming. Never use output creation: streaming creates it before the
21
+ * reply is finished. The replies are found once per transcript. */
22
+ export let inputText = (
23
+ transcript: Bundle[],
24
+ names = new Map<Eid, string>(),
25
+ inputSource?: (entry: Bundle) => string | undefined,
26
+ inputBody?: (entry: Bundle, body: string) => string,
27
+ ) => {
28
+ let byId = new Map(transcript.map((b) => [b.entity.eid, b]))
29
+ let replies = transcript.flatMap((b) => {
30
+ if (kindOf(b) != 'output' || !b.content || b.notice) return []
31
+ let ask = byId.get(String((b.output as Comp)?.source))
32
+ let through = ask && byId.get(String((ask.ask as Comp)?.through))
33
+ return ask && through ? [{ ask, through: seqOf(through) }] : []
34
+ })
35
+ return (entry: Bundle): Exclude<InputText, string> => {
36
+ let explicit = (entry.entry as Comp | undefined)?.from
37
+ let from = typeof explicit == 'string' && explicit.trim()
38
+ ? explicit
39
+ : inputSource?.(entry) ?? source(entry, names)
40
+ let body = textOf(entry)
41
+ let text = inputBody ? inputBody(entry, body) : body
42
+ let sent = made(entry).at
43
+ if (entry.notice || typeof sent != 'string') {
44
+ return { text, from }
45
+ }
46
+ let previous = replies.findLast((r) => r.through < seqOf(entry))
47
+ let ask = previous?.ask
48
+ let ended = ask && attemptState(ask) == 'completed'
49
+ ? (ask.updated as Comp | undefined)?.at
50
+ : undefined
51
+ return {
52
+ from,
53
+ text:
54
+ `[Sent ${sent}; previous reply ended ${
55
+ ended ?? (previous ? 'unknown' : 'none')
56
+ }]\n` + text,
57
+ }
58
+ }
59
+ }
60
+
61
+ /** The names of who sent a transcript's inputs: a person or a persona by its
62
+ * title and id, another session by its id and the persona it wears. Only the
63
+ * writers and instruments of its inputs are read. */
64
+ export let senders = async (
65
+ g: Graph,
66
+ transcript: Bundle[],
67
+ ): Promise<Map<Eid, string>> => {
68
+ let ids = new Set<Eid>()
69
+ for (let b of transcript) {
70
+ if (kindOf(b) != 'input') continue
71
+ let { by, via } = made(b)
72
+ if (typeof by == 'string') ids.add(by)
73
+ if (typeof via == 'string' && via != selfOf(b)) ids.add(via)
74
+ }
75
+ // Read, not got: a read answers each row's number, which its id shows.
76
+ let named = (eids: Iterable<Eid>) =>
77
+ g.read(
78
+ line`.entity.eid=${[...eids]} ?person ?persona ?session ?doc`,
79
+ )
80
+ if (!ids.size) return new Map()
81
+ let rows = await named(ids)
82
+ let worn = new Set(rows.flatMap((b) => {
83
+ let p = (b.session as Comp | undefined)?.persona
84
+ return typeof p == 'string' ? [p] : []
85
+ }))
86
+ let personas = new Map(
87
+ (worn.size ? await named(worn) : []).map((b) => [b.entity.eid, b]),
88
+ )
89
+ let id = human(g.vocab)
90
+ let label = (b: Bundle) => {
91
+ let title = (b.doc as Comp | undefined)?.title
92
+ return title ? `${title} (${id(b)})` : id(b)
93
+ }
94
+ return new Map(rows.map((b): [Eid, string] => {
95
+ if (!b.session) return [b.entity.eid, label(b)]
96
+ let p = personas.get(String((b.session as Comp).persona))
97
+ return [b.entity.eid, p ? `${id(b)} wearing ${label(p)}` : id(b)]
98
+ }))
99
+ }
@@ -0,0 +1,111 @@
1
+ import { Refused } from "@yaks/graph/errors";
2
+ import { targetFor } from "@yaks/machine/targets";
3
+ import { snapshot } from "@yaks/context";
4
+ import { personaOf } from "./worn.js";
5
+ import { unsaid, wearing } from "@yaks/persona";
6
+ import { gather } from "@yaks/topic";
7
+ /** Each directory from the root down to `dir`, an absolute path. */ let ancestors = (dir)=>dir == '/' ? [
8
+ '/'
9
+ ] : [
10
+ ...ancestors(dir.slice(0, dir.lastIndexOf('/')) || '/'),
11
+ dir
12
+ ];
13
+ /** Root-to-leaf AGENTS.md files on a machine, with canonical source paths:
14
+ * the machine says what each candidate is, then its files are read. A link
15
+ * to nothing is a projection gone missing, kept in `missing` where given. */ export let instructionFiles = async (machine, cwd, home, missing)=>{
16
+ let [here] = await machine.stat([
17
+ cwd
18
+ ]);
19
+ if (here.kind != 'directory') {
20
+ throw new Error('Instruction discovery has no directory: ' + cwd);
21
+ }
22
+ let global = home && `${home.replace(/\/+$/, '')}/.agents/AGENTS.md`;
23
+ let paths = [
24
+ ...global ? [
25
+ global
26
+ ] : [],
27
+ ...ancestors(here.canonical).map((d)=>(d == '/' ? '' : d) + '/AGENTS.md')
28
+ ];
29
+ let found = await machine.stat(paths);
30
+ let lost = paths.filter((p, i)=>found[i].kind == 'dangling' && p != global);
31
+ if (lost.length && !missing) throw unavailable(lost);
32
+ missing?.push(...lost);
33
+ let files = [];
34
+ for (let [i, source] of paths.entries()){
35
+ let { kind } = found[i];
36
+ if (kind == 'missing' || kind == 'dangling') continue;
37
+ if (kind != 'file') {
38
+ throw new Error('Instruction file is not a regular file: ' + source);
39
+ }
40
+ files.push(await snapshot(await machine.read(source), source));
41
+ }
42
+ return files;
43
+ };
44
+ let unavailable = (paths)=>new Refused('Instruction projection is missing: ' + paths.join(', ') + '. Supply --persona or --work with the graph task or project.');
45
+ // The instruction files of the machine a home names, and the projections
46
+ // missing among them. A home naming no directory reads none.
47
+ let admitted = async (machine, home, missing)=>home.cwd ? await instructionFiles(machine, String(home.cwd), undefined, missing) : [];
48
+ /** The instruction files a session opening at `home` reads from its machine.
49
+ * Opening a graph-only session does not acquire any machine. An explicit
50
+ * ready workspace may be connected because instruction admission reads files.
51
+ * A projection gone missing is kept in `missing` where given, and refused
52
+ * where not. */ export let instructionsFor = async (g, home, workspaces, missing)=>{
53
+ if (!home.workspace) return [];
54
+ let [row] = await g.get([
55
+ String(home.workspace)
56
+ ]);
57
+ if (row?.pending) return [];
58
+ if (!workspaces) {
59
+ throw new Refused('This host lends no workspace execution binding');
60
+ }
61
+ let target = await targetFor(g, undefined, home);
62
+ home.cwd ??= target.cwd;
63
+ let lost = [];
64
+ let files = await admitted(await workspaces.connect(target.workspace), home, lost);
65
+ if (lost.length && !missing) throw unavailable(lost);
66
+ missing?.push(...lost);
67
+ return files;
68
+ };
69
+ // Who a session wears where nothing chose it a persona and neither its work
70
+ // nor its checkout carries one: the persona of the session that started it
71
+ // (./worn.ts `personaOf`), as a helper works as its starter does.
72
+ let starting = async (g, home, context)=>{
73
+ if (context.persona || !context.starter) return context;
74
+ if (await wearing(g, String(home.cwd ?? ''), context)) return context;
75
+ let persona = await personaOf(g, context.starter);
76
+ return persona ? {
77
+ ...context,
78
+ persona
79
+ } : context;
80
+ };
81
+ /** What a session opening here begins with: the instruction files of its
82
+ * machine and the persona it wears (@yaks/persona `wearing`, else its
83
+ * starter's).
84
+ *
85
+ * The persona's text, the topic's tree and the tools the persona offers are
86
+ * not among them: the runner reads all three afresh for every ask (./worn.ts
87
+ * `wornBy`, `offeredTo`), so a file that only projects the persona is left
88
+ * out too (@yaks/persona `unsaid`). A topic that
89
+ * is not there is refused, as a persona is, and so is a projection gone
90
+ * missing where nothing is worn. */ export let openingFor = async (g, home, workspaces, context = {})=>{
91
+ let as = await starting(g, home, context);
92
+ if (as.topic && !await gather(g)(as.topic)) {
93
+ throw new Refused(`no topic called ${as.topic}`);
94
+ }
95
+ let missing = [];
96
+ let files = await instructionsFor(g, home, workspaces, missing);
97
+ let cwd = String(home.cwd ?? '');
98
+ let worn = await wearing(g, cwd, as);
99
+ if (missing.length && !worn) throw unavailable(missing);
100
+ if (worn && files.length) {
101
+ let kept = await unsaid(g, cwd, files.map((f)=>f.body), as);
102
+ files = files.filter((f)=>kept.includes(f.body));
103
+ }
104
+ return {
105
+ files,
106
+ ...worn ? {
107
+ persona: worn.persona.entity.eid
108
+ } : {}
109
+ };
110
+ };
111
+ //# sourceMappingURL=instructions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instructions.js","sources":["./instructions.ts"],"names":[],"mappings":"AAAA,SAAS,OAAO,kCAAuC;AACvD,SAAS,SAAS,qCAA2D;AAI7E,SAAwB,QAAQ,6BAAkC;AAElE,SAAS,SAAS,oBAAmB;AACrC,SAAkB,MAAM,EAAE,OAAO,6BAAkC;AACnE,SAAS,MAAM,2BAAgC;AAE/C,kEAAkE,GAClE,IAAI,YAAY,CAAC,MACf,OAAO,MACH;IAAC;GAAI,GACL;OAAI,UAAU,IAAI,KAAK,CAAC,GAAG,IAAI,WAAW,CAAC,SAAS;IAAM;GAAI;AAEpE;;2EAE2E,GAC3E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OAAO,KAAK,GAAG,IAAI;EAC7B,IAAI,OAAO,MAAM,QAAQ,GAAG,KAAK;EACjC,IAAI,QAAQ,MAAM,IAAI,CAAC,MAAM,MAAM,YAAY;EAC/C,IAAI,QAAQ,MAAM,MAAM,EAAE;IACxB,IAAI,OAAO,MAAM,OAAO,GAAG,KAAK,MAAM,GAAG,CAAC,CAAC,IAAM,EAAE,IAAI,GAAG;IAC1D,QAAQ,MAAM,MAAM,CAAC,CAAC,IAAM,KAAK,QAAQ,CAAC,EAAE,IAAI;EAClD;EACA,OAAO;IAAE;IAAO,GAAG,OAAO;MAAE,SAAS,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG;IAAC,IAAI,CAAC,CAAC;EAAC;AACtE,EAAC"}
@@ -0,0 +1,145 @@
1
+ import { Refused } from '@yaks/graph/errors'
2
+ import { targetFor, type Workspaces } from '@yaks/machine/targets'
3
+ // Instruction admission reads only the explicitly named machine. The files
4
+ // become immutable prompt snapshots; later machine edits do not rewrite them.
5
+ import type { Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
6
+ import { type Snapshot, snapshot } from '@yaks/context'
7
+ import type { Machine } from 'jsr:@yaks/machine@0.2.36'
8
+ import { personaOf } from './worn.js'
9
+ import { type As, unsaid, wearing } from '@yaks/persona'
10
+ import { gather } from '@yaks/topic'
11
+
12
+ /** Each directory from the root down to `dir`, an absolute path. */
13
+ let ancestors = (dir: string): string[] =>
14
+ dir == '/'
15
+ ? ['/']
16
+ : [...ancestors(dir.slice(0, dir.lastIndexOf('/')) || '/'), dir]
17
+
18
+ /** Root-to-leaf AGENTS.md files on a machine, with canonical source paths:
19
+ * the machine says what each candidate is, then its files are read. A link
20
+ * to nothing is a projection gone missing, kept in `missing` where given. */
21
+ export let instructionFiles = async (
22
+ machine: Machine,
23
+ cwd: string,
24
+ home?: string,
25
+ missing?: string[],
26
+ ): Promise<Snapshot[]> => {
27
+ let [here] = await machine.stat([cwd])
28
+ if (here.kind != 'directory') {
29
+ throw new Error('Instruction discovery has no directory: ' + cwd)
30
+ }
31
+ let global = home && `${home.replace(/\/+$/, '')}/.agents/AGENTS.md`
32
+ let paths = [
33
+ ...global ? [global] : [],
34
+ ...ancestors(here.canonical).map((d) => (d == '/' ? '' : d) + '/AGENTS.md'),
35
+ ]
36
+ let found = await machine.stat(paths)
37
+ let lost = paths.filter((p, i) => found[i].kind == 'dangling' && p != global)
38
+ if (lost.length && !missing) throw unavailable(lost)
39
+ missing?.push(...lost)
40
+ let files: Snapshot[] = []
41
+ for (let [i, source] of paths.entries()) {
42
+ let { kind } = found[i]
43
+ if (kind == 'missing' || kind == 'dangling') continue
44
+ if (kind != 'file') {
45
+ throw new Error('Instruction file is not a regular file: ' + source)
46
+ }
47
+ files.push(await snapshot(await machine.read(source), source))
48
+ }
49
+ return files
50
+ }
51
+
52
+ let unavailable = (paths: string[]) =>
53
+ new Refused(
54
+ 'Instruction projection is missing: ' + paths.join(', ') +
55
+ '. Supply --persona or --work with the graph task or project.',
56
+ )
57
+
58
+ // The instruction files of the machine a home names, and the projections
59
+ // missing among them. A home naming no directory reads none.
60
+ let admitted = async (
61
+ machine: Machine,
62
+ home: Comp,
63
+ missing: string[],
64
+ ): Promise<Snapshot[]> =>
65
+ home.cwd
66
+ ? await instructionFiles(machine, String(home.cwd), undefined, missing)
67
+ : []
68
+
69
+ /** The instruction files a session opening at `home` reads from its machine.
70
+ * Opening a graph-only session does not acquire any machine. An explicit
71
+ * ready workspace may be connected because instruction admission reads files.
72
+ * A projection gone missing is kept in `missing` where given, and refused
73
+ * where not. */
74
+ export let instructionsFor = async (
75
+ g: Graph,
76
+ home: Comp,
77
+ workspaces?: Workspaces,
78
+ missing?: string[],
79
+ ): Promise<Snapshot[]> => {
80
+ if (!home.workspace) return []
81
+ let [row] = await g.get([String(home.workspace)])
82
+ if (row?.pending) return []
83
+ if (!workspaces) {
84
+ throw new Refused('This host lends no workspace execution binding')
85
+ }
86
+ let target = await targetFor(g, undefined, home)
87
+ home.cwd ??= target.cwd
88
+ let lost: string[] = []
89
+ let files = await admitted(
90
+ await workspaces.connect(target.workspace),
91
+ home,
92
+ lost,
93
+ )
94
+ if (lost.length && !missing) throw unavailable(lost)
95
+ missing?.push(...lost)
96
+ return files
97
+ }
98
+
99
+ /** Who a session is and what it is opened on: its persona or work (@yaks/persona
100
+ * `As`), the topic whose tree follows the persona's text on every ask, and the
101
+ * session that started it, whose persona it wears where nothing else gives
102
+ * one. */
103
+ export type On = As & { topic?: string; starter?: Eid }
104
+
105
+ // Who a session wears where nothing chose it a persona and neither its work
106
+ // nor its checkout carries one: the persona of the session that started it
107
+ // (./worn.ts `personaOf`), as a helper works as its starter does.
108
+ let starting = async (g: Graph, home: Comp, context: On): Promise<On> => {
109
+ if (context.persona || !context.starter) return context
110
+ if (await wearing(g, String(home.cwd ?? ''), context)) return context
111
+ let persona = await personaOf(g, context.starter)
112
+ return persona ? { ...context, persona } : context
113
+ }
114
+
115
+ /** What a session opening here begins with: the instruction files of its
116
+ * machine and the persona it wears (@yaks/persona `wearing`, else its
117
+ * starter's).
118
+ *
119
+ * The persona's text, the topic's tree and the tools the persona offers are
120
+ * not among them: the runner reads all three afresh for every ask (./worn.ts
121
+ * `wornBy`, `offeredTo`), so a file that only projects the persona is left
122
+ * out too (@yaks/persona `unsaid`). A topic that
123
+ * is not there is refused, as a persona is, and so is a projection gone
124
+ * missing where nothing is worn. */
125
+ export let openingFor = async (
126
+ g: Graph,
127
+ home: Comp,
128
+ workspaces?: Workspaces,
129
+ context: On = {},
130
+ ): Promise<{ files: Snapshot[]; persona?: Eid }> => {
131
+ let as = await starting(g, home, context)
132
+ if (as.topic && !await gather(g)(as.topic)) {
133
+ throw new Refused(`no topic called ${as.topic}`)
134
+ }
135
+ let missing: string[] = []
136
+ let files = await instructionsFor(g, home, workspaces, missing)
137
+ let cwd = String(home.cwd ?? '')
138
+ let worn = await wearing(g, cwd, as)
139
+ if (missing.length && !worn) throw unavailable(missing)
140
+ if (worn && files.length) {
141
+ let kept = await unsaid(g, cwd, files.map((f) => f.body), as)
142
+ files = files.filter((f) => kept.includes(f.body))
143
+ }
144
+ return { files, ...worn ? { persona: worn.persona.entity.eid } : {} }
145
+ }