@yaks/effects 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 +979 -2
- package/_dist/docs.d.ts +10 -0
- package/_dist/docs.d.ts.map +1 -0
- package/_dist/enqueue.d.ts +12 -0
- package/_dist/enqueue.d.ts.map +1 -0
- package/_dist/expire.d.ts +31 -0
- package/_dist/expire.d.ts.map +1 -0
- package/_dist/invocation.d.ts +36 -0
- package/_dist/invocation.d.ts.map +1 -0
- package/_dist/lease.d.ts +98 -0
- package/_dist/lease.d.ts.map +1 -0
- package/_dist/mod.d.ts +89 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/outage.d.ts +48 -0
- package/_dist/outage.d.ts.map +1 -0
- package/_dist/pool.d.ts +148 -0
- package/_dist/pool.d.ts.map +1 -0
- package/_dist/provisional.d.ts +8 -0
- package/_dist/provisional.d.ts.map +1 -0
- package/_dist/ready.d.ts +3 -0
- package/_dist/ready.d.ts.map +1 -0
- package/_dist/registration.d.ts +17 -0
- package/_dist/registration.d.ts.map +1 -0
- package/_dist/registry.d.ts +153 -0
- package/_dist/registry.d.ts.map +1 -0
- package/_dist/tools.d.ts +21 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/trace.d.ts +63 -0
- package/_dist/trace.d.ts.map +1 -0
- package/_dist/views.d.ts +15 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +11 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/worker.d.ts +19 -0
- package/_dist/worker.d.ts.map +1 -0
- package/_dist/write.d.ts +25 -0
- package/_dist/write.d.ts.map +1 -0
- package/cleanup.js +75 -0
- package/cleanup.js.map +1 -0
- package/cleanup.ts +83 -0
- package/deno.json +1 -0
- package/docs.js +13 -0
- package/docs.js.map +1 -0
- package/docs.ts +13 -0
- package/enqueue.js +71 -0
- package/enqueue.js.map +1 -0
- package/enqueue.ts +86 -0
- package/expire.js +225 -0
- package/expire.js.map +1 -0
- package/expire.ts +244 -0
- package/invocation.js +245 -0
- package/invocation.js.map +1 -0
- package/invocation.ts +250 -0
- package/lease.js +343 -0
- package/lease.js.map +1 -0
- package/lease.ts +444 -0
- package/mod.js +89 -0
- package/mod.js.map +1 -0
- package/mod.ts +92 -0
- package/outage.js +77 -0
- package/outage.js.map +1 -0
- package/outage.ts +84 -0
- package/package.json +52 -4
- package/page.js +315 -0
- package/page.js.map +1 -0
- package/page.ts +524 -0
- package/pool.js +1474 -0
- package/pool.js.map +1 -0
- package/pool.ts +1635 -0
- package/provisional.js +20 -0
- package/provisional.js.map +1 -0
- package/provisional.ts +26 -0
- package/ready.js +48 -0
- package/ready.js.map +1 -0
- package/ready.ts +93 -0
- package/registration.js +21 -0
- package/registration.js.map +1 -0
- package/registration.ts +43 -0
- package/registry.js +872 -0
- package/registry.js.map +1 -0
- package/registry.ts +1196 -0
- package/tools.js +182 -0
- package/tools.js.map +1 -0
- package/tools.ts +229 -0
- package/trace.js +167 -0
- package/trace.js.map +1 -0
- package/trace.ts +217 -0
- package/views.js +18 -0
- package/views.js.map +1 -0
- package/views.ts +18 -0
- package/vocab.js +18 -0
- package/vocab.js.map +1 -0
- package/vocab.json +364 -0
- package/vocab.ts +20 -0
- package/worker.js +114 -0
- package/worker.js.map +1 -0
- package/worker.ts +146 -0
- package/write.js +38 -0
- package/write.js.map +1 -0
- package/write.ts +60 -0
package/tools.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// The tool an agent can call here: the module exported as
|
|
2
|
+
// `@yaks/effects/tools`, holding the implementation behind the `effect_check`
|
|
3
|
+
// declaration in ./vocab.json. One tool, and it is a check — a tool whose verb
|
|
4
|
+
// is `check`, which is all a "doctor" command is (@yaks/tools ./check.ts).
|
|
5
|
+
//
|
|
6
|
+
// The pool is written for exactly this. ./pool.ts retries a run that did not
|
|
7
|
+
// complete, backing off between attempts, and when its last attempt is spent
|
|
8
|
+
// marks the row `failed` with the error beside it and leaves it for a person.
|
|
9
|
+
// Nothing in that sequence tells the person. This does.
|
|
10
|
+
//
|
|
11
|
+
// The other half is a row that never got that far: a run is written pending
|
|
12
|
+
// by the commit that owes it, so a row that has been waiting since long before
|
|
13
|
+
// it came due, with no claim a worker renews, means nobody is working the
|
|
14
|
+
// pool — no process serves the effects role, or none of them handles that
|
|
15
|
+
// effect. That failure is invisible from every other angle: the writes
|
|
16
|
+
// commit, the graph looks normal, the mail just never goes out.
|
|
17
|
+
//
|
|
18
|
+
// And the person it tells can act: `effect_retry` puts failed runs back to
|
|
19
|
+
// pending for the pool to work again, `effect_drop` deletes the ones whose work
|
|
20
|
+
// no one owes any more. Both write the pool's own way — a trusted batch guarded
|
|
21
|
+
// by the state they read — so a run a worker has since touched is left alone.
|
|
22
|
+
//
|
|
23
|
+
// A graph with no `effect` component keeps no pool, which is a legitimate
|
|
24
|
+
// configuration (effects run where they were committed, nothing written down)
|
|
25
|
+
// and not a fault — the check reports that and finds nothing.
|
|
26
|
+
import { and, eq, list } from "@yaks/query";
|
|
27
|
+
import { addressed, argsOf, who } from "@yaks/graph/call";
|
|
28
|
+
import { signed } from "@yaks/graph/provenance";
|
|
29
|
+
import { token } from "@yaks/graph/sha256";
|
|
30
|
+
import { human } from "@yaks/id";
|
|
31
|
+
import { CallError } from "@yaks/tools/errors";
|
|
32
|
+
import { checked } from "@yaks/tools/check";
|
|
33
|
+
import { said } from "@yaks/tools/result";
|
|
34
|
+
import { effectsIn } from "@yaks/vocab";
|
|
35
|
+
import { effectRetry } from "./enqueue.js";
|
|
36
|
+
import { EFFECT } from "./pool.js";
|
|
37
|
+
import { firstPending } from "./ready.js";
|
|
38
|
+
let MINUTES = 10;
|
|
39
|
+
let SAMPLE = 5;
|
|
40
|
+
let comp = (b)=>b[EFFECT];
|
|
41
|
+
// A few names, and how many more there were: a list of six hundred stuck rows
|
|
42
|
+
// is not more actionable than a list of five.
|
|
43
|
+
let some = (rows, id, sample)=>{
|
|
44
|
+
let shown = rows.slice(0, sample).map(id).join(', ');
|
|
45
|
+
let rest = rows.length - Math.min(rows.length, sample);
|
|
46
|
+
return `${shown}${rest > 0 ? `, and ${rest} more` : ''}`;
|
|
47
|
+
};
|
|
48
|
+
// A run is a person's to settle when it spent its attempts, or when it waits
|
|
49
|
+
// for a handler no plugin declares any more, which no process will ever run.
|
|
50
|
+
// Retrying either kind of run only strands it again unless its handler exists.
|
|
51
|
+
let dead = (vocab)=>{
|
|
52
|
+
let declared = new Set(effectsIn(vocab.docs).map((e)=>e.name));
|
|
53
|
+
let state = (b)=>comp(b)?.state;
|
|
54
|
+
let known = (b)=>declared.has(String(comp(b)?.handler));
|
|
55
|
+
let orphan = (b)=>state(b) == 'pending' && !known(b);
|
|
56
|
+
return {
|
|
57
|
+
orphan,
|
|
58
|
+
retry: (b)=>state(b) == 'failed' && known(b),
|
|
59
|
+
drop: (b)=>state(b) == 'failed' || orphan(b)
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
// The runs a handler name or a single run's id points at, among those `owed`.
|
|
63
|
+
let named = async (graph, of, owed)=>{
|
|
64
|
+
// Done runs are left out by the query: a handler's history can be long.
|
|
65
|
+
let byHandler = (await graph.read(and(eq(`${EFFECT}.handler`, of), eq(`${EFFECT}.state`, list('failed', 'pending'))))).filter(owed);
|
|
66
|
+
if (byHandler.length) return byHandler;
|
|
67
|
+
let [eid] = await addressed(graph, [
|
|
68
|
+
of
|
|
69
|
+
]);
|
|
70
|
+
let [row] = await graph.get([
|
|
71
|
+
eid
|
|
72
|
+
]);
|
|
73
|
+
return row && owed(row) ? [
|
|
74
|
+
row
|
|
75
|
+
] : [];
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* The write that retries (failed runs back to pending with a fresh allowance)
|
|
79
|
+
* or drops (failed runs, and pending ones no handler is declared for, deleted)
|
|
80
|
+
* the runs `of` names: a handler, or one run's id. Each change is guarded by
|
|
81
|
+
* the state read, so a run a worker has since touched is left alone. Empty
|
|
82
|
+
* when `of` names none. What `effect_retry` and `effect_drop` apply, and what
|
|
83
|
+
* a host without those tools applies the same way (yaks.app's
|
|
84
|
+
* `store_effect_retry`).
|
|
85
|
+
*/ export let settling = async (graph, vocab, act, of)=>{
|
|
86
|
+
let rows = await named(graph, of, dead(vocab)[act]);
|
|
87
|
+
return rows.map((b)=>{
|
|
88
|
+
let state = String(comp(b)?.state);
|
|
89
|
+
return act == 'retry' ? effectRetry(b.entity.eid, state) : {
|
|
90
|
+
entity: {
|
|
91
|
+
eid: b.entity.eid
|
|
92
|
+
},
|
|
93
|
+
$delete: true,
|
|
94
|
+
$was: {
|
|
95
|
+
[EFFECT]: {
|
|
96
|
+
state: token(state)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
// Settle the runs named by the call, and say how many.
|
|
103
|
+
let settle = (host, act, done)=>async (call, graph)=>{
|
|
104
|
+
let of = String(argsOf(call).of);
|
|
105
|
+
let bundles = await settling(graph, host.vocab, act, of);
|
|
106
|
+
if (!bundles.length) {
|
|
107
|
+
throw new CallError('refused', `${of} names no effect run to ${act}`);
|
|
108
|
+
}
|
|
109
|
+
await graph.apply(signed(bundles, who(call)), {
|
|
110
|
+
trusted: true
|
|
111
|
+
});
|
|
112
|
+
return [
|
|
113
|
+
said(call, `${done} ${bundles.length} run(s) of ${of}`)
|
|
114
|
+
];
|
|
115
|
+
};
|
|
116
|
+
/** The implementation behind the tool ./vocab.json declares. */ export let runs = (host, options = {})=>{
|
|
117
|
+
let settles = dead(host.vocab);
|
|
118
|
+
let ordered = new Set(effectsIn(host.vocab.docs).filter((e)=>e.fifo).map((e)=>e.name));
|
|
119
|
+
return {
|
|
120
|
+
effect_retry: settle(host, 'retry', 'retried'),
|
|
121
|
+
effect_drop: settle(host, 'drop', 'dropped'),
|
|
122
|
+
effect_check: async (call, graph)=>{
|
|
123
|
+
let about = 'every effect run reached an end somebody would hear about';
|
|
124
|
+
if (!host.vocab.comp(EFFECT)) {
|
|
125
|
+
// Not a fault: an application that runs its effects where it commits
|
|
126
|
+
// them loads no `effect` component, so there is nothing written down to
|
|
127
|
+
// fall behind on.
|
|
128
|
+
return checked(call.entity.eid, about, []);
|
|
129
|
+
}
|
|
130
|
+
let id = human(host.vocab);
|
|
131
|
+
let sample = options.sample ?? SAMPLE;
|
|
132
|
+
let cutoff = Date.now() - (options.minutes ?? MINUTES) * 60_000;
|
|
133
|
+
let failed = await graph.read(and(eq(`${EFFECT}.state`, 'failed')));
|
|
134
|
+
let pending = await graph.read(and(eq(`${EFFECT}.state`, 'pending')));
|
|
135
|
+
let first = new Set(await Promise.all([
|
|
136
|
+
...ordered
|
|
137
|
+
].map(async (name)=>(await firstPending(graph, name))?.entity.eid)));
|
|
138
|
+
let orphans = pending.filter(settles.orphan);
|
|
139
|
+
let overdue = pending.filter((b)=>!settles.orphan(b)).filter((b)=>{
|
|
140
|
+
// Since when it has been waiting: a failure that reported is owed its
|
|
141
|
+
// next run at `next`, not at the instant the run was first written
|
|
142
|
+
// down — a backoff is not a symptom. A claim its worker still renews
|
|
143
|
+
// is a run in progress, however long ago it was owed: a transcript's
|
|
144
|
+
// run lasts as long as the transcript does.
|
|
145
|
+
let row = comp(b);
|
|
146
|
+
let at = Date.parse(String(row?.next ?? row?.at ?? ''));
|
|
147
|
+
let held = Date.parse(String(row?.lease_expiry ?? '')) > Date.now();
|
|
148
|
+
let due = !ordered.has(String(row?.handler)) || first.has(b.entity.eid);
|
|
149
|
+
return due && !isNaN(at) && at < cutoff && !held;
|
|
150
|
+
});
|
|
151
|
+
let stuck = overdue.filter((b)=>!comp(b)?.waiting);
|
|
152
|
+
let blocked = overdue.filter((b)=>comp(b)?.waiting);
|
|
153
|
+
let found = [];
|
|
154
|
+
if (failed.length) {
|
|
155
|
+
found.push({
|
|
156
|
+
level: 'fail',
|
|
157
|
+
text: `${failed.length} effect run(s) spent their attempts and were ` + `left for a person: ${some(failed, id, sample)}; ` + '`yak effect retry <handler>` puts them back to the pool, ' + '`yak effect drop <handler>` deletes what no one owes'
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if (orphans.length) {
|
|
161
|
+
found.push({
|
|
162
|
+
level: 'warn',
|
|
163
|
+
text: `${orphans.length} effect run(s) wait for a handler no plugin ` + `declares, so nothing will ever work them: ` + `${some(orphans, id, sample)}; \`yak effect drop <handler>\` ` + 'deletes them'
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
if (stuck.length) {
|
|
167
|
+
found.push({
|
|
168
|
+
level: 'warn',
|
|
169
|
+
text: `${stuck.length} effect run(s) have been pending since before ` + `${new Date(cutoff).toISOString()} — nothing is working them: ` + `${some(stuck, id, sample)}`
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if (blocked.length) {
|
|
173
|
+
found.push({
|
|
174
|
+
level: 'warn',
|
|
175
|
+
text: `${blocked.length} effect run(s) last deferred for a dependency: ` + some(blocked, (b)=>`${id(b)} (${comp(b)?.waiting})`, sample)
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return checked(call.entity.eid, about, found);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
//# sourceMappingURL=tools.js.map
|
package/tools.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.js","sources":["./tools.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,8EAA8E;AAC9E,+EAA+E;AAC/E,2EAA2E;AAC3E,EAAE;AACF,6EAA6E;AAC7E,6EAA6E;AAC7E,8EAA8E;AAC9E,wDAAwD;AACxD,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAC/E,0EAA0E;AAC1E,0EAA0E;AAC1E,uEAAuE;AACvE,gEAAgE;AAChE,EAAE;AACF,2EAA2E;AAC3E,gFAAgF;AAChF,gFAAgF;AAChF,8EAA8E;AAC9E,EAAE;AACF,0EAA0E;AAC1E,8EAA8E;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,MAAM,EAAE;QAClB,MAAM,IAAI,CAAC;UACT,OAAO;UACP,MACE,GAAG,QAAQ,MAAM,CAAC,+CAA+C,CAAC,GAClE,KAAK,SAAS,CAAC,IAAM,GAAG,GAAG,GAAG,EAAE,EAAE,KAAK,IAAI,QAAQ,CAAC,CAAC,EAAE;QAC3D;MACF;MACA,OAAO,QAAQ,KAAK,MAAM,CAAC,GAAG,EAAE,OAAO;IACzC;EACF;AACF,EAAC"}
|
package/tools.ts
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// The tool an agent can call here: the module exported as
|
|
2
|
+
// `@yaks/effects/tools`, holding the implementation behind the `effect_check`
|
|
3
|
+
// declaration in ./vocab.json. One tool, and it is a check — a tool whose verb
|
|
4
|
+
// is `check`, which is all a "doctor" command is (@yaks/tools ./check.ts).
|
|
5
|
+
//
|
|
6
|
+
// The pool is written for exactly this. ./pool.ts retries a run that did not
|
|
7
|
+
// complete, backing off between attempts, and when its last attempt is spent
|
|
8
|
+
// marks the row `failed` with the error beside it and leaves it for a person.
|
|
9
|
+
// Nothing in that sequence tells the person. This does.
|
|
10
|
+
//
|
|
11
|
+
// The other half is a row that never got that far: a run is written pending
|
|
12
|
+
// by the commit that owes it, so a row that has been waiting since long before
|
|
13
|
+
// it came due, with no claim a worker renews, means nobody is working the
|
|
14
|
+
// pool — no process serves the effects role, or none of them handles that
|
|
15
|
+
// effect. That failure is invisible from every other angle: the writes
|
|
16
|
+
// commit, the graph looks normal, the mail just never goes out.
|
|
17
|
+
//
|
|
18
|
+
// And the person it tells can act: `effect_retry` puts failed runs back to
|
|
19
|
+
// pending for the pool to work again, `effect_drop` deletes the ones whose work
|
|
20
|
+
// no one owes any more. Both write the pool's own way — a trusted batch guarded
|
|
21
|
+
// by the state they read — so a run a worker has since touched is left alone.
|
|
22
|
+
//
|
|
23
|
+
// A graph with no `effect` component keeps no pool, which is a legitimate
|
|
24
|
+
// configuration (effects run where they were committed, nothing written down)
|
|
25
|
+
// and not a fault — the check reports that and finds nothing.
|
|
26
|
+
|
|
27
|
+
import { and, eq, list } from '@yaks/query'
|
|
28
|
+
import type { Bundle, Comp, Graph } from 'jsr:@yaks/graph@0.2.36'
|
|
29
|
+
import { addressed, argsOf, who } from '@yaks/graph/call'
|
|
30
|
+
import { signed } from '@yaks/graph/provenance'
|
|
31
|
+
import { token } from '@yaks/graph/sha256'
|
|
32
|
+
import type { Runs } from 'jsr:@yaks/graph@0.2.36/tools'
|
|
33
|
+
import { human } from '@yaks/id'
|
|
34
|
+
import type { Finding } from 'jsr:@yaks/tools@0.2.36'
|
|
35
|
+
import { CallError } from '@yaks/tools/errors'
|
|
36
|
+
import { checked } from '@yaks/tools/check'
|
|
37
|
+
import { said } from '@yaks/tools/result'
|
|
38
|
+
import { effectsIn, type Vocab } from '@yaks/vocab'
|
|
39
|
+
import { effectRetry } from './enqueue.js'
|
|
40
|
+
import { EFFECT } from './pool.js'
|
|
41
|
+
import { firstPending } from './ready.js'
|
|
42
|
+
|
|
43
|
+
/** What configuration this package's check accepts. */
|
|
44
|
+
export type Options = {
|
|
45
|
+
/** how long a run may sit pending past its due instant before that means
|
|
46
|
+
* nothing is working them, in minutes (default 10) */
|
|
47
|
+
minutes?: number
|
|
48
|
+
/** how many of the failed runs to name (default 5) */
|
|
49
|
+
sample?: number
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let MINUTES = 10
|
|
53
|
+
let SAMPLE = 5
|
|
54
|
+
|
|
55
|
+
let comp = (b: Bundle) => b[EFFECT] as Comp | undefined
|
|
56
|
+
|
|
57
|
+
// A few names, and how many more there were: a list of six hundred stuck rows
|
|
58
|
+
// is not more actionable than a list of five.
|
|
59
|
+
let some = (rows: Bundle[], id: (b: Bundle) => string, sample: number) => {
|
|
60
|
+
let shown = rows.slice(0, sample).map(id).join(', ')
|
|
61
|
+
let rest = rows.length - Math.min(rows.length, sample)
|
|
62
|
+
return `${shown}${rest > 0 ? `, and ${rest} more` : ''}`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// A run is a person's to settle when it spent its attempts, or when it waits
|
|
66
|
+
// for a handler no plugin declares any more, which no process will ever run.
|
|
67
|
+
// Retrying either kind of run only strands it again unless its handler exists.
|
|
68
|
+
let dead = (vocab: Vocab) => {
|
|
69
|
+
let declared = new Set(effectsIn(vocab.docs).map((e) => e.name))
|
|
70
|
+
let state = (b: Bundle) => comp(b)?.state
|
|
71
|
+
let known = (b: Bundle) => declared.has(String(comp(b)?.handler))
|
|
72
|
+
let orphan = (b: Bundle) => state(b) == 'pending' && !known(b)
|
|
73
|
+
return {
|
|
74
|
+
orphan,
|
|
75
|
+
retry: (b: Bundle) => state(b) == 'failed' && known(b),
|
|
76
|
+
drop: (b: Bundle) => state(b) == 'failed' || orphan(b),
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// The runs a handler name or a single run's id points at, among those `owed`.
|
|
81
|
+
let named = async (
|
|
82
|
+
graph: Pick<Graph, 'read' | 'get' | 'address'>,
|
|
83
|
+
of: string,
|
|
84
|
+
owed: (b: Bundle) => boolean,
|
|
85
|
+
) => {
|
|
86
|
+
// Done runs are left out by the query: a handler's history can be long.
|
|
87
|
+
let byHandler = (await graph.read(and(
|
|
88
|
+
eq(`${EFFECT}.handler`, of),
|
|
89
|
+
eq(`${EFFECT}.state`, list('failed', 'pending')),
|
|
90
|
+
))).filter(owed)
|
|
91
|
+
if (byHandler.length) return byHandler
|
|
92
|
+
let [eid] = await addressed(graph, [of])
|
|
93
|
+
let [row] = await graph.get([eid])
|
|
94
|
+
return row && owed(row) ? [row] : []
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The write that retries (failed runs back to pending with a fresh allowance)
|
|
99
|
+
* or drops (failed runs, and pending ones no handler is declared for, deleted)
|
|
100
|
+
* the runs `of` names: a handler, or one run's id. Each change is guarded by
|
|
101
|
+
* the state read, so a run a worker has since touched is left alone. Empty
|
|
102
|
+
* when `of` names none. What `effect_retry` and `effect_drop` apply, and what
|
|
103
|
+
* a host without those tools applies the same way (yaks.app's
|
|
104
|
+
* `store_effect_retry`).
|
|
105
|
+
*/
|
|
106
|
+
export let settling = async (
|
|
107
|
+
graph: Pick<Graph, 'read' | 'get' | 'address'>,
|
|
108
|
+
vocab: Vocab,
|
|
109
|
+
act: 'retry' | 'drop',
|
|
110
|
+
of: string,
|
|
111
|
+
): Promise<Bundle[]> => {
|
|
112
|
+
let rows = await named(graph, of, dead(vocab)[act])
|
|
113
|
+
return rows.map((b) => {
|
|
114
|
+
let state = String(comp(b)?.state)
|
|
115
|
+
return act == 'retry' ? effectRetry(b.entity.eid, state) : {
|
|
116
|
+
entity: { eid: b.entity.eid },
|
|
117
|
+
$delete: true,
|
|
118
|
+
$was: { [EFFECT]: { state: token(state) } },
|
|
119
|
+
}
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Settle the runs named by the call, and say how many.
|
|
124
|
+
let settle = (
|
|
125
|
+
host: { vocab: Vocab },
|
|
126
|
+
act: 'retry' | 'drop',
|
|
127
|
+
done: string,
|
|
128
|
+
): Runs[string] =>
|
|
129
|
+
async (call, graph) => {
|
|
130
|
+
let of = String(argsOf(call).of)
|
|
131
|
+
let bundles = await settling(graph, host.vocab, act, of)
|
|
132
|
+
if (!bundles.length) {
|
|
133
|
+
throw new CallError('refused', `${of} names no effect run to ${act}`)
|
|
134
|
+
}
|
|
135
|
+
await graph.apply(signed(bundles, who(call)), { trusted: true })
|
|
136
|
+
return [said(call, `${done} ${bundles.length} run(s) of ${of}`)]
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The implementation behind the tool ./vocab.json declares. */
|
|
140
|
+
export let runs = (
|
|
141
|
+
host: { vocab: Vocab },
|
|
142
|
+
options: Options = {},
|
|
143
|
+
): Runs => {
|
|
144
|
+
let settles = dead(host.vocab)
|
|
145
|
+
let ordered = new Set(
|
|
146
|
+
effectsIn(host.vocab.docs).filter((e) => e.fifo).map((e) => e.name),
|
|
147
|
+
)
|
|
148
|
+
return {
|
|
149
|
+
effect_retry: settle(host, 'retry', 'retried'),
|
|
150
|
+
effect_drop: settle(host, 'drop', 'dropped'),
|
|
151
|
+
effect_check: async (call, graph) => {
|
|
152
|
+
let about = 'every effect run reached an end somebody would hear about'
|
|
153
|
+
if (!host.vocab.comp(EFFECT)) {
|
|
154
|
+
// Not a fault: an application that runs its effects where it commits
|
|
155
|
+
// them loads no `effect` component, so there is nothing written down to
|
|
156
|
+
// fall behind on.
|
|
157
|
+
return checked(call.entity.eid, about, [])
|
|
158
|
+
}
|
|
159
|
+
let id = human(host.vocab)
|
|
160
|
+
let sample = options.sample ?? SAMPLE
|
|
161
|
+
let cutoff = Date.now() - (options.minutes ?? MINUTES) * 60_000
|
|
162
|
+
let failed = await graph.read(and(eq(`${EFFECT}.state`, 'failed')))
|
|
163
|
+
let pending = await graph.read(and(eq(`${EFFECT}.state`, 'pending')))
|
|
164
|
+
let first = new Set(
|
|
165
|
+
await Promise.all(
|
|
166
|
+
[...ordered].map(async (name) =>
|
|
167
|
+
(await firstPending(graph, name))?.entity.eid
|
|
168
|
+
),
|
|
169
|
+
),
|
|
170
|
+
)
|
|
171
|
+
let orphans = pending.filter(settles.orphan)
|
|
172
|
+
let overdue = pending.filter((b) => !settles.orphan(b))
|
|
173
|
+
.filter((b) => {
|
|
174
|
+
// Since when it has been waiting: a failure that reported is owed its
|
|
175
|
+
// next run at `next`, not at the instant the run was first written
|
|
176
|
+
// down — a backoff is not a symptom. A claim its worker still renews
|
|
177
|
+
// is a run in progress, however long ago it was owed: a transcript's
|
|
178
|
+
// run lasts as long as the transcript does.
|
|
179
|
+
let row = comp(b)
|
|
180
|
+
let at = Date.parse(String(row?.next ?? row?.at ?? ''))
|
|
181
|
+
let held = Date.parse(String(row?.lease_expiry ?? '')) > Date.now()
|
|
182
|
+
let due = !ordered.has(String(row?.handler)) ||
|
|
183
|
+
first.has(b.entity.eid)
|
|
184
|
+
return due && !isNaN(at) && at < cutoff && !held
|
|
185
|
+
})
|
|
186
|
+
let stuck = overdue.filter((b) => !comp(b)?.waiting)
|
|
187
|
+
let blocked = overdue.filter((b) => comp(b)?.waiting)
|
|
188
|
+
let found: Finding[] = []
|
|
189
|
+
if (failed.length) {
|
|
190
|
+
found.push({
|
|
191
|
+
level: 'fail',
|
|
192
|
+
text:
|
|
193
|
+
`${failed.length} effect run(s) spent their attempts and were ` +
|
|
194
|
+
`left for a person: ${some(failed, id, sample)}; ` +
|
|
195
|
+
'`yak effect retry <handler>` puts them back to the pool, ' +
|
|
196
|
+
'`yak effect drop <handler>` deletes what no one owes',
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
if (orphans.length) {
|
|
200
|
+
found.push({
|
|
201
|
+
level: 'warn',
|
|
202
|
+
text:
|
|
203
|
+
`${orphans.length} effect run(s) wait for a handler no plugin ` +
|
|
204
|
+
`declares, so nothing will ever work them: ` +
|
|
205
|
+
`${some(orphans, id, sample)}; \`yak effect drop <handler>\` ` +
|
|
206
|
+
'deletes them',
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
if (stuck.length) {
|
|
210
|
+
found.push({
|
|
211
|
+
level: 'warn',
|
|
212
|
+
text:
|
|
213
|
+
`${stuck.length} effect run(s) have been pending since before ` +
|
|
214
|
+
`${new Date(cutoff).toISOString()} — nothing is working them: ` +
|
|
215
|
+
`${some(stuck, id, sample)}`,
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
if (blocked.length) {
|
|
219
|
+
found.push({
|
|
220
|
+
level: 'warn',
|
|
221
|
+
text:
|
|
222
|
+
`${blocked.length} effect run(s) last deferred for a dependency: ` +
|
|
223
|
+
some(blocked, (b) => `${id(b)} (${comp(b)?.waiting})`, sample),
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
return checked(call.entity.eid, about, found)
|
|
227
|
+
},
|
|
228
|
+
}
|
|
229
|
+
}
|
package/trace.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// What happened, worked out from what was committed. The bundles a client
|
|
2
|
+
// sends record only what to write: the same bundle patches a component that
|
|
3
|
+
// already existed and creates one that did not, and a cascade's casualty comes
|
|
4
|
+
// back as a bare tombstone with none of the components it used to carry. A
|
|
5
|
+
// handler needs the difference — "a post was published" is not "a post exists"
|
|
6
|
+
// — so this file derives it.
|
|
7
|
+
//
|
|
8
|
+
// It takes two readings. BEFORE the patches go in, `before()` reads what each
|
|
9
|
+
// entity in the batch carries, plus the same for everything the batch is about
|
|
10
|
+
// to delete (@yaks/graph's own `doomed()` walk, so the death rule is reused,
|
|
11
|
+
// never re-implemented). After the commit, `events()` replays the applied
|
|
12
|
+
// batch against that reading: a component absent before is a creation, one
|
|
13
|
+
// present before is a change carrying only the properties that moved, and a
|
|
14
|
+
// component or an entity that went is a removal, carrying that component as it
|
|
15
|
+
// was. That is the one thing a handler cannot read afterwards: a deleted edge
|
|
16
|
+
// no longer says which ends it had. A removal carries its own component and
|
|
17
|
+
// nothing else the entity lost, so a run kept for it never copies the rest: a
|
|
18
|
+
// handler that needs a deleted link's ends watches `edge` going.
|
|
19
|
+
//
|
|
20
|
+
// The reading rides forward on the batch itself, under `$before`. A `$`-key is
|
|
21
|
+
// not a component — `comps()` skips it, so no storage adapter ever sees it —
|
|
22
|
+
// which is how one phase of `apply()` passes what it learned to a later one
|
|
23
|
+
// without a global anywhere. The effect phase strips it back off.
|
|
24
|
+
import { after } from "@yaks/fp";
|
|
25
|
+
import { comps, dead } from "@yaks/graph/bundle";
|
|
26
|
+
import { doomed } from "@yaks/graph/read";
|
|
27
|
+
/** The key the reading rides forward on. Not a component: `$`-prefixed keys
|
|
28
|
+
* are `apply()`'s pipeline, never properties, so storage never sees it. */ export let BEFORE = '$before';
|
|
29
|
+
// The entities this batch deletes, written either way.
|
|
30
|
+
let killing = (bundles)=>[
|
|
31
|
+
...new Set(bundles.filter(dead).map((b)=>b.entity.eid))
|
|
32
|
+
];
|
|
33
|
+
/**
|
|
34
|
+
* Read what the batch's entities — and everything that will die with them —
|
|
35
|
+
* carry right now, and ride it forward on the batch. Registered on the
|
|
36
|
+
* `precondition` phase, so it reads inside the transaction and before a single
|
|
37
|
+
* patch has gone in.
|
|
38
|
+
*/ export let before = (vocab)=>(bundles, tx)=>{
|
|
39
|
+
let killed = killing(bundles);
|
|
40
|
+
return after(killed.length ? doomed(tx, vocab, killed) : [], (gone)=>after(tx.get([
|
|
41
|
+
...new Set([
|
|
42
|
+
...bundles.map((b)=>b.entity.eid),
|
|
43
|
+
...gone
|
|
44
|
+
])
|
|
45
|
+
]), (found)=>{
|
|
46
|
+
let map = {};
|
|
47
|
+
for (let b of found)map[b.entity.eid] = b;
|
|
48
|
+
return bundles.map((b)=>({
|
|
49
|
+
...b,
|
|
50
|
+
[BEFORE]: map
|
|
51
|
+
}));
|
|
52
|
+
}));
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* What {@link before} is about to read, declared before it reads it: every
|
|
56
|
+
* entity the batch is about, and — for a delete — everything pointing at what
|
|
57
|
+
* is being deleted, which is what the `doomed()` walk asks. Declared as the
|
|
58
|
+
* plugin's `wants` so @yaks/graph gathers both in one pass.
|
|
59
|
+
*/ export let wanting = (vocab)=>(bundles)=>{
|
|
60
|
+
let killed = killing(bundles);
|
|
61
|
+
return [
|
|
62
|
+
{
|
|
63
|
+
eids: bundles.map((b)=>b.entity.eid)
|
|
64
|
+
},
|
|
65
|
+
...killed.length ? [
|
|
66
|
+
{
|
|
67
|
+
about: killed,
|
|
68
|
+
comps: [
|
|
69
|
+
...new Set(vocab.deaths('cascade').map(([c])=>c))
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
] : []
|
|
73
|
+
];
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Take the reading back off, so the batch a caller gets back is the batch as
|
|
77
|
+
* applied and nothing else. Done in place, deliberately: `apply()` returns the
|
|
78
|
+
* bundles it committed rather than what the effect phase returns, so a copy
|
|
79
|
+
* made here would be a copy nobody reads. The bundles it edits are the ones
|
|
80
|
+
* `before()` copied, never the caller's own.
|
|
81
|
+
*/ export let strip = (bundles)=>{
|
|
82
|
+
for (let b of bundles)if (BEFORE in b) delete b[BEFORE];
|
|
83
|
+
return bundles;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* The committed batch, read as what happened. Replays the bundles in order
|
|
87
|
+
* against the reading `before()` took: a component the entity did not carry is
|
|
88
|
+
* `created` (once — a second patch for it in the same batch is a `changed`), a
|
|
89
|
+
* component it did carry is `changed`, a null component or a deleted entity is
|
|
90
|
+
* `removed`, one event per component the casualty carried, each carrying
|
|
91
|
+
* `before`: that component, as the reading found it.
|
|
92
|
+
*/ export let events = (bundles)=>{
|
|
93
|
+
let map = bundles.find((b)=>b[BEFORE])?.[BEFORE] ?? {};
|
|
94
|
+
// A new entity's `num` rides back on its own bundle, so an event about the
|
|
95
|
+
// entity that was just created can carry the number storage gave it.
|
|
96
|
+
let nums = new Map();
|
|
97
|
+
for (let b of bundles){
|
|
98
|
+
if (b.entity.num != null) nums.set(b.entity.eid, b.entity.num);
|
|
99
|
+
}
|
|
100
|
+
let named = (e)=>e.num != null || !nums.has(e.eid) ? e : {
|
|
101
|
+
...e,
|
|
102
|
+
num: nums.get(e.eid)
|
|
103
|
+
};
|
|
104
|
+
// What each entity carries as the replay walks the batch, so the batch's own
|
|
105
|
+
// earlier bundles count: created then patched is one creation, not two.
|
|
106
|
+
let held = new Map();
|
|
107
|
+
let has = (eid)=>{
|
|
108
|
+
let s = held.get(eid);
|
|
109
|
+
if (!s) {
|
|
110
|
+
held.set(eid, s = new Set(map[eid] ? comps(map[eid]).map(([n])=>n) : []));
|
|
111
|
+
}
|
|
112
|
+
return s;
|
|
113
|
+
};
|
|
114
|
+
let removed = (entity, name)=>{
|
|
115
|
+
let was = map[entity.eid]?.[name];
|
|
116
|
+
return was == null ? {
|
|
117
|
+
kind: 'removed',
|
|
118
|
+
entity,
|
|
119
|
+
name
|
|
120
|
+
} : {
|
|
121
|
+
kind: 'removed',
|
|
122
|
+
entity,
|
|
123
|
+
name,
|
|
124
|
+
before: {
|
|
125
|
+
entity,
|
|
126
|
+
[name]: was
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
let out = [];
|
|
131
|
+
for (let b of bundles){
|
|
132
|
+
let entity = named(b.entity);
|
|
133
|
+
let carried = has(entity.eid);
|
|
134
|
+
if (dead(b)) {
|
|
135
|
+
for (let name of carried)out.push(removed(entity, name));
|
|
136
|
+
carried.clear();
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
for (let [name, comp] of comps(b)){
|
|
140
|
+
if (comp == null) {
|
|
141
|
+
if (carried.delete(name)) out.push(removed(entity, name));
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
let born = !carried.has(name);
|
|
145
|
+
carried.add(name);
|
|
146
|
+
out.push({
|
|
147
|
+
kind: born ? 'created' : 'changed',
|
|
148
|
+
entity,
|
|
149
|
+
name,
|
|
150
|
+
comp
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
let touched = new Map();
|
|
155
|
+
for (let e of out){
|
|
156
|
+
let names = touched.get(e.entity.eid) ?? new Set();
|
|
157
|
+
names.add(e.name);
|
|
158
|
+
touched.set(e.entity.eid, names);
|
|
159
|
+
}
|
|
160
|
+
return out.map((e)=>({
|
|
161
|
+
...e,
|
|
162
|
+
touched: [
|
|
163
|
+
...touched.get(e.entity.eid)
|
|
164
|
+
]
|
|
165
|
+
}));
|
|
166
|
+
};
|
|
167
|
+
//# sourceMappingURL=trace.js.map
|
package/trace.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.js","sources":["./trace.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,4EAA4E;AAC5E,+EAA+E;AAC/E,2EAA2E;AAC3E,+EAA+E;AAC/E,6BAA6B;AAC7B,EAAE;AACF,8EAA8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