@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/status.js
ADDED
|
@@ -0,0 +1,1222 @@
|
|
|
1
|
+
// What a transcript is doing, computed from its entries — never stored. The
|
|
2
|
+
// rule is written twice on purpose, once over bundles (for the runner,
|
|
3
|
+
// @yaks/ram, and any code holding entries) and once as SQL (the
|
|
4
|
+
// `session.status` derived property @yaks/sqlite reads and filters through),
|
|
5
|
+
// and a test holds the two together. There is no third copy: a stored `status`
|
|
6
|
+
// property that had to be kept in sync is exactly what the old session
|
|
7
|
+
// component was, and why its views disagreed.
|
|
8
|
+
//
|
|
9
|
+
// Settled means nothing is outstanding. The newest entry covers most of it, but
|
|
10
|
+
// not all: a model that returns prose before it calls a tool leaves an `output`
|
|
11
|
+
// as the newest entry in the middle of its turn, and reading that alone ended a
|
|
12
|
+
// run 14 minutes early (T-35230). So an open call — one the newest ask made
|
|
13
|
+
// that no result answers — outranks the newest entry, and it is the same set
|
|
14
|
+
// `react` runs next, so the status and the step cannot disagree.
|
|
15
|
+
// an open call → running a tool owes an answer, whatever landed after
|
|
16
|
+
// input, result → pending the model owes a turn
|
|
17
|
+
// ask, call → running a model or a tool owes an answer
|
|
18
|
+
// output → settled the turn returned prose and asked for nothing
|
|
19
|
+
// stop → stopped nothing may be done
|
|
20
|
+
// exception → failed the runner could not continue past it
|
|
21
|
+
// refusal → failed a deliberate no, until new input
|
|
22
|
+
// `pending` is the runner's to answer (./run.ts), and it answers only a
|
|
23
|
+
// transcript that asked it: one carrying a `using` (its request) or an `ask`
|
|
24
|
+
// (a turn it took, not one read from a harness's file). One with neither is run
|
|
25
|
+
// outside the graph — a harness's session its hooks record, a Claude Code
|
|
26
|
+
// subagent, a run from before the runner — so its newest input
|
|
27
|
+
// is owed by whatever runs it: running, never pending, which the runner would
|
|
28
|
+
// answer with no model to ask.
|
|
29
|
+
//
|
|
30
|
+
// A turn lands as one batch — the ask, the prose, and the calls together — so
|
|
31
|
+
// no reader ever sees the prose without the calls that came with it.
|
|
32
|
+
//
|
|
33
|
+
// Prose is `content{body}`; alone it is an input, and an `output{source}`
|
|
34
|
+
// beside it records what produced it — the ask, for what a model returned. A
|
|
35
|
+
// result, refusal or exception carries its prose the same way and is its own
|
|
36
|
+
// kind.
|
|
37
|
+
import { among, and, as, at, col, cross, desc, descriptorHolds, eq, exists, fn, from, gate, ge, gt, iff, isNull, join, le, left, lit, not, notNull, op, or, select, star, sub, table, unionAll, when } from "@yaks/sql/core";
|
|
38
|
+
import { ASK, RESULT } from "./native.js";
|
|
39
|
+
import { attemptDerived } from "./attempt.js";
|
|
40
|
+
import { derived as executionDerived } from "@yaks/tools/vocab";
|
|
41
|
+
import { sessionCost } from "./cost.js";
|
|
42
|
+
import { COST } from "@yaks/model/vocab";
|
|
43
|
+
export * from "./state.js";
|
|
44
|
+
import { RETRIES, UNFINISHED } from "./state.js";
|
|
45
|
+
import { claudeStops } from "./claude-control.js";
|
|
46
|
+
// Old servers still write state during the marks expansion. A non-null state
|
|
47
|
+
// wins until cutover; otherwise waiting precedes admitted in the declared ladder.
|
|
48
|
+
export let dispatchStatus = {
|
|
49
|
+
stable: true,
|
|
50
|
+
tag: 'text',
|
|
51
|
+
values: [
|
|
52
|
+
'queued',
|
|
53
|
+
'active',
|
|
54
|
+
'waiting',
|
|
55
|
+
'settled'
|
|
56
|
+
],
|
|
57
|
+
deps: [
|
|
58
|
+
'dispatch',
|
|
59
|
+
'waiting',
|
|
60
|
+
'admitted'
|
|
61
|
+
],
|
|
62
|
+
expr: (owner, marks = [
|
|
63
|
+
'waiting',
|
|
64
|
+
'admitted'
|
|
65
|
+
])=>{
|
|
66
|
+
let marked = (comp)=>exists(select({
|
|
67
|
+
cols: [
|
|
68
|
+
lit(1)
|
|
69
|
+
],
|
|
70
|
+
from: table(comp, 'm'),
|
|
71
|
+
where: eq(col('entity', 'm'), owner)
|
|
72
|
+
}));
|
|
73
|
+
let rungs = marks.map((comp)=>[
|
|
74
|
+
marked(comp),
|
|
75
|
+
lit(comp == 'waiting' ? 'waiting' : 'active')
|
|
76
|
+
]);
|
|
77
|
+
let status = rungs.length ? when(rungs, lit('queued')) : lit('queued');
|
|
78
|
+
return sub(select({
|
|
79
|
+
cols: [
|
|
80
|
+
fn('coalesce', col('state', 'd'), status)
|
|
81
|
+
],
|
|
82
|
+
from: table('dispatch', 'd'),
|
|
83
|
+
where: eq(col('entity', 'd'), owner)
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
// How far down a transcript the status selector walks to prove a turn at rest.
|
|
88
|
+
let TAIL = 128;
|
|
89
|
+
// The common table expressions the SQL rule names, apart from any component.
|
|
90
|
+
let LATEST = '__latest';
|
|
91
|
+
let ANCHOR = '__anchor';
|
|
92
|
+
// The session index orders entries by identity, independently of sequence.
|
|
93
|
+
// These bounds seek sparse marks; the join still checks their session when
|
|
94
|
+
// other transcripts' entries lie between the two ends.
|
|
95
|
+
let within = (owner, entry)=>{
|
|
96
|
+
let bound = (descending)=>sub(select({
|
|
97
|
+
cols: [
|
|
98
|
+
col('entity', 'extent')
|
|
99
|
+
],
|
|
100
|
+
from: table('entry', 'extent'),
|
|
101
|
+
where: eq(col('session', 'extent'), owner),
|
|
102
|
+
order: [
|
|
103
|
+
descending ? desc(col('entity', 'extent')) : col('entity', 'extent')
|
|
104
|
+
],
|
|
105
|
+
limit: lit(1)
|
|
106
|
+
}));
|
|
107
|
+
return and(ge(entry, bound(false)), le(entry, bound(true)));
|
|
108
|
+
};
|
|
109
|
+
let anyWords = {
|
|
110
|
+
declared: ()=>true,
|
|
111
|
+
holds: (tables, comp)=>descriptorHolds(tables, undefined, comp)
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* The same rule as SQL, for @yaks/sqlite's derived-property registry
|
|
115
|
+
* (`storage(driver, vocab, { derived: sessionDerived(vocab) })`), so
|
|
116
|
+
* `.session.status=running` compiles through the index. `owner` is the SQL
|
|
117
|
+
* naming the session's integer id; a reference column stores the referent's
|
|
118
|
+
* integer id, which is what `entry.session` is compared against.
|
|
119
|
+
*/ export let sessionStatus = {
|
|
120
|
+
tag: 'text',
|
|
121
|
+
values: [
|
|
122
|
+
'empty',
|
|
123
|
+
'pending',
|
|
124
|
+
'running',
|
|
125
|
+
'queued',
|
|
126
|
+
'settled',
|
|
127
|
+
'stopped',
|
|
128
|
+
'failed'
|
|
129
|
+
],
|
|
130
|
+
deps: [
|
|
131
|
+
'session'
|
|
132
|
+
],
|
|
133
|
+
// Only a session not ended owes anything, and sessions end: what the cap
|
|
134
|
+
// counts is a few dozen of a store's tens of thousands.
|
|
135
|
+
few: [
|
|
136
|
+
'pending',
|
|
137
|
+
'running',
|
|
138
|
+
'queued'
|
|
139
|
+
],
|
|
140
|
+
select: (values, dispatch = dispatchStatus, words = anyWords, text = (stored)=>stored, one)=>{
|
|
141
|
+
let { declared, holds } = words;
|
|
142
|
+
if (!values.every((v)=>(v.t == 'val' || v.t == 'lit') && typeof v.v == 'string')) return undefined;
|
|
143
|
+
let wanted = values.map((v)=>v.v);
|
|
144
|
+
if (!wanted.every((v)=>[
|
|
145
|
+
'pending',
|
|
146
|
+
'running',
|
|
147
|
+
'queued',
|
|
148
|
+
'stopped',
|
|
149
|
+
'empty'
|
|
150
|
+
].includes(v))) return undefined;
|
|
151
|
+
let owner = col('entity', 'candidate');
|
|
152
|
+
let wears = (name, entry)=>declared(name) ? exists(select({
|
|
153
|
+
cols: [
|
|
154
|
+
lit(1)
|
|
155
|
+
],
|
|
156
|
+
from: table(name, 'mark'),
|
|
157
|
+
where: eq(col('entity', 'mark'), entry)
|
|
158
|
+
})) : lit(false);
|
|
159
|
+
let cancelled = (entry)=>[
|
|
160
|
+
'notice',
|
|
161
|
+
'content',
|
|
162
|
+
'lines'
|
|
163
|
+
].every(declared) ? exists(select({
|
|
164
|
+
cols: [
|
|
165
|
+
lit(1)
|
|
166
|
+
],
|
|
167
|
+
from: table('lines', 'origin'),
|
|
168
|
+
joins: [
|
|
169
|
+
{
|
|
170
|
+
...cross(table('content', 'prose')),
|
|
171
|
+
on: eq(col('entity', 'origin'), col('entity', 'prose'))
|
|
172
|
+
}
|
|
173
|
+
],
|
|
174
|
+
where: and(eq(col('entity', 'prose'), entry), among(text(col('body', 'prose')), claudeStops.map(lit)))
|
|
175
|
+
})) : lit(false);
|
|
176
|
+
// Seek one latest meaningful entry per owner, not the status expression
|
|
177
|
+
// for every owner. A notice is passive except historical cancellation.
|
|
178
|
+
let latest = sub(select({
|
|
179
|
+
cols: [
|
|
180
|
+
col('entity', 'recent')
|
|
181
|
+
],
|
|
182
|
+
from: table('entry', 'recent'),
|
|
183
|
+
where: and(eq(col('session', 'recent'), col('entity', 's')), or(not(wears('notice', col('entity', 'recent'))), cancelled(col('entity', 'recent')))),
|
|
184
|
+
order: [
|
|
185
|
+
desc(col('seq', 'recent'))
|
|
186
|
+
],
|
|
187
|
+
limit: lit(1)
|
|
188
|
+
}));
|
|
189
|
+
let stopped = wanted.includes('stopped');
|
|
190
|
+
// A transcript with no entry a passive notice leaves is empty, ended or not.
|
|
191
|
+
let empty = wanted.includes('empty');
|
|
192
|
+
// Nothing ended owes anything, so the mark answers before any entry is read.
|
|
193
|
+
// Given `one` owner, the screen reads that session alone.
|
|
194
|
+
let kept = [
|
|
195
|
+
...one ? [
|
|
196
|
+
eq(col('entity', 's'), one)
|
|
197
|
+
] : [],
|
|
198
|
+
...stopped || empty ? [] : [
|
|
199
|
+
not(fn('coalesce', col('ended', 's'), lit(false)))
|
|
200
|
+
]
|
|
201
|
+
];
|
|
202
|
+
let candidates = select({
|
|
203
|
+
cols: [
|
|
204
|
+
col('entity', 's'),
|
|
205
|
+
col('ended', 's'),
|
|
206
|
+
as(latest, 'latest')
|
|
207
|
+
],
|
|
208
|
+
from: table('session', 's'),
|
|
209
|
+
...kept.length ? {
|
|
210
|
+
where: and(...kept)
|
|
211
|
+
} : {}
|
|
212
|
+
});
|
|
213
|
+
let actionable = wanted.some((v)=>v != 'stopped' && v != 'empty');
|
|
214
|
+
let newest = col('latest', 'candidate');
|
|
215
|
+
let ended = fn('coalesce', col('ended', 'candidate'), lit(false));
|
|
216
|
+
// A stopped transcript must either be ended or contain a stop/control.
|
|
217
|
+
// Historical controls are candidates, never an alternative status rule.
|
|
218
|
+
let controls = [
|
|
219
|
+
'stop',
|
|
220
|
+
'notice'
|
|
221
|
+
].filter(declared).map((name)=>select({
|
|
222
|
+
cols: [
|
|
223
|
+
col('session', 'ce'),
|
|
224
|
+
col('seq', 'ce')
|
|
225
|
+
],
|
|
226
|
+
from: table(name, 'cm'),
|
|
227
|
+
joins: [
|
|
228
|
+
{
|
|
229
|
+
...cross(table('entry', 'ce')),
|
|
230
|
+
on: eq(col('entity', 'ce'), col('entity', 'cm'))
|
|
231
|
+
}
|
|
232
|
+
],
|
|
233
|
+
...name == 'notice' ? {
|
|
234
|
+
where: cancelled(col('entity', 'ce'))
|
|
235
|
+
} : {}
|
|
236
|
+
}));
|
|
237
|
+
let controlOwners = controls.length ? unionAll(...controls) : select({
|
|
238
|
+
cols: [
|
|
239
|
+
as(lit(null), 'session'),
|
|
240
|
+
as(lit(null), 'seq')
|
|
241
|
+
],
|
|
242
|
+
where: lit(false)
|
|
243
|
+
});
|
|
244
|
+
// Whether a transcript's newest turn is at rest, read walking down from its
|
|
245
|
+
// newest entry: the first entry holding work or an ask ends the walk. An ask
|
|
246
|
+
// with nothing unread above it proves it, and so does reaching the start of
|
|
247
|
+
// a transcript that asked nothing, whose inputs no runner owes. A longer
|
|
248
|
+
// transcript is left to the exact rule. What ends the walk is worked out in
|
|
249
|
+
// FROM subqueries, which add nothing to this expression's height (workerd's
|
|
250
|
+
// depth limit, as below).
|
|
251
|
+
let inputKinds = [
|
|
252
|
+
'output',
|
|
253
|
+
'notice',
|
|
254
|
+
'result',
|
|
255
|
+
'tool_progress',
|
|
256
|
+
'refusal',
|
|
257
|
+
'exception',
|
|
258
|
+
'ask',
|
|
259
|
+
'call',
|
|
260
|
+
'stop',
|
|
261
|
+
'interrupted'
|
|
262
|
+
].filter(declared);
|
|
263
|
+
let input = (entry)=>and(wears('content', entry), ...inputKinds.map((name)=>not(wears(name, entry))));
|
|
264
|
+
// A historical stop matters only until the next input. Find that input
|
|
265
|
+
// directly after the newest control, rather than working out the entire
|
|
266
|
+
// status of every transcript that ever stopped and later resumed.
|
|
267
|
+
let halted = exists(select({
|
|
268
|
+
cols: [
|
|
269
|
+
lit(1)
|
|
270
|
+
],
|
|
271
|
+
from: table('__status_controls', 'control'),
|
|
272
|
+
where: and(eq(col('session', 'control'), owner), or(col('legacy', 'control'), not(exists(select({
|
|
273
|
+
cols: [
|
|
274
|
+
lit(1)
|
|
275
|
+
],
|
|
276
|
+
from: table('entry', 'resumed'),
|
|
277
|
+
where: and(eq(col('session', 'resumed'), owner), gt(col('seq', 'resumed'), col('seq', 'control')), input(col('entity', 'resumed')))
|
|
278
|
+
})))))
|
|
279
|
+
}));
|
|
280
|
+
let inflight = (entry)=>declared('attempt') ? exists(select({
|
|
281
|
+
cols: [
|
|
282
|
+
lit(1)
|
|
283
|
+
],
|
|
284
|
+
from: table('attempt', 'flight'),
|
|
285
|
+
where: and(eq(col('entity', 'flight'), entry), notNull(col('by', 'flight')))
|
|
286
|
+
})) : lit(false);
|
|
287
|
+
// An input arriving above what the newest ask read. Any other reply there
|
|
288
|
+
// would have ended the walk, or lies between the two and is read here too.
|
|
289
|
+
let unread = (ask, seq)=>exists(select({
|
|
290
|
+
cols: [
|
|
291
|
+
lit(1)
|
|
292
|
+
],
|
|
293
|
+
from: from(select({
|
|
294
|
+
cols: [
|
|
295
|
+
as(or(wears('result', col('entity', 'gap')), wears('tool_progress', col('entity', 'gap')), input(col('entity', 'gap'))), 'heard')
|
|
296
|
+
],
|
|
297
|
+
from: table('entry', 'gap'),
|
|
298
|
+
where: and(eq(col('session', 'gap'), owner), gt(col('seq', 'gap'), fn('coalesce', sub(select({
|
|
299
|
+
cols: [
|
|
300
|
+
col('seq', 'read')
|
|
301
|
+
],
|
|
302
|
+
from: table('ask', 'request'),
|
|
303
|
+
joins: [
|
|
304
|
+
join(table('entry', 'read'), eq(col('entity', 'read'), col('through', 'request')))
|
|
305
|
+
],
|
|
306
|
+
where: eq(col('entity', 'request'), ask)
|
|
307
|
+
})), seq)))
|
|
308
|
+
}), 'above'),
|
|
309
|
+
where: col('heard', 'above')
|
|
310
|
+
}));
|
|
311
|
+
// In a store keeping archetypes, one joined row answers for every component
|
|
312
|
+
// an entry wears, the way the exact rule reads it (`archetype`), where a
|
|
313
|
+
// probe of each table costs a seek per component per entry.
|
|
314
|
+
let typed = declared('archetype');
|
|
315
|
+
let shapes = typed ? [
|
|
316
|
+
{
|
|
317
|
+
...cross(table('entity', 'owner')),
|
|
318
|
+
on: eq(col('id', 'owner'), col('entity', 'e'))
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
...cross(table('archetype', 'shape')),
|
|
322
|
+
on: eq(col('entity', 'shape'), col('archetype', 'owner'))
|
|
323
|
+
}
|
|
324
|
+
] : [];
|
|
325
|
+
let walks = (name, entry)=>typed ? declared(name) ? holds(col('tables', 'shape'), name) : lit(false) : wears(name, entry);
|
|
326
|
+
let resting = (inputs)=>{
|
|
327
|
+
let e = col('entity', 'e');
|
|
328
|
+
let work = [
|
|
329
|
+
'stop',
|
|
330
|
+
'call',
|
|
331
|
+
'result',
|
|
332
|
+
'tool_progress',
|
|
333
|
+
'tool_async',
|
|
334
|
+
'exception',
|
|
335
|
+
'interrupted'
|
|
336
|
+
];
|
|
337
|
+
return fn('coalesce', sub(select({
|
|
338
|
+
cols: [
|
|
339
|
+
col('clean', 'tail')
|
|
340
|
+
],
|
|
341
|
+
from: from(select({
|
|
342
|
+
cols: [
|
|
343
|
+
col('seq', 'e'),
|
|
344
|
+
as(or(walks('ask', e), isNull(col('seq', 'e')), ...work.map((name)=>walks(name, e)), and(walks('attempt', e), inflight(e)), and(walks('notice', e), cancelled(e))), 'ends'),
|
|
345
|
+
as(and(walks('ask', e), notNull(col('seq', 'e')), ...work.map((name)=>not(walks(name, e))), not(and(walks('attempt', e), inflight(e))), ...inputs ? [
|
|
346
|
+
not(unread(e, col('seq', 'e')))
|
|
347
|
+
] : []), 'clean')
|
|
348
|
+
],
|
|
349
|
+
from: table('entry', 'e'),
|
|
350
|
+
joins: shapes,
|
|
351
|
+
where: eq(col('session', 'e'), owner),
|
|
352
|
+
order: [
|
|
353
|
+
desc(col('seq', 'e'))
|
|
354
|
+
],
|
|
355
|
+
limit: lit(TAIL)
|
|
356
|
+
}), 'tail'),
|
|
357
|
+
where: col('ends', 'tail'),
|
|
358
|
+
order: [
|
|
359
|
+
desc(col('seq', 'tail'))
|
|
360
|
+
],
|
|
361
|
+
limit: lit(1)
|
|
362
|
+
})), not(exists(select({
|
|
363
|
+
cols: [
|
|
364
|
+
lit(1)
|
|
365
|
+
],
|
|
366
|
+
from: table('entry', 'beyond'),
|
|
367
|
+
where: eq(col('session', 'beyond'), owner),
|
|
368
|
+
order: [
|
|
369
|
+
desc(col('seq', 'beyond'))
|
|
370
|
+
],
|
|
371
|
+
limit: lit(1),
|
|
372
|
+
offset: lit(TAIL - 1)
|
|
373
|
+
}))));
|
|
374
|
+
};
|
|
375
|
+
let notQueued = not(eq(fn('coalesce', dispatch.expr(owner), lit('')), lit('queued')));
|
|
376
|
+
let marked = (entry, names)=>names.map((name)=>not(wears(name, entry)));
|
|
377
|
+
// Three last turns the rule can only read as finished: prose that asks for
|
|
378
|
+
// nothing, a refusal naming no request, and a turn given up on. Nothing
|
|
379
|
+
// about them needs the exact rule, and they are nearly every transcript.
|
|
380
|
+
// A background call still unanswered: probed in each candidate's own
|
|
381
|
+
// entries where the candidates are the few not ended, and read once for
|
|
382
|
+
// the store where every ended session is one too.
|
|
383
|
+
let unanswered = (p)=>not(exists(select({
|
|
384
|
+
cols: [
|
|
385
|
+
lit(1)
|
|
386
|
+
],
|
|
387
|
+
from: table('result', 'answer'),
|
|
388
|
+
where: eq(col('call', 'answer'), col('entity', p))
|
|
389
|
+
})));
|
|
390
|
+
let few = !stopped && !empty;
|
|
391
|
+
let background = declared('tool_async') && declared('result');
|
|
392
|
+
let waiting = !background ? lit(false) : few ? exists(select({
|
|
393
|
+
cols: [
|
|
394
|
+
lit(1)
|
|
395
|
+
],
|
|
396
|
+
from: table('tool_async', 'p'),
|
|
397
|
+
joins: [
|
|
398
|
+
{
|
|
399
|
+
...cross(table('entry', 'pe')),
|
|
400
|
+
on: eq(col('entity', 'p'), col('entity', 'pe'))
|
|
401
|
+
}
|
|
402
|
+
],
|
|
403
|
+
where: and(within(owner, col('entity', 'p')), eq(col('session', 'pe'), owner), unanswered('p'))
|
|
404
|
+
})) : among(owner, select({
|
|
405
|
+
cols: [
|
|
406
|
+
col('session')
|
|
407
|
+
],
|
|
408
|
+
from: table('__status_waiting')
|
|
409
|
+
}));
|
|
410
|
+
let atRest = or(and(wears('output', newest), ...marked(newest, [
|
|
411
|
+
'ask',
|
|
412
|
+
'refusal'
|
|
413
|
+
]), resting(true), not(waiting), notQueued), and(wears('refusal', newest), ...marked(newest, [
|
|
414
|
+
'output',
|
|
415
|
+
'ask',
|
|
416
|
+
'result'
|
|
417
|
+
]), resting(false), notQueued), and(wears('ask', newest), wears('interrupted', newest), wears('failed', newest), ...marked(newest, [
|
|
418
|
+
'call',
|
|
419
|
+
'result',
|
|
420
|
+
'refusal',
|
|
421
|
+
'exception'
|
|
422
|
+
]), notQueued));
|
|
423
|
+
let eligible = or(empty ? isNull(newest) : lit(false), stopped ? or(wears('stop', newest), halted, and(ended, notNull(newest), not(atRest))) : lit(false), actionable ? and(not(ended), notNull(newest), not(wears('stop', newest)), not(atRest)) : lit(false));
|
|
424
|
+
// Materialization is essential: SQLite must apply the cheap candidate
|
|
425
|
+
// filter before it evaluates the exact turn expression.
|
|
426
|
+
return select({
|
|
427
|
+
with: [
|
|
428
|
+
{
|
|
429
|
+
name: '__status_latest',
|
|
430
|
+
materialized: true,
|
|
431
|
+
q: candidates
|
|
432
|
+
},
|
|
433
|
+
...stopped ? [
|
|
434
|
+
{
|
|
435
|
+
name: '__status_controls',
|
|
436
|
+
materialized: true,
|
|
437
|
+
q: select({
|
|
438
|
+
cols: [
|
|
439
|
+
col('session', 'controls'),
|
|
440
|
+
as(fn('max', col('seq', 'controls')), 'seq'),
|
|
441
|
+
as(fn('max', isNull(col('seq', 'controls'))), 'legacy')
|
|
442
|
+
],
|
|
443
|
+
from: from(controlOwners, 'controls'),
|
|
444
|
+
group: [
|
|
445
|
+
col('session', 'controls')
|
|
446
|
+
]
|
|
447
|
+
})
|
|
448
|
+
}
|
|
449
|
+
] : [],
|
|
450
|
+
...background && !few ? [
|
|
451
|
+
{
|
|
452
|
+
name: '__status_waiting',
|
|
453
|
+
materialized: true,
|
|
454
|
+
q: select({
|
|
455
|
+
cols: [
|
|
456
|
+
col('session', 'pe')
|
|
457
|
+
],
|
|
458
|
+
from: table('tool_async', 'p'),
|
|
459
|
+
joins: [
|
|
460
|
+
{
|
|
461
|
+
...cross(table('entry', 'pe')),
|
|
462
|
+
on: eq(col('entity', 'pe'), col('entity', 'p'))
|
|
463
|
+
}
|
|
464
|
+
],
|
|
465
|
+
where: unanswered('p')
|
|
466
|
+
})
|
|
467
|
+
}
|
|
468
|
+
] : [],
|
|
469
|
+
{
|
|
470
|
+
name: '__status_candidates',
|
|
471
|
+
materialized: true,
|
|
472
|
+
q: select({
|
|
473
|
+
cols: [
|
|
474
|
+
owner,
|
|
475
|
+
col('ended', 'candidate'),
|
|
476
|
+
newest
|
|
477
|
+
],
|
|
478
|
+
from: table('__status_latest', 'candidate'),
|
|
479
|
+
where: eligible
|
|
480
|
+
})
|
|
481
|
+
}
|
|
482
|
+
],
|
|
483
|
+
cols: [
|
|
484
|
+
col('entity', 'chosen')
|
|
485
|
+
],
|
|
486
|
+
from: table('__status_candidates', 'chosen'),
|
|
487
|
+
where: among(when([
|
|
488
|
+
[
|
|
489
|
+
isNull(col('latest', 'chosen')),
|
|
490
|
+
lit('empty')
|
|
491
|
+
],
|
|
492
|
+
[
|
|
493
|
+
wears('stop', col('latest', 'chosen')),
|
|
494
|
+
lit('stopped')
|
|
495
|
+
]
|
|
496
|
+
], sessionStatus.expr(col('entity', 'chosen'), dispatch, words, text)), values)
|
|
497
|
+
});
|
|
498
|
+
},
|
|
499
|
+
expr: (owner, dispatch = dispatchStatus, words = anyWords, text = (stored)=>stored, terminal = false)=>{
|
|
500
|
+
let { declared, holds } = words;
|
|
501
|
+
// Each fact about an entry is said once (`marks`), and each fact about
|
|
502
|
+
// the transcript is said once, as a common table expression the next
|
|
503
|
+
// reads: its newest entry (LATEST) and the anchors of its turn (ANCHOR).
|
|
504
|
+
// Both name the owner, so SQLite runs them again at every reference,
|
|
505
|
+
// whatever the hint: they are left for the planner to flatten rather than
|
|
506
|
+
// materialized, and each is referenced as few times as the rule allows.
|
|
507
|
+
// The turn's rows are read once, and every fact in the decision is
|
|
508
|
+
// reduced from them in one pass. Each read seeks its own rows through the
|
|
509
|
+
// entry index, so the cost is bounded by the turn, whatever the
|
|
510
|
+
// transcript's length.
|
|
511
|
+
let typed = declared('archetype');
|
|
512
|
+
let mine = (alias)=>eq(col('session', alias), owner);
|
|
513
|
+
let inputKinds = [
|
|
514
|
+
'output',
|
|
515
|
+
'notice',
|
|
516
|
+
'result',
|
|
517
|
+
'tool_progress',
|
|
518
|
+
'refusal',
|
|
519
|
+
'exception',
|
|
520
|
+
'ask',
|
|
521
|
+
'call',
|
|
522
|
+
'stop',
|
|
523
|
+
'interrupted'
|
|
524
|
+
].filter(declared);
|
|
525
|
+
// Older consumed imports classified a cancellation as a passive notice.
|
|
526
|
+
// Interpret that format here without rewriting the owner's transcripts:
|
|
527
|
+
// whether the entry with the id `entry` holds the prose such an import
|
|
528
|
+
// recorded.
|
|
529
|
+
let cancellation = (entry)=>[
|
|
530
|
+
'notice',
|
|
531
|
+
'content',
|
|
532
|
+
'lines'
|
|
533
|
+
].every(declared) ? exists(select({
|
|
534
|
+
cols: [
|
|
535
|
+
lit(1)
|
|
536
|
+
],
|
|
537
|
+
from: table('lines', 'origin'),
|
|
538
|
+
joins: [
|
|
539
|
+
{
|
|
540
|
+
...cross(table('content', 'text')),
|
|
541
|
+
on: eq(col('entity', 'origin'), col('entity', 'text'))
|
|
542
|
+
}
|
|
543
|
+
],
|
|
544
|
+
where: and(eq(col('entity', 'text'), entry), among(text(col('body', 'text')), claudeStops.map(lit)))
|
|
545
|
+
})) : lit(false);
|
|
546
|
+
// What an entry is to the rule, from whether it `wears` each declared
|
|
547
|
+
// component and whether its prose is `cancelled`.
|
|
548
|
+
let marks = (wears, cancelled)=>{
|
|
549
|
+
let worn = (comp)=>declared(comp) ? wears(comp) : lit(false);
|
|
550
|
+
let notice = worn('notice');
|
|
551
|
+
let cancel = iff(notice, cancelled, lit(false));
|
|
552
|
+
return {
|
|
553
|
+
worn,
|
|
554
|
+
cancel,
|
|
555
|
+
halted: or(worn('stop'), cancel),
|
|
556
|
+
passive: and(notice, not(cancelled)),
|
|
557
|
+
// A cancellation is a notice, so an input is neither.
|
|
558
|
+
input: and(worn('content'), ...inputKinds.map((c)=>not(wears(c))))
|
|
559
|
+
};
|
|
560
|
+
};
|
|
561
|
+
// Whether an entry wears a component: in a store that keeps archetypes,
|
|
562
|
+
// read off the archetype one joined row names, which answers for every
|
|
563
|
+
// component; else by seeking the component's own table.
|
|
564
|
+
// Each joined in the order written: the entry leads, so it is the one
|
|
565
|
+
// sought through its index.
|
|
566
|
+
let archetype = (alias)=>[
|
|
567
|
+
{
|
|
568
|
+
...cross(table('entity', 'owner')),
|
|
569
|
+
on: eq(col('id', 'owner'), col('entity', alias))
|
|
570
|
+
},
|
|
571
|
+
{
|
|
572
|
+
...cross(table('archetype', 'shape')),
|
|
573
|
+
on: eq(col('entity', 'shape'), col('archetype', 'owner'))
|
|
574
|
+
}
|
|
575
|
+
];
|
|
576
|
+
let shaped = (comp)=>holds(col('tables', 'shape'), comp);
|
|
577
|
+
let probed = (alias)=>(comp)=>exists(select({
|
|
578
|
+
cols: [
|
|
579
|
+
lit(1)
|
|
580
|
+
],
|
|
581
|
+
from: table(comp, 'm'),
|
|
582
|
+
where: eq(col('entity', 'm'), col('entity', alias))
|
|
583
|
+
}));
|
|
584
|
+
let shapes = typed ? archetype('e') : [];
|
|
585
|
+
let e = marks(typed ? shaped : probed('e'), cancellation(col('entity', 'e')));
|
|
586
|
+
// The newest entry a passive notice does not hide, whether it is a stop,
|
|
587
|
+
// and, when it is a refusal, the request it refused.
|
|
588
|
+
let latest = select({
|
|
589
|
+
cols: [
|
|
590
|
+
col('entity', 'e'),
|
|
591
|
+
col('seq', 'e'),
|
|
592
|
+
as(e.worn('stop'), 'stop'),
|
|
593
|
+
as(and(e.worn('ask'), not(e.worn('attempt')), ...[
|
|
594
|
+
'exception',
|
|
595
|
+
'refusal',
|
|
596
|
+
'interrupted'
|
|
597
|
+
].map((c)=>not(e.worn(c)))), 'unfinished'),
|
|
598
|
+
as(iff(e.worn('refusal'), sub(select({
|
|
599
|
+
cols: [
|
|
600
|
+
col('source', 'o')
|
|
601
|
+
],
|
|
602
|
+
from: table('output', 'o'),
|
|
603
|
+
where: eq(col('entity', 'o'), col('entity', 'e'))
|
|
604
|
+
})), lit(null)), 'refused_source'),
|
|
605
|
+
...typed ? [
|
|
606
|
+
col('tables', 'shape')
|
|
607
|
+
] : []
|
|
608
|
+
],
|
|
609
|
+
from: table('entry', 'e'),
|
|
610
|
+
joins: shapes,
|
|
611
|
+
where: and(mine('e'), not(e.passive)),
|
|
612
|
+
order: [
|
|
613
|
+
desc(col('seq', 'e'))
|
|
614
|
+
],
|
|
615
|
+
limit: lit(1)
|
|
616
|
+
});
|
|
617
|
+
// Imported asks may have no input boundary. Seek the last input/stop in
|
|
618
|
+
// this transcript so a late ask cannot hide the stop it followed.
|
|
619
|
+
let control = select({
|
|
620
|
+
cols: [
|
|
621
|
+
col('seq', 'e')
|
|
622
|
+
],
|
|
623
|
+
from: table('entry', 'e'),
|
|
624
|
+
joins: shapes,
|
|
625
|
+
where: and(mine('e'), notNull(col('seq', 'e')), // a stop no passive notice hides: a plain stop, or a cancellation
|
|
626
|
+
or(and(e.worn('stop'), not(e.worn('notice'))), e.cancel, e.input)),
|
|
627
|
+
order: [
|
|
628
|
+
desc(col('seq', 'e'))
|
|
629
|
+
],
|
|
630
|
+
limit: lit(1)
|
|
631
|
+
});
|
|
632
|
+
// The newest ask: the newest sequenced one, else the newest one written
|
|
633
|
+
// before entries were sequenced. The first arm with a row ends the read,
|
|
634
|
+
// so a sequenced ask never pays for the search.
|
|
635
|
+
let onAsk = eq(col('entity', 'a'), col('entity', 'e'));
|
|
636
|
+
// A correlated scalar cannot flatten into the join whose tiny ANALYZE
|
|
637
|
+
// estimate makes SQLite scan all asks instead of seeking their primary key.
|
|
638
|
+
let askField = (alias, name)=>sub(select({
|
|
639
|
+
cols: [
|
|
640
|
+
col(name, 'point_ask')
|
|
641
|
+
],
|
|
642
|
+
from: table(ASK, 'point_ask'),
|
|
643
|
+
where: eq(col('entity', 'point_ask'), col('entity', alias))
|
|
644
|
+
}));
|
|
645
|
+
let askAt = (where)=>select({
|
|
646
|
+
cols: [
|
|
647
|
+
star()
|
|
648
|
+
],
|
|
649
|
+
from: from(select({
|
|
650
|
+
cols: [
|
|
651
|
+
col('entity', 'candidate'),
|
|
652
|
+
col('seq', 'candidate'),
|
|
653
|
+
as(fn('json_extract', col('request', 'candidate'), lit('$[1]')), 'through')
|
|
654
|
+
],
|
|
655
|
+
from: from(select({
|
|
656
|
+
cols: [
|
|
657
|
+
col('entity', 'e'),
|
|
658
|
+
col('seq', 'e'),
|
|
659
|
+
as(sub(select({
|
|
660
|
+
cols: [
|
|
661
|
+
fn('json_array', col('entity', 'point_ask'), col('through', 'point_ask'))
|
|
662
|
+
],
|
|
663
|
+
from: table(ASK, 'point_ask'),
|
|
664
|
+
where: eq(col('entity', 'point_ask'), col('entity', 'e'))
|
|
665
|
+
})), 'request')
|
|
666
|
+
],
|
|
667
|
+
from: table('entry', 'e'),
|
|
668
|
+
where,
|
|
669
|
+
order: [
|
|
670
|
+
desc(col('seq', 'e'))
|
|
671
|
+
],
|
|
672
|
+
// Keep the scalar row together: flattening would evaluate it once
|
|
673
|
+
// for presence and again for through, billing the ask twice.
|
|
674
|
+
limit: lit(-1)
|
|
675
|
+
}), 'candidate'),
|
|
676
|
+
where: notNull(col('request', 'candidate')),
|
|
677
|
+
limit: lit(1)
|
|
678
|
+
}))
|
|
679
|
+
});
|
|
680
|
+
let unsequenced = (source, joins, point = false)=>sub(select({
|
|
681
|
+
cols: [
|
|
682
|
+
col('entity', 'e')
|
|
683
|
+
],
|
|
684
|
+
from: source,
|
|
685
|
+
joins,
|
|
686
|
+
where: and(mine('e'), isNull(col('seq', 'e')), ...point ? [
|
|
687
|
+
notNull(askField('e', 'entity'))
|
|
688
|
+
] : []),
|
|
689
|
+
order: [
|
|
690
|
+
desc(col('seq', 'e'))
|
|
691
|
+
],
|
|
692
|
+
limit: lit(1)
|
|
693
|
+
}));
|
|
694
|
+
// Only a transcript holding an entry written before entries were
|
|
695
|
+
// sequenced is searched for one: one seek of its own entries answers for
|
|
696
|
+
// the rest, where the search reads the store's asks while they are few.
|
|
697
|
+
let legacy = exists(select({
|
|
698
|
+
cols: [
|
|
699
|
+
lit(1)
|
|
700
|
+
],
|
|
701
|
+
from: gate('entry', 'old'),
|
|
702
|
+
where: and(mine('old'), isNull(col('seq', 'old'))),
|
|
703
|
+
limit: lit(1)
|
|
704
|
+
}));
|
|
705
|
+
let ask = {
|
|
706
|
+
...unionAll(askAt(and(mine('e'), notNull(col('seq', 'e')))), askAt(and(mine('e'), eq(col('entity', 'e'), iff(legacy, iff(exists(select({
|
|
707
|
+
cols: [
|
|
708
|
+
lit(1)
|
|
709
|
+
],
|
|
710
|
+
from: gate(ASK),
|
|
711
|
+
limit: lit(1),
|
|
712
|
+
offset: lit(64)
|
|
713
|
+
})), unsequenced(table('entry', 'e'), [], true), unsequenced(table(ASK, 'a'), [
|
|
714
|
+
{
|
|
715
|
+
...cross(table('entry', 'e')),
|
|
716
|
+
on: onAsk
|
|
717
|
+
}
|
|
718
|
+
])), lit(null)))))),
|
|
719
|
+
limit: lit(1)
|
|
720
|
+
};
|
|
721
|
+
let anchors = select({
|
|
722
|
+
cols: [
|
|
723
|
+
as(col('entity', 'l'), 'newest'),
|
|
724
|
+
as(col('seq', 'l'), 'newest_seq'),
|
|
725
|
+
as(col('entity', 'a'), 'asked'),
|
|
726
|
+
as(col('seq', 'a'), 'asked_seq'),
|
|
727
|
+
as(col('seq', 'boundary'), 'boundary_seq'),
|
|
728
|
+
// The ask's boundary also acknowledges input admitted while that
|
|
729
|
+
// request was in flight; a stop there is part of the turn.
|
|
730
|
+
as(fn('coalesce', op('+', col('seq', 'boundary'), iff(or(probed('boundary')('stop'), iff(probed('boundary')('notice'), cancellation(col('entity', 'boundary')), lit(false))), lit(0), lit(1))), sub(control)), 'control_seq'),
|
|
731
|
+
as(sub(select({
|
|
732
|
+
cols: [
|
|
733
|
+
col('seq', 'r')
|
|
734
|
+
],
|
|
735
|
+
from: table('entry', 'r'),
|
|
736
|
+
where: eq(col('entity', 'r'), col('refused_source', 'l'))
|
|
737
|
+
})), 'refused_seq'),
|
|
738
|
+
as(col('refused_source', 'l'), 'refused_source'),
|
|
739
|
+
...typed ? [
|
|
740
|
+
as(col('tables', 'l'), 'newest_tables')
|
|
741
|
+
] : []
|
|
742
|
+
],
|
|
743
|
+
from: table(LATEST, 'l'),
|
|
744
|
+
joins: [
|
|
745
|
+
left(from(ask, 'a'), lit(true)),
|
|
746
|
+
left(table('entry', 'boundary'), eq(col('entity', 'boundary'), col('through', 'a')))
|
|
747
|
+
]
|
|
748
|
+
});
|
|
749
|
+
let anchor = at('anchor');
|
|
750
|
+
let start = fn('min', fn('coalesce', anchor('asked_seq'), lit(0)), fn('coalesce', anchor('control_seq'), anchor('asked_seq'), anchor('newest_seq')), fn('coalesce', op('+', anchor('refused_seq'), lit(1)), anchor('newest_seq')));
|
|
751
|
+
// The turn: its entries from its start through the newest, or the newest
|
|
752
|
+
// alone in a transcript written before entries were sequenced. The
|
|
753
|
+
// anchors are read once, so the turn's rows hang off them as a left join
|
|
754
|
+
// rather than a union of two reads. Each row reads every component it
|
|
755
|
+
// wears in one pass: through the archetype, else a join per component.
|
|
756
|
+
// The shape is joined on the entry itself, where a reader's scope admits
|
|
757
|
+
// it with the entry (@yaks/sql bind.ts `ownerReads`); the newest alone
|
|
758
|
+
// brings its own. Naming the anchor columns keeps the admitted `entity`
|
|
759
|
+
// unambiguous through this projection.
|
|
760
|
+
let span = select({
|
|
761
|
+
cols: [
|
|
762
|
+
as(fn('coalesce', col('entity', 'i'), anchor('newest')), 'entity'),
|
|
763
|
+
col('seq', 'i'),
|
|
764
|
+
anchor('asked'),
|
|
765
|
+
anchor('asked_seq'),
|
|
766
|
+
anchor('boundary_seq'),
|
|
767
|
+
anchor('newest'),
|
|
768
|
+
anchor('refused_source'),
|
|
769
|
+
...typed ? [
|
|
770
|
+
as(fn('coalesce', col('tables', 'shape'), iff(isNull(col('entity', 'i')), anchor('newest_tables'), lit(null))), 'tables')
|
|
771
|
+
] : []
|
|
772
|
+
],
|
|
773
|
+
from: table(ANCHOR, 'anchor'),
|
|
774
|
+
joins: [
|
|
775
|
+
left(table('entry', 'i'), and(mine('i'), notNull(anchor('newest_seq')), ge(col('seq', 'i'), start))),
|
|
776
|
+
...typed ? [
|
|
777
|
+
left(table('entity', 'owner'), eq(col('id', 'owner'), col('entity', 'i'))),
|
|
778
|
+
left(table('archetype', 'shape'), eq(col('entity', 'shape'), col('archetype', 'owner')))
|
|
779
|
+
] : []
|
|
780
|
+
],
|
|
781
|
+
where: or(isNull(anchor('newest_seq')), notNull(col('entity', 'i')))
|
|
782
|
+
});
|
|
783
|
+
let wears = typed ? (comp)=>holds(col('tables', 'span'), comp) : (comp)=>notNull(col('entity', comp));
|
|
784
|
+
// Each component the rule reads, where the entry wears it: its id.
|
|
785
|
+
let comps = [
|
|
786
|
+
'stop',
|
|
787
|
+
'notice',
|
|
788
|
+
'content',
|
|
789
|
+
'exception',
|
|
790
|
+
'refusal',
|
|
791
|
+
'interrupted',
|
|
792
|
+
'failed',
|
|
793
|
+
'provisional',
|
|
794
|
+
'using',
|
|
795
|
+
'sending',
|
|
796
|
+
'ask',
|
|
797
|
+
'call',
|
|
798
|
+
'result',
|
|
799
|
+
'tool_progress',
|
|
800
|
+
'tool_async',
|
|
801
|
+
'output',
|
|
802
|
+
'attempt',
|
|
803
|
+
'execution'
|
|
804
|
+
];
|
|
805
|
+
let joins = [];
|
|
806
|
+
if (!typed) {
|
|
807
|
+
joins.push(...comps.filter(declared).map((comp)=>left(table(comp, comp), eq(col('entity', comp), col('entity', 'span')))));
|
|
808
|
+
}
|
|
809
|
+
let prop = (comp, p)=>as(!declared(comp) ? lit(null) : typed ? iff(wears(comp), sub(select({
|
|
810
|
+
cols: [
|
|
811
|
+
col(p, 'v')
|
|
812
|
+
],
|
|
813
|
+
from: table(comp, 'v'),
|
|
814
|
+
where: eq(col('entity', 'v'), col('entity', 'span'))
|
|
815
|
+
})), lit(null)) : col(p, comp), `${comp}_${p}`);
|
|
816
|
+
let turn = select({
|
|
817
|
+
cols: [
|
|
818
|
+
col('entity', 'span'),
|
|
819
|
+
col('seq', 'span'),
|
|
820
|
+
...comps.map((comp)=>as(!declared(comp) ? lit(null) : typed ? iff(wears(comp), col('entity', 'span'), lit(null)) : col('entity', comp), comp)),
|
|
821
|
+
as(declared('notice') ? iff(wears('notice'), cancellation(col('entity', 'span')), lit(false)) : lit(false), 'cancelled'),
|
|
822
|
+
prop('attempt', 'by'),
|
|
823
|
+
prop('execution', 'by'),
|
|
824
|
+
// Final results close work; a Pending entry of the newest reply's
|
|
825
|
+
// background call permits the model to continue.
|
|
826
|
+
as(declared('call') ? iff(wears('call'), sub(select({
|
|
827
|
+
cols: [
|
|
828
|
+
col('entity', 'r')
|
|
829
|
+
],
|
|
830
|
+
from: table(RESULT, 'r'),
|
|
831
|
+
where: eq(col('call', 'r'), col('entity', 'span')),
|
|
832
|
+
limit: lit(1)
|
|
833
|
+
})), lit(null)) : lit(null), 'answered'),
|
|
834
|
+
prop('call', 'source'),
|
|
835
|
+
as(declared('tool_progress') ? iff(wears('call'), exists(select({
|
|
836
|
+
cols: [
|
|
837
|
+
lit(1)
|
|
838
|
+
],
|
|
839
|
+
from: table('tool_progress', 'p'),
|
|
840
|
+
where: eq(col('call', 'p'), col('entity', 'span'))
|
|
841
|
+
})), lit(false)) : lit(false), 'progressed'),
|
|
842
|
+
col('boundary_seq', 'span'),
|
|
843
|
+
as(col('asked', 'span'), 'ask_id'),
|
|
844
|
+
col('refused_source', 'span'),
|
|
845
|
+
as(eq(col('entity', 'span'), col('newest', 'span')), 'newest'),
|
|
846
|
+
as(eq(col('entity', 'span'), col('asked', 'span')), 'asked'),
|
|
847
|
+
as(ge(col('seq', 'span'), fn('coalesce', col('asked_seq', 'span'), lit(0))), 'current')
|
|
848
|
+
],
|
|
849
|
+
from: from(span, 'span'),
|
|
850
|
+
joins
|
|
851
|
+
});
|
|
852
|
+
let has = (comp, alias = 't')=>notNull(col(comp, alias));
|
|
853
|
+
// Whether the entry `alias` was read from a harness's file: looked up only
|
|
854
|
+
// for an ask, the one entry it changes.
|
|
855
|
+
let read = (alias)=>declared('lines') ? exists(select({
|
|
856
|
+
cols: [
|
|
857
|
+
lit(1)
|
|
858
|
+
],
|
|
859
|
+
from: table('lines', 'line'),
|
|
860
|
+
where: eq(col('entity', 'line'), col('entity', alias))
|
|
861
|
+
})) : lit(false);
|
|
862
|
+
let lacks = (comp, alias = 't')=>isNull(col(comp, alias));
|
|
863
|
+
let current = col('current', 't');
|
|
864
|
+
let open = and(current, has('call'), lacks('answered'), lacks('tool_async'));
|
|
865
|
+
let inflight = and(current, has('attempt'), notNull(col('attempt_by', 't')), lacks('interrupted'));
|
|
866
|
+
let flag = (name, condition)=>as(fn('max', iff(condition, lit(1), lit(0))), name);
|
|
867
|
+
let newest = col('newest', 't');
|
|
868
|
+
let asked = col('asked', 't');
|
|
869
|
+
let row = marks((comp)=>has(comp), col('cancelled', 't'));
|
|
870
|
+
// A result carrying its call's refusal is a result, as `kindOf` reads it.
|
|
871
|
+
let value = (name)=>name == 'stop' ? iff(row.halted, col('entity', 't'), lit(null)) : name == 'exception' || name == 'refusal' ? iff(has('result'), lit(null), col(name, 't')) : col(name, 't');
|
|
872
|
+
let names = [
|
|
873
|
+
'stop',
|
|
874
|
+
'exception',
|
|
875
|
+
'refusal',
|
|
876
|
+
'interrupted',
|
|
877
|
+
'failed',
|
|
878
|
+
'ask',
|
|
879
|
+
'call',
|
|
880
|
+
'result',
|
|
881
|
+
'tool_progress',
|
|
882
|
+
'output',
|
|
883
|
+
'attempt',
|
|
884
|
+
'attempt_by',
|
|
885
|
+
'refused_source',
|
|
886
|
+
'boundary_seq'
|
|
887
|
+
];
|
|
888
|
+
let askNames = [
|
|
889
|
+
'interrupted',
|
|
890
|
+
'failed',
|
|
891
|
+
'provisional',
|
|
892
|
+
'seq'
|
|
893
|
+
];
|
|
894
|
+
// Keep these historical predicates out of the turn aggregate: earlier
|
|
895
|
+
// verdicts need neither. Presence includes answered background calls;
|
|
896
|
+
// only the running branch needs an unanswered one.
|
|
897
|
+
let background = (unanswered)=>declared('tool_async') ? iff(exists(select({
|
|
898
|
+
cols: [
|
|
899
|
+
lit(1)
|
|
900
|
+
],
|
|
901
|
+
from: gate('tool_async')
|
|
902
|
+
})), sub(select({
|
|
903
|
+
cols: [
|
|
904
|
+
lit(1)
|
|
905
|
+
],
|
|
906
|
+
from: table('entry', 'pe'),
|
|
907
|
+
joins: [
|
|
908
|
+
{
|
|
909
|
+
...cross(table('tool_async', 'p')),
|
|
910
|
+
on: eq(col('entity', 'pe'), col('entity', 'p'))
|
|
911
|
+
}
|
|
912
|
+
],
|
|
913
|
+
where: and(mine('pe'), ...unanswered ? [
|
|
914
|
+
not(exists(select({
|
|
915
|
+
cols: [
|
|
916
|
+
lit(1)
|
|
917
|
+
],
|
|
918
|
+
from: table(RESULT, 'r'),
|
|
919
|
+
where: eq(col('call', 'r'), col('entity', 'p'))
|
|
920
|
+
})))
|
|
921
|
+
] : []),
|
|
922
|
+
// EXISTS discards ordering. A scalar LIMIT keeps newest-first seeks.
|
|
923
|
+
order: [
|
|
924
|
+
desc(col('seq', 'pe'))
|
|
925
|
+
],
|
|
926
|
+
limit: lit(1)
|
|
927
|
+
})), lit(null)) : lit(null);
|
|
928
|
+
let facts = select({
|
|
929
|
+
cols: [
|
|
930
|
+
flag('open', open),
|
|
931
|
+
flag('abandoned', and(open, has('execution'), isNull(col('execution_by', 't')), lacks('interrupted'))),
|
|
932
|
+
flag('inflight', inflight),
|
|
933
|
+
// A call of the newest reply still running within its patience: the
|
|
934
|
+
// reply's other answers wait for it.
|
|
935
|
+
flag('unheard', and(current, has('tool_async'), eq(col('call_source', 't'), col('ask_id', 't')), lacks('answered'), not(col('progressed', 't')))),
|
|
936
|
+
as(fn('max', iff(or(has('result'), declared('tool_progress') && declared('call') ? and(has('tool_progress'), exists(select({
|
|
937
|
+
cols: [
|
|
938
|
+
lit(1)
|
|
939
|
+
],
|
|
940
|
+
from: table('tool_progress', 'rp'),
|
|
941
|
+
joins: [
|
|
942
|
+
join(table('call', 'rc'), eq(col('entity', 'rc'), col('call', 'rp')))
|
|
943
|
+
],
|
|
944
|
+
where: and(eq(col('entity', 'rp'), col('entity', 't')), eq(col('source', 'rc'), col('ask_id', 't')))
|
|
945
|
+
}))) : lit(false)), col('seq', 't'), lit(null))), 'ready_seq'),
|
|
946
|
+
as(fn('max', iff(row.input, col('seq', 't'), lit(null))), 'input_seq'),
|
|
947
|
+
as(fn('max', iff(row.halted, col('seq', 't'), lit(null))), 'stop_seq'),
|
|
948
|
+
flag('served', or(has('using'), and(has('ask'), not(read('t'))))),
|
|
949
|
+
...names.map((name)=>as(fn('max', iff(newest, value(name), lit(null))), 'n_' + name)),
|
|
950
|
+
...askNames.map((name)=>as(fn('max', iff(asked, col(name, 't'), lit(null))), 'a_' + name)),
|
|
951
|
+
as(fn('max', col('entity', 't')), 'any')
|
|
952
|
+
],
|
|
953
|
+
from: from(turn, 't'),
|
|
954
|
+
// Past plain outputs contribute no historical fact. Keep both anchors
|
|
955
|
+
// and every other mark: an output can also carry outstanding work.
|
|
956
|
+
where: and(not(row.passive), or(newest, asked, lacks('output'), ...comps.filter((c)=>c != 'content' && c != 'output').map((c)=>has(c))))
|
|
957
|
+
});
|
|
958
|
+
let n = (name)=>notNull(col('n_' + name, 'f'));
|
|
959
|
+
let f = (name)=>col(name, 'f');
|
|
960
|
+
let a = (name)=>notNull(col('a_' + name, 'f'));
|
|
961
|
+
let after = (seq)=>fn('coalesce', gt(f('input_seq'), seq), lit(false));
|
|
962
|
+
let boundary = fn('coalesce', f('n_boundary_seq'), f('a_seq'), lit(0));
|
|
963
|
+
// Two bounded historical facts remain: a refusal can explicitly name an
|
|
964
|
+
// older ask, and the retry limit covers at most RETRIES consecutive asks.
|
|
965
|
+
// Neither is an open-work scan.
|
|
966
|
+
let refused = sub(select({
|
|
967
|
+
cols: [
|
|
968
|
+
col('seq', 'b')
|
|
969
|
+
],
|
|
970
|
+
from: table('entry', 'b'),
|
|
971
|
+
where: and(mine('b'), notNull(askField('b', 'entity')), eq(col('entity', 'b'), col('n_refused_source', 'f')))
|
|
972
|
+
}));
|
|
973
|
+
let probe = marks(probed('e'), cancellation(col('entity', 'e')));
|
|
974
|
+
let retries = sub(select({
|
|
975
|
+
cols: [
|
|
976
|
+
fn('sum', col('interrupted', 'r'))
|
|
977
|
+
],
|
|
978
|
+
from: from(select({
|
|
979
|
+
cols: [
|
|
980
|
+
col('seq', 'e'),
|
|
981
|
+
as(probe.worn('interrupted'), 'interrupted')
|
|
982
|
+
],
|
|
983
|
+
from: table('entry', 'e'),
|
|
984
|
+
where: and(mine('e'), notNull(askField('e', 'entity')), not(probe.passive)),
|
|
985
|
+
order: [
|
|
986
|
+
desc(col('seq', 'e'))
|
|
987
|
+
],
|
|
988
|
+
limit: lit(RETRIES)
|
|
989
|
+
}), 'r'),
|
|
990
|
+
where: or(isNull(f('input_seq')), gt(col('seq', 'r'), f('input_seq')))
|
|
991
|
+
}));
|
|
992
|
+
// Before the first ask, a model selection on the original request still
|
|
993
|
+
// makes a later input owed by this runner. Once asked, the turn itself is
|
|
994
|
+
// the proof of service and no historical request is read, unless it was
|
|
995
|
+
// read from a harness's file: that harness answers its own transcript.
|
|
996
|
+
let owed = iff(f('served'), lit('pending'), lit('running'));
|
|
997
|
+
// Once these branches decline, only nonterminal answers remain. A test
|
|
998
|
+
// for stopped/failed needs none of the historical progress census below.
|
|
999
|
+
let verdict = (rest)=>when([
|
|
1000
|
+
[
|
|
1001
|
+
or(n('stop'), and(notNull(f('stop_seq')), not(after(f('stop_seq'))))),
|
|
1002
|
+
lit('stopped')
|
|
1003
|
+
],
|
|
1004
|
+
[
|
|
1005
|
+
f('abandoned'),
|
|
1006
|
+
lit('running')
|
|
1007
|
+
],
|
|
1008
|
+
[
|
|
1009
|
+
n('exception'),
|
|
1010
|
+
lit('failed')
|
|
1011
|
+
],
|
|
1012
|
+
[
|
|
1013
|
+
and(n('interrupted'), n('failed'), not(n('ask')), not(n('call'))),
|
|
1014
|
+
lit('failed')
|
|
1015
|
+
],
|
|
1016
|
+
[
|
|
1017
|
+
f('inflight'),
|
|
1018
|
+
lit('running')
|
|
1019
|
+
],
|
|
1020
|
+
[
|
|
1021
|
+
eq(dispatch.expr(owner), lit('queued')),
|
|
1022
|
+
lit('queued')
|
|
1023
|
+
],
|
|
1024
|
+
[
|
|
1025
|
+
n('refusal'),
|
|
1026
|
+
iff(after(refused), lit('pending'), lit('failed'))
|
|
1027
|
+
],
|
|
1028
|
+
[
|
|
1029
|
+
and(a('interrupted'), a('failed'), not(after(col('a_seq', 'f')))),
|
|
1030
|
+
lit('failed')
|
|
1031
|
+
],
|
|
1032
|
+
[
|
|
1033
|
+
and(a('interrupted'), after(col('a_seq', 'f'))),
|
|
1034
|
+
lit('pending')
|
|
1035
|
+
],
|
|
1036
|
+
[
|
|
1037
|
+
and(a('interrupted'), a('provisional')),
|
|
1038
|
+
lit('pending')
|
|
1039
|
+
],
|
|
1040
|
+
[
|
|
1041
|
+
a('interrupted'),
|
|
1042
|
+
iff(ge(retries, lit(RETRIES)), lit('failed'), lit('pending'))
|
|
1043
|
+
]
|
|
1044
|
+
], rest);
|
|
1045
|
+
let nonterminal = when([
|
|
1046
|
+
[
|
|
1047
|
+
f('open'),
|
|
1048
|
+
lit('running')
|
|
1049
|
+
],
|
|
1050
|
+
[
|
|
1051
|
+
and(or(after(boundary), and(eq(f('unheard'), lit(0)), gt(f('ready_seq'), boundary))), notNull(background(false))),
|
|
1052
|
+
lit('pending')
|
|
1053
|
+
],
|
|
1054
|
+
[
|
|
1055
|
+
notNull(background(true)),
|
|
1056
|
+
lit('running')
|
|
1057
|
+
],
|
|
1058
|
+
[
|
|
1059
|
+
and(n('ask'), n('attempt'), isNull(col('n_attempt_by', 'f')), not(n('interrupted'))),
|
|
1060
|
+
lit('settled')
|
|
1061
|
+
],
|
|
1062
|
+
[
|
|
1063
|
+
or(n('ask'), n('call')),
|
|
1064
|
+
lit('running')
|
|
1065
|
+
],
|
|
1066
|
+
[
|
|
1067
|
+
and(n('output'), not(n('result')), not(n('tool_progress')), not(n('interrupted'))),
|
|
1068
|
+
iff(after(col('n_boundary_seq', 'f')), lit('pending'), lit('settled'))
|
|
1069
|
+
]
|
|
1070
|
+
], owed);
|
|
1071
|
+
// Keep the expression below workerd's depth limit, including the runner's
|
|
1072
|
+
// enclosing union. FROM subqueries and common table expressions don't add
|
|
1073
|
+
// to expression height.
|
|
1074
|
+
let decision = (rest)=>select({
|
|
1075
|
+
cols: [
|
|
1076
|
+
as(verdict(rest), 'v')
|
|
1077
|
+
],
|
|
1078
|
+
from: from(facts, 'f'),
|
|
1079
|
+
where: notNull(col('any', 'f'))
|
|
1080
|
+
});
|
|
1081
|
+
let ended = sub(select({
|
|
1082
|
+
cols: [
|
|
1083
|
+
col('ended', 's')
|
|
1084
|
+
],
|
|
1085
|
+
from: table('session', 's'),
|
|
1086
|
+
where: eq(col('entity', 's'), owner)
|
|
1087
|
+
}));
|
|
1088
|
+
// A newest stop, or an ended ask with no attempt or failure, is stopped
|
|
1089
|
+
// whatever the earlier turn owes. Neither needs its history read.
|
|
1090
|
+
let said = (verdict)=>fn('coalesce', sub(select({
|
|
1091
|
+
with: [
|
|
1092
|
+
{
|
|
1093
|
+
name: LATEST,
|
|
1094
|
+
q: latest,
|
|
1095
|
+
materialized: false
|
|
1096
|
+
},
|
|
1097
|
+
// The anchor is consumed once by the sequenced branch (or its
|
|
1098
|
+
// legacy fallback). Inlining avoids a billed temporary-row read;
|
|
1099
|
+
// its newest-entry and ask seeks remain bounded.
|
|
1100
|
+
{
|
|
1101
|
+
name: ANCHOR,
|
|
1102
|
+
q: anchors,
|
|
1103
|
+
materialized: false
|
|
1104
|
+
}
|
|
1105
|
+
],
|
|
1106
|
+
cols: [
|
|
1107
|
+
iff(exists(select({
|
|
1108
|
+
from: table(LATEST, 'l'),
|
|
1109
|
+
where: or(col('stop', 'l'), and(ended, col('unfinished', 'l')))
|
|
1110
|
+
})), lit('stopped'), sub(select({
|
|
1111
|
+
cols: [
|
|
1112
|
+
col('v', 'x')
|
|
1113
|
+
],
|
|
1114
|
+
from: from(verdict, 'x')
|
|
1115
|
+
})))
|
|
1116
|
+
]
|
|
1117
|
+
})), lit('empty'));
|
|
1118
|
+
// A process of its own runs it while its row wears a `process` with no
|
|
1119
|
+
// `exit` (./state.ts `Marks`): what the entries leave owing is that
|
|
1120
|
+
// process's to answer, never the runner's.
|
|
1121
|
+
let wearing = (name)=>exists(select({
|
|
1122
|
+
cols: [
|
|
1123
|
+
lit(1)
|
|
1124
|
+
],
|
|
1125
|
+
from: table(name, 'm'),
|
|
1126
|
+
where: eq(col('entity', 'm'), owner)
|
|
1127
|
+
}));
|
|
1128
|
+
let process = declared('process') && declared('exit') ? and(wearing('process'), not(wearing('exit'))) : lit(false);
|
|
1129
|
+
// The mark ends only what its entries leave unfinished. The limit keeps the
|
|
1130
|
+
// status one column here, or flattening would work it out once per mention.
|
|
1131
|
+
let whole = sub(select({
|
|
1132
|
+
cols: [
|
|
1133
|
+
when([
|
|
1134
|
+
[
|
|
1135
|
+
and(fn('coalesce', col('ended', 'r'), lit(false)), among(col('v', 'r'), UNFINISHED.map(lit))),
|
|
1136
|
+
lit('stopped')
|
|
1137
|
+
],
|
|
1138
|
+
[
|
|
1139
|
+
and(process, eq(col('v', 'r'), lit('pending'))),
|
|
1140
|
+
lit('running')
|
|
1141
|
+
]
|
|
1142
|
+
], col('v', 'r'))
|
|
1143
|
+
],
|
|
1144
|
+
from: from(select({
|
|
1145
|
+
cols: [
|
|
1146
|
+
as(said(decision(nonterminal)), 'v'),
|
|
1147
|
+
as(ended, 'ended')
|
|
1148
|
+
],
|
|
1149
|
+
limit: lit(-1)
|
|
1150
|
+
}), 'r')
|
|
1151
|
+
}));
|
|
1152
|
+
if (!terminal) return whole;
|
|
1153
|
+
return iff(ended, whole, said(decision(lit(''))));
|
|
1154
|
+
}
|
|
1155
|
+
};
|
|
1156
|
+
/** The derived-property registry a SQLite store loads to read
|
|
1157
|
+
* `session.status`, compatible `dispatch.status`, and `session.cost` (./cost.ts)
|
|
1158
|
+
* where the vocabulary declares the `cost.dollars` it sums: a store may hold an app's own `cost`
|
|
1159
|
+
* in place of @yaks/model's. The configured `reading` registry resolves content
|
|
1160
|
+
* before interpreting historical controls; without an override, content is
|
|
1161
|
+
* text in its component. */ export let sessionDerived = (vocab, reading = {})=>{
|
|
1162
|
+
// Small compositions can declare dispatch without loading kernel's marks.
|
|
1163
|
+
// Missing marks are absent, not tables to read (nor an empty CASE ladder).
|
|
1164
|
+
let marks = [
|
|
1165
|
+
'waiting',
|
|
1166
|
+
'admitted'
|
|
1167
|
+
].filter((comp)=>vocab.comp(comp));
|
|
1168
|
+
let dispatch = {
|
|
1169
|
+
...dispatchStatus,
|
|
1170
|
+
deps: [
|
|
1171
|
+
'dispatch',
|
|
1172
|
+
...marks
|
|
1173
|
+
],
|
|
1174
|
+
expr: (owner)=>dispatchStatus.expr(owner, marks)
|
|
1175
|
+
};
|
|
1176
|
+
let states = {
|
|
1177
|
+
...attemptDerived(vocab),
|
|
1178
|
+
...executionDerived(vocab)
|
|
1179
|
+
};
|
|
1180
|
+
let text = reading['content.body']?.text;
|
|
1181
|
+
let declared = (scope)=>({
|
|
1182
|
+
declared: (name)=>!!vocab.comp(name) && (name != 'archetype' || !scope?.masked.length),
|
|
1183
|
+
holds: (tables, comp)=>descriptorHolds(tables, vocab, comp)
|
|
1184
|
+
});
|
|
1185
|
+
return {
|
|
1186
|
+
...states,
|
|
1187
|
+
'session.status': {
|
|
1188
|
+
...sessionStatus,
|
|
1189
|
+
stable: !reading['content.body'] || reading['content.body'].stable == true,
|
|
1190
|
+
test: (owner, values, scope)=>{
|
|
1191
|
+
// Unfinished words screen the one session as the select screens
|
|
1192
|
+
// every session: most rest, and the exact rule reads only the rest.
|
|
1193
|
+
if (!values.every((v)=>(v.t == 'val' || v.t == 'lit') && [
|
|
1194
|
+
'stopped',
|
|
1195
|
+
'failed'
|
|
1196
|
+
].includes(String(v.v)))) {
|
|
1197
|
+
let one = sessionStatus.select(values, dispatch, declared(scope), text, owner);
|
|
1198
|
+
if (one && values.every((v)=>(v.t == 'val' || v.t == 'lit') && UNFINISHED.includes(String(v.v)))) {
|
|
1199
|
+
// Ended sessions cannot match. Decline before creating even empty
|
|
1200
|
+
// candidate CTEs for each historical owner on an ordered page.
|
|
1201
|
+
return iff(sub(select({
|
|
1202
|
+
cols: [
|
|
1203
|
+
col('ended', 'ended')
|
|
1204
|
+
],
|
|
1205
|
+
from: table('session', 'ended'),
|
|
1206
|
+
where: eq(col('entity', 'ended'), owner)
|
|
1207
|
+
})), lit(false), exists(one));
|
|
1208
|
+
}
|
|
1209
|
+
return one && exists(one);
|
|
1210
|
+
}
|
|
1211
|
+
return among(sessionStatus.expr(owner, dispatch, declared(scope), text, true), values);
|
|
1212
|
+
},
|
|
1213
|
+
select: (values, scope)=>sessionStatus.select(values, dispatch, declared(scope), text),
|
|
1214
|
+
expr: (owner, scope)=>sessionStatus.expr(owner, dispatch, declared(scope), text)
|
|
1215
|
+
},
|
|
1216
|
+
'dispatch.status': dispatch,
|
|
1217
|
+
...vocab.prop(COST, 'dollars') ? {
|
|
1218
|
+
'session.cost': sessionCost
|
|
1219
|
+
} : {}
|
|
1220
|
+
};
|
|
1221
|
+
};
|
|
1222
|
+
//# sourceMappingURL=status.js.map
|