@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/joined.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Which entries make the lines of a conversation: one pure function every
|
|
2
|
+
// drawing of a transcript shares, whatever draws its lines (./conversation.ts,
|
|
3
|
+
// browse's session page). It reads nothing but the entries.
|
|
4
|
+
|
|
5
|
+
import type { Bundle } from 'jsr:@yaks/render@0.2.36'
|
|
6
|
+
import { CALL } from './native.js'
|
|
7
|
+
|
|
8
|
+
let comp = (b: Bundle, name: string) =>
|
|
9
|
+
b[name] as Record<string, unknown> | undefined
|
|
10
|
+
|
|
11
|
+
/** A line of a conversation: the entry it is drawn at, the result that
|
|
12
|
+
* answered it when that entry is a call, and how many entries in a row it
|
|
13
|
+
* stands for (`n`), where it stands for more than its own. */
|
|
14
|
+
export type Joined = { entry: Bundle; result?: Bundle; n?: number }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Which entries make the lines of a conversation, in its order: each entry is
|
|
18
|
+
* a line, except a result whose call is among them, which joins that call's
|
|
19
|
+
* line, so an answer settles in its call's place however many calls ran
|
|
20
|
+
* beside it. A result whose call is not among them is a line of its own.
|
|
21
|
+
* Where `same` says an entry repeats the one before it (a stream telling one
|
|
22
|
+
* fact frame by frame), the run is one line: its last entry, counting the
|
|
23
|
+
* run.
|
|
24
|
+
*
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { joined } from './joined.ts'
|
|
27
|
+
* import { equal } from '@yaks/testing'
|
|
28
|
+
* let e = (eid: string, comps = {}) => ({ entity: { eid }, entry: {}, ...comps })
|
|
29
|
+
* let lines = joined([
|
|
30
|
+
* e('c', { call: { to: 't' } }),
|
|
31
|
+
* e('w', { content: { body: 'Hi' } }),
|
|
32
|
+
* e('r', { result: { call: 'c' } }),
|
|
33
|
+
* e('lone', { result: { call: 'gone' } }),
|
|
34
|
+
* ])
|
|
35
|
+
* equal(lines.map((l) => [l.entry.entity.eid, l.result?.entity.eid]), [
|
|
36
|
+
* ['c', 'r'],
|
|
37
|
+
* ['w', undefined],
|
|
38
|
+
* ['lone', undefined],
|
|
39
|
+
* ])
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export let joined = (
|
|
43
|
+
entries: Bundle[],
|
|
44
|
+
same?: (a: Bundle, b: Bundle) => boolean,
|
|
45
|
+
): Joined[] => {
|
|
46
|
+
let calls = new Set(entries.filter((b) => b[CALL]).map((b) => b.entity.eid))
|
|
47
|
+
let answer = (b: Bundle) => {
|
|
48
|
+
let call = comp(b, 'result')?.call
|
|
49
|
+
return call != null && calls.has(String(call)) ? String(call) : undefined
|
|
50
|
+
}
|
|
51
|
+
let results = new Map(
|
|
52
|
+
entries.flatMap((b) => {
|
|
53
|
+
let call = answer(b)
|
|
54
|
+
return call ? [[call, b] as const] : []
|
|
55
|
+
}),
|
|
56
|
+
)
|
|
57
|
+
let out: Joined[] = []
|
|
58
|
+
for (let b of entries) {
|
|
59
|
+
if (answer(b)) continue
|
|
60
|
+
let last = out.at(-1)
|
|
61
|
+
if (last && same?.(last.entry, b)) {
|
|
62
|
+
out[out.length - 1] = { entry: b, n: (last.n ?? 1) + 1 }
|
|
63
|
+
continue
|
|
64
|
+
}
|
|
65
|
+
let result = results.get(b.entity.eid)
|
|
66
|
+
out.push(result ? { entry: b, result } : { entry: b })
|
|
67
|
+
}
|
|
68
|
+
return out
|
|
69
|
+
}
|
package/lease.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// The lock rule, as one hook on the `precondition` phase.
|
|
2
|
+
//
|
|
3
|
+
// Why that phase. `precondition` runs inside the batch's own transaction and
|
|
4
|
+
// before a single row has moved. Both facts are load-bearing. Inside the
|
|
5
|
+
// transaction, so the holder this hook reads is the holder the batch is about
|
|
6
|
+
// to write against — a check outside it is a check against a graph somebody
|
|
7
|
+
// else may have moved in between. Before any write, so the holder is read
|
|
8
|
+
// before the cascade phase can remove it: a batch that deletes a session and
|
|
9
|
+
// takes its lock in the same breath must still bounce, and a check that ran
|
|
10
|
+
// after the cascade would find an empty lock and admit the take.
|
|
11
|
+
//
|
|
12
|
+
// The lock. A claim is a lease, not a patch. Writing one over another session's
|
|
13
|
+
// fails the whole batch loudly, except for a trusted handoff guarded by the
|
|
14
|
+
// previous holder's token. The same session
|
|
15
|
+
// re-claiming is a no-op refresh, so a worker replaying its own take is
|
|
16
|
+
// idempotent. A release (`claim: null`) is deliberately unguarded: releasing is
|
|
17
|
+
// how a lock is handed over, and the start-up pass frees a dead session's locks
|
|
18
|
+
// without impersonating that session.
|
|
19
|
+
//
|
|
20
|
+
// A new lock also leaves a `worked` edge from its session to what it took
|
|
21
|
+
// (@yaks/edge `link`), so what a session worked on outlives the claim: its
|
|
22
|
+
// name (./name.ts), its row and memory_session read it after the release. A
|
|
23
|
+
// graph whose vocabulary declares no links gets none, as a rule about
|
|
24
|
+
// components a vocabulary lacks never fires.
|
|
25
|
+
//
|
|
26
|
+
// The batch is checked as a whole, because a batch may take two locks, and the
|
|
27
|
+
// verdict has to be about the batch rather than about each bundle in turn.
|
|
28
|
+
import { line } from "@yaks/query";
|
|
29
|
+
import { token } from "@yaks/graph/guard";
|
|
30
|
+
import { after } from "@yaks/fp";
|
|
31
|
+
import { EDGE, link } from "@yaks/edge";
|
|
32
|
+
import { CLAIM } from "./comp.js";
|
|
33
|
+
import { Bounced } from "./bounce.js";
|
|
34
|
+
// One component off a bundle, or undefined. `null` means "drop it", which is
|
|
35
|
+
// not a value the check is about.
|
|
36
|
+
let of = (b, name)=>b?.[name] ?? undefined;
|
|
37
|
+
/** The locks a batch takes: the entity, and the session it hands the lock to.
|
|
38
|
+
* A bundle that drops a lock states no take and is not here. */ export let takes = (bundles)=>bundles.flatMap((b)=>{
|
|
39
|
+
let s = of(b, CLAIM)?.session;
|
|
40
|
+
return s ? [
|
|
41
|
+
[
|
|
42
|
+
b.entity.eid,
|
|
43
|
+
String(s)
|
|
44
|
+
]
|
|
45
|
+
] : [];
|
|
46
|
+
});
|
|
47
|
+
/** Transfer a lease in the same batch that gives its new holder work. */ export let handoffClaim = (on, from, to)=>({
|
|
48
|
+
entity: {
|
|
49
|
+
eid: on
|
|
50
|
+
},
|
|
51
|
+
claim: {
|
|
52
|
+
session: to
|
|
53
|
+
},
|
|
54
|
+
$was: {
|
|
55
|
+
claim: {
|
|
56
|
+
session: token(from)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
/** The relation a take leaves from its session to what it took. */ export let WORKED = 'worked';
|
|
61
|
+
// One end of the `worked` edges whose other end is among `ids`. A claim goes
|
|
62
|
+
// once its task is done or cancelled (./run.ts), so who worked what is read
|
|
63
|
+
// here; a vocabulary without the relation has no such edges.
|
|
64
|
+
let across = async (g, end, ids)=>{
|
|
65
|
+
if (!ids.length || !g.vocab.comp(WORKED)) return [];
|
|
66
|
+
let far = end == 'from' ? 'to' : 'from';
|
|
67
|
+
let rows = await g.rows(line`.${WORKED}&.${EDGE}.${end}=${ids}&.fields=${EDGE}.${far}`);
|
|
68
|
+
return [
|
|
69
|
+
...new Set(rows.map((r)=>String(r[`${EDGE}.${far}`])))
|
|
70
|
+
];
|
|
71
|
+
};
|
|
72
|
+
/** What `session` took, whether or not it still holds it. */ export let workedOn = (g, session)=>across(g, 'from', [
|
|
73
|
+
session
|
|
74
|
+
]);
|
|
75
|
+
/** The sessions that took any of `on`, whether or not they still hold it. */ export let takers = (g, on)=>across(g, 'to', on);
|
|
76
|
+
/**
|
|
77
|
+
* The `precondition` hook: refuse a take of a held lock without a trusted,
|
|
78
|
+
* guarded handoff ({@link Bounced}),
|
|
79
|
+
* stamp `at` on every lock the batch newly takes, and link its session
|
|
80
|
+
* `worked` to what it took.
|
|
81
|
+
*
|
|
82
|
+
* Registered by {@link https://jsr.io/@yaks/session/doc/~/sessions | sessions};
|
|
83
|
+
* exported on its own for a graph that wants the rule without the vocabulary.
|
|
84
|
+
*/ export let leasing = (opts = {})=>(bundles, tx, _, ctx)=>{
|
|
85
|
+
let taken = takes(bundles);
|
|
86
|
+
if (!taken.length) return bundles;
|
|
87
|
+
let now = opts.now ?? (()=>new Date().toISOString());
|
|
88
|
+
let want = [
|
|
89
|
+
...new Set(taken.map(([on])=>on))
|
|
90
|
+
];
|
|
91
|
+
return after(tx.get(want), (found)=>{
|
|
92
|
+
let at = new Map(found.map((b)=>[
|
|
93
|
+
b.entity.eid,
|
|
94
|
+
b
|
|
95
|
+
]));
|
|
96
|
+
// The holder of each contested entity, as the batch found it — then as the
|
|
97
|
+
// batch itself leaves it, so two bundles taking one lock for two sessions
|
|
98
|
+
// collide with each other and not only with the graph.
|
|
99
|
+
let held = new Map();
|
|
100
|
+
for (let [on, b] of at){
|
|
101
|
+
let s = of(b, CLAIM)?.session;
|
|
102
|
+
if (s) held.set(on, String(s));
|
|
103
|
+
}
|
|
104
|
+
let fresh = new Set();
|
|
105
|
+
let handoffs = new Map((ctx?.trusted ? bundles : []).filter((b)=>of(b, CLAIM)?.session && b.$was?.claim?.session).map((b)=>[
|
|
106
|
+
b.entity.eid,
|
|
107
|
+
b
|
|
108
|
+
]));
|
|
109
|
+
for (let [on, session] of taken){
|
|
110
|
+
let holder = held.get(on);
|
|
111
|
+
if (holder && holder != session) {
|
|
112
|
+
let handoff = handoffs.get(on);
|
|
113
|
+
if (fresh.has(on) || handoff?.$was?.claim?.session !== token(holder) || of(handoff, CLAIM)?.session !== session) throw new Bounced(on, session, holder);
|
|
114
|
+
}
|
|
115
|
+
if (holder != session) fresh.add(on);
|
|
116
|
+
held.set(on, session);
|
|
117
|
+
}
|
|
118
|
+
if (!fresh.size) return bundles;
|
|
119
|
+
// A new lock is stamped with the moment it was taken. `at` is
|
|
120
|
+
// server-owned, so admission refuses it from an untrusted client; this
|
|
121
|
+
// phase runs after admission, which is what lets a hook stamp at all.
|
|
122
|
+
let stamp = now();
|
|
123
|
+
let vocab = ctx?.graph?.vocab;
|
|
124
|
+
let links = !!vocab?.comp(EDGE) && !!vocab.comp(WORKED);
|
|
125
|
+
let worked = links ? [
|
|
126
|
+
...fresh
|
|
127
|
+
].map((on)=>link(held.get(on), WORKED, on)) : [];
|
|
128
|
+
let stamped = bundles.map((b)=>{
|
|
129
|
+
let c = of(b, CLAIM);
|
|
130
|
+
if (!c?.session || !fresh.has(b.entity.eid)) return b;
|
|
131
|
+
fresh.delete(b.entity.eid); // one stamp per lock, however it was stated
|
|
132
|
+
return {
|
|
133
|
+
...b,
|
|
134
|
+
[CLAIM]: {
|
|
135
|
+
...c,
|
|
136
|
+
at: stamp
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
return [
|
|
141
|
+
...stamped,
|
|
142
|
+
...worked
|
|
143
|
+
];
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
//# sourceMappingURL=lease.js.map
|
package/lease.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lease.js","sources":["./lease.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,EAAE;AACF,6EAA6E;AAC7E,yEAAyE;AACzE,8EAA8E;AAC9E,4EAA4E;AAC5E,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAC5E,iEAAiE;AACjE,EAAE;AACF,gFAAgF;AAChF,4EAA4E;AAC5E,4CAA4C;AAC5C,wEAAwE;AACxE,gFAAgF;AAChF,gFAAgF;AACh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C,GAAG;UAAE;SAAE;MAEhC,KAAK,IAAI,CAAC,IAAI,QAAQ,IAAI,MAAO;QAC/B,IAAI,SAAS,KAAK,GAAG,CAAC;QACtB,IAAI,UAAU,UAAU,SAAS;UAC/B,IAAI,UAAU,SAAS,GAAG,CAAC;UAC3B,IACE,MAAM,GAAG,CAAC,OAAO,SAAS,MAAM,OAAO,YAAY,MAAM,WACzD,GAAG,SAAS,QAAQ,YAAY,SAChC,MAAM,IAAI,QAAQ,IAAI,SAAS;QACnC;QACA,IAAI,UAAU,SAAS,MAAM,GAAG,CAAC;QACjC,KAAK,GAAG,CAAC,IAAI;MACf;MACA,IAAI,CAAC,MAAM,IAAI,EAAE,OAAO;MACxB,8DAA8D;MAC9D,uEAAuE;MACvE,sEAAsE;MACtE,IAAI,QAAQ;MACZ,IAAI,QAAS,KAAK,OAAyC;MAC3D,IAAI,QAAQ,CAAC,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,MAAM,IAAI,CAAC;MAChD,IAAI,SAAS,QACT;WAAI;OAAM,CAAC,GAAG,CAAC,CAAC,KAAO,KAAK,KAAK,GAAG,CAAC,KAAM,QAAQ,OACnD,EAAE;MACN,IAAI,UAAU,QAAQ,GAAG,CAAC,CAAC;QACzB,IAAI,IAAI,GAAG,GAAG;QACd,IAAI,CAAC,GAAG,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,MAAM,CAAC,GAAG,GAAG,OAAO;QACpD,MAAM,MAAM,CAAC,EAAE,MAAM,CAAC,GAAG,GAAE,4CAA4C;QACvE,OAAO;UAAE,GAAG,CAAC;UAAE,CAAC,MAAM,EAAE;YAAE,GAAG,CAAC;YAAE,IAAI;UAAM;QAAE;MAC9C;MACA,OAAO;WAAI;WAAY;OAAO;IAChC;EACF,EAAC"}
|
package/lease.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// The lock rule, as one hook on the `precondition` phase.
|
|
2
|
+
//
|
|
3
|
+
// Why that phase. `precondition` runs inside the batch's own transaction and
|
|
4
|
+
// before a single row has moved. Both facts are load-bearing. Inside the
|
|
5
|
+
// transaction, so the holder this hook reads is the holder the batch is about
|
|
6
|
+
// to write against — a check outside it is a check against a graph somebody
|
|
7
|
+
// else may have moved in between. Before any write, so the holder is read
|
|
8
|
+
// before the cascade phase can remove it: a batch that deletes a session and
|
|
9
|
+
// takes its lock in the same breath must still bounce, and a check that ran
|
|
10
|
+
// after the cascade would find an empty lock and admit the take.
|
|
11
|
+
//
|
|
12
|
+
// The lock. A claim is a lease, not a patch. Writing one over another session's
|
|
13
|
+
// fails the whole batch loudly, except for a trusted handoff guarded by the
|
|
14
|
+
// previous holder's token. The same session
|
|
15
|
+
// re-claiming is a no-op refresh, so a worker replaying its own take is
|
|
16
|
+
// idempotent. A release (`claim: null`) is deliberately unguarded: releasing is
|
|
17
|
+
// how a lock is handed over, and the start-up pass frees a dead session's locks
|
|
18
|
+
// without impersonating that session.
|
|
19
|
+
//
|
|
20
|
+
// A new lock also leaves a `worked` edge from its session to what it took
|
|
21
|
+
// (@yaks/edge `link`), so what a session worked on outlives the claim: its
|
|
22
|
+
// name (./name.ts), its row and memory_session read it after the release. A
|
|
23
|
+
// graph whose vocabulary declares no links gets none, as a rule about
|
|
24
|
+
// components a vocabulary lacks never fires.
|
|
25
|
+
//
|
|
26
|
+
// The batch is checked as a whole, because a batch may take two locks, and the
|
|
27
|
+
// verdict has to be about the batch rather than about each bundle in turn.
|
|
28
|
+
|
|
29
|
+
import { line } from '@yaks/query'
|
|
30
|
+
import type { Bundle, Comp, Eid, Graph, Hook } from 'jsr:@yaks/graph@0.2.36'
|
|
31
|
+
import { token } from '@yaks/graph/guard'
|
|
32
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
33
|
+
import { after } from '@yaks/fp'
|
|
34
|
+
import { EDGE, link } from '@yaks/edge'
|
|
35
|
+
import { CLAIM } from './comp.js'
|
|
36
|
+
import { Bounced } from './bounce.js'
|
|
37
|
+
|
|
38
|
+
/** What the hook needs from the application: a clock, so a test can stamp a
|
|
39
|
+
* fixed moment and a graph can stamp the moment it committed. */
|
|
40
|
+
export type LeaseOpts = {
|
|
41
|
+
/** the moment a new lock is stamped with (default: now, ISO-8601) */
|
|
42
|
+
now?: () => string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// One component off a bundle, or undefined. `null` means "drop it", which is
|
|
46
|
+
// not a value the check is about.
|
|
47
|
+
let of = (b: Bundle | undefined, name: string): Comp | undefined =>
|
|
48
|
+
(b?.[name] ?? undefined) as Comp | undefined
|
|
49
|
+
|
|
50
|
+
/** The locks a batch takes: the entity, and the session it hands the lock to.
|
|
51
|
+
* A bundle that drops a lock states no take and is not here. */
|
|
52
|
+
export let takes = (bundles: Bundle[]): [Eid, Eid][] =>
|
|
53
|
+
bundles.flatMap((b) => {
|
|
54
|
+
let s = of(b, CLAIM)?.session
|
|
55
|
+
return s ? [[b.entity.eid, String(s)] as [Eid, Eid]] : []
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
/** Transfer a lease in the same batch that gives its new holder work. */
|
|
59
|
+
export let handoffClaim = (on: Eid, from: Eid, to: Eid): Bundle => ({
|
|
60
|
+
entity: { eid: on },
|
|
61
|
+
claim: { session: to },
|
|
62
|
+
$was: { claim: { session: token(from) } },
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
/** The relation a take leaves from its session to what it took. */
|
|
66
|
+
export let WORKED = 'worked'
|
|
67
|
+
|
|
68
|
+
// One end of the `worked` edges whose other end is among `ids`. A claim goes
|
|
69
|
+
// once its task is done or cancelled (./run.ts), so who worked what is read
|
|
70
|
+
// here; a vocabulary without the relation has no such edges.
|
|
71
|
+
let across = async (
|
|
72
|
+
g: Graph,
|
|
73
|
+
end: 'from' | 'to',
|
|
74
|
+
ids: Eid[],
|
|
75
|
+
): Promise<Eid[]> => {
|
|
76
|
+
if (!ids.length || !g.vocab.comp(WORKED)) return []
|
|
77
|
+
let far = end == 'from' ? 'to' : 'from'
|
|
78
|
+
let rows = await g.rows(
|
|
79
|
+
line`.${WORKED}&.${EDGE}.${end}=${ids}&.fields=${EDGE}.${far}`,
|
|
80
|
+
)
|
|
81
|
+
return [...new Set(rows.map((r) => String(r[`${EDGE}.${far}`])))]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** What `session` took, whether or not it still holds it. */
|
|
85
|
+
export let workedOn = (g: Graph, session: Eid): Promise<Eid[]> =>
|
|
86
|
+
across(g, 'from', [session])
|
|
87
|
+
|
|
88
|
+
/** The sessions that took any of `on`, whether or not they still hold it. */
|
|
89
|
+
export let takers = (g: Graph, on: Eid[]): Promise<Eid[]> => across(g, 'to', on)
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The `precondition` hook: refuse a take of a held lock without a trusted,
|
|
93
|
+
* guarded handoff ({@link Bounced}),
|
|
94
|
+
* stamp `at` on every lock the batch newly takes, and link its session
|
|
95
|
+
* `worked` to what it took.
|
|
96
|
+
*
|
|
97
|
+
* Registered by {@link https://jsr.io/@yaks/session/doc/~/sessions | sessions};
|
|
98
|
+
* exported on its own for a graph that wants the rule without the vocabulary.
|
|
99
|
+
*/
|
|
100
|
+
export let leasing = (opts: LeaseOpts = {}): Hook => (bundles, tx, _, ctx) => {
|
|
101
|
+
let taken = takes(bundles)
|
|
102
|
+
if (!taken.length) return bundles
|
|
103
|
+
let now = opts.now ?? (() => new Date().toISOString())
|
|
104
|
+
let want = [...new Set(taken.map(([on]) => on))]
|
|
105
|
+
return after(tx.get(want), (found) => {
|
|
106
|
+
let at = new Map(found.map((b) => [b.entity.eid, b]))
|
|
107
|
+
// The holder of each contested entity, as the batch found it — then as the
|
|
108
|
+
// batch itself leaves it, so two bundles taking one lock for two sessions
|
|
109
|
+
// collide with each other and not only with the graph.
|
|
110
|
+
let held = new Map<Eid, Eid>()
|
|
111
|
+
for (let [on, b] of at) {
|
|
112
|
+
let s = of(b, CLAIM)?.session
|
|
113
|
+
if (s) held.set(on, String(s))
|
|
114
|
+
}
|
|
115
|
+
let fresh = new Set<Eid>()
|
|
116
|
+
let handoffs = new Map(
|
|
117
|
+
(ctx?.trusted ? bundles : []).filter((b) =>
|
|
118
|
+
of(b, CLAIM)?.session && b.$was?.claim?.session
|
|
119
|
+
).map((b) => [b.entity.eid, b]),
|
|
120
|
+
)
|
|
121
|
+
for (let [on, session] of taken) {
|
|
122
|
+
let holder = held.get(on)
|
|
123
|
+
if (holder && holder != session) {
|
|
124
|
+
let handoff = handoffs.get(on)
|
|
125
|
+
if (
|
|
126
|
+
fresh.has(on) || handoff?.$was?.claim?.session !== token(holder) ||
|
|
127
|
+
of(handoff, CLAIM)?.session !== session
|
|
128
|
+
) throw new Bounced(on, session, holder)
|
|
129
|
+
}
|
|
130
|
+
if (holder != session) fresh.add(on)
|
|
131
|
+
held.set(on, session)
|
|
132
|
+
}
|
|
133
|
+
if (!fresh.size) return bundles
|
|
134
|
+
// A new lock is stamped with the moment it was taken. `at` is
|
|
135
|
+
// server-owned, so admission refuses it from an untrusted client; this
|
|
136
|
+
// phase runs after admission, which is what lets a hook stamp at all.
|
|
137
|
+
let stamp = now()
|
|
138
|
+
let vocab = (ctx?.graph as { vocab?: Vocab } | undefined)?.vocab
|
|
139
|
+
let links = !!vocab?.comp(EDGE) && !!vocab.comp(WORKED)
|
|
140
|
+
let worked = links
|
|
141
|
+
? [...fresh].map((on) => link(held.get(on)!, WORKED, on))
|
|
142
|
+
: []
|
|
143
|
+
let stamped = bundles.map((b) => {
|
|
144
|
+
let c = of(b, CLAIM)
|
|
145
|
+
if (!c?.session || !fresh.has(b.entity.eid)) return b
|
|
146
|
+
fresh.delete(b.entity.eid) // one stamp per lock, however it was stated
|
|
147
|
+
return { ...b, [CLAIM]: { ...c, at: stamp } }
|
|
148
|
+
})
|
|
149
|
+
return [...stamped, ...worked]
|
|
150
|
+
})
|
|
151
|
+
}
|
package/lifecycle.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Lifecycle events use the ordinary tool runner, so HTTP and local fallback
|
|
2
|
+
// create the same sessions, calls and claims. Only the harness reply differs:
|
|
3
|
+
// command stdout carries the JSON additionalContext supported by Claude Code.
|
|
4
|
+
import { answerOf, faulted, toolEid } from "@yaks/tools/result";
|
|
5
|
+
export let lifecycle = async (host, request, hook)=>{
|
|
6
|
+
let event = hook.hook_event_name;
|
|
7
|
+
if (![
|
|
8
|
+
'SessionStart',
|
|
9
|
+
'SubagentStart',
|
|
10
|
+
'SessionEnd'
|
|
11
|
+
].includes(String(event))) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
if (!host.runner) throw new Error('lifecycle hooks need a tool runner');
|
|
15
|
+
let tool = event == 'SessionEnd' ? 'session_wrap' : 'session_context';
|
|
16
|
+
await host.runner.ensure([
|
|
17
|
+
tool
|
|
18
|
+
]);
|
|
19
|
+
let id = crypto.randomUUID();
|
|
20
|
+
let actor = await host.who?.(request);
|
|
21
|
+
let landed = await host.runner.call({
|
|
22
|
+
entity: {
|
|
23
|
+
eid: id
|
|
24
|
+
},
|
|
25
|
+
call: {
|
|
26
|
+
to: toolEid(tool),
|
|
27
|
+
args: {
|
|
28
|
+
hook: JSON.stringify(hook)
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
...actor ? {
|
|
32
|
+
$actor: actor
|
|
33
|
+
} : {}
|
|
34
|
+
});
|
|
35
|
+
if (faulted(landed, id)) return new Response('', {
|
|
36
|
+
status: 500
|
|
37
|
+
});
|
|
38
|
+
if (!host.text) throw new Error('lifecycle hooks need a text renderer');
|
|
39
|
+
let text = await host.text(answerOf(landed, id), {
|
|
40
|
+
readOnly: false
|
|
41
|
+
});
|
|
42
|
+
return Response.json(text && event != 'SessionEnd' ? {
|
|
43
|
+
hookSpecificOutput: {
|
|
44
|
+
hookEventName: event,
|
|
45
|
+
additionalContext: text
|
|
46
|
+
}
|
|
47
|
+
} : {});
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=lifecycle.js.map
|
package/lifecycle.js.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P and local fallback
|
|
2
|
+
// create the same sessions, calls and claims. Only the harness reply differs:
|
|
3
|
+
// command stdout carries the JSON additionalContext supported by Claude Code.
|
|
4
|
+
import type { Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
5
|
+
import type { Authenticate } from 'jsr:@yaks/api@0.2.36'
|
|
6
|
+
import type { Runner } from 'jsr:@yaks/tools@0.2.36'
|
|
7
|
+
import type { Host } from 'jsr:@yaks/host@0.2.36'
|
|
8
|
+
import { answerOf, faulted, toolEid } from '@yaks/tools/result'
|
|
9
|
+
|
|
10
|
+
export let lifecycle = async (
|
|
11
|
+
host: {
|
|
12
|
+
graph: Graph
|
|
13
|
+
runner?: Runner
|
|
14
|
+
who?: Authenticate
|
|
15
|
+
text?: Host['text']
|
|
16
|
+
},
|
|
17
|
+
request: Request,
|
|
18
|
+
hook: Record<string, unknown>,
|
|
19
|
+
): Promise<Response | undefined> => {
|
|
20
|
+
let event = hook.hook_event_name
|
|
21
|
+
if (
|
|
22
|
+
!['SessionStart', 'SubagentStart', 'SessionEnd'].includes(String(event))
|
|
23
|
+
) {
|
|
24
|
+
return undefined
|
|
25
|
+
}
|
|
26
|
+
if (!host.runner) throw new Error('lifecycle hooks need a tool runner')
|
|
27
|
+
let tool = event == 'SessionEnd' ? 'session_wrap' : 'session_context'
|
|
28
|
+
await host.runner.ensure([tool])
|
|
29
|
+
let id = crypto.randomUUID()
|
|
30
|
+
let actor = await host.who?.(request)
|
|
31
|
+
let landed = await host.runner.call({
|
|
32
|
+
entity: { eid: id },
|
|
33
|
+
call: { to: toolEid(tool), args: { hook: JSON.stringify(hook) } },
|
|
34
|
+
...(actor ? { $actor: actor } : {}),
|
|
35
|
+
})
|
|
36
|
+
if (faulted(landed, id)) return new Response('', { status: 500 })
|
|
37
|
+
if (!host.text) throw new Error('lifecycle hooks need a text renderer')
|
|
38
|
+
let text = await host.text(answerOf(landed, id), { readOnly: false })
|
|
39
|
+
return Response.json(
|
|
40
|
+
text && event != 'SessionEnd'
|
|
41
|
+
? {
|
|
42
|
+
hookSpecificOutput: { hookEventName: event, additionalContext: text },
|
|
43
|
+
}
|
|
44
|
+
: {},
|
|
45
|
+
)
|
|
46
|
+
}
|
package/lines.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// How a transcript reads: one `Line` per entry, and a `Status` for the
|
|
2
|
+
// transcript itself, as portable @yaks/render renderers — the same tree prints
|
|
3
|
+
// as plain text in a CLI (@yaks/text) or mounts in a browser. The status is
|
|
4
|
+
// computed from the entries the caller hands in as `ctx.entries`, because a
|
|
5
|
+
// store with no derived properties (@yaks/ram) has nothing else to read it
|
|
6
|
+
// from.
|
|
7
|
+
import { parse } from "@yaks/query/core";
|
|
8
|
+
import { define } from "@yaks/render";
|
|
9
|
+
import { passage } from "@yaks/render/views";
|
|
10
|
+
import { SESSION } from "./comp.js";
|
|
11
|
+
import { ASK, CALL, ENTRY } from "./native.js";
|
|
12
|
+
import { kindOf, statusOf, textOf } from "./state.js";
|
|
13
|
+
import { asks, sessionName } from "./name.js";
|
|
14
|
+
let comp = (b, name)=>b[name];
|
|
15
|
+
/** What an ask or a call reached: `→ gpt-6-astra`, `→ echo`. The names are the
|
|
16
|
+
* caller's (`ctx.names`, eid → name); a provider's anchor on an ask prints only
|
|
17
|
+
* when the caller can read it (`ctx.anchor`), because which comp holds it is
|
|
18
|
+
* the provider's business, not this view's. */ let reached = (b, names, anchor)=>{
|
|
19
|
+
let to = comp(b, ASK)?.to ?? comp(b, CALL)?.to;
|
|
20
|
+
if (to == null) return '';
|
|
21
|
+
let id = ASK in b ? anchor?.(b) : undefined;
|
|
22
|
+
return `→ ${names[String(to)] ?? String(to)}` + (id ? ` (${id.slice(0, 12)}…)` : '');
|
|
23
|
+
};
|
|
24
|
+
let bound = (v)=>{
|
|
25
|
+
let n = Number(v);
|
|
26
|
+
return Number.isSafeInteger(n) && n > 0 ? n : Infinity;
|
|
27
|
+
};
|
|
28
|
+
// Full prose up to `maxLines` lines and `maxChars` characters; a cut says how
|
|
29
|
+
// long the whole is and the entry that holds it, one read away.
|
|
30
|
+
let preview = (b, body, ctx)=>{
|
|
31
|
+
let lines = body.split('\n');
|
|
32
|
+
let kept = lines.slice(0, bound(ctx.maxLines)).join('\n').slice(0, bound(ctx.maxChars));
|
|
33
|
+
return kept.length < body.length ? `${kept}\n… [${body.length} characters total; ${b.entity.eid}]` : body;
|
|
34
|
+
};
|
|
35
|
+
/** Shared prose tree; renderers keep metadata structural rather than stripping
|
|
36
|
+
* text. */ export let entryBody = (b, h, ctx)=>{
|
|
37
|
+
let body = textOf(b);
|
|
38
|
+
let first = ctx.full ? preview(b, body, ctx) : body.split('\n')[0].slice(0, 70);
|
|
39
|
+
let names = ctx.names ?? {};
|
|
40
|
+
let anchor = ctx.anchor;
|
|
41
|
+
let line = [
|
|
42
|
+
reached(b, names, anchor),
|
|
43
|
+
first
|
|
44
|
+
].filter(Boolean).join(' ');
|
|
45
|
+
// Text renderers strip literal control bytes. Structural line breaks
|
|
46
|
+
// preserve transcript lines without letting other control characters
|
|
47
|
+
// through.
|
|
48
|
+
return h('span', null, ...line.split('\n').flatMap((text, i)=>i ? [
|
|
49
|
+
h('br', null),
|
|
50
|
+
text
|
|
51
|
+
] : [
|
|
52
|
+
text
|
|
53
|
+
]));
|
|
54
|
+
};
|
|
55
|
+
let line = (b, h, ctx)=>h('p', null, String(comp(b, ENTRY).seq).padStart(3), ' ', (kindOf(b) ?? 'entry').padEnd(9), ' ', entryBody(b, h, ctx));
|
|
56
|
+
// A transcript as one line of a list: the word that reaches it again, which
|
|
57
|
+
// every session tool takes (./who.ts `sessionFor`) — its number where it has
|
|
58
|
+
// one (`S-12`), else its runner's own id — and its status where the bundle
|
|
59
|
+
// carries one. A patch fresh from a write carries none, and a line that said
|
|
60
|
+
// "unknown" there would be wrong about a transcript that is fine.
|
|
61
|
+
let tile = (b, h, ctx)=>{
|
|
62
|
+
let id = ctx.id;
|
|
63
|
+
let status = comp(b, SESSION)?.status;
|
|
64
|
+
return h('p', null, b.entity.num != null && id ? id(b) : String(comp(b, SESSION)?.id ?? b.entity.eid), ...status == null ? [] : [
|
|
65
|
+
': ',
|
|
66
|
+
String(status)
|
|
67
|
+
]);
|
|
68
|
+
};
|
|
69
|
+
// A session as it is named wherever one is mentioned: by what it works on
|
|
70
|
+
// (./name.ts), read through the host's `query`, never by the command that
|
|
71
|
+
// launched it. A host that holds none of it names it by its id.
|
|
72
|
+
let title = (b, h, ctx)=>{
|
|
73
|
+
let s = ctx;
|
|
74
|
+
let read = Object.fromEntries(Object.entries(asks(b.entity.eid)).map(([k, q])=>[
|
|
75
|
+
k,
|
|
76
|
+
s.query?.(q)
|
|
77
|
+
]));
|
|
78
|
+
return h('span', null, sessionName(b, read) || s.id(b));
|
|
79
|
+
};
|
|
80
|
+
// What kind of words an entry's are, where they are not a person's or a
|
|
81
|
+
// model's prose: the same words a transcript says them by.
|
|
82
|
+
let labels = [
|
|
83
|
+
[
|
|
84
|
+
'reasoning',
|
|
85
|
+
'reasoning'
|
|
86
|
+
],
|
|
87
|
+
[
|
|
88
|
+
'prompt',
|
|
89
|
+
'instructions'
|
|
90
|
+
],
|
|
91
|
+
[
|
|
92
|
+
'notice',
|
|
93
|
+
'context'
|
|
94
|
+
],
|
|
95
|
+
[
|
|
96
|
+
'checkpoint',
|
|
97
|
+
'checkpoint'
|
|
98
|
+
]
|
|
99
|
+
];
|
|
100
|
+
let what = (b)=>{
|
|
101
|
+
let kind = kindOf(b);
|
|
102
|
+
if (kind == 'call' || kind == 'result') return `tool ${kind}`;
|
|
103
|
+
if (kind && kind != 'input' && kind != 'output') return kind;
|
|
104
|
+
return labels.find(([comp])=>comp in b)?.[1];
|
|
105
|
+
};
|
|
106
|
+
// Words said in a transcript, as one row of a list: who said them, when, the
|
|
107
|
+
// transcript they are in, and the passage around what was sought.
|
|
108
|
+
let said = passage({
|
|
109
|
+
texts: (b)=>[
|
|
110
|
+
textOf(b)
|
|
111
|
+
],
|
|
112
|
+
what,
|
|
113
|
+
where: (b)=>{
|
|
114
|
+
let session = comp(b, ENTRY)?.session;
|
|
115
|
+
return typeof session == 'string' ? [
|
|
116
|
+
'in',
|
|
117
|
+
session
|
|
118
|
+
] : undefined;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
/** The transcript views: `Line` for an entry, `Status` for a transcript. The
|
|
122
|
+
* context may carry `names` (model or tool eid → name), `anchor` (reads a
|
|
123
|
+
* provider's anchor off an ask entry), and `entries` (the transcript, for
|
|
124
|
+
* `Status`), `full` (entry prose), `maxLines` and `maxChars` (bounds on that
|
|
125
|
+
* prose's preview). In a list
|
|
126
|
+
* of answers, words someone said in a transcript are a passage (`words`, the
|
|
127
|
+
* words sought, marks them), any other entry's `Tile` is its line, and a
|
|
128
|
+
* transcript's is its status as the store computed it. A transcript's `Title`
|
|
129
|
+
* is its name (./name.ts), read through the host's `query`. */ export let sessionViews = define([
|
|
130
|
+
{
|
|
131
|
+
view: 'Line',
|
|
132
|
+
match: parse('.entry'),
|
|
133
|
+
render: line
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
view: 'Tile',
|
|
137
|
+
match: parse('.entry .created .content.body'),
|
|
138
|
+
...said
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
view: 'Tile',
|
|
142
|
+
match: parse('.entry'),
|
|
143
|
+
render: line
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
view: 'Tile',
|
|
147
|
+
match: parse(`.${SESSION}`),
|
|
148
|
+
render: tile
|
|
149
|
+
},
|
|
150
|
+
// Named ahead of @yaks/process's `Title`, the command a process runs.
|
|
151
|
+
{
|
|
152
|
+
view: 'Title',
|
|
153
|
+
match: parse(`.${SESSION}&?process`),
|
|
154
|
+
render: title
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
view: 'Body',
|
|
158
|
+
match: parse('.entry'),
|
|
159
|
+
render: entryBody
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
view: 'Status',
|
|
163
|
+
match: parse(`.${SESSION}`),
|
|
164
|
+
render: (b, h, ctx)=>{
|
|
165
|
+
let entries = ctx.entries ?? [];
|
|
166
|
+
let name = String(comp(b, SESSION)?.id ?? b.entity.eid);
|
|
167
|
+
return h('p', null, `${name}: ${statusOf(entries)}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
]);
|
|
171
|
+
//# sourceMappingURL=lines.js.map
|
package/lines.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lines.js","sources":["./lines.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,+EAA+E;AAC/E,4EAA4E;AAC5E,4EAA4E;AAC5E,2EAA2E;AAC3E,QAAQ;AAGR,SAAS,KAAK,gCAAqC;AACnD,SAEE,MAAM,4BAGwB;AAChC,SAAS,OAAO,kCAAmD;AACnE,SAAS,OAAO,oBAAmB;AACnC,SAAS,GAAG,EAAE,IAAI,EAAE,KAAK,sBAAqB;AAC9C,SAAS,MAAM,EAAE,QAAQ,EAAE,MAAM,qBAAoB;AACrD,SAAS,IAAI,EAAa,WAAW,oBAAmB;AAExD,IAAI,OAAO,CAAC,GAAW,OAAiB,CAAC,CAAC,KAAK;AAE/C;;;6CAG6C,GAC7C,IAAI,UAAU,CACZ,GACA,OACA;EAEA,IAAI,KAAK,KAAK,GAAG,MAAM,MAAM,KAAK,GAAG,OAAO;EAC5C,IAAI,MAAM,MAAM,OAAO;EACvB,IAAI,KAAK,OAAO,IAAI,SAAS,KAAK;EAClC,OAAO,CAAC,EAAE,EAAE,KAAK,CAAC,OAAO,IAAI,IAAI,OAAO,KAAK,GAC3C,CAAC,KAAK,CAAC,EAAE,EAAE,GAAG,KAAK,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,EAAE;AACvC;AAEA,IAAI,QAAQ,CAAC;EACX,IAAI,IAAI,OAAO;EACf,OAAO,OAAO,aAAa,CAAC,MAAM,IAAI,IAAI,IAAI;AAChD;AAEA,8EAA8E;AAC9E,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