@wrenautomation/birdwatch 0.0.0-stage → 0.30.0

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.
Files changed (141) hide show
  1. package/LICENSE.md +61 -0
  2. package/README.md +320 -2
  3. package/dist/app/cli.d.ts +4 -0
  4. package/dist/app/cli.js +149 -0
  5. package/dist/app/cli.js.map +1 -0
  6. package/dist/app/engine-cli.d.ts +44 -0
  7. package/dist/app/engine-cli.js +596 -0
  8. package/dist/app/engine-cli.js.map +1 -0
  9. package/dist/app/index.d.ts +6 -0
  10. package/dist/app/index.js +7 -0
  11. package/dist/app/index.js.map +1 -0
  12. package/dist/app/serve.d.ts +14 -0
  13. package/dist/app/serve.js +102 -0
  14. package/dist/app/serve.js.map +1 -0
  15. package/dist/document/body.d.ts +6 -0
  16. package/dist/document/body.js +18 -0
  17. package/dist/document/body.js.map +1 -0
  18. package/dist/document/check.d.ts +35 -0
  19. package/dist/document/check.js +68 -0
  20. package/dist/document/check.js.map +1 -0
  21. package/dist/document/extract.d.ts +81 -0
  22. package/dist/document/extract.js +226 -0
  23. package/dist/document/extract.js.map +1 -0
  24. package/dist/document/index.d.ts +11 -0
  25. package/dist/document/index.js +12 -0
  26. package/dist/document/index.js.map +1 -0
  27. package/dist/document/model.d.ts +10 -0
  28. package/dist/document/model.js +2 -0
  29. package/dist/document/model.js.map +1 -0
  30. package/dist/document/readers.d.ts +24 -0
  31. package/dist/document/readers.js +87 -0
  32. package/dist/document/readers.js.map +1 -0
  33. package/dist/document/types.d.ts +99 -0
  34. package/dist/document/types.js +93 -0
  35. package/dist/document/types.js.map +1 -0
  36. package/dist/engine/agent.d.ts +475 -0
  37. package/dist/engine/agent.js +990 -0
  38. package/dist/engine/agent.js.map +1 -0
  39. package/dist/engine/canonical.d.ts +4 -0
  40. package/dist/engine/canonical.js +55 -0
  41. package/dist/engine/canonical.js.map +1 -0
  42. package/dist/engine/context.d.ts +9 -0
  43. package/dist/engine/context.js +141 -0
  44. package/dist/engine/context.js.map +1 -0
  45. package/dist/engine/cost.d.ts +13 -0
  46. package/dist/engine/cost.js +31 -0
  47. package/dist/engine/cost.js.map +1 -0
  48. package/dist/engine/index.d.ts +16 -0
  49. package/dist/engine/index.js +17 -0
  50. package/dist/engine/index.js.map +1 -0
  51. package/dist/engine/judge.d.ts +158 -0
  52. package/dist/engine/judge.js +323 -0
  53. package/dist/engine/judge.js.map +1 -0
  54. package/dist/engine/people.d.ts +34 -0
  55. package/dist/engine/people.js +125 -0
  56. package/dist/engine/people.js.map +1 -0
  57. package/dist/engine/plan.d.ts +6 -0
  58. package/dist/engine/plan.js +7 -0
  59. package/dist/engine/plan.js.map +1 -0
  60. package/dist/engine/plans/index.d.ts +19 -0
  61. package/dist/engine/plans/index.js +104 -0
  62. package/dist/engine/plans/index.js.map +1 -0
  63. package/dist/engine/ports.d.ts +76 -0
  64. package/dist/engine/ports.js +28 -0
  65. package/dist/engine/ports.js.map +1 -0
  66. package/dist/engine/recall.d.ts +42 -0
  67. package/dist/engine/recall.js +71 -0
  68. package/dist/engine/recall.js.map +1 -0
  69. package/dist/engine/state.d.ts +96 -0
  70. package/dist/engine/state.js +162 -0
  71. package/dist/engine/state.js.map +1 -0
  72. package/dist/engine/steps.d.ts +98 -0
  73. package/dist/engine/steps.js +435 -0
  74. package/dist/engine/steps.js.map +1 -0
  75. package/dist/engine/tree.d.ts +76 -0
  76. package/dist/engine/tree.js +148 -0
  77. package/dist/engine/tree.js.map +1 -0
  78. package/dist/engine/types.d.ts +250 -0
  79. package/dist/engine/types.js +168 -0
  80. package/dist/engine/types.js.map +1 -0
  81. package/dist/engine/view.d.ts +138 -0
  82. package/dist/engine/view.js +188 -0
  83. package/dist/engine/view.js.map +1 -0
  84. package/dist/index.d.ts +15 -0
  85. package/dist/index.js +16 -0
  86. package/dist/index.js.map +1 -0
  87. package/dist/ledger/files.d.ts +39 -0
  88. package/dist/ledger/files.js +113 -0
  89. package/dist/ledger/files.js.map +1 -0
  90. package/dist/ledger/ids.d.ts +4 -0
  91. package/dist/ledger/ids.js +9 -0
  92. package/dist/ledger/ids.js.map +1 -0
  93. package/dist/ledger/index.d.ts +11 -0
  94. package/dist/ledger/index.js +12 -0
  95. package/dist/ledger/index.js.map +1 -0
  96. package/dist/ledger/ledger.d.ts +46 -0
  97. package/dist/ledger/ledger.js +105 -0
  98. package/dist/ledger/ledger.js.map +1 -0
  99. package/dist/ledger/memory.d.ts +19 -0
  100. package/dist/ledger/memory.js +19 -0
  101. package/dist/ledger/memory.js.map +1 -0
  102. package/dist/ledger/records.d.ts +1424 -0
  103. package/dist/ledger/records.js +292 -0
  104. package/dist/ledger/records.js.map +1 -0
  105. package/dist/sources/arxiv.d.ts +105 -0
  106. package/dist/sources/arxiv.js +321 -0
  107. package/dist/sources/arxiv.js.map +1 -0
  108. package/dist/sources/exa.d.ts +19 -0
  109. package/dist/sources/exa.js +59 -0
  110. package/dist/sources/exa.js.map +1 -0
  111. package/dist/sources/fetcher.d.ts +35 -0
  112. package/dist/sources/fetcher.js +67 -0
  113. package/dist/sources/fetcher.js.map +1 -0
  114. package/dist/sources/google.d.ts +16 -0
  115. package/dist/sources/google.js +64 -0
  116. package/dist/sources/google.js.map +1 -0
  117. package/dist/sources/index.d.ts +17 -0
  118. package/dist/sources/index.js +18 -0
  119. package/dist/sources/index.js.map +1 -0
  120. package/dist/sources/junk.d.ts +10 -0
  121. package/dist/sources/junk.js +16 -0
  122. package/dist/sources/junk.js.map +1 -0
  123. package/dist/sources/ladder.d.ts +47 -0
  124. package/dist/sources/ladder.js +158 -0
  125. package/dist/sources/ladder.js.map +1 -0
  126. package/dist/sources/ledger.d.ts +7 -0
  127. package/dist/sources/ledger.js +35 -0
  128. package/dist/sources/ledger.js.map +1 -0
  129. package/dist/sources/paste.d.ts +8 -0
  130. package/dist/sources/paste.js +22 -0
  131. package/dist/sources/paste.js.map +1 -0
  132. package/dist/sources/searxng.d.ts +11 -0
  133. package/dist/sources/searxng.js +38 -0
  134. package/dist/sources/searxng.js.map +1 -0
  135. package/dist/sources/sites.d.ts +53 -0
  136. package/dist/sources/sites.js +285 -0
  137. package/dist/sources/sites.js.map +1 -0
  138. package/dist/sources/ytdlp.d.ts +43 -0
  139. package/dist/sources/ytdlp.js +117 -0
  140. package/dist/sources/ytdlp.js.map +1 -0
  141. package/package.json +61 -3
@@ -0,0 +1,98 @@
1
+ /**
2
+ * The fixed steps the agent commands run: search, read, extract, check. A step reads the fold,
3
+ * calls ports, and appends records; it holds nothing between calls. Source calls fan out (per query across
4
+ * the searchers; reads and extracts a few at a time) and their records land in order, so a
5
+ * trace reads the same whatever answered first. A step runs again after a transient failure
6
+ * from where its records say it got to: a search with an ok spend, a hit with a document, a
7
+ * document with a verdict, are not done twice.
8
+ */
9
+ import { type Model } from "@wrenautomation/models";
10
+ import type { EntryType, Judged, JudgeFailure, Questions } from "@wrenautomation/models/judge";
11
+ import type { DocumentRecord, Envelope, FindingRecord, From, HitRecord, LedgerRecord, Step } from "../ledger/records.js";
12
+ import { type Deps, type Source } from "./ports.js";
13
+ import { type RunState } from "./state.js";
14
+ import { type Parent } from "./tree.js";
15
+ import { HitView, type HitViewIn } from "./types.js";
16
+ export interface StepContext {
17
+ deps: Deps;
18
+ /** The run's model, wrapped so every ask writes a spend; a Transient after the tries run out. */
19
+ model: Model | null;
20
+ /** The same model, its spends marked with the record they are about. */
21
+ modelOn(on: string): Model | null;
22
+ run: string;
23
+ round: number;
24
+ step: Step;
25
+ /**
26
+ * One judge call with its spend written; its answers, or the failure's kind (`off`: no judge).
27
+ * Never throws.
28
+ */
29
+ judge(on: string, about: string, state: EntryType, questions: Questions): Promise<Judged | {
30
+ failure: JudgeFailure | "off";
31
+ }>;
32
+ /** Appends one record with the envelope filled in. */
33
+ write<T extends Omit<LedgerRecord, keyof Envelope>>(record: T, who: {
34
+ from: From;
35
+ by: string;
36
+ }): Promise<string>;
37
+ /**
38
+ * Times a source call and writes its spend. A throw becomes `ok: false` and null; a
39
+ * Transient is tried again, and thrown when the tries run out.
40
+ */
41
+ invoke<T>(source: Source, op: "search" | "open", about: string, on: string, call: () => Promise<T>): Promise<T | null>;
42
+ }
43
+ export type StepResult = {
44
+ ok: true;
45
+ note?: string;
46
+ } | {
47
+ ok: false;
48
+ note: string;
49
+ };
50
+ export type StepFn = (ctx: StepContext, state: RunState) => Promise<StepResult>;
51
+ /** Reads and extracts in flight at once. */
52
+ export declare const PARALLEL = 4;
53
+ /**
54
+ * What round `round` left open: its review's why when it said again, its gaps, and a person's
55
+ * keep or drop on its findings (often written after the review, between rounds).
56
+ */
57
+ export declare const openAfter: (state: RunState, round: number) => string[];
58
+ /** A line with a URL or a ledger id on it: a fact someone can go and check. */
59
+ export declare const sourced: (line: string) => boolean;
60
+ /**
61
+ * The ask with each known line that names no source moved to its premises. A run seeded with
62
+ * "the buyer is X" plans inside X unless something tests X; a sourced line is looked past.
63
+ */
64
+ export declare function withPremises<A extends {
65
+ known?: readonly string[];
66
+ premises?: readonly string[];
67
+ }>(ask: A): A & {
68
+ known: string[];
69
+ premises: string[];
70
+ };
71
+ /** One hit per url, sources merged, order of first sight. */
72
+ export declare function dedupeHits(hits: readonly HitViewIn[]): HitView[];
73
+ /**
74
+ * What a round's plan reads: the known lines (what
75
+ * the ask brought, what the run kept, then what its children found), the premises, and what is
76
+ * open (the last round's holes, children still going). A new child also reads what its parent
77
+ * and siblings found.
78
+ */
79
+ export declare function planAsk(deps: Pick<Deps, "ledger">, state: RunState, base: {
80
+ text: string;
81
+ known?: readonly string[];
82
+ premises?: readonly string[];
83
+ }, round: number, parent?: Parent): Promise<{
84
+ known: string[];
85
+ premises: string[];
86
+ open: string[];
87
+ }>;
88
+ export declare const fetchStep: StepFn;
89
+ export declare function readHits(ctx: StepContext, state: RunState, wanted: readonly HitRecord[]): Promise<StepResult>;
90
+ /** Extracts `docs` with the extractor on the run's model; the agent's `extract` passes its own list. */
91
+ export declare function extractDocuments(ctx: StepContext, state: RunState, docs: readonly DocumentRecord[]): Promise<StepResult>;
92
+ /**
93
+ * The sample check: a few kept findings, spread across the round's documents, each read by a
94
+ * model against its document. A finding whose quote does not back it gets a check record and a
95
+ * drop call; one that holds gets a check record. The share that fails is the review's read on
96
+ * the extract. A check record is the checkpoint: a rerun checks only the rest of the sample.
97
+ */
98
+ export declare function checkSample(ctx: StepContext, state: RunState, sample: readonly FindingRecord[]): Promise<StepResult>;
@@ -0,0 +1,435 @@
1
+ /**
2
+ * The fixed steps the agent commands run: search, read, extract, check. A step reads the fold,
3
+ * calls ports, and appends records; it holds nothing between calls. Source calls fan out (per query across
4
+ * the searchers; reads and extracts a few at a time) and their records land in order, so a
5
+ * trace reads the same whatever answered first. A step runs again after a transient failure
6
+ * from where its records say it got to: a search with an ok spend, a hit with a document, a
7
+ * document with a verdict, are not done twice.
8
+ */
9
+ import { isTransient, Transient } from "@wrenautomation/models";
10
+ import { checkFinding, gateFindings } from "../document/index.js";
11
+ import { canonical } from "./canonical.js";
12
+ import { DOUBT, judgeDocuments, judgeFindings, judgeHits, passNote, yesOf } from "./judge.js";
13
+ import { FOLLOW_N, pick, searchersFor } from "./ports.js";
14
+ import { fold, found, kept } from "./state.js";
15
+ import { fromChildren, fromSiblings, treeRoom } from "./tree.js";
16
+ import { HitView } from "./types.js";
17
+ const message = (err) => (err instanceof Error ? err.message : String(err));
18
+ /** Reads and extracts in flight at once. */
19
+ export const PARALLEL = 4;
20
+ const tool = (by) => ({ from: "tool", by });
21
+ /** A gap as a prompt line: the step that wrote it, or the person. */
22
+ const gapLine = (g) => `${g.step ?? g.by}: ${g.note}`;
23
+ /** A person's keep or drop on a finding of round `round`, as prompt lines: the finding, then why. */
24
+ const personCalls = (state, round) => {
25
+ const textOf = new Map(state.findings.map((f) => [f.id, f.text]));
26
+ return state.calls
27
+ .filter((c) => c.from === "human" && c.round === round && textOf.has(c.on))
28
+ .map((c) => `${c.by} ${c.verdict}s "${textOf.get(c.on)}": ${c.why}`);
29
+ };
30
+ /**
31
+ * What round `round` left open: its review's why when it said again, its gaps, and a person's
32
+ * keep or drop on its findings (often written after the review, between rounds).
33
+ */
34
+ export const openAfter = (state, round) => {
35
+ const edge = state.edges.filter((e) => e.round === round).at(-1);
36
+ return [
37
+ ...(edge?.next === "again" ? [edge.why] : []),
38
+ ...state.gaps.filter((g) => g.round === round).map(gapLine),
39
+ ...personCalls(state, round),
40
+ ];
41
+ };
42
+ const sourcesOf = (ctx, state) => pick(ctx.deps.sources, state.snapshot?.sources ?? []);
43
+ /** The allow-list plus the openers let in to open and follow; one this machine lacks is skipped. */
44
+ const readersOf = (ctx, state) => [
45
+ ...sourcesOf(ctx, state),
46
+ ...(state.snapshot?.opens ?? []).flatMap((n) => ctx.deps.sources.get(n) ?? []),
47
+ ];
48
+ /** Settled calls in order: the values, the first Transient, and any other error thrown on. */
49
+ function gather(settled) {
50
+ let transient = null;
51
+ const values = settled.map((r) => {
52
+ if (r.status === "fulfilled")
53
+ return r.value;
54
+ if (isTransient(r.reason)) {
55
+ transient ??= r.reason;
56
+ return null;
57
+ }
58
+ throw r.reason;
59
+ });
60
+ return { values, transient };
61
+ }
62
+ /**
63
+ * `call` a few at a time, in order; `keep` gets each batch's answers (null where the call
64
+ * failed) before the next batch starts, so what one item wrote is on record whatever the next
65
+ * does. The first Transient comes back; the first other error is thrown once its batch is kept.
66
+ */
67
+ async function batched(items, call, keep) {
68
+ let transient = null;
69
+ for (let i = 0; i < items.length; i += PARALLEL) {
70
+ const batch = items.slice(i, i + PARALLEL);
71
+ const settled = await Promise.allSettled(batch.map(call));
72
+ let failed;
73
+ let anyFailed = false;
74
+ for (const [j, r] of settled.entries()) {
75
+ if (r.status === "fulfilled")
76
+ await keep(batch[j], r.value);
77
+ else if (isTransient(r.reason))
78
+ transient ??= r.reason;
79
+ else if (!anyFailed) {
80
+ anyFailed = true;
81
+ failed = r.reason;
82
+ }
83
+ }
84
+ if (anyFailed)
85
+ throw failed;
86
+ }
87
+ return { transient };
88
+ }
89
+ /** A line with a URL or a ledger id on it: a fact someone can go and check. */
90
+ export const sourced = (line) => /https?:\/\/\S|\b[0-9A-HJKMNP-TV-Z]{26}\b/.test(line);
91
+ /**
92
+ * The ask with each known line that names no source moved to its premises. A run seeded with
93
+ * "the buyer is X" plans inside X unless something tests X; a sourced line is looked past.
94
+ */
95
+ export function withPremises(ask) {
96
+ const known = ask.known ?? [];
97
+ return {
98
+ ...ask,
99
+ known: known.filter(sourced),
100
+ premises: [...new Set([...(ask.premises ?? []), ...known.filter((l) => !sourced(l))])],
101
+ };
102
+ }
103
+ /** One hit per url, sources merged, order of first sight. */
104
+ export function dedupeHits(hits) {
105
+ const byUrl = new Map();
106
+ for (const h of hits.map((x) => HitView.parse(x))) {
107
+ const url = h.url.trim();
108
+ const have = byUrl.get(url);
109
+ if (!have) {
110
+ byUrl.set(url, { ...h, url, sources: [...new Set(h.sources)] });
111
+ continue;
112
+ }
113
+ for (const s of h.sources)
114
+ if (!have.sources.includes(s))
115
+ have.sources.push(s);
116
+ for (const k of ["title", "channel", "length", "date", "snippet"]) {
117
+ if (have[k] === undefined && h[k] !== undefined)
118
+ have[k] = h[k];
119
+ }
120
+ }
121
+ return [...byUrl.values()];
122
+ }
123
+ /**
124
+ * What a round's plan reads: the known lines (what
125
+ * the ask brought, what the run kept, then what its children found), the premises, and what is
126
+ * open (the last round's holes, children still going). A new child also reads what its parent
127
+ * and siblings found.
128
+ */
129
+ export async function planAsk(deps, state, base, round, parent) {
130
+ const { known, premises } = withPremises({
131
+ known: [...new Set([...(base.known ?? []), ...found(state)])],
132
+ premises: [...(base.premises ?? [])],
133
+ });
134
+ const children = await fromChildren(deps, state.run);
135
+ const siblings = parent && round === 1 ? await fromSiblings(deps, parent.run, base.text) : [];
136
+ return {
137
+ known: [...new Set([...known, ...siblings, ...children.known])],
138
+ premises,
139
+ open: [...(round > 1 ? openAfter(state, round - 1) : []), ...children.open],
140
+ };
141
+ }
142
+ export const fetchStep = async (ctx, state) => {
143
+ const queries = state.queries.filter((q) => q.round === ctx.round);
144
+ const searchers = sourcesOf(ctx, state);
145
+ const followers = readersOf(ctx, state);
146
+ if (!searchers.some((s) => s.search) && !queries.some((q) => q.follow))
147
+ return { ok: false, note: "no search source in the allow-list" };
148
+ const searched = new Set(state.spends
149
+ .filter((s) => s.round === ctx.round && s.op === "search" && s.ok && s.on)
150
+ .map((s) => `${s.on}\u0000${s.source}`));
151
+ // Each query goes to the allow-list as it stood when it was written: a source a later
152
+ // `--with` let in does not run the earlier queries.
153
+ const listOf = new Map();
154
+ let list = null;
155
+ for (const r of state.records)
156
+ if (r.type === "snapshot")
157
+ list = new Set([...r.sources, ...r.opens]);
158
+ else if (r.type === "query" && list)
159
+ listOf.set(r.id, list);
160
+ const idOf = new Map(state.hitByKey);
161
+ let written = 0;
162
+ let again = 0;
163
+ let calls = 0;
164
+ let answered = 0;
165
+ let transient = null;
166
+ for (const q of queries) {
167
+ const todo = searchersFor(q.follow ? followers : searchers, q).filter((s) => !searched.has(`${q.id}\u0000${s.name}`) && (listOf.get(q.id)?.has(s.name) ?? true));
168
+ calls += todo.length;
169
+ const got = gather(await Promise.allSettled(todo.map((s) => ctx.invoke(s, "search", q.terms, q.id, () => q.follow
170
+ ? s.follow(q.follow.as, q.follow.url, FOLLOW_N)
171
+ : s.search(q)))));
172
+ transient ??= got.transient;
173
+ const byKey = new Map();
174
+ for (const [i, hits] of got.values.entries()) {
175
+ if (!hits)
176
+ continue;
177
+ answered++;
178
+ const s = todo[i];
179
+ for (const h of hits) {
180
+ const key = canonical(h.url);
181
+ const have = byKey.get(key);
182
+ if (have)
183
+ have.hit.sources = [...new Set([...(have.hit.sources ?? []), s.name])];
184
+ else
185
+ byKey.set(key, {
186
+ hit: { ...h, sources: [s.name] },
187
+ by: s.by ?? s.name,
188
+ from: s.from ?? "tool",
189
+ });
190
+ }
191
+ }
192
+ for (const [key, { hit, by, from }] of byKey) {
193
+ const known = idOf.get(key);
194
+ if (known) {
195
+ await ctx.write({ type: "found", hit: known, query: q.id }, { from, by });
196
+ again++;
197
+ continue;
198
+ }
199
+ const [view] = dedupeHits([hit]);
200
+ if (!view)
201
+ continue;
202
+ idOf.set(key, await ctx.write({ type: "hit", ...view, key, queries: [q.id] }, { from, by }));
203
+ written++;
204
+ }
205
+ }
206
+ if (transient && !answered)
207
+ throw transient;
208
+ // A query every searcher answered with no new hit is a gap: read from the records, so a
209
+ // rerun after a Transient neither repeats one nor writes one for a query it skipped.
210
+ const after = await ctx.deps.ledger.trace(ctx.run);
211
+ const okOn = new Set(after
212
+ .filter((r) => r.type === "spend" && r.op === "search" && r.ok && r.round === ctx.round)
213
+ .map((r) => `${r.on}\u0000${r.source}`));
214
+ const hitOn = new Set(after.flatMap((r) => (r.type === "hit" ? r.queries : r.type === "found" ? [r.query] : [])));
215
+ const gapOn = new Set(after.flatMap((r) => (r.type === "gap" && r.on ? [r.on] : [])));
216
+ let empty = 0;
217
+ for (const q of queries) {
218
+ // A tree's query is thin, not a gap: the tree says so when a facet's every try came up empty.
219
+ if (q.facet || hitOn.has(q.id) || gapOn.has(q.id))
220
+ continue;
221
+ if (!searchersFor(q.follow ? followers : searchers, q).every((s) => okOn.has(`${q.id}\u0000${s.name}`)))
222
+ continue;
223
+ await ctx.write({ type: "gap", on: q.id, note: `query "${q.terms}": no new hits` }, tool(ctx.deps.writer));
224
+ empty++;
225
+ }
226
+ const judged = await judgeHits(ctx, fold(ctx.run, await ctx.deps.ledger.trace(ctx.run)));
227
+ return {
228
+ ok: true,
229
+ note: `${written} new hits${again ? `, ${again} found again,` : ""} from ${calls} searches${empty ? `, ${empty} queries with none` : ""}${passNote(judged)}`,
230
+ };
231
+ };
232
+ export async function readHits(ctx, state, wanted) {
233
+ // The tree's cap is the one hard stop on reads.
234
+ const room = wanted.length ? await treeRoom(ctx.deps, ctx.run) : null;
235
+ if (room && !room.left)
236
+ return {
237
+ ok: false,
238
+ note: `the tree under ${room.root} read ${room.read} pages, its cap ${room.cap}: stop a run, or plan a new root with a higher runs.reads`,
239
+ };
240
+ const hits = room ? wanted.slice(0, room.left) : wanted;
241
+ const capped = hits.length < wanted.length ? `; the tree's cap left ${hits.length} of ${wanted.length}` : "";
242
+ const listed = readersOf(ctx, state).filter((s) => s.open);
243
+ // A site's own reader before the general openers; the allow-list's order inside each.
244
+ const openers = [...listed.filter((s) => s.first), ...listed.filter((s) => !s.first)];
245
+ if (hits.length && !openers.length)
246
+ return { ok: false, note: "no open source in the allow-list" };
247
+ let transient = null;
248
+ /**
249
+ * The openers that claim the URL, in order; the first document wins. A miss (the source threw:
250
+ * a 403, a block) has its spend `ok: false` and leaves the next its turn, its reason kept for
251
+ * the gap. A Transient leaves it to the next try. No document and no Transient: `missed`, so
252
+ * the hit is a gap.
253
+ */
254
+ const openOne = async (h) => {
255
+ const why = [];
256
+ let retrying = false;
257
+ for (const s of openers) {
258
+ if (s.opens && !s.opens(h.url))
259
+ continue;
260
+ try {
261
+ const doc = await ctx.invoke(s, "open", h.url, h.id, () => s.open(h).catch((err) => {
262
+ if (!isTransient(err))
263
+ why.push(`${s.name}: ${message(err)}`);
264
+ throw err;
265
+ }));
266
+ if (doc)
267
+ return { doc, source: s };
268
+ }
269
+ catch (err) {
270
+ if (!isTransient(err))
271
+ throw err;
272
+ transient ??= err;
273
+ retrying = true;
274
+ }
275
+ }
276
+ return { missed: retrying ? null : why };
277
+ };
278
+ const gapOn = new Set(state.gaps.map((g) => g.on));
279
+ let read = 0;
280
+ let missed = 0;
281
+ await batched([...hits], openOne, async (h, g) => {
282
+ if (!("doc" in g)) {
283
+ if (!g.missed || gapOn.has(h.id))
284
+ return;
285
+ const why = g.missed.length ? `: ${g.missed.join("; ")}` : "";
286
+ await ctx.write({ type: "gap", on: h.id, note: `no source could open ${h.url}${why}` }, tool(ctx.deps.writer));
287
+ missed++;
288
+ return;
289
+ }
290
+ await ctx.write({ type: "document", ...g.doc, hit: h.id }, { from: g.source.from ?? "tool", by: g.source.by ?? g.source.name });
291
+ read++;
292
+ });
293
+ if (transient && !read)
294
+ throw transient;
295
+ // Openers this machine has outside the run's allow-list: the agent can let them in.
296
+ const allowed = new Set(openers.map((s) => s.name));
297
+ const outside = [...ctx.deps.sources.values()].filter((s) => s.open && !allowed.has(s.name));
298
+ const hint = missed && outside.length
299
+ ? `; not in the allow-list: ${outside.map((s) => s.name).join(", ")} (open --with)`
300
+ : "";
301
+ const judged = read
302
+ ? await judgeDocuments(ctx, fold(ctx.run, await ctx.deps.ledger.trace(ctx.run)))
303
+ : { judged: 0, failed: 0 };
304
+ return {
305
+ ok: true,
306
+ note: `${read} of ${hits.length} hits read${missed ? `, ${missed} no source could open` : ""}${capped}${hint}${passNote(judged)}`,
307
+ };
308
+ }
309
+ const stopped = (t, finished) => new Transient(`${t.message} (${finished} before it stopped; run the step again for the rest)`, {
310
+ retryAfter: t.retryAfter,
311
+ ...(t.status === undefined ? {} : { status: t.status }),
312
+ cause: t,
313
+ });
314
+ /** Extracts `docs` with the extractor on the run's model; the agent's `extract` passes its own list. */
315
+ export async function extractDocuments(ctx, state, docs) {
316
+ if (!docs.length)
317
+ return { ok: true, note: "nothing to extract" };
318
+ if (!ctx.model)
319
+ return { ok: false, note: "no model: nothing extracted" };
320
+ const ask = state.snapshot?.ask;
321
+ if (!ask)
322
+ return { ok: false, note: "no snapshot" };
323
+ const known = [...new Set([...ask.known, ...found(state)])];
324
+ const extractOne = async (d) => {
325
+ const { type: _t, id, run: _r, round: _ro, step: _s, at: _a, from: _f, by: _b, hit: _h, ...doc } = d;
326
+ const model = ctx.modelOn(id);
327
+ return {
328
+ out: await ctx.deps.extractor.extract(doc, { text: ask.text, known, steer: ask.steer }, model),
329
+ model,
330
+ };
331
+ };
332
+ let proposed = 0;
333
+ let dropped = 0;
334
+ let done = 0;
335
+ let continued = 0;
336
+ // Each document's findings and verdict land as its batch settles: a document whose extract
337
+ // fails costs only itself. The verdict is the checkpoint: the documents without one are
338
+ // extracted on the next try.
339
+ const got = await batched([...docs], extractOne, async (d, g) => {
340
+ const who = { from: "ai", by: g.model.id };
341
+ for (const f of g.out.findings)
342
+ await ctx.write({ type: "finding", ...f, document: d.id, ...(d.date ? { date: d.date } : {}) }, who);
343
+ const { quality, gap } = g.out.verdict;
344
+ await ctx.write({ type: "verdict", document: d.id, quality, ...(gap ? { gap } : {}) }, who);
345
+ proposed += g.out.findings.length;
346
+ dropped += g.out.dropped.length;
347
+ continued += g.out.continued ?? 0;
348
+ done++;
349
+ });
350
+ if (got.transient)
351
+ throw stopped(got.transient, `${proposed} findings from ${done} of ${docs.length} documents`);
352
+ return {
353
+ ok: true,
354
+ note: `${proposed} findings from ${done} documents, ${dropped} dropped at the gate${continued ? `, ${continued} cut answers continued` : ""}`,
355
+ };
356
+ }
357
+ /**
358
+ * The sample check: a few kept findings, spread across the round's documents, each read by a
359
+ * model against its document. A finding whose quote does not back it gets a check record and a
360
+ * drop call; one that holds gets a check record. The share that fails is the review's read on
361
+ * the extract. A check record is the checkpoint: a rerun checks only the rest of the sample.
362
+ */
363
+ export async function checkSample(ctx, state, sample) {
364
+ const checked = new Set(state.checks.map((c) => c.on));
365
+ const pool = state.findings.filter((f) => f.proof.quote && kept(state, f) && !checked.has(f.id));
366
+ const judged = await judgeFindings(ctx, state, pool);
367
+ const now = judged.judged ? fold(ctx.run, await ctx.deps.ledger.trace(ctx.run)) : state;
368
+ const inSample = new Set(sample.map((f) => f.id));
369
+ const doubted = pool.filter((f) => !inSample.has(f.id) && (yesOf(now, f.id, "backs") ?? 1) < DOUBT);
370
+ const findings = [...sample, ...doubted];
371
+ const judgeNote = passNote(judged);
372
+ if (!findings.length)
373
+ return { ok: true, note: `nothing to check${judgeNote}` };
374
+ if (!ctx.model)
375
+ return { ok: true, note: `no model: no checks${judgeNote}` };
376
+ const docs = new Map(state.documents.map((d) => [d.id, d]));
377
+ const checkOne = async (f) => {
378
+ const d = docs.get(f.document);
379
+ if (!d)
380
+ throw new Error(`finding ${f.id}: no document ${f.document}`);
381
+ const model = ctx.modelOn(f.id);
382
+ return { out: await checkFinding(f, d, model), model };
383
+ };
384
+ let ok = 0;
385
+ let failed = 0;
386
+ let fixed = 0;
387
+ /** The fix as a finding when the gate passes it and a second check backs it. */
388
+ const repair = async (f, fix, model) => {
389
+ const d = docs.get(f.document);
390
+ const gated = gateFindings({ findings: [{ text: fix, quote: f.proof.quote ?? "" }] }, d)
391
+ .findings[0];
392
+ if (!gated)
393
+ return;
394
+ const again = await checkFinding(gated, d, model).catch(() => null);
395
+ if (!again)
396
+ return;
397
+ const who = { from: "ai", by: model.id };
398
+ const id = await ctx.write({
399
+ type: "finding",
400
+ ...gated,
401
+ document: d.id,
402
+ ...(f.date ? { date: f.date } : {}),
403
+ fixes: f.id,
404
+ }, who);
405
+ await ctx.write({ type: "check", on: id, ok: again.ok, why: again.why }, who);
406
+ if (again.ok)
407
+ fixed++;
408
+ else
409
+ await ctx.write({ type: "call", on: id, verdict: "drop", why: again.why }, who);
410
+ };
411
+ const got = await batched([...findings], checkOne, async (f, g) => {
412
+ if (!g.out)
413
+ return;
414
+ const who = { from: "ai", by: g.model.id };
415
+ await ctx.write({ type: "check", on: f.id, ok: g.out.ok, why: g.out.why }, who);
416
+ if (g.out.ok)
417
+ ok++;
418
+ else {
419
+ await ctx.write({ type: "call", on: f.id, verdict: "drop", why: g.out.why }, who);
420
+ failed++;
421
+ if (g.out.fix)
422
+ await repair(f, g.out.fix, g.model);
423
+ }
424
+ });
425
+ if (got.transient)
426
+ throw stopped(got.transient, `${ok + failed} of ${sample.length} checks done`);
427
+ const why = doubted.length
428
+ ? ` (${sample.length} sampled, ${doubted.length} the judge doubted)`
429
+ : "";
430
+ return {
431
+ ok: true,
432
+ note: `${ok + failed} checked${why}, ${failed} not backed by their quote${fixed ? ` (${fixed} fixed: a narrower sentence on the same quote)` : ""}${judgeNote}`,
433
+ };
434
+ }
435
+ //# sourceMappingURL=steps.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"steps.js","sourceRoot":"","sources":["../../src/engine/steps.ts"],"names":[],"mappings":"AAAA;;;;;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C,EAAE,CAAC;IACP,OAAO;QACL,EAAE,EAAE,IAAI;QACR,IAAI,EAAE,GAAG,EAAE,GAAG,MAAM,WAAW,GAAG,KAAK,MAAM,6BAA6B,KAAK,CAAC,CAAC,CAAC,KAAK,KAAK,gDAAgD,CAAC,CAAC,CAAC,EAAE,GAAG,SAAS,EAAE;KAChK,CAAC;AACJ,CAAC"}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * The run tree: a child run names its parent on every snapshot. A wide run finds the niches;
3
+ * a child per niche goes deep; a stopped child's patterns and dead ends reach the parent's next
4
+ * plan. Each child keeps its own budget, stop and resume. A child's budget is never more than
5
+ * its parent's. The depth and the children per round are guard rails: past the depth a warning.
6
+ * The one hard stop is the pages the whole tree reads, pinned on the root's snapshot: depth costs
7
+ * nothing a sibling would not, so only the total is worth a wall.
8
+ */
9
+ import type { SnapshotRecord } from "../ledger/records.js";
10
+ import { costOf } from "./cost.js";
11
+ import type { Budget, PlanFile } from "./plan.js";
12
+ import type { Deps } from "./ports.js";
13
+ /** The caps when the plan file names none: the root and its children; six children per round. */
14
+ export declare const RUNS: {
15
+ depth: number;
16
+ children: number;
17
+ };
18
+ export type Parent = NonNullable<SnapshotRecord["parent"]>;
19
+ type Reads = Pick<Deps, "ledger">;
20
+ /** The runs that name this one as parent, each by its first snapshot, in the order they began. */
21
+ export declare function childrenOf(deps: Reads, run: string): Promise<SnapshotRecord[]>;
22
+ /** The pages a tree may read, for a root's snapshot: the plan file's, else from the root's budget. */
23
+ export declare const treeReadsOf: (planFile: PlanFile, budget: Budget) => number;
24
+ /** The run's ancestors, root first, each by its latest snapshot. */
25
+ export declare function lineageOf(deps: Reads, run: string): Promise<SnapshotRecord[]>;
26
+ /** What the tree the run sits in may still read; null for a tree with no cap (a run before 0.21). */
27
+ export declare function treeRoom(deps: Reads, run: string): Promise<{
28
+ root: string;
29
+ cap: number;
30
+ read: number;
31
+ left: number;
32
+ } | null>;
33
+ /** Per field, the smaller: a child never gets more than its parent. */
34
+ export declare const minBudget: (a: Budget, b: Budget) => Budget;
35
+ /** The plan file's budget, capped by the parent's on each round of a child. */
36
+ export declare function budgetUnder(deps: Reads, parent: Parent | undefined, budget: Budget): Promise<Budget>;
37
+ /**
38
+ * A new child's link, checked: the parent has a plan, its round has room for one more child, and
39
+ * the tree has pages left to read. Past the depth, a warning: the why says why deeper beats a
40
+ * sibling.
41
+ */
42
+ export declare function linkTo(deps: Reads, to: {
43
+ run: string;
44
+ why: string;
45
+ }, planFile: PlanFile): Promise<{
46
+ parent: Parent;
47
+ warnings: string[];
48
+ }>;
49
+ /**
50
+ * What a parent's next plan reads from its children. A stopped child gives known lines: its
51
+ * stop, its patterns with their sites per class, its dead ends (facets to skip). A child still
52
+ * going is open. The lines name the child run and the pattern: the ledger is their source.
53
+ */
54
+ export declare function fromChildren(deps: Reads, run: string): Promise<{
55
+ known: string[];
56
+ open: string[];
57
+ }>;
58
+ /** For a new child: what its parent and siblings already found that bears on its ask, with URLs. */
59
+ export declare function fromSiblings(deps: Reads, parent: string, ask: string, n?: number): Promise<string[]>;
60
+ export interface ChildView {
61
+ run: string;
62
+ ask: string;
63
+ why: string;
64
+ /** The parent's round it began in. */
65
+ round: number;
66
+ stopped: boolean;
67
+ patterns: number;
68
+ reads: number;
69
+ }
70
+ /** The run's children, and what the whole tree under it read and cost. */
71
+ export declare function runTree(deps: Reads, run: string): Promise<{
72
+ children: ChildView[];
73
+ reads: number;
74
+ cost: ReturnType<typeof costOf>;
75
+ }>;
76
+ export {};