@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/parts.ts
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
// What each model request carried, part by part. A request is sent as a fixed
|
|
2
|
+
// part every request of a session carries (the harness's system prompt, the
|
|
3
|
+
// instructions and persona, the listings of skills, tools and agents, each
|
|
4
|
+
// tool's schema), then the conversation since the last checkpoint. A walk
|
|
5
|
+
// follows a transcript in order, as the importer reads it (./tail.ts) or as
|
|
6
|
+
// the graph holds it, keeping both; at each request it lays them out in prompt
|
|
7
|
+
// order and splits the usage the provider reported across them with
|
|
8
|
+
// @yaks/model's `attribute`. The split goes on the request's entry as
|
|
9
|
+
// `context_parts{parts, entries}`. There the fixed part is one key, `fixed`;
|
|
10
|
+
// what it is made of is the session's `fixed{parts}`, kept once rather than
|
|
11
|
+
// on every request.
|
|
12
|
+
//
|
|
13
|
+
// Sizes are characters. They only weigh parts against each other: each split
|
|
14
|
+
// is scaled to the input the provider reported, so a request's totals are
|
|
15
|
+
// measured and the split across its parts is estimated. Where a log never
|
|
16
|
+
// shows part of what was sent (Codex's tool schemas, a native transcript read
|
|
17
|
+
// back from the graph), the first request's reported input says how much is
|
|
18
|
+
// missing, and that becomes the fixed part `unseen`.
|
|
19
|
+
//
|
|
20
|
+
// The entries a request generated are its own output, not its input: the
|
|
21
|
+
// output lines (what the model said, thought and called) since the last line
|
|
22
|
+
// that came from elsewhere (an input, a result, a notice) or the last request.
|
|
23
|
+
// That holds whether a log writes the request before its output (Claude Code,
|
|
24
|
+
// the native runner) or after it (Codex).
|
|
25
|
+
|
|
26
|
+
import type { Bundle, Comp, Eid } from 'jsr:@yaks/graph@0.2.36'
|
|
27
|
+
import { HUMAN, SHORT } from '@yaks/id'
|
|
28
|
+
import { and, type Clause, eq, present } from '@yaks/query/core'
|
|
29
|
+
import {
|
|
30
|
+
attribute,
|
|
31
|
+
type Item,
|
|
32
|
+
type Paid,
|
|
33
|
+
type Part,
|
|
34
|
+
type Tool,
|
|
35
|
+
type Usage,
|
|
36
|
+
} from '@yaks/model'
|
|
37
|
+
|
|
38
|
+
/** What a harness's log says about the fixed part: the parts one source of it
|
|
39
|
+
* holds now, or, with `add`, more of them. */
|
|
40
|
+
export type Fixed = { source: string; parts: Part[]; add?: boolean }
|
|
41
|
+
|
|
42
|
+
/** A transcript followed so far. */
|
|
43
|
+
export type Walk = {
|
|
44
|
+
/** the fixed part, by the source each piece came from, in first-seen order */
|
|
45
|
+
fixed: Map<string, Part[]>
|
|
46
|
+
/** whether the fixed part changed since the session last recorded it */
|
|
47
|
+
changed: boolean
|
|
48
|
+
/** the conversation since the last checkpoint: key, size, entry */
|
|
49
|
+
lines: Line[]
|
|
50
|
+
/** where this request's own output can begin in `lines` */
|
|
51
|
+
own: number
|
|
52
|
+
/** tool names by tool entity, and calls by entity */
|
|
53
|
+
names: Map<Eid, string>
|
|
54
|
+
calls: Map<Eid, string>
|
|
55
|
+
/** the model the transcript last asked for */
|
|
56
|
+
model?: string
|
|
57
|
+
/** the previous request, whose later lines name it again */
|
|
58
|
+
last?: Eid
|
|
59
|
+
/** whether part of the fixed part is yet to be sized from a request */
|
|
60
|
+
unseen: boolean
|
|
61
|
+
/** characters per token, to size what a log never showed */
|
|
62
|
+
ratio: number
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A line this large, in tokens, is named on its own as well as by its kind. */
|
|
66
|
+
export let BIG = 5000
|
|
67
|
+
|
|
68
|
+
/** A new walk. `unseen` (default true) sizes what the log never shows from the
|
|
69
|
+
* first request; a log that shows a whole fixed part says so itself. */
|
|
70
|
+
export let walker = (
|
|
71
|
+
o: { unseen?: boolean; ratio?: number; names?: Map<Eid, string> } = {},
|
|
72
|
+
): Walk => ({
|
|
73
|
+
fixed: new Map(),
|
|
74
|
+
changed: false,
|
|
75
|
+
lines: [],
|
|
76
|
+
own: 0,
|
|
77
|
+
names: o.names ?? new Map(),
|
|
78
|
+
calls: new Map(),
|
|
79
|
+
unseen: o.unseen ?? true,
|
|
80
|
+
ratio: o.ratio ?? 3.5,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
/** The fixed part's pieces, in order, pieces with one key added together. */
|
|
84
|
+
export let fixedParts = (w: Walk): Part[] => {
|
|
85
|
+
let out = new Map<string, number>()
|
|
86
|
+
for (let parts of w.fixed.values()) {
|
|
87
|
+
for (let [k, n] of parts) out.set(k, (out.get(k) ?? 0) + n)
|
|
88
|
+
}
|
|
89
|
+
return [...out]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Follow what a log says about the fixed part.
|
|
94
|
+
*
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { fix, fixedParts, walker } from '@yaks/session/parts'
|
|
97
|
+
* import { equal } from '@yaks/testing'
|
|
98
|
+
*
|
|
99
|
+
* let w = walker()
|
|
100
|
+
* fix(w, { source: 'tools', parts: [['tool Read', 10]] })
|
|
101
|
+
* fix(w, { source: 'skills', parts: [['skills', 5]] })
|
|
102
|
+
* fix(w, { source: 'skills', parts: [['skills', 2]], add: true })
|
|
103
|
+
* fix(w, { source: 'tools', parts: [['tool Bash', 7]] })
|
|
104
|
+
* equal(fixedParts(w), [['tool Bash', 7], ['skills', 7]])
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export let fix = (w: Walk, f: Fixed): void => {
|
|
108
|
+
w.fixed.set(f.source, [
|
|
109
|
+
...(f.add ? w.fixed.get(f.source) ?? [] : []),
|
|
110
|
+
...f.parts,
|
|
111
|
+
])
|
|
112
|
+
// Once a log shows the tools, nothing the fixed part holds is unseen.
|
|
113
|
+
if (f.source == 'tools' && !f.add && f.parts.length) {
|
|
114
|
+
w.unseen = false
|
|
115
|
+
w.fixed.delete('unseen')
|
|
116
|
+
}
|
|
117
|
+
w.changed = true
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
let comp = (b: Bundle, name: string) => b[name] as Comp | undefined
|
|
121
|
+
let text = (b: Bundle) => String(comp(b, 'content')?.body ?? '')
|
|
122
|
+
|
|
123
|
+
let named = (w: Walk, eid: unknown) => w.names.get(String(eid)) ?? 'tool'
|
|
124
|
+
|
|
125
|
+
// What a line is, as a key, or nothing for an entry that is no line.
|
|
126
|
+
let kind = (w: Walk, b: Bundle): string | undefined =>
|
|
127
|
+
b.checkpoint
|
|
128
|
+
? 'checkpoint'
|
|
129
|
+
: b.call
|
|
130
|
+
? `call ${named(w, comp(b, 'call')!.to)}`
|
|
131
|
+
: b.result
|
|
132
|
+
? `result ${w.calls.get(String(comp(b, 'result')!.call)) ?? 'tool'}`
|
|
133
|
+
: !b.content || !text(b)
|
|
134
|
+
? undefined
|
|
135
|
+
: b.reasoning
|
|
136
|
+
? 'reasoning'
|
|
137
|
+
: b.output
|
|
138
|
+
? 'output'
|
|
139
|
+
: b.notice
|
|
140
|
+
? text(b).startsWith('# AGENTS.md') ? 'instructions AGENTS.md' : 'notice'
|
|
141
|
+
: 'input'
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The entries a part is made of, as a query, where one names them: a
|
|
145
|
+
* `call <tool>` part is the calls to that tool and a `result <tool>` part
|
|
146
|
+
* their results. Every other part is no set of entries a query names, so it
|
|
147
|
+
* has none.
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* import { partQuery } from './parts.ts'
|
|
151
|
+
* import { stringify } from '@yaks/query'
|
|
152
|
+
* import { equal } from '@yaks/testing'
|
|
153
|
+
*
|
|
154
|
+
* equal(stringify(partQuery('result Bash')!),
|
|
155
|
+
* '.result&.result.call.call.to.tool.name=Bash')
|
|
156
|
+
* equal(stringify(partQuery('call mcp__tasks__task_new')!),
|
|
157
|
+
* '.call&.call.to.tool.name=mcp__tasks__task_new')
|
|
158
|
+
* equal(partQuery('input'), undefined)
|
|
159
|
+
* equal(partQuery('tool Bash'), undefined)
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
export let partQuery = (key: string): Clause | undefined => {
|
|
163
|
+
let [, line, tool] = key.match(/^(call|result) (.+)$/) ?? []
|
|
164
|
+
return line == 'call'
|
|
165
|
+
? and(present('call'), eq('call.to.tool.name', tool))
|
|
166
|
+
: line == 'result'
|
|
167
|
+
? and(present('result'), eq('result.call.call.to.tool.name', tool))
|
|
168
|
+
: undefined
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let generating = (key: string) =>
|
|
172
|
+
key == 'output' || key == 'reasoning' || key.startsWith('call ')
|
|
173
|
+
|
|
174
|
+
let size = (b: Bundle) =>
|
|
175
|
+
text(b).length +
|
|
176
|
+
(b.call && comp(b, 'call')!.args
|
|
177
|
+
? JSON.stringify(comp(b, 'call')!.args).length
|
|
178
|
+
: 0)
|
|
179
|
+
|
|
180
|
+
let round = (p: Paid): Paid => p.map(Math.round) as Paid
|
|
181
|
+
|
|
182
|
+
/** A line of the conversation as a request carried it: its key, its size,
|
|
183
|
+
* and the entry it is, where it is one. */
|
|
184
|
+
export type Line = [key: string, size: number, eid?: Eid]
|
|
185
|
+
|
|
186
|
+
let sum = (ps: [string, number, ...unknown[]][]) =>
|
|
187
|
+
ps.reduce((n, [, s]) => n + s, 0)
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* One request's `context_parts`: the fixed part laid out first as one part,
|
|
191
|
+
* then the conversation's lines, the reported input split across them by
|
|
192
|
+
* where each sits; a line of `BIG` tokens or more is also named on its own.
|
|
193
|
+
* What the request generated is reasoning, as far as the provider counted it,
|
|
194
|
+
* and output.
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* import { contextParts } from '@yaks/session/parts'
|
|
198
|
+
* import { equal } from '@yaks/testing'
|
|
199
|
+
*
|
|
200
|
+
* let usage = { input_tokens: 20000, cached_tokens: 10000, output_tokens: 9, reasoning_tokens: 4 }
|
|
201
|
+
* equal(contextParts([['tool Read', 50]], [['result Read', 150, 'e1']], usage), {
|
|
202
|
+
* parts: {
|
|
203
|
+
* fixed: [5000, 0, 0, 0],
|
|
204
|
+
* 'result Read': [5000, 0, 10000, 0],
|
|
205
|
+
* output: [0, 0, 0, 5],
|
|
206
|
+
* reasoning: [0, 0, 0, 4],
|
|
207
|
+
* },
|
|
208
|
+
* entries: { e1: [5000, 0, 10000, 0] },
|
|
209
|
+
* })
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
export let contextParts = (
|
|
213
|
+
fixed: Part[],
|
|
214
|
+
lines: Line[],
|
|
215
|
+
u: Usage,
|
|
216
|
+
): { parts: Record<string, Paid>; entries?: Record<string, Paid> } => {
|
|
217
|
+
let input = u.input_tokens ?? 0
|
|
218
|
+
let whole = sum(fixed) + sum(lines)
|
|
219
|
+
let scale = whole ? input / whole : 0
|
|
220
|
+
let parts: Part[] = [
|
|
221
|
+
['fixed', sum(fixed)],
|
|
222
|
+
...lines.map(([k, n, eid]): Part =>
|
|
223
|
+
eid && n * scale >= BIG ? [`${k}\n${eid}`, n] : [k, n]
|
|
224
|
+
),
|
|
225
|
+
]
|
|
226
|
+
let out: Record<string, Paid> = {}
|
|
227
|
+
let entries: Record<string, Paid> = {}
|
|
228
|
+
for (let [key, p] of Object.entries(attribute(u, parts))) {
|
|
229
|
+
let [k, eid] = key.split('\n')
|
|
230
|
+
let at = out[k] ??= [0, 0, 0, 0]
|
|
231
|
+
p.forEach((n, i) => at[i] += n)
|
|
232
|
+
if (eid) entries[eid] = round(p)
|
|
233
|
+
}
|
|
234
|
+
let made = u.output_tokens ?? 0
|
|
235
|
+
let thought = Math.min(u.reasoning_tokens ?? 0, made)
|
|
236
|
+
if (made - thought) (out.output ??= [0, 0, 0, 0])[3] += made - thought
|
|
237
|
+
if (thought) (out.reasoning ??= [0, 0, 0, 0])[3] += thought
|
|
238
|
+
return {
|
|
239
|
+
parts: Object.fromEntries(
|
|
240
|
+
Object.entries(out).map(([k, p]) => [k, round(p)]),
|
|
241
|
+
),
|
|
242
|
+
...Object.keys(entries).length ? { entries } : {},
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// One request of a walk. What its log never showed is sized from the first.
|
|
247
|
+
let split = (w: Walk, u: Usage) => {
|
|
248
|
+
let lines = w.lines.slice(0, w.own)
|
|
249
|
+
if (w.unseen) {
|
|
250
|
+
let missing = (u.input_tokens ?? 0) * w.ratio - sum(fixedParts(w)) -
|
|
251
|
+
sum(lines)
|
|
252
|
+
if (missing > 0) {
|
|
253
|
+
w.fixed.set('unseen', [['unseen', Math.round(missing)]])
|
|
254
|
+
w.changed = true
|
|
255
|
+
}
|
|
256
|
+
w.unseen = false
|
|
257
|
+
}
|
|
258
|
+
return contextParts(fixedParts(w), lines, u)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Follow one entry, in transcript order, and answer what it owes: for a
|
|
263
|
+
* request (an entry carrying `usage`), its `context_parts`, the model where
|
|
264
|
+
* the request did not name one, and the session's `fixed` where the fixed
|
|
265
|
+
* part changed. A request named again (Claude Code writes a reply over several
|
|
266
|
+
* lines, each naming it) owes nothing more.
|
|
267
|
+
*
|
|
268
|
+
* ```ts
|
|
269
|
+
* import { fix, step, walker } from '@yaks/session/parts'
|
|
270
|
+
* import { equal } from '@yaks/testing'
|
|
271
|
+
*
|
|
272
|
+
* let w = walker()
|
|
273
|
+
* fix(w, { source: 'tools', parts: [['tool Read', 40]] })
|
|
274
|
+
* let e = (eid: string, c: object) => ({ entity: { eid }, entry: { session: 's' }, ...c })
|
|
275
|
+
* step(w, e('i', { content: { body: 'x'.repeat(40) } }))
|
|
276
|
+
* let [ask, session] = step(w, e('a', { ask: {}, usage: { input_tokens: 80, cached_tokens: 40, output_tokens: 3 } }))
|
|
277
|
+
* equal(ask.context_parts, { parts: { fixed: [40, 0, 0, 0], input: [0, 0, 40, 0], output: [0, 0, 0, 3] } })
|
|
278
|
+
* equal(session.fixed, { parts: { 'tool Read': 40 } })
|
|
279
|
+
* equal(step(w, e('a', { ask: {}, usage: { input_tokens: 80 } })), [])
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
export let step = (w: Walk, b: Bundle): Bundle[] => {
|
|
283
|
+
let eid = b.entity.eid
|
|
284
|
+
if (b.tool && comp(b, 'tool')?.name) {
|
|
285
|
+
w.names.set(eid, String(comp(b, 'tool')!.name))
|
|
286
|
+
}
|
|
287
|
+
let model = comp(b, 'using')?.model ?? comp(b, 'ask')?.to
|
|
288
|
+
if (model) w.model = String(model)
|
|
289
|
+
if (b.usage) {
|
|
290
|
+
if (eid == w.last) return []
|
|
291
|
+
w.last = eid
|
|
292
|
+
let owed: Bundle[] = [{
|
|
293
|
+
entity: { eid },
|
|
294
|
+
context_parts: split(w, b.usage as Usage),
|
|
295
|
+
...b.ask && !comp(b, 'ask')!.to && w.model
|
|
296
|
+
? { ask: { to: w.model } }
|
|
297
|
+
: {},
|
|
298
|
+
}]
|
|
299
|
+
let session = comp(b, 'entry')?.session
|
|
300
|
+
if (w.changed && session) {
|
|
301
|
+
owed.push({
|
|
302
|
+
entity: { eid: String(session) },
|
|
303
|
+
fixed: { parts: Object.fromEntries(fixedParts(w)) },
|
|
304
|
+
})
|
|
305
|
+
w.changed = false
|
|
306
|
+
}
|
|
307
|
+
w.own = w.lines.length
|
|
308
|
+
return owed
|
|
309
|
+
}
|
|
310
|
+
let key = kind(w, b)
|
|
311
|
+
if (!key) return []
|
|
312
|
+
if (b.call) w.calls.set(eid, named(w, comp(b, 'call')!.to))
|
|
313
|
+
if (b.checkpoint) w.lines = []
|
|
314
|
+
w.lines.push([key, size(b), eid])
|
|
315
|
+
if (!generating(key)) w.own = w.lines.length
|
|
316
|
+
return []
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Pieces of instruction text: a persona the graph projects is cut at each
|
|
321
|
+
* part it holds (`persona N-…`, `persona M-…`, `persona reads`), and the rest
|
|
322
|
+
* is the one piece `name`.
|
|
323
|
+
*
|
|
324
|
+
* ```ts
|
|
325
|
+
* import { instructionParts } from '@yaks/session/parts'
|
|
326
|
+
* import { equal } from '@yaks/testing'
|
|
327
|
+
*
|
|
328
|
+
* let persona = '# N-1 common\n\nhi\n\n---\n\n# M-2 a rule\n\nbe\n\n---\n\n## Reads\n\n- M-3'
|
|
329
|
+
* equal(instructionParts(persona).map(([k]) => k), ['persona N-1', 'persona M-2', 'persona reads'])
|
|
330
|
+
* equal(instructionParts('## Core\n\nbe kind'), [['instructions', 16]])
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
export let instructionParts = (body: string, name = 'instructions'): Part[] => {
|
|
334
|
+
let out = new Map<string, number>()
|
|
335
|
+
for (let s of body.split(/\n-{3,}\n+(?=#{1,2} )/)) {
|
|
336
|
+
let head = s.match(/^#{1,2} (\S+)/m)?.[1] ?? ''
|
|
337
|
+
let key = head == 'Reads'
|
|
338
|
+
? 'persona reads'
|
|
339
|
+
: HUMAN.test(head) || SHORT.test(head)
|
|
340
|
+
? `persona ${head}`
|
|
341
|
+
: name
|
|
342
|
+
out.set(key, (out.get(key) ?? 0) + s.length)
|
|
343
|
+
}
|
|
344
|
+
return [...out]
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** A tool as a piece of the fixed part, sized by what declares it. */
|
|
348
|
+
export let toolPart = (tool: Tool | { name?: unknown }): Part => [
|
|
349
|
+
`tool ${String(tool.name ?? 'tool')}`,
|
|
350
|
+
JSON.stringify(tool).length,
|
|
351
|
+
]
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* A model request's items as the lines of its conversation: what was typed,
|
|
355
|
+
* said, called and returned, each result named by the tool its call named.
|
|
356
|
+
*
|
|
357
|
+
* ```ts
|
|
358
|
+
* import { itemLines } from '@yaks/session/parts'
|
|
359
|
+
* import { equal } from '@yaks/testing'
|
|
360
|
+
*
|
|
361
|
+
* equal(itemLines([
|
|
362
|
+
* { kind: 'user', text: 'hi' },
|
|
363
|
+
* { kind: 'call', id: 'c', name: 'Read', args: '{}' },
|
|
364
|
+
* { kind: 'result', id: 'c', output: 'abc' },
|
|
365
|
+
* ]), [['input', 2], ['call Read', 2], ['result Read', 3]])
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
export let itemLines = (items: Item[]): Line[] => {
|
|
369
|
+
let calls = new Map<string, string>()
|
|
370
|
+
return items.map((i): Line =>
|
|
371
|
+
i.kind == 'user'
|
|
372
|
+
? ['input', i.text.length]
|
|
373
|
+
: i.kind == 'assistant'
|
|
374
|
+
? ['output', i.text.length]
|
|
375
|
+
: i.kind == 'instruction'
|
|
376
|
+
? ['notice', i.text.length]
|
|
377
|
+
: i.kind == 'call'
|
|
378
|
+
? (calls.set(i.id, i.name), [`call ${i.name}`, i.args.length])
|
|
379
|
+
: i.kind == 'result'
|
|
380
|
+
? [`result ${calls.get(i.id) ?? 'tool'}`, i.output.length]
|
|
381
|
+
: [i.kind, i.bytes.length]
|
|
382
|
+
)
|
|
383
|
+
}
|
package/plugin.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { resultEntries } from "./result-entry.js";
|
|
2
|
+
import { sequencing } from "./append.js";
|
|
3
|
+
import { weighing } from "./cost.js";
|
|
4
|
+
// The package as a graph plugin: the vocabulary, the rules, and the audit.
|
|
5
|
+
//
|
|
6
|
+
// It needs nothing from the application — no app to speak for, no roster to
|
|
7
|
+
// consult — because everything it decides is about the batch and the entities
|
|
8
|
+
// the batch names. So the factory takes only the two things a test wants to
|
|
9
|
+
// hold still: the clock, and the eid a conflict row is written under.
|
|
10
|
+
//
|
|
11
|
+
// The two hooks are two phases on purpose. `precondition` refuses, inside the
|
|
12
|
+
// transaction, before anything moves. `audit` remembers, outside it, after the
|
|
13
|
+
// rollback. One of them cannot do both jobs: a refusal that could also write
|
|
14
|
+
// would write into the transaction it just condemned.
|
|
15
|
+
import { after } from "@yaks/fp";
|
|
16
|
+
import { SESSION, sessionDoc } from "./comp.js";
|
|
17
|
+
import { auditing } from "./audit.js";
|
|
18
|
+
import { leasing } from "./lease.js";
|
|
19
|
+
import { naming } from "./naming.js";
|
|
20
|
+
import { reopening } from "./reopen.js";
|
|
21
|
+
import { routing } from "./routine.js";
|
|
22
|
+
import { attributing, runners, sessionEid } from "./who.js";
|
|
23
|
+
/**
|
|
24
|
+
* The session plugin: the vocabulary ({@link sessionDoc}), a `precondition`
|
|
25
|
+
* hook that refuses a take of a held lock, refuses a component of this
|
|
26
|
+
* package's that names the wrong kind of entity, and weighs a request's `cost`
|
|
27
|
+
* at its model's price where the provider reported none ({@link weighing}),
|
|
28
|
+
* and marks a knock that is a helper's to take (./routine.ts);
|
|
29
|
+
* an `authorize` hook that holds a write through a door's session to its own
|
|
30
|
+
* writer ({@link attributing});
|
|
31
|
+
* and an `audit` hook that records a collision after the rollback.
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
35
|
+
* import { graph } from '@yaks/graph'
|
|
36
|
+
* import { ram } from '@yaks/ram'
|
|
37
|
+
* import { contextDoc } from '@yaks/context'
|
|
38
|
+
* import { modelDoc } from '@yaks/model'
|
|
39
|
+
* import { toolsDoc } from '@yaks/tools'
|
|
40
|
+
* import { sessionDoc, sessions } from '@yaks/session'
|
|
41
|
+
*
|
|
42
|
+
* // a transcript uses four packages' components, each declared once
|
|
43
|
+
* let vocab = loadVocab([sessionDoc, toolsDoc, contextDoc, modelDoc])
|
|
44
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [sessions()] })
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* The plugin runs nothing: it is the model and the rules. Entries appear, and
|
|
48
|
+
* what reacts to them is {@link react}, taken as pool work by
|
|
49
|
+
* {@link running} or looped by {@link settle}.
|
|
50
|
+
*/ export let sessions = (opts = {})=>{
|
|
51
|
+
let lease = leasing(opts);
|
|
52
|
+
let precondition = (bundles, tx, err, ctx)=>after(lease(bundles, tx, err, ctx), (b)=>after(naming(b, tx, err), (named)=>after(resultEntries(named, tx, err), (joined)=>after(sequencing(joined, tx, err), (placed)=>after(weighing(placed, tx, err), (weighed)=>after(reopening(weighed, tx, err), (reopened)=>routing(reopened, tx, err, ctx)))))));
|
|
53
|
+
return {
|
|
54
|
+
name: '@yaks/session',
|
|
55
|
+
admission: ()=>true,
|
|
56
|
+
vocab: [
|
|
57
|
+
sessionDoc
|
|
58
|
+
],
|
|
59
|
+
// A write through a session standing at a door is by who the door said.
|
|
60
|
+
authorize: attributing,
|
|
61
|
+
hooks: {
|
|
62
|
+
precondition,
|
|
63
|
+
audit: auditing(opts)
|
|
64
|
+
},
|
|
65
|
+
// A session written under an alias with the harness's own id for it is
|
|
66
|
+
// the entity that id names (./who.ts `sessionEid`); one with no id is
|
|
67
|
+
// given a fresh eid.
|
|
68
|
+
derive: {
|
|
69
|
+
[SESSION]: (c)=>typeof c.id == 'string' && c.id ? sessionEid(c.id) : ''
|
|
70
|
+
},
|
|
71
|
+
// An id meant to name a session may be the runner's own id for it, the
|
|
72
|
+
// one a harness hands out (`$CLAUDE_CODE_SESSION_ID`). Only when the
|
|
73
|
+
// caller says it means a session: every other id is left alone.
|
|
74
|
+
address: (tx, ids, kind)=>kind == SESSION ? runners(tx, ids) : new Map()
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=plugin.js.map
|
package/plugin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["./plugin.ts"],"names":[],"mappings":"AAAA,SAAS,aAAa,4BAA2B;AACjD,SAAS,UAAU,sBAAqB;AACxC,SAAS,QAAQ,oBAAmB;AACpC,2EAA2E;AAC3E,EAAE;AACF,4EAA4E;AAC5E,8EAA8E;AAC9E,4EAA4E;AAC5E,sEAAsE;AACtE,EAAE;AACF,8EAA8E;AAC9E,+EAA+E;AAC/E,6EAA6E;AAC7E,sDAAsD;AAEtD,SAAS,KAAK,wBAA6B;AAE3C,SAAS,OAAO,EAAE,UAAU,oBAAmB;AAC/C,SAAS,QAAQ,qBAAoC;AACrD,SAAS,OAAO,qBAAoB;AACpC,SAAS,MAAM,sBAAqB;AACpC,SAAS,SAAS,sBAAqB;AACvC,SAAS,OAAO,uBAAsB;AACtC,SAAS,WAAW,EAAE,OAAO,EAAE,UAAU,mBAAkB;AAM3D;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BC,GACD,OAAO,IAAI,WAAW,CAAC,OAAoB,CAAC,CAAC;EAC3C,IAAI,QAAQ,QAAQ;EACpB,IAAI,eAAqB,CAAC,SAAS,IAAI,KAAK,MAC1C,MACE,MAAM,SAAS,IAAI,KAAK,MACxB,CAAC,IACC,MAAM,OAAO,GAAG,IAAI,MAAM,CAAC,QACzB,MAAM,cAAc,OAAO,IAAI,MAAM,CAAC,SACpC,MACE,WAAW,QAAQ,IAAI,MACvB,CAAC,SACC,MACE,SAAS,QAAQ,IAAI,MACrB,CAAC,UACC,MACE,UAAU,SAAS,IAAI,MACvB,CAAC,WACC,QAAQ,UAAU,IAAI,KAAK;EAKjD,OAAO;IACL,MAAM;IACN,WAAW,IAAM;IACjB,OAAO;MAAC;KAAW;IACnB,wEAAwE;IACxE,WAAW;IACX,OAAO;MAAE;MAAc,OAAO,SAAS;IAAM;IAC7C,uEAAuE;IACvE,sEAAsE;IACtE,qBAAqB;IACrB,QAAQ;MACN,CAAC,QAAQ,EAAE,CAAC,IAAM,OAAO,EAAE,EAAE,IAAI,YAAY,EAAE,EAAE,GAAG,WAAW,EAAE,EAAE,IAAI;IACzE;IACA,uEAAuE;IACvE,qEAAqE;IACrE,gEAAgE;IAChE,SAAS,CAAC,IAAI,KAAK,OAAS,QAAQ,UAAU,QAAQ,IAAI,OAAO,IAAI;EACvE;AACF,EAAC"}
|
package/plugin.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { resultEntries } from './result-entry.js'
|
|
2
|
+
import { sequencing } from './append.js'
|
|
3
|
+
import { weighing } from './cost.js'
|
|
4
|
+
// The package as a graph plugin: the vocabulary, the rules, and the audit.
|
|
5
|
+
//
|
|
6
|
+
// It needs nothing from the application — no app to speak for, no roster to
|
|
7
|
+
// consult — because everything it decides is about the batch and the entities
|
|
8
|
+
// the batch names. So the factory takes only the two things a test wants to
|
|
9
|
+
// hold still: the clock, and the eid a conflict row is written under.
|
|
10
|
+
//
|
|
11
|
+
// The two hooks are two phases on purpose. `precondition` refuses, inside the
|
|
12
|
+
// transaction, before anything moves. `audit` remembers, outside it, after the
|
|
13
|
+
// rollback. One of them cannot do both jobs: a refusal that could also write
|
|
14
|
+
// would write into the transaction it just condemned.
|
|
15
|
+
|
|
16
|
+
import { after } from '@yaks/fp'
|
|
17
|
+
import type { Hook, Plugin } from 'jsr:@yaks/graph@0.2.36'
|
|
18
|
+
import { SESSION, sessionDoc } from './comp.js'
|
|
19
|
+
import { auditing, type AuditOpts } from './audit.js'
|
|
20
|
+
import { leasing } from './lease.js'
|
|
21
|
+
import { naming } from './naming.js'
|
|
22
|
+
import { reopening } from './reopen.js'
|
|
23
|
+
import { routing } from './routine.js'
|
|
24
|
+
import { attributing, runners, sessionEid } from './who.js'
|
|
25
|
+
|
|
26
|
+
/** The plugin's two injection points: a clock for both stamps, and the eid a
|
|
27
|
+
* conflict row is minted under. */
|
|
28
|
+
export type SessionOpts = AuditOpts
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The session plugin: the vocabulary ({@link sessionDoc}), a `precondition`
|
|
32
|
+
* hook that refuses a take of a held lock, refuses a component of this
|
|
33
|
+
* package's that names the wrong kind of entity, and weighs a request's `cost`
|
|
34
|
+
* at its model's price where the provider reported none ({@link weighing}),
|
|
35
|
+
* and marks a knock that is a helper's to take (./routine.ts);
|
|
36
|
+
* an `authorize` hook that holds a write through a door's session to its own
|
|
37
|
+
* writer ({@link attributing});
|
|
38
|
+
* and an `audit` hook that records a collision after the rollback.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* import { loadVocab } from '@yaks/vocab'
|
|
42
|
+
* import { graph } from '@yaks/graph'
|
|
43
|
+
* import { ram } from '@yaks/ram'
|
|
44
|
+
* import { contextDoc } from '@yaks/context'
|
|
45
|
+
* import { modelDoc } from '@yaks/model'
|
|
46
|
+
* import { toolsDoc } from '@yaks/tools'
|
|
47
|
+
* import { sessionDoc, sessions } from '@yaks/session'
|
|
48
|
+
*
|
|
49
|
+
* // a transcript uses four packages' components, each declared once
|
|
50
|
+
* let vocab = loadVocab([sessionDoc, toolsDoc, contextDoc, modelDoc])
|
|
51
|
+
* let g = graph({ storage: ram(vocab), vocab, plugins: [sessions()] })
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* The plugin runs nothing: it is the model and the rules. Entries appear, and
|
|
55
|
+
* what reacts to them is {@link react}, taken as pool work by
|
|
56
|
+
* {@link running} or looped by {@link settle}.
|
|
57
|
+
*/
|
|
58
|
+
export let sessions = (opts: SessionOpts = {}): Plugin => {
|
|
59
|
+
let lease = leasing(opts)
|
|
60
|
+
let precondition: Hook = (bundles, tx, err, ctx) =>
|
|
61
|
+
after(
|
|
62
|
+
lease(bundles, tx, err, ctx),
|
|
63
|
+
(b) =>
|
|
64
|
+
after(naming(b, tx, err), (named) =>
|
|
65
|
+
after(resultEntries(named, tx, err), (joined) =>
|
|
66
|
+
after(
|
|
67
|
+
sequencing(joined, tx, err),
|
|
68
|
+
(placed) =>
|
|
69
|
+
after(
|
|
70
|
+
weighing(placed, tx, err),
|
|
71
|
+
(weighed) =>
|
|
72
|
+
after(
|
|
73
|
+
reopening(weighed, tx, err),
|
|
74
|
+
(reopened) =>
|
|
75
|
+
routing(reopened, tx, err, ctx),
|
|
76
|
+
),
|
|
77
|
+
),
|
|
78
|
+
))),
|
|
79
|
+
)
|
|
80
|
+
return {
|
|
81
|
+
name: '@yaks/session',
|
|
82
|
+
admission: () => true,
|
|
83
|
+
vocab: [sessionDoc],
|
|
84
|
+
// A write through a session standing at a door is by who the door said.
|
|
85
|
+
authorize: attributing,
|
|
86
|
+
hooks: { precondition, audit: auditing(opts) },
|
|
87
|
+
// A session written under an alias with the harness's own id for it is
|
|
88
|
+
// the entity that id names (./who.ts `sessionEid`); one with no id is
|
|
89
|
+
// given a fresh eid.
|
|
90
|
+
derive: {
|
|
91
|
+
[SESSION]: (c) => typeof c.id == 'string' && c.id ? sessionEid(c.id) : '',
|
|
92
|
+
},
|
|
93
|
+
// An id meant to name a session may be the runner's own id for it, the
|
|
94
|
+
// one a harness hands out (`$CLAUDE_CODE_SESSION_ID`). Only when the
|
|
95
|
+
// caller says it means a session: every other id is left alone.
|
|
96
|
+
address: (tx, ids, kind) => kind == SESSION ? runners(tx, ids) : new Map(),
|
|
97
|
+
}
|
|
98
|
+
}
|