@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/tail.js
ADDED
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
// The importer: a harness's own log, read into a session's transcript. One
|
|
2
|
+
// importer serves every session run outside the graph's own daemon, whatever
|
|
3
|
+
// started it: @yaks/spawn follows a managed run's stdout with it, and the
|
|
4
|
+
// package's duty (./service.ts) follows the transcript file an interactive
|
|
5
|
+
// harness keeps for itself. A reader (./readers.ts) says what each line means;
|
|
6
|
+
// this decides which entities it becomes, and writes them.
|
|
7
|
+
//
|
|
8
|
+
// The log is the durable record, and the graph holds the transcript read out
|
|
9
|
+
// of it. The log is a file (@yaks/file's `file{path, root}`, on the volume that
|
|
10
|
+
// holds it), and each entry wears the line it came from, `lines{file, start}`.
|
|
11
|
+
// The file carries `transcript{session, consumed, version, finished}`: how many
|
|
12
|
+
// of its lines have been read, written in the transaction that writes each
|
|
13
|
+
// line's entries, and on its own once lines that make none are read past. A
|
|
14
|
+
// resume begins there, whatever has become of the entries since: a line that
|
|
15
|
+
// made none, or one whose entries a strip deleted, is never read again.
|
|
16
|
+
//
|
|
17
|
+
// Codex compresses a finished rollout from `X.jsonl` to `X.jsonl.zst`. That is
|
|
18
|
+
// another file holding the same lines, so its reading moves with it: the
|
|
19
|
+
// transcript, and the session's `session.transcript` where it named the old
|
|
20
|
+
// file.
|
|
21
|
+
//
|
|
22
|
+
// Each entry's eid is derived from its session, its line and its place in the
|
|
23
|
+
// line, and a call's from the harness's own id for it, which is what its result
|
|
24
|
+
// names. Reading a line twice writes the same entities twice, which changes
|
|
25
|
+
// nothing, and a result finds its call without a lookup. A harness can write
|
|
26
|
+
// one session's id into two files (two processes under one id), and their
|
|
27
|
+
// lines would collide: the session's first file (`session.transcript`) names
|
|
28
|
+
// its entries by session and line alone, and an entry of any other file names
|
|
29
|
+
// that file's path too.
|
|
30
|
+
//
|
|
31
|
+
// A call arrives already running, held by the session itself
|
|
32
|
+
// (`execution{state, by}`): the harness ran it in its own process, and a tool
|
|
33
|
+
// runner (@yaks/tools) leaves a call somebody else holds alone. Its result
|
|
34
|
+
// says whether it went through.
|
|
35
|
+
//
|
|
36
|
+
// This module reads files; it is reached through `@yaks/session/tail`, never
|
|
37
|
+
// the main module, which a browser loads.
|
|
38
|
+
import { line as query } from "@yaks/query";
|
|
39
|
+
import { after, batches, records } from "./log.js";
|
|
40
|
+
import { derivedEid, identityEid } from "@yaks/graph/identity";
|
|
41
|
+
import { onDisk } from "@yaks/file/local";
|
|
42
|
+
import { status } from "@yaks/graph/errors";
|
|
43
|
+
import { TOMBSTONE } from "@yaks/graph/bundle";
|
|
44
|
+
import { who } from "@yaks/graph/call";
|
|
45
|
+
import { metering } from "@yaks/model";
|
|
46
|
+
import { humanCaller } from "./who.js";
|
|
47
|
+
import { fix, step, walker } from "./parts.js";
|
|
48
|
+
import { imported, merge, pieced, retained, typed } from "./imported.js";
|
|
49
|
+
export { askOf, callOf, imported, pieced, prose } from "./imported.js";
|
|
50
|
+
/** Whether a tail is behind its file: a pull stopped before its end, and
|
|
51
|
+
* holds lines it has not read into the graph yet. */ export let behind = (t)=>t.rest.includes('\n');
|
|
52
|
+
let comp = (b, name)=>b?.[name];
|
|
53
|
+
// What a line says, its plan-limit readings timed and placed: a line that
|
|
54
|
+
// reports limits and says no time of its own (Claude's `rate_limit_event`)
|
|
55
|
+
// was written after the newest line that said one, and a limit on one model
|
|
56
|
+
// is on the model the log last said answers it. `last` keeps both.
|
|
57
|
+
let timed = (said, last)=>{
|
|
58
|
+
last.time = said.at ?? last.time;
|
|
59
|
+
last.model = said.model ?? last.model;
|
|
60
|
+
if (!said.limits) return said;
|
|
61
|
+
let model = last.model && identityEid('model', [
|
|
62
|
+
last.model
|
|
63
|
+
]);
|
|
64
|
+
return {
|
|
65
|
+
...said,
|
|
66
|
+
...!said.at && last.time ? {
|
|
67
|
+
at: last.time
|
|
68
|
+
} : {},
|
|
69
|
+
limits: metering(said.limits, model)
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
// How many lines one transaction writes at most, and how much text: a line
|
|
73
|
+
// to a transaction rewrites the same index pages once per line, which is
|
|
74
|
+
// most of what reading a long log in costs.
|
|
75
|
+
let RUN = 500;
|
|
76
|
+
let RUN_TEXT = 4 * 1024 * 1024;
|
|
77
|
+
// Each line of a run, without the rows it only touches where the graph or a
|
|
78
|
+
// line before it in the run has settled them: a tool's row is written where it
|
|
79
|
+
// is missing, since writing it again moves its stamp for nothing, and so is a
|
|
80
|
+
// request, which every line of its reply names again; an answer's state only
|
|
81
|
+
// where its call is there, or the patch alone would make an entity; a
|
|
82
|
+
// plan-limit reading only over an older one. A line `worth` refuses is left
|
|
83
|
+
// out of what the lines after it see. Answers the eids the graph held too.
|
|
84
|
+
let settled = async (g, run, worth = ()=>true)=>{
|
|
85
|
+
let eids = new Set(run.flatMap((l)=>l.bundles.map((b)=>b.entity.eid)));
|
|
86
|
+
let read = new Map();
|
|
87
|
+
let held = new Set();
|
|
88
|
+
for (let r of eids.size ? await g.get([
|
|
89
|
+
...eids
|
|
90
|
+
], [
|
|
91
|
+
'rate_limit'
|
|
92
|
+
]) : []){
|
|
93
|
+
if (!r || r[TOMBSTONE] != null) continue;
|
|
94
|
+
held.add(r.entity.eid);
|
|
95
|
+
read.set(r.entity.eid, String(comp(r, 'rate_limit')?.at ?? ''));
|
|
96
|
+
}
|
|
97
|
+
let there = new Set(held);
|
|
98
|
+
let lines = run.map(({ bundles }, i)=>{
|
|
99
|
+
let kept = bundles.filter((b)=>{
|
|
100
|
+
let eid = b.entity.eid;
|
|
101
|
+
if (b.rate_limit) {
|
|
102
|
+
return String(comp(b, 'rate_limit')?.at ?? '') >= (read.get(eid) ?? '');
|
|
103
|
+
}
|
|
104
|
+
if (b.entry ? b.ask : b.tool || b.provider || b.model) {
|
|
105
|
+
return !there.has(eid);
|
|
106
|
+
}
|
|
107
|
+
return b.entry || !b.execution || there.has(eid);
|
|
108
|
+
});
|
|
109
|
+
if (!worth(kept, i)) return [];
|
|
110
|
+
for (let b of kept){
|
|
111
|
+
there.add(b.entity.eid);
|
|
112
|
+
if (b.rate_limit) {
|
|
113
|
+
read.set(b.entity.eid, String(comp(b, 'rate_limit')?.at ?? ''));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return kept;
|
|
117
|
+
});
|
|
118
|
+
return {
|
|
119
|
+
lines,
|
|
120
|
+
held
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
// A run's lines as one batch, each entity dated as a transaction per line
|
|
124
|
+
// would date it (@yaks/graph `$at`): one the graph held, when the last line
|
|
125
|
+
// that writes it was written; a new one, when the first was.
|
|
126
|
+
let joined = (run, held)=>{
|
|
127
|
+
let at = new Map();
|
|
128
|
+
for (let { bundles, at: when } of run){
|
|
129
|
+
if (!when) continue;
|
|
130
|
+
for (let { entity: { eid } } of bundles){
|
|
131
|
+
if (held.has(eid) || !at.has(eid)) at.set(eid, when);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return fold([], run.flatMap((l)=>l.bundles)).map((b)=>at.has(b.entity.eid) ? {
|
|
135
|
+
...b,
|
|
136
|
+
$at: at.get(b.entity.eid)
|
|
137
|
+
} : b);
|
|
138
|
+
};
|
|
139
|
+
let live = (b)=>b && b[TOMBSTONE] == null ? b : undefined;
|
|
140
|
+
/** How far the file at `path` has been read, and into which session: its
|
|
141
|
+
* `transcript`, if it has one. */ export let transcriptOf = async (g, path)=>{
|
|
142
|
+
let [file] = await onDisk(g, path);
|
|
143
|
+
let [row] = await g.get([
|
|
144
|
+
file.entity.eid
|
|
145
|
+
], [
|
|
146
|
+
'transcript'
|
|
147
|
+
]);
|
|
148
|
+
return comp(live(row), 'transcript');
|
|
149
|
+
};
|
|
150
|
+
// Whether a file is its session's first: the one the session names, or any
|
|
151
|
+
// file of a session that names none yet.
|
|
152
|
+
let owns = async (g, session, file)=>{
|
|
153
|
+
if (!session) return true;
|
|
154
|
+
// Ownership needs one stored reference, not the derived transcript status.
|
|
155
|
+
let [s] = await g.read(query`.entity.eid=${session} .fields=session.transcript`);
|
|
156
|
+
let first = comp(s, 'session')?.transcript;
|
|
157
|
+
return !first || first == file;
|
|
158
|
+
};
|
|
159
|
+
// Codex compresses a finished rollout from `X.jsonl` to `X.jsonl.zst`: another
|
|
160
|
+
// file, holding the same lines. A compressed file not read yet takes over the
|
|
161
|
+
// reading of the one it was compressed from, and the session's first file
|
|
162
|
+
// moves with it.
|
|
163
|
+
let compressed = async (g, file, path)=>{
|
|
164
|
+
if (!path.endsWith('.zst')) return undefined;
|
|
165
|
+
let [was] = await onDisk(g, path.slice(0, -'.zst'.length));
|
|
166
|
+
let from = was.entity.eid;
|
|
167
|
+
let [row] = await g.get([
|
|
168
|
+
from
|
|
169
|
+
], [
|
|
170
|
+
'transcript'
|
|
171
|
+
]);
|
|
172
|
+
let held = comp(live(row), 'transcript');
|
|
173
|
+
if (!held) return undefined;
|
|
174
|
+
let session = held.session;
|
|
175
|
+
let [s] = session ? await g.read(query`.entity.eid=${session} .fields=session.transcript`) : [];
|
|
176
|
+
// Compressing a log is no edit to its session, so `updated` stays. The session
|
|
177
|
+
// moves first: stopped between the two, the next read moves the file.
|
|
178
|
+
if (comp(s, 'session')?.transcript == from) {
|
|
179
|
+
await g.apply([
|
|
180
|
+
{
|
|
181
|
+
entity: {
|
|
182
|
+
eid: session
|
|
183
|
+
},
|
|
184
|
+
session: {
|
|
185
|
+
transcript: file.entity.eid
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
], {
|
|
189
|
+
trusted: true,
|
|
190
|
+
stamp: false
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
await g.apply([
|
|
194
|
+
{
|
|
195
|
+
...file,
|
|
196
|
+
transcript: held
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
entity: {
|
|
200
|
+
eid: from
|
|
201
|
+
},
|
|
202
|
+
transcript: null
|
|
203
|
+
}
|
|
204
|
+
], {
|
|
205
|
+
trusted: true
|
|
206
|
+
});
|
|
207
|
+
return held;
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* A tail on `path`, placed after the lines its transcript says were read.
|
|
211
|
+
*
|
|
212
|
+
* ```ts
|
|
213
|
+
* import { tail } from '@yaks/session/tail'
|
|
214
|
+
*
|
|
215
|
+
* // let t = await tail(graph, '/logs/run.out', { session })
|
|
216
|
+
* ```
|
|
217
|
+
*/ export let tail = async (g, path, o = {})=>{
|
|
218
|
+
let [file] = await onDisk(g, path);
|
|
219
|
+
let [row] = await g.get([
|
|
220
|
+
file.entity.eid
|
|
221
|
+
], [
|
|
222
|
+
'transcript'
|
|
223
|
+
]);
|
|
224
|
+
let held = comp(live(row), 'transcript') ?? await compressed(g, file, path);
|
|
225
|
+
let previous = held?.version ?? held?.finished;
|
|
226
|
+
let changed = o.version && previous && previous != o.version;
|
|
227
|
+
let line = changed ? 0 : Number(held?.consumed ?? 0);
|
|
228
|
+
if (changed) {
|
|
229
|
+
await g.apply([
|
|
230
|
+
{
|
|
231
|
+
entity: {
|
|
232
|
+
eid: file.entity.eid
|
|
233
|
+
},
|
|
234
|
+
transcript: {
|
|
235
|
+
consumed: 0,
|
|
236
|
+
finished: null,
|
|
237
|
+
version: o.version
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
], {
|
|
241
|
+
trusted: true
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
let fields = [
|
|
245
|
+
'session.operator',
|
|
246
|
+
'session.source',
|
|
247
|
+
'spawned.parent',
|
|
248
|
+
'created.by',
|
|
249
|
+
'created.via'
|
|
250
|
+
].filter((name)=>{
|
|
251
|
+
let [c, p] = name.split('.');
|
|
252
|
+
return g.vocab.props(c).includes(p);
|
|
253
|
+
});
|
|
254
|
+
let [session] = o.session ? await g.read(fields.length ? query`.entity.eid=${o.session} .fields=${fields}` : query`.entity.eid=${o.session} .fields=`) : [];
|
|
255
|
+
let operatorStored = comp(session, 'session')?.operator;
|
|
256
|
+
let automatic = session && (comp(session, 'session')?.source || comp(session, 'spawned')?.parent || !await humanCaller(g, who(session), session.entity.eid));
|
|
257
|
+
return {
|
|
258
|
+
path,
|
|
259
|
+
...o,
|
|
260
|
+
operator: automatic ? false : operatorStored,
|
|
261
|
+
operatorStored,
|
|
262
|
+
file,
|
|
263
|
+
own: await owns(g, o.session, file.entity.eid),
|
|
264
|
+
known: !!held,
|
|
265
|
+
line,
|
|
266
|
+
marked: line,
|
|
267
|
+
at: o.stream ? 0 : await after(path, line),
|
|
268
|
+
...o.stream ? {
|
|
269
|
+
skip: line
|
|
270
|
+
} : {},
|
|
271
|
+
rest: o.prefix ?? '',
|
|
272
|
+
dec: new TextDecoder()
|
|
273
|
+
};
|
|
274
|
+
};
|
|
275
|
+
let parsed = (text)=>{
|
|
276
|
+
try {
|
|
277
|
+
let e = JSON.parse(text);
|
|
278
|
+
return e && typeof e == 'object' ? e : undefined;
|
|
279
|
+
} catch {
|
|
280
|
+
return undefined // not JSON: diagnostics, not transcript
|
|
281
|
+
;
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
// What reading to `line` writes beside the line's own entities: how far the
|
|
285
|
+
// file is read, and, the first time, the file itself and whose transcript it
|
|
286
|
+
// holds; the session learns its first file, and that a person typed into it.
|
|
287
|
+
let marks = (t, line, typed = false)=>{
|
|
288
|
+
let operator = typed && t.operator == null ? true : t.operator;
|
|
289
|
+
return [
|
|
290
|
+
{
|
|
291
|
+
...t.known ? {
|
|
292
|
+
entity: t.file.entity
|
|
293
|
+
} : t.file,
|
|
294
|
+
transcript: {
|
|
295
|
+
consumed: line,
|
|
296
|
+
...t.version ? {
|
|
297
|
+
version: t.version
|
|
298
|
+
} : {},
|
|
299
|
+
...t.known ? {} : {
|
|
300
|
+
session: t.session
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
...t.own && !t.known || operator != t.operatorStored ? [
|
|
305
|
+
{
|
|
306
|
+
entity: {
|
|
307
|
+
eid: t.session
|
|
308
|
+
},
|
|
309
|
+
session: {
|
|
310
|
+
...t.own && !t.known ? {
|
|
311
|
+
transcript: t.file.entity.eid
|
|
312
|
+
} : {},
|
|
313
|
+
...operator != t.operatorStored ? {
|
|
314
|
+
operator
|
|
315
|
+
} : {}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
] : []
|
|
319
|
+
];
|
|
320
|
+
};
|
|
321
|
+
// Bundles for entities a list already names folded into those.
|
|
322
|
+
let fold = (list, more)=>{
|
|
323
|
+
let out = new Map(list.map((b)=>[
|
|
324
|
+
b.entity.eid,
|
|
325
|
+
b
|
|
326
|
+
]));
|
|
327
|
+
for (let b of more){
|
|
328
|
+
let was = out.get(b.entity.eid);
|
|
329
|
+
out.set(b.entity.eid, was ? merge(was, b) : b);
|
|
330
|
+
}
|
|
331
|
+
return [
|
|
332
|
+
...out.values()
|
|
333
|
+
];
|
|
334
|
+
};
|
|
335
|
+
// A file's first write learns again whether it is its session's first:
|
|
336
|
+
// another file of the session may have been written first since the tail was
|
|
337
|
+
// opened.
|
|
338
|
+
let settle = async (g, t)=>{
|
|
339
|
+
if (!t.known) t.own = await owns(g, t.session, t.file.entity.eid);
|
|
340
|
+
};
|
|
341
|
+
// The session a file names, created now that it has said something, as the
|
|
342
|
+
// entity the harness's id for it names (./who.ts `sessionEid`).
|
|
343
|
+
let created = async (g, t)=>{
|
|
344
|
+
let made = await g.apply([
|
|
345
|
+
{
|
|
346
|
+
entity: {
|
|
347
|
+
eid: '$session'
|
|
348
|
+
},
|
|
349
|
+
session: {
|
|
350
|
+
id: t.id
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
]);
|
|
354
|
+
return made.find((b)=>b.session).entity.eid;
|
|
355
|
+
};
|
|
356
|
+
// What a line's entries owe the requests they carry (./parts.ts).
|
|
357
|
+
let walked = (w, made)=>made.filter((b)=>b.entry || b.tool).flatMap((b)=>step(w, b));
|
|
358
|
+
// The walk a tail keeps, begun before it reads a line: a log read from its
|
|
359
|
+
// start begins one empty, and one taken up partway first follows the lines
|
|
360
|
+
// already read, writing nothing, so its next request knows what came before.
|
|
361
|
+
let follow = async (t, read, o)=>{
|
|
362
|
+
if (!t.walk) {
|
|
363
|
+
t.walk = walker();
|
|
364
|
+
if (t.line && !t.stream && t.session) {
|
|
365
|
+
t.replay = {
|
|
366
|
+
line: 0,
|
|
367
|
+
lines: records(t.path)
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
let replay = t.replay;
|
|
372
|
+
if (!replay) return true;
|
|
373
|
+
let breath = performance.now(), from = replay.line;
|
|
374
|
+
while(replay.line < t.line){
|
|
375
|
+
if (o.signal?.aborted || replay.line > from && performance.now() > (o.until ?? Infinity)) return false;
|
|
376
|
+
if (performance.now() - breath > 50) {
|
|
377
|
+
await new Promise((go)=>setTimeout(go, 0));
|
|
378
|
+
breath = performance.now();
|
|
379
|
+
}
|
|
380
|
+
let raw = await replay.lines.next();
|
|
381
|
+
if (raw.done) break;
|
|
382
|
+
let line = ++replay.line;
|
|
383
|
+
let event = parsed(raw.value);
|
|
384
|
+
if (!event) continue;
|
|
385
|
+
let said = read(event);
|
|
386
|
+
for (let f of said.fixed ?? [])fix(t.walk, f);
|
|
387
|
+
for (let [one, at] of pieced(replay, said, line)){
|
|
388
|
+
walked(t.walk, imported(one, {
|
|
389
|
+
...o,
|
|
390
|
+
session: t.session,
|
|
391
|
+
file: t.file.entity.eid,
|
|
392
|
+
path: t.path,
|
|
393
|
+
line: at,
|
|
394
|
+
own: t.own
|
|
395
|
+
}));
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
await replay.lines.return(undefined);
|
|
399
|
+
t.replay = undefined;
|
|
400
|
+
return true;
|
|
401
|
+
};
|
|
402
|
+
/**
|
|
403
|
+
* Read what the log holds past the tail into its session, a run of lines to a
|
|
404
|
+
* transaction, each entity dated when the harness wrote the line that wrote it
|
|
405
|
+
* where the line says. A line the reader does not recognize, or one that is not
|
|
406
|
+
* JSON at all, becomes nothing: the file keeps it. Answers how many lines were
|
|
407
|
+
* read.
|
|
408
|
+
*
|
|
409
|
+
* ```ts
|
|
410
|
+
* import { claude } from '@yaks/session'
|
|
411
|
+
* import { pull, tail } from '@yaks/session/tail'
|
|
412
|
+
*
|
|
413
|
+
* // let t = await tail(graph, path, { id })
|
|
414
|
+
* // await pull(graph, t, claude, { person })
|
|
415
|
+
* ```
|
|
416
|
+
*/ export let pull = async (g, t, read, o = {})=>{
|
|
417
|
+
let from = t.line;
|
|
418
|
+
let breath = performance.now();
|
|
419
|
+
let run = [];
|
|
420
|
+
let text = 0;
|
|
421
|
+
// What a run of lines writes, in one transaction. Trusted: `transcript` is
|
|
422
|
+
// server-owned, and this is the server reading a file on its own machine. A
|
|
423
|
+
// run the graph refuses is written again a line at a time, so only the line
|
|
424
|
+
// it refuses is left out: reported, and left in the file, since it would be
|
|
425
|
+
// refused again. Any other failure ends the read, past lines this tail has
|
|
426
|
+
// already taken, so the caller opens a new tail where the transcript stands.
|
|
427
|
+
let write = async (lines)=>{
|
|
428
|
+
let { lines: kept, held } = await settled(g, lines);
|
|
429
|
+
let last = lines.at(-1);
|
|
430
|
+
let person = lines.some((l)=>l.person);
|
|
431
|
+
await g.apply(fold(joined(lines.map((l, i)=>({
|
|
432
|
+
...l,
|
|
433
|
+
bundles: kept[i]
|
|
434
|
+
})), held), marks(t, last.line, person)), {
|
|
435
|
+
trusted: true
|
|
436
|
+
});
|
|
437
|
+
t.marked = last.line;
|
|
438
|
+
t.known = true;
|
|
439
|
+
if (person && t.operator == null) t.operator = true;
|
|
440
|
+
t.operatorStored = t.operator;
|
|
441
|
+
};
|
|
442
|
+
// A run written, or, when the graph refuses it, each of its lines alone.
|
|
443
|
+
let land = async (lines)=>{
|
|
444
|
+
try {
|
|
445
|
+
await write(lines);
|
|
446
|
+
} catch (e) {
|
|
447
|
+
if (status(e) >= 500) throw e;
|
|
448
|
+
if (lines.length == 1) return void (o.report ?? console.error)(e);
|
|
449
|
+
for (let l of lines)await land([
|
|
450
|
+
l
|
|
451
|
+
]);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
let flush = async ()=>{
|
|
455
|
+
let lines = run;
|
|
456
|
+
run = [];
|
|
457
|
+
text = 0;
|
|
458
|
+
if (lines.length) await land(lines);
|
|
459
|
+
};
|
|
460
|
+
// What a line says, its entries sitting at line `at`, into the run that
|
|
461
|
+
// reading `line` writes: a run of parts is placed at its first line, and
|
|
462
|
+
// read once the line that ends it is.
|
|
463
|
+
let take = async (said, at, line)=>{
|
|
464
|
+
let kept = o.prose ? said.entries.filter(retained) : said.entries;
|
|
465
|
+
if (!kept.length && !said.about && !said.limits?.length) return;
|
|
466
|
+
t.session ??= await created(g, t);
|
|
467
|
+
// A run is one write, so one reading of `own` holds for all its lines.
|
|
468
|
+
if (!run.length) await settle(g, t);
|
|
469
|
+
let made = imported(said, {
|
|
470
|
+
...o,
|
|
471
|
+
session: t.session,
|
|
472
|
+
file: t.file.entity.eid,
|
|
473
|
+
path: t.path,
|
|
474
|
+
line: at,
|
|
475
|
+
own: t.own
|
|
476
|
+
});
|
|
477
|
+
let signed = made.some((b)=>b.$actor);
|
|
478
|
+
// A line a person signed is a transaction of its own: in a batch, an
|
|
479
|
+
// entity nobody signs is written by the first writer the batch names
|
|
480
|
+
// (@yaks/graph `writers`), and nothing else in the run is theirs.
|
|
481
|
+
if (signed) await flush();
|
|
482
|
+
run.push({
|
|
483
|
+
bundles: fold(made, t.walk ? walked(t.walk, made) : []),
|
|
484
|
+
at: said.at,
|
|
485
|
+
line,
|
|
486
|
+
person: kept.some(typed)
|
|
487
|
+
});
|
|
488
|
+
if (signed || run.length >= RUN || text >= RUN_TEXT) await flush();
|
|
489
|
+
};
|
|
490
|
+
reading: for await (let all of batches(t, o.final)){
|
|
491
|
+
if (!o.prose && !await follow(t, read, o)) {
|
|
492
|
+
t.rest = [
|
|
493
|
+
...all,
|
|
494
|
+
t.rest
|
|
495
|
+
].join('\n');
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
for (let [i, raw] of all.entries()){
|
|
499
|
+
if (o.signal?.aborted || t.line > from && performance.now() > (o.until ?? Infinity)) {
|
|
500
|
+
t.rest = [
|
|
501
|
+
...all.slice(i),
|
|
502
|
+
t.rest
|
|
503
|
+
].join('\n');
|
|
504
|
+
break reading;
|
|
505
|
+
}
|
|
506
|
+
// A long read hands the event loop back every so often: the lease its duty
|
|
507
|
+
// holds and every other duty in the process renew and fire on timers.
|
|
508
|
+
if (performance.now() - breath > 50) {
|
|
509
|
+
await new Promise((go)=>setTimeout(go, 0));
|
|
510
|
+
breath = performance.now();
|
|
511
|
+
}
|
|
512
|
+
let line = ++t.line;
|
|
513
|
+
let event = parsed(raw);
|
|
514
|
+
if (!event) continue;
|
|
515
|
+
let said = timed(read(event), t);
|
|
516
|
+
if (t.walk) {
|
|
517
|
+
for (let f of said.fixed ?? [])fix(t.walk, f);
|
|
518
|
+
}
|
|
519
|
+
for (let [one, at] of pieced(t, said, line))await take(one, at, line);
|
|
520
|
+
text += raw.length;
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
// A run of parts the log ends on is all it will say.
|
|
524
|
+
if (t.part && (o.final || o.prose || t.stream?.done)) {
|
|
525
|
+
let open = t.part;
|
|
526
|
+
t.part = undefined;
|
|
527
|
+
await take({
|
|
528
|
+
entries: [
|
|
529
|
+
open.entry
|
|
530
|
+
]
|
|
531
|
+
}, open.line, t.line);
|
|
532
|
+
}
|
|
533
|
+
await flush();
|
|
534
|
+
// Lines past the last that made an entry, or refused ones, are marked read
|
|
535
|
+
// where the file is done or not yet known; a file still being written has
|
|
536
|
+
// its next entry carry the mark, or `rest` write it once the file is quiet.
|
|
537
|
+
if (o.final || o.prose || t.stream?.done || !t.known) await rest(g, t);
|
|
538
|
+
if (t.stream?.done && !behind(t) && !t.skip && t.version) {
|
|
539
|
+
await g.apply([
|
|
540
|
+
{
|
|
541
|
+
entity: t.file.entity,
|
|
542
|
+
transcript: {
|
|
543
|
+
finished: t.version
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
], {
|
|
547
|
+
trusted: true
|
|
548
|
+
});
|
|
549
|
+
}
|
|
550
|
+
return t.line - from;
|
|
551
|
+
};
|
|
552
|
+
/**
|
|
553
|
+
* Mark a tail's file read to the line the tail stands at, so a resume starts
|
|
554
|
+
* after it and a quiet file reads as imported to its end: what lines that made
|
|
555
|
+
* no entry leave unwritten while the file is still being written (`pull`).
|
|
556
|
+
*
|
|
557
|
+
* ```ts
|
|
558
|
+
* import { rest } from '@yaks/session/tail'
|
|
559
|
+
*
|
|
560
|
+
* // await rest(graph, t) // once its file has gone quiet
|
|
561
|
+
* ```
|
|
562
|
+
*/ export let rest = async (g, t)=>{
|
|
563
|
+
// A run of parts not yet ended is read again from its first line.
|
|
564
|
+
let read = t.part ? t.part.line - 1 : t.line;
|
|
565
|
+
if (read <= t.marked) return;
|
|
566
|
+
t.session ??= await created(g, t);
|
|
567
|
+
await settle(g, t);
|
|
568
|
+
await g.apply(marks(t, read), {
|
|
569
|
+
trusted: true
|
|
570
|
+
});
|
|
571
|
+
t.known = true;
|
|
572
|
+
t.marked = read;
|
|
573
|
+
};
|
|
574
|
+
// What an earlier reading of a line put on an entry that a request now
|
|
575
|
+
// carries on its own entry: a run's whole usage on its ending, Codex's usage
|
|
576
|
+
// on an entry of its own. The usage comes off, with the cost, request and
|
|
577
|
+
// context beside it, and an entry left with nothing of its own goes. `rows`
|
|
578
|
+
// holds what the graph holds at the line's entries (`placesOf`).
|
|
579
|
+
let unspent = (rows, key, line, said, made)=>{
|
|
580
|
+
let now = new Map(made.map((b)=>[
|
|
581
|
+
b.entity.eid,
|
|
582
|
+
b
|
|
583
|
+
]));
|
|
584
|
+
let plumbing = [
|
|
585
|
+
'entry',
|
|
586
|
+
'lines',
|
|
587
|
+
'usage',
|
|
588
|
+
'cost',
|
|
589
|
+
'ask',
|
|
590
|
+
'context_parts',
|
|
591
|
+
'created',
|
|
592
|
+
'updated'
|
|
593
|
+
];
|
|
594
|
+
return placesOf(key, line, said).flatMap((eid)=>{
|
|
595
|
+
let r = rows.get(eid);
|
|
596
|
+
if (!r || r[TOMBSTONE] != null || !r.usage) return [];
|
|
597
|
+
if (now.get(eid)?.usage) return [];
|
|
598
|
+
let own = Object.keys(r).some((k)=>k != 'entity' && !k.startsWith('$') && !plumbing.includes(k));
|
|
599
|
+
return [
|
|
600
|
+
own ? {
|
|
601
|
+
entity: {
|
|
602
|
+
eid
|
|
603
|
+
},
|
|
604
|
+
usage: null,
|
|
605
|
+
cost: null,
|
|
606
|
+
ask: null,
|
|
607
|
+
context_parts: null
|
|
608
|
+
} : {
|
|
609
|
+
entity: {
|
|
610
|
+
eid
|
|
611
|
+
},
|
|
612
|
+
$delete: true
|
|
613
|
+
}
|
|
614
|
+
];
|
|
615
|
+
});
|
|
616
|
+
};
|
|
617
|
+
// The entries a line's place in the file names.
|
|
618
|
+
let placesOf = (key, line, said)=>said.entries.map((_, i)=>derivedEid(`${key} ${line} ${i}`));
|
|
619
|
+
/**
|
|
620
|
+
* Read a log already imported again from its start, for its model requests
|
|
621
|
+
* alone: each request's entry (`ask`, `usage`, `context_parts`) where the
|
|
622
|
+
* session lacks it, the session's `fixed`, and plan limits, each dated when
|
|
623
|
+
* the line was written. What an earlier reading counted elsewhere (on the
|
|
624
|
+
* line's other entries, or as the whole run's usage on its ending) comes off,
|
|
625
|
+
* so each request is counted once. The log can be a copy of the session's
|
|
626
|
+
* own, such as the transcript a provider keeps of a managed run. The
|
|
627
|
+
* transcript's other entries are followed, never written. A run of lines is
|
|
628
|
+
* read and written at once. Answers how many requests it wrote.
|
|
629
|
+
*
|
|
630
|
+
* ```ts
|
|
631
|
+
* import { claude } from '@yaks/session'
|
|
632
|
+
* import { recount } from '@yaks/session/tail'
|
|
633
|
+
*
|
|
634
|
+
* // await recount(graph, path, claude, { session })
|
|
635
|
+
* ```
|
|
636
|
+
*/ export let recount = async (g, path, read, o)=>{
|
|
637
|
+
let [file] = await onDisk(g, path);
|
|
638
|
+
let own = await owns(g, o.session, file.entity.eid);
|
|
639
|
+
let key = own ? o.session : `${o.session} ${path}`;
|
|
640
|
+
let w = walker();
|
|
641
|
+
let line = 0, wrote = 0, placed = false;
|
|
642
|
+
let last = {};
|
|
643
|
+
let run = [];
|
|
644
|
+
let size = 0;
|
|
645
|
+
let flush = async ()=>{
|
|
646
|
+
let lines = run;
|
|
647
|
+
run = [];
|
|
648
|
+
size = 0;
|
|
649
|
+
if (!lines.length) return;
|
|
650
|
+
let places = lines.flatMap((l)=>placesOf(key, l.line, l.said));
|
|
651
|
+
// Which places hold usage is read alone; a whole entry, with all it
|
|
652
|
+
// says, only where the usage is one this reading does not make.
|
|
653
|
+
let rows = new Map();
|
|
654
|
+
for (let r of places.length ? await g.get(places, [
|
|
655
|
+
'usage'
|
|
656
|
+
]) : []){
|
|
657
|
+
if (r) rows.set(r.entity.eid, r);
|
|
658
|
+
}
|
|
659
|
+
let made = new Set(lines.flatMap((l)=>l.made.filter((b)=>b.usage)).map((b)=>b.entity.eid));
|
|
660
|
+
let stale = [
|
|
661
|
+
...rows.values()
|
|
662
|
+
].filter((r)=>r.usage && !made.has(r.entity.eid)).map((r)=>r.entity.eid);
|
|
663
|
+
for (let r of stale.length ? await g.get(stale) : []){
|
|
664
|
+
if (r) rows.set(r.entity.eid, r);
|
|
665
|
+
}
|
|
666
|
+
let gone = lines.map((l)=>unspent(rows, key, l.line, l.said, l.made));
|
|
667
|
+
let { lines: kept, held } = await settled(g, lines, // A line with nothing of its own to write is not written.
|
|
668
|
+
(bundles, i)=>!!gone[i].length || bundles.some((b)=>b.entry || b.rate_limit || b.fixed));
|
|
669
|
+
let all = joined(lines.map((l, i)=>({
|
|
670
|
+
...l,
|
|
671
|
+
bundles: [
|
|
672
|
+
...kept[i],
|
|
673
|
+
...gone[i]
|
|
674
|
+
]
|
|
675
|
+
})), new Set([
|
|
676
|
+
...held,
|
|
677
|
+
...rows.keys()
|
|
678
|
+
]));
|
|
679
|
+
if (!all.length) return;
|
|
680
|
+
// The entries it writes sit at lines of the file: the first write makes
|
|
681
|
+
// the file an entity too.
|
|
682
|
+
await g.apply([
|
|
683
|
+
...placed ? [] : [
|
|
684
|
+
file
|
|
685
|
+
],
|
|
686
|
+
...all
|
|
687
|
+
], {
|
|
688
|
+
trusted: true
|
|
689
|
+
});
|
|
690
|
+
placed = true;
|
|
691
|
+
wrote += kept.flat().filter((b)=>b.entry).length;
|
|
692
|
+
};
|
|
693
|
+
let breath = performance.now();
|
|
694
|
+
for await (let raw of records(path)){
|
|
695
|
+
if (o.signal?.aborted) break;
|
|
696
|
+
if (performance.now() - breath > 50) {
|
|
697
|
+
await new Promise((go)=>setTimeout(go, 0));
|
|
698
|
+
breath = performance.now();
|
|
699
|
+
}
|
|
700
|
+
line++;
|
|
701
|
+
let event = parsed(raw);
|
|
702
|
+
if (!event) continue;
|
|
703
|
+
let said = timed(read(event), last);
|
|
704
|
+
for (let f of said.fixed ?? [])fix(w, f);
|
|
705
|
+
let made = imported(said, {
|
|
706
|
+
session: o.session,
|
|
707
|
+
file: file.entity.eid,
|
|
708
|
+
path,
|
|
709
|
+
line,
|
|
710
|
+
own
|
|
711
|
+
});
|
|
712
|
+
let owed = walked(w, made);
|
|
713
|
+
let asked = made.filter((b)=>b.entry ? b.ask && b.usage : b.provider || b.model || b.rate_limit);
|
|
714
|
+
run.push({
|
|
715
|
+
bundles: fold(asked, owed),
|
|
716
|
+
at: said.at,
|
|
717
|
+
line,
|
|
718
|
+
said,
|
|
719
|
+
made
|
|
720
|
+
});
|
|
721
|
+
size += raw.length;
|
|
722
|
+
if (run.length >= RUN || size >= RUN_TEXT) await flush();
|
|
723
|
+
}
|
|
724
|
+
await flush();
|
|
725
|
+
let over = await ended(g, o.session);
|
|
726
|
+
if (over.length) await g.apply(over, {
|
|
727
|
+
trusted: true
|
|
728
|
+
});
|
|
729
|
+
return wrote;
|
|
730
|
+
};
|
|
731
|
+
// A run's ending that an earlier reading gave the whole run's usage, where
|
|
732
|
+
// the session's requests now carry their own: the usage comes off it, with
|
|
733
|
+
// the cost, request and context beside it, as `unspent` takes it off a line
|
|
734
|
+
// read again. A provider that reports usage only per run keeps it there.
|
|
735
|
+
let ended = async (g, session)=>{
|
|
736
|
+
let [counted] = await g.read(query`.entry.session=${session} .ask .usage !stop .limit=1`);
|
|
737
|
+
if (!counted) return [];
|
|
738
|
+
return (await g.read(query`.entry.session=${session} .stop .usage`)).map((b)=>({
|
|
739
|
+
entity: {
|
|
740
|
+
eid: b.entity.eid
|
|
741
|
+
},
|
|
742
|
+
usage: null,
|
|
743
|
+
cost: null,
|
|
744
|
+
ask: null,
|
|
745
|
+
context_parts: null
|
|
746
|
+
}));
|
|
747
|
+
};
|
|
748
|
+
//# sourceMappingURL=tail.js.map
|