@yaks/session 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.
- package/LICENSE +202 -0
- package/README.md +1392 -2
- package/_dist/admission.d.ts +15 -0
- package/_dist/admission.d.ts.map +1 -0
- package/_dist/announce.d.ts +17 -0
- package/_dist/announce.d.ts.map +1 -0
- package/_dist/answer.d.ts +200 -0
- package/_dist/answer.d.ts.map +1 -0
- package/_dist/append.d.ts +10 -0
- package/_dist/append.d.ts.map +1 -0
- package/_dist/archive.d.ts +6 -0
- package/_dist/archive.d.ts.map +1 -0
- package/_dist/asks.d.ts +11 -0
- package/_dist/asks.d.ts.map +1 -0
- package/_dist/audit.d.ts +15 -0
- package/_dist/audit.d.ts.map +1 -0
- package/_dist/bounce.d.ts +19 -0
- package/_dist/bounce.d.ts.map +1 -0
- package/_dist/bus.d.ts +8 -0
- package/_dist/bus.d.ts.map +1 -0
- package/_dist/cache.d.ts +45 -0
- package/_dist/cache.d.ts.map +1 -0
- package/_dist/child-notice.d.ts +6 -0
- package/_dist/child-notice.d.ts.map +1 -0
- package/_dist/children.d.ts +59 -0
- package/_dist/children.d.ts.map +1 -0
- package/_dist/cli.d.ts +8 -0
- package/_dist/cli.d.ts.map +1 -0
- package/_dist/comp.d.ts +9 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/compact.d.ts +19 -0
- package/_dist/compact.d.ts.map +1 -0
- package/_dist/conversation.d.ts +158 -0
- package/_dist/conversation.d.ts.map +1 -0
- package/_dist/cost.d.ts +13 -0
- package/_dist/cost.d.ts.map +1 -0
- package/_dist/deferred.d.ts +79 -0
- package/_dist/deferred.d.ts.map +1 -0
- package/_dist/ended.d.ts +27 -0
- package/_dist/ended.d.ts.map +1 -0
- package/_dist/files.d.ts +73 -0
- package/_dist/files.d.ts.map +1 -0
- package/_dist/fork.d.ts +23 -0
- package/_dist/fork.d.ts.map +1 -0
- package/_dist/graft.d.ts +33 -0
- package/_dist/graft.d.ts.map +1 -0
- package/_dist/graph.d.ts +14 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/handoff.d.ts +29 -0
- package/_dist/handoff.d.ts.map +1 -0
- package/_dist/imported.d.ts +63 -0
- package/_dist/imported.d.ts.map +1 -0
- package/_dist/intake.d.ts +43 -0
- package/_dist/intake.d.ts.map +1 -0
- package/_dist/joined.d.ts +35 -0
- package/_dist/joined.d.ts.map +1 -0
- package/_dist/lease.d.ts +21 -0
- package/_dist/lease.d.ts.map +1 -0
- package/_dist/lines.d.ts +13 -0
- package/_dist/lines.d.ts.map +1 -0
- package/_dist/mod.d.ts +107 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/naming.d.ts +10 -0
- package/_dist/naming.d.ts.map +1 -0
- package/_dist/native.d.ts +13 -0
- package/_dist/native.d.ts.map +1 -0
- package/_dist/parts.d.ts +145 -0
- package/_dist/parts.d.ts.map +1 -0
- package/_dist/plugin.d.ts +33 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/project.d.ts +67 -0
- package/_dist/project.d.ts.map +1 -0
- package/_dist/prose.d.ts +7 -0
- package/_dist/prose.d.ts.map +1 -0
- package/_dist/providers.d.ts +36 -0
- package/_dist/providers.d.ts.map +1 -0
- package/_dist/reach.d.ts +39 -0
- package/_dist/reach.d.ts.map +1 -0
- package/_dist/react.d.ts +236 -0
- package/_dist/react.d.ts.map +1 -0
- package/_dist/readers.d.ts +221 -0
- package/_dist/readers.d.ts.map +1 -0
- package/_dist/reap.d.ts +21 -0
- package/_dist/reap.d.ts.map +1 -0
- package/_dist/recall.d.ts +32 -0
- package/_dist/recall.d.ts.map +1 -0
- package/_dist/routes.d.ts +15 -0
- package/_dist/routes.d.ts.map +1 -0
- package/_dist/run.d.ts +81 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/service.d.ts +131 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/source.d.ts +9 -0
- package/_dist/source.d.ts.map +1 -0
- package/_dist/start.d.ts +29 -0
- package/_dist/start.d.ts.map +1 -0
- package/_dist/state.d.ts +75 -0
- package/_dist/state.d.ts.map +1 -0
- package/_dist/status.d.ts +38 -0
- package/_dist/status.d.ts.map +1 -0
- package/_dist/tail.d.ts +121 -0
- package/_dist/tail.d.ts.map +1 -0
- package/_dist/timing.d.ts +9 -0
- package/_dist/timing.d.ts.map +1 -0
- package/_dist/told.d.ts +17 -0
- package/_dist/told.d.ts.map +1 -0
- package/_dist/tools.d.ts +41 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/transcript.d.ts +5 -0
- package/_dist/transcript.d.ts.map +1 -0
- package/_dist/turn.d.ts +49 -0
- package/_dist/turn.d.ts.map +1 -0
- package/_dist/unknown.d.ts +6 -0
- package/_dist/unknown.d.ts.map +1 -0
- package/_dist/views.d.ts +9 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +9 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/wait.d.ts +15 -0
- package/_dist/wait.d.ts.map +1 -0
- package/_dist/wake.d.ts +116 -0
- package/_dist/wake.d.ts.map +1 -0
- package/_dist/watch.d.ts +52 -0
- package/_dist/watch.d.ts.map +1 -0
- package/_dist/who.d.ts +84 -0
- package/_dist/who.d.ts.map +1 -0
- package/_dist/window.d.ts +43 -0
- package/_dist/window.d.ts.map +1 -0
- package/admission.js +137 -0
- package/admission.js.map +1 -0
- package/admission.ts +164 -0
- package/announce.js +123 -0
- package/announce.js.map +1 -0
- package/announce.ts +126 -0
- package/answer.js +372 -0
- package/answer.js.map +1 -0
- package/answer.ts +426 -0
- package/append.js +125 -0
- package/append.js.map +1 -0
- package/append.ts +205 -0
- package/archive.js +62 -0
- package/archive.js.map +1 -0
- package/archive.ts +62 -0
- package/asks.js +80 -0
- package/asks.js.map +1 -0
- package/asks.ts +106 -0
- package/attempt.js +46 -0
- package/attempt.js.map +1 -0
- package/attempt.ts +60 -0
- package/audit.js +57 -0
- package/audit.js.map +1 -0
- package/audit.ts +65 -0
- package/bounce.js +29 -0
- package/bounce.js.map +1 -0
- package/bounce.ts +34 -0
- package/bus.js +568 -0
- package/bus.js.map +1 -0
- package/bus.ts +683 -0
- package/cache.js +169 -0
- package/cache.js.map +1 -0
- package/cache.ts +230 -0
- package/child-notice.js +128 -0
- package/child-notice.js.map +1 -0
- package/child-notice.ts +136 -0
- package/children.js +937 -0
- package/children.js.map +1 -0
- package/children.ts +925 -0
- package/claude-control.js +8 -0
- package/claude-control.js.map +1 -0
- package/claude-control.ts +12 -0
- package/cli.js +23 -0
- package/cli.js.map +1 -0
- package/cli.ts +28 -0
- package/comp.js +76 -0
- package/comp.js.map +1 -0
- package/comp.ts +78 -0
- package/compact.js +157 -0
- package/compact.js.map +1 -0
- package/compact.ts +209 -0
- package/compact_ask.js +280 -0
- package/compact_ask.js.map +1 -0
- package/compact_ask.ts +199 -0
- package/continue.js +131 -0
- package/continue.js.map +1 -0
- package/continue.ts +143 -0
- package/conversation.js +337 -0
- package/conversation.js.map +1 -0
- package/conversation.ts +593 -0
- package/cost.js +111 -0
- package/cost.js.map +1 -0
- package/cost.ts +93 -0
- package/deferred.js +207 -0
- package/deferred.js.map +1 -0
- package/deferred.ts +219 -0
- package/deno.json +1 -0
- package/dependencies.js +114 -0
- package/dependencies.js.map +1 -0
- package/dependencies.ts +110 -0
- package/draining.js +20 -0
- package/draining.js.map +1 -0
- package/draining.ts +26 -0
- package/ended.js +210 -0
- package/ended.js.map +1 -0
- package/ended.ts +225 -0
- package/files.js +285 -0
- package/files.js.map +1 -0
- package/files.ts +318 -0
- package/fork.js +96 -0
- package/fork.js.map +1 -0
- package/fork.ts +124 -0
- package/graft.js +97 -0
- package/graft.js.map +1 -0
- package/graft.ts +88 -0
- package/graph.js +49 -0
- package/graph.js.map +1 -0
- package/graph.ts +61 -0
- package/handoff.js +111 -0
- package/handoff.js.map +1 -0
- package/handoff.ts +147 -0
- package/help.js +432 -0
- package/help.js.map +1 -0
- package/help.ts +431 -0
- package/hooks.js +253 -0
- package/hooks.js.map +1 -0
- package/hooks.ts +333 -0
- package/imported.js +259 -0
- package/imported.js.map +1 -0
- package/imported.ts +201 -0
- package/inspect.js +353 -0
- package/inspect.js.map +1 -0
- package/inspect.ts +378 -0
- package/intake.js +115 -0
- package/intake.js.map +1 -0
- package/intake.ts +124 -0
- package/joined.js +67 -0
- package/joined.js.map +1 -0
- package/joined.ts +69 -0
- package/lease.js +146 -0
- package/lease.js.map +1 -0
- package/lease.ts +151 -0
- package/lifecycle.js +49 -0
- package/lifecycle.js.map +1 -0
- package/lifecycle.ts +46 -0
- package/lines.js +171 -0
- package/lines.js.map +1 -0
- package/lines.ts +177 -0
- package/listen.js +604 -0
- package/listen.js.map +1 -0
- package/listen.ts +976 -0
- package/log.js +78 -0
- package/log.js.map +1 -0
- package/log.ts +88 -0
- package/mod.js +107 -0
- package/mod.js.map +1 -0
- package/mod.ts +113 -0
- package/name.js +51 -0
- package/name.js.map +1 -0
- package/name.ts +84 -0
- package/naming.js +96 -0
- package/naming.js.map +1 -0
- package/naming.ts +102 -0
- package/native.js +17 -0
- package/native.js.map +1 -0
- package/native.ts +17 -0
- package/package.json +133 -4
- package/parts.js +334 -0
- package/parts.js.map +1 -0
- package/parts.ts +383 -0
- package/plugin.js +77 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +98 -0
- package/project.js +354 -0
- package/project.js.map +1 -0
- package/project.ts +407 -0
- package/prompt.js +57 -0
- package/prompt.js.map +1 -0
- package/prompt.ts +92 -0
- package/prose.js +43 -0
- package/prose.js.map +1 -0
- package/prose.ts +63 -0
- package/providers.js +279 -0
- package/providers.js.map +1 -0
- package/providers.ts +306 -0
- package/pruning.js +80 -0
- package/pruning.js.map +1 -0
- package/pruning.ts +147 -0
- package/reach.js +923 -0
- package/reach.js.map +1 -0
- package/reach.ts +1107 -0
- package/react.js +2259 -0
- package/react.js.map +1 -0
- package/react.ts +2864 -0
- package/readers.js +956 -0
- package/readers.js.map +1 -0
- package/readers.ts +1017 -0
- package/reap.js +60 -0
- package/reap.js.map +1 -0
- package/reap.ts +65 -0
- package/recall.js +67 -0
- package/recall.js.map +1 -0
- package/recall.ts +114 -0
- package/reopen.js +45 -0
- package/reopen.js.map +1 -0
- package/reopen.ts +44 -0
- package/result-entry.js +32 -0
- package/result-entry.js.map +1 -0
- package/result-entry.ts +34 -0
- package/resume.js +266 -0
- package/resume.js.map +1 -0
- package/resume.ts +283 -0
- package/routes.js +129 -0
- package/routes.js.map +1 -0
- package/routes.ts +147 -0
- package/routine.js +179 -0
- package/routine.js.map +1 -0
- package/routine.ts +191 -0
- package/run.js +932 -0
- package/run.js.map +1 -0
- package/run.ts +999 -0
- package/service.js +806 -0
- package/service.js.map +1 -0
- package/service.ts +911 -0
- package/source.js +26 -0
- package/source.js.map +1 -0
- package/source.ts +40 -0
- package/start.js +197 -0
- package/start.js.map +1 -0
- package/start.ts +214 -0
- package/state.js +247 -0
- package/state.js.map +1 -0
- package/state.ts +404 -0
- package/status.js +1222 -0
- package/status.js.map +1 -0
- package/status.ts +1555 -0
- package/status_bench.ts +50 -0
- package/statusline.js +187 -0
- package/statusline.js.map +1 -0
- package/statusline.ts +232 -0
- package/tail.js +748 -0
- package/tail.js.map +1 -0
- package/tail.ts +815 -0
- package/talk.js +58 -0
- package/talk.js.map +1 -0
- package/talk.ts +51 -0
- package/task-context.js +41 -0
- package/task-context.js.map +1 -0
- package/task-context.ts +53 -0
- package/texted.js +65 -0
- package/texted.js.map +1 -0
- package/texted.ts +65 -0
- package/timing.js +37 -0
- package/timing.js.map +1 -0
- package/timing.ts +52 -0
- package/told.js +56 -0
- package/told.js.map +1 -0
- package/told.ts +54 -0
- package/tools.js +595 -0
- package/tools.js.map +1 -0
- package/tools.ts +633 -0
- package/transcript.js +34 -0
- package/transcript.js.map +1 -0
- package/transcript.ts +41 -0
- package/turn.js +47 -0
- package/turn.js.map +1 -0
- package/turn.ts +68 -0
- package/unknown.js +8 -0
- package/unknown.js.map +1 -0
- package/unknown.ts +9 -0
- package/views.js +15 -0
- package/views.js.map +1 -0
- package/views.ts +21 -0
- package/vocab.js +24 -0
- package/vocab.js.map +1 -0
- package/vocab.json +1573 -0
- package/vocab.ts +30 -0
- package/wait.js +30 -0
- package/wait.js.map +1 -0
- package/wait.ts +45 -0
- package/wake.js +552 -0
- package/wake.js.map +1 -0
- package/wake.ts +597 -0
- package/watch.js +520 -0
- package/watch.js.map +1 -0
- package/watch.ts +596 -0
- package/who.js +309 -0
- package/who.js.map +1 -0
- package/who.ts +347 -0
- package/window.js +179 -0
- package/window.js.map +1 -0
- package/window.ts +268 -0
- package/woken.js +129 -0
- package/woken.js.map +1 -0
- package/woken.ts +127 -0
package/admission.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// The bound on children, kept in the graph. A spawned transcript waits
|
|
2
|
+
// `queued` until fewer than `maxChildren` are `active`; admission is a guarded
|
|
3
|
+
// write to its `dispatch`, so two workers admitting one child settle it in the
|
|
4
|
+
// transaction, and the write owes the child a run (./run.ts). A child waiting
|
|
5
|
+
// on its own children steps aside (`waiting`) so they can be admitted, and
|
|
6
|
+
// queues again for its place when the wait is over.
|
|
7
|
+
|
|
8
|
+
import type { Bundle, Comp, Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
9
|
+
import { Stale } from '@yaks/graph/errors'
|
|
10
|
+
import { token } from '@yaks/graph/sha256'
|
|
11
|
+
import { absent, and, eq, list, or, present, want } from '@yaks/query'
|
|
12
|
+
import type { ChildLimits } from './children.ts'
|
|
13
|
+
import { changes } from './wait.js'
|
|
14
|
+
|
|
15
|
+
let dispatch = (b: Bundle | undefined) => b?.dispatch as Comp | undefined
|
|
16
|
+
|
|
17
|
+
/** During expansion the old server's value wins. After conversion only marks
|
|
18
|
+
* remain; no dispatch means no place, not a queued root. */
|
|
19
|
+
export let dispatchStatus = (b: Bundle | undefined): unknown => {
|
|
20
|
+
let d = dispatch(b)
|
|
21
|
+
if (!d) return null
|
|
22
|
+
return d.state ??
|
|
23
|
+
(b?.waiting ? 'waiting' : b?.admitted ? 'active' : 'queued')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// What admission reads of a transcript: its place and the marks naming it,
|
|
27
|
+
// never the rest it wears (a session's computed status reads every entry).
|
|
28
|
+
// A narrow consumer's vocabulary may lack the marks.
|
|
29
|
+
let marks = (g: Graph) =>
|
|
30
|
+
['admitted', 'waiting'].filter((name) => g.vocab.comp(name))
|
|
31
|
+
|
|
32
|
+
let place = async (g: Graph, eid: Eid) =>
|
|
33
|
+
(await g.get([eid], ['dispatch', ...marks(g)]))[0]
|
|
34
|
+
|
|
35
|
+
// The children holding a place. A legacy row keeps its dispatch once settled,
|
|
36
|
+
// and those outnumber the rest many times over: the index on `dispatch.state`
|
|
37
|
+
// seeks past them, which a `!= 'settled'` would scan to the last.
|
|
38
|
+
let holders = async (g: Graph): Promise<Bundle[]> =>
|
|
39
|
+
await g.read(and(
|
|
40
|
+
present('dispatch'),
|
|
41
|
+
or(
|
|
42
|
+
absent('dispatch.state'),
|
|
43
|
+
eq('dispatch.state', list('queued', 'active', 'waiting')),
|
|
44
|
+
),
|
|
45
|
+
...marks(g).map(want),
|
|
46
|
+
))
|
|
47
|
+
|
|
48
|
+
// The children in `status`.
|
|
49
|
+
let placed = async (g: Graph, status: string): Promise<Bundle[]> =>
|
|
50
|
+
(await holders(g)).filter((b) => dispatchStatus(b) == status)
|
|
51
|
+
|
|
52
|
+
let state = async (g: Graph, eid: Eid) => dispatchStatus(await place(g, eid))
|
|
53
|
+
|
|
54
|
+
/** Patch a transcript's `dispatch` only if its state is still `was`. */
|
|
55
|
+
export let swap = async (
|
|
56
|
+
g: Graph,
|
|
57
|
+
eid: Eid,
|
|
58
|
+
was: unknown,
|
|
59
|
+
patch: Comp,
|
|
60
|
+
together: Bundle[] = [],
|
|
61
|
+
): Promise<boolean> => {
|
|
62
|
+
let b = await place(g, eid)
|
|
63
|
+
if (dispatchStatus(b) != was || !b?.dispatch) return false
|
|
64
|
+
let d = dispatch(b)!
|
|
65
|
+
let next = patch.state
|
|
66
|
+
// Narrow consumers may still load only the old session vocabulary. They
|
|
67
|
+
// keep writing the legacy shape; marks-only rows require both core marks.
|
|
68
|
+
let marks = g.vocab.comps.includes('admitted') &&
|
|
69
|
+
g.vocab.comps.includes('waiting')
|
|
70
|
+
try {
|
|
71
|
+
await g.apply([{
|
|
72
|
+
entity: { eid },
|
|
73
|
+
dispatch: next == 'settled' && d.state == null
|
|
74
|
+
? null
|
|
75
|
+
: { ...patch, ...(d.state == null ? { state: null } : {}) },
|
|
76
|
+
...(marks
|
|
77
|
+
? next == 'active'
|
|
78
|
+
? { admitted: {}, waiting: null }
|
|
79
|
+
: next == 'waiting'
|
|
80
|
+
? { admitted: null, waiting: {} }
|
|
81
|
+
: next == 'queued' || next == 'settled'
|
|
82
|
+
? { admitted: null, waiting: null }
|
|
83
|
+
: {}
|
|
84
|
+
: {}),
|
|
85
|
+
$was: {
|
|
86
|
+
dispatch: {
|
|
87
|
+
state: token(d.state ?? null),
|
|
88
|
+
order: token(d.order ?? null),
|
|
89
|
+
args: token(d.args ?? null),
|
|
90
|
+
},
|
|
91
|
+
...marks
|
|
92
|
+
? {
|
|
93
|
+
...Object.fromEntries(['admitted', 'waiting'].map((name) => [
|
|
94
|
+
name,
|
|
95
|
+
Object.fromEntries(['at', 'by', 'via'].map((prop) => [
|
|
96
|
+
prop,
|
|
97
|
+
token((b[name] as Comp)?.[prop]),
|
|
98
|
+
])),
|
|
99
|
+
])),
|
|
100
|
+
}
|
|
101
|
+
: {},
|
|
102
|
+
},
|
|
103
|
+
}, ...together], { trusted: true })
|
|
104
|
+
return true
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (error instanceof Stale) return false
|
|
107
|
+
throw error
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The children waiting for a place, oldest first. */
|
|
112
|
+
export let queue = async (g: Graph): Promise<Bundle[]> =>
|
|
113
|
+
(await placed(g, 'queued')).toSorted((a, b) =>
|
|
114
|
+
Number(dispatch(a)?.order ?? 0) - Number(dispatch(b)?.order ?? 0)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
/** The children holding a place. Their transcript may have just ended. */
|
|
118
|
+
export let activeChildren = (g: Graph): Promise<Bundle[]> => placed(g, 'active')
|
|
119
|
+
|
|
120
|
+
/** How many children hold a place. */
|
|
121
|
+
export let active = async (g: Graph): Promise<number> =>
|
|
122
|
+
(await activeChildren(g)).length
|
|
123
|
+
|
|
124
|
+
/** The order a child joining the queue takes: behind every other holding a
|
|
125
|
+
* place. A settled child's order no longer places anyone. */
|
|
126
|
+
export let tail = async (g: Graph): Promise<number> =>
|
|
127
|
+
Math.max(
|
|
128
|
+
0,
|
|
129
|
+
...(await holders(g)).map((b) => Number(dispatch(b)?.order ?? 0)),
|
|
130
|
+
) +
|
|
131
|
+
1
|
|
132
|
+
|
|
133
|
+
/** Admit the oldest queued children while fewer than the bound are active. */
|
|
134
|
+
export let admitNext = async (
|
|
135
|
+
g: Graph,
|
|
136
|
+
limits: ChildLimits = {},
|
|
137
|
+
): Promise<void> => {
|
|
138
|
+
let free = (limits.maxChildren ?? 32) - await active(g)
|
|
139
|
+
for (let b of (await queue(g)).slice(0, Math.max(0, free))) {
|
|
140
|
+
await swap(g, b.entity.eid, 'queued', { state: 'active' })
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Give up a child's place while it waits, and answer how to take one back:
|
|
145
|
+
* queue again and return once admitted, or once `signal` aborts. A transcript
|
|
146
|
+
* holding no place (a root) has nothing to give up. */
|
|
147
|
+
export let aside = async (
|
|
148
|
+
g: Graph,
|
|
149
|
+
session: Eid,
|
|
150
|
+
limits: ChildLimits,
|
|
151
|
+
signal?: AbortSignal,
|
|
152
|
+
): Promise<() => Promise<void>> => {
|
|
153
|
+
if (!await swap(g, session, 'active', { state: 'waiting' })) {
|
|
154
|
+
return () => Promise.resolve()
|
|
155
|
+
}
|
|
156
|
+
await admitNext(g, limits)
|
|
157
|
+
return async () => {
|
|
158
|
+
await swap(g, session, 'waiting', { state: 'queued' })
|
|
159
|
+
await admitNext(g, limits)
|
|
160
|
+
for await (let over of changes(g, { signal, clock: limits.clock })) {
|
|
161
|
+
if (over || await state(g, session) != 'queued') return
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
package/announce.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Which run makes each of a harness's tool calls, as the harness's hook said
|
|
2
|
+
// just before the call (Claude Code's `PreToolUse`, posted to the hook door,
|
|
3
|
+
// ./routes.ts). One harness process serves a session and all its subagents
|
|
4
|
+
// over one MCP connection, so a call reaching the MCP door says nothing of who
|
|
5
|
+
// made it except the harness's own id for the call, which Claude Code sends in
|
|
6
|
+
// the request's `_meta` (`claudecode/toolUseId`). The hook is the one report
|
|
7
|
+
// that pairs that id with the session and the subagent behind it.
|
|
8
|
+
//
|
|
9
|
+
// The graph keeps the pairing: a hook and its call can reach different
|
|
10
|
+
// processes, including either side of a ready-first restart. A harness waits
|
|
11
|
+
// on its hook only so long (./hooks.ts), then makes the call anyway, so a call
|
|
12
|
+
// can reach the door while this process is still hearing its hook and the
|
|
13
|
+
// pairing is not yet written. The door waits on that hearing rather than
|
|
14
|
+
// reading the graph too soon and writing as nobody.
|
|
15
|
+
import { identityEid } from "@yaks/graph/identity";
|
|
16
|
+
import { sessionFor, speaking, subagentSession } from "./who.js";
|
|
17
|
+
let announcement = (call)=>identityEid('announced', [
|
|
18
|
+
call
|
|
19
|
+
]);
|
|
20
|
+
// The calls whose `PreToolUse` this process is hearing, each to the run that
|
|
21
|
+
// makes it. A harness's call id is its own, so the id alone keys it.
|
|
22
|
+
let hearing = new Map();
|
|
23
|
+
/** Remember that `run` makes the harness's call `call`. */ export let announce = async (graph, call, run)=>{
|
|
24
|
+
await graph.apply([
|
|
25
|
+
{
|
|
26
|
+
entity: {
|
|
27
|
+
eid: announcement(call)
|
|
28
|
+
},
|
|
29
|
+
announced: {
|
|
30
|
+
call,
|
|
31
|
+
session: run
|
|
32
|
+
},
|
|
33
|
+
$actor: await speaking(graph, {
|
|
34
|
+
entity: {
|
|
35
|
+
eid: run
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
], {
|
|
40
|
+
trusted: true
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
/** The run that makes the harness's call `call`, where its hook said. */ export let announced = async (graph, call)=>{
|
|
44
|
+
let heard = await hearing.get(call)?.catch(()=>undefined);
|
|
45
|
+
if (heard) return heard;
|
|
46
|
+
let [row] = await graph.get([
|
|
47
|
+
announcement(call)
|
|
48
|
+
], [
|
|
49
|
+
'announced'
|
|
50
|
+
]);
|
|
51
|
+
let named = row?.announced;
|
|
52
|
+
return named?.session;
|
|
53
|
+
};
|
|
54
|
+
/** Forget a call whose result the harness reported. */ export let forget = async (graph, call)=>{
|
|
55
|
+
let run = await announced(graph, call);
|
|
56
|
+
if (!run) return;
|
|
57
|
+
await graph.apply([
|
|
58
|
+
{
|
|
59
|
+
entity: {
|
|
60
|
+
eid: announcement(call)
|
|
61
|
+
},
|
|
62
|
+
announced: null,
|
|
63
|
+
$actor: await speaking(graph, {
|
|
64
|
+
entity: {
|
|
65
|
+
eid: run
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
], {
|
|
70
|
+
trusted: true
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* The harness's id for the tool call a JSON-RPC message makes: Claude Code's
|
|
75
|
+
* `claudecode/toolUseId` in the message's `_meta`.
|
|
76
|
+
*
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { callIn } from '@yaks/session/graph'
|
|
79
|
+
* import { assertEquals } from '@std/assert'
|
|
80
|
+
*
|
|
81
|
+
* let message = {
|
|
82
|
+
* method: 'tools/call',
|
|
83
|
+
* params: { name: 'graph_show', _meta: { 'claudecode/toolUseId': 'toolu_1' } },
|
|
84
|
+
* }
|
|
85
|
+
* assertEquals(callIn(message), 'toolu_1')
|
|
86
|
+
* assertEquals(callIn({ method: 'tools/list' }), undefined)
|
|
87
|
+
* ```
|
|
88
|
+
*/ export let callIn = (message)=>{
|
|
89
|
+
let params = message?.params;
|
|
90
|
+
let id = params?._meta?.['claudecode/toolUseId'];
|
|
91
|
+
return typeof id == 'string' && id ? id : undefined;
|
|
92
|
+
};
|
|
93
|
+
let str = (v)=>typeof v == 'string' ? v : '';
|
|
94
|
+
/**
|
|
95
|
+
* Hear a hook event about a tool call: before the call (`PreToolUse`), the run
|
|
96
|
+
* that makes it is remembered, a subagent's session written where the graph
|
|
97
|
+
* has not met it; once a result is reported, the call is forgotten. Answers
|
|
98
|
+
* whether the event was a call's announcement, which asks for no answer.
|
|
99
|
+
*/ export let hear = async (graph, hook)=>{
|
|
100
|
+
let call = str(hook.tool_use_id);
|
|
101
|
+
if (!call) return false;
|
|
102
|
+
if (hook.hook_event_name != 'PreToolUse') {
|
|
103
|
+
await forget(graph, call);
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
let session = str(hook.session_id);
|
|
107
|
+
let agent = str(hook.agent_id);
|
|
108
|
+
let heard = (async ()=>{
|
|
109
|
+
let run = agent ? await subagentSession(graph, session, agent) : session ? (await sessionFor(graph, session, [
|
|
110
|
+
'session.id'
|
|
111
|
+
]))?.entity.eid : undefined;
|
|
112
|
+
if (run) await announce(graph, call, run);
|
|
113
|
+
return run;
|
|
114
|
+
})();
|
|
115
|
+
hearing.set(call, heard);
|
|
116
|
+
try {
|
|
117
|
+
await heard;
|
|
118
|
+
} finally{
|
|
119
|
+
if (hearing.get(call) == heard) hearing.delete(call);
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
};
|
|
123
|
+
//# sourceMappingURL=announce.js.map
|
package/announce.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"announce.js","sources":["./announce.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,6EAA6E;AAC7E,2EAA2E;AAC3E,+EAA+E;AAC/E,+EAA+E;AAC/E,6EAA6E;AAC7E,kEAAkE;AAClE,EAAE;AACF,uEAAuE;AACvE,6EAA6E;AAC7E,+EAA+E;AAC/E,0EAA0E;AAC1E,yEAAyE;AACzE,oDAAoD;AAGpD,SAAS,WAAW,oCAAyC;AAC7D,SAAS,UAAU,EAAE,QAAQ,EAAE,eAAe,mBAAkB;AAEhE,IAAI,eAAe,CAAC,OAAsB,YAAY,aAAa;IAAC;GAAK;AAEzE,6EAA6E;AAC7E,qEAAqE;AACrE,IAAI,UAAU,IAAI;AAElB,yDAAyD,GACzD,OAAO,IAAI,WAAW,OACpB,OACA,MACA;EAEA,MAAM,MAAM,KAAK,CAAC;IAAC;MACjB,QAAQ;QAAE,KAAK,aAAa;MAAM;MAClC,WAAW;QAAE;QAAM,SAAS;MAAI;MAChC,QAAQ,MAAM,SAAS,OAAO;QAAE,QAAQ;UAAE,KAAK;QAAI;MAAE;IACvD;GAAE,EAAE;IAAE,SAAS;EAAK;AACtB,EAAC;AAED,uEAAuE,GACvE,OAAO,IAAI,YAAY,OACrB,OACA;EAEA,IAAI,QAAQ,MAAM,QAAQ,GAAG,CAAC,OAAO,MAAM,IAAM;EACjD,IAAI,OAAO,OAAO;EAClB,IAAI,CAAC,IAAI,GAAG,MAAM,MAAM,GAAG,CAAC;IAAC,aAAa;GAAM,EAAE;IAAC;GAAY;EAC/D,IAAI,QAAQ,KAAK;EACjB,OAAO,OAAO;AAChB,EAAC;AAED,qDAAqD,GACrD,OAAO,IAAI,SAAS,OAAO,OAAc;EACvC,IAAI,MAAM,MAAM,UAAU,OAAO;EACjC,IAAI,CAAC,KAAK;EACV,MAAM,MAAM,KAAK,CAAC;IAAC;MACjB,QAAQ;QAAE,KAAK,aAAa;MAAM;MAClC,WAAW;MACX,QAAQ,MAAM,SAAS,OAAO;QAAE,QAAQ;UAAE,KAAK;QAAI;MAAE;IACvD;GAAE,EAAE;IAAE,SAAS;EAAK;AACtB,EAAC;AAED;;;;;;;;;;;;;;;CAeC,GACD,OAAO,IAAI,SAAS,CAAC;EACnB,IAAI,SAAU,SACV;EACJ,IAAI,KAAK,QAAQ,OAAO,CAAC,uBAAuB;EAChD,OAAO,OAAO,MAAM,YAAY,KAAK,KAAK;AAC5C,EAAC;AAUD,IAAI,MAAM,CAAC,IAAuB,OAAO,KAAK,WAAW,IAAI;AAE7D;;;;;CAKC,GACD,OAAO,IAAI,OAAO,OAAO,OAAc;EACrC,IAAI,OAAO,IAAI,KAAK,WAAW;EAC/B,IAAI,CAAC,MAAM,OAAO;EAClB,IAAI,KAAK,eAAe,IAAI,cAAc;IACxC,MAAM,OAAO,OAAO;IACpB,OAAO;EACT;EACA,IAAI,UAAU,IAAI,KAAK,UAAU;EACjC,IAAI,QAAQ,IAAI,KAAK,QAAQ;EAC7B,IAAI,QAAQ,CAAC;IACX,IAAI,MAAM,QACN,MAAM,gBAAgB,OAAO,SAAS,SACtC,UACA,CAAC,MAAM,WAAW,OAAO,SAAS;MAAC;KAAa,CAAC,GAAG,OAAO,MAC3D;IACJ,IAAI,KAAK,MAAM,SAAS,OAAO,MAAM;IACrC,OAAO;EACT,CAAC;EACD,QAAQ,GAAG,CAAC,MAAM;EAClB,IAAI;IACF,MAAM;EACR,SAAU;IACR,IAAI,QAAQ,GAAG,CAAC,SAAS,OAAO,QAAQ,MAAM,CAAC;EACjD;EACA,OAAO;AACT,EAAC"}
|
package/announce.ts
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Which run makes each of a harness's tool calls, as the harness's hook said
|
|
2
|
+
// just before the call (Claude Code's `PreToolUse`, posted to the hook door,
|
|
3
|
+
// ./routes.ts). One harness process serves a session and all its subagents
|
|
4
|
+
// over one MCP connection, so a call reaching the MCP door says nothing of who
|
|
5
|
+
// made it except the harness's own id for the call, which Claude Code sends in
|
|
6
|
+
// the request's `_meta` (`claudecode/toolUseId`). The hook is the one report
|
|
7
|
+
// that pairs that id with the session and the subagent behind it.
|
|
8
|
+
//
|
|
9
|
+
// The graph keeps the pairing: a hook and its call can reach different
|
|
10
|
+
// processes, including either side of a ready-first restart. A harness waits
|
|
11
|
+
// on its hook only so long (./hooks.ts), then makes the call anyway, so a call
|
|
12
|
+
// can reach the door while this process is still hearing its hook and the
|
|
13
|
+
// pairing is not yet written. The door waits on that hearing rather than
|
|
14
|
+
// reading the graph too soon and writing as nobody.
|
|
15
|
+
|
|
16
|
+
import type { Eid, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
17
|
+
import { identityEid } from '@yaks/graph/identity'
|
|
18
|
+
import { sessionFor, speaking, subagentSession } from './who.js'
|
|
19
|
+
|
|
20
|
+
let announcement = (call: string): Eid => identityEid('announced', [call])
|
|
21
|
+
|
|
22
|
+
// The calls whose `PreToolUse` this process is hearing, each to the run that
|
|
23
|
+
// makes it. A harness's call id is its own, so the id alone keys it.
|
|
24
|
+
let hearing = new Map<string, Promise<Eid | undefined>>()
|
|
25
|
+
|
|
26
|
+
/** Remember that `run` makes the harness's call `call`. */
|
|
27
|
+
export let announce = async (
|
|
28
|
+
graph: Graph,
|
|
29
|
+
call: string,
|
|
30
|
+
run: Eid,
|
|
31
|
+
): Promise<void> => {
|
|
32
|
+
await graph.apply([{
|
|
33
|
+
entity: { eid: announcement(call) },
|
|
34
|
+
announced: { call, session: run },
|
|
35
|
+
$actor: await speaking(graph, { entity: { eid: run } }),
|
|
36
|
+
}], { trusted: true })
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The run that makes the harness's call `call`, where its hook said. */
|
|
40
|
+
export let announced = async (
|
|
41
|
+
graph: Graph,
|
|
42
|
+
call: string,
|
|
43
|
+
): Promise<Eid | undefined> => {
|
|
44
|
+
let heard = await hearing.get(call)?.catch(() => undefined)
|
|
45
|
+
if (heard) return heard
|
|
46
|
+
let [row] = await graph.get([announcement(call)], ['announced'])
|
|
47
|
+
let named = row?.announced as { session?: Eid } | undefined
|
|
48
|
+
return named?.session
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Forget a call whose result the harness reported. */
|
|
52
|
+
export let forget = async (graph: Graph, call: string): Promise<void> => {
|
|
53
|
+
let run = await announced(graph, call)
|
|
54
|
+
if (!run) return
|
|
55
|
+
await graph.apply([{
|
|
56
|
+
entity: { eid: announcement(call) },
|
|
57
|
+
announced: null,
|
|
58
|
+
$actor: await speaking(graph, { entity: { eid: run } }),
|
|
59
|
+
}], { trusted: true })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The harness's id for the tool call a JSON-RPC message makes: Claude Code's
|
|
64
|
+
* `claudecode/toolUseId` in the message's `_meta`.
|
|
65
|
+
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { callIn } from '@yaks/session/graph'
|
|
68
|
+
* import { assertEquals } from '@std/assert'
|
|
69
|
+
*
|
|
70
|
+
* let message = {
|
|
71
|
+
* method: 'tools/call',
|
|
72
|
+
* params: { name: 'graph_show', _meta: { 'claudecode/toolUseId': 'toolu_1' } },
|
|
73
|
+
* }
|
|
74
|
+
* assertEquals(callIn(message), 'toolu_1')
|
|
75
|
+
* assertEquals(callIn({ method: 'tools/list' }), undefined)
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export let callIn = (message: unknown): string | undefined => {
|
|
79
|
+
let params = (message as { params?: { _meta?: Record<string, unknown> } })
|
|
80
|
+
?.params
|
|
81
|
+
let id = params?._meta?.['claudecode/toolUseId']
|
|
82
|
+
return typeof id == 'string' && id ? id : undefined
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** What a harness hook says of one tool call (Claude Code's field names). */
|
|
86
|
+
export type Heard = {
|
|
87
|
+
hook_event_name?: unknown
|
|
88
|
+
tool_use_id?: unknown
|
|
89
|
+
session_id?: unknown
|
|
90
|
+
agent_id?: unknown
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let str = (v: unknown): string => typeof v == 'string' ? v : ''
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Hear a hook event about a tool call: before the call (`PreToolUse`), the run
|
|
97
|
+
* that makes it is remembered, a subagent's session written where the graph
|
|
98
|
+
* has not met it; once a result is reported, the call is forgotten. Answers
|
|
99
|
+
* whether the event was a call's announcement, which asks for no answer.
|
|
100
|
+
*/
|
|
101
|
+
export let hear = async (graph: Graph, hook: Heard): Promise<boolean> => {
|
|
102
|
+
let call = str(hook.tool_use_id)
|
|
103
|
+
if (!call) return false
|
|
104
|
+
if (hook.hook_event_name != 'PreToolUse') {
|
|
105
|
+
await forget(graph, call)
|
|
106
|
+
return false
|
|
107
|
+
}
|
|
108
|
+
let session = str(hook.session_id)
|
|
109
|
+
let agent = str(hook.agent_id)
|
|
110
|
+
let heard = (async () => {
|
|
111
|
+
let run = agent
|
|
112
|
+
? await subagentSession(graph, session, agent)
|
|
113
|
+
: session
|
|
114
|
+
? (await sessionFor(graph, session, ['session.id']))?.entity.eid
|
|
115
|
+
: undefined
|
|
116
|
+
if (run) await announce(graph, call, run)
|
|
117
|
+
return run
|
|
118
|
+
})()
|
|
119
|
+
hearing.set(call, heard)
|
|
120
|
+
try {
|
|
121
|
+
await heard
|
|
122
|
+
} finally {
|
|
123
|
+
if (hearing.get(call) == heard) hearing.delete(call)
|
|
124
|
+
}
|
|
125
|
+
return true
|
|
126
|
+
}
|