@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/children.js
ADDED
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
import { line, sub } from "@yaks/query";
|
|
2
|
+
import { derivedEid } from "@yaks/graph/identity";
|
|
3
|
+
import { appendEntry } from "./append.js";
|
|
4
|
+
import { workedOn } from "./lease.js";
|
|
5
|
+
// Delegation is transcript structure, not a process handle. A spawned session
|
|
6
|
+
// names its parent and originating call; a fork additionally names a prefix.
|
|
7
|
+
// Submission is serialized per graph (across parents and tool tables). The
|
|
8
|
+
// durable queue order and fork prefix commit with the child. Replays find
|
|
9
|
+
// that same child; preparation is deferred until the runner admits it
|
|
10
|
+
// (./run.ts).
|
|
11
|
+
import { aside, dispatchStatus, tail } from "./admission.js";
|
|
12
|
+
import { identityEid } from "@yaks/graph/identity";
|
|
13
|
+
import { Stale } from "@yaks/graph/errors";
|
|
14
|
+
import { token } from "@yaks/graph/sha256";
|
|
15
|
+
import { link } from "@yaks/edge";
|
|
16
|
+
import { human, parse } from "@yaks/id";
|
|
17
|
+
import { done, statusComps, statusOf as taskStatus } from "@yaks/task";
|
|
18
|
+
import { ToolError, transcript, wordsOf } from "./react.js";
|
|
19
|
+
import { entryStatus, newestOf, openOf, usingOf } from "./reach.js";
|
|
20
|
+
import { live, newestAsk, seqOf, textOf, usingBefore } from "./state.js";
|
|
21
|
+
import { system } from "@yaks/fp";
|
|
22
|
+
import { changes } from "./wait.js";
|
|
23
|
+
import { calledIn, childNotice, overheard } from "./child-notice.js";
|
|
24
|
+
import { askedFor, steered } from "./answer.js";
|
|
25
|
+
import { coldFork, forkRequest } from "./fork.js";
|
|
26
|
+
let locks = new WeakMap();
|
|
27
|
+
let comp = (b, name)=>b?.[name];
|
|
28
|
+
let row = async (g, eid, comps)=>(await g.get([
|
|
29
|
+
eid
|
|
30
|
+
], comps))[0];
|
|
31
|
+
let spawnedOf = async (g, session)=>comp(await row(g, session, [
|
|
32
|
+
'spawned'
|
|
33
|
+
]), 'spawned');
|
|
34
|
+
/** The session that directly started this helper or fork. */ export let parentOf = async (g, session)=>{
|
|
35
|
+
let parent = (await spawnedOf(g, session))?.parent;
|
|
36
|
+
return typeof parent == 'string' ? parent : undefined;
|
|
37
|
+
};
|
|
38
|
+
// Waiting needs only the child's place, not its computed transcript status/cost.
|
|
39
|
+
export let childPlace = async (g, eid)=>(await g.get([
|
|
40
|
+
eid
|
|
41
|
+
], [
|
|
42
|
+
'dispatch',
|
|
43
|
+
'admitted',
|
|
44
|
+
'waiting'
|
|
45
|
+
].filter((name)=>g.vocab.comp(name))))[0];
|
|
46
|
+
let taskRow = async (g, id, comps)=>{
|
|
47
|
+
let b = await row(g, id, comps);
|
|
48
|
+
if (!b && parse(id)) [b] = await g.read(line`.task .entity.eid=${id} *`);
|
|
49
|
+
if (!b?.task) throw new ToolError('task', `not a task: ${id}`);
|
|
50
|
+
return b;
|
|
51
|
+
};
|
|
52
|
+
/** The child a call starts ({@link spawnChild}'s `source`): the same each
|
|
53
|
+
* time it is asked, so a replay finds it, and so does a caller that names
|
|
54
|
+
* its own sources. */ export let childOf = (call)=>derivedEid(`child:${call}`);
|
|
55
|
+
/** Direct children, including forks created by the session tools. */ export let children = (g, session)=>Promise.resolve(g.read(line`.spawned.parent=${session}&*`));
|
|
56
|
+
// How many sessions the cap counts: their number, never their rows.
|
|
57
|
+
let running = async (g)=>Number((await g.rows(line`${sub(live)} .count`))[0]?.n ?? 0);
|
|
58
|
+
/** The admission path the harness calls, shared by root starts and delegated
|
|
59
|
+
* starts. */ export let admit = (g, parent, limits, create)=>{
|
|
60
|
+
let go = (locks.get(g) ?? Promise.resolve()).catch(()=>{}).then(async ()=>{
|
|
61
|
+
let maxChildren = limits.maxChildren ?? 32;
|
|
62
|
+
let maxSessions = limits.maxSessions ?? 64;
|
|
63
|
+
for (let n of [
|
|
64
|
+
maxChildren,
|
|
65
|
+
maxSessions
|
|
66
|
+
]){
|
|
67
|
+
if (!Number.isInteger(n) || n < 0) {
|
|
68
|
+
throw new Error('invalid session cap');
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (!parent && await running(g) >= maxSessions) {
|
|
72
|
+
throw new ToolError('session_cap', `live session cap (${maxSessions}) reached`);
|
|
73
|
+
}
|
|
74
|
+
return await create();
|
|
75
|
+
});
|
|
76
|
+
locks.set(g, go);
|
|
77
|
+
return go;
|
|
78
|
+
};
|
|
79
|
+
let caller = (ctx)=>{
|
|
80
|
+
if (!ctx) throw new ToolError('caller', 'session tool requires a caller');
|
|
81
|
+
return ctx;
|
|
82
|
+
};
|
|
83
|
+
// The name a child was given (`title`), as its own `doc{title}`, which
|
|
84
|
+
// names it wherever a session is named (./name.ts `sessionName`).
|
|
85
|
+
let named = (g, title)=>typeof title == 'string' && title.trim() && g.vocab.comp('doc') ? {
|
|
86
|
+
doc: {
|
|
87
|
+
title: title.trim().replace(/\s+/g, ' ')
|
|
88
|
+
}
|
|
89
|
+
} : {};
|
|
90
|
+
let FORK_LATEST = 'You are a fork of this conversation; do the work the latest message asks for, then report';
|
|
91
|
+
let forkStarted = (child, request, title)=>`Fork ${child} started for message ${request.entity.eid}` + (typeof title == 'string' ? ` (${title.trim()}).` : '.');
|
|
92
|
+
let calledFrom = (ctx)=>{
|
|
93
|
+
let source = comp(ctx.call, 'call')?.source;
|
|
94
|
+
let ask = source ? ctx.entries.find((b)=>b.entity.eid == source) : newestAsk(ctx.entries);
|
|
95
|
+
let through = comp(ask, 'ask')?.through;
|
|
96
|
+
return {
|
|
97
|
+
ask,
|
|
98
|
+
anchor: ctx.entries.find((b)=>b.entity.eid == through)
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
/** One admission/write path for model delegation and user-created tasks. */ let delegation = (g, limits, fork, minted, submitted = false)=>({
|
|
102
|
+
name: fork ? 'fork' : 'spawn',
|
|
103
|
+
...fork ? {
|
|
104
|
+
excludeFromForks: true
|
|
105
|
+
} : {},
|
|
106
|
+
step: fork == 'latest' ? {
|
|
107
|
+
doing: 'Starting work…',
|
|
108
|
+
done: 'Started work'
|
|
109
|
+
} : {
|
|
110
|
+
doing: 'Starting a helper…',
|
|
111
|
+
done: 'Started a helper'
|
|
112
|
+
},
|
|
113
|
+
description: fork == 'latest' ? 'Default for delegating the latest request. The fork sees your conversation through that request and reports back automatically. Give it a short title. The harness supplies its only instruction.' : fork ? 'Fork your transcript before this tool turn with a prompt OR atomically claimed task (original title/body). Returns the concurrent child session id; completion is delivered automatically. For work of many steps or parts that run side by side; do what takes a call or two yourself.' : 'Start a fresh subagent with a prompt OR a task id. A task is claimed atomically and its title/body become the input. Returns a child ID immediately; a shared worker pool queues excess work durably and delivers completion automatically. For work of many steps or that needs a clean start; do what takes a call or two yourself.',
|
|
114
|
+
parameters: {
|
|
115
|
+
type: 'object',
|
|
116
|
+
properties: {
|
|
117
|
+
...fork != 'latest' ? {
|
|
118
|
+
...limits.childProperties,
|
|
119
|
+
prompt: {
|
|
120
|
+
type: 'string'
|
|
121
|
+
},
|
|
122
|
+
task: {
|
|
123
|
+
type: 'string',
|
|
124
|
+
description: 'task entity id or T-id'
|
|
125
|
+
},
|
|
126
|
+
instructions: {
|
|
127
|
+
type: 'string'
|
|
128
|
+
},
|
|
129
|
+
model: {
|
|
130
|
+
type: 'string',
|
|
131
|
+
description: 'model name or existing model entity id'
|
|
132
|
+
},
|
|
133
|
+
effort: {
|
|
134
|
+
type: 'string'
|
|
135
|
+
}
|
|
136
|
+
} : {},
|
|
137
|
+
title: {
|
|
138
|
+
type: 'string',
|
|
139
|
+
description: 'A short name for the job, 3 to 6 words, as a person would name it. Lists of helpers show it.'
|
|
140
|
+
},
|
|
141
|
+
...fork != 'latest' ? {
|
|
142
|
+
report: {
|
|
143
|
+
type: 'string',
|
|
144
|
+
enum: [
|
|
145
|
+
'resume',
|
|
146
|
+
'record'
|
|
147
|
+
],
|
|
148
|
+
description: 'Record a successful report without starting a turn, or resume this conversation when it finishes. Failures and reports a turn is waiting for still reach it.'
|
|
149
|
+
}
|
|
150
|
+
} : {}
|
|
151
|
+
},
|
|
152
|
+
...fork == 'latest' ? {
|
|
153
|
+
additionalProperties: false
|
|
154
|
+
} : {}
|
|
155
|
+
},
|
|
156
|
+
run: async (args, context)=>{
|
|
157
|
+
let ctx = caller(context);
|
|
158
|
+
let latest = fork == 'latest';
|
|
159
|
+
// A cached offer or a replay can still send the earlier tool's fields.
|
|
160
|
+
if (latest) args = {
|
|
161
|
+
title: args.title,
|
|
162
|
+
report: 'record'
|
|
163
|
+
};
|
|
164
|
+
if (!latest && args.task != null == (args.prompt != null)) {
|
|
165
|
+
throw new ToolError('spawn', 'name exactly one prompt or task');
|
|
166
|
+
}
|
|
167
|
+
if (args.task != null && (typeof args.task != 'string' || !args.task.trim())) {
|
|
168
|
+
throw new ToolError('task', 'a nonempty task id is required');
|
|
169
|
+
}
|
|
170
|
+
if (!latest && args.task == null && (typeof args.prompt != 'string' || !args.prompt.trim())) {
|
|
171
|
+
throw new ToolError('prompt', 'a nonempty prompt is required');
|
|
172
|
+
}
|
|
173
|
+
let eid = childOf(ctx.call.entity.eid);
|
|
174
|
+
let at = latest ? calledFrom(ctx).anchor : undefined;
|
|
175
|
+
let request = at && forkRequest(ctx.session, at, ctx.entries);
|
|
176
|
+
let started = at ? forkStarted(eid, request ?? at, args.title) : eid;
|
|
177
|
+
if (await row(g, eid)) return started;
|
|
178
|
+
// An older request or a model guessing the tool name can still call it.
|
|
179
|
+
if (fork && !minted && (await row(g, ctx.session))?.fork) {
|
|
180
|
+
throw new ToolError('fork', 'You are a fork already: do this task here yourself, and give your report as your final reply.');
|
|
181
|
+
}
|
|
182
|
+
return admit(g, ctx.session, limits, async ()=>{
|
|
183
|
+
// A concurrent replay may have waited behind the original admission.
|
|
184
|
+
if (await row(g, eid)) return started;
|
|
185
|
+
let task = minted ?? (args.task == null ? undefined : await taskRow(g, String(args.task)));
|
|
186
|
+
let work = minted ? await g.read(line`.task .claim.session=${ctx.session}`) : [];
|
|
187
|
+
let parents = work.length ? work.map((b)=>b.entity.eid) : [
|
|
188
|
+
ctx.session
|
|
189
|
+
];
|
|
190
|
+
let doc = comp(task, 'doc');
|
|
191
|
+
let prompt = latest ? FORK_LATEST : minted ? doc?.body || doc?.title || '' : task ? [
|
|
192
|
+
doc?.title,
|
|
193
|
+
doc?.body
|
|
194
|
+
].filter(Boolean).join('\n\n') : args.prompt;
|
|
195
|
+
if (minted && limits.taskDefaults) {
|
|
196
|
+
args = {
|
|
197
|
+
...await limits.taskDefaults(ctx.session, eid),
|
|
198
|
+
...args
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (minted) ctx = {
|
|
202
|
+
...ctx,
|
|
203
|
+
entries: await transcript(g, ctx.session)
|
|
204
|
+
};
|
|
205
|
+
let { ask, anchor } = calledFrom(ctx);
|
|
206
|
+
let inherited = latest ? comp(ask, 'using') ?? usingBefore(ctx.entries, anchor && seqOf(anchor)) : usingBefore(ctx.entries);
|
|
207
|
+
let using = {
|
|
208
|
+
...inherited
|
|
209
|
+
};
|
|
210
|
+
let models = [];
|
|
211
|
+
if (args.model != null) {
|
|
212
|
+
// A model is named by its id or by its own name. The provider in force
|
|
213
|
+
// stays only while it serves the model; otherwise the resolver picks
|
|
214
|
+
// one of the model's providers. A name nothing knows yet becomes a
|
|
215
|
+
// requested model (@yaks/model `requesting`): the provider in force is
|
|
216
|
+
// asked for it, and only its answer records that it serves it. Until
|
|
217
|
+
// then nobody serves it, so a helper started meanwhile asks the
|
|
218
|
+
// provider in force too.
|
|
219
|
+
let said = String(args.model);
|
|
220
|
+
let known = await row(g, said);
|
|
221
|
+
let model = known?.model ? said : identityEid('model', [
|
|
222
|
+
said
|
|
223
|
+
]);
|
|
224
|
+
using.model = model;
|
|
225
|
+
let held = known?.model ? known : await row(g, model);
|
|
226
|
+
if (held && !held.pending) {
|
|
227
|
+
let offers = await g.read(line`.serves .edge.to=${model}`);
|
|
228
|
+
if (!offers.some((o)=>comp(o, 'edge')?.from == using.provider)) {
|
|
229
|
+
delete using.provider;
|
|
230
|
+
}
|
|
231
|
+
} else if (!held) {
|
|
232
|
+
models.push({
|
|
233
|
+
entity: {
|
|
234
|
+
eid: model
|
|
235
|
+
},
|
|
236
|
+
model: {
|
|
237
|
+
name: said,
|
|
238
|
+
offered: false
|
|
239
|
+
},
|
|
240
|
+
pending: {}
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
// Legacy base instructions are inherited; child guidance is additive.
|
|
245
|
+
for (let key of [
|
|
246
|
+
'effort'
|
|
247
|
+
]){
|
|
248
|
+
if (args[key] != null) using[key] = String(args[key]);
|
|
249
|
+
}
|
|
250
|
+
if (limits.using) using = await limits.using(using, inherited ?? {});
|
|
251
|
+
// Never inherit the unanswered delegation call (or any sibling calls).
|
|
252
|
+
if (minted) {
|
|
253
|
+
// User tasks reference a contiguous parent prefix. Only an active
|
|
254
|
+
// attempt can still change it; terminal attempts must not pin future
|
|
255
|
+
// forks to an old boundary. Inputs after this boundary remain in the
|
|
256
|
+
// parent, rather than being copied into the child.
|
|
257
|
+
let active = ctx.entries.findIndex((b)=>b.attempt?.state == 'inflight');
|
|
258
|
+
anchor = active == 0 ? undefined : ctx.entries.at(active < 0 ? -1 : active - 1);
|
|
259
|
+
}
|
|
260
|
+
if (fork && !anchor && !minted) {
|
|
261
|
+
throw new ToolError('fork', 'no prefix to fork');
|
|
262
|
+
}
|
|
263
|
+
if (fork && anchor && ctx.entries.some((b)=>b.attempt?.state == 'inflight' && Number(b.entry?.seq) <= Number(anchor.entry?.seq))) {
|
|
264
|
+
throw new ToolError('fork', 'Cannot inherit an in-flight response; wait for its completion');
|
|
265
|
+
}
|
|
266
|
+
// Fork history is immutable. Fresh children receive the same shared
|
|
267
|
+
// snapshots, never a filesystem reread or a replacement persona.
|
|
268
|
+
let context = fork ? [] : ctx.entries.filter((b)=>b.prompt?.scope == 'shared').map((b, i)=>({
|
|
269
|
+
entity: {
|
|
270
|
+
eid: derivedEid(eid + ':shared:' + i)
|
|
271
|
+
},
|
|
272
|
+
content: {
|
|
273
|
+
body: textOf(b)
|
|
274
|
+
},
|
|
275
|
+
prompt: {
|
|
276
|
+
...b.prompt
|
|
277
|
+
}
|
|
278
|
+
}));
|
|
279
|
+
if (fork && !latest) {
|
|
280
|
+
context.push({
|
|
281
|
+
entity: {
|
|
282
|
+
eid: derivedEid(eid + ':fork-context')
|
|
283
|
+
},
|
|
284
|
+
prompt: {
|
|
285
|
+
scope: 'local',
|
|
286
|
+
source: 'harness:fork'
|
|
287
|
+
},
|
|
288
|
+
content: {
|
|
289
|
+
body: 'You are a fork of the conversation above, started for the task below. Do it here yourself: you already hold all of its context, so forking it again only repeats you. Start a fresh subagent only for a separate part that needs a clean start. Your final reply is your report to the parent and ends your work, so give it when the task is done, not before.'
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
if (args.instructions != null) {
|
|
294
|
+
context.push({
|
|
295
|
+
entity: {
|
|
296
|
+
eid: derivedEid(eid + ':guidance')
|
|
297
|
+
},
|
|
298
|
+
prompt: {
|
|
299
|
+
scope: 'local',
|
|
300
|
+
source: 'delegation:instructions'
|
|
301
|
+
},
|
|
302
|
+
content: {
|
|
303
|
+
body: String(args.instructions)
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
context = context.map((b)=>({
|
|
308
|
+
...b,
|
|
309
|
+
entry: {
|
|
310
|
+
session: eid,
|
|
311
|
+
...comp(b, 'prompt')?.source == 'delegation:instructions' ? {
|
|
312
|
+
from: 'your parent session'
|
|
313
|
+
} : {}
|
|
314
|
+
}
|
|
315
|
+
}));
|
|
316
|
+
let cold = latest && request && anchor && limits.forkRecap ? coldFork(ctx.entries, ctx.session, request, anchor) : undefined;
|
|
317
|
+
let recap = cold && request ? await limits.forkRecap?.(request, cold.visible) : undefined;
|
|
318
|
+
let order = await tail(g);
|
|
319
|
+
await g.apply([
|
|
320
|
+
...minted ? [
|
|
321
|
+
{
|
|
322
|
+
entity: {
|
|
323
|
+
eid: derivedEid('notice:' + eid)
|
|
324
|
+
},
|
|
325
|
+
entry: {
|
|
326
|
+
session: ctx.session
|
|
327
|
+
},
|
|
328
|
+
notice: {
|
|
329
|
+
child: eid,
|
|
330
|
+
task: minted.entity.eid
|
|
331
|
+
},
|
|
332
|
+
content: {
|
|
333
|
+
body: [
|
|
334
|
+
'Task: ' + String(doc?.title ?? ''),
|
|
335
|
+
'Origin: user-created task, automatically delegated to child ' + eid,
|
|
336
|
+
'Request:',
|
|
337
|
+
String(doc?.body ?? '')
|
|
338
|
+
].join('\n')
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
] : [],
|
|
342
|
+
...models,
|
|
343
|
+
...context,
|
|
344
|
+
...minted ? [
|
|
345
|
+
minted,
|
|
346
|
+
...parents.map((parent)=>link(parent, 'contains', minted.entity.eid))
|
|
347
|
+
] : [],
|
|
348
|
+
...task ? [
|
|
349
|
+
{
|
|
350
|
+
entity: task.entity,
|
|
351
|
+
claim: {
|
|
352
|
+
session: eid
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
] : [],
|
|
356
|
+
{
|
|
357
|
+
entity: {
|
|
358
|
+
eid
|
|
359
|
+
},
|
|
360
|
+
dispatch: {
|
|
361
|
+
state: 'queued',
|
|
362
|
+
args: JSON.stringify(args),
|
|
363
|
+
order
|
|
364
|
+
},
|
|
365
|
+
session: {
|
|
366
|
+
id: eid
|
|
367
|
+
},
|
|
368
|
+
native: {},
|
|
369
|
+
spawned: {
|
|
370
|
+
parent: ctx.session,
|
|
371
|
+
...args.report == 'resume' || args.report == 'record' ? {
|
|
372
|
+
report: args.report
|
|
373
|
+
} : {},
|
|
374
|
+
...(await spawnedOf(g, ctx.session))?.notice ? {
|
|
375
|
+
notice: true
|
|
376
|
+
} : {},
|
|
377
|
+
...minted || submitted ? {} : {
|
|
378
|
+
call: ctx.call.entity.eid
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
...fork && anchor ? {
|
|
382
|
+
fork: {
|
|
383
|
+
from: anchor.entity.eid,
|
|
384
|
+
...request ? {
|
|
385
|
+
request: request.entity.eid
|
|
386
|
+
} : {},
|
|
387
|
+
...recap && cold?.before ? {
|
|
388
|
+
elided_before: cold.before
|
|
389
|
+
} : {}
|
|
390
|
+
}
|
|
391
|
+
} : {},
|
|
392
|
+
...named(g, args.title)
|
|
393
|
+
},
|
|
394
|
+
...recap ? [
|
|
395
|
+
{
|
|
396
|
+
entity: {
|
|
397
|
+
eid: derivedEid(`${eid}:recap`)
|
|
398
|
+
},
|
|
399
|
+
entry: {
|
|
400
|
+
session: eid
|
|
401
|
+
},
|
|
402
|
+
content: {
|
|
403
|
+
body: recap
|
|
404
|
+
},
|
|
405
|
+
prompt: {
|
|
406
|
+
scope: 'recap',
|
|
407
|
+
source: 'harness:recap'
|
|
408
|
+
},
|
|
409
|
+
system: {}
|
|
410
|
+
}
|
|
411
|
+
] : [],
|
|
412
|
+
{
|
|
413
|
+
entity: {
|
|
414
|
+
eid: derivedEid(`${eid}:input`)
|
|
415
|
+
},
|
|
416
|
+
entry: {
|
|
417
|
+
session: eid,
|
|
418
|
+
...latest ? {} : {
|
|
419
|
+
from: 'your parent session'
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
content: {
|
|
423
|
+
body: prompt
|
|
424
|
+
},
|
|
425
|
+
...latest ? {
|
|
426
|
+
prompt: {
|
|
427
|
+
scope: 'local',
|
|
428
|
+
source: 'harness:fork'
|
|
429
|
+
},
|
|
430
|
+
system: {}
|
|
431
|
+
} : {},
|
|
432
|
+
using,
|
|
433
|
+
// What a steered turn starts is steered: a call made where someone
|
|
434
|
+
// else's words may be asking hands its child those words' mark, so
|
|
435
|
+
// the child's own turn is steered too (./answer.ts `steered`),
|
|
436
|
+
// judged for whom the call's turn works (`askedFor`).
|
|
437
|
+
...!minted && !submitted && steered(ctx.entries, askedFor(ctx.entries, ctx.call)) ? {
|
|
438
|
+
heard: {}
|
|
439
|
+
} : {}
|
|
440
|
+
}
|
|
441
|
+
], {
|
|
442
|
+
trusted: true
|
|
443
|
+
});
|
|
444
|
+
return started;
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
/** Submit helper work from an outside caller, without inventing a model tool
|
|
449
|
+
* call in the parent's transcript. It shares the model's admission path. */ export let spawnChild = async (g, session, args, limits = {}, source = crypto.randomUUID())=>wordsOf(await delegation(g, limits, false, undefined, true).run(args, {
|
|
450
|
+
session,
|
|
451
|
+
call: {
|
|
452
|
+
entity: {
|
|
453
|
+
eid: source
|
|
454
|
+
}
|
|
455
|
+
},
|
|
456
|
+
entries: await transcript(g, session)
|
|
457
|
+
}));
|
|
458
|
+
/** Submit a microtask under the session's claimed work (or the session when
|
|
459
|
+
* it holds no task). The task, containment, child and claim commit together:
|
|
460
|
+
* queued work is accepted without preparing a checkout. No filing metadata is inherited.
|
|
461
|
+
* The first line is the title; the complete submitted text is kept as body.
|
|
462
|
+
* This is called by a person, not by a model, so delivery is an ordinary
|
|
463
|
+
* input. */ export let taskEntry = async (g, session, text, limits = {})=>{
|
|
464
|
+
if (!text.trim()) throw new ToolError('task', 'a nonempty task is required');
|
|
465
|
+
if (!(await row(g, session))?.session) {
|
|
466
|
+
throw new ToolError('session', `not a session: ${session}`);
|
|
467
|
+
}
|
|
468
|
+
let task = crypto.randomUUID();
|
|
469
|
+
let minted = {
|
|
470
|
+
entity: {
|
|
471
|
+
eid: task
|
|
472
|
+
},
|
|
473
|
+
doc: {
|
|
474
|
+
title: text.trim().split('\n')[0].slice(0, 120),
|
|
475
|
+
body: text
|
|
476
|
+
},
|
|
477
|
+
task: {}
|
|
478
|
+
};
|
|
479
|
+
let child = await delegation(g, limits, true, minted).run({
|
|
480
|
+
task
|
|
481
|
+
}, {
|
|
482
|
+
session,
|
|
483
|
+
// Identity only; no fabricated tool call is written into the transcript.
|
|
484
|
+
call: {
|
|
485
|
+
entity: {
|
|
486
|
+
eid: task
|
|
487
|
+
}
|
|
488
|
+
},
|
|
489
|
+
entries: await transcript(g, session)
|
|
490
|
+
});
|
|
491
|
+
return {
|
|
492
|
+
task,
|
|
493
|
+
child: wordsOf(child)
|
|
494
|
+
};
|
|
495
|
+
};
|
|
496
|
+
/** The model-facing tools use the same admission and spawn write as
|
|
497
|
+
* taskEntry. */ export let sessionTools = (g, limits = {})=>{
|
|
498
|
+
let fork = delegation(g, limits, limits.forkLatest ? 'latest' : true);
|
|
499
|
+
let spawn = delegation(g, limits, false);
|
|
500
|
+
return [
|
|
501
|
+
fork,
|
|
502
|
+
spawn,
|
|
503
|
+
{
|
|
504
|
+
name: 'notice',
|
|
505
|
+
description: 'Append your words as passive context to a session without waking it, identified as coming from your session. Sequence is assigned atomically; do not guess entry.seq.',
|
|
506
|
+
parameters: {
|
|
507
|
+
type: 'object',
|
|
508
|
+
properties: {
|
|
509
|
+
session: {
|
|
510
|
+
type: 'string'
|
|
511
|
+
},
|
|
512
|
+
body: {
|
|
513
|
+
type: 'string'
|
|
514
|
+
},
|
|
515
|
+
eid: {
|
|
516
|
+
type: 'string',
|
|
517
|
+
description: 'optional idempotent entry identity'
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
required: [
|
|
521
|
+
'session',
|
|
522
|
+
'body'
|
|
523
|
+
]
|
|
524
|
+
},
|
|
525
|
+
run: async (args, context)=>{
|
|
526
|
+
let ctx = caller(context);
|
|
527
|
+
let session = String(args.session);
|
|
528
|
+
if (!(await row(g, session))?.session) {
|
|
529
|
+
throw new ToolError('session', 'not a session: ' + session);
|
|
530
|
+
}
|
|
531
|
+
let added = await appendEntry(g, session, String(args.body), {
|
|
532
|
+
notice: true,
|
|
533
|
+
from: `session ${ctx.session}`,
|
|
534
|
+
eid: args.eid == null ? undefined : String(args.eid)
|
|
535
|
+
});
|
|
536
|
+
return added[0].entity.eid;
|
|
537
|
+
}
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
name: 'wait',
|
|
541
|
+
waits: true,
|
|
542
|
+
description: 'Wait on direct children or tasks. Tasks wait until settled with no open dependencies; returns status when complete or the timeout passes.',
|
|
543
|
+
parameters: {
|
|
544
|
+
type: 'object',
|
|
545
|
+
properties: {
|
|
546
|
+
children: {
|
|
547
|
+
type: 'array',
|
|
548
|
+
items: {
|
|
549
|
+
type: 'string'
|
|
550
|
+
},
|
|
551
|
+
minItems: 1
|
|
552
|
+
},
|
|
553
|
+
tasks: {
|
|
554
|
+
type: 'array',
|
|
555
|
+
items: {
|
|
556
|
+
type: 'string'
|
|
557
|
+
},
|
|
558
|
+
minItems: 1
|
|
559
|
+
},
|
|
560
|
+
timeout: {
|
|
561
|
+
type: 'number',
|
|
562
|
+
description: 'milliseconds (default 60000)'
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
},
|
|
566
|
+
run: async (args, context)=>{
|
|
567
|
+
let ctx = caller(context);
|
|
568
|
+
if (args.tasks == null == (args.children == null)) {
|
|
569
|
+
throw new ToolError('wait', 'name either tasks or child sessions');
|
|
570
|
+
}
|
|
571
|
+
let named = args.tasks ?? args.children;
|
|
572
|
+
if (!Array.isArray(named) || !named.length || named.some((s)=>typeof s != 'string')) {
|
|
573
|
+
throw new ToolError('wait', 'name at least one task or child session');
|
|
574
|
+
}
|
|
575
|
+
let ids = [
|
|
576
|
+
...new Set(named)
|
|
577
|
+
];
|
|
578
|
+
// Each look reads only what decides the answer: a task's status marks,
|
|
579
|
+
// a child's place and transcript status. A child's output is read once,
|
|
580
|
+
// for the answer.
|
|
581
|
+
let marks = statusComps(g.vocab);
|
|
582
|
+
if (args.tasks != null) {
|
|
583
|
+
ids = await Promise.all(ids.map(async (id)=>(await taskRow(g, id, marks)).entity.eid));
|
|
584
|
+
}
|
|
585
|
+
for (let id of args.tasks == null ? ids : []){
|
|
586
|
+
if (await parentOf(g, id) != ctx.session) {
|
|
587
|
+
throw new ToolError('children', `not your child: ${id}`);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
let ms = Number(args.timeout ?? 60_000);
|
|
591
|
+
if (!Number.isFinite(ms) || ms < 0) {
|
|
592
|
+
throw new ToolError('timeout', 'invalid timeout');
|
|
593
|
+
}
|
|
594
|
+
let clock = limits.clock ?? system;
|
|
595
|
+
let end = clock.now() + ms;
|
|
596
|
+
let back = await aside(g, ctx.session, limits, ctx.signal);
|
|
597
|
+
let task = async (id)=>({
|
|
598
|
+
task: id,
|
|
599
|
+
status: taskStatus(g.vocab, await taskRow(g, id, marks)),
|
|
600
|
+
done: await done(g, id)
|
|
601
|
+
});
|
|
602
|
+
let child = async (session)=>{
|
|
603
|
+
let state = dispatchStatus(await childPlace(g, session));
|
|
604
|
+
let now = await entryStatus(g, session);
|
|
605
|
+
return state == 'queued' && now != 'stopped' ? 'queued' : now;
|
|
606
|
+
};
|
|
607
|
+
let ended = (status)=>[
|
|
608
|
+
'settled',
|
|
609
|
+
'failed',
|
|
610
|
+
'stopped'
|
|
611
|
+
].includes(status);
|
|
612
|
+
try {
|
|
613
|
+
for await (let over of changes(g, {
|
|
614
|
+
timeout: end - clock.now(),
|
|
615
|
+
signal: ctx.signal,
|
|
616
|
+
clock
|
|
617
|
+
})){
|
|
618
|
+
if (ctx.signal?.aborted) return JSON.stringify({
|
|
619
|
+
stopped: true
|
|
620
|
+
});
|
|
621
|
+
if (args.tasks != null) {
|
|
622
|
+
let results = await Promise.all(ids.map(task));
|
|
623
|
+
if (results.every((r)=>r.done) || over) {
|
|
624
|
+
return JSON.stringify(results);
|
|
625
|
+
}
|
|
626
|
+
} else {
|
|
627
|
+
let states = await Promise.all(ids.map(child));
|
|
628
|
+
if (states.every(ended) || over) {
|
|
629
|
+
let text = JSON.stringify(await Promise.all(ids.map(async (session, n)=>{
|
|
630
|
+
let last = await newestOf(g, session);
|
|
631
|
+
return {
|
|
632
|
+
session,
|
|
633
|
+
status: states[n],
|
|
634
|
+
output: last ? textOf(last) : ''
|
|
635
|
+
};
|
|
636
|
+
})));
|
|
637
|
+
// A child's output is its report, `heard` as it is anywhere.
|
|
638
|
+
let heard = (await Promise.all(ids.map((c)=>overheard(g, c)))).some(Boolean);
|
|
639
|
+
return heard ? {
|
|
640
|
+
text,
|
|
641
|
+
with: {
|
|
642
|
+
heard: {}
|
|
643
|
+
}
|
|
644
|
+
} : text;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
throw new Error('wait ended without a final read');
|
|
649
|
+
} finally{
|
|
650
|
+
await back();
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
];
|
|
655
|
+
};
|
|
656
|
+
/** The session `session` works for: up its `spawned` links to a person's
|
|
657
|
+
* conversation, or to a session started on its own (`spawned.quiet`). */ export let topOf = async (g, session)=>{
|
|
658
|
+
let link = await spawnedOf(g, session);
|
|
659
|
+
if (!link?.parent || link.quiet != null) return session;
|
|
660
|
+
return topOf(g, String(link.parent));
|
|
661
|
+
};
|
|
662
|
+
// What a helper was asked, in a line: its title, else the first words it
|
|
663
|
+
// was started with.
|
|
664
|
+
let askedOf = async (g, helper)=>{
|
|
665
|
+
let title = String(comp(await row(g, helper, [
|
|
666
|
+
'doc'
|
|
667
|
+
]), 'doc')?.title ?? '');
|
|
668
|
+
if (title) return title;
|
|
669
|
+
let opening = await g.read(line`.entry.session=${helper}&.order=entry.seq&.limit=3&*`);
|
|
670
|
+
let first = opening.find((b)=>!b.prompt && b.content);
|
|
671
|
+
return textOf(first ?? {
|
|
672
|
+
entity: {
|
|
673
|
+
eid: helper
|
|
674
|
+
}
|
|
675
|
+
}).trim().split('\n')[0].slice(0, 80) || 'a piece of work';
|
|
676
|
+
};
|
|
677
|
+
/**
|
|
678
|
+
* Stop `session` and every helper under it, however deep, in one write: the
|
|
679
|
+
* step each is in is withdrawn (./run.ts), and none goes on past it until new
|
|
680
|
+
* words reopen it. A helper `by` stops says who stopped it in its stop, which
|
|
681
|
+
* the session that started it reads in its report (`deliverChild`), and the
|
|
682
|
+
* conversation it works for ({@link topOf}), where that is further up, is told
|
|
683
|
+
* of its end in a notice, as a passive child's is (./child-notice.ts), which
|
|
684
|
+
* no report of its own would tell it and which starts no turn. Answers whether
|
|
685
|
+
* any of them was working; where none was, nothing is written.
|
|
686
|
+
*/ export let stopAll = async (g, session, by, via)=>{
|
|
687
|
+
let helpers = [];
|
|
688
|
+
for(let parents = [
|
|
689
|
+
session
|
|
690
|
+
]; parents.length;){
|
|
691
|
+
parents = (await g.read(line`.spawned.parent=${parents}`)).map((b)=>b.entity.eid);
|
|
692
|
+
helpers.push(...parents);
|
|
693
|
+
}
|
|
694
|
+
// Each one's status as the store computes it (`session.status`): a tree of
|
|
695
|
+
// hundreds read transcript by transcript costs more than a request has.
|
|
696
|
+
let busy = [], idle = [];
|
|
697
|
+
for (let b of (await g.get([
|
|
698
|
+
session,
|
|
699
|
+
...helpers
|
|
700
|
+
], [
|
|
701
|
+
'session'
|
|
702
|
+
]))){
|
|
703
|
+
let status = comp(b, 'session')?.status;
|
|
704
|
+
if (status == 'pending' || status == 'running' || status == 'queued') {
|
|
705
|
+
busy.push(b.entity.eid);
|
|
706
|
+
} else if (status != 'stopped') idle.push(b.entity.eid);
|
|
707
|
+
}
|
|
708
|
+
let link = await spawnedOf(g, session);
|
|
709
|
+
if (!busy.length && !link?.notice) return false;
|
|
710
|
+
// The person as the model knows them: the id their own words are labeled
|
|
711
|
+
// with (@yaks/harness `senders`).
|
|
712
|
+
let person = link && !via ? human(g.vocab)(await row(g, by) ?? {
|
|
713
|
+
entity: {
|
|
714
|
+
eid: by
|
|
715
|
+
}
|
|
716
|
+
}) : '';
|
|
717
|
+
let parent = String(link?.parent ?? '');
|
|
718
|
+
let top = link ? await topOf(g, parent) : '';
|
|
719
|
+
let leave = "so leave it stopped, and don't start its work again another way, until you have talked with them.";
|
|
720
|
+
await g.apply([
|
|
721
|
+
...top && top != parent ? [
|
|
722
|
+
{
|
|
723
|
+
entity: {
|
|
724
|
+
eid: crypto.randomUUID()
|
|
725
|
+
},
|
|
726
|
+
entry: {
|
|
727
|
+
session: top
|
|
728
|
+
},
|
|
729
|
+
notice: {
|
|
730
|
+
child: session
|
|
731
|
+
},
|
|
732
|
+
delivery: {
|
|
733
|
+
child: session,
|
|
734
|
+
outcome: 'stopped'
|
|
735
|
+
},
|
|
736
|
+
content: {
|
|
737
|
+
body: via ? `Your Operator stopped helper ${session} because the request changed.` : `The person (${person}) stopped helper ${session} ("${await askedOf(g, session)}"), which one of your helpers started, ${leave}`
|
|
738
|
+
},
|
|
739
|
+
$actor: {
|
|
740
|
+
by,
|
|
741
|
+
via: via ?? top
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
] : [],
|
|
745
|
+
...[
|
|
746
|
+
...busy,
|
|
747
|
+
...idle
|
|
748
|
+
].map((s)=>({
|
|
749
|
+
entity: {
|
|
750
|
+
eid: crypto.randomUUID()
|
|
751
|
+
},
|
|
752
|
+
entry: {
|
|
753
|
+
session: s
|
|
754
|
+
},
|
|
755
|
+
stop: {},
|
|
756
|
+
content: {
|
|
757
|
+
body: via ? 'Your Operator stopped this work because the request changed.' : link && s == session ? `The person (${person}) stopped helper ${session}, ${leave}` : 'Stopped by the person watching.'
|
|
758
|
+
},
|
|
759
|
+
$actor: {
|
|
760
|
+
by,
|
|
761
|
+
via: via ?? s
|
|
762
|
+
}
|
|
763
|
+
}))
|
|
764
|
+
], {
|
|
765
|
+
trusted: true
|
|
766
|
+
});
|
|
767
|
+
return true;
|
|
768
|
+
};
|
|
769
|
+
/** Stop a fork under the same conversation when its request changes. */ export let stopFork = (g, by)=>({
|
|
770
|
+
name: 'session_stop',
|
|
771
|
+
description: 'Stop an earlier fork of this conversation when the person changes what the work is for.',
|
|
772
|
+
parameters: {
|
|
773
|
+
type: 'object',
|
|
774
|
+
properties: {
|
|
775
|
+
session: {
|
|
776
|
+
type: 'string',
|
|
777
|
+
description: 'The earlier fork’s session id.'
|
|
778
|
+
}
|
|
779
|
+
},
|
|
780
|
+
required: [
|
|
781
|
+
'session'
|
|
782
|
+
]
|
|
783
|
+
},
|
|
784
|
+
run: async ({ session }, context)=>{
|
|
785
|
+
let ctx = caller(context);
|
|
786
|
+
let id = String(session);
|
|
787
|
+
let [target] = await g.get([
|
|
788
|
+
id
|
|
789
|
+
], [
|
|
790
|
+
'spawned',
|
|
791
|
+
'fork'
|
|
792
|
+
]);
|
|
793
|
+
if (!target?.fork || id == ctx.session || comp(target, 'spawned')?.parent != await topOf(g, ctx.session)) {
|
|
794
|
+
throw new ToolError('session', 'Name an earlier fork of this conversation.');
|
|
795
|
+
}
|
|
796
|
+
return JSON.stringify({
|
|
797
|
+
session: id,
|
|
798
|
+
stopped: await stopAll(g, id, by, ctx.session)
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
});
|
|
802
|
+
// Whether any of a session's helpers, at any depth, has yet to end and be
|
|
803
|
+
// told. A child's dispatch settles after its report is written, or held for
|
|
804
|
+
// helpers of its own (./run.ts), so a settled child still works while one of
|
|
805
|
+
// its own does.
|
|
806
|
+
export let workingChildren = async (g, session, repliesOnly = false)=>{
|
|
807
|
+
for (let c of (await children(g, session))){
|
|
808
|
+
if (repliesOnly && comp(c, 'spawned')?.notice) continue;
|
|
809
|
+
let place = dispatchStatus(c);
|
|
810
|
+
if (place != null && place != 'settled') return true;
|
|
811
|
+
let status = await entryStatus(g, c.entity.eid);
|
|
812
|
+
if (![
|
|
813
|
+
'settled',
|
|
814
|
+
'failed',
|
|
815
|
+
'stopped'
|
|
816
|
+
].includes(status)) return true;
|
|
817
|
+
if (status == 'settled' && await workingChildren(g, c.entity.eid)) {
|
|
818
|
+
return true;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
return false;
|
|
822
|
+
};
|
|
823
|
+
/** Build one idempotent completion receipt, while holding the parent's queue.
|
|
824
|
+
* The final-entry-derived id also allows a child to be sent another turn. */ export let deliverChild = async (g, child, now = Date.now())=>{
|
|
825
|
+
let link = comp(await row(g, child), 'spawned');
|
|
826
|
+
if (!link?.parent || !await row(g, String(link.parent))) return;
|
|
827
|
+
if (link.notice && link.report == null) {
|
|
828
|
+
return childNotice(g, child, link, now, ()=>workingChildren(g, child));
|
|
829
|
+
}
|
|
830
|
+
// A settled child's receipt usually already exists. Inspect its local tail
|
|
831
|
+
// before materializing the inherited prefix (which can be very large).
|
|
832
|
+
let [tail] = await g.read(line`.entry.session=${child}&.order=-entry.seq&.limit=1&*`);
|
|
833
|
+
// The task it was given, which lets go of the child's claim once done.
|
|
834
|
+
let tasks = g.vocab.comps.includes('task') ? await g.get(await workedOn(g, child)) : [];
|
|
835
|
+
let task = tasks.find((b)=>b?.task);
|
|
836
|
+
let ready = task && await done(g, task.entity.eid);
|
|
837
|
+
let last = tail ?? await newestOf(g, child);
|
|
838
|
+
if (!last) return;
|
|
839
|
+
// A parent's own completion is already known to it: the completion came
|
|
840
|
+
// through it (`via`; `by` is the actor it works for). Fall back to the
|
|
841
|
+
// ordinary child receipt identity: a new child response must still arrive,
|
|
842
|
+
// while an already delivered response must not echo on completion/restart.
|
|
843
|
+
let announceTask = ready && (taskStatus(g.vocab, task) != 'done' || comp(task, 'completed')?.via != String(link.parent));
|
|
844
|
+
let receipt = (last)=>derivedEid(announceTask ? `delivery:${child}:task:${task.entity.eid}:${taskStatus(g.vocab, task)}` : `delivery:${child}:${last.entity.eid}`);
|
|
845
|
+
let eid = receipt(last);
|
|
846
|
+
if (await row(g, eid)) return;
|
|
847
|
+
let status = await entryStatus(g, child);
|
|
848
|
+
// A new receipt still requires the full transcript's terminal outcome.
|
|
849
|
+
if (![
|
|
850
|
+
'settled',
|
|
851
|
+
'failed',
|
|
852
|
+
'stopped'
|
|
853
|
+
].includes(status)) return;
|
|
854
|
+
// A child whose own helpers still work has not answered yet: their reports
|
|
855
|
+
// will wake it, and one call reports once, after they end.
|
|
856
|
+
if (status == 'settled' && await workingChildren(g, child)) return;
|
|
857
|
+
// The model may have finished while the fast-path reads were awaiting.
|
|
858
|
+
// Use the same terminal snapshot for the receipt identity and its text.
|
|
859
|
+
last = await newestOf(g, child);
|
|
860
|
+
eid = receipt(last);
|
|
861
|
+
if (await row(g, eid)) return;
|
|
862
|
+
let message = announceTask ? `task ${calledIn(g, task)} ${taskStatus(g.vocab, task)}` : `child ${child} ${status}`;
|
|
863
|
+
let notice = await row(g, derivedEid('notice:' + child));
|
|
864
|
+
let context = notice?.notice ? textOf(notice) + '\nOutcome: child ' + status + (ready ? '; task ' + taskStatus(g.vocab, task) : '') + '\nResult:\n' : '';
|
|
865
|
+
let parent = String(link.parent);
|
|
866
|
+
// A stop is an explicit end, not a request to wake on the next delivery.
|
|
867
|
+
if (await entryStatus(g, parent) == 'stopped') return;
|
|
868
|
+
let open = (await openOf(g, parent)).some((b)=>b.entity.eid == link.call);
|
|
869
|
+
let record = link.report == 'record' && status == 'settled' && (!task || ready) && !open && !!textOf(last);
|
|
870
|
+
// The parent's `using` in force rides on the receipt: it is the request for
|
|
871
|
+
// the turn the receipt calls for (./run.ts).
|
|
872
|
+
let using = await usingOf(g, parent);
|
|
873
|
+
try {
|
|
874
|
+
await g.apply([
|
|
875
|
+
{
|
|
876
|
+
entity: {
|
|
877
|
+
eid
|
|
878
|
+
},
|
|
879
|
+
$was: {
|
|
880
|
+
entry: {
|
|
881
|
+
session: token(null)
|
|
882
|
+
}
|
|
883
|
+
},
|
|
884
|
+
entry: {
|
|
885
|
+
session: parent
|
|
886
|
+
},
|
|
887
|
+
content: {
|
|
888
|
+
body: message + '\n' + context + textOf(last)
|
|
889
|
+
},
|
|
890
|
+
delivery: {
|
|
891
|
+
child,
|
|
892
|
+
outcome: status,
|
|
893
|
+
...link.report ? {
|
|
894
|
+
through: last.entity.eid
|
|
895
|
+
} : {}
|
|
896
|
+
},
|
|
897
|
+
...record ? {
|
|
898
|
+
notice: {
|
|
899
|
+
child
|
|
900
|
+
}
|
|
901
|
+
} : {},
|
|
902
|
+
...open ? {
|
|
903
|
+
result: {
|
|
904
|
+
call: link.call
|
|
905
|
+
}
|
|
906
|
+
} : {},
|
|
907
|
+
...using && !record ? {
|
|
908
|
+
using
|
|
909
|
+
} : {},
|
|
910
|
+
...await overheard(g, child) ? {
|
|
911
|
+
heard: {}
|
|
912
|
+
} : {}
|
|
913
|
+
},
|
|
914
|
+
...link.notice ? [
|
|
915
|
+
{
|
|
916
|
+
entity: {
|
|
917
|
+
eid: child
|
|
918
|
+
},
|
|
919
|
+
completed: {}
|
|
920
|
+
}
|
|
921
|
+
] : [],
|
|
922
|
+
...link.notice && !record ? [
|
|
923
|
+
{
|
|
924
|
+
entity: {
|
|
925
|
+
eid: parent
|
|
926
|
+
},
|
|
927
|
+
completed: null
|
|
928
|
+
}
|
|
929
|
+
] : []
|
|
930
|
+
], {
|
|
931
|
+
trusted: true
|
|
932
|
+
});
|
|
933
|
+
} catch (error) {
|
|
934
|
+
if (!(error instanceof Stale) || error.eid != eid) throw error;
|
|
935
|
+
}
|
|
936
|
+
};
|
|
937
|
+
//# sourceMappingURL=children.js.map
|