bareloop 0.55.0 → 0.57.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.
- package/CHANGELOG.md +82 -0
- package/README.md +3 -3
- package/bareloop.context.md +69 -209
- package/package.json +2 -2
- package/src/authorflow.js +164 -340
- package/src/authoring.js +7 -127
- package/src/authorjob.js +139 -472
- package/src/authorreadout.js +24 -368
- package/src/authorrun.js +21 -215
- package/src/behaviour.js +14 -0
- package/src/blockasks.js +43 -0
- package/src/bundlerun.js +4 -1
- package/src/checkblock.js +798 -0
- package/src/checklist.js +128 -0
- package/src/checkpick.js +39 -312
- package/src/checkwords.js +3 -1
- package/src/cli.js +3 -3
- package/src/declaredclose.js +38 -108
- package/src/destpoint.js +4 -2
- package/src/draftspend.js +46 -0
- package/src/facts.js +8 -1
- package/src/index.js +3 -43
- package/src/interviewrun.js +13 -8
- package/src/job.js +20 -1
- package/src/jobblock.js +11 -46
- package/src/judged.js +13 -1229
- package/src/kinds.js +5 -347
- package/src/noteask.js +124 -0
- package/src/pagedry.js +27 -5
- package/src/pagemap.js +15 -3
- package/src/pagerules.js +18 -3
- package/src/panel/authorroutes.js +24 -6
- package/src/panel/authorsession.js +222 -177
- package/src/panel/importrun.js +0 -1
- package/src/panel/index.html +198 -26
- package/src/panel/jobplan.js +87 -33
- package/src/panel/server.js +74 -68
- package/src/panel/status.js +3 -0
- package/src/pickrecord.js +34 -55
- package/src/placerule.js +355 -0
- package/src/planrun.js +2 -48
- package/src/promptregisters.js +13 -17
- package/src/providerrows.js +0 -36
- package/src/providers.js +4 -25
- package/src/questionbox.js +104 -0
- package/src/rowedit.js +115 -0
- package/src/rubricstep.js +234 -127
- package/src/run.js +11 -16
- package/src/userrun.js +60 -112
- package/types/authorflow.d.ts +41 -197
- package/types/authoring.d.ts +1 -14
- package/types/authorjob.d.ts +35 -59
- package/types/authorreadout.d.ts +10 -88
- package/types/behaviour.d.ts +12 -0
- package/types/blockasks.d.ts +36 -0
- package/types/checkblock.d.ts +376 -0
- package/types/checklist.d.ts +134 -0
- package/types/checkpick.d.ts +8 -140
- package/types/cli.d.ts +1 -3
- package/types/declaredclose.d.ts +7 -33
- package/types/destpoint.d.ts +2 -0
- package/types/draftspend.d.ts +21 -0
- package/types/index.d.ts +2 -4
- package/types/job.d.ts +8 -0
- package/types/jobblock.d.ts +8 -38
- package/types/judged.d.ts +7 -357
- package/types/kinds.d.ts +3 -104
- package/types/noteask.d.ts +76 -0
- package/types/pagedry.d.ts +13 -0
- package/types/pagemap.d.ts +4 -0
- package/types/pagerules.d.ts +7 -0
- package/types/panel/authorsession.d.ts +47 -5
- package/types/panel/jobgrid.d.ts +8 -8
- package/types/panel/jobplan.d.ts +48 -10
- package/types/panel/server.d.ts +27 -18
- package/types/panel/status.d.ts +5 -0
- package/types/pickrecord.d.ts +20 -13
- package/types/placerule.d.ts +194 -0
- package/types/planrun.d.ts +1 -22
- package/types/providerrows.d.ts +0 -31
- package/types/providers.d.ts +3 -19
- package/types/questionbox.d.ts +87 -0
- package/types/rowedit.d.ts +32 -0
- package/types/rubricstep.d.ts +100 -48
- package/types/run.d.ts +9 -19
- package/types/userrun.d.ts +0 -1
- package/src/calibrate.js +0 -702
- package/src/cardauthor.js +0 -502
- package/src/fitcheck.js +0 -166
- package/types/calibrate.d.ts +0 -201
- package/types/cardauthor.d.ts +0 -358
- package/types/fitcheck.d.ts +0 -113
package/src/cardauthor.js
DELETED
|
@@ -1,502 +0,0 @@
|
|
|
1
|
-
// THE TWO SIGNED ARTIFACTS — softgreen module 4 (design record
|
|
2
|
-
// docs/logs/2026-08-17-softgreen-review-door-design.md §4.3 and §4.4,
|
|
3
|
-
// plus the 2026-08-18 threshold addendum).
|
|
4
|
-
//
|
|
5
|
-
// Module 1 built the judged floor's core, module 2 wired the stage into a close,
|
|
6
|
-
// module 3 opened the class and asked the extra question. Nothing compiled its
|
|
7
|
-
// answer. This module does exactly that and nothing more:
|
|
8
|
-
//
|
|
9
|
-
// Judge Examples → THE RUBRIC CARD (§4.3) — the person's own pass/fail
|
|
10
|
-
// lines, as ENUMERATED items over the arbiter's own
|
|
11
|
-
// rulebook, compiled from the "why" half of their answer.
|
|
12
|
-
// → THE CALIBRATION SET (§4.4) — TEN cases, frozen, that the
|
|
13
|
-
// whole pipe must grade before the close is signable.
|
|
14
|
-
//
|
|
15
|
-
// PRD item 33 M3 piece 3 (2026-09-13): the old Q6 ("what separates a pass from
|
|
16
|
-
// a fail?") and Q7 ("give one you'd pass and one you'd fail, and say why")
|
|
17
|
-
// merged into ONE form field, Judge Examples — Q7's wording, verbatim
|
|
18
|
-
// (`SOFTGREEN_JUDGE_EXAMPLES_KEY`/`JUDGE_EXAMPLES_QUESTION`, `authorflow.js`).
|
|
19
|
-
// Q6 is not lost: the "why" lines of a real pass/fail pair are what this
|
|
20
|
-
// module already compiled a card FROM, and asking the differentiator twice —
|
|
21
|
-
// once in the abstract, once against a real example — was the double-ask.
|
|
22
|
-
//
|
|
23
|
-
// ── THE D5 PATH, AND WHY IT IS THIS ONE ─────────────────────────────────────
|
|
24
|
-
//
|
|
25
|
-
// Both artifacts are SHOWN AND FIXED: an LLM proposes the extractable rewrite of
|
|
26
|
-
// what the person said, the SIGNER fixes it, and THE FIXED VERSION IS WHAT IS
|
|
27
|
-
// STORED — enumerated inside the spec hash, so a card line or a case is a re-sign
|
|
28
|
-
// exactly like every other spec edit. Asking the person to write the rubric
|
|
29
|
-
// themselves is the SWE tax this product refuses; letting the model's draft ride
|
|
30
|
-
// unsigned is the arbiter authored by a model. The signer in the middle is the
|
|
31
|
-
// whole design.
|
|
32
|
-
//
|
|
33
|
-
// ── WHAT THIS MODULE DOES NOT DO ────────────────────────────────────────────
|
|
34
|
-
//
|
|
35
|
-
// * IT DOES NOT GRADE. The calibration GATE — running LOCATE and `decide()`
|
|
36
|
-
// over the ten and refusing a signature on anything short of 10/10 with
|
|
37
|
-
// itemized reds, alongside the upstream INJECTION_BATTERY — is module 5. What
|
|
38
|
-
// is built here is what that gate reads.
|
|
39
|
-
// * IT STORES NO INJECTION CASES. The battery is upstream's and it is module
|
|
40
|
-
// 5's to run; a copy of it stored here would be a second inventory of
|
|
41
|
-
// somebody else's evidence.
|
|
42
|
-
// * IT PICKS NO NUMBER. `CALIBRATION_SIZE` is hamr's own ruling, verbatim
|
|
43
|
-
// (*"go build 10, we could double later"*). It is operator territory and a
|
|
44
|
-
// size change is a spec-level threshold change — the standing
|
|
45
|
-
// no-agent-threshold-picking rule binds here exactly as everywhere else.
|
|
46
|
-
// * IT ADMITS NO DOC-GENRE ARTIFACT. A case is code-genre text, because the
|
|
47
|
-
// catalogue is code-genre only.
|
|
48
|
-
//
|
|
49
|
-
// ── THE CASE SHAPE, AND WHY EACH FIELD EXISTS ───────────────────────────────
|
|
50
|
-
//
|
|
51
|
-
// { id, artifact, expect: { verdict: "pass"|"red", reds: [{ rule, fn }] } }
|
|
52
|
-
//
|
|
53
|
-
// `artifact` REAL TEXT — a file excerpt from the patient or a realistic
|
|
54
|
-
// snippet the signer edits. It is what LOCATE will be pointed at,
|
|
55
|
-
// so it is the artifact and never a description of one.
|
|
56
|
-
// `verdict` what `decide()` must render. Two values, both reachable.
|
|
57
|
-
// `reds` the ITEMIZED expectation, and the floor is itemized reds rather
|
|
58
|
-
// than verdict-match: a pipe that reds the right case for the wrong
|
|
59
|
-
// reason has not been calibrated, it has been lucky. `rule` selects
|
|
60
|
-
// from the CARD's own lines (a case cannot expect a red from a line
|
|
61
|
-
// the card never named — §4.3's ceiling, made mechanical), and `fn`
|
|
62
|
-
// names the function the red lands on, which is the address
|
|
63
|
-
// `decide()` itself reports.
|
|
64
|
-
//
|
|
65
|
-
// WHAT IS DELIBERATELY NOT STORED: the red's `why` and its `quote`. Both are the
|
|
66
|
-
// judge's own prose about a specific emission, and a signer cannot predict either
|
|
67
|
-
// — pinning them would fail an honest pipe on wording. `expectedOf` is the one
|
|
68
|
-
// spelling of that reduction, so the shape the signer stores and the shape the
|
|
69
|
-
// pipe produces are one shape read twice (module 5 compares them mechanically).
|
|
70
|
-
|
|
71
|
-
import {
|
|
72
|
-
JUDGE_RULE_IDS, JUDGE_RULES, CALIBRATION_SIZE, CASE_VERDICTS, validateJudgedArtifacts,
|
|
73
|
-
} from './judged.js';
|
|
74
|
-
import { askStructured, makeCostBook, SOFTGREEN_JUDGE_EXAMPLES_KEY, JUDGE_EXAMPLES_QUESTION } from './authorflow.js';
|
|
75
|
-
import { judgedStages } from './kinds.js';
|
|
76
|
-
import { isObj, redactSecrets } from './validate.js';
|
|
77
|
-
|
|
78
|
-
/** @typedef {{code: string, path: string, detail: string, [k: string]: any}} Red */
|
|
79
|
-
|
|
80
|
-
// ── THE NUMBERS, ALL OPERATOR-SET ───────────────────────────────────────────
|
|
81
|
-
//
|
|
82
|
-
// `CALIBRATION_SIZE` and `CASE_VERDICTS` are imported from `src/judged.js`, never
|
|
83
|
-
// re-spelled: what a legal set IS belongs to the arbiter beside the rulebook, and
|
|
84
|
-
// a second copy of the size here is a second threshold to keep in step.
|
|
85
|
-
|
|
86
|
-
/** the output channel for the compile. It takes no action; it carries the answer. */
|
|
87
|
-
export const PROPOSAL_TOOL_NAME = 'propose_rubric';
|
|
88
|
-
/** what the tool returns. The loop stops on delivery, so the model never reads it. */
|
|
89
|
-
export const PROPOSAL_ACK = 'proposal received';
|
|
90
|
-
/** the label the compile call is metered and persisted under — one spelling */
|
|
91
|
-
export const PROPOSAL_LABEL = 'judged-compile';
|
|
92
|
-
/** a malformed EMISSION is a transport fault, bounded on its own axis — the same
|
|
93
|
-
* ceiling `authorClose` declares for the declaration channel */
|
|
94
|
-
export const MAX_PROPOSAL_RETRIES = 2;
|
|
95
|
-
|
|
96
|
-
// ── 1. THE SCHEMA — the closed sets, handed over ────────────────────────────
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* The proposal's shape, and therefore the whole shape the compiler can express.
|
|
100
|
-
*
|
|
101
|
-
* `rule` is an ENUM off the judge's own rulebook in BOTH places it appears — the
|
|
102
|
-
* card's lines and a case's expected reds — because wherever only a closed set
|
|
103
|
-
* can satisfy a field, the illegal value is made INEXPRESSIBLE rather than
|
|
104
|
-
* rejected after the fact (the `check-passes(name)` model). The SIZE is pinned by
|
|
105
|
-
* `minItems`/`maxItems` from the one constant, so nine and eleven are both
|
|
106
|
-
* unsayable rather than argued about afterwards.
|
|
107
|
-
*
|
|
108
|
-
* `artifact` and `text` are the only free strings, and neither decides anything:
|
|
109
|
-
* one is the code the judge will read, the other is the signer's own words.
|
|
110
|
-
*/
|
|
111
|
-
export function proposalSchema() {
|
|
112
|
-
const ruleEnum = { enum: [...JUDGE_RULE_IDS], description: 'which owned rule this line asserts' };
|
|
113
|
-
return {
|
|
114
|
-
type: 'object',
|
|
115
|
-
properties: {
|
|
116
|
-
card: {
|
|
117
|
-
type: 'object',
|
|
118
|
-
description: 'the rubric, as enumerated items: one line per thing the person actually looks for, in the '
|
|
119
|
-
+ 'order they look for them.',
|
|
120
|
-
properties: {
|
|
121
|
-
items: {
|
|
122
|
-
type: 'array',
|
|
123
|
-
minItems: 1,
|
|
124
|
-
items: {
|
|
125
|
-
type: 'object',
|
|
126
|
-
properties: {
|
|
127
|
-
rule: ruleEnum,
|
|
128
|
-
text: { type: 'string', minLength: 1, description: 'what this line means, in the person\'s own words' },
|
|
129
|
-
},
|
|
130
|
-
required: ['rule', 'text'],
|
|
131
|
-
additionalProperties: false,
|
|
132
|
-
},
|
|
133
|
-
},
|
|
134
|
-
},
|
|
135
|
-
required: ['items'],
|
|
136
|
-
additionalProperties: false,
|
|
137
|
-
},
|
|
138
|
-
cases: {
|
|
139
|
-
type: 'array',
|
|
140
|
-
minItems: CALIBRATION_SIZE,
|
|
141
|
-
maxItems: CALIBRATION_SIZE,
|
|
142
|
-
description: `exactly ${CALIBRATION_SIZE} calibration cases, and at least one of each verdict — a set that `
|
|
143
|
-
+ 'cannot fail one way proves nothing',
|
|
144
|
-
items: {
|
|
145
|
-
type: 'object',
|
|
146
|
-
properties: {
|
|
147
|
-
id: { type: 'string', minLength: 1, description: 'a unique lowercase-hyphenated slug' },
|
|
148
|
-
artifact: {
|
|
149
|
-
type: 'string',
|
|
150
|
-
minLength: 1,
|
|
151
|
-
description: 'the REAL source text this case is about — what the judge will actually read, never a '
|
|
152
|
-
+ 'description of it',
|
|
153
|
-
},
|
|
154
|
-
expect: {
|
|
155
|
-
type: 'object',
|
|
156
|
-
properties: {
|
|
157
|
-
verdict: { enum: [...CASE_VERDICTS] },
|
|
158
|
-
reds: {
|
|
159
|
-
type: 'array',
|
|
160
|
-
description: 'the itemized expectation: empty for a pass, and one entry per card line the artifact '
|
|
161
|
-
+ 'violates for a red',
|
|
162
|
-
items: {
|
|
163
|
-
type: 'object',
|
|
164
|
-
properties: {
|
|
165
|
-
rule: ruleEnum,
|
|
166
|
-
fn: { type: 'string', minLength: 1, description: 'the function the red lands on' },
|
|
167
|
-
},
|
|
168
|
-
required: ['rule', 'fn'],
|
|
169
|
-
additionalProperties: false,
|
|
170
|
-
},
|
|
171
|
-
},
|
|
172
|
-
},
|
|
173
|
-
required: ['verdict', 'reds'],
|
|
174
|
-
additionalProperties: false,
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
required: ['id', 'artifact', 'expect'],
|
|
178
|
-
additionalProperties: false,
|
|
179
|
-
},
|
|
180
|
-
},
|
|
181
|
-
},
|
|
182
|
-
required: ['card', 'cases'],
|
|
183
|
-
additionalProperties: false,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* The output channel. It records and acknowledges; it takes no action, which is
|
|
189
|
-
* why handing it to a toolless compile call does not reopen D3.
|
|
190
|
-
* @param {{calls: any[]}} box
|
|
191
|
-
*/
|
|
192
|
-
export function proposalTool(box) {
|
|
193
|
-
return {
|
|
194
|
-
name: PROPOSAL_TOOL_NAME,
|
|
195
|
-
description: `Deliver the rubric card and the ${CALIBRATION_SIZE} calibration cases. Call this exactly once.`,
|
|
196
|
-
parameters: proposalSchema(),
|
|
197
|
-
execute: async (/** @type {any} */ args) => { box.calls.push(args); return PROPOSAL_ACK; },
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// ── 2. THE PROMPT ───────────────────────────────────────────────────────────
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* The system prompt a caller BINDS ITS SEAM WITH — this module owns no provider
|
|
205
|
-
* and picks none, exactly as `authorClose` does not: the compile call arrives as
|
|
206
|
-
* `generate`, and a caller wires it with `makeLoopGenerate(provider, {system:
|
|
207
|
-
* COMPILE_SYSTEM})`, whose own `maxTokens` default (F30) is what holds ten
|
|
208
|
-
* artifacts. Exported rather than inlined so the one register this call is made
|
|
209
|
-
* in is not re-spelled at every integration.
|
|
210
|
-
*/
|
|
211
|
-
export const COMPILE_SYSTEM = 'You compile a person\'s own words into a rubric a deterministic arbiter can run. You '
|
|
212
|
-
+ 'never decide whether anything passes, you never write code that runs, and you never invent a rule: the rulebook '
|
|
213
|
-
+ 'is fixed and you select from it.';
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* The compile prompt. The Judge Examples answer travels VERBATIM — it is the
|
|
217
|
-
* whole input, and paraphrasing a lay answer is how a rubric stops being the
|
|
218
|
-
* person's. PRD item 33 M3 piece 3: this used to be two answers (Q6+Q7);
|
|
219
|
-
* `SOFTGREEN_JUDGE_EXAMPLES_KEY` is the ONE key both the interview and this
|
|
220
|
-
* compile now read, so a future renumbering of the form moves both readers
|
|
221
|
-
* together rather than drifting.
|
|
222
|
-
*
|
|
223
|
-
* The rulebook is rendered from `JUDGE_RULES` itself rather than restated, for
|
|
224
|
-
* the reason the catalogue block is rendered from the catalogue: the prompt and
|
|
225
|
-
* the gate must describe one set of facts.
|
|
226
|
-
* @param {{answers: Record<string|number, any>, questions?: Record<string|number, string>}} o
|
|
227
|
-
*/
|
|
228
|
-
export function cardCasesPrompt({ answers, questions = {} }) {
|
|
229
|
-
const qKey = SOFTGREEN_JUDGE_EXAMPLES_KEY;
|
|
230
|
-
const q = questions[qKey] ?? JUDGE_EXAMPLES_QUESTION;
|
|
231
|
-
const rules = JUDGE_RULE_IDS.map((id) => `- ${id}\n what it can check: ${JUDGE_RULES[id].means}\n the facts it reads: ${JUDGE_RULES[id].ask.trim().split('\n')[0].trim()}`);
|
|
232
|
-
|
|
233
|
-
return `You are compiling ONE person's answers into the two artifacts a judged close is signed with.
|
|
234
|
-
|
|
235
|
-
They are not a software engineer. They described what they look for in their own words, and your whole job is to
|
|
236
|
-
express that over the rulebook below — never to add a standard they did not state, and never to drop one they did.
|
|
237
|
-
|
|
238
|
-
THEIR ANSWER — verbatim, and the only input you have
|
|
239
|
-
|
|
240
|
-
Q${qKey}. ${q}
|
|
241
|
-
A${qKey}. ${answers?.[qKey] ?? '(no answer given)'}
|
|
242
|
-
|
|
243
|
-
Each example is kept with the number of the job line the person wrote it under. Never attach an example to a different
|
|
244
|
-
line than the one it names (run mv1j01sl guessed which example belonged to which part of the goal).
|
|
245
|
-
|
|
246
|
-
---
|
|
247
|
-
|
|
248
|
-
THE RULEBOOK — the only rules that exist. You SELECT from it; a rule that is not here cannot be asked for.
|
|
249
|
-
|
|
250
|
-
${rules.join('\n')}
|
|
251
|
-
|
|
252
|
-
---
|
|
253
|
-
|
|
254
|
-
THE CARD
|
|
255
|
-
|
|
256
|
-
One item per thing they actually look for, in the order they look for them. \`rule\` is selected from the rulebook;
|
|
257
|
-
\`text\` is what that line means IN THEIR WORDS. The judge only ever checks lines this card names, so a standard you
|
|
258
|
-
leave out is a standard nobody checks — and a standard they never stated is one you must not add.
|
|
259
|
-
|
|
260
|
-
THE CALIBRATION SET
|
|
261
|
-
|
|
262
|
-
Exactly ${CALIBRATION_SIZE} cases. Each one is REAL SOURCE TEXT — a small, realistic file the judge could be pointed
|
|
263
|
-
at — plus what the arbiter must decide about it.
|
|
264
|
-
|
|
265
|
-
- \`artifact\` is the code itself, never a description of it.
|
|
266
|
-
- \`expect.verdict\` is "pass" when the artifact satisfies every card line, "red" when it violates any.
|
|
267
|
-
- \`expect.reds\` is EMPTY for a pass, and for a red it names one entry per violation: the card \`rule\` it breaks
|
|
268
|
-
and the \`fn\` (function name) it breaks it on.
|
|
269
|
-
|
|
270
|
-
AT LEAST ONE PASS AND AT LEAST ONE RED. A set that can only fail one way measures nothing.
|
|
271
|
-
Every rule you name in a case must be a rule your card carries.
|
|
272
|
-
A case may expect a red only where the rule's "what it can check" line says it can raise one on that artifact: a
|
|
273
|
-
function with any doc block above it never breaks has-doc, and "the doc only restates the function name" is a
|
|
274
|
-
says-what red, never a has-doc one.
|
|
275
|
-
|
|
276
|
-
HOW TO ANSWER
|
|
277
|
-
|
|
278
|
-
Call the ${PROPOSAL_TOOL_NAME} tool exactly once, with the card and the ${CALIBRATION_SIZE} cases as its arguments.
|
|
279
|
-
Do not write them as text.`;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// ── 3. THE COMPILE CHANNEL ──────────────────────────────────────────────────
|
|
283
|
-
|
|
284
|
-
/** the compile's own channel, over the SHARED bounded ladder (`askStructured`) */
|
|
285
|
-
const proposalChannel = () => ({
|
|
286
|
-
name: PROPOSAL_TOOL_NAME,
|
|
287
|
-
instruction: `Your reply did not deliver exactly one proposal through the ${PROPOSAL_TOOL_NAME} tool. `
|
|
288
|
-
+ `Call ${PROPOSAL_TOOL_NAME} exactly once, with the card and the ${CALIBRATION_SIZE} cases as its arguments. `
|
|
289
|
-
+ 'Do not write them as text.',
|
|
290
|
-
textPath: 'proposal',
|
|
291
|
-
tool: (/** @type {{calls: any[]}} */ box) => proposalTool(box),
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
// ── 4. SHOWN AND FIXED ──────────────────────────────────────────────────────
|
|
295
|
-
|
|
296
|
-
/** every free string in the two artifacts, scrubbed at INGEST — an answer, a
|
|
297
|
-
* proposed snippet and a signer's edit all become a SIGNED artefact that outlives
|
|
298
|
-
* the run, and a file that captures a key captures it forever. The ONE shared
|
|
299
|
-
* inventory, never a second one.
|
|
300
|
-
*
|
|
301
|
-
* AND IT ANNOUNCES. A scrub that silently rewrites bytes leaves a signer reading
|
|
302
|
-
* one artifact on screen and signing another on disk — the same failure
|
|
303
|
-
* `applyGenreEnv` refuses by returning its `dropped` list rather than quietly
|
|
304
|
-
* deleting a model-authored env var. Every altered value is reported by PATH (the
|
|
305
|
-
* path only: echoing the matched secret into the announcement would be the same
|
|
306
|
-
* leak, one hop on), so the caller can say *this was masked* out loud.
|
|
307
|
-
* @param {any} v @param {string} at @param {{path: string}[]} altered */
|
|
308
|
-
function scrubDeep(v, at, altered) {
|
|
309
|
-
if (typeof v === 'string') {
|
|
310
|
-
const masked = redactSecrets(v);
|
|
311
|
-
// `redactSecrets` returns a non-matching string byte-identical, so this
|
|
312
|
-
// compares equal on every honest value and costs the clean path nothing
|
|
313
|
-
if (masked !== v) altered.push({ path: at });
|
|
314
|
-
return masked;
|
|
315
|
-
}
|
|
316
|
-
if (Array.isArray(v)) return v.map((x, i) => scrubDeep(x, `${at}[${i}]`, altered));
|
|
317
|
-
if (isObj(v)) return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, scrubDeep(x, at ? `${at}.${k}` : k, altered)]));
|
|
318
|
-
return v;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
/**
|
|
322
|
-
* THE D5 MOMENT. The proposal is SHOWN; the signer's `fix` is what is STORED.
|
|
323
|
-
*
|
|
324
|
-
* The fix replaces the proposal WHOLE rather than merging into it. A merge would
|
|
325
|
-
* mean a signer who deleted a card line got it back, and "the signer signs the
|
|
326
|
-
* whole hash" is not a rule that survives a field-wise reconciliation. Handing
|
|
327
|
-
* back no fix is a legal answer — signing the proposal as proposed — and it says
|
|
328
|
-
* so (`source`), because *a person read this and changed nothing* and *nobody has
|
|
329
|
-
* looked* are two different facts.
|
|
330
|
-
*
|
|
331
|
-
* THE GATE RUNS OVER WHAT WILL BE STORED, never over the proposal: a signer can
|
|
332
|
-
* fix a legal proposal into an illegal one, and the artifact that reaches the
|
|
333
|
-
* hash is the one that must be legal.
|
|
334
|
-
*
|
|
335
|
-
* `scrubbed` is the INGEST SCRUB'S OWN ANNOUNCEMENT — the paths whose bytes the
|
|
336
|
-
* secret inventory masked on the way in. Empty on every honest artifact, and
|
|
337
|
-
* never silent when it is not: a signer signs a hash over the STORED bytes, so
|
|
338
|
-
* "what you typed is not what was stored" is a fact they are owed rather than one
|
|
339
|
-
* the redactor keeps.
|
|
340
|
-
* @param {{proposal: any, fix?: any}} o
|
|
341
|
-
* @returns {{ok: boolean, reds: Red[], card: any, cases: any[]|null, source: 'signer'|'proposal',
|
|
342
|
-
* scrubbed: {path: string}[]}}
|
|
343
|
-
*/
|
|
344
|
-
export function signJudgedArtifacts({ proposal, fix = null }) {
|
|
345
|
-
const source = /** @type {'signer'|'proposal'} */ (fix === null || fix === undefined ? 'proposal' : 'signer');
|
|
346
|
-
const chosen = source === 'signer' ? fix : proposal;
|
|
347
|
-
if (!isObj(chosen)) {
|
|
348
|
-
return {
|
|
349
|
-
ok: false,
|
|
350
|
-
reds: [{ code: 'invalid-value', path: 'proposal', detail: 'the signed artifacts are an object {card, cases}' }],
|
|
351
|
-
card: null,
|
|
352
|
-
cases: null,
|
|
353
|
-
source,
|
|
354
|
-
scrubbed: [],
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
// A COPY, scrubbed. The caller keeps its proposal (the run's own audit of what
|
|
358
|
-
// the model said); what is stored can never be reached back through it.
|
|
359
|
-
/** @type {{path: string}[]} */
|
|
360
|
-
const scrubbed = [];
|
|
361
|
-
const card = scrubDeep(structuredClone(/** @type {any} */ (chosen).card ?? null), 'card', scrubbed);
|
|
362
|
-
const cases = scrubDeep(structuredClone(/** @type {any} */ (chosen).cases ?? null), 'cases', scrubbed);
|
|
363
|
-
const v = validateJudgedArtifacts({ card, cases });
|
|
364
|
-
return { ok: v.ok, reds: v.reds, card, cases: Array.isArray(cases) ? cases : null, source, scrubbed };
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
// ── 5. THE FOLD — enumerated into the signed spec ───────────────────────────
|
|
368
|
-
|
|
369
|
-
/**
|
|
370
|
-
* Fold the two signed artifacts into the close declaration, ENUMERATED.
|
|
371
|
-
*
|
|
372
|
-
* WHERE EACH LANDS, and both are inside the spec so `jobSpecHash` covers them by
|
|
373
|
-
* construction (the hash is over the whole resolved spec):
|
|
374
|
-
* - THE CARD replaces the judged stage's own `params.card`. ONE card, and it is
|
|
375
|
-
* the SIGNED one: the composer's draft is a draft, and two cards in one close
|
|
376
|
-
* is the drift a single storage site exists to prevent. The stage is where it
|
|
377
|
-
* already lives, which is what `validateCard`, the M2 runner and the locate
|
|
378
|
-
* prompt all read.
|
|
379
|
-
* - THE CASES land beside the stages as `calibration.cases` — a close-level
|
|
380
|
-
* artifact, because the set calibrates the RULER and not one stage's params,
|
|
381
|
-
* and because module 5's gate reads it before any stage runs.
|
|
382
|
-
* - THE JUDGE MODEL lands beside them as `calibration.judgeModel`. Since PRD item
|
|
383
|
-
* 32.1 that is the RESOLVED judge identity the caller hands in (`resolveJudge`,
|
|
384
|
-
* src/judged.js — the spec's signed `judge` override, else the job's own worker
|
|
385
|
-
* model), not a library pin. A calibration set is only worth the judge that
|
|
386
|
-
* graded it, so the set has to carry which judge certified it or nothing
|
|
387
|
-
* downstream can tell a signed floor from a moved one. Storing it HERE, inside
|
|
388
|
-
* the spec, is what makes a judge change flip `jobSpecHash`: the signature dies,
|
|
389
|
-
* the signer re-signs, and a re-sign is a re-run of the calibration gate. It is
|
|
390
|
-
* REQUIRED, with no default — a set stamped with a model nobody named is exactly
|
|
391
|
-
* the unattributable floor this field exists to prevent (F45).
|
|
392
|
-
*
|
|
393
|
-
* A card with NOWHERE TO LAND THROWS. It is a caller's own broken input — a
|
|
394
|
-
* signed rubric folded into a close with no judged ruler — and there is no safe
|
|
395
|
-
* direction to degrade toward: storing it would sign a rubric nothing reads.
|
|
396
|
-
*
|
|
397
|
-
* The input is never mutated: a closeDecl is a signed artefact and rewriting a
|
|
398
|
-
* caller's copy in place is how two readers come to disagree about what was
|
|
399
|
-
* signed.
|
|
400
|
-
* @param {any} closeDecl @param {{card: any, cases: any[], judgeModel: string}} o
|
|
401
|
-
* @returns {any} a new closeDecl
|
|
402
|
-
*/
|
|
403
|
-
export function foldJudgedArtifacts(closeDecl, { card, cases, judgeModel }) {
|
|
404
|
-
if (typeof judgeModel !== 'string' || judgeModel.trim() === '') {
|
|
405
|
-
throw new Error('[cardauthor] foldJudgedArtifacts needs the resolved judgeModel (PRD item 32.1): the stored set '
|
|
406
|
-
+ 'carries the judge that certified it, and there is no library pin to fall back to.');
|
|
407
|
-
}
|
|
408
|
-
const out = structuredClone(closeDecl);
|
|
409
|
-
// `judgedStages` filters `out`'s OWN stage objects, so the card assignment
|
|
410
|
-
// below mutates the clone in place — the reason this reads `out`, not the
|
|
411
|
-
// caller's `closeDecl`.
|
|
412
|
-
const judged = judgedStages(out);
|
|
413
|
-
if (judged.length !== 1) {
|
|
414
|
-
throw new Error(`[cardauthor] the signed card folds into the close's ONE judged-floor stage, and this close has `
|
|
415
|
-
+ `${judged.length} — a rubric with nowhere to land is signed evidence nothing reads, and a second judged stage `
|
|
416
|
-
+ 'is a second bar for one verdict');
|
|
417
|
-
}
|
|
418
|
-
judged[0].params = { ...(isObj(judged[0].params) ? judged[0].params : {}), card: structuredClone(card) };
|
|
419
|
-
out.calibration = { cases: structuredClone(cases), judgeModel };
|
|
420
|
-
return out;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// ── 6. THE PAID SEAM ────────────────────────────────────────────────────────
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* ONE compile call: the person's Judge Examples answer in, a proposal out.
|
|
427
|
-
*
|
|
428
|
-
* It buys ONE artifact through ONE channel, and the malformed-emission ladder is
|
|
429
|
-
* the SHARED one (`askStructured`) rather than a second copy — a second ladder is
|
|
430
|
-
* a second place for the cap, the reject block and the re-ask to drift.
|
|
431
|
-
*
|
|
432
|
-
* WHAT IS AND IS NOT RETRIED, and the split is the standing one: a reply that
|
|
433
|
-
* carries no proposal is a TRANSPORT fault and is retried under the bound; a
|
|
434
|
-
* proposal that PARSED and then broke the ruling is not — the model answered
|
|
435
|
-
* perfectly clearly and re-asking buys another call to be told the same thing.
|
|
436
|
-
* The proposal is returned as it was said, never repaired: no JSON repair, ever.
|
|
437
|
-
*
|
|
438
|
-
* The book is the CALLER's, so one ceiling governs the survey, the declaration
|
|
439
|
-
* ladder and this compile — the advertised ceiling and the enforced ceiling are
|
|
440
|
-
* the same ceiling.
|
|
441
|
-
* @param {{answers: Record<string|number, any>, questions?: Record<string|number, string>,
|
|
442
|
-
* generate: Function, book?: ReturnType<typeof makeCostBook>,
|
|
443
|
-
* structuredMode?: 'tool'|'text', structureRetries?: number}} o
|
|
444
|
-
* @returns {Promise<{ok: boolean, proposal: any, reds: Red[], stop: string|null,
|
|
445
|
-
* attempts: number, raw: string, cost: any, raws: any[]}>}
|
|
446
|
-
*/
|
|
447
|
-
export async function proposeJudgedArtifacts({
|
|
448
|
-
answers, questions = {}, generate,
|
|
449
|
-
book = makeCostBook({}),
|
|
450
|
-
structuredMode = 'tool',
|
|
451
|
-
structureRetries = MAX_PROPOSAL_RETRIES,
|
|
452
|
-
}) {
|
|
453
|
-
// TIGHTEN-ONLY, floor 0, exactly as `authorClose` clamps its own two axes: a
|
|
454
|
-
// caller may buy fewer malformed-emission retries, never more than the ceiling
|
|
455
|
-
// this module declares and the suite pins.
|
|
456
|
-
const retries = Math.max(0, Math.min(Math.trunc(Number(structureRetries)) || 0, MAX_PROPOSAL_RETRIES));
|
|
457
|
-
const prompt = cardCasesPrompt({ answers, questions });
|
|
458
|
-
const ask = await askStructured({
|
|
459
|
-
messages: [{ role: 'user', content: prompt }],
|
|
460
|
-
generate,
|
|
461
|
-
mode: structuredMode,
|
|
462
|
-
retries,
|
|
463
|
-
label: PROPOSAL_LABEL,
|
|
464
|
-
book,
|
|
465
|
-
channel: proposalChannel(),
|
|
466
|
-
});
|
|
467
|
-
|
|
468
|
-
const base = {
|
|
469
|
-
proposal: ask.artifact ?? null, attempts: ask.attempts, raw: ask.raw,
|
|
470
|
-
cost: book.report(), raws: book.raws(),
|
|
471
|
-
};
|
|
472
|
-
if (ask.budget) {
|
|
473
|
-
return {
|
|
474
|
-
...base,
|
|
475
|
-
ok: false,
|
|
476
|
-
stop: ask.budget,
|
|
477
|
-
reds: [{
|
|
478
|
-
code: ask.budget,
|
|
479
|
-
path: 'budgetUsd',
|
|
480
|
-
detail: `the authoring ceiling bound before the rubric compile: $${book.ceilingUsd} is spent, and the call was `
|
|
481
|
-
+ 'never made. The stop IS the checkpoint — raise the ceiling and re-run, or read what is here and stop',
|
|
482
|
-
}],
|
|
483
|
-
};
|
|
484
|
-
}
|
|
485
|
-
if (ask.providerError) {
|
|
486
|
-
// a casualty, never evidence about the model — and the transport's own prose
|
|
487
|
-
// is scrubbed on the way into a red the caller keeps
|
|
488
|
-
return {
|
|
489
|
-
...base,
|
|
490
|
-
ok: false,
|
|
491
|
-
stop: 'provider-red',
|
|
492
|
-
reds: [{ code: 'provider-red', path: PROPOSAL_LABEL, detail: redactSecrets(String(ask.providerError)) }],
|
|
493
|
-
};
|
|
494
|
-
}
|
|
495
|
-
if (!ask.artifact) {
|
|
496
|
-
return { ...base, ok: false, stop: 'artifact-red', reds: [/** @type {Red} */ (ask.red)] };
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
const v = validateJudgedArtifacts({ card: ask.artifact.card, cases: ask.artifact.cases });
|
|
500
|
-
if (!v.ok) return { ...base, ok: false, stop: 'proposal-invalid', reds: v.reds };
|
|
501
|
-
return { ...base, ok: true, stop: null, reds: [] };
|
|
502
|
-
}
|
package/src/fitcheck.js
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
// THE FIT CHECK (docs/product/ITEM33-BUILD.md "Addendum 2026-10-21 - fit check build spec", F243, fit POC v6). A rubric job, at drafting, once, after the
|
|
2
|
-
// inputs are frozen and before the look: ONE paid AI call splits each job line into the short parts it asks for (words copied from that line) and says,
|
|
3
|
-
// for each, whether the saved page shows it, with a quote copied from the page. The AI never decides: plain code checks every ask against its own
|
|
4
|
-
// line and every quote against the page, and a part not found becomes one question to the person (never a stop by itself, never a count). Failure is
|
|
5
|
-
// OPEN - a wrong stop is the costly mistake - so a call that does not finish is `absent` and drafting goes on. The call/book/retry pattern is
|
|
6
|
-
// src/pagemap.js's `runPageMap`.
|
|
7
|
-
|
|
8
|
-
import { createRequire } from 'node:module';
|
|
9
|
-
import { priceOf, scrubRaw, tallyCalls, capStop, callCasualty } from './text.js';
|
|
10
|
-
import { redactSecrets } from './validate.js';
|
|
11
|
-
import { isTransportFailure } from './transport.js';
|
|
12
|
-
import { isRateLimited, rateLimitWaitMs } from './ratelimit.js';
|
|
13
|
-
import { isOutputCutOff } from './authorflow.js';
|
|
14
|
-
import { AUTHOR_CALL_TIMEOUT_MS, AUTHOR_SCOUT_MAX_TOKENS, SURVEY_CAUSES } from './authorscout.js';
|
|
15
|
-
|
|
16
|
-
const require = createRequire(import.meta.url);
|
|
17
|
-
const { Loop } = require('bare-agent');
|
|
18
|
-
|
|
19
|
-
/** the label of the one paid call, as the drafting book and the progress line see it */
|
|
20
|
-
export const FIT_CALL_LABEL = 'fit-check';
|
|
21
|
-
/** what the chat says when the check did not finish (drafting goes on) */
|
|
22
|
-
export const FIT_ABSENT_NOTE = 'The fit check did not finish; drafting goes on.';
|
|
23
|
-
/** the answer that keeps drafting after the fit question (any case, a closing full stop allowed) */
|
|
24
|
-
export const FIT_GO_ON = 'go on';
|
|
25
|
-
/** @param {unknown} text @returns {boolean} */
|
|
26
|
-
export const isGoOn = (text) => norm(String(text ?? '').trim().replace(/\.$/, '')) === FIT_GO_ON;
|
|
27
|
-
|
|
28
|
-
export const FIT_SYSTEM = 'You check whether saved web pages show what a job asks for. You never invent words: every ask must be copied from its job line and every quote must be copied from the page, character for character.';
|
|
29
|
-
|
|
30
|
-
/** collapse whitespace, lower case: the one spelling every fit comparison uses @param {unknown} s @returns {string} */
|
|
31
|
-
export const norm = (s) => String(s).replace(/\s+/g, ' ').trim().toLowerCase();
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* What the AI is told: the job lines (the numbered text only, no `~` rules) and the WHOLE text of every input under its label. Adapted from the POC
|
|
35
|
-
* call (.claude/scratch/fit-poc6/run.mjs): the asks are copied from each job line instead of hand-written.
|
|
36
|
-
* @param {{jobLines: {n: number, text: string}[], texts: Record<string, string>}} o
|
|
37
|
-
* @returns {string}
|
|
38
|
-
*/
|
|
39
|
-
export function fitPrompt({ jobLines, texts }) {
|
|
40
|
-
const job = jobLines.map((l) => `LINE ${l.n}: ${l.text}`).join('\n');
|
|
41
|
-
const pages = Object.entries(texts).map(([label, text]) => `THE PAGE (the input "${label}"):\n${text}`).join('\n\n');
|
|
42
|
-
return `THE JOB:\n${job}\n\n${pages}\n\n`
|
|
43
|
-
+ 'For EACH job line, split it into the short parts it asks for (for example where it starts, where it ends, a price, a time), using ONLY words copied from that line. '
|
|
44
|
-
+ 'Every line has at least one part. For EACH part, say whether the page shows it the way the job means it. A page that shows the opposite (for example a flight that arrives where the job says it leaves from, or a different city) does NOT show it. '
|
|
45
|
-
+ 'If found, copy a short piece of page text that shows it, character for character. If not found, quote is null. '
|
|
46
|
-
+ 'Reply with JSON only, no prose, no code fence: {"asks":[{"line":<line number>,"ask":"<the part, copied from that line>","found":true|false,"quote":"<page text or null>"}]}';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** @typedef {{line: number, ask: string, found: boolean, quote: string|null, madeUp: boolean, ignored?: 'number'}} FitAsk */
|
|
50
|
-
|
|
51
|
-
/** an ask that is only a number (a count the job gives, not something the page shows): digits, optional spaces, commas, dots @param {string} ask @returns {boolean} */
|
|
52
|
-
const isNumberOnly = (ask) => /^\d[\d\s,.]*$/.test(norm(ask));
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Read the AI's reply and decide, all in code. Malformed (no repair): not JSON, another form, a line that is not a job line, an ask not copied from its own
|
|
56
|
-
* line, found not a boolean, a quote neither text nor null, or a job line with no ask. An ask is found only if `found` is true AND its quote is on a page;
|
|
57
|
-
* `found` with a quote that is not on any page (or no quote) is made up and counts as not found.
|
|
58
|
-
* @param {unknown} text the reply
|
|
59
|
-
* @param {{n: number, text: string}[]} jobLines
|
|
60
|
-
* @param {Record<string, string>} texts the frozen input texts by label
|
|
61
|
-
* @returns {{status: 'malformed'}|{status: 'ok', state: 'fits'|'ask', asks: FitAsk[], notFound: {line: number, ask: string}[]}}
|
|
62
|
-
*/
|
|
63
|
-
export function decideFit(text, jobLines, texts) {
|
|
64
|
-
/** @type {any} */
|
|
65
|
-
let j;
|
|
66
|
-
try { j = JSON.parse(String(text ?? '').trim()); } catch { return { status: 'malformed' }; }
|
|
67
|
-
if (j === null || typeof j !== 'object' || Array.isArray(j) || !Array.isArray(j.asks)) return { status: 'malformed' };
|
|
68
|
-
const lines = new Map(jobLines.map((l) => [l.n, norm(l.text)]));
|
|
69
|
-
const pages = Object.values(texts).map(norm);
|
|
70
|
-
/** @type {FitAsk[]} */
|
|
71
|
-
const asks = [];
|
|
72
|
-
for (const a of j.asks) {
|
|
73
|
-
if (a === null || typeof a !== 'object' || Array.isArray(a)) return { status: 'malformed' };
|
|
74
|
-
if (!Number.isInteger(a.line) || !lines.has(a.line)) return { status: 'malformed' };
|
|
75
|
-
if (typeof a.ask !== 'string' || norm(a.ask) === '' || !(/** @type {string} */ (lines.get(a.line))).includes(norm(a.ask))) return { status: 'malformed' };
|
|
76
|
-
if (typeof a.found !== 'boolean') return { status: 'malformed' };
|
|
77
|
-
const quote = a.quote ?? null;
|
|
78
|
-
if (quote !== null && typeof quote !== 'string') return { status: 'malformed' };
|
|
79
|
-
const q = quote === null ? '' : norm(quote);
|
|
80
|
-
const onPage = q !== '' && pages.some((p) => p.includes(q));
|
|
81
|
-
// a bare number is ignored: kept in the record (flagged), never counted, never asked about
|
|
82
|
-
asks.push({ line: a.line, ask: a.ask, found: a.found && onPage, quote, madeUp: a.found && !onPage, ...(isNumberOnly(a.ask) ? { ignored: /** @type {'number'} */ ('number') } : {}) });
|
|
83
|
-
}
|
|
84
|
-
if (!jobLines.every((l) => asks.some((a) => a.line === l.n))) return { status: 'malformed' };
|
|
85
|
-
const notFound = asks.filter((a) => !a.ignored && !a.found).map((a) => ({ line: a.line, ask: a.ask }));
|
|
86
|
-
return { status: 'ok', state: notFound.length === 0 ? 'fits' : 'ask', asks, notFound };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** the not-found asks as the person reads them: `<ask> (line N), <ask> (line N)` @param {{line: number, ask: string}[]} notFound @returns {string} */
|
|
90
|
-
export const notFoundList = (notFound) => notFound.map((x) => `${x.ask} (line ${x.line})`).join(', ');
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* The one question, worded by code.
|
|
94
|
-
* @param {{found: number, total: number, notFound: {line: number, ask: string}[]}} o
|
|
95
|
-
* @returns {string}
|
|
96
|
-
*/
|
|
97
|
-
export const fitQuestionText = ({ found, total, notFound }) => redactSecrets(
|
|
98
|
-
`I found ${found} of ${total} of your asks on this page. Not found: ${notFoundList(notFound)}. Type go on to keep drafting, or anything else to stop and change the job or the page.`,
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
/** the default loop seam - one spelling of how this repo drives a model */
|
|
102
|
-
const defaultLoop = (/** @type {any} */ { system, onLlmResult, provider, rates }) =>
|
|
103
|
-
new Loop({ provider, system, onLlmResult, ...(rates ? { rates } : {}) });
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Run the fit check: one call (one retry on a malformed reply or a transport failure or a 429). Never throws on a call casualty; every failure is `absent`.
|
|
107
|
-
* @param {{jobLines: {n: number, text: string}[], texts: Record<string, string>, provider?: any, maxTokens?: number, ceilingUsd?: number|null,
|
|
108
|
-
* callTimeoutMs?: number, onCall?: (call: {label: string, costUsd: number|null, unpricedRounds: number, thinkingStatus?: string, raw?: string}) => void,
|
|
109
|
-
* createLoop?: Function, rates?: {in: number, out: number}|null, callOptions?: Record<string, any>, sleep?: (ms: number) => Promise<void>}} o
|
|
110
|
-
* @returns {Promise<{state: 'fits'|'ask'|'absent', asks: FitAsk[], notFound: {line: number, ask: string}[], reason: string|null, cause: string|null,
|
|
111
|
-
* calls: {label: string, costUsd: number|null, unpricedRounds: number}[], budgetStop: 'cap-halt'|'pricing-red'|null}>}
|
|
112
|
-
*/
|
|
113
|
-
export async function runFitCheck({
|
|
114
|
-
jobLines, texts, provider = null, maxTokens = AUTHOR_SCOUT_MAX_TOKENS, ceilingUsd = null, callTimeoutMs = AUTHOR_CALL_TIMEOUT_MS,
|
|
115
|
-
onCall = () => {}, createLoop = defaultLoop, rates = null, callOptions = {}, sleep = (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
116
|
-
}) {
|
|
117
|
-
const callTimeout = Number.isFinite(Number(callTimeoutMs)) && Number(callTimeoutMs) > 0
|
|
118
|
-
? Math.min(Math.trunc(Number(callTimeoutMs)), AUTHOR_CALL_TIMEOUT_MS) : AUTHOR_CALL_TIMEOUT_MS;
|
|
119
|
-
const priced = rates ? { rates } : {};
|
|
120
|
-
/** @type {{label: string, costUsd: number|null, unpricedRounds: number}[]} */
|
|
121
|
-
const calls = [];
|
|
122
|
-
let attempts = 0;
|
|
123
|
-
const record = (/** @type {any} */ r) => {
|
|
124
|
-
const call = { label: FIT_CALL_LABEL, ...priceOf(r) };
|
|
125
|
-
calls.push(call);
|
|
126
|
-
const raw = scrubRaw({ label: FIT_CALL_LABEL, attempt: attempts, text: r?.text });
|
|
127
|
-
onCall({ ...call, raw: raw.text, ...(r?.thinkingStatus === undefined ? {} : { thinkingStatus: r.thinkingStatus }) });
|
|
128
|
-
};
|
|
129
|
-
const absent = (/** @type {string} */ reason, /** @type {string} */ cause, /** @type {'cap-halt'|'pricing-red'|null} */ budgetStop = null) => ({
|
|
130
|
-
state: /** @type {'absent'} */ ('absent'), asks: [], notFound: [], reason, cause, calls, budgetStop,
|
|
131
|
-
});
|
|
132
|
-
const content = fitPrompt({ jobLines, texts });
|
|
133
|
-
let malformed = 0;
|
|
134
|
-
let transportRetried = false;
|
|
135
|
-
let rateRetried = false;
|
|
136
|
-
for (;;) {
|
|
137
|
-
// the cap is checked BEFORE every call, never inside one
|
|
138
|
-
const halt = capStop({ ceilingUsd, ...tallyCalls(calls) });
|
|
139
|
-
if (halt) return absent(halt === 'cap-halt' ? `the fit check was not made: the $${ceilingUsd} ceiling was already spent` : 'the fit check was not made: spend could not be priced, so the ceiling could not be enforced', SURVEY_CAUSES.NOT_FUNDED, halt);
|
|
140
|
-
attempts += 1;
|
|
141
|
-
const loop = createLoop({ system: FIT_SYSTEM, onLlmResult: undefined, provider, ...priced });
|
|
142
|
-
/** @type {any} */
|
|
143
|
-
let r;
|
|
144
|
-
try {
|
|
145
|
-
r = await loop.run([{ role: 'user', content }], [], { cacheMessages: true, maxTokens, timeoutMs: callTimeout, ...callOptions });
|
|
146
|
-
} catch (e) {
|
|
147
|
-
if (isRateLimited(e) && !rateRetried) {
|
|
148
|
-
const wait = rateLimitWaitMs(e);
|
|
149
|
-
if (wait !== null) { rateRetried = true; await sleep(wait); continue; }
|
|
150
|
-
}
|
|
151
|
-
const casualty = callCasualty(e) ?? (isTransportFailure(e) ? String(/** @type {any} */ (e)?.message ?? e) : null);
|
|
152
|
-
if (casualty === null) throw e;
|
|
153
|
-
// the failed attempt may already be billed: booked UNPRICED, so the spend stays "not complete" (never a $0)
|
|
154
|
-
record({ error: casualty });
|
|
155
|
-
if (!transportRetried) { transportRetried = true; continue; }
|
|
156
|
-
return absent(`the fit check call failed: ${redactSecrets(casualty)}`, SURVEY_CAUSES.CALL_FAILED);
|
|
157
|
-
}
|
|
158
|
-
record(r);
|
|
159
|
-
if (isOutputCutOff(r)) return absent('the fit check answer was cut off', SURVEY_CAUSES.UNPARSEABLE);
|
|
160
|
-
if (r?.error) return absent(`the fit check call failed: ${redactSecrets(String(r.error))}`, SURVEY_CAUSES.CALL_FAILED);
|
|
161
|
-
const d = decideFit(r?.text, jobLines, texts);
|
|
162
|
-
if (d.status === 'ok') return { state: d.state, asks: d.asks, notFound: d.notFound, reason: null, cause: null, calls, budgetStop: null };
|
|
163
|
-
malformed += 1;
|
|
164
|
-
if (malformed >= 2) return absent('the fit check answer was not readable twice', SURVEY_CAUSES.UNPARSEABLE);
|
|
165
|
-
}
|
|
166
|
-
}
|