@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/who.js
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
// The transcript a caller names, and the actor it writes as. One resolution
|
|
2
|
+
// for both, because the CLI and the HTTP server are asking the same question.
|
|
3
|
+
//
|
|
4
|
+
// `--session` used to mean two things: an eid or a human-readable id to
|
|
5
|
+
// `claim take`, and the harness's own id for the run to
|
|
6
|
+
// `session brief|wrap|context`. So `claim take --session S-37703` locked
|
|
7
|
+
// something for the session a person could see, and `session wrap S-37703`
|
|
8
|
+
// returned `[]` and released nothing — the same id, two different entities. It
|
|
9
|
+
// means the SESSION entity now, resolved the way any id is resolved here:
|
|
10
|
+
// addressed first (an eid, `S-37703`, a name the graph resolves), and where
|
|
11
|
+
// nothing matches, read as the harness's own id for a transcript — which is the
|
|
12
|
+
// only one of the three that may not exist yet, and the one
|
|
13
|
+
// `session context --hook -` mints.
|
|
14
|
+
//
|
|
15
|
+
// The same sequence resolves an HTTP request. A request names which run it
|
|
16
|
+
// speaks for (in `x-via`, the fleet's own header for the instrument behind a
|
|
17
|
+
// write), and what it writes is attributed `by` the actor that run works as
|
|
18
|
+
// and `via` the run itself. An actor is one of a handful, and never a
|
|
19
|
+
// session: there are thousands of sessions, and the owner's word is that
|
|
20
|
+
// sessions never go in `by` (M-31958). The actor is the session's persona; the
|
|
21
|
+
// owner, verbatim: "the persona should always be the actor. it used to be the
|
|
22
|
+
// project. i was saying i think actually the persona is the right one", and
|
|
23
|
+
// "i guess you could have fallbacks still, but it should be rare to have
|
|
24
|
+
// agents with no persona". So a run works as its persona, else the actor it
|
|
25
|
+
// names (`session.actor`), else the project it is filed in, else whatever the
|
|
26
|
+
// session that spawned it works as (`actorOf`).
|
|
27
|
+
//
|
|
28
|
+
// A session created from the harness's own id takes its eid from that id
|
|
29
|
+
// (`sessionEid`): the id itself where it is a uuid, as Claude Code's and
|
|
30
|
+
// Codex's are, since an eid is a uuid; one derived from it otherwise. So
|
|
31
|
+
// whoever holds the harness's id holds the eid, and the hook and the importer
|
|
32
|
+
// that both create a session for one id land on one entity. A subagent's id
|
|
33
|
+
// (Claude Code's agent id, its transcript `agent-<id>.jsonl`) names it within
|
|
34
|
+
// its parent, so its eid derives from the two (`subagentSession`).
|
|
35
|
+
import { after } from "@yaks/fp";
|
|
36
|
+
import { and, eq, fields, limit, line, list, or, present, sub } from "@yaks/query";
|
|
37
|
+
import { writers } from "@yaks/graph";
|
|
38
|
+
import { hook } from "@yaks/graph/plugin";
|
|
39
|
+
import { addressed } from "@yaks/graph/call";
|
|
40
|
+
import { identityEid } from "@yaks/graph/identity";
|
|
41
|
+
import { TOMBSTONE } from "@yaks/graph/bundle";
|
|
42
|
+
import { SESSION } from "./comp.js";
|
|
43
|
+
// The properties a read projects, as its `.fields` clause.
|
|
44
|
+
let projected = (names)=>names.length ? line`.fields=${names}` : line`.fields=`;
|
|
45
|
+
/** Whether a request came from a person or their command, rather than an
|
|
46
|
+
* agent or automation. An agent may speak for an identity, so both the author
|
|
47
|
+
* and the instrument matter. A transcript's own stamp is not its parent. */ export let humanCaller = async (g, actor, self)=>{
|
|
48
|
+
let ids = [
|
|
49
|
+
...new Set([
|
|
50
|
+
actor?.by,
|
|
51
|
+
actor?.via
|
|
52
|
+
].filter((id)=>!!id && id != self))
|
|
53
|
+
];
|
|
54
|
+
let kinds = [
|
|
55
|
+
'session',
|
|
56
|
+
'effect',
|
|
57
|
+
'builder'
|
|
58
|
+
].filter((name)=>g.vocab.comp(name));
|
|
59
|
+
if (!ids.length || !kinds.length) return true;
|
|
60
|
+
let callers = await g.read(and(eq('entity.eid', list(...ids)), or(...kinds.map((name)=>present(name))), fields('entity.eid'), limit(1)));
|
|
61
|
+
return !callers.length;
|
|
62
|
+
};
|
|
63
|
+
// A uuid, whatever its version.
|
|
64
|
+
let UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
65
|
+
/**
|
|
66
|
+
* The eid of the session a harness knows by `id`: the id itself where it is a
|
|
67
|
+
* uuid, and otherwise one derived from it. A subagent's, whose id names it
|
|
68
|
+
* within its `parent`, derives from both.
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { sessionEid } from '@yaks/session'
|
|
72
|
+
* import { assertEquals } from '@std/assert'
|
|
73
|
+
*
|
|
74
|
+
* let id = '49805559-ca98-4c0a-873e-45c19ec7316c'
|
|
75
|
+
* assertEquals(sessionEid(id), id)
|
|
76
|
+
* ```
|
|
77
|
+
*/ export let sessionEid = (id, parent)=>parent ? identityEid(SESSION, [
|
|
78
|
+
parent,
|
|
79
|
+
id
|
|
80
|
+
]) : UUID.test(id) ? id : identityEid(SESSION, [
|
|
81
|
+
id
|
|
82
|
+
]);
|
|
83
|
+
/** A command speaks for its own transcript, never its launcher's. Clearing
|
|
84
|
+
* the other harness IDs also matters when `yak` chooses the first one it sees. */ export let sessionEnv = (session, env)=>{
|
|
85
|
+
let { CLAUDE_CODE_SESSION_ID: _claude, CODEX_THREAD_ID: _codex, TASKS_SESSION: _tasks, ...rest } = env;
|
|
86
|
+
return {
|
|
87
|
+
...rest,
|
|
88
|
+
TASKS_SESSION: session
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* The transcripts these ids are the runner's own ids of (`session.id`), each
|
|
93
|
+
* to its eid: the key only a session has, which is how an id meant to name a
|
|
94
|
+
* session resolves where no plugin knows it as an eid or a name (the
|
|
95
|
+
* plugin's `address`, asked with the kind `session`).
|
|
96
|
+
*/ export let runners = (tx, ids)=>ids.reduce((at, id)=>after(at, (found)=>after(tx.read(line`.${SESSION}.id=${id} .fields=entity.eid`), ([b])=>b ? new Map([
|
|
97
|
+
...found,
|
|
98
|
+
[
|
|
99
|
+
id,
|
|
100
|
+
b.entity.eid
|
|
101
|
+
]
|
|
102
|
+
]) : found)), new Map());
|
|
103
|
+
/**
|
|
104
|
+
* The transcript an id names: the entity it addresses, else the one carrying it
|
|
105
|
+
* as its runner's own id. Nothing is minted here — resolution only reads, and a
|
|
106
|
+
* tool that wants a transcript created does that itself. `{ id }` selects only
|
|
107
|
+
* the runner's own session id, without trying entity addresses. `fields` selects only
|
|
108
|
+
* the properties the caller reads, avoiding derived transcript history.
|
|
109
|
+
*/ export let sessionFor = async (g, said, fields)=>{
|
|
110
|
+
if (typeof said != 'string') {
|
|
111
|
+
if (!said.id) return undefined;
|
|
112
|
+
return (await g.read(line`.${SESSION}.id=${said.id} ${sub(fields ? projected(fields) : line`*`)}`))[0];
|
|
113
|
+
}
|
|
114
|
+
if (!said) return undefined;
|
|
115
|
+
let [eid] = await addressed(g, [
|
|
116
|
+
said
|
|
117
|
+
]);
|
|
118
|
+
let get = async (id)=>(fields ? await g.read(line`.entity.eid=${id} .${SESSION} ${sub(projected(fields))}`) : await g.get([
|
|
119
|
+
id
|
|
120
|
+
]))[0];
|
|
121
|
+
let row = await get(eid);
|
|
122
|
+
// A projected query already required session presence; its selected
|
|
123
|
+
// properties may all be absent on a native transcript.
|
|
124
|
+
if (row && (fields || row[SESSION]) && row[TOMBSTONE] == null) return row;
|
|
125
|
+
let run = (await runners(g, [
|
|
126
|
+
said
|
|
127
|
+
])).get(said);
|
|
128
|
+
return run ? await get(run) : undefined;
|
|
129
|
+
};
|
|
130
|
+
// How far `actorOf` follows a chain of spawning sessions before it stops: a
|
|
131
|
+
// chain longer than this is a cycle someone wrote, not a family.
|
|
132
|
+
let DEPTH = 8;
|
|
133
|
+
let str = (v)=>typeof v == 'string' ? v : '';
|
|
134
|
+
/**
|
|
135
|
+
* The actor a session works as: its persona, else the actor it names
|
|
136
|
+
* (`session.actor`), else the project it is filed in (`filed.project`), else
|
|
137
|
+
* whatever the session that spawned it works as, so a subagent works as the
|
|
138
|
+
* persona it runs under. Never a session; `undefined` where nothing names one.
|
|
139
|
+
* A session with no persona anywhere up its line should be rare, and a door
|
|
140
|
+
* that starts one is worth fixing rather than falling back for. Each session
|
|
141
|
+
* is read for those properties alone: the rest of `session` (its computed
|
|
142
|
+
* `status` above all) costs a command its compile and answers nothing here.
|
|
143
|
+
*/ export let actorOf = async (g, session)=>{
|
|
144
|
+
let fields = [
|
|
145
|
+
`${SESSION}.persona`,
|
|
146
|
+
`${SESSION}.actor`,
|
|
147
|
+
'filed.project',
|
|
148
|
+
'spawned.parent'
|
|
149
|
+
].filter((f)=>g.vocab.prop(...f.split('.')));
|
|
150
|
+
let seen = new Set();
|
|
151
|
+
for(let at = session; at && !seen.has(at) && seen.size < DEPTH;){
|
|
152
|
+
seen.add(at);
|
|
153
|
+
let [s] = await g.read(line`.entity.eid=${at} .${SESSION} ${sub(projected(fields))}`);
|
|
154
|
+
if (!s) return;
|
|
155
|
+
let named = s[SESSION];
|
|
156
|
+
let project = str(s.filed?.project);
|
|
157
|
+
let by = str(named?.persona) || str(named?.actor) || project;
|
|
158
|
+
if (by) return by;
|
|
159
|
+
at = str(s.spawned?.parent);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* The actor a transcript writes as: `by` the actor it works as
|
|
164
|
+
* ({@link actorOf}), the identity that survives a `/clear`, and `via` the run
|
|
165
|
+
* itself. A session that works as nobody leaves `by` unsaid rather than
|
|
166
|
+
* standing in it.
|
|
167
|
+
*/ export let speaking = async (g, s)=>{
|
|
168
|
+
let via = s.entity.eid;
|
|
169
|
+
let by = await actorOf(g, via);
|
|
170
|
+
return by ? {
|
|
171
|
+
by,
|
|
172
|
+
via
|
|
173
|
+
} : {
|
|
174
|
+
via
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* The session standing in this graph for a conversation held in another
|
|
179
|
+
* store and reached through `door` (a tunnel), written where it is new, and
|
|
180
|
+
* the actor a request from it writes as: `by` the `persona` such a caller
|
|
181
|
+
* works as here, `via` that session. Its eid derives from the door and the
|
|
182
|
+
* conversation's id, as a subagent's does from its parent and its own, so
|
|
183
|
+
* every request from one conversation speaks through one session and no id a
|
|
184
|
+
* request sends names one of this graph's own runs. Where the id is an eid,
|
|
185
|
+
* `session.source` keeps it: the conversation is an entity, in its own store.
|
|
186
|
+
* A request naming no conversation speaks through the door's one session.
|
|
187
|
+
* `session.door` says the session is the door's, which is what holds a write
|
|
188
|
+
* through it to its own writer ({@link attributing}). Where the door says
|
|
189
|
+
* whose conversation it is, and that is a person here, `session.person`
|
|
190
|
+
* keeps them: what is news to them is told to it (./watch.ts).
|
|
191
|
+
*/ export let standIn = async (g, door, conversation = '', persona, person)=>{
|
|
192
|
+
let via = sessionEid(conversation, door);
|
|
193
|
+
let [had] = await g.get([
|
|
194
|
+
via
|
|
195
|
+
], [
|
|
196
|
+
SESSION
|
|
197
|
+
]);
|
|
198
|
+
let named = had?.[TOMBSTONE] == null ? had?.[SESSION] : undefined;
|
|
199
|
+
if (!named || named.door != door || persona && named.persona != persona || person && named.person != person) {
|
|
200
|
+
await g.apply([
|
|
201
|
+
{
|
|
202
|
+
entity: {
|
|
203
|
+
eid: via
|
|
204
|
+
},
|
|
205
|
+
[SESSION]: {
|
|
206
|
+
door,
|
|
207
|
+
...persona ? {
|
|
208
|
+
persona
|
|
209
|
+
} : {},
|
|
210
|
+
...person ? {
|
|
211
|
+
person
|
|
212
|
+
} : {},
|
|
213
|
+
...UUID.test(conversation) ? {
|
|
214
|
+
source: conversation
|
|
215
|
+
} : {}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
], {
|
|
219
|
+
trusted: true
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return persona ? {
|
|
223
|
+
by: persona,
|
|
224
|
+
via
|
|
225
|
+
} : {
|
|
226
|
+
via
|
|
227
|
+
};
|
|
228
|
+
};
|
|
229
|
+
/**
|
|
230
|
+
* Who a write through a session standing at a door is by is the door's word,
|
|
231
|
+
* and nothing the write says: a request naming somebody else as the one a mark
|
|
232
|
+
* is by (`$decider`, @yaks/kernel) is dropped from it, so the mark is its
|
|
233
|
+
* writer's. A hosted assistant answering a decision through a tunnel answers
|
|
234
|
+
* as itself, whoever it names; the person's own answer arrives as the person
|
|
235
|
+
* (@yaks/tunnel `x-yak-by`), through no session.
|
|
236
|
+
*/ export let attributing = hook([
|
|
237
|
+
'$decider'
|
|
238
|
+
], (bundles, tx)=>{
|
|
239
|
+
let writer = writers(bundles);
|
|
240
|
+
let through = (b)=>b.$decider == null ? undefined : writer(b.entity.eid).via;
|
|
241
|
+
let vias = [
|
|
242
|
+
...new Set(bundles.flatMap((b)=>through(b) ?? []))
|
|
243
|
+
];
|
|
244
|
+
if (!vias.length) return bundles;
|
|
245
|
+
return after(tx.get(vias, [
|
|
246
|
+
SESSION
|
|
247
|
+
]), (rows)=>{
|
|
248
|
+
let doors = new Set(rows.filter((s)=>s[SESSION]?.door).map((s)=>s.entity.eid));
|
|
249
|
+
return bundles.map((b)=>{
|
|
250
|
+
let via = through(b);
|
|
251
|
+
if (!via || !doors.has(via)) return b;
|
|
252
|
+
let { $decider: _, ...own } = b;
|
|
253
|
+
return own;
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
/**
|
|
258
|
+
* The session of a subagent its harness knows by `id` within the session it
|
|
259
|
+
* knows by `parent`: its eid, and the rows that write it where the graph has
|
|
260
|
+
* not met it yet (with the parent, where that is new too), else none.
|
|
261
|
+
*/ export let spawning = async (g, parent, id)=>{
|
|
262
|
+
let eid = sessionEid(id, parent);
|
|
263
|
+
let [s] = await g.get([
|
|
264
|
+
eid
|
|
265
|
+
], [
|
|
266
|
+
SESSION
|
|
267
|
+
]);
|
|
268
|
+
if (s?.[SESSION] && s[TOMBSTONE] == null) return {
|
|
269
|
+
eid,
|
|
270
|
+
rows: []
|
|
271
|
+
};
|
|
272
|
+
let up = (await sessionFor(g, parent, [
|
|
273
|
+
'session.id'
|
|
274
|
+
]))?.entity.eid;
|
|
275
|
+
return {
|
|
276
|
+
eid,
|
|
277
|
+
rows: [
|
|
278
|
+
...up ? [] : [
|
|
279
|
+
{
|
|
280
|
+
entity: {
|
|
281
|
+
eid: '$parent'
|
|
282
|
+
},
|
|
283
|
+
[SESSION]: {
|
|
284
|
+
id: parent
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
],
|
|
288
|
+
{
|
|
289
|
+
entity: {
|
|
290
|
+
eid
|
|
291
|
+
},
|
|
292
|
+
[SESSION]: {
|
|
293
|
+
id
|
|
294
|
+
},
|
|
295
|
+
spawned: {
|
|
296
|
+
parent: up ?? '$parent'
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
]
|
|
300
|
+
};
|
|
301
|
+
};
|
|
302
|
+
/** The session of a subagent ({@link spawning}), written where it is new.
|
|
303
|
+
* Answers its eid, or nothing where either id is empty. */ export let subagentSession = async (g, parent, id)=>{
|
|
304
|
+
if (!parent || !id) return;
|
|
305
|
+
let { eid, rows } = await spawning(g, parent, id);
|
|
306
|
+
if (rows.length) await g.apply(rows);
|
|
307
|
+
return eid;
|
|
308
|
+
};
|
|
309
|
+
//# sourceMappingURL=who.js.map
|
package/who.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"who.js","sources":["./who.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,8EAA8E;AAC9E,EAAE;AACF,wEAAwE;AACxE,wDAAwD;AACxD,yEAAyE;AACzE,2EAA2E;AAC3E,+E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QAAG;QAChB,SAAS;UAAE,QAAQ,MAAM;QAAU;MACrC;KACD;EACH;AACF,EAAC;AAED;yDACyD,GACzD,OAAO,IAAI,kBAAkB,OAC3B,GACA,QACA;EAEA,IAAI,CAAC,UAAU,CAAC,IAAI;EACpB,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,MAAM,SAAS,GAAG,QAAQ;EAC9C,IAAI,KAAK,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC;EAC/B,OAAO;AACT,EAAC"}
|
package/who.ts
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
// The transcript a caller names, and the actor it writes as. One resolution
|
|
2
|
+
// for both, because the CLI and the HTTP server are asking the same question.
|
|
3
|
+
//
|
|
4
|
+
// `--session` used to mean two things: an eid or a human-readable id to
|
|
5
|
+
// `claim take`, and the harness's own id for the run to
|
|
6
|
+
// `session brief|wrap|context`. So `claim take --session S-37703` locked
|
|
7
|
+
// something for the session a person could see, and `session wrap S-37703`
|
|
8
|
+
// returned `[]` and released nothing — the same id, two different entities. It
|
|
9
|
+
// means the SESSION entity now, resolved the way any id is resolved here:
|
|
10
|
+
// addressed first (an eid, `S-37703`, a name the graph resolves), and where
|
|
11
|
+
// nothing matches, read as the harness's own id for a transcript — which is the
|
|
12
|
+
// only one of the three that may not exist yet, and the one
|
|
13
|
+
// `session context --hook -` mints.
|
|
14
|
+
//
|
|
15
|
+
// The same sequence resolves an HTTP request. A request names which run it
|
|
16
|
+
// speaks for (in `x-via`, the fleet's own header for the instrument behind a
|
|
17
|
+
// write), and what it writes is attributed `by` the actor that run works as
|
|
18
|
+
// and `via` the run itself. An actor is one of a handful, and never a
|
|
19
|
+
// session: there are thousands of sessions, and the owner's word is that
|
|
20
|
+
// sessions never go in `by` (M-31958). The actor is the session's persona; the
|
|
21
|
+
// owner, verbatim: "the persona should always be the actor. it used to be the
|
|
22
|
+
// project. i was saying i think actually the persona is the right one", and
|
|
23
|
+
// "i guess you could have fallbacks still, but it should be rare to have
|
|
24
|
+
// agents with no persona". So a run works as its persona, else the actor it
|
|
25
|
+
// names (`session.actor`), else the project it is filed in, else whatever the
|
|
26
|
+
// session that spawned it works as (`actorOf`).
|
|
27
|
+
//
|
|
28
|
+
// A session created from the harness's own id takes its eid from that id
|
|
29
|
+
// (`sessionEid`): the id itself where it is a uuid, as Claude Code's and
|
|
30
|
+
// Codex's are, since an eid is a uuid; one derived from it otherwise. So
|
|
31
|
+
// whoever holds the harness's id holds the eid, and the hook and the importer
|
|
32
|
+
// that both create a session for one id land on one entity. A subagent's id
|
|
33
|
+
// (Claude Code's agent id, its transcript `agent-<id>.jsonl`) names it within
|
|
34
|
+
// its parent, so its eid derives from the two (`subagentSession`).
|
|
35
|
+
|
|
36
|
+
import { after } from '@yaks/fp'
|
|
37
|
+
import {
|
|
38
|
+
and,
|
|
39
|
+
type Built,
|
|
40
|
+
eq,
|
|
41
|
+
fields,
|
|
42
|
+
limit,
|
|
43
|
+
line,
|
|
44
|
+
list,
|
|
45
|
+
or,
|
|
46
|
+
present,
|
|
47
|
+
sub,
|
|
48
|
+
} from '@yaks/query'
|
|
49
|
+
import type { Actor, Bundle, Comp, Eid, Graph, Hook, Tx } from '@yaks/graph'
|
|
50
|
+
import { writers } from '@yaks/graph'
|
|
51
|
+
import { hook } from '@yaks/graph/plugin'
|
|
52
|
+
import { addressed } from '@yaks/graph/call'
|
|
53
|
+
import { identityEid } from '@yaks/graph/identity'
|
|
54
|
+
import { TOMBSTONE } from '@yaks/graph/bundle'
|
|
55
|
+
import { SESSION } from './comp.js'
|
|
56
|
+
|
|
57
|
+
// The properties a read projects, as its `.fields` clause.
|
|
58
|
+
let projected = (names: string[]): Built =>
|
|
59
|
+
names.length ? line`.fields=${names}` : line`.fields=`
|
|
60
|
+
|
|
61
|
+
/** Whether a request came from a person or their command, rather than an
|
|
62
|
+
* agent or automation. An agent may speak for an identity, so both the author
|
|
63
|
+
* and the instrument matter. A transcript's own stamp is not its parent. */
|
|
64
|
+
export let humanCaller = async (
|
|
65
|
+
g: Pick<Graph, 'read' | 'vocab'>,
|
|
66
|
+
actor: Actor | null,
|
|
67
|
+
self?: Eid,
|
|
68
|
+
): Promise<boolean> => {
|
|
69
|
+
let ids = [
|
|
70
|
+
...new Set(
|
|
71
|
+
[actor?.by, actor?.via].filter((id): id is Eid => !!id && id != self),
|
|
72
|
+
),
|
|
73
|
+
]
|
|
74
|
+
let kinds = ['session', 'effect', 'builder'].filter((name) =>
|
|
75
|
+
g.vocab.comp(name)
|
|
76
|
+
)
|
|
77
|
+
if (!ids.length || !kinds.length) return true
|
|
78
|
+
let callers = await g.read(and(
|
|
79
|
+
eq('entity.eid', list(...ids)),
|
|
80
|
+
or(...kinds.map((name) => present(name))),
|
|
81
|
+
fields('entity.eid'),
|
|
82
|
+
limit(1),
|
|
83
|
+
))
|
|
84
|
+
return !callers.length
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// A uuid, whatever its version.
|
|
88
|
+
let UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The eid of the session a harness knows by `id`: the id itself where it is a
|
|
92
|
+
* uuid, and otherwise one derived from it. A subagent's, whose id names it
|
|
93
|
+
* within its `parent`, derives from both.
|
|
94
|
+
*
|
|
95
|
+
* ```ts
|
|
96
|
+
* import { sessionEid } from '@yaks/session'
|
|
97
|
+
* import { assertEquals } from '@std/assert'
|
|
98
|
+
*
|
|
99
|
+
* let id = '49805559-ca98-4c0a-873e-45c19ec7316c'
|
|
100
|
+
* assertEquals(sessionEid(id), id)
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export let sessionEid = (id: string, parent?: string): Eid =>
|
|
104
|
+
parent
|
|
105
|
+
? identityEid(SESSION, [parent, id])
|
|
106
|
+
: UUID.test(id)
|
|
107
|
+
? id
|
|
108
|
+
: identityEid(SESSION, [id])
|
|
109
|
+
|
|
110
|
+
/** A command speaks for its own transcript, never its launcher's. Clearing
|
|
111
|
+
* the other harness IDs also matters when `yak` chooses the first one it sees. */
|
|
112
|
+
export let sessionEnv = (
|
|
113
|
+
session: string,
|
|
114
|
+
env: Record<string, string>,
|
|
115
|
+
): Record<string, string> => {
|
|
116
|
+
let {
|
|
117
|
+
CLAUDE_CODE_SESSION_ID: _claude,
|
|
118
|
+
CODEX_THREAD_ID: _codex,
|
|
119
|
+
TASKS_SESSION: _tasks,
|
|
120
|
+
...rest
|
|
121
|
+
} = env
|
|
122
|
+
return { ...rest, TASKS_SESSION: session }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The transcripts these ids are the runner's own ids of (`session.id`), each
|
|
127
|
+
* to its eid: the key only a session has, which is how an id meant to name a
|
|
128
|
+
* session resolves where no plugin knows it as an eid or a name (the
|
|
129
|
+
* plugin's `address`, asked with the kind `session`).
|
|
130
|
+
*/
|
|
131
|
+
export let runners = (
|
|
132
|
+
tx: Pick<Tx, 'read'>,
|
|
133
|
+
ids: string[],
|
|
134
|
+
): Map<string, Eid> | Promise<Map<string, Eid>> =>
|
|
135
|
+
ids.reduce<Map<string, Eid> | Promise<Map<string, Eid>>>(
|
|
136
|
+
(at, id) =>
|
|
137
|
+
after(at, (found) =>
|
|
138
|
+
after(
|
|
139
|
+
tx.read(line`.${SESSION}.id=${id} .fields=entity.eid`),
|
|
140
|
+
([b]) => b ? new Map([...found, [id, b.entity.eid]]) : found,
|
|
141
|
+
)),
|
|
142
|
+
new Map(),
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The transcript an id names: the entity it addresses, else the one carrying it
|
|
147
|
+
* as its runner's own id. Nothing is minted here — resolution only reads, and a
|
|
148
|
+
* tool that wants a transcript created does that itself. `{ id }` selects only
|
|
149
|
+
* the runner's own session id, without trying entity addresses. `fields` selects only
|
|
150
|
+
* the properties the caller reads, avoiding derived transcript history.
|
|
151
|
+
*/
|
|
152
|
+
export let sessionFor = async (
|
|
153
|
+
g: Pick<Graph, 'address' | 'get' | 'read'>,
|
|
154
|
+
said: string | { id: string },
|
|
155
|
+
fields?: string[],
|
|
156
|
+
): Promise<Bundle | undefined> => {
|
|
157
|
+
if (typeof said != 'string') {
|
|
158
|
+
if (!said.id) return undefined
|
|
159
|
+
return (await g.read(
|
|
160
|
+
line`.${SESSION}.id=${said.id} ${
|
|
161
|
+
sub(fields ? projected(fields) : line`*`)
|
|
162
|
+
}`,
|
|
163
|
+
))[0]
|
|
164
|
+
}
|
|
165
|
+
if (!said) return undefined
|
|
166
|
+
let [eid] = await addressed(g, [said])
|
|
167
|
+
let get = async (id: Eid): Promise<Bundle | undefined> =>
|
|
168
|
+
(fields
|
|
169
|
+
? await g.read(
|
|
170
|
+
line`.entity.eid=${id} .${SESSION} ${sub(projected(fields))}`,
|
|
171
|
+
)
|
|
172
|
+
: await g.get([id]))[0]
|
|
173
|
+
let row = await get(eid)
|
|
174
|
+
// A projected query already required session presence; its selected
|
|
175
|
+
// properties may all be absent on a native transcript.
|
|
176
|
+
if (row && (fields || row[SESSION]) && row[TOMBSTONE] == null) return row
|
|
177
|
+
let run = (await runners(g, [said])).get(said)
|
|
178
|
+
return run ? await get(run) : undefined
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// How far `actorOf` follows a chain of spawning sessions before it stops: a
|
|
182
|
+
// chain longer than this is a cycle someone wrote, not a family.
|
|
183
|
+
let DEPTH = 8
|
|
184
|
+
|
|
185
|
+
let str = (v: unknown): string => typeof v == 'string' ? v : ''
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The actor a session works as: its persona, else the actor it names
|
|
189
|
+
* (`session.actor`), else the project it is filed in (`filed.project`), else
|
|
190
|
+
* whatever the session that spawned it works as, so a subagent works as the
|
|
191
|
+
* persona it runs under. Never a session; `undefined` where nothing names one.
|
|
192
|
+
* A session with no persona anywhere up its line should be rare, and a door
|
|
193
|
+
* that starts one is worth fixing rather than falling back for. Each session
|
|
194
|
+
* is read for those properties alone: the rest of `session` (its computed
|
|
195
|
+
* `status` above all) costs a command its compile and answers nothing here.
|
|
196
|
+
*/
|
|
197
|
+
export let actorOf = async (
|
|
198
|
+
g: Pick<Graph, 'read' | 'vocab'>,
|
|
199
|
+
session: Eid,
|
|
200
|
+
): Promise<Eid | undefined> => {
|
|
201
|
+
let fields = [
|
|
202
|
+
`${SESSION}.persona`,
|
|
203
|
+
`${SESSION}.actor`,
|
|
204
|
+
'filed.project',
|
|
205
|
+
'spawned.parent',
|
|
206
|
+
].filter((f) => g.vocab.prop(...f.split('.') as [string, string]))
|
|
207
|
+
let seen = new Set<Eid>()
|
|
208
|
+
for (let at: Eid = session; at && !seen.has(at) && seen.size < DEPTH;) {
|
|
209
|
+
seen.add(at)
|
|
210
|
+
let [s] = await g.read(
|
|
211
|
+
line`.entity.eid=${at} .${SESSION} ${sub(projected(fields))}`,
|
|
212
|
+
)
|
|
213
|
+
if (!s) return
|
|
214
|
+
let named = s[SESSION] as Comp | undefined
|
|
215
|
+
let project = str((s.filed as Comp | undefined)?.project)
|
|
216
|
+
let by = str(named?.persona) || str(named?.actor) || project
|
|
217
|
+
if (by) return by as Eid
|
|
218
|
+
at = str((s.spawned as Comp | undefined)?.parent) as Eid
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* The actor a transcript writes as: `by` the actor it works as
|
|
224
|
+
* ({@link actorOf}), the identity that survives a `/clear`, and `via` the run
|
|
225
|
+
* itself. A session that works as nobody leaves `by` unsaid rather than
|
|
226
|
+
* standing in it.
|
|
227
|
+
*/
|
|
228
|
+
export let speaking = async (
|
|
229
|
+
g: Pick<Graph, 'read' | 'vocab'>,
|
|
230
|
+
s: Bundle,
|
|
231
|
+
): Promise<Actor> => {
|
|
232
|
+
let via = s.entity.eid
|
|
233
|
+
let by = await actorOf(g, via)
|
|
234
|
+
return by ? { by, via } : { via }
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* The session standing in this graph for a conversation held in another
|
|
239
|
+
* store and reached through `door` (a tunnel), written where it is new, and
|
|
240
|
+
* the actor a request from it writes as: `by` the `persona` such a caller
|
|
241
|
+
* works as here, `via` that session. Its eid derives from the door and the
|
|
242
|
+
* conversation's id, as a subagent's does from its parent and its own, so
|
|
243
|
+
* every request from one conversation speaks through one session and no id a
|
|
244
|
+
* request sends names one of this graph's own runs. Where the id is an eid,
|
|
245
|
+
* `session.source` keeps it: the conversation is an entity, in its own store.
|
|
246
|
+
* A request naming no conversation speaks through the door's one session.
|
|
247
|
+
* `session.door` says the session is the door's, which is what holds a write
|
|
248
|
+
* through it to its own writer ({@link attributing}). Where the door says
|
|
249
|
+
* whose conversation it is, and that is a person here, `session.person`
|
|
250
|
+
* keeps them: what is news to them is told to it (./watch.ts).
|
|
251
|
+
*/
|
|
252
|
+
export let standIn = async (
|
|
253
|
+
g: Pick<Graph, 'get' | 'apply'>,
|
|
254
|
+
door: string,
|
|
255
|
+
conversation = '',
|
|
256
|
+
persona?: Eid,
|
|
257
|
+
person?: Eid,
|
|
258
|
+
): Promise<Actor> => {
|
|
259
|
+
let via = sessionEid(conversation, door)
|
|
260
|
+
let [had] = await g.get([via], [SESSION])
|
|
261
|
+
let named = had?.[TOMBSTONE] == null
|
|
262
|
+
? had?.[SESSION] as Comp | undefined
|
|
263
|
+
: undefined
|
|
264
|
+
if (
|
|
265
|
+
!named || named.door != door || (persona && named.persona != persona) ||
|
|
266
|
+
(person && named.person != person)
|
|
267
|
+
) {
|
|
268
|
+
await g.apply([{
|
|
269
|
+
entity: { eid: via },
|
|
270
|
+
[SESSION]: {
|
|
271
|
+
door,
|
|
272
|
+
...persona ? { persona } : {},
|
|
273
|
+
...person ? { person } : {},
|
|
274
|
+
...UUID.test(conversation) ? { source: conversation } : {},
|
|
275
|
+
},
|
|
276
|
+
}], { trusted: true })
|
|
277
|
+
}
|
|
278
|
+
return persona ? { by: persona, via } : { via }
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Who a write through a session standing at a door is by is the door's word,
|
|
283
|
+
* and nothing the write says: a request naming somebody else as the one a mark
|
|
284
|
+
* is by (`$decider`, @yaks/kernel) is dropped from it, so the mark is its
|
|
285
|
+
* writer's. A hosted assistant answering a decision through a tunnel answers
|
|
286
|
+
* as itself, whoever it names; the person's own answer arrives as the person
|
|
287
|
+
* (@yaks/tunnel `x-yak-by`), through no session.
|
|
288
|
+
*/
|
|
289
|
+
export let attributing: Hook = hook(['$decider'], (bundles, tx) => {
|
|
290
|
+
let writer = writers(bundles)
|
|
291
|
+
let through = (b: Bundle) =>
|
|
292
|
+
b.$decider == null ? undefined : writer(b.entity.eid).via
|
|
293
|
+
let vias = [...new Set(bundles.flatMap((b) => through(b) ?? []))]
|
|
294
|
+
if (!vias.length) return bundles
|
|
295
|
+
return after(tx.get(vias, [SESSION]), (rows) => {
|
|
296
|
+
let doors = new Set(
|
|
297
|
+
rows.filter((s) => (s[SESSION] as Comp | undefined)?.door)
|
|
298
|
+
.map((s) => s.entity.eid),
|
|
299
|
+
)
|
|
300
|
+
return bundles.map((b) => {
|
|
301
|
+
let via = through(b)
|
|
302
|
+
if (!via || !doors.has(via)) return b
|
|
303
|
+
let { $decider: _, ...own } = b
|
|
304
|
+
return own
|
|
305
|
+
})
|
|
306
|
+
})
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* The session of a subagent its harness knows by `id` within the session it
|
|
311
|
+
* knows by `parent`: its eid, and the rows that write it where the graph has
|
|
312
|
+
* not met it yet (with the parent, where that is new too), else none.
|
|
313
|
+
*/
|
|
314
|
+
export let spawning = async (
|
|
315
|
+
g: Pick<Graph, 'get' | 'read' | 'address'>,
|
|
316
|
+
parent: string,
|
|
317
|
+
id: string,
|
|
318
|
+
): Promise<{ eid: Eid; rows: Bundle[] }> => {
|
|
319
|
+
let eid = sessionEid(id, parent)
|
|
320
|
+
let [s] = await g.get([eid], [SESSION])
|
|
321
|
+
if (s?.[SESSION] && s[TOMBSTONE] == null) return { eid, rows: [] }
|
|
322
|
+
let up = (await sessionFor(g, parent, ['session.id']))?.entity.eid
|
|
323
|
+
return {
|
|
324
|
+
eid,
|
|
325
|
+
rows: [
|
|
326
|
+
...up ? [] : [{ entity: { eid: '$parent' }, [SESSION]: { id: parent } }],
|
|
327
|
+
{
|
|
328
|
+
entity: { eid },
|
|
329
|
+
[SESSION]: { id },
|
|
330
|
+
spawned: { parent: up ?? '$parent' },
|
|
331
|
+
},
|
|
332
|
+
],
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** The session of a subagent ({@link spawning}), written where it is new.
|
|
337
|
+
* Answers its eid, or nothing where either id is empty. */
|
|
338
|
+
export let subagentSession = async (
|
|
339
|
+
g: Pick<Graph, 'get' | 'read' | 'address' | 'apply'>,
|
|
340
|
+
parent: string,
|
|
341
|
+
id: string,
|
|
342
|
+
): Promise<Eid | undefined> => {
|
|
343
|
+
if (!parent || !id) return
|
|
344
|
+
let { eid, rows } = await spawning(g, parent, id)
|
|
345
|
+
if (rows.length) await g.apply(rows)
|
|
346
|
+
return eid
|
|
347
|
+
}
|