@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/service.js
ADDED
|
@@ -0,0 +1,806 @@
|
|
|
1
|
+
// The duty a host runs at `@yaks/session/service` for as long as it is up:
|
|
2
|
+
// every session a harness ran on this machine, read into the graph from the
|
|
3
|
+
// transcript file the harness keeps (./tail.ts), and pending addressed items
|
|
4
|
+
// delivered to native sessions as the graph's commits make them due
|
|
5
|
+
// (./bus.ts `follow`), beside the transcripts and never waiting on them.
|
|
6
|
+
//
|
|
7
|
+
// The duty opens by freeing the locks whose holder is gone (./reap.ts). Start-up
|
|
8
|
+
// is the one moment that answer is fresh, and holding the duty is what makes
|
|
9
|
+
// this process the one to give it: the lease is renewed for as long as the
|
|
10
|
+
// process that stays up runs, so a command started beside it leaves the reap
|
|
11
|
+
// to it rather than writing the same releases again. A one-shot command reaps
|
|
12
|
+
// and reads nothing: its tails would outlive the lease it gives back.
|
|
13
|
+
//
|
|
14
|
+
// Claude Code keeps each session as `<project>/<session>.jsonl` under its
|
|
15
|
+
// projects directory, and appends to it as the session runs. A transcript
|
|
16
|
+
// written to in the last `full` days is followed as it grows, at full depth:
|
|
17
|
+
// what the person typed, what the harness put in front of the model, what the
|
|
18
|
+
// model said and thought, and every tool call with its result. An older one is
|
|
19
|
+
// read in lazily, one file per pass, and only its prose. A look reads each
|
|
20
|
+
// file for a slice of time, so a long backlog is read over many looks and what
|
|
21
|
+
// a live session says never waits behind it. A session the graph has not met
|
|
22
|
+
// is created under the harness's id for it, and one a person typed into is
|
|
23
|
+
// marked `operator`, which is what tells the person's words apart from an
|
|
24
|
+
// agent's brief.
|
|
25
|
+
//
|
|
26
|
+
// A session whose transcript asks a provider for a run (`using{provider}` on
|
|
27
|
+
// an entry nobody imported) is a managed run: @yaks/spawn launched it and reads
|
|
28
|
+
// its stdout, and its transcript file is left alone.
|
|
29
|
+
//
|
|
30
|
+
// A subagent keeps a transcript of its own, `<session>/subagents/agent-<id>.jsonl`
|
|
31
|
+
// beside its parent's, and is a session of its own: spawned by the session
|
|
32
|
+
// whose directory holds it, from the call its `agent-<id>.meta.json` names,
|
|
33
|
+
// and read like any other, as it is written.
|
|
34
|
+
//
|
|
35
|
+
// A session keeps its full depth for `full` days after it last said anything.
|
|
36
|
+
// Once an hour the duty finds every imported session older than that, from
|
|
37
|
+
// whichever importer, and strips it to its prose: each imported entry the
|
|
38
|
+
// prose-only read would have left out goes, a small batch between looks, so
|
|
39
|
+
// what a session says now never waits behind it. What a person typed and what
|
|
40
|
+
// the model said stay, and so does everything nobody imported.
|
|
41
|
+
import { line, sub } from "@yaks/query";
|
|
42
|
+
import * as fs from "@yaks/file/io";
|
|
43
|
+
import { sleep } from "@yaks/fp";
|
|
44
|
+
import { TOMBSTONE } from "@yaks/graph/bundle";
|
|
45
|
+
import { follow } from "./bus.js";
|
|
46
|
+
import { SESSION } from "./comp.js";
|
|
47
|
+
import { accounted, antigravity, claude, codexRollout, grok, subagent } from "./readers.js";
|
|
48
|
+
import { reapLeases } from "./reap.js";
|
|
49
|
+
import { endGone } from "./ended.js";
|
|
50
|
+
import { behind, callOf, pull, recount, rest, tail, transcriptOf } from "./tail.js";
|
|
51
|
+
import { sessionEid, sessionFor } from "./who.js";
|
|
52
|
+
import { step, walker } from "./parts.js";
|
|
53
|
+
import { archive } from "./archive.js";
|
|
54
|
+
import { accountRows, header, held, homes, runningOf, transcriptsOf, versionOf } from "./files.js";
|
|
55
|
+
export { held, homes, rollouts, transcripts } from "./files.js";
|
|
56
|
+
let str = (v)=>typeof v == 'string' ? v : '';
|
|
57
|
+
/** How long a session keeps its full depth once it stops being written. */ export let FULL = 14 * 24 * 60 * 60 * 1000;
|
|
58
|
+
/** Whether a session is a managed run: one @yaks/spawn launches, claiming
|
|
59
|
+
* its `process` before the launch, and reads from the run's own stream, so
|
|
60
|
+
* the transcript file the run also keeps is not read a second time. A model
|
|
61
|
+
* taking a conversation over here (@yaks/harness `session_send`) makes none:
|
|
62
|
+
* the harness that ran it may write to its file again. */ export let managed = async (g, session)=>{
|
|
63
|
+
if (!g.vocab.comp('process')) return false;
|
|
64
|
+
let [s] = await g.get([
|
|
65
|
+
session
|
|
66
|
+
], [
|
|
67
|
+
'process'
|
|
68
|
+
]);
|
|
69
|
+
return !!s?.process;
|
|
70
|
+
};
|
|
71
|
+
// What the harness says of a subagent in the meta file beside its
|
|
72
|
+
// transcript: the call that started it, and what that call said it was for.
|
|
73
|
+
let meta = (path)=>{
|
|
74
|
+
try {
|
|
75
|
+
let m = JSON.parse(fs.readTextFileSync(`${path.slice(0, -'.jsonl'.length)}.meta.json`));
|
|
76
|
+
let said = (v)=>typeof v == 'string' && v ? v : undefined;
|
|
77
|
+
return {
|
|
78
|
+
call: said(m?.toolUseId),
|
|
79
|
+
title: said(m?.description)
|
|
80
|
+
};
|
|
81
|
+
} catch {
|
|
82
|
+
return {} // no meta file: the link names no call
|
|
83
|
+
;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
// A subagent's session, linked to the session that started it and to the call
|
|
87
|
+
// that did, where the graph holds it, and titled by what that call said it
|
|
88
|
+
// was for: written the first time its transcript is opened, with the parent
|
|
89
|
+
// where the graph has not met it yet. One the hook door met first, or one
|
|
90
|
+
// opened before it was titled, gains its call and title then.
|
|
91
|
+
let spawned = async (g, f)=>{
|
|
92
|
+
let eid = sessionEid(f.id, f.parent);
|
|
93
|
+
let { call: id, title } = meta(f.path);
|
|
94
|
+
let doc = title && g.vocab.comp('doc') ? {
|
|
95
|
+
doc: {
|
|
96
|
+
title
|
|
97
|
+
}
|
|
98
|
+
} : {};
|
|
99
|
+
let [s] = await g.get([
|
|
100
|
+
eid
|
|
101
|
+
], [
|
|
102
|
+
'spawned',
|
|
103
|
+
'doc'
|
|
104
|
+
]);
|
|
105
|
+
let parent = s?.spawned && s[TOMBSTONE] == null ? str(s.spawned.parent) : (await sessionFor(g, f.parent, [
|
|
106
|
+
'session.id'
|
|
107
|
+
]))?.entity.eid;
|
|
108
|
+
let call = parent && id ? callOf(parent, id) : undefined;
|
|
109
|
+
let [made] = call && !s?.spawned?.call ? await g.get([
|
|
110
|
+
call
|
|
111
|
+
]) : [];
|
|
112
|
+
let linked = made?.entry && made[TOMBSTONE] == null ? {
|
|
113
|
+
call
|
|
114
|
+
} : {};
|
|
115
|
+
if (s?.spawned && s[TOMBSTONE] == null) {
|
|
116
|
+
let more = {
|
|
117
|
+
...s.doc ? {} : doc,
|
|
118
|
+
...linked.call ? {
|
|
119
|
+
spawned: linked
|
|
120
|
+
} : {}
|
|
121
|
+
};
|
|
122
|
+
if (Object.keys(more).length) {
|
|
123
|
+
await g.apply([
|
|
124
|
+
{
|
|
125
|
+
entity: {
|
|
126
|
+
eid
|
|
127
|
+
},
|
|
128
|
+
...more
|
|
129
|
+
}
|
|
130
|
+
], {
|
|
131
|
+
trusted: true,
|
|
132
|
+
stamp: false
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return eid;
|
|
136
|
+
}
|
|
137
|
+
await g.apply([
|
|
138
|
+
...parent ? [] : [
|
|
139
|
+
{
|
|
140
|
+
entity: {
|
|
141
|
+
eid: '$parent'
|
|
142
|
+
},
|
|
143
|
+
[SESSION]: {
|
|
144
|
+
id: f.parent
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
],
|
|
148
|
+
{
|
|
149
|
+
entity: {
|
|
150
|
+
eid
|
|
151
|
+
},
|
|
152
|
+
[SESSION]: {
|
|
153
|
+
id: f.id
|
|
154
|
+
},
|
|
155
|
+
spawned: {
|
|
156
|
+
parent: parent ?? '$parent',
|
|
157
|
+
...linked
|
|
158
|
+
},
|
|
159
|
+
...doc
|
|
160
|
+
}
|
|
161
|
+
]);
|
|
162
|
+
return eid;
|
|
163
|
+
};
|
|
164
|
+
// Write what the graph does not already hold as given. A row it holds is being
|
|
165
|
+
// corrected, which is no edit to it, so its `updated` stays; only a new row is
|
|
166
|
+
// stamped. A write stamps even when it changes nothing, so a transcript opened
|
|
167
|
+
// again by each new process would otherwise rewrite every session it names.
|
|
168
|
+
let correct = async (g, bundles)=>{
|
|
169
|
+
let held = new Map((await g.get(bundles.map((b)=>b.entity.eid))).map((b)=>[
|
|
170
|
+
b.entity.eid,
|
|
171
|
+
b
|
|
172
|
+
]));
|
|
173
|
+
let isNew = (b)=>{
|
|
174
|
+
let had = held.get(b.entity.eid);
|
|
175
|
+
return !had || had[TOMBSTONE] != null;
|
|
176
|
+
};
|
|
177
|
+
let lacking = bundles.filter((b)=>isNew(b) || Object.entries(b).some(([c, comp])=>c != 'entity' && Object.entries(comp).some(([p, v])=>held.get(b.entity.eid)[c]?.[p] !== v)));
|
|
178
|
+
let fresh = lacking.filter(isNew);
|
|
179
|
+
let known = lacking.filter((b)=>!isNew(b));
|
|
180
|
+
if (fresh.length) await g.apply(fresh, {
|
|
181
|
+
trusted: true
|
|
182
|
+
});
|
|
183
|
+
if (known.length) await g.apply(known, {
|
|
184
|
+
trusted: true,
|
|
185
|
+
stamp: false
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
// A tail on a transcript, or null where its session is a managed run.
|
|
189
|
+
let opened = async (g, f, signal)=>{
|
|
190
|
+
if (f.runner == 'codex') {
|
|
191
|
+
let s = await sessionFor(g, f.id, [
|
|
192
|
+
'session.id',
|
|
193
|
+
'session.actor',
|
|
194
|
+
'session.persona'
|
|
195
|
+
]);
|
|
196
|
+
if (s && await managed(g, s.entity.eid)) return null;
|
|
197
|
+
let version = f.path.endsWith('.zst') ? versionOf(await stat(f.path)) : undefined;
|
|
198
|
+
let read = version ? await transcriptOf(g, f.path) : undefined;
|
|
199
|
+
if (version && read?.finished == version) return null;
|
|
200
|
+
let stream = version ? archive(f.path, signal) : undefined;
|
|
201
|
+
let prefix = '';
|
|
202
|
+
let meta;
|
|
203
|
+
try {
|
|
204
|
+
if (stream) {
|
|
205
|
+
while(!prefix.includes('\n') && !stream.done && !signal?.aborted && prefix.length < 1024 * 1024){
|
|
206
|
+
prefix += await stream.read() ?? '';
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
meta = JSON.parse(prefix.split('\n')[0]).payload ?? {};
|
|
210
|
+
} catch {
|
|
211
|
+
meta = {};
|
|
212
|
+
}
|
|
213
|
+
} else meta = header(f.path);
|
|
214
|
+
if (signal?.aborted) {
|
|
215
|
+
await stream?.close();
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
let source = meta.source;
|
|
219
|
+
f.parent = source?.subagent?.thread_spawn?.parent_thread_id;
|
|
220
|
+
// A thread is the session that already holds its id, a managed run's
|
|
221
|
+
// above all, whose eid is its own and not the thread's; only one the
|
|
222
|
+
// graph has not met takes its eid from the id. A subagent's parent is
|
|
223
|
+
// found the same way, so it is spawned by the run that started it and
|
|
224
|
+
// works as that run's persona.
|
|
225
|
+
let eid = s?.entity.eid ?? sessionEid(f.id);
|
|
226
|
+
let up = f.parent ? (await sessionFor(g, f.parent, [
|
|
227
|
+
'session.id'
|
|
228
|
+
]))?.entity.eid : undefined;
|
|
229
|
+
let parent = f.parent && (up ?? sessionEid(f.parent));
|
|
230
|
+
// `codex exec` is a program's run, so its first words are a brief, not a
|
|
231
|
+
// person's. A session wears what its checkout carries, as a hook says it
|
|
232
|
+
// does for Claude Code (./tools.ts); Codex has no hook to say it.
|
|
233
|
+
let automatic = !!parent || meta.source == 'exec';
|
|
234
|
+
let named = s?.[SESSION];
|
|
235
|
+
let worn = !parent && !named?.actor && !named?.persona && typeof meta.cwd == 'string' ? await (await import("@yaks/persona")).carried(g, meta.cwd) : undefined;
|
|
236
|
+
await correct(g, [
|
|
237
|
+
...parent && !up ? [
|
|
238
|
+
{
|
|
239
|
+
entity: {
|
|
240
|
+
eid: parent
|
|
241
|
+
},
|
|
242
|
+
session: {
|
|
243
|
+
id: f.parent
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
] : [],
|
|
247
|
+
{
|
|
248
|
+
entity: {
|
|
249
|
+
eid
|
|
250
|
+
},
|
|
251
|
+
session: {
|
|
252
|
+
id: f.id,
|
|
253
|
+
...automatic ? {
|
|
254
|
+
operator: false
|
|
255
|
+
} : {},
|
|
256
|
+
...worn ? {
|
|
257
|
+
persona: worn.persona.entity.eid
|
|
258
|
+
} : {}
|
|
259
|
+
},
|
|
260
|
+
...parent ? {
|
|
261
|
+
spawned: {
|
|
262
|
+
parent
|
|
263
|
+
}
|
|
264
|
+
} : {}
|
|
265
|
+
}
|
|
266
|
+
]);
|
|
267
|
+
return await tail(g, f.path, {
|
|
268
|
+
session: eid,
|
|
269
|
+
id: f.id,
|
|
270
|
+
stream,
|
|
271
|
+
prefix,
|
|
272
|
+
version
|
|
273
|
+
});
|
|
274
|
+
} catch (error) {
|
|
275
|
+
await stream?.close();
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (f.parent) {
|
|
280
|
+
return tail(g, f.path, {
|
|
281
|
+
session: await spawned(g, {
|
|
282
|
+
...f,
|
|
283
|
+
parent: f.parent
|
|
284
|
+
}),
|
|
285
|
+
id: f.id
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
let s = await sessionFor(g, f.id, [
|
|
289
|
+
'session.id'
|
|
290
|
+
]);
|
|
291
|
+
if (s && await managed(g, s.entity.eid)) return null;
|
|
292
|
+
return tail(g, f.path, {
|
|
293
|
+
...s ? {
|
|
294
|
+
session: s.entity.eid
|
|
295
|
+
} : {},
|
|
296
|
+
id: f.id
|
|
297
|
+
});
|
|
298
|
+
};
|
|
299
|
+
// How a transcript's lines read: a subagent's are all side conversations.
|
|
300
|
+
let format = (f, t = {})=>f.runner == 'grok' ? accounted(grok, f.account) : f.runner == 'antigravity' ? accounted(antigravity, f.account) : f.runner == 'codex' ? (e)=>{
|
|
301
|
+
let said = accounted(codexRollout, f.account)(e);
|
|
302
|
+
if (e.type == 'session_meta' && e.payload?.id != f.id) return {
|
|
303
|
+
entries: []
|
|
304
|
+
};
|
|
305
|
+
if (f.parent || t.operator === false) {
|
|
306
|
+
said.entries = said.entries.map((c)=>c.content && !c.output && !c.result ? {
|
|
307
|
+
...c,
|
|
308
|
+
notice: {}
|
|
309
|
+
} : c);
|
|
310
|
+
}
|
|
311
|
+
return said;
|
|
312
|
+
} : f.parent ? subagent : claude;
|
|
313
|
+
let stat = async (path)=>{
|
|
314
|
+
try {
|
|
315
|
+
return await fs.stat(path);
|
|
316
|
+
} catch {
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
let pending = (t, size)=>behind(t) || (t.stream ? !t.stream.done : t.at < size);
|
|
321
|
+
/** How long a look reads one transcript before it turns to the next, in
|
|
322
|
+
* milliseconds. */ export let SLICE = 100;
|
|
323
|
+
/** How long a transcript is unwritten before a look marks the lines it read
|
|
324
|
+
* that made no entry (./tail.ts `rest`), in milliseconds. */ export let QUIET = 5000;
|
|
325
|
+
/** How long one look reads in all, in milliseconds. */ export let BUDGET = 1000;
|
|
326
|
+
/** How often a look lists every transcript again, in milliseconds. Listing
|
|
327
|
+
* and asking after each of the thousands a box keeps is a look's dearest
|
|
328
|
+
* part, so the looks between listings ask only after the transcripts open or
|
|
329
|
+
* written in the last {@link HOT}, and the next one to read in. A new
|
|
330
|
+
* transcript, or an old one written again, is found by the next listing. */ export let SCAN = 30_000;
|
|
331
|
+
/** How lately a transcript was written for the looks between listings to ask
|
|
332
|
+
* after it, in milliseconds. */ export let HOT = 10 * 60_000;
|
|
333
|
+
// Every transcript, listed again once `scan` has passed since the last
|
|
334
|
+
// listing; `all` says whether this look listed them.
|
|
335
|
+
let listed = async (from, seen, now, scan)=>{
|
|
336
|
+
let last = seen.listed;
|
|
337
|
+
if (last && now - last.at < scan) return {
|
|
338
|
+
files: last.files,
|
|
339
|
+
all: false
|
|
340
|
+
};
|
|
341
|
+
let files = await held(from);
|
|
342
|
+
seen.listed = {
|
|
343
|
+
at: now,
|
|
344
|
+
files
|
|
345
|
+
};
|
|
346
|
+
return {
|
|
347
|
+
files,
|
|
348
|
+
all: true
|
|
349
|
+
};
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* One look at every transcript the accounts `from` keep: each written to in the last
|
|
353
|
+
* `full` milliseconds is read on at full depth, and one older transcript not
|
|
354
|
+
* yet read to its end is read in, its prose only. The one least behind is read
|
|
355
|
+
* first, each for a `slice` (and at least a line), until the look has read for
|
|
356
|
+
* its `budget`: a long backlog is read over many looks, and what a live
|
|
357
|
+
* session says next never waits behind it. A look whose `signal` aborts stops
|
|
358
|
+
* between lines, and the next process reads on from there. Every transcript is
|
|
359
|
+
* listed again only each `scan` ({@link SCAN}).
|
|
360
|
+
*/ export let look = async (g, from, seen, o = {})=>{
|
|
361
|
+
let now = o.now ?? Date.now();
|
|
362
|
+
let full = o.full ?? FULL;
|
|
363
|
+
let start = performance.now();
|
|
364
|
+
let spent = ()=>performance.now() - start >= (o.budget ?? BUDGET);
|
|
365
|
+
let old;
|
|
366
|
+
let read = ()=>({
|
|
367
|
+
person: o.person,
|
|
368
|
+
signal: o.signal,
|
|
369
|
+
until: performance.now() + (o.slice ?? SLICE)
|
|
370
|
+
});
|
|
371
|
+
// Each transcript with lines to read: how far behind a tail on it is, or
|
|
372
|
+
// when it was written where none is open yet.
|
|
373
|
+
let owed = [];
|
|
374
|
+
let { files, all } = await listed(from, seen, now, o.scan ?? SCAN);
|
|
375
|
+
let written = seen.written ??= new Map();
|
|
376
|
+
for (let f of files){
|
|
377
|
+
let t = seen.tails.get(f.path);
|
|
378
|
+
if (!all) {
|
|
379
|
+
// Between listings: what is open or written lately, and the next old
|
|
380
|
+
// transcript to read in.
|
|
381
|
+
let was = written.get(f.path);
|
|
382
|
+
let hot = !!t || was != null && now - was < HOT;
|
|
383
|
+
let next = !old && was != null && !seen.done.has(f.path);
|
|
384
|
+
if (!hot && !next) continue;
|
|
385
|
+
}
|
|
386
|
+
let st = await stat(f.path);
|
|
387
|
+
if (st) written.set(f.path, st.mtime?.getTime() ?? 0);
|
|
388
|
+
if (f.path.endsWith('.zst')) {
|
|
389
|
+
let version = versionOf(st);
|
|
390
|
+
seen.versions ??= new Map();
|
|
391
|
+
if (seen.versions.has(f.path) && seen.versions.get(f.path) != version) {
|
|
392
|
+
await t?.stream?.close();
|
|
393
|
+
seen.tails.delete(f.path);
|
|
394
|
+
seen.done.delete(f.path);
|
|
395
|
+
seen.retry?.delete(f.path);
|
|
396
|
+
t = undefined;
|
|
397
|
+
}
|
|
398
|
+
seen.versions.set(f.path, version);
|
|
399
|
+
}
|
|
400
|
+
if ((seen.retry?.get(f.path) ?? 0) > now) continue;
|
|
401
|
+
let at = st?.mtime?.getTime() ?? 0;
|
|
402
|
+
if (!st || t === null) continue;
|
|
403
|
+
if (t && !pending(t, st.size)) {
|
|
404
|
+
// Read to a file now quiet: its last lines are marked read.
|
|
405
|
+
if (now - at >= QUIET) await rest(g, t).catch(o.report ?? console.error);
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
if (now - at >= full) {
|
|
409
|
+
if (!seen.done.has(f.path)) old ??= {
|
|
410
|
+
f,
|
|
411
|
+
at,
|
|
412
|
+
prose: true
|
|
413
|
+
};
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
owed.push({
|
|
417
|
+
f,
|
|
418
|
+
at,
|
|
419
|
+
...t ? {
|
|
420
|
+
lag: st.size - t.at + t.rest.length
|
|
421
|
+
} : {}
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
// The tails already open, least behind first, then the rest, most recently
|
|
425
|
+
// written first: a live session's next lines lead every look. Of the rest,
|
|
426
|
+
// sessions come before subagents, as `transcripts()` lists them: a subagent
|
|
427
|
+
// writes after the call that started it, so its transcript is the newer one,
|
|
428
|
+
// and opened first it would find no call to link to.
|
|
429
|
+
owed.sort((a, b)=>a.lag != null && b.lag != null ? a.lag - b.lag : a.lag != null ? -1 : b.lag != null ? 1 : Number(!!a.f.parent) - Number(!!b.f.parent) || b.at - a.at);
|
|
430
|
+
// One slice cannot be monopolized by growing tails. The rest of the look
|
|
431
|
+
// retains live priority; a cold source and old backfill both get a turn.
|
|
432
|
+
let turns = seen.turns ??= new Map();
|
|
433
|
+
let candidates = [
|
|
434
|
+
...owed,
|
|
435
|
+
...old ? [
|
|
436
|
+
old
|
|
437
|
+
] : []
|
|
438
|
+
].filter(({ f })=>!f.parent || !owed.some((parent)=>parent.f.id == f.parent && !parent.f.parent && !seen.tails.has(parent.f.path)));
|
|
439
|
+
// New sources join behind those already waiting, including when the old
|
|
440
|
+
// backfill finishes a small file and discovers the next one.
|
|
441
|
+
for (let { f } of candidates.toSorted((a, b)=>a.at - b.at)){
|
|
442
|
+
if (!turns.has(f.path)) turns.set(f.path, seen.turn = (seen.turn ?? 0) + 1);
|
|
443
|
+
}
|
|
444
|
+
let fair = candidates.sort((a, b)=>turns.get(a.f.path) - turns.get(b.f.path))[0];
|
|
445
|
+
if (fair) {
|
|
446
|
+
owed = [
|
|
447
|
+
fair,
|
|
448
|
+
...owed.filter((d)=>d != fair)
|
|
449
|
+
];
|
|
450
|
+
if (fair == old) old = undefined;
|
|
451
|
+
}
|
|
452
|
+
for (let [i, { f, prose }] of owed.entries()){
|
|
453
|
+
if (o.signal?.aborted || i && spent()) return;
|
|
454
|
+
let t = seen.tails.get(f.path);
|
|
455
|
+
try {
|
|
456
|
+
t ??= await opened(g, f, o.signal);
|
|
457
|
+
seen.tails.set(f.path, t);
|
|
458
|
+
if (t) await pull(g, t, format(f, t), {
|
|
459
|
+
...read(),
|
|
460
|
+
prose
|
|
461
|
+
});
|
|
462
|
+
turns.set(f.path, seen.turn = (seen.turn ?? 0) + 1);
|
|
463
|
+
if (prose && !o.signal?.aborted && !(t && pending(t, (await stat(f.path))?.size ?? 0))) {
|
|
464
|
+
seen.done.add(f.path);
|
|
465
|
+
seen.tails.delete(f.path);
|
|
466
|
+
}
|
|
467
|
+
} catch (error) {
|
|
468
|
+
seen.tails.delete(f.path);
|
|
469
|
+
await t?.stream?.close();
|
|
470
|
+
if (!f.path.endsWith('.zst')) throw error;
|
|
471
|
+
(seen.retry ??= new Map()).set(f.path, now + 60000);
|
|
472
|
+
(o.report ?? console.error)(error);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (!old || o.signal?.aborted || owed.length && spent()) return;
|
|
476
|
+
// Keep the cursor and unread bytes between backfill slices.
|
|
477
|
+
let f = old.f;
|
|
478
|
+
let t = seen.tails.get(f.path);
|
|
479
|
+
try {
|
|
480
|
+
t ??= await opened(g, f, o.signal);
|
|
481
|
+
seen.tails.set(f.path, t);
|
|
482
|
+
if (t) await pull(g, t, format(f, t), {
|
|
483
|
+
...read(),
|
|
484
|
+
prose: true
|
|
485
|
+
});
|
|
486
|
+
turns.set(f.path, seen.turn = (seen.turn ?? 0) + 1);
|
|
487
|
+
} catch (error) {
|
|
488
|
+
seen.tails.delete(f.path);
|
|
489
|
+
await t?.stream?.close();
|
|
490
|
+
if (!f.path.endsWith('.zst')) throw error;
|
|
491
|
+
(seen.retry ??= new Map()).set(f.path, now + 60000);
|
|
492
|
+
(o.report ?? console.error)(error);
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
if (!o.signal?.aborted && !(t && pending(t, (await stat(f.path))?.size ?? 0))) {
|
|
496
|
+
seen.done.add(f.path);
|
|
497
|
+
seen.tails.delete(f.path);
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
// What a strip takes from an imported entry: all of it where there is no
|
|
501
|
+
// prose (a call), and each whose prose sits beside a kind the prose-only read
|
|
502
|
+
// leaves out (`prose` in ./tail.ts), of the kinds the vocabulary has. An entry
|
|
503
|
+
// that records what a turn cost stays, since the session's `cost` is its sum.
|
|
504
|
+
let STRIPPED = [
|
|
505
|
+
'reasoning',
|
|
506
|
+
'notice',
|
|
507
|
+
'result',
|
|
508
|
+
'error',
|
|
509
|
+
'refusal',
|
|
510
|
+
'exception',
|
|
511
|
+
'stop',
|
|
512
|
+
'usage'
|
|
513
|
+
];
|
|
514
|
+
let stripped = (g)=>{
|
|
515
|
+
let kept = STRIPPED.filter((c)=>g.vocab.comp(c)).reduce((q, c)=>line`${sub(q)}|.${c}`, line`!content`);
|
|
516
|
+
return g.vocab.comp('cost') ? line`.lines&!using&!cost&(${sub(kept)})` : line`.lines&!using&(${sub(kept)})`;
|
|
517
|
+
};
|
|
518
|
+
/**
|
|
519
|
+
* One bounded page of imported entries to strip from a quiet session. Empty
|
|
520
|
+
* pages give the duty its turn back after a recent or already stripped session.
|
|
521
|
+
* The newest entry is checked before every page, so a session that speaks again
|
|
522
|
+
* keeps its remaining depth. Identity cursors survive the entries' deletion.
|
|
523
|
+
*/ export let stale = async function*(g, o = {}) {
|
|
524
|
+
let before = new Date((o.now ?? Date.now()) - (o.full ?? FULL)).toISOString();
|
|
525
|
+
let page = (query, after, limit = 1)=>after ? line`${sub(query)} .fields=entity.eid .order=entity.eid .limit=${limit} .after=${after}` : line`${sub(query)} .fields=entity.eid .order=entity.eid .limit=${limit}`;
|
|
526
|
+
let after;
|
|
527
|
+
while(!o.signal?.aborted){
|
|
528
|
+
let [found] = await g.rows(page(line`.session`, after));
|
|
529
|
+
if (!found) return;
|
|
530
|
+
after = found.eid;
|
|
531
|
+
let cursor;
|
|
532
|
+
while(!o.signal?.aborted){
|
|
533
|
+
let [last] = await g.rows(line`.entry.session=${after} .order=-entry.seq .limit=1 .fields=created.at`);
|
|
534
|
+
if ((last?.['created.at'] ?? '') >= before) break;
|
|
535
|
+
let rows = await g.rows(page(line`${sub(stripped(g))} .entry.session=${after}`, cursor, BATCH));
|
|
536
|
+
if (!rows.length) break;
|
|
537
|
+
cursor = rows[rows.length - 1].eid;
|
|
538
|
+
yield rows.map((b)=>b.eid);
|
|
539
|
+
}
|
|
540
|
+
yield [];
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
/** How many entries one step of a strip deletes. */ export let BATCH = 50;
|
|
544
|
+
/** One step of a strip: these entries, deleted. */ export let strip = async (g, eids)=>{
|
|
545
|
+
if (eids.length) {
|
|
546
|
+
await g.apply(eids.map((eid)=>({
|
|
547
|
+
entity: {
|
|
548
|
+
eid
|
|
549
|
+
},
|
|
550
|
+
$delete: true
|
|
551
|
+
})));
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
// How often the duty looks for sessions to strip.
|
|
555
|
+
let HOUR = 60 * 60 * 1000;
|
|
556
|
+
/** Free the locks whose holder is gone, then read transcripts in, end the
|
|
557
|
+
* sessions whose process is gone and strip the stale sessions until `signal`
|
|
558
|
+
* aborts. A pass that fails is logged and
|
|
559
|
+
* made again on the next, where the transcript stands. What the config's
|
|
560
|
+
* `person` typed is signed with them. */ export let service = async (host, options = {}, signal = AbortSignal.abort())=>{
|
|
561
|
+
await reapLeases(host.graph.storage);
|
|
562
|
+
if (signal.aborted) return;
|
|
563
|
+
let bus = follow(host, signal, {
|
|
564
|
+
every: options.every,
|
|
565
|
+
quiet: options.quiet
|
|
566
|
+
});
|
|
567
|
+
let said = host.config?.person;
|
|
568
|
+
let person = said && ((await host.graph.address([
|
|
569
|
+
said
|
|
570
|
+
])).get(said) ?? said);
|
|
571
|
+
let seen = {
|
|
572
|
+
tails: new Map(),
|
|
573
|
+
done: new Set()
|
|
574
|
+
};
|
|
575
|
+
let trimming;
|
|
576
|
+
let due = 0;
|
|
577
|
+
let reconcile = 0;
|
|
578
|
+
let from = [];
|
|
579
|
+
let named = new Set();
|
|
580
|
+
let found = 0;
|
|
581
|
+
try {
|
|
582
|
+
while(!signal.aborted){
|
|
583
|
+
try {
|
|
584
|
+
// The accounts are found again as often as transcripts are listed,
|
|
585
|
+
// so one signed in while the server runs is read too.
|
|
586
|
+
if (Date.now() >= found) {
|
|
587
|
+
found = Date.now() + (options.scan ?? SCAN);
|
|
588
|
+
from = await homes(options.homes, options.find);
|
|
589
|
+
let fresh = from.filter((h)=>!named.has(h.dir));
|
|
590
|
+
if (fresh.length) {
|
|
591
|
+
await host.graph.apply(accountRows(fresh), {
|
|
592
|
+
trusted: true
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
for (let h of fresh)named.add(h.dir);
|
|
596
|
+
}
|
|
597
|
+
await look(host.graph, from, seen, {
|
|
598
|
+
...person ? {
|
|
599
|
+
person: person
|
|
600
|
+
} : {},
|
|
601
|
+
full: options.full,
|
|
602
|
+
scan: options.scan,
|
|
603
|
+
signal
|
|
604
|
+
});
|
|
605
|
+
if (Date.now() >= reconcile) {
|
|
606
|
+
reconcile = Date.now() + HOUR;
|
|
607
|
+
await endGone(host.graph, {
|
|
608
|
+
claude: from.filter((h)=>h.runner == 'claude').map((h)=>({
|
|
609
|
+
projects: transcriptsOf(h),
|
|
610
|
+
running: runningOf(h)
|
|
611
|
+
}))
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
if (!trimming && Date.now() >= due) {
|
|
615
|
+
trimming = stale(host.graph, {
|
|
616
|
+
full: options.full,
|
|
617
|
+
signal
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
if (trimming) {
|
|
621
|
+
let page;
|
|
622
|
+
try {
|
|
623
|
+
page = await trimming.next();
|
|
624
|
+
} catch (e) {
|
|
625
|
+
trimming = undefined;
|
|
626
|
+
throw e;
|
|
627
|
+
}
|
|
628
|
+
if (page.done) {
|
|
629
|
+
trimming = undefined;
|
|
630
|
+
due = Date.now() + HOUR;
|
|
631
|
+
} else {
|
|
632
|
+
// The iterator has advanced before deletion, so a failed page is
|
|
633
|
+
// skipped until the next hourly pass rather than retried forever.
|
|
634
|
+
await strip(host.graph, page.value);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
} catch (e) {
|
|
638
|
+
console.error('transcripts —', e);
|
|
639
|
+
}
|
|
640
|
+
await sleep(options.every ?? 1000, signal);
|
|
641
|
+
}
|
|
642
|
+
} finally{
|
|
643
|
+
await bus;
|
|
644
|
+
await Promise.all([
|
|
645
|
+
...seen.tails.values()
|
|
646
|
+
].map((t)=>t?.stream?.close()));
|
|
647
|
+
}
|
|
648
|
+
};
|
|
649
|
+
// How many model requests a session's entries hold.
|
|
650
|
+
let requests = async (g, session)=>session ? (await g.rows(line`.entry.session=${session} .ask .usage .fields=entity.eid`)).length : 0;
|
|
651
|
+
// A transcript read to its end, as a look reads it but with no budget: at
|
|
652
|
+
// full depth where it was written in the last `full` milliseconds, else its
|
|
653
|
+
// prose. A managed run's is left alone. Answers how many requests it read.
|
|
654
|
+
let through = async (g, f, o)=>{
|
|
655
|
+
let st = await stat(f.path);
|
|
656
|
+
if (!st) return 0;
|
|
657
|
+
let prose = o.now - (st.mtime?.getTime() ?? 0) >= o.full;
|
|
658
|
+
let t = null;
|
|
659
|
+
try {
|
|
660
|
+
t = await opened(g, f, o.signal);
|
|
661
|
+
if (!t || !pending(t, st.size)) return 0;
|
|
662
|
+
let had = await requests(g, t.session);
|
|
663
|
+
while(!o.signal?.aborted && pending(t, st.size)){
|
|
664
|
+
let at = t.at;
|
|
665
|
+
await pull(g, t, format(f, t), {
|
|
666
|
+
...o,
|
|
667
|
+
prose
|
|
668
|
+
});
|
|
669
|
+
if (t.at == at && !behind(t) && !t.stream) break;
|
|
670
|
+
}
|
|
671
|
+
await rest(g, t);
|
|
672
|
+
return await requests(g, t.session) - had;
|
|
673
|
+
} catch (error) {
|
|
674
|
+
if (!f.path.endsWith('.zst')) throw error;
|
|
675
|
+
console.error(error);
|
|
676
|
+
return 0;
|
|
677
|
+
} finally{
|
|
678
|
+
await t?.stream?.close();
|
|
679
|
+
}
|
|
680
|
+
};
|
|
681
|
+
/**
|
|
682
|
+
* Read every transcript written since `since` to its end, as the duty would
|
|
683
|
+
* but at once, then again for its model requests (./tail.ts `recount`), and
|
|
684
|
+
* walk the graph's own sessions for theirs: what was written before requests
|
|
685
|
+
* were read and measured; and title each subagent read before sessions were
|
|
686
|
+
* titled. What `person` typed is signed with them. A managed run's
|
|
687
|
+
* transcript is @yaks/spawn's (`recounted` there). Answers how many requests
|
|
688
|
+
* it measured.
|
|
689
|
+
*
|
|
690
|
+
* ```ts
|
|
691
|
+
* import { recounted } from '@yaks/session/service'
|
|
692
|
+
*
|
|
693
|
+
* // await recounted(graph, '2026-10-13')
|
|
694
|
+
* ```
|
|
695
|
+
*/ export let recounted = async (g, since, o = {})=>{
|
|
696
|
+
let after = Date.parse(since);
|
|
697
|
+
let now = o.now ?? Date.now();
|
|
698
|
+
let from = await homes(o.homes, o.find);
|
|
699
|
+
if (from.length) await g.apply(accountRows(from), {
|
|
700
|
+
trusted: true
|
|
701
|
+
});
|
|
702
|
+
let all = await held(from);
|
|
703
|
+
let found = [];
|
|
704
|
+
for (let f of all){
|
|
705
|
+
if (((await stat(f.path))?.mtime?.getTime() ?? 0) >= after) found.push(f);
|
|
706
|
+
}
|
|
707
|
+
let wrote = 0;
|
|
708
|
+
for (let f of found){
|
|
709
|
+
if (o.signal?.aborted) break;
|
|
710
|
+
wrote += await through(g, f, {
|
|
711
|
+
person: o.person,
|
|
712
|
+
full: o.full ?? FULL,
|
|
713
|
+
now,
|
|
714
|
+
signal: o.signal
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
for (let f of found.filter((f)=>!f.path.endsWith('.zst'))){
|
|
718
|
+
if (o.signal?.aborted) break;
|
|
719
|
+
let session = (await transcriptOf(g, f.path))?.session;
|
|
720
|
+
if (!session) continue;
|
|
721
|
+
if (await managed(g, String(session))) continue;
|
|
722
|
+
// A subagent read before sessions were titled is titled now.
|
|
723
|
+
if (f.runner != 'codex' && f.parent) {
|
|
724
|
+
await spawned(g, {
|
|
725
|
+
...f,
|
|
726
|
+
parent: f.parent
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
let [s] = await g.get([
|
|
730
|
+
String(session)
|
|
731
|
+
], [
|
|
732
|
+
SESSION
|
|
733
|
+
]);
|
|
734
|
+
if (f.runner == 'codex') {
|
|
735
|
+
let source = header(f.path).source;
|
|
736
|
+
f.parent = source?.subagent?.thread_spawn?.parent_thread_id;
|
|
737
|
+
}
|
|
738
|
+
let operator = s?.[SESSION]?.operator;
|
|
739
|
+
wrote += await recount(g, f.path, format(f, {
|
|
740
|
+
operator
|
|
741
|
+
}), {
|
|
742
|
+
session: String(session),
|
|
743
|
+
signal: o.signal
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
return wrote + await natives(g, since, o.signal);
|
|
747
|
+
};
|
|
748
|
+
/**
|
|
749
|
+
* The copy each provider keeps of its threads on this machine (Claude Code's
|
|
750
|
+
* transcripts, Codex's rollouts), by the provider's id for the thread: where
|
|
751
|
+
* it is and how its lines read. A managed run's own log can be tidied away
|
|
752
|
+
* while its provider's copy stays (@yaks/spawn `recounted`).
|
|
753
|
+
*
|
|
754
|
+
* ```ts
|
|
755
|
+
* import { kept } from '@yaks/session/service'
|
|
756
|
+
*
|
|
757
|
+
* // let copy = (await kept()).get(thread)
|
|
758
|
+
* ```
|
|
759
|
+
*/ export let kept = async (o = {})=>new Map((await held(await homes(o.homes, o.find))).filter((f)=>!f.parent && !f.path.endsWith('.zst')).map((f)=>[
|
|
760
|
+
f.id,
|
|
761
|
+
{
|
|
762
|
+
path: f.path,
|
|
763
|
+
read: format(f)
|
|
764
|
+
}
|
|
765
|
+
]));
|
|
766
|
+
// What each request of the graph's own sessions carried (./parts.ts), walked
|
|
767
|
+
// from their entries as the graph holds them, for the requests written before
|
|
768
|
+
// the runner recorded it at send time (./react.ts). Their fixed part never
|
|
769
|
+
// reached the graph, so it is sized from each session's first request.
|
|
770
|
+
let natives = async (g, since, signal)=>{
|
|
771
|
+
let owed = await g.rows(line`.ask .usage !lines !context_parts .created.at>=${since} .fields=entry.session`);
|
|
772
|
+
let sessions = [
|
|
773
|
+
...new Set(owed.map((r)=>r['entry.session']))
|
|
774
|
+
].filter((s)=>!!s);
|
|
775
|
+
let names = new Map((await g.read('.tool *')).map((b)=>[
|
|
776
|
+
b.entity.eid,
|
|
777
|
+
String(b.tool.name)
|
|
778
|
+
]));
|
|
779
|
+
let wrote = 0;
|
|
780
|
+
for (let session of sessions){
|
|
781
|
+
if (signal?.aborted) break;
|
|
782
|
+
let w = walker({
|
|
783
|
+
names
|
|
784
|
+
});
|
|
785
|
+
let [held] = await g.get([
|
|
786
|
+
session
|
|
787
|
+
], [
|
|
788
|
+
'fixed'
|
|
789
|
+
]);
|
|
790
|
+
let bundles = [];
|
|
791
|
+
for (let b of (await g.read(line`.entry.session=${session} .order=entry.seq *`))){
|
|
792
|
+
let made = step(w, b);
|
|
793
|
+
if (!b.usage || b.context_parts) continue;
|
|
794
|
+
bundles.push(...made.filter((m)=>!m.fixed || !held?.fixed));
|
|
795
|
+
}
|
|
796
|
+
for(let i = 0; i < bundles.length; i += BATCH){
|
|
797
|
+
await g.apply(bundles.slice(i, i + BATCH), {
|
|
798
|
+
trusted: true,
|
|
799
|
+
stamp: false
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
wrote += bundles.filter((b)=>b.context_parts).length;
|
|
803
|
+
}
|
|
804
|
+
return wrote;
|
|
805
|
+
};
|
|
806
|
+
//# sourceMappingURL=service.js.map
|