@yaks/cli 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 +1059 -2
- package/_dist/account-forward.d.ts +12 -0
- package/_dist/account-forward.d.ts.map +1 -0
- package/_dist/accounts.d.ts +23 -0
- package/_dist/accounts.d.ts.map +1 -0
- package/_dist/answer.d.ts +88 -0
- package/_dist/answer.d.ts.map +1 -0
- package/_dist/apply.d.ts +17 -0
- package/_dist/apply.d.ts.map +1 -0
- package/_dist/args.d.ts +86 -0
- package/_dist/args.d.ts.map +1 -0
- package/_dist/commands.d.ts +15 -0
- package/_dist/commands.d.ts.map +1 -0
- package/_dist/complete.d.ts +15 -0
- package/_dist/complete.d.ts.map +1 -0
- package/_dist/config.d.ts +37 -0
- package/_dist/config.d.ts.map +1 -0
- package/_dist/dependencies.d.ts +6 -0
- package/_dist/dependencies.d.ts.map +1 -0
- package/_dist/facets.d.ts +71 -0
- package/_dist/facets.d.ts.map +1 -0
- package/_dist/grammar.d.ts +5 -0
- package/_dist/grammar.d.ts.map +1 -0
- package/_dist/host.d.ts +164 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/install.d.ts +43 -0
- package/_dist/install.d.ts.map +1 -0
- package/_dist/main.d.ts +16 -0
- package/_dist/main.d.ts.map +1 -0
- package/_dist/memo.d.ts +60 -0
- package/_dist/memo.d.ts.map +1 -0
- package/_dist/mod.d.ts +46 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/page.d.ts +73 -0
- package/_dist/page.d.ts.map +1 -0
- package/_dist/platform.d.ts +27 -0
- package/_dist/platform.d.ts.map +1 -0
- package/_dist/reader.d.ts +10 -0
- package/_dist/reader.d.ts.map +1 -0
- package/_dist/release.d.ts +48 -0
- package/_dist/release.d.ts.map +1 -0
- package/_dist/remote.d.ts +13 -0
- package/_dist/remote.d.ts.map +1 -0
- package/_dist/reported.d.ts +11 -0
- package/_dist/reported.d.ts.map +1 -0
- package/_dist/roles.d.ts +2 -0
- package/_dist/roles.d.ts.map +1 -0
- package/_dist/roster.d.ts +41 -0
- package/_dist/roster.d.ts.map +1 -0
- package/_dist/rpc.d.ts +29 -0
- package/_dist/rpc.d.ts.map +1 -0
- package/_dist/run.d.ts +164 -0
- package/_dist/run.d.ts.map +1 -0
- package/_dist/runtime.d.ts +4 -0
- package/_dist/runtime.d.ts.map +1 -0
- package/_dist/searching.d.ts +7 -0
- package/_dist/searching.d.ts.map +1 -0
- package/_dist/show.d.ts +22 -0
- package/_dist/show.d.ts.map +1 -0
- package/_dist/store.d.ts +25 -0
- package/_dist/store.d.ts.map +1 -0
- package/_dist/terminal.d.ts +17 -0
- package/_dist/terminal.d.ts.map +1 -0
- package/_dist/tool.d.ts +55 -0
- package/_dist/tool.d.ts.map +1 -0
- package/_dist/tty.d.ts +3 -0
- package/_dist/tty.d.ts.map +1 -0
- package/_dist/vault.d.ts +9 -0
- package/_dist/vault.d.ts.map +1 -0
- package/_dist/words.d.ts +11 -0
- package/_dist/words.d.ts.map +1 -0
- package/_dist/yak.d.ts +10 -0
- package/_dist/yak.d.ts.map +1 -0
- package/account-forward.js +40 -0
- package/account-forward.js.map +1 -0
- package/account-forward.ts +50 -0
- package/accounts.js +162 -0
- package/accounts.js.map +1 -0
- package/accounts.ts +228 -0
- package/answer.js +394 -0
- package/answer.js.map +1 -0
- package/answer.ts +569 -0
- package/apply.js +49 -0
- package/apply.js.map +1 -0
- package/apply.ts +54 -0
- package/args.js +420 -0
- package/args.js.map +1 -0
- package/args.ts +494 -0
- package/code.js +209 -0
- package/code.js.map +1 -0
- package/code.ts +227 -0
- package/commands.js +131 -0
- package/commands.js.map +1 -0
- package/commands.ts +142 -0
- package/complete.js +104 -0
- package/complete.js.map +1 -0
- package/complete.ts +152 -0
- package/config.js +125 -0
- package/config.js.map +1 -0
- package/config.ts +180 -0
- package/declarations.js +67 -0
- package/declarations.js.map +1 -0
- package/declarations.ts +84 -0
- package/deno.json +1 -0
- package/dependencies.js +126 -0
- package/dependencies.js.map +1 -0
- package/dependencies.ts +141 -0
- package/derive.js +102 -0
- package/derive.js.map +1 -0
- package/derive.ts +125 -0
- package/drain.js +83 -0
- package/drain.js.map +1 -0
- package/drain.ts +115 -0
- package/duties.ts +17 -0
- package/exports.json +1486 -0
- package/external.js +373 -0
- package/external.js.map +1 -0
- package/external.ts +435 -0
- package/facets.js +214 -0
- package/facets.js.map +1 -0
- package/facets.ts +232 -0
- package/feedback.js +54 -0
- package/feedback.js.map +1 -0
- package/feedback.ts +57 -0
- package/files.js +84 -0
- package/files.js.map +1 -0
- package/files.ts +112 -0
- package/get_starts.js +31 -0
- package/get_starts.js.map +1 -0
- package/get_starts.ts +34 -0
- package/git-credential.js +79 -0
- package/git-credential.js.map +1 -0
- package/git-credential.ts +77 -0
- package/grammar.js +4 -0
- package/grammar.js.map +1 -0
- package/grammar.ts +5 -0
- package/held.js +51 -0
- package/held.js.map +1 -0
- package/held.ts +66 -0
- package/host.js +1174 -0
- package/host.js.map +1 -0
- package/host.ts +1343 -0
- package/init.js +138 -0
- package/init.js.map +1 -0
- package/init.ts +139 -0
- package/install.js +216 -0
- package/install.js.map +1 -0
- package/install.ts +223 -0
- package/lifetime.js +56 -0
- package/lifetime.js.map +1 -0
- package/lifetime.ts +66 -0
- package/local-page.ts +114 -0
- package/local.js +417 -0
- package/local.js.map +1 -0
- package/local.ts +537 -0
- package/lockfile.js +20 -0
- package/lockfile.js.map +1 -0
- package/lockfile.ts +32 -0
- package/machines.js +33 -0
- package/machines.js.map +1 -0
- package/machines.ts +51 -0
- package/made/4f855610bdabcd736100d34e4e467ea5.json +1 -0
- package/main.js +534 -0
- package/main.js.map +1 -0
- package/main.ts +547 -0
- package/mcp.js +108 -0
- package/mcp.js.map +1 -0
- package/mcp.ts +113 -0
- package/member.js +103 -0
- package/member.js.map +1 -0
- package/member.ts +133 -0
- package/memo.js +503 -0
- package/memo.js.map +1 -0
- package/memo.ts +634 -0
- package/mod.js +46 -0
- package/mod.js.map +1 -0
- package/mod.ts +99 -0
- package/operations.js +53 -0
- package/operations.js.map +1 -0
- package/operations.ts +68 -0
- package/package.json +115 -4
- package/page.js +211 -0
- package/page.js.map +1 -0
- package/page.ts +308 -0
- package/parser.js +45 -0
- package/parser.js.map +1 -0
- package/parser.ts +77 -0
- package/people-search.js +112 -0
- package/people-search.js.map +1 -0
- package/people-search.ts +162 -0
- package/platform.js +184 -0
- package/platform.js.map +1 -0
- package/platform.ts +224 -0
- package/platform_effects.js +5 -0
- package/platform_effects.js.map +1 -0
- package/platform_effects.ts +13 -0
- package/prepare.js +40 -0
- package/prepare.js.map +1 -0
- package/prepare.ts +46 -0
- package/read_activity.js +54 -0
- package/read_activity.js.map +1 -0
- package/read_activity.ts +61 -0
- package/read_thread.js +387 -0
- package/read_thread.js.map +1 -0
- package/read_thread.ts +506 -0
- package/read_worker.ts +164 -0
- package/reader.js +48 -0
- package/reader.js.map +1 -0
- package/reader.ts +57 -0
- package/release.js +82 -0
- package/release.js.map +1 -0
- package/release.json +127 -0
- package/release.ts +98 -0
- package/remote-page.ts +70 -0
- package/remote.js +96 -0
- package/remote.js.map +1 -0
- package/remote.ts +153 -0
- package/report.js +83 -0
- package/report.js.map +1 -0
- package/report.ts +101 -0
- package/reported.js +32 -0
- package/reported.js.map +1 -0
- package/reported.ts +38 -0
- package/restart.js +349 -0
- package/restart.js.map +1 -0
- package/restart.ts +478 -0
- package/restore.js +32 -0
- package/restore.js.map +1 -0
- package/restore.ts +38 -0
- package/roles.js +32 -0
- package/roles.js.map +1 -0
- package/roles.ts +49 -0
- package/roster.js +65 -0
- package/roster.js.map +1 -0
- package/roster.ts +84 -0
- package/rpc.js +113 -0
- package/rpc.js.map +1 -0
- package/rpc.ts +159 -0
- package/run.js +533 -0
- package/run.js.map +1 -0
- package/run.ts +741 -0
- package/runtime.js +381 -0
- package/runtime.js.map +1 -0
- package/runtime.ts +433 -0
- package/sandboxes.js +26 -0
- package/sandboxes.js.map +1 -0
- package/sandboxes.ts +30 -0
- package/searching.js +33 -0
- package/searching.js.map +1 -0
- package/searching.ts +50 -0
- package/show.js +109 -0
- package/show.js.map +1 -0
- package/show.ts +144 -0
- package/shown.js +13 -0
- package/shown.js.map +1 -0
- package/shown.ts +44 -0
- package/stall.js +90 -0
- package/stall.js.map +1 -0
- package/stall.ts +156 -0
- package/stall_profile.js +113 -0
- package/stall_profile.js.map +1 -0
- package/stall_profile.ts +142 -0
- package/stall_wait.js +183 -0
- package/stall_wait.js.map +1 -0
- package/stall_wait.ts +249 -0
- package/stall_wait_worker.ts +11 -0
- package/start.js +349 -0
- package/start.js.map +1 -0
- package/start.ts +343 -0
- package/starvation.js +147 -0
- package/starvation.js.map +1 -0
- package/starvation.ts +224 -0
- package/store.js +118 -0
- package/store.js.map +1 -0
- package/store.ts +193 -0
- package/subcommands.js +62 -0
- package/subcommands.js.map +1 -0
- package/subcommands.ts +77 -0
- package/terminal.js +93 -0
- package/terminal.js.map +1 -0
- package/terminal.ts +128 -0
- package/text.js +20 -0
- package/text.js.map +1 -0
- package/text.ts +37 -0
- package/tool.js +76 -0
- package/tool.js.map +1 -0
- package/tool.ts +157 -0
- package/trace_control.js +231 -0
- package/trace_control.js.map +1 -0
- package/trace_control.ts +233 -0
- package/traces.js +138 -0
- package/traces.js.map +1 -0
- package/traces.ts +160 -0
- package/tracker.js +22 -0
- package/tracker.js.map +1 -0
- package/tracker.ts +27 -0
- package/tty.js +82 -0
- package/tty.js.map +1 -0
- package/tty.ts +142 -0
- package/unit_failure.js +178 -0
- package/unit_failure.js.map +1 -0
- package/unit_failure.ts +191 -0
- package/upkeep_thread.js +32 -0
- package/upkeep_thread.js.map +1 -0
- package/upkeep_thread.ts +35 -0
- package/upkeep_worker.ts +27 -0
- package/vault.js +186 -0
- package/vault.js.map +1 -0
- package/vault.ts +220 -0
- package/vocab-ids.js +63 -0
- package/vocab-ids.js.map +1 -0
- package/vocab-ids.ts +62 -0
- package/vocab.json +35 -0
- package/warm.js +138 -0
- package/warm.js.map +1 -0
- package/warm.ts +171 -0
- package/watch_revision.js +25 -0
- package/watch_revision.js.map +1 -0
- package/watch_revision.ts +30 -0
- package/words.js +29 -0
- package/words.js.map +1 -0
- package/words.ts +41 -0
- package/workspaces.js +40 -0
- package/workspaces.js.map +1 -0
- package/workspaces.ts +58 -0
- package/yak-unit-failure.conf +6 -0
- package/yak-unit-handoff.conf +2 -0
- package/yak.js +123 -0
- package/yak.js.map +1 -0
- package/yak.ts +136 -0
package/host.ts
ADDED
|
@@ -0,0 +1,1343 @@
|
|
|
1
|
+
import { statements } from '@yaks/sqlite/statements'
|
|
2
|
+
import { freshFetch } from '@yaks/process/http'
|
|
3
|
+
import * as process from '@yaks/process/system'
|
|
4
|
+
import * as disk from '@yaks/file/io'
|
|
5
|
+
import { getStarts } from './get_starts.js'
|
|
6
|
+
import { workspaceCapabilities } from './workspaces.js'
|
|
7
|
+
import { overdue } from '@yaks/machine/targets'
|
|
8
|
+
import { resolveHome } from './machines.js'
|
|
9
|
+
import { sandboxCapabilities } from './sandboxes.js'
|
|
10
|
+
import { trackerOptions } from './tracker.js'
|
|
11
|
+
/**
|
|
12
|
+
* The graph a config file names, opened by a process for the roles it serves.
|
|
13
|
+
*
|
|
14
|
+
* A plugin says what it contributes, one facet per subpath: the components and
|
|
15
|
+
* tools it declares (`@yaks/mail/vocab`), what it adds to a graph (`/graph`), the
|
|
16
|
+
* functions behind its tools (`/tools`), what runs after a commit
|
|
17
|
+
* (`/effects`), the work it keeps doing while a process is up (`/service`), the
|
|
18
|
+
* HTTP it adds (`/routes`), its terminal controls (`/cli`), and how its
|
|
19
|
+
* entities are drawn (`/views`, `/tui`) and dressed (`/ui`, their kits, UX kits, themes and skins).
|
|
20
|
+
* It never says where any of that runs. A process serves roles, and imports
|
|
21
|
+
* only the facets of the roles it serves:
|
|
22
|
+
*
|
|
23
|
+
* ```
|
|
24
|
+
* graph vocab, graph, tools open the file, admit writes, run tool calls
|
|
25
|
+
* web routes, ui the HTTP a listener answers with
|
|
26
|
+
* effects effects the code behind the effects a commit owes
|
|
27
|
+
* @yaks/mail that plugin's service the timer or poll one plugin keeps up
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* The first three are {@link ROLES}, which every graph has; a plugin with a
|
|
31
|
+
* `./service` brings a role of its own, named by its package. The config names
|
|
32
|
+
* the plugins; each process picks its roles. `yak serve` serves `web`, the
|
|
33
|
+
* role @yaks/api's `serve` declares. A `yak` command serves commands and
|
|
34
|
+
* rendering, which are its own (./run.ts, ./answer.ts), and reaches the graph
|
|
35
|
+
* either through a server or by composing the graph role here itself
|
|
36
|
+
* (local.ts). A host that stays up explicitly starts duty roles in a thread
|
|
37
|
+
* of the same process (@yaks/threads), a host of its own. A role
|
|
38
|
+
* this process does not serve costs it nothing: its facets
|
|
39
|
+
* are never imported, so a command that opens the graph to read it never loads
|
|
40
|
+
* a line of HTTP, and a process that serves no `web` never asks a plugin for a
|
|
41
|
+
* route.
|
|
42
|
+
*
|
|
43
|
+
* {@link compose} opens the box's SQLite file, vault and capabilities and calls
|
|
44
|
+
* {@link assembly} in @yaks/host to load facets and build the graph over the
|
|
45
|
+
* plugins' rules and the other roles' contributions. A subpath a package does not export is skipped;
|
|
46
|
+
* one that exists and fails to import is an error, never a skip. Nothing here
|
|
47
|
+
* binds a port: `serve`, declared and implemented by
|
|
48
|
+
* {@link https://jsr.io/@yaks/api | @yaks/api}, listens with the
|
|
49
|
+
* {@link Host.handler} built here, and only a config naming @yaks/api has one.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { compose } from '@yaks/cli/host'
|
|
53
|
+
*
|
|
54
|
+
* // let config = { db: 'graph.db', plugins: ['@yaks/task'] }
|
|
55
|
+
* // let host = await compose(config, ['graph'])
|
|
56
|
+
* // console.log(await host.graph.read('.task'))
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* @module
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
import type {
|
|
63
|
+
Actor,
|
|
64
|
+
Bundle,
|
|
65
|
+
Comp,
|
|
66
|
+
Eid,
|
|
67
|
+
Graph,
|
|
68
|
+
NamedTool,
|
|
69
|
+
Storage,
|
|
70
|
+
} from 'jsr:@yaks/graph@0.2.36'
|
|
71
|
+
import { Refused, Stale } from '@yaks/graph/errors'
|
|
72
|
+
import { hit } from '@yaks/graph/hit'
|
|
73
|
+
import type { Vocab, VocabDoc } from 'jsr:@yaks/vocab@0.2.36'
|
|
74
|
+
import { EXIT, PROCESS } from '@yaks/process/vocab'
|
|
75
|
+
import { ended, selfEid, started } from '@yaks/process/self'
|
|
76
|
+
import type { History, Store } from '@yaks/sqlite'
|
|
77
|
+
import type { Stmt } from 'jsr:@yaks/sql@0.2.36'
|
|
78
|
+
import { migrations, migrationSchema } from '@yaks/sqlite/migration'
|
|
79
|
+
import { add, follow, printing, printOf } from '@yaks/sqlite/follow'
|
|
80
|
+
import { bindStorage } from '@yaks/sqlite'
|
|
81
|
+
import { requirements, requires } from '@yaks/sqlite/meta'
|
|
82
|
+
import { filler, SCHEMA_DUTY, tend } from '@yaks/sqlite/owe'
|
|
83
|
+
import { survey } from '@yaks/sqlite/survey'
|
|
84
|
+
import type { Opened } from '@yaks/sqlite/db'
|
|
85
|
+
import { type Found, searching } from './searching.js'
|
|
86
|
+
import { peopleSearch } from './people-search.js'
|
|
87
|
+
import {
|
|
88
|
+
adopt,
|
|
89
|
+
find,
|
|
90
|
+
heal,
|
|
91
|
+
type Hit,
|
|
92
|
+
indexed,
|
|
93
|
+
pieces,
|
|
94
|
+
schema as textSchema,
|
|
95
|
+
search,
|
|
96
|
+
split,
|
|
97
|
+
unfound,
|
|
98
|
+
worded as wordFields,
|
|
99
|
+
} from '@yaks/fts'
|
|
100
|
+
import { EFFECT, effects, expiration, released } from '@yaks/effects'
|
|
101
|
+
import { type Local, peek } from '@yaks/secrets'
|
|
102
|
+
import { opKeeper } from '@yaks/secrets/op'
|
|
103
|
+
import { command, reporter, revision } from './report.js'
|
|
104
|
+
import { faults } from '@yaks/api/refuse'
|
|
105
|
+
import { type Backend, backend } from '@yaks/blob/backend'
|
|
106
|
+
import { artifactsAt, fileBlobs } from '@yaks/file/blobs'
|
|
107
|
+
import { blobSchema, sqliteBlobs } from '@yaks/blob/sqlite'
|
|
108
|
+
import type { Blobs } from 'jsr:@yaks/blob@0.2.36'
|
|
109
|
+
import { type Config, dbOf, given, used } from './config.js'
|
|
110
|
+
import { ALIAS, aliasEid } from '@yaks/alias'
|
|
111
|
+
import { format } from '@yaks/id'
|
|
112
|
+
import {
|
|
113
|
+
configured,
|
|
114
|
+
facetsOf,
|
|
115
|
+
meet,
|
|
116
|
+
numbering,
|
|
117
|
+
type Reveal,
|
|
118
|
+
} from '@yaks/host/config'
|
|
119
|
+
import { facet } from './words.js'
|
|
120
|
+
import {
|
|
121
|
+
assembly,
|
|
122
|
+
type Binding,
|
|
123
|
+
type Facets,
|
|
124
|
+
type Load,
|
|
125
|
+
type MeaningHit,
|
|
126
|
+
} from '@yaks/host'
|
|
127
|
+
export { every } from '@yaks/host/roles'
|
|
128
|
+
import { declarations } from './declarations.js'
|
|
129
|
+
import { ROLES } from './roles.js'
|
|
130
|
+
export { ROLES } from './roles.js'
|
|
131
|
+
import { drain } from './drain.js'
|
|
132
|
+
import { vaultOf } from './vault.js'
|
|
133
|
+
import type { Command, Ctx } from './run.ts'
|
|
134
|
+
import { roles as serveRoles, type Thread } from '@yaks/threads'
|
|
135
|
+
export type { Thread } from '@yaks/threads'
|
|
136
|
+
import { during, outcome, peek as tracing, type Span } from '@yaks/trace'
|
|
137
|
+
import { compositionAnatomy, secretNames } from '@yaks/host/anatomy'
|
|
138
|
+
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
139
|
+
import { type Context as TraceContext, installContext } from '@yaks/trace'
|
|
140
|
+
import { type Asker, installAsker } from '@yaks/graph/asker'
|
|
141
|
+
import { spool } from '@yaks/tracker/report'
|
|
142
|
+
import { CallError } from '@yaks/tools/errors'
|
|
143
|
+
import { persist } from '@yaks/tools/persist'
|
|
144
|
+
import { feedbackTool } from './feedback.js'
|
|
145
|
+
import { operations } from './operations.js'
|
|
146
|
+
import { delivery } from './remote.js'
|
|
147
|
+
import { type Clock, system } from '@yaks/fp'
|
|
148
|
+
import { files } from '@yaks/tracker/file'
|
|
149
|
+
import type { Traces } from './traces.js'
|
|
150
|
+
import { traceControl } from './trace_control.js'
|
|
151
|
+
import { upkeepThread } from './upkeep_thread.js'
|
|
152
|
+
import { watchRevision } from './watch_revision.js'
|
|
153
|
+
|
|
154
|
+
// One carrier per runtime, not per composed graph. Multiple CLI hosts can
|
|
155
|
+
// coexist without replacing one another's async context.
|
|
156
|
+
let traceLocal = new AsyncLocalStorage<TraceContext | undefined>()
|
|
157
|
+
|
|
158
|
+
// What finds the processes that ended without closing, imported by the first
|
|
159
|
+
// host that asks after one: a command that buries nobody never loads the
|
|
160
|
+
// supervisor it lives in.
|
|
161
|
+
let supervisor = () => import('@yaks/process')
|
|
162
|
+
installContext({
|
|
163
|
+
get: () => traceLocal.getStore(),
|
|
164
|
+
run: (ctx, work) => traceLocal.run(ctx, work),
|
|
165
|
+
})
|
|
166
|
+
let askerLocal = new AsyncLocalStorage<Asker | undefined>()
|
|
167
|
+
installAsker({
|
|
168
|
+
get: () => askerLocal.getStore(),
|
|
169
|
+
run: (asker, work) => askerLocal.run(asker, work),
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
// A read that ran on, named by who asked it: its door and page as tags, its
|
|
173
|
+
// session as what it ran during.
|
|
174
|
+
let asked = (asker?: Asker) => ({
|
|
175
|
+
tags: {
|
|
176
|
+
step: 'read',
|
|
177
|
+
...(asker && { door: asker.door }),
|
|
178
|
+
...(asker?.page && { page: asker.page }),
|
|
179
|
+
},
|
|
180
|
+
...(asker?.session && { during: { session: asker.session } }),
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
export { type Declared, facet, type Words, words } from './words.js'
|
|
184
|
+
|
|
185
|
+
export {
|
|
186
|
+
type Config,
|
|
187
|
+
configPath,
|
|
188
|
+
given,
|
|
189
|
+
type Options,
|
|
190
|
+
person,
|
|
191
|
+
type Plug,
|
|
192
|
+
read,
|
|
193
|
+
used,
|
|
194
|
+
} from './config.js'
|
|
195
|
+
|
|
196
|
+
import type { Host } from '@yaks/host'
|
|
197
|
+
export type { Feed, Host } from '@yaks/host'
|
|
198
|
+
|
|
199
|
+
/** A role: one of {@link ROLES}, or a plugin's package, for its service. */
|
|
200
|
+
export type Role = string
|
|
201
|
+
|
|
202
|
+
// Scores from FTS and a vector space are incomparable. Rank positions give
|
|
203
|
+
// each source a vote; direct word matches get a small lead, and a hit in both
|
|
204
|
+
// gets both votes.
|
|
205
|
+
let merged = (words: Hit[], meaning: MeaningHit[], limit: number) => {
|
|
206
|
+
let by = new Map<Eid, {
|
|
207
|
+
entity: Eid
|
|
208
|
+
score: number
|
|
209
|
+
snippet: string
|
|
210
|
+
source: 'text' | 'meaning' | 'both'
|
|
211
|
+
}>()
|
|
212
|
+
words.forEach((h, i) =>
|
|
213
|
+
by.set(h.entity, {
|
|
214
|
+
entity: h.entity,
|
|
215
|
+
score: 1.1 / (60 + i + 1),
|
|
216
|
+
snippet: h.snippet,
|
|
217
|
+
source: 'text',
|
|
218
|
+
})
|
|
219
|
+
)
|
|
220
|
+
meaning.forEach((h, i) => {
|
|
221
|
+
let had = by.get(h.entity)
|
|
222
|
+
by.set(
|
|
223
|
+
h.entity,
|
|
224
|
+
had
|
|
225
|
+
? {
|
|
226
|
+
...had,
|
|
227
|
+
score: had.score + 1 / (60 + i + 1),
|
|
228
|
+
source: 'both',
|
|
229
|
+
}
|
|
230
|
+
: {
|
|
231
|
+
entity: h.entity,
|
|
232
|
+
score: 1 / (60 + i + 1),
|
|
233
|
+
snippet: h.excerpt,
|
|
234
|
+
source: 'meaning',
|
|
235
|
+
},
|
|
236
|
+
)
|
|
237
|
+
})
|
|
238
|
+
return [...by.values()]
|
|
239
|
+
.sort((a, b) => b.score - a.score || a.entity.localeCompare(b.entity))
|
|
240
|
+
.slice(0, limit)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** A plugin's command-line control. It opens the graph but is not a persisted
|
|
244
|
+
* tool call: a password prompt or OAuth attempt cannot be replayed by a
|
|
245
|
+
* server after the terminal process exits. */
|
|
246
|
+
export type CliCommand = Omit<Command, 'run'> & {
|
|
247
|
+
run: (
|
|
248
|
+
args: Record<string, unknown>,
|
|
249
|
+
host: Host,
|
|
250
|
+
context: Ctx,
|
|
251
|
+
) => number | Promise<number>
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** `<plugin>/cli` — commands for the person at this terminal, not graph tools. */
|
|
255
|
+
export type CliFacet = {
|
|
256
|
+
commands?: CliCommand[]
|
|
257
|
+
/** Explicit global-input bindings for this plugin's graph tools. */
|
|
258
|
+
globals?: Record<string, NonNullable<Command['globals']>>
|
|
259
|
+
/** Local tool invocations that can finish without opening a graph. Undefined
|
|
260
|
+
* delegates to the ordinary tool runner. Arguments are already validated. */
|
|
261
|
+
tools?: Record<
|
|
262
|
+
string,
|
|
263
|
+
(
|
|
264
|
+
args: Record<string, unknown>,
|
|
265
|
+
context: Ctx,
|
|
266
|
+
) => number | undefined | Promise<number | undefined>
|
|
267
|
+
>
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** An assembled host: everything a plugin factory was given, plus what only
|
|
271
|
+
* the caller of {@link compose} needs. */
|
|
272
|
+
export type Served = Host & {
|
|
273
|
+
storage: Omit<Store, keyof Storage> & Storage
|
|
274
|
+
/** The file's upkeep on this connection. File hosts lend this only to
|
|
275
|
+
* their dedicated upkeep thread; memory has no second connection. */
|
|
276
|
+
upkeep: (
|
|
277
|
+
signal: AbortSignal,
|
|
278
|
+
report: (error: unknown, work: string) => void,
|
|
279
|
+
) => Promise<void>
|
|
280
|
+
/** the text half of {@link Host.search}: the best `count` text hits for the
|
|
281
|
+
* words, read here, where the search tool searches at all */
|
|
282
|
+
hits?: (words: string, count: number) => Hit[]
|
|
283
|
+
/** the rest of {@link Host.search}: the best `limit` of what its halves
|
|
284
|
+
* found, read and said as hit lines */
|
|
285
|
+
said?: (words: string, found: Found, limit: number) => Promise<Bundle[]>
|
|
286
|
+
/** wind down, with the graph still open: {@link Host.stopping} aborts, so
|
|
287
|
+
* the duties stop and whatever hangs off it ends — a server stops taking
|
|
288
|
+
* requests and answers the ones in flight, a transcript starts no new step —
|
|
289
|
+
* and it leaves the pool, so what is running can finish and be written
|
|
290
|
+
* before {@link close} */
|
|
291
|
+
stop: () => void
|
|
292
|
+
/** close the graph, once the effects it started have finished: every call
|
|
293
|
+
* this process's runner is still running ended as interrupted, every lease
|
|
294
|
+
* it holds released and its `exit` stamped —
|
|
295
|
+
* with the code it is given, or with none where nobody knows how it ended.
|
|
296
|
+
* Await it when the process is about to end, or that last write races the
|
|
297
|
+
* exit and the row reads as still running forever. */
|
|
298
|
+
close: (code?: number) => void | Promise<void>
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** How a host is composed, beyond its roles. */
|
|
302
|
+
export type ComposeOpts = {
|
|
303
|
+
/** The config file this host was read from, which a restart it is asked
|
|
304
|
+
* for reads the box's roles from. */
|
|
305
|
+
path?: string
|
|
306
|
+
/** Open SQLite read-only and record no process or tool-call bookkeeping. A
|
|
307
|
+
* reader never changes the schema; every other host brings the file to its
|
|
308
|
+
* composed schema as it opens (@yaks/sqlite `follow`). */
|
|
309
|
+
readOnly?: boolean
|
|
310
|
+
/** the thread running the duties this host hands off */
|
|
311
|
+
thread?: Thread
|
|
312
|
+
/** The read door of a web or worker; this host closes an owned reader
|
|
313
|
+
* before telemetry. */
|
|
314
|
+
reader?: NonNullable<Host['reader']> & {
|
|
315
|
+
close?: () => void | Promise<void>
|
|
316
|
+
/** where the reader tells of a read that runs on, and who asked it */
|
|
317
|
+
overrun?: (tell: (error: Error, asker?: Asker) => void) => void
|
|
318
|
+
}
|
|
319
|
+
/** what the host carries as {@link Host.warm} */
|
|
320
|
+
warm?: () => Promise<void>
|
|
321
|
+
/** a reader beside a host records no process of its own */
|
|
322
|
+
process?: boolean
|
|
323
|
+
/** Open the graph as of a transaction, or on a branch, where its store
|
|
324
|
+
* keeps history (D-130964): read only as of a transaction, writing only to
|
|
325
|
+
* the branch on one. Default: `$YAK_AT` or `$YAK_BRANCH`, else main. */
|
|
326
|
+
mode?: { at: number } | { branch: number }
|
|
327
|
+
/** The clock the host keeps: its graph's and its runner's (@yaks/host
|
|
328
|
+
* `Binding.clock`), and its own waits, a process write storage refused and
|
|
329
|
+
* a drain step's warning (default the runtime's). */
|
|
330
|
+
clock?: Clock
|
|
331
|
+
/** The graph's own words, loaded after its plugins', each component
|
|
332
|
+
* shadowing a plugin's of the same name (@yaks/host `AssemblyOpts.docs`):
|
|
333
|
+
* an app's vocab.json. A config's graph has none. */
|
|
334
|
+
docs?: VocabDoc[]
|
|
335
|
+
/** The graph's own commands, offered beside this host's operations: an
|
|
336
|
+
* app's, which its vocab.json declares (@yaks/platform `released`). A
|
|
337
|
+
* config's graph has none. */
|
|
338
|
+
tools?: (host: Host) => NamedTool[]
|
|
339
|
+
/** What each plugin is given beside its configured options (@yaks/host
|
|
340
|
+
* `Binding.options`): what only the caller can lend it. */
|
|
341
|
+
options?: Binding['options']
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// The mode a process opens its graph in, as its environment says.
|
|
345
|
+
let moding = (): ComposeOpts['mode'] => {
|
|
346
|
+
let at = process.env.get('YAK_AT'), branch = process.env.get('YAK_BRANCH')
|
|
347
|
+
return at
|
|
348
|
+
? { at: Number(at) }
|
|
349
|
+
: branch
|
|
350
|
+
? { branch: Number(branch) }
|
|
351
|
+
: undefined
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export { dbOf } from './config.js'
|
|
355
|
+
|
|
356
|
+
// `{"secret": "NAME"}` anywhere in a plugin's options reads that secret at the
|
|
357
|
+
// moment the value is accessed (@yaks/secrets `peek`): the local vault value,
|
|
358
|
+
// or else the environment variable of that name. It is the one thing a config
|
|
359
|
+
// file cannot hold in the open, and the one thing that can arrive after the
|
|
360
|
+
// host is already running, so a plugin that re-reads its options on each pass starts
|
|
361
|
+
// the moment a key is written rather than needing a restart. The value comes
|
|
362
|
+
// from this box's vault; no config read starts a 1Password process. A secret
|
|
363
|
+
// nobody has provided reads as undefined, so the plugin reports in its own
|
|
364
|
+
// words what it is waiting for.
|
|
365
|
+
let revealing = (vault?: Local): Reveal =>
|
|
366
|
+
vault ? { secret: (name) => peek(vault, name, { env: process.env.get }) } : {}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Who a host writes as when no request named a caller: nobody, through this
|
|
370
|
+
* PROCESS. A process is an instrument, never an actor, so it stands in `via`
|
|
371
|
+
* alone, the way a session does (M-31958).
|
|
372
|
+
*
|
|
373
|
+
* There is no configured actor name. A run of a program is not a singleton and
|
|
374
|
+
* never was — two `yak` commands and a `yak serve` over one file are three
|
|
375
|
+
* writers — so the instrument is the `process` row this run wrote on the way in
|
|
376
|
+
* (@yaks/process `started`). That makes `created.via` the answer to *which run*
|
|
377
|
+
* wrote a thing, and lets a child process's row, written by its parent, record
|
|
378
|
+
* whose child it is without needing a property for it.
|
|
379
|
+
*
|
|
380
|
+
* A graph whose vocabulary has no `process` component has no such row, and
|
|
381
|
+
* writes go unattributed rather than attributed to an id nothing created.
|
|
382
|
+
*/
|
|
383
|
+
export let writer = (vocab: Vocab): Actor | null =>
|
|
384
|
+
vocab.comp(PROCESS) ? { via: selfEid() } : null
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Open the graph a config names, for the roles this process serves: import
|
|
388
|
+
* those roles' facets and nothing else, open the database, build the graph, and
|
|
389
|
+
* wire in what each role brings — the code behind the effects, and the worker
|
|
390
|
+
* that runs them, for `effects`; the request handler for `web`; a plugin's
|
|
391
|
+
* service for the role named by its package. Every process serves `graph`,
|
|
392
|
+
* since the graph is what it opened.
|
|
393
|
+
*
|
|
394
|
+
* `load` is how one of a plugin's subpaths becomes a module ({@link facet}).
|
|
395
|
+
* It is injectable, so a test can assemble a host from modules it wrote inline
|
|
396
|
+
* rather than files on disk.
|
|
397
|
+
*/
|
|
398
|
+
export let compose = (
|
|
399
|
+
config: Config,
|
|
400
|
+
roles: readonly Role[],
|
|
401
|
+
load: Load = facet,
|
|
402
|
+
opts: ComposeOpts = {},
|
|
403
|
+
): Promise<Served> => {
|
|
404
|
+
// The config exists before its graph does, so a consumer can subscribe to
|
|
405
|
+
// its channel before any startup work. No recorder is created by a host.
|
|
406
|
+
let c = tracing(config)
|
|
407
|
+
if (!c) return composed(config, roles, load, opts)
|
|
408
|
+
let root = c.begin({
|
|
409
|
+
kind: 'process-start',
|
|
410
|
+
name: '@yaks/cli.compose',
|
|
411
|
+
package: '@yaks/cli',
|
|
412
|
+
})
|
|
413
|
+
let part: Span | undefined
|
|
414
|
+
let next = (name: string) => {
|
|
415
|
+
part?.end()
|
|
416
|
+
part = c.begin({
|
|
417
|
+
kind: 'phase',
|
|
418
|
+
name: `@yaks/cli.compose.${name}`,
|
|
419
|
+
package: '@yaks/cli',
|
|
420
|
+
parent: root?.id,
|
|
421
|
+
})
|
|
422
|
+
}
|
|
423
|
+
return during(root, async () => {
|
|
424
|
+
try {
|
|
425
|
+
let host = await composed(config, roles, load, opts, next)
|
|
426
|
+
part?.end()
|
|
427
|
+
return host
|
|
428
|
+
} catch (error) {
|
|
429
|
+
part?.end({ outcome: outcome(error) })
|
|
430
|
+
throw error
|
|
431
|
+
}
|
|
432
|
+
}) as Promise<Served>
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
let composed = async (
|
|
436
|
+
config: Config,
|
|
437
|
+
roles: readonly Role[],
|
|
438
|
+
load: Load,
|
|
439
|
+
opts: ComposeOpts,
|
|
440
|
+
part?: (name: string) => void,
|
|
441
|
+
): Promise<Served> => {
|
|
442
|
+
part?.('reporter')
|
|
443
|
+
if (!roles.includes('graph')) {
|
|
444
|
+
throw new CallError(
|
|
445
|
+
'config',
|
|
446
|
+
'a host opens a graph — its roles include graph',
|
|
447
|
+
)
|
|
448
|
+
}
|
|
449
|
+
let uses = new Set(config.plugins?.map(used))
|
|
450
|
+
if (load == facet && !uses.has('@yaks/egress')) {
|
|
451
|
+
config = { ...config, plugins: [...config.plugins ?? [], '@yaks/egress'] }
|
|
452
|
+
}
|
|
453
|
+
let platformOperations = roles.includes('effects')
|
|
454
|
+
? (await import('./platform_effects.js')).platformEffects
|
|
455
|
+
: undefined
|
|
456
|
+
let path = dbOf(config)
|
|
457
|
+
let clock = opts.clock ?? system
|
|
458
|
+
let report = reporter(
|
|
459
|
+
opts.readOnly ? {} : config,
|
|
460
|
+
{ via: selfEid() },
|
|
461
|
+
// Asked now and awaited only by a report: the commit is this run's, and
|
|
462
|
+
// a command that reports nothing does not wait on git to learn it.
|
|
463
|
+
config.tracker && !opts.readOnly
|
|
464
|
+
? config.tracker.commit ?? revision()
|
|
465
|
+
: undefined,
|
|
466
|
+
{ command: command(process.args), roles: roles.join(',') },
|
|
467
|
+
)
|
|
468
|
+
opts.reader?.overrun?.((error, asker) => void report(error, asked(asker)))
|
|
469
|
+
let cleanup = async (step: string, release: () => void | Promise<void>) => {
|
|
470
|
+
try {
|
|
471
|
+
await release()
|
|
472
|
+
} catch (error) {
|
|
473
|
+
await report(error, { tags: { step } })
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
let mode = opts.mode ?? moding()
|
|
477
|
+
let opened: Opened | undefined
|
|
478
|
+
// A store as of a transaction or on a branch is @yaks/sqlite's writing store
|
|
479
|
+
// (`moded`), which the read-only door does not open, so a reader in a mode
|
|
480
|
+
// takes the writer's connection.
|
|
481
|
+
let open = async (): Promise<Opened> =>
|
|
482
|
+
opts.readOnly && !mode
|
|
483
|
+
? (await import('@yaks/sqlite/read-db')).open(path)
|
|
484
|
+
: (await import('@yaks/sqlite/db')).open(path)
|
|
485
|
+
let recorded: Traces | undefined
|
|
486
|
+
let captureControl: ReturnType<typeof traceControl> | undefined
|
|
487
|
+
// What @yaks/api catches in this process and answers, or meets with nobody
|
|
488
|
+
// to answer (a page body that did not build, a socket, a subscription), are
|
|
489
|
+
// reported here too, named by where they were caught.
|
|
490
|
+
let caught = (error: unknown, where: string) =>
|
|
491
|
+
void report(error, {
|
|
492
|
+
during: { process: selfEid() },
|
|
493
|
+
tags: { step: where },
|
|
494
|
+
})
|
|
495
|
+
// A rejection nobody held is reported where it surfaced, and the host lives
|
|
496
|
+
// on: one stray promise never ends the process and every request it serves.
|
|
497
|
+
// A script's top-level await that failed arrives the same way, and is the
|
|
498
|
+
// script's own end: said and reported as a failure it met (a refusal of
|
|
499
|
+
// what it asked is its runner's to read, and only said), then the process
|
|
500
|
+
// exits 1, as it would with no host.
|
|
501
|
+
let stray = (event: PromiseRejectionEvent) => {
|
|
502
|
+
event.preventDefault()
|
|
503
|
+
let own = process.mainFailed(event.reason)
|
|
504
|
+
void report(
|
|
505
|
+
event.reason,
|
|
506
|
+
own.then((ended) =>
|
|
507
|
+
ended ? { tags: { step: 'main module' } } : {
|
|
508
|
+
during: { process: selfEid() },
|
|
509
|
+
mechanism: 'onunhandledrejection',
|
|
510
|
+
tags: { step: 'unhandled rejection' },
|
|
511
|
+
}
|
|
512
|
+
),
|
|
513
|
+
).then(async () => {
|
|
514
|
+
if (await own) process.exit(1)
|
|
515
|
+
})
|
|
516
|
+
}
|
|
517
|
+
globalThis.addEventListener('unhandledrejection', stray)
|
|
518
|
+
let unhooked = faults(caught)
|
|
519
|
+
let unfault = () => {
|
|
520
|
+
unhooked()
|
|
521
|
+
globalThis.removeEventListener('unhandledrejection', stray)
|
|
522
|
+
}
|
|
523
|
+
try {
|
|
524
|
+
// An existing graph can refuse before any plugin runtime is imported.
|
|
525
|
+
// Keep its connection; absent graphs wait for a valid assembly before
|
|
526
|
+
// creating a file or its directory.
|
|
527
|
+
let exists = false
|
|
528
|
+
if (path != ':memory:') {
|
|
529
|
+
try {
|
|
530
|
+
disk.statSync(path)
|
|
531
|
+
exists = true
|
|
532
|
+
} catch (error) {
|
|
533
|
+
if (!(error instanceof disk.errors.NotFound)) throw error
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
// What the store's data requires (a plugin that shaped how its rows are
|
|
537
|
+
// written) this config must compose, or the store is refused: a host
|
|
538
|
+
// without it would read or write those rows wrongly.
|
|
539
|
+
let required: string[] = []
|
|
540
|
+
if (exists) {
|
|
541
|
+
part?.('sqlite')
|
|
542
|
+
opened = await open()
|
|
543
|
+
part?.('migrations')
|
|
544
|
+
migrations(opened).ready()
|
|
545
|
+
required = requirements(opened)
|
|
546
|
+
meet(config, required)
|
|
547
|
+
}
|
|
548
|
+
// Where this graph's secrets are kept (./vault.ts). Nothing is read from
|
|
549
|
+
// it here: an option naming a secret reads its local value when the
|
|
550
|
+
// option is read.
|
|
551
|
+
part?.('vault')
|
|
552
|
+
let trace = opts.process !== false && (config.tracker || path != ':memory:')
|
|
553
|
+
? await import('./traces.js')
|
|
554
|
+
: undefined
|
|
555
|
+
let vault = vaultOf(path)
|
|
556
|
+
let secretDeclarations = (config.plugins ?? []).flatMap((p) =>
|
|
557
|
+
secretNames(given(p)).map((name) => ({ name, package: used(p) }))
|
|
558
|
+
)
|
|
559
|
+
let plugins = configured(config, revealing(vault))
|
|
560
|
+
let declared = load === facet ? await declarations(config) : undefined
|
|
561
|
+
let stored = new Map(
|
|
562
|
+
declared?.vocabs.filter((v) => v.value !== null).map((
|
|
563
|
+
v,
|
|
564
|
+
) => [v.plugin, v]),
|
|
565
|
+
)
|
|
566
|
+
part?.('roles')
|
|
567
|
+
let observed = compositionAnatomy(
|
|
568
|
+
(config.plugins ?? []).map((p) => ({
|
|
569
|
+
name: used(p),
|
|
570
|
+
facets: facetsOf(p),
|
|
571
|
+
})),
|
|
572
|
+
roles,
|
|
573
|
+
ROLES,
|
|
574
|
+
secretDeclarations,
|
|
575
|
+
)
|
|
576
|
+
// A declaration hit substitutes data only while its sources agree. The
|
|
577
|
+
// assembly sees the same facets as an ordinary loader, without CLI policy.
|
|
578
|
+
let preparedLoad: Load = Object.assign(
|
|
579
|
+
async <F extends keyof Facets>(
|
|
580
|
+
plugin: string,
|
|
581
|
+
name: F,
|
|
582
|
+
): Promise<Facets[F] | null> => {
|
|
583
|
+
let data = name == 'vocab' ? stored.get(plugin) : undefined
|
|
584
|
+
return data && !data.factories
|
|
585
|
+
? data.value as Facets[F] | null
|
|
586
|
+
: declared && !declared.facets[plugin].includes(name)
|
|
587
|
+
? null
|
|
588
|
+
: await load(plugin, name)
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
together: (requests: readonly (readonly [string, keyof Facets])[]) =>
|
|
592
|
+
load.together?.(
|
|
593
|
+
requests.filter(([plugin, name]) =>
|
|
594
|
+
(!declared || declared.facets[plugin].includes(name)) &&
|
|
595
|
+
(name != 'vocab' || !declared || !!stored.get(plugin)?.factories)
|
|
596
|
+
),
|
|
597
|
+
) ?? Promise.resolve(),
|
|
598
|
+
},
|
|
599
|
+
)
|
|
600
|
+
let plan = await assembly(config, roles, preparedLoad, {
|
|
601
|
+
plugins,
|
|
602
|
+
observed,
|
|
603
|
+
part,
|
|
604
|
+
readOnly: opts.readOnly,
|
|
605
|
+
docs: opts.docs,
|
|
606
|
+
})
|
|
607
|
+
let { storage } = opts.readOnly
|
|
608
|
+
? await import('@yaks/sqlite/read-store')
|
|
609
|
+
: await import('@yaks/sqlite')
|
|
610
|
+
// A store as of a transaction or on a branch reads through a connection
|
|
611
|
+
// of its own: this one, which opened for nothing else.
|
|
612
|
+
let moded = mode && await import('@yaks/sqlite')
|
|
613
|
+
let opening = (
|
|
614
|
+
o: Parameters<typeof storage>[2] & { history?: History },
|
|
615
|
+
) => {
|
|
616
|
+
if (!mode || !moded) return storage(sql, vocab, o)
|
|
617
|
+
let { history } = o
|
|
618
|
+
if (!history) {
|
|
619
|
+
throw new Error(
|
|
620
|
+
'this graph keeps no history to read as of a transaction or branch: ' +
|
|
621
|
+
'list @yaks/journal with versions on',
|
|
622
|
+
)
|
|
623
|
+
}
|
|
624
|
+
if ('at' in mode) return moded.moded(sql, vocab, { ...o, history }, mode)
|
|
625
|
+
let m = moded.branchOf(sql, mode.branch)
|
|
626
|
+
if (!m) throw new Error(`no branch ${mode.branch}`)
|
|
627
|
+
return moded.moded(sql, vocab, { ...o, history }, m)
|
|
628
|
+
}
|
|
629
|
+
let { vocab, derived, backed } = plan.words
|
|
630
|
+
|
|
631
|
+
if (!opened) {
|
|
632
|
+
part?.('sqlite')
|
|
633
|
+
opened = await open()
|
|
634
|
+
part?.('migrations')
|
|
635
|
+
// A new file has no migration control yet; a writer adds it first.
|
|
636
|
+
if (!opts.readOnly) add(opened, migrationSchema())
|
|
637
|
+
} else part?.('migrations')
|
|
638
|
+
let sql = opened
|
|
639
|
+
let layout = bindStorage(sql, vocab)
|
|
640
|
+
// Facet imports may have awaited while another process announced work.
|
|
641
|
+
// Refuse again on the retained connection before domain schema or factories.
|
|
642
|
+
migrations(sql).ready()
|
|
643
|
+
part?.('host')
|
|
644
|
+
let blobs = sqliteBlobs(statements(sql), {}, opts.reader?.statements)
|
|
645
|
+
let chosen = plugins.find(([name]) => name == '@yaks/blob')?.[1].store as
|
|
646
|
+
| Backend
|
|
647
|
+
| undefined
|
|
648
|
+
let selected: { store?: Blobs; waiting?: string } = chosen
|
|
649
|
+
? backend(chosen, { blobs, files: fileBlobs })
|
|
650
|
+
: { store: artifactsAt(path) }
|
|
651
|
+
let artifacts = selected.store
|
|
652
|
+
if (!artifacts) throw new Error(`@yaks/blob: ${selected.waiting}`)
|
|
653
|
+
let store: Store | undefined
|
|
654
|
+
// What the file keeps beside the store's own schema over its component
|
|
655
|
+
// tables, the search indexes among them, made with a component's table by
|
|
656
|
+
// its first write (@yaks/sqlite `over`). Composed once the plugins say
|
|
657
|
+
// theirs, after the store is bound.
|
|
658
|
+
let over: Stmt[] = []
|
|
659
|
+
// Search is a property of the declaration: a property marked `search: true`
|
|
660
|
+
// is indexed, whoever declared it, so wiring @yaks/fts here rather than in
|
|
661
|
+
// a plugin is what takes the vocabulary at its word. Two things follow
|
|
662
|
+
// from that one list of properties — a bare word in any query compiles to a
|
|
663
|
+
// text match (one more clause compiler beside the plugins'), and `/mcp`
|
|
664
|
+
// lists a ranked `search` tool. A property marked `contains: true` keeps a
|
|
665
|
+
// substring index beside them, which a `~=` test of it reads.
|
|
666
|
+
let text = indexed(vocab)
|
|
667
|
+
// The search's halves where it searches at all (./searching.ts).
|
|
668
|
+
let hits: Served['hits']
|
|
669
|
+
let said: Served['said']
|
|
670
|
+
|
|
671
|
+
let host: Host
|
|
672
|
+
let g: Graph | undefined
|
|
673
|
+
let record = (bundles: Bundle[]) =>
|
|
674
|
+
persist(
|
|
675
|
+
() => g!.apply(bundles, { trusted: true }),
|
|
676
|
+
(error) =>
|
|
677
|
+
report(error, {
|
|
678
|
+
during: { process: selfEid() },
|
|
679
|
+
tags: { step: 'process write' },
|
|
680
|
+
}),
|
|
681
|
+
clock,
|
|
682
|
+
)
|
|
683
|
+
let finish = async (holder: Eid, end: Bundle) => {
|
|
684
|
+
for (;;) {
|
|
685
|
+
try {
|
|
686
|
+
return await record([...await released(g!, holder), end])
|
|
687
|
+
} catch (error) {
|
|
688
|
+
if (!(error instanceof Stale)) throw error
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
let endings = new Set<Promise<void>>()
|
|
693
|
+
let closed = false
|
|
694
|
+
let doing: Host['duties'] | undefined
|
|
695
|
+
let stopping = new AbortController()
|
|
696
|
+
let self = writer(vocab)
|
|
697
|
+
// The store's print, digested by the platform before the store is bound,
|
|
698
|
+
// so the schema below finds it made.
|
|
699
|
+
if (!opts.readOnly && !mode) await printing(layout, derived, !!sql.file)
|
|
700
|
+
// A web host warms when its server asks (@yaks/api `serve`): first what
|
|
701
|
+
// its routes make ahead of a first request, then, once that is made, the
|
|
702
|
+
// rest (`opts.warm`), so the first page answers before workers start.
|
|
703
|
+
// A make's failure is its route's to report; here it only settles.
|
|
704
|
+
let makes: (() => Promise<unknown>)[] = []
|
|
705
|
+
let warmed: Promise<void> | undefined
|
|
706
|
+
let warm = opts.warm &&
|
|
707
|
+
(() =>
|
|
708
|
+
warmed ??= Promise.allSettled(makes.map((make) => make()))
|
|
709
|
+
.then(() => opts.warm!()))
|
|
710
|
+
let early = opts.warm && ((make: () => Promise<unknown>) => {
|
|
711
|
+
if (warmed) void Promise.allSettled([make()])
|
|
712
|
+
else makes.push(make)
|
|
713
|
+
})
|
|
714
|
+
// Each branch's host, composed the first time a call names the branch:
|
|
715
|
+
// this config over a connection of its own on the branch, every plugin
|
|
716
|
+
// bound to it, so nothing it writes reaches main. Closed with this host,
|
|
717
|
+
// or when its branch is dropped.
|
|
718
|
+
let forks = new Map<number, Promise<Served>>()
|
|
719
|
+
let forked = (id: number): Promise<Served> => {
|
|
720
|
+
let made = forks.get(id)
|
|
721
|
+
if (made) return made
|
|
722
|
+
made = composed(config, ['graph'], load, {
|
|
723
|
+
mode: { branch: id },
|
|
724
|
+
process: false,
|
|
725
|
+
docs: opts.docs,
|
|
726
|
+
options: opts.options,
|
|
727
|
+
...opts.clock ? { clock: opts.clock } : {},
|
|
728
|
+
})
|
|
729
|
+
forks.set(id, made)
|
|
730
|
+
made.catch(() => forks.get(id) === made && forks.delete(id))
|
|
731
|
+
return made
|
|
732
|
+
}
|
|
733
|
+
let unfork = async (id: number) => {
|
|
734
|
+
let made = forks.get(id)
|
|
735
|
+
forks.delete(id)
|
|
736
|
+
await (await made?.catch(() => undefined))?.close()
|
|
737
|
+
}
|
|
738
|
+
let unforked = () => Promise.all([...forks.keys()].map(unfork))
|
|
739
|
+
let built = plan.bind({
|
|
740
|
+
operations: platformOperations,
|
|
741
|
+
options: (plugin, options) => {
|
|
742
|
+
let lent = opts.options?.(plugin, options) ?? options
|
|
743
|
+
return plugin == '@yaks/tracker' ? trackerOptions(lent) : lent
|
|
744
|
+
},
|
|
745
|
+
// A database in memory is its connection's alone, so no branch's host
|
|
746
|
+
// could open it.
|
|
747
|
+
branches: (h) => {
|
|
748
|
+
let kept = h.storage.branches
|
|
749
|
+
if (!kept || mode || path == ':memory:') return undefined
|
|
750
|
+
return {
|
|
751
|
+
make: async () => await kept.make(),
|
|
752
|
+
list: async (trash) => await kept.list(trash),
|
|
753
|
+
restore: async (id) => await kept.restore(id),
|
|
754
|
+
changes: async (id) => await kept.changes(id),
|
|
755
|
+
drop: async (id) => {
|
|
756
|
+
await unfork(id)
|
|
757
|
+
await kept.drop(id)
|
|
758
|
+
},
|
|
759
|
+
// A branch nobody made is the caller's mistake, said before any
|
|
760
|
+
// host is composed for it.
|
|
761
|
+
on: async (id) => {
|
|
762
|
+
if (
|
|
763
|
+
!forks.has(id) && !(await kept.list()).some((b) => b.id == id)
|
|
764
|
+
) {
|
|
765
|
+
throw new Refused(`no branch ${id}`)
|
|
766
|
+
}
|
|
767
|
+
return (await forked(id)).graph
|
|
768
|
+
},
|
|
769
|
+
}
|
|
770
|
+
},
|
|
771
|
+
unobserved: !!mode && 'branch' in mode,
|
|
772
|
+
tools: {
|
|
773
|
+
package: '@yaks/cli',
|
|
774
|
+
offer: (host) => [
|
|
775
|
+
...operations(host, { config: opts.path }),
|
|
776
|
+
feedbackTool(host),
|
|
777
|
+
...opts.tools?.(host) ?? [],
|
|
778
|
+
],
|
|
779
|
+
},
|
|
780
|
+
host: {
|
|
781
|
+
fetch: freshFetch(),
|
|
782
|
+
cwd: process.cwd,
|
|
783
|
+
tailText: disk.tailTextSync,
|
|
784
|
+
vault,
|
|
785
|
+
// A key given to the box can be kept in 1Password too
|
|
786
|
+
// (@yaks/connections `keep`), where this machine has `op`.
|
|
787
|
+
keepers: { '1password': opKeeper(vault) },
|
|
788
|
+
blobs,
|
|
789
|
+
artifacts,
|
|
790
|
+
reader: opts.reader,
|
|
791
|
+
warm,
|
|
792
|
+
early,
|
|
793
|
+
me: selfEid(),
|
|
794
|
+
stopping: stopping.signal,
|
|
795
|
+
report: (error, context = {}) =>
|
|
796
|
+
report(error, {
|
|
797
|
+
...context,
|
|
798
|
+
during: { process: selfEid(), ...context.during },
|
|
799
|
+
}),
|
|
800
|
+
duties: (signal, only) => {
|
|
801
|
+
if (!doing) throw new Error('the duties are not built yet')
|
|
802
|
+
return doing(signal, only)
|
|
803
|
+
},
|
|
804
|
+
end: (holder, why) => {
|
|
805
|
+
if (closed) return Promise.resolve()
|
|
806
|
+
let work = (async () => {
|
|
807
|
+
// A thread that failed before it wrote itself in holds nothing.
|
|
808
|
+
if (!self || !g || !(await g.get([holder])).length) return
|
|
809
|
+
if (!opts.readOnly) await host.runner.interrupt(why, holder)
|
|
810
|
+
await finish(holder, { entity: { eid: holder }, [EXIT]: {} })
|
|
811
|
+
})()
|
|
812
|
+
endings.add(work)
|
|
813
|
+
let done = () => endings.delete(work)
|
|
814
|
+
work.then(done, done)
|
|
815
|
+
return work
|
|
816
|
+
},
|
|
817
|
+
bury: async () => {
|
|
818
|
+
if (!self || !g) return []
|
|
819
|
+
let { store, vanished } = await supervisor()
|
|
820
|
+
let dead = await vanished(store(g), { me: selfEid() })
|
|
821
|
+
for (let b of dead) {
|
|
822
|
+
let pid = (b[PROCESS] as Comp | undefined)?.pid
|
|
823
|
+
await host.end(
|
|
824
|
+
b.entity.eid,
|
|
825
|
+
`interrupted: process ${pid} ended without closing`,
|
|
826
|
+
)
|
|
827
|
+
}
|
|
828
|
+
// A workspace command records its ending when it is looked at, and
|
|
829
|
+
// whoever waited on it may have stopped looking first.
|
|
830
|
+
if (host.workspaces) {
|
|
831
|
+
await overdue(
|
|
832
|
+
g,
|
|
833
|
+
host.workspaces,
|
|
834
|
+
(error, workspace) =>
|
|
835
|
+
report(error, {
|
|
836
|
+
during: { process: selfEid() },
|
|
837
|
+
tags: { step: 'workspace process look', workspace },
|
|
838
|
+
}),
|
|
839
|
+
)
|
|
840
|
+
}
|
|
841
|
+
return dead
|
|
842
|
+
},
|
|
843
|
+
gone: async (holder) => {
|
|
844
|
+
if (!self || !g) return false
|
|
845
|
+
let { gone, store } = await supervisor()
|
|
846
|
+
return await gone(store(g), holder, { me: selfEid() })
|
|
847
|
+
},
|
|
848
|
+
},
|
|
849
|
+
actor: self,
|
|
850
|
+
process: started({ roles: [...roles] })[PROCESS] as Comp,
|
|
851
|
+
readOnly: opts.readOnly,
|
|
852
|
+
...opts.clock ? { clock: opts.clock } : {},
|
|
853
|
+
storage: (built, extend, _words, history) => {
|
|
854
|
+
host = built
|
|
855
|
+
let o = {
|
|
856
|
+
derived,
|
|
857
|
+
backed,
|
|
858
|
+
extend,
|
|
859
|
+
number: numbering(vocab, config.numbers),
|
|
860
|
+
// A number given out before and kept as a name (@yaks/alias) is
|
|
861
|
+
// never given again.
|
|
862
|
+
...vocab.comp(ALIAS)
|
|
863
|
+
? {
|
|
864
|
+
names: (prefix: string, num: number) =>
|
|
865
|
+
aliasEid(format(prefix, num)),
|
|
866
|
+
}
|
|
867
|
+
: {},
|
|
868
|
+
history,
|
|
869
|
+
// A model turn reads its window again each step, a thousand lines
|
|
870
|
+
// of a long transcript: more than a Durable Object's 4 MiB keeps.
|
|
871
|
+
memory: 32 << 20,
|
|
872
|
+
over: () => over,
|
|
873
|
+
// What the store outlives and repairs, an entity no archetype
|
|
874
|
+
// names among it, is a bug all the same.
|
|
875
|
+
report: (error: Error) =>
|
|
876
|
+
report(error, { during: { process: selfEid() } }),
|
|
877
|
+
}
|
|
878
|
+
store = opening(o)
|
|
879
|
+
return store
|
|
880
|
+
},
|
|
881
|
+
// The text index follows plugin compilers, preserving their precedence.
|
|
882
|
+
extend: () => text.length ? [search(text, sql, filler(sql))] : [],
|
|
883
|
+
// One print covers everything this file needs: the store's tables, the
|
|
884
|
+
// ones beside them, the plugins' own and the text indexes. A file that
|
|
885
|
+
// recorded it opens with no DDL; one that did not adds what is missing.
|
|
886
|
+
// What the pass leaves undone (a table whose rows do not fit, a unique
|
|
887
|
+
// index they break) or a pass that fails is reported, and the graph
|
|
888
|
+
// opens with the shape it holds; the print stays unrecorded, so the
|
|
889
|
+
// next opener tries again.
|
|
890
|
+
// A mode's connection reads the file its writers keep in shape.
|
|
891
|
+
schema: opts.readOnly || mode ? undefined : (_built, stmts) => {
|
|
892
|
+
let beside = [...migrationSchema(), ...blobSchema(), ...stmts]
|
|
893
|
+
let indexes = textSchema(text, derived, layout)
|
|
894
|
+
over = [...beside, ...indexes]
|
|
895
|
+
let print = printOf(store!.print(), [...beside, ...indexes])
|
|
896
|
+
let told = (error: unknown) =>
|
|
897
|
+
void report(error, {
|
|
898
|
+
during: { process: selfEid() },
|
|
899
|
+
tags: { step: 'schema' },
|
|
900
|
+
})
|
|
901
|
+
let needs = plan.required.filter((p) => !required.includes(p))
|
|
902
|
+
let pass = () => {
|
|
903
|
+
try {
|
|
904
|
+
follow(sql, print, () => {
|
|
905
|
+
let left = [
|
|
906
|
+
...store!.install([...beside, ...indexes]),
|
|
907
|
+
...add(sql, beside),
|
|
908
|
+
]
|
|
909
|
+
if (text.length) {
|
|
910
|
+
adopt(sql, text, derived, { heal: false, fill: filler(sql) })
|
|
911
|
+
}
|
|
912
|
+
left.forEach(told)
|
|
913
|
+
return !left.length
|
|
914
|
+
})
|
|
915
|
+
} catch (error) {
|
|
916
|
+
told(error)
|
|
917
|
+
}
|
|
918
|
+
// Before this host writes a row, the store records each plugin
|
|
919
|
+
// this composition says the rows it writes will need.
|
|
920
|
+
try {
|
|
921
|
+
requires(sql, needs)
|
|
922
|
+
} catch (error) {
|
|
923
|
+
told(error)
|
|
924
|
+
}
|
|
925
|
+
// What a reader's first look at the file finds, kept for every
|
|
926
|
+
// opener after this one until the schema moves.
|
|
927
|
+
try {
|
|
928
|
+
survey(sql, layout, derived)
|
|
929
|
+
} catch (error) {
|
|
930
|
+
told(error)
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
// A graph in memory this process already made with this print is a
|
|
934
|
+
// copy of the first: hundreds of statements become a page copy.
|
|
935
|
+
let key = JSON.stringify([print, needs])
|
|
936
|
+
sql.template ? sql.template(key, pass) : pass()
|
|
937
|
+
},
|
|
938
|
+
effects: (built, disabled) => {
|
|
939
|
+
return effects(vocab, {
|
|
940
|
+
owes: roles.includes('effects') ? 'handled' : 'declared',
|
|
941
|
+
disabled,
|
|
942
|
+
write: (b) => built.graph.apply(b, { trusted: true }),
|
|
943
|
+
owner: built.me,
|
|
944
|
+
lease: config.lease,
|
|
945
|
+
gone: built.gone,
|
|
946
|
+
// How busy a worker says its process is (@yaks/effects worker).
|
|
947
|
+
cpu: () => {
|
|
948
|
+
let { user, system } = process.cpuUsage()
|
|
949
|
+
return (user + system) / 1000
|
|
950
|
+
},
|
|
951
|
+
nudge: opts.thread?.nudge,
|
|
952
|
+
// Runs another process commits wake this one's pool at once. Only
|
|
953
|
+
// a new run wakes it: every other commit on the box would set it
|
|
954
|
+
// reading for work it already has, several times a second.
|
|
955
|
+
watch: (heard) => {
|
|
956
|
+
let newest = opts.reader?.storage?.newest
|
|
957
|
+
return newest
|
|
958
|
+
? watchRevision(
|
|
959
|
+
() => newest(EFFECT),
|
|
960
|
+
heard,
|
|
961
|
+
(error) =>
|
|
962
|
+
void report(error, { tags: { step: 'watch effects' } }),
|
|
963
|
+
clock,
|
|
964
|
+
)
|
|
965
|
+
: store?.watch?.(EFFECT, heard) ?? (() => {})
|
|
966
|
+
},
|
|
967
|
+
report: (error, job) =>
|
|
968
|
+
void report(error, {
|
|
969
|
+
during: {
|
|
970
|
+
...job.event ? { entity: job.event.entity.eid } : {},
|
|
971
|
+
process: built.me,
|
|
972
|
+
},
|
|
973
|
+
tags: { handler: job.handler },
|
|
974
|
+
}),
|
|
975
|
+
})
|
|
976
|
+
},
|
|
977
|
+
ready: (built) => {
|
|
978
|
+
host = built
|
|
979
|
+
g = built.graph
|
|
980
|
+
Object.assign(built, delivery(built))
|
|
981
|
+
// Read workers return their recorded trees to their caller rather than
|
|
982
|
+
// duplicating delivery. A passing CLI and every duty/web process observe
|
|
983
|
+
// their graph, without composing tracker tables into the watched store.
|
|
984
|
+
// An unconfigured private memory graph has no telemetry destination
|
|
985
|
+
// or PID-local capture sidecar. Keeping its channel subscribed would
|
|
986
|
+
// build and discard a complete tree for every startup/catalogue write.
|
|
987
|
+
if (opts.process !== false && (config.tracker || path != ':memory:')) {
|
|
988
|
+
captureControl = config.tracker ? traceControl(path) : undefined
|
|
989
|
+
recorded = trace!.traces(g, {
|
|
990
|
+
getStart: getStarts(path),
|
|
991
|
+
process: selfEid(),
|
|
992
|
+
take: captureControl?.take ??
|
|
993
|
+
(() => Promise.resolve({ requested: false, rate: 0 })),
|
|
994
|
+
sink: config.tracker
|
|
995
|
+
? spool(files(config.tracker.spool).append)
|
|
996
|
+
: undefined,
|
|
997
|
+
})
|
|
998
|
+
}
|
|
999
|
+
built.workspaces = workspaceCapabilities(built, config)
|
|
1000
|
+
built.sandboxes = sandboxCapabilities(built, config)
|
|
1001
|
+
built.resolveHome = resolveHome(built, config)
|
|
1002
|
+
},
|
|
1003
|
+
search: (built) => {
|
|
1004
|
+
part?.('search')
|
|
1005
|
+
if (!wordFields(text).length) return undefined
|
|
1006
|
+
hits = (words, count) => find(sql, text, words, { limit: count })
|
|
1007
|
+
said = async (words, found, limit) => {
|
|
1008
|
+
let people = await peopleSearch(built.graph, words, limit)
|
|
1009
|
+
let ranked = merged(found.words, found.meaning, limit)
|
|
1010
|
+
let hits = new Map(ranked.map((h) => [h.entity, h]))
|
|
1011
|
+
let named = new Set(people.map((h) => h.entity))
|
|
1012
|
+
for (let h of people) {
|
|
1013
|
+
let ranked = hits.get(h.entity)
|
|
1014
|
+
hits.set(h.entity, {
|
|
1015
|
+
...h,
|
|
1016
|
+
score: ranked?.score ?? 0,
|
|
1017
|
+
snippet: h.snippet || ranked?.snippet || '',
|
|
1018
|
+
source: ranked?.source == 'meaning'
|
|
1019
|
+
? 'both'
|
|
1020
|
+
: ranked?.source ?? 'text',
|
|
1021
|
+
})
|
|
1022
|
+
}
|
|
1023
|
+
let ordered = [...new Set([...named, ...hits.keys()])]
|
|
1024
|
+
let rows = await built.graph.get(ordered)
|
|
1025
|
+
let hidden = unfound(vocab).not ?? []
|
|
1026
|
+
let at = new Map(rows.map((b) => [b.entity.eid, b]))
|
|
1027
|
+
// A snippet that says only the title says what the hit line
|
|
1028
|
+
// already does; the row's other text says what it is.
|
|
1029
|
+
let line = hit(vocab, split(words))
|
|
1030
|
+
return ordered.flatMap((eid) => {
|
|
1031
|
+
let b = at.get(eid), h = hits.get(eid)!
|
|
1032
|
+
if (
|
|
1033
|
+
!b || named.has(eid) && (!b.person || hidden.some((c) => b[c]))
|
|
1034
|
+
) return []
|
|
1035
|
+
return [{
|
|
1036
|
+
entity: b.entity,
|
|
1037
|
+
hit: {
|
|
1038
|
+
...line(b, h.snippet),
|
|
1039
|
+
source: h.source,
|
|
1040
|
+
...(b.entry && typeof b.entry == 'object' &&
|
|
1041
|
+
'session' in b.entry && typeof b.entry.session == 'string'
|
|
1042
|
+
? {
|
|
1043
|
+
session: b.entry.session,
|
|
1044
|
+
speaker: b.output ? 'output' : 'input',
|
|
1045
|
+
}
|
|
1046
|
+
: {}),
|
|
1047
|
+
},
|
|
1048
|
+
}]
|
|
1049
|
+
}).slice(0, limit)
|
|
1050
|
+
}
|
|
1051
|
+
return searching(
|
|
1052
|
+
async (words, count, semantic) => ({
|
|
1053
|
+
words: hits!(words, count),
|
|
1054
|
+
meaning: await built.meaning?.(semantic, { limit: count }) ?? [],
|
|
1055
|
+
}),
|
|
1056
|
+
said,
|
|
1057
|
+
)
|
|
1058
|
+
},
|
|
1059
|
+
})
|
|
1060
|
+
host = built.host
|
|
1061
|
+
g = host.graph
|
|
1062
|
+
let worded: import('@yaks/host').Host['text']
|
|
1063
|
+
host.text = async (answer, tool, on) => {
|
|
1064
|
+
worded ??= (await import('./text.js')).text(host)
|
|
1065
|
+
return worded(answer, tool, on)
|
|
1066
|
+
}
|
|
1067
|
+
// A numbered graph draws each eid in an answer's prose as the entity's
|
|
1068
|
+
// id, from the small series it holds in memory and a read of the rest. A
|
|
1069
|
+
// read that failed is reported, and the answer drawn with what it holds.
|
|
1070
|
+
if (config.numbers) {
|
|
1071
|
+
let draw: ((answer: Bundle[]) => Promise<Bundle[]>) | undefined
|
|
1072
|
+
host.readable = async (answer) => {
|
|
1073
|
+
if (!draw) {
|
|
1074
|
+
let [{ held }, { drawing }] = await Promise.all([
|
|
1075
|
+
import('@yaks/id/graph'),
|
|
1076
|
+
import('@yaks/tools/readable'),
|
|
1077
|
+
])
|
|
1078
|
+
let small = held(vocab, host.graph)
|
|
1079
|
+
draw = drawing(
|
|
1080
|
+
vocab,
|
|
1081
|
+
(eids) =>
|
|
1082
|
+
small(eids).catch((error) => {
|
|
1083
|
+
report(error)
|
|
1084
|
+
return () => undefined
|
|
1085
|
+
}),
|
|
1086
|
+
opts.reader?.readable,
|
|
1087
|
+
)
|
|
1088
|
+
}
|
|
1089
|
+
return draw(answer)
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
let fx = host.fx
|
|
1093
|
+
// A read-only host commits nothing, so it owes no effects to work, even
|
|
1094
|
+
// when its roles name them (a graph with no pool runs them where they
|
|
1095
|
+
// are committed).
|
|
1096
|
+
let effecting = roles.includes('effects') && !opts.readOnly
|
|
1097
|
+
// Whoever works the effects also works off the schema work a pass owed
|
|
1098
|
+
// (@yaks/sqlite/owe), one process at a time under its lease, and does the
|
|
1099
|
+
// file's upkeep: its statistics, and once a day a check that each text
|
|
1100
|
+
// index holds every row, refilling in windows one that drifted. Between
|
|
1101
|
+
// them it makes the pieces of the texts longer than a substring index
|
|
1102
|
+
// holds (@yaks/fts `pieces`), a window each.
|
|
1103
|
+
let upkeep: Served['upkeep'] = (signal, report) =>
|
|
1104
|
+
tend(sql, {
|
|
1105
|
+
signal,
|
|
1106
|
+
daily: text.length
|
|
1107
|
+
? [{
|
|
1108
|
+
name: 'heal',
|
|
1109
|
+
run: (d) => void heal(d, text, { derived, fill: filler(d) }),
|
|
1110
|
+
}]
|
|
1111
|
+
: [],
|
|
1112
|
+
chores: text.length
|
|
1113
|
+
? [{ name: 'pieces', run: (d) => pieces(d, text, derived) }]
|
|
1114
|
+
: [],
|
|
1115
|
+
report,
|
|
1116
|
+
})
|
|
1117
|
+
let tending: Served['upkeep'] = sql.file
|
|
1118
|
+
? (signal, tell) => upkeepThread(config, signal, tell)
|
|
1119
|
+
: upkeep
|
|
1120
|
+
let duties = effecting
|
|
1121
|
+
? [...built.services, {
|
|
1122
|
+
name: SCHEMA_DUTY,
|
|
1123
|
+
run: (signal: AbortSignal) =>
|
|
1124
|
+
tending(
|
|
1125
|
+
signal,
|
|
1126
|
+
(error, work) =>
|
|
1127
|
+
void report(error, {
|
|
1128
|
+
during: { process: selfEid() },
|
|
1129
|
+
tags: { step: 'schema', work },
|
|
1130
|
+
}),
|
|
1131
|
+
),
|
|
1132
|
+
}, {
|
|
1133
|
+
name: '@yaks/effects',
|
|
1134
|
+
run: (signal: AbortSignal) =>
|
|
1135
|
+
expiration(host.graph, {
|
|
1136
|
+
owner: selfEid(),
|
|
1137
|
+
signal,
|
|
1138
|
+
report: (error) =>
|
|
1139
|
+
void report(error, {
|
|
1140
|
+
during: { process: selfEid() },
|
|
1141
|
+
tags: { step: 'expiration' },
|
|
1142
|
+
}),
|
|
1143
|
+
}),
|
|
1144
|
+
}]
|
|
1145
|
+
: built.services
|
|
1146
|
+
// Started together and stopped together, by one signal: a host shutting
|
|
1147
|
+
// down is one fact, and a duty that outlived the database it
|
|
1148
|
+
// reads would be a crash nobody asked for. One that throws is reported
|
|
1149
|
+
// and that plugin's duty stops — the others keep going, the way a failing
|
|
1150
|
+
// effect is telemetry rather than a broken host. The duties this host
|
|
1151
|
+
// handed to a thread of its own run beside these, under the same signal.
|
|
1152
|
+
// A config that turned them off (`duties: false`) takes no lease, works
|
|
1153
|
+
// no effects and runs none of them, in either form. The CLI's
|
|
1154
|
+
// `--no-duties` disables inline duties; web still restores external workers:
|
|
1155
|
+
// what it commits is left written down for a process that does.
|
|
1156
|
+
//
|
|
1157
|
+
// A thread gone without closing — ended where it stood, or failed — ran in
|
|
1158
|
+
// this process's pid, which goes on, so nobody can tell it is over until
|
|
1159
|
+
// this host writes its ending: once, however many of its promises say so.
|
|
1160
|
+
let ending: Promise<void> | undefined
|
|
1161
|
+
let lose = (error: unknown) => {
|
|
1162
|
+
void report(error, {
|
|
1163
|
+
during: { process: host.me },
|
|
1164
|
+
tags: { step: 'duty thread' },
|
|
1165
|
+
})
|
|
1166
|
+
return ending ??= host.end(
|
|
1167
|
+
opts.thread!.me,
|
|
1168
|
+
'interrupted: its thread ended before it closed',
|
|
1169
|
+
).catch((e) => report(e, { tags: { step: 'end duty thread' } }))
|
|
1170
|
+
}
|
|
1171
|
+
let dutiesHost = serveRoles(g, {
|
|
1172
|
+
me: selfEid(),
|
|
1173
|
+
roles: [...effecting ? ['effects'] : [], ...duties.map((d) => d.name)],
|
|
1174
|
+
fx: effecting ? fx : undefined,
|
|
1175
|
+
services: Object.fromEntries(duties.map((d) => [d.name, d.run])),
|
|
1176
|
+
hold: config.lease,
|
|
1177
|
+
gone: host.gone,
|
|
1178
|
+
report: (error, role) =>
|
|
1179
|
+
report(error, {
|
|
1180
|
+
during: { process: host.me },
|
|
1181
|
+
tags: role == 'effects' ? { step: 'effects' } : { role },
|
|
1182
|
+
}),
|
|
1183
|
+
})
|
|
1184
|
+
doing = config.duties == false ? async () => {} : (signal, only) => {
|
|
1185
|
+
let until = signal ?? stopping.signal
|
|
1186
|
+
return Promise.all([
|
|
1187
|
+
...(opts.thread ? [opts.thread.duties(until).catch(lose)] : []),
|
|
1188
|
+
dutiesHost.duties(until, only),
|
|
1189
|
+
]).then(() => {})
|
|
1190
|
+
}
|
|
1191
|
+
// A draining process is alive but no longer covers its duty roles. Publish
|
|
1192
|
+
// that before waiting for in-flight steps, so the web can replace it.
|
|
1193
|
+
let retiring: Promise<unknown> | undefined
|
|
1194
|
+
let retire = () =>
|
|
1195
|
+
retiring ??= self && opts.process !== false && !opts.readOnly
|
|
1196
|
+
? Promise.resolve(record([{
|
|
1197
|
+
entity: { eid: selfEid() },
|
|
1198
|
+
process: {
|
|
1199
|
+
roles: roles.filter((role) => role == 'graph' || role == 'web'),
|
|
1200
|
+
},
|
|
1201
|
+
}])).catch((error) =>
|
|
1202
|
+
report(error, { tags: { step: 'retire duties' } })
|
|
1203
|
+
)
|
|
1204
|
+
: Promise.resolve()
|
|
1205
|
+
// Its effects, drained once: begun by the first of `stop` and `close`,
|
|
1206
|
+
// and awaited by `close`.
|
|
1207
|
+
let draining: Promise<void> | undefined
|
|
1208
|
+
let drained = () => draining ??= drain(fx, (line) => console.error(line))
|
|
1209
|
+
let closing: Promise<void> | undefined
|
|
1210
|
+
// This process, written in, with the roles it serves. First among the
|
|
1211
|
+
// writes, because everything after is attributed to it and `created.by`
|
|
1212
|
+
// is a reference: a process attributing writes to an entity nothing
|
|
1213
|
+
// created would store a dangling id on its very first write. What a
|
|
1214
|
+
// process starting owes is owed when it starts working the effects
|
|
1215
|
+
// (@yaks/effects `start`), not here.
|
|
1216
|
+
part?.('process')
|
|
1217
|
+
if (self && opts.process !== false && !opts.readOnly) {
|
|
1218
|
+
await record([started({ roles: [...roles] })])
|
|
1219
|
+
}
|
|
1220
|
+
let lifecycle = {
|
|
1221
|
+
storage: g.storage as Served['storage'],
|
|
1222
|
+
upkeep,
|
|
1223
|
+
graph: g,
|
|
1224
|
+
fx,
|
|
1225
|
+
hits,
|
|
1226
|
+
said,
|
|
1227
|
+
// The last transaction, then the file: every lease this process holds
|
|
1228
|
+
// released, and its ending stamped. One transaction, because they are
|
|
1229
|
+
// one fact — a process that is over is not doing any work, and the next
|
|
1230
|
+
// process to ask should not have to wait out a lease nobody is using.
|
|
1231
|
+
// Last, because an absent `exit` is what running means, so a process
|
|
1232
|
+
// that closed without stamping one reads as still running forever.
|
|
1233
|
+
//
|
|
1234
|
+
// First of all, the abort: the duties stop and every timer a
|
|
1235
|
+
// plugin hung off {@link Host.stopping} is cancelled, so nothing is
|
|
1236
|
+
// still pending over a database that is about to be closed. It leaves
|
|
1237
|
+
// the pool, so what it commits from then on, its last transaction
|
|
1238
|
+
// included, is left written down for another, and the effects it
|
|
1239
|
+
// started are let finish, for as long as each takes (./drain.ts). Then
|
|
1240
|
+
// the thread beside it closes, draining the same way. A thread that does
|
|
1241
|
+
// not close, this host writes the ending of. The calls this process was
|
|
1242
|
+
// still running are ended first, as interrupted, so none is left to
|
|
1243
|
+
// lapse.
|
|
1244
|
+
stop: () => {
|
|
1245
|
+
void retire()
|
|
1246
|
+
stopping.abort()
|
|
1247
|
+
dutiesHost.stop()
|
|
1248
|
+
if (effecting) void drained()
|
|
1249
|
+
},
|
|
1250
|
+
close: (code?: number) =>
|
|
1251
|
+
closing ??= (async () => {
|
|
1252
|
+
let wait = async <T>(name: string, work: () => T | Promise<T>) => {
|
|
1253
|
+
let cancel = clock.after(
|
|
1254
|
+
5000,
|
|
1255
|
+
() => console.error(`host drain — ${name}`),
|
|
1256
|
+
)
|
|
1257
|
+
try {
|
|
1258
|
+
return await work()
|
|
1259
|
+
} finally {
|
|
1260
|
+
cancel()
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
await wait('retire', retire)
|
|
1264
|
+
await wait('branches', unforked)
|
|
1265
|
+
stopping.abort()
|
|
1266
|
+
dutiesHost.stop()
|
|
1267
|
+
await wait('duty thread', () => opts.thread?.close().catch(lose))
|
|
1268
|
+
await wait('thread ending', () => ending)
|
|
1269
|
+
if (effecting) await wait('effects', drained)
|
|
1270
|
+
await wait('duties', () => dutiesHost.close())
|
|
1271
|
+
await wait('plugins', () => cleanup('dispose plugins', built.close))
|
|
1272
|
+
let shut = async () => {
|
|
1273
|
+
await wait('capture', () =>
|
|
1274
|
+
cleanup('close capture', () => captureControl?.close()))
|
|
1275
|
+
await wait('traces', () =>
|
|
1276
|
+
cleanup('close traces', () =>
|
|
1277
|
+
recorded?.close()))
|
|
1278
|
+
// A forced ending can arrive while graceful shutdown drains. Stop
|
|
1279
|
+
// admitting endings and finish those already admitted before SQL
|
|
1280
|
+
// closes; a late repeat has nothing left to release. An ending
|
|
1281
|
+
// reads through the reader, so it closes after them.
|
|
1282
|
+
closed = true
|
|
1283
|
+
await wait('endings', () =>
|
|
1284
|
+
Promise.allSettled([...endings]))
|
|
1285
|
+
await wait('reader', () =>
|
|
1286
|
+
cleanup('close reader', () =>
|
|
1287
|
+
opts.reader?.close?.()))
|
|
1288
|
+
await wait('storage', () =>
|
|
1289
|
+
cleanup('close storage', () =>
|
|
1290
|
+
sql.close()))
|
|
1291
|
+
unfault()
|
|
1292
|
+
await wait('reports', () =>
|
|
1293
|
+
report.close())
|
|
1294
|
+
}
|
|
1295
|
+
// A call whose tool has not returned by now never will in this
|
|
1296
|
+
// process. Ended as interrupted, rather than left claimed for a sweep
|
|
1297
|
+
// to run again in a process nobody asked — a server, a tail, a land in
|
|
1298
|
+
// somebody else's checkout.
|
|
1299
|
+
if (!opts.readOnly) {
|
|
1300
|
+
await wait('interrupt calls', () =>
|
|
1301
|
+
built.interrupt(
|
|
1302
|
+
`interrupted: this process ended${
|
|
1303
|
+
code == null ? '' : ` with code ${code}`
|
|
1304
|
+
} before the tool returned`,
|
|
1305
|
+
).catch((e) =>
|
|
1306
|
+
report(e, { tags: { step: 'interrupt calls' } })
|
|
1307
|
+
))
|
|
1308
|
+
}
|
|
1309
|
+
if (!self || opts.process === false || opts.readOnly) {
|
|
1310
|
+
return shut()
|
|
1311
|
+
}
|
|
1312
|
+
try {
|
|
1313
|
+
await wait('process ending', () => finish(selfEid(), ended(code)))
|
|
1314
|
+
} catch (e) {
|
|
1315
|
+
await report(e, { tags: { step: 'close host' } })
|
|
1316
|
+
}
|
|
1317
|
+
await shut()
|
|
1318
|
+
})(),
|
|
1319
|
+
}
|
|
1320
|
+
// Spreading Host would invoke its lazy tools/runner getters here.
|
|
1321
|
+
return Object.defineProperties({}, {
|
|
1322
|
+
...Object.getOwnPropertyDescriptors(host),
|
|
1323
|
+
...Object.getOwnPropertyDescriptors(lifecycle),
|
|
1324
|
+
}) as Served
|
|
1325
|
+
} catch (error) {
|
|
1326
|
+
try {
|
|
1327
|
+
await report(error, { tags: { step: 'compose host' } })
|
|
1328
|
+
await cleanup('close capture', () => captureControl?.close())
|
|
1329
|
+
await cleanup('close traces', () => recorded?.close())
|
|
1330
|
+
await cleanup('close storage', () => opened?.close())
|
|
1331
|
+
} finally {
|
|
1332
|
+
unfault()
|
|
1333
|
+
await report.close()
|
|
1334
|
+
}
|
|
1335
|
+
throw error
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
export type { Reader } from './reader.js'
|
|
1340
|
+
export let reader = async (
|
|
1341
|
+
...args: Parameters<typeof import('./reader.js').reader>
|
|
1342
|
+
): Promise<import('./reader.js').Reader> =>
|
|
1343
|
+
(await import('./reader.js')).reader(...args)
|