@jive-sh/skill-sync 0.9.0 → 0.10.0-latest-darwin-x64
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/multi-call-binary +4 -0
- package/package.json +11 -45
- package/README.md +0 -208
- package/bin/package.json +0 -1
- package/bin/skill-sync +0 -34
- package/bin/skill-sync-daemon +0 -34
- package/dist/daemons/skill-sync-daemon.js +0 -640
- package/dist/daemons/skill-sync-daemon.js.map +0 -1
- package/dist/entrypoints/skill-sync.js +0 -7
- package/dist/entrypoints/skill-sync.js.map +0 -1
- package/dist/index.js +0 -10
- package/dist/index.js.map +0 -1
- package/dist/internal/agents/agent-tool.js +0 -102
- package/dist/internal/agents/agent-tool.js.map +0 -1
- package/dist/internal/agents/claude.js +0 -424
- package/dist/internal/agents/claude.js.map +0 -1
- package/dist/internal/agents/codex.js +0 -270
- package/dist/internal/agents/codex.js.map +0 -1
- package/dist/internal/agents/command.js +0 -219
- package/dist/internal/agents/command.js.map +0 -1
- package/dist/internal/agents/grok.js +0 -170
- package/dist/internal/agents/grok.js.map +0 -1
- package/dist/internal/agents/index.js +0 -70
- package/dist/internal/agents/index.js.map +0 -1
- package/dist/internal/cli.js +0 -185
- package/dist/internal/cli.js.map +0 -1
- package/dist/internal/config.js +0 -131
- package/dist/internal/config.js.map +0 -1
- package/dist/internal/control.js +0 -113
- package/dist/internal/control.js.map +0 -1
- package/dist/internal/daemon-control/impl.js +0 -68
- package/dist/internal/daemon-control/impl.js.map +0 -1
- package/dist/internal/daemon-control/interface.js +0 -4
- package/dist/internal/daemon-control/interface.js.map +0 -1
- package/dist/internal/folders.js +0 -47
- package/dist/internal/folders.js.map +0 -1
- package/dist/internal/local-skills.js +0 -43
- package/dist/internal/local-skills.js.map +0 -1
- package/dist/internal/path.js +0 -36
- package/dist/internal/path.js.map +0 -1
- package/dist/internal/server.js +0 -80
- package/dist/internal/server.js.map +0 -1
- package/dist/internal/skill-check.js +0 -83
- package/dist/internal/skill-check.js.map +0 -1
- package/dist/internal/skill-refinement/evals/cases.js +0 -143
- package/dist/internal/skill-refinement/evals/cases.js.map +0 -1
- package/dist/internal/skill-refinement/evals/opus-action-judge-original.json +0 -350
- package/dist/internal/skill-refinement/evals/opus-judge-original.json +0 -165
- package/dist/internal/skill-refinement/feedback/intake.js +0 -52
- package/dist/internal/skill-refinement/feedback/intake.js.map +0 -1
- package/dist/internal/skill-refinement/jev/impl.js +0 -63
- package/dist/internal/skill-refinement/jev/impl.js.map +0 -1
- package/dist/internal/skill-refinement/jev/interface.js +0 -6
- package/dist/internal/skill-refinement/jev/interface.js.map +0 -1
- package/dist/internal/skill-refinement/paths.js +0 -62
- package/dist/internal/skill-refinement/paths.js.map +0 -1
- package/dist/internal/skill-refinement/platform.js +0 -67
- package/dist/internal/skill-refinement/platform.js.map +0 -1
- package/dist/internal/skill-refinement/service.js +0 -261
- package/dist/internal/skill-refinement/service.js.map +0 -1
- package/dist/internal/skill-refinement/skills/skill-text.js +0 -217
- package/dist/internal/skill-refinement/skills/skill-text.js.map +0 -1
- package/dist/internal/skill-refinement/workflow/retry.js +0 -69
- package/dist/internal/skill-refinement/workflow/retry.js.map +0 -1
- package/dist/internal/skill-refinement/workflow/skill-refinement.js +0 -597
- package/dist/internal/skill-refinement/workflow/skill-refinement.js.map +0 -1
- package/dist/internal/skill-refinement/writer/impl.js +0 -74
- package/dist/internal/skill-refinement/writer/impl.js.map +0 -1
- package/dist/internal/skill-refinement/writer/interface.js +0 -6
- package/dist/internal/skill-refinement/writer/interface.js.map +0 -1
- package/dist/internal/sync.js +0 -182
- package/dist/internal/sync.js.map +0 -1
- package/dist/internal/wire.js +0 -31
- package/dist/internal/wire.js.map +0 -1
- package/postinstall.cjs +0 -663
- package/src/daemons/skill-sync-daemon.ts +0 -645
- package/src/entrypoints/skill-sync.ts +0 -12
- package/src/index.ts +0 -15
- package/src/internal/agents/agent-tool.ts +0 -149
- package/src/internal/agents/claude.ts +0 -442
- package/src/internal/agents/codex.ts +0 -288
- package/src/internal/agents/command.ts +0 -257
- package/src/internal/agents/grok.ts +0 -174
- package/src/internal/agents/index.ts +0 -74
- package/src/internal/cli.ts +0 -217
- package/src/internal/config.ts +0 -172
- package/src/internal/control.ts +0 -157
- package/src/internal/daemon-control/impl.ts +0 -80
- package/src/internal/daemon-control/interface.ts +0 -40
- package/src/internal/folders.ts +0 -50
- package/src/internal/local-skills.ts +0 -58
- package/src/internal/path.ts +0 -32
- package/src/internal/server.ts +0 -76
- package/src/internal/skill-check.ts +0 -97
- package/src/internal/skill-refinement/evals/cases.ts +0 -167
- package/src/internal/skill-refinement/evals/opus-action-judge-original.json +0 -350
- package/src/internal/skill-refinement/evals/opus-judge-original.json +0 -165
- package/src/internal/skill-refinement/feedback/intake.ts +0 -66
- package/src/internal/skill-refinement/jev/impl.ts +0 -76
- package/src/internal/skill-refinement/jev/interface.ts +0 -56
- package/src/internal/skill-refinement/paths.ts +0 -87
- package/src/internal/skill-refinement/platform.ts +0 -71
- package/src/internal/skill-refinement/service.ts +0 -307
- package/src/internal/skill-refinement/skills/skill-text.ts +0 -290
- package/src/internal/skill-refinement/workflow/retry.ts +0 -76
- package/src/internal/skill-refinement/workflow/skill-refinement.ts +0 -671
- package/src/internal/skill-refinement/writer/impl.ts +0 -86
- package/src/internal/skill-refinement/writer/interface.ts +0 -34
- package/src/internal/sync.ts +0 -216
- package/src/internal/wire.ts +0 -439
|
@@ -1,597 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SKILL REFINEMENT: ONE RUN PER FEEDBACK ITEM, EACH STEP A FRESH, SMALL CALL.
|
|
3
|
-
*
|
|
4
|
-
* Pick takes the head of the intake's queue.
|
|
5
|
-
* Calibrate judge Jev, the judge of reply cases, grades the original replies Alex objected to (and the one he called
|
|
6
|
-
* good) once per judge version; agreeing with him less often than the Opus judge did ends the run here.
|
|
7
|
-
* Classify Jev names the skill section the item is about, and whether it is new, a repeat or a correction;
|
|
8
|
-
* a section that points to another skill's section for the rule is asked again with those sections.
|
|
9
|
-
* Draft the generative model reads only that skill (with its line budget, where it states a cap), its
|
|
10
|
-
* example names, its rules for changing skills and the item, and writes candidate edits, the item's
|
|
11
|
-
* example and its A/B case. A candidate applies when its edits do, and every sentence it takes out
|
|
12
|
-
* that no synced skill holds after it is still required by its new text, as Jev judges.
|
|
13
|
-
* Choose examples Jev names the section that owns each A/B case's failure; the changed section's cases, and the
|
|
14
|
-
* item's own, are tested.
|
|
15
|
-
* A/B for each chosen case, the generative model writes the reply under the current text and under
|
|
16
|
-
* each candidate, two samples each.
|
|
17
|
-
* Judge each reply goes to its case kind's judge: an action case to the Opus judge, a reply case to Jev;
|
|
18
|
-
* candidates are ranked by their passes.
|
|
19
|
-
* Install the best candidate that fails no case the current text passes is written to the synced skill,
|
|
20
|
-
* rebased on any edit made to it since Draft, then skill-sync's synced list must show the skill
|
|
21
|
-
* edited after the install.
|
|
22
|
-
*
|
|
23
|
-
* With no winner (or no candidate that applies), Draft writes new candidates once more in the same run, reading why each
|
|
24
|
-
* reply failed, against the same case; the current text's replies and verdicts are reused, so only the new candidates
|
|
25
|
-
* are written and judged. Run on 23 past runs (2026-11-19), every run with no winner failed the item's own case under
|
|
26
|
-
* the current text and both candidates alike, most often for a part another rule of the skill requires (a quote of his
|
|
27
|
-
* message, a Blocked on you line), which a change to the one rule the item names cannot fix: those reasons reached Draft
|
|
28
|
-
* only as a note for a next run, and an item whose inputs are unchanged gets none.
|
|
29
|
-
*
|
|
30
|
-
* The content rules (what a good change is) live in the `autonomous-skill-refinement` skill around this code; Draft
|
|
31
|
-
* reads them from it each run. Jev classifies and judges; only Draft and A/B, which must write text, call a generative model.
|
|
32
|
-
*/
|
|
33
|
-
import { AgentStep, RUN_ENDS, RUN_STARTS, A_STEP_FAILS } from '@jive-sh/workflow-platform';
|
|
34
|
-
import { Effect, Option, Result, Schema } from 'effect';
|
|
35
|
-
import fs from 'node:fs';
|
|
36
|
-
import path from 'node:path';
|
|
37
|
-
import { Jev } from '../jev/interface';
|
|
38
|
-
import { Writer } from '../writer/interface';
|
|
39
|
-
import { applyEdits, lineBudgetOf, pointedSections, sentencesRemoved, shaOf, SkillFiles } from '../skills/skill-text';
|
|
40
|
-
import { actionJudgePrompt, actionPasses, ActionVerdict, caseSummary, EvalCase, judgeOf, judgeQuestions, judgeState, judgeVersion, nextCaseFile, readCases, REPLY_CRITERIA, verdictOf, writerPrompt, writerSystem } from '../evals/cases';
|
|
41
|
-
import { evalCasesFolder, runFolder } from '../paths';
|
|
42
|
-
import { retryAt, retryInput } from './retry';
|
|
43
|
-
export const WORKFLOW = 'Skill refinement';
|
|
44
|
-
/** What the workflow does, in a line: its definition's description and the dashboard's top level. */
|
|
45
|
-
export const WORKFLOW_DESCRIPTION = 'Turns each feedback item into a tested change to the synced skills.';
|
|
46
|
-
/** The skill that owns this process: Draft reads its rules for a good change on each run. */
|
|
47
|
-
export const OWNING_SKILL = 'autonomous-skill-refinement';
|
|
48
|
-
/** Replies written per case and version: one varies too much to decide on (`evals/README.md`). */
|
|
49
|
-
export const SAMPLES = 2;
|
|
50
|
-
/** Candidate edits drafted per item. */
|
|
51
|
-
export const CANDIDATES = 2;
|
|
52
|
-
/** Draft's tries per run: the first, and one more that reads why the first one's replies failed. */
|
|
53
|
-
export const DRAFT_ROUNDS = 2;
|
|
54
|
-
/** Writer calls at once. */
|
|
55
|
-
const WRITERS_AT_ONCE = 4;
|
|
56
|
-
/** Installs per run on the skill's latest text, when an edit lands while one is being written or replaces it afterwards. */
|
|
57
|
-
const INSTALL_TRIES = 3;
|
|
58
|
-
const Classified = Schema.Struct({ skill: Schema.String, section: Schema.String, sectionConfidence: Schema.Finite, kind: Schema.String, kindConfidence: Schema.Finite, jevModel: Schema.String });
|
|
59
|
-
const Drafted = Schema.Struct({
|
|
60
|
-
skill: Schema.String, draftedOn: Schema.String, file: Schema.String,
|
|
61
|
-
candidates: Schema.Array(Schema.Struct({ summary: Schema.String, applies: Schema.Boolean, why: Schema.String })),
|
|
62
|
-
costUsd: Schema.Finite,
|
|
63
|
-
});
|
|
64
|
-
const Chosen = Schema.Struct({ cases: Schema.Array(Schema.String), considered: Schema.Finite });
|
|
65
|
-
const Calibration = Schema.Struct({
|
|
66
|
-
fingerprint: Schema.String, agreed: Schema.Finite, graded: Schema.Finite, opusAgreed: Schema.Finite, opusGraded: Schema.Finite,
|
|
67
|
-
actionAgreed: Schema.Finite, actionGraded: Schema.Finite, actionBaselineAgreed: Schema.Finite, actionBaselineGraded: Schema.Finite,
|
|
68
|
-
usable: Schema.Boolean, file: Schema.String,
|
|
69
|
-
});
|
|
70
|
-
const Replies = Schema.Struct({ file: Schema.String, replies: Schema.Finite, costUsd: Schema.Finite });
|
|
71
|
-
const Ranked = Schema.Struct({
|
|
72
|
-
file: Schema.String,
|
|
73
|
-
versions: Schema.Array(Schema.Struct({ version: Schema.String, passes: Schema.Finite, of: Schema.Finite, score: Schema.Finite, losesTo: Schema.Array(Schema.String) })),
|
|
74
|
-
winner: Schema.NullOr(Schema.Finite),
|
|
75
|
-
why: Schema.String,
|
|
76
|
-
});
|
|
77
|
-
const InstalledStep = Schema.Struct({ skill: Schema.String, wordsBefore: Schema.Finite, wordsAfter: Schema.Finite, files: Schema.Array(Schema.String), rebased: Schema.Boolean, syncListLine: Schema.String });
|
|
78
|
-
export const Outcome = Schema.Struct({ item: Schema.NullOr(Schema.String), outcome: Schema.String, finished: Schema.Boolean });
|
|
79
|
-
/** What Draft writes for one item, as the model returns it. */
|
|
80
|
-
const DraftReply = Schema.Struct({
|
|
81
|
-
candidates: Schema.Array(Schema.Struct({ summary: Schema.String, edits: Schema.Array(Schema.Struct({ find: Schema.String, replace: Schema.String })) })),
|
|
82
|
-
history: Schema.String,
|
|
83
|
-
example: Schema.Struct({ name: Schema.String, text: Schema.String }),
|
|
84
|
-
eval_case: Schema.Struct({ his_question: Schema.String, original_reply: Schema.String, his_reaction: Schema.String, state_before: Schema.Array(Schema.String), wanted: Schema.String, went_wrong: Schema.String }),
|
|
85
|
-
});
|
|
86
|
-
/** What a later round of Draft writes: new candidates for the same case. */
|
|
87
|
-
const RevisedCandidates = Schema.Struct({ candidates: DraftReply.fields.candidates });
|
|
88
|
-
const jsonIn = (text) => text.slice(text.indexOf('{'), text.lastIndexOf('}') + 1);
|
|
89
|
-
const writeJson = (file, value) => Effect.sync(() => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, JSON.stringify(value, null, 1)); });
|
|
90
|
-
const readJson = (file) => Effect.sync(() => JSON.parse(fs.readFileSync(file, 'utf8')));
|
|
91
|
-
/** Puts one model call's spend on the step's record as it lands. */
|
|
92
|
-
const recordWriterSpend = (id, spend) => Effect.flatMap(AgentStep, (step) => Effect.sync(() => {
|
|
93
|
-
step.usage(id, spend.model, { input_tokens: spend.inputTokens, cache_read_input_tokens: spend.cacheReadTokens, cache_creation_input_tokens: spend.cacheWriteTokens, output_tokens: spend.outputTokens });
|
|
94
|
-
}));
|
|
95
|
-
const recordJevSpend = (id, model, inputTokens, outputTokens) => Effect.flatMap(AgentStep, (step) => Effect.sync(() => {
|
|
96
|
-
step.usage(id, model, { input_tokens: inputTokens, output_tokens: outputTokens });
|
|
97
|
-
}));
|
|
98
|
-
/** How likely Jev must find it that a removed sentence's requirement is still stated, for the change to keep it. */
|
|
99
|
-
const KEPT_REQUIREMENT_CUT = 0.5;
|
|
100
|
-
/**
|
|
101
|
-
* Whether a drafted candidate may be tested and installed: its edits apply to the skill's text, and every sentence it
|
|
102
|
-
* takes out that no synced skill holds after it is still required by the text that replaced it, as Jev judges. Deleting a
|
|
103
|
-
* rule he set needs his yes (autonomous-skill-refinement § 3 rule 4); on 2026-11-20 a candidate that moved his numbering
|
|
104
|
-
* rule out of senior-swe "with" an edit to agentic-collaboration installed the deletion alone, as a change to one skill does.
|
|
105
|
-
*/
|
|
106
|
-
const checkCandidate = (input) => Effect.gen(function* () {
|
|
107
|
-
const applied = applyEdits(input.current, input.edits);
|
|
108
|
-
if (Result.isFailure(applied))
|
|
109
|
-
return { applies: false, why: applied.failure };
|
|
110
|
-
const removed = sentencesRemoved({ before: input.current, after: applied.success, elsewhere: input.elsewhere });
|
|
111
|
-
if (removed.length === 0)
|
|
112
|
-
return { applies: true, why: 'applies' };
|
|
113
|
-
const jev = yield* Jev;
|
|
114
|
-
const asked = yield* jev.ask({
|
|
115
|
-
state: { removed_sentences: removed, text_that_replaced_them: input.edits.map((e) => e.replace).join('\n\n') },
|
|
116
|
-
questions: Object.fromEntries(removed.map((sentence, i) => [`kept_${i}`, { type: 'noul', instructions: { question: 'Does the text that replaced them still require what this removed sentence requires?', focus: sentence } }])),
|
|
117
|
-
});
|
|
118
|
-
yield* recordJevSpend('draft kept rules', asked.model, asked.inputTokens, asked.outputTokens);
|
|
119
|
-
const lost = removed.filter((_, i) => asked.answers[`kept_${i}`] < KEPT_REQUIREMENT_CUT);
|
|
120
|
-
return lost.length === 0
|
|
121
|
-
? { applies: true, why: 'applies' }
|
|
122
|
-
: { applies: false, why: `it deletes a rule no synced skill states after it, which needs his yes: "${lost[0]}"${lost.length > 1 ? ` and ${lost.length - 1} more` : ''}` };
|
|
123
|
-
});
|
|
124
|
-
/**
|
|
125
|
-
* The candidates A/B tests: those Draft found may be tested, as edits of the skill's text now, each under the number Draft
|
|
126
|
-
* gave it (`version`), which Judge ranks it by and Install takes the winner by, whichever others stop applying. Pure.
|
|
127
|
-
*/
|
|
128
|
-
const testedVersions = (input) => input.reply.candidates.flatMap((c, i) => {
|
|
129
|
-
if (input.drafted.candidates[i]?.applies !== true)
|
|
130
|
-
return [];
|
|
131
|
-
const applied = applyEdits(input.current, c.edits);
|
|
132
|
-
return Result.isSuccess(applied) ? [{ candidate: c, version: i + 1, text: applied.success }] : [];
|
|
133
|
-
});
|
|
134
|
-
/** Every section of the skills as Jev chooses among them: its label, and its opening as what it means. Pure. */
|
|
135
|
-
const sectionCriteria = (sections) => Object.fromEntries(sections.map((x) => [x.label, x.text.slice(0, 600)]));
|
|
136
|
-
/**
|
|
137
|
-
* WHERE AN ITEM GOES: the skill whose sections Jev gives the most probability in all, then that skill's likeliest section.
|
|
138
|
-
* Taking the single likeliest section put the convention-over-configuration item (how jive lays out packages) in effect's
|
|
139
|
-
* "Which conventions win" at 0.17, by the word the two share, while senior-swe's sections held 0.53 in all. Measured on 10
|
|
140
|
-
* items whose skill was known (2026-11-16): the same skill as the single likeliest section on the other 9; asking for the
|
|
141
|
-
* skill first, from each skill's description and opening, got 8 of 10. Pure.
|
|
142
|
-
*/
|
|
143
|
-
export const placeBySkill = (input) => {
|
|
144
|
-
const skillOf = new Map(input.sections.map((x) => [x.label, x.skill]));
|
|
145
|
-
const perSkill = new Map();
|
|
146
|
-
for (const [label, p] of Object.entries(input.probabilities)) {
|
|
147
|
-
const skill = skillOf.get(label);
|
|
148
|
-
if (skill !== undefined)
|
|
149
|
-
perSkill.set(skill, (perSkill.get(skill) ?? 0) + p);
|
|
150
|
-
}
|
|
151
|
-
const [skill, skillProbability] = [...perSkill].sort((a, b) => b[1] - a[1])[0];
|
|
152
|
-
const [section, sectionProbability] = Object.entries(input.probabilities).filter(([label]) => skillOf.get(label) === skill).sort((a, b) => b[1] - a[1])[0];
|
|
153
|
-
return { skill, skillProbability, section, sectionProbability };
|
|
154
|
-
};
|
|
155
|
-
/**
|
|
156
|
-
* The skill's line budget, for a skill that states a cap: told to Draft before it writes, since a candidate over the cap
|
|
157
|
-
* is refused after it (every jive item ended "no candidate applies: the skill would have 218 lines, over its own cap of
|
|
158
|
-
* 200", the skill already at 201). Pure.
|
|
159
|
-
*/
|
|
160
|
-
const lineBudgetLine = (text) => {
|
|
161
|
-
const budget = lineBudgetOf(text);
|
|
162
|
-
if (budget === null)
|
|
163
|
-
return '';
|
|
164
|
-
return `The skill has ${budget.lines} lines and states its own cap of ${budget.cap}${budget.lines >= budget.cap ? ', so it has no room for a line more' : ''}. A candidate that leaves it over ${budget.cap} lines is refused: one that would go over compacts the skill in the same edits, the way the skill's own rule for its length says.`;
|
|
165
|
-
};
|
|
166
|
-
/** Draft's system prompt: the rules for changing skills, from the skill that owns them. Pure. */
|
|
167
|
-
export const draftSystem = (howToChange) => `You maintain Alex's skills: the rules every agent working for him loads. You change one skill's text for one piece of his feedback. These are the rules for changing skills, from the skill that owns them (${OWNING_SKILL}):\n\n${howToChange}`;
|
|
168
|
-
const draftContext = (input) => [
|
|
169
|
-
`The feedback item, as the agent that received it handed it over:\n${input.itemText}`,
|
|
170
|
-
`Jev classified it as about "${input.section}" (confidence ${input.sectionConfidence.toFixed(2)}), kind: ${input.kind}.`,
|
|
171
|
-
input.notes.length > 0 ? `Earlier tries on this item, and why they ended:\n${input.notes}` : '',
|
|
172
|
-
`The skill's example files, by name: ${input.examples.join(', ')}`,
|
|
173
|
-
`The skill ${input.skill}'s whole text now:\n<<<SKILL\n${input.skillText}\nSKILL>>>`,
|
|
174
|
-
lineBudgetLine(input.skillText),
|
|
175
|
-
];
|
|
176
|
-
const CANDIDATE_JSON = '{"summary": "the rule change in one sentence", "edits": [{"find": "...", "replace": "..."}]}';
|
|
177
|
-
const EDITS_ASKED = 'each a list of exact replacements in that text: "find" must be copied exactly from the text and occur once.';
|
|
178
|
-
/** Draft's first prompt for an item: candidates, the history entry, the example and the item's A/B case. Pure. */
|
|
179
|
-
export const draftPrompt = (input) => [
|
|
180
|
-
...draftContext(input),
|
|
181
|
-
`Write ${CANDIDATES} different candidate changes, ${EDITS_ASKED}`
|
|
182
|
-
+ ' Also write the history entry for history.md (a "## " heading naming the rule, then his words, the date and what drew them), the example file (name and text: what happened, his words, why), and the A/B case for this instance as an action case.',
|
|
183
|
-
`Return only JSON: {"candidates": [${CANDIDATE_JSON}], "history": "...", "example": {"name": "topic-words.md", "text": "..."}, "eval_case": {"his_question": "his message that the agent acted on", "original_reply": "what the agent did and said", "his_reaction": "his exact words", "state_before": ["fact"], "wanted": "...", "went_wrong": "..."}}`,
|
|
184
|
-
].filter(Boolean).join('\n\n');
|
|
185
|
-
/**
|
|
186
|
-
* Draft's next prompt in a run whose candidates did not win: the same item and case, the candidates tried, and why each
|
|
187
|
-
* reply failed, for new candidates only; the case stays as the first round wrote it, so a candidate is never measured
|
|
188
|
-
* against an easier one. Pure.
|
|
189
|
-
*/
|
|
190
|
-
export const reviseDraftPrompt = (input) => [
|
|
191
|
-
...draftContext(input),
|
|
192
|
-
`The A/B case every candidate is tested on, which a winner must pass:\n${JSON.stringify(input.evalCase, null, 1)}`,
|
|
193
|
-
`The candidates already tried for this item:\n${input.tried.map((t, i) => `${i + 1}. ${t.summary} (${t.why})`).join('\n')}`,
|
|
194
|
-
`Why none of them won: ${input.whyNoneWon}`,
|
|
195
|
-
'Each failure above is a judge\'s reason for one reply written under one version of the skill: the current text or a candidate. A failure every version shares comes from text they all keep, which can be a rule other than the one the feedback names.'
|
|
196
|
-
+ ` Find the rules in the skill text that produce the failures, and write ${CANDIDATES} new candidate changes that remove them, ${EDITS_ASKED}`
|
|
197
|
-
+ ' A candidate never drops or weakens a requirement he stated (the rules for changing skills); where passing the case would need that, its summary names the rule and his words that stand in the way.',
|
|
198
|
-
`Return only JSON: {"candidates": [${CANDIDATE_JSON}]}`,
|
|
199
|
-
].filter(Boolean).join('\n\n');
|
|
200
|
-
/**
|
|
201
|
-
* Draft's version: its prompts on fixed inputs, and the counts a run tests with. With the judge's version and the skills'
|
|
202
|
-
* text it decides when an item that ended without an install runs again (`configFingerprint`). Pure.
|
|
203
|
-
*/
|
|
204
|
-
export const draftVersion = () => {
|
|
205
|
-
const fixed = { itemText: '', section: '', sectionConfidence: 0, kind: '', notes: '', examples: [], skill: '', skillText: '' };
|
|
206
|
-
return JSON.stringify({
|
|
207
|
-
system: draftSystem(''), first: draftPrompt(fixed),
|
|
208
|
-
revise: reviseDraftPrompt({ ...fixed, evalCase: { his_question: '', original_reply: '', his_reaction: '', state_before: [], wanted: '', went_wrong: '' }, tried: [], whyNoneWon: '' }),
|
|
209
|
-
CANDIDATES, SAMPLES, DRAFT_ROUNDS,
|
|
210
|
-
});
|
|
211
|
-
};
|
|
212
|
-
/** A round's file in the run folder: the first round's keeps its plain name. Pure. */
|
|
213
|
-
const roundFile = (name, round) => (round === 1 ? `${name}.json` : `${name}-${round}.json`);
|
|
214
|
-
/** What calibration measured, in a line: each judge against its baseline. Pure. */
|
|
215
|
-
const calibrationLine = (c) => `Jev agrees with Alex on ${c.agreed} of ${c.graded} original replies (Opus judge: ${c.opusAgreed} of ${c.opusGraded}); the action judge fails ${c.actionAgreed} of ${c.actionGraded} action originals he objected to (original prompt: ${c.actionBaselineAgreed} of ${c.actionBaselineGraded})`;
|
|
216
|
-
export const defineSkillRefinement = (deps) => {
|
|
217
|
-
const { defineStep, defineWorkflow, queueNow } = deps.platform.steps;
|
|
218
|
-
const at = import.meta.path;
|
|
219
|
-
const PICK = defineStep({ name: 'Pick', runs: 'deterministic', at, description: 'Takes the head of the intake queue.', success: Schema.Option(Schema.String), subject: (o) => Option.getOrNull(o), picked: Option.isSome });
|
|
220
|
-
const CLASSIFY = defineStep({ name: 'Classify', runs: 'agentic', model: deps.models.jev, at, description: 'Jev names the skill section the item is about, and whether it is new, a repeat or a correction.', success: Classified, reason: (c) => `${c.section} (${c.kind})` });
|
|
221
|
-
const DRAFT = defineStep({ name: 'Draft', runs: 'agentic', at, description: 'The generative model drafts candidate edits from the target skill, its example names and the item.', success: Drafted, reason: (d) => `${d.candidates.filter((c) => c.applies).length} of ${d.candidates.length} candidates apply` });
|
|
222
|
-
const CHOOSE = defineStep({ name: 'Choose examples', runs: 'agentic', model: deps.models.jev, at, description: 'Jev names the section that owns each A/B case; the cases the changed section owns are tested.', success: Chosen, reason: (c) => `${c.cases.length} of ${c.considered} cases` });
|
|
223
|
-
const CALIBRATE = defineStep({ name: 'Calibrate judge', runs: 'agentic', shared: true, at, description: 'Once per judge version: Jev grades the original reply cases and the Opus judge the action originals; a judge below its baseline is not used.', success: Calibration, reason: (c) => calibrationLine(c) });
|
|
224
|
-
const AB = defineStep({ name: 'A/B', runs: 'agentic', at, description: 'The generative model writes each chosen case under the current text and each candidate.', success: Replies, reason: (r) => `${r.replies} replies, $${r.costUsd.toFixed(2)}` });
|
|
225
|
-
const JUDGE = defineStep({ name: 'Judge', runs: 'agentic', at, description: 'Each reply is judged by its case kind\'s judge (an action case by the Opus judge, a reply case by Jev), and the candidates are ranked.', success: Ranked, reason: (r) => r.why });
|
|
226
|
-
const INSTALL = defineStep({ name: 'Install', runs: 'agentic', model: deps.models.jev, at, description: 'Writes the winner to the synced skill, rebased on any edit made since Draft (Jev re-checks a rebased change for deleted rules), and checks that the synced list shows it.', success: InstalledStep, writes: [], reason: (i) => i.syncListLine });
|
|
227
|
-
/** Pick: the queue's head, the platform's order. */
|
|
228
|
-
const pick = (run) => Effect.map(queueNow({ workflow: WORKFLOW, run }), (queue) => Option.fromNullishOr(queue[0]?.ref.item));
|
|
229
|
-
const classify = (item) => Effect.gen(function* () {
|
|
230
|
-
const jev = yield* Jev;
|
|
231
|
-
const text = yield* deps.intake.text(item);
|
|
232
|
-
const sections = yield* (yield* SkillFiles).sections();
|
|
233
|
-
const where = yield* jev.ask({
|
|
234
|
-
state: { feedback_item: text },
|
|
235
|
-
questions: { section: { type: 'choice', instructions: 'Which section of the skills is the rule this feedback is about, the one a change for it would edit?', criteria: sectionCriteria(sections) } },
|
|
236
|
-
});
|
|
237
|
-
yield* recordJevSpend('classify section', where.model, where.inputTokens, where.outputTokens);
|
|
238
|
-
const placed = placeBySkill({ probabilities: where.answers.section.probabilities, sections });
|
|
239
|
-
const placedSection = sections.find((s) => s.label === placed.section);
|
|
240
|
-
// A section that points to another skill's section for a rule does not hold it: Jev chooses again among it and those.
|
|
241
|
-
const pointed = pointedSections(placedSection, sections);
|
|
242
|
-
const holder = pointed.length === 0 ? null : yield* jev.ask({
|
|
243
|
-
state: { feedback_item: text },
|
|
244
|
-
questions: { section: { type: 'choice', instructions: 'Which of these sections holds the rule this feedback is about, the one a change for it would edit? A section that only points to another skill\'s section for the rule does not hold it.', criteria: sectionCriteria([placedSection, ...pointed]) } },
|
|
245
|
-
});
|
|
246
|
-
if (holder !== null)
|
|
247
|
-
yield* recordJevSpend('classify pointed section', holder.model, holder.inputTokens, holder.outputTokens);
|
|
248
|
-
const section = holder === null ? placedSection : sections.find((s) => s.label === holder.answers.section.choice);
|
|
249
|
-
const sectionConfidence = holder === null ? placed.sectionProbability : holder.answers.section.confidence;
|
|
250
|
-
const kind = yield* jev.ask({
|
|
251
|
-
state: { feedback_item: text, the_section_now: section.text },
|
|
252
|
-
questions: { kind: { type: 'choice', instructions: 'What is this feedback, against the section as it reads now?', criteria: {
|
|
253
|
-
new: 'The section has no rule covering what he objects to.',
|
|
254
|
-
repeat: 'The section already has a rule covering it, and the agent broke it again.',
|
|
255
|
-
correction: 'It corrects an earlier feedback item, or a rule saved for one (a wording, a mechanism added to his words).',
|
|
256
|
-
} } },
|
|
257
|
-
});
|
|
258
|
-
yield* recordJevSpend('classify kind', kind.model, kind.inputTokens, kind.outputTokens);
|
|
259
|
-
return { skill: section.skill, section: section.label, sectionConfidence, kind: kind.answers.kind.choice, kindConfidence: kind.answers.kind.confidence, jevModel: kind.model };
|
|
260
|
-
});
|
|
261
|
-
/**
|
|
262
|
-
* Draft for one round. The first writes the candidates, history entry, example and case; a later one, given the round
|
|
263
|
-
* before it, writes new candidates only, keeping that round's history, example and case.
|
|
264
|
-
*/
|
|
265
|
-
const draft = (input) => Effect.gen(function* () {
|
|
266
|
-
const files = yield* SkillFiles;
|
|
267
|
-
const writer = yield* Writer;
|
|
268
|
-
const target = yield* files.skillText(input.classified.skill);
|
|
269
|
-
const howToChange = (yield* files.skillText(OWNING_SKILL)).text;
|
|
270
|
-
const context = {
|
|
271
|
-
itemText: yield* deps.intake.text(input.item),
|
|
272
|
-
section: input.classified.section, sectionConfidence: input.classified.sectionConfidence, kind: input.classified.kind,
|
|
273
|
-
notes: yield* Effect.orElseSucceed(deps.platform.tasks.notesForAgent({ workflow: WORKFLOW, item: input.item }), () => ''),
|
|
274
|
-
examples: yield* files.exampleNames(input.classified.skill),
|
|
275
|
-
skill: input.classified.skill, skillText: target.text,
|
|
276
|
-
};
|
|
277
|
-
const earlier = input.previous === null ? null : { ...input.previous, reply: yield* readJson(input.previous.drafted.file) };
|
|
278
|
-
const written = yield* writer.write({
|
|
279
|
-
system: draftSystem(howToChange),
|
|
280
|
-
prompt: earlier === null
|
|
281
|
-
? draftPrompt(context)
|
|
282
|
-
: reviseDraftPrompt({ ...context, evalCase: earlier.reply.eval_case, tried: earlier.drafted.candidates, whyNoneWon: earlier.whyNoneWon }),
|
|
283
|
-
});
|
|
284
|
-
yield* recordWriterSpend(input.round === 1 ? 'draft' : `draft ${input.round}`, written.spend);
|
|
285
|
-
const reply = earlier === null
|
|
286
|
-
? yield* Effect.mapError(Schema.decodeUnknownEffect(Schema.fromJsonString(DraftReply))(jsonIn(written.text)), (e) => `the draft was not the JSON asked for: ${e.message}`)
|
|
287
|
-
: { ...earlier.reply, candidates: (yield* Effect.mapError(Schema.decodeUnknownEffect(Schema.fromJsonString(RevisedCandidates))(jsonIn(written.text)), (e) => `round ${input.round}'s draft was not the JSON asked for: ${e.message}`)).candidates };
|
|
288
|
-
const elsewhere = (yield* files.sections()).filter((x) => x.skill !== input.classified.skill).map((x) => x.text).join('\n');
|
|
289
|
-
const file = path.join(runFolder(deps.folders, input.run), roundFile('draft', input.round));
|
|
290
|
-
yield* writeJson(file, { ...reply, skill: input.classified.skill, draftedOn: target.sha, raw: written.text });
|
|
291
|
-
return {
|
|
292
|
-
skill: input.classified.skill, draftedOn: target.sha, file, costUsd: written.spend.costUsd,
|
|
293
|
-
candidates: yield* Effect.forEach(reply.candidates, (c) => Effect.map(checkCandidate({ current: target.text, elsewhere, edits: c.edits }), (checked) => ({ summary: c.summary, ...checked }))),
|
|
294
|
-
};
|
|
295
|
-
});
|
|
296
|
-
/** The item's own case, as Draft wrote it, under the id it gets when installed. */
|
|
297
|
-
const itemCase = (reply, id) => ({
|
|
298
|
-
id, kind: 'action', his_question: reply.eval_case.his_question, original_reply: reply.eval_case.original_reply, his_reaction: reply.eval_case.his_reaction,
|
|
299
|
-
action: { state_before: reply.eval_case.state_before, wanted: reply.eval_case.wanted, went_wrong: reply.eval_case.went_wrong },
|
|
300
|
-
});
|
|
301
|
-
/**
|
|
302
|
-
* THE EXAMPLES OF THE CHANGED RULE: the cases whose failure Jev places in the section the item changes, asked as Classify
|
|
303
|
-
* asks (one section of all the skills' sections owns each failure), plus the item's own. Asked as "does this rule
|
|
304
|
-
* govern it?", Jev said yes to 51 of 102 cases for one rule (2026-11-16), which would have cost about $21 of A/B for
|
|
305
|
-
* one item. Each case's section is kept per case text and skills version, so it is asked once.
|
|
306
|
-
*/
|
|
307
|
-
const choose = (classified) => Effect.gen(function* () {
|
|
308
|
-
const jev = yield* Jev;
|
|
309
|
-
const sections = yield* (yield* SkillFiles).sections();
|
|
310
|
-
const criteria = sectionCriteria(sections);
|
|
311
|
-
const version = shaOf(JSON.stringify(criteria));
|
|
312
|
-
const cases = yield* readCases(evalCasesFolder(deps.folders));
|
|
313
|
-
const owners = yield* Effect.forEach(cases, (c) => Effect.gen(function* () {
|
|
314
|
-
const file = path.join(deps.folders.local, 'case-sections', `${shaOf(JSON.stringify(c))}-${version}.json`);
|
|
315
|
-
if (fs.existsSync(file))
|
|
316
|
-
return { id: c.id, section: (yield* readJson(file)).section };
|
|
317
|
-
const asked = yield* jev.ask({ state: { example: caseSummary(c) }, questions: { section: { type: 'choice', instructions: 'Which section of the skills holds the rule whose breaking this example shows?', criteria } } });
|
|
318
|
-
yield* recordJevSpend(`choose ${c.id}`, asked.model, asked.inputTokens, asked.outputTokens);
|
|
319
|
-
yield* writeJson(file, { case: c.id, section: asked.answers.section.choice, confidence: asked.answers.section.confidence });
|
|
320
|
-
return { id: c.id, section: asked.answers.section.choice };
|
|
321
|
-
}), { concurrency: 8 });
|
|
322
|
-
return { cases: owners.filter((o) => o.section === classified.section).map((o) => o.id), considered: cases.length };
|
|
323
|
-
});
|
|
324
|
-
/** The judge's version: its questions, the rule that scores them, and the cases it is calibrated on. */
|
|
325
|
-
const judgeFingerprint = (cases, model) => shaOf(JSON.stringify({ judge: judgeVersion(), model, cases: cases.map((c) => [c.id, c.original_reply]) }));
|
|
326
|
-
/** The Opus judge's grade of one plan and reply for an action case, its spend on the step's record. */
|
|
327
|
-
const opusGrade = (input) => Effect.gen(function* () {
|
|
328
|
-
const graded = yield* (yield* Writer).write({ system: 'You grade one agent\'s plan and reply. Return only the JSON asked for.', prompt: actionJudgePrompt(input.c, input.reply) });
|
|
329
|
-
yield* recordWriterSpend(input.id, graded.spend);
|
|
330
|
-
return yield* Effect.mapError(Schema.decodeUnknownEffect(Schema.fromJsonString(ActionVerdict))(jsonIn(graded.text)), (e) => `the Opus judge's verdict on ${input.c.id} was not the JSON asked for: ${e.message}`);
|
|
331
|
-
});
|
|
332
|
-
/**
|
|
333
|
-
* BOTH JUDGES ARE HELD TO A BAR BEFORE ANY ITEM'S MODEL CALL, once per judge version. Jev, the judge of reply cases, must
|
|
334
|
-
* agree with Alex on the original replies at least as often as the Opus judge did; the Opus judge of action cases must
|
|
335
|
-
* fail at least as many of the action originals he objected to (all of them) as the evals' original action-judge prompt
|
|
336
|
-
* did, so a change to its prompt never makes it more lenient on what he rejected.
|
|
337
|
-
*/
|
|
338
|
-
const calibrate = () => Effect.gen(function* () {
|
|
339
|
-
const jev = yield* Jev;
|
|
340
|
-
const all = yield* readCases(evalCasesFolder(deps.folders));
|
|
341
|
-
const replyCases = new Set(all.filter((c) => judgeOf(c) === 'jev').map((c) => c.id));
|
|
342
|
-
const opus = deps.opusJudgeGrades.filter((o) => replyCases.has(o.case));
|
|
343
|
-
const actionBaseline = deps.opusActionJudgeGrades;
|
|
344
|
-
// With no grade to compare against, the bar would be zero and any judge would pass it.
|
|
345
|
-
if (opus.length === 0 || actionBaseline.length === 0)
|
|
346
|
-
return yield* Effect.fail('the Opus judge has no baseline grades, so no judge can be held to them');
|
|
347
|
-
const graded = new Set(opus.map((o) => o.case));
|
|
348
|
-
const cases = all.filter((c) => graded.has(c.id));
|
|
349
|
-
const actionIds = new Set(actionBaseline.map((o) => o.case));
|
|
350
|
-
const actionCases = all.filter((c) => actionIds.has(c.id));
|
|
351
|
-
const fingerprint = judgeFingerprint([...cases, ...actionCases], 'jev-latest');
|
|
352
|
-
const file = path.join(deps.folders.local, 'judge-calibration', `${fingerprint}.json`);
|
|
353
|
-
if (fs.existsSync(file))
|
|
354
|
-
return yield* readJson(file);
|
|
355
|
-
// He objected to every original reply but the ones he labelled good: a judge agrees with him when it fails those and passes these.
|
|
356
|
-
const wanted = (id) => id.startsWith('good-');
|
|
357
|
-
const verdicts = yield* Effect.forEach(cases, (c) => Effect.gen(function* () { const a = yield* jev.ask({ state: judgeState(c, c.original_reply), questions: judgeQuestions() }); return { id: c.id, a, verdict: verdictOf(a.answers) }; }), { concurrency: 8 });
|
|
358
|
-
for (const v of verdicts)
|
|
359
|
-
yield* recordJevSpend(`calibrate ${v.id}`, v.a.model, v.a.inputTokens, v.a.outputTokens);
|
|
360
|
-
const agreed = verdicts.filter((v) => v.verdict.pass === wanted(v.id)).length;
|
|
361
|
-
const opusAgreed = opus.filter((o) => o.pass === wanted(o.case)).length;
|
|
362
|
-
const actions = yield* Effect.forEach(actionCases, (c) => Effect.map(opusGrade({ id: `calibrate ${c.id}`, c, reply: c.original_reply }), (v) => ({ id: c.id, pass: actionPasses(v), why: v.why })), { concurrency: WRITERS_AT_ONCE });
|
|
363
|
-
const actionAgreed = actions.filter((a) => !a.pass).length;
|
|
364
|
-
const actionBaselineAgreed = actionBaseline.filter((o) => !o.pass).length;
|
|
365
|
-
const result = {
|
|
366
|
-
fingerprint, agreed, graded: verdicts.length, opusAgreed, opusGraded: opus.length,
|
|
367
|
-
actionAgreed, actionGraded: actions.length, actionBaselineAgreed, actionBaselineGraded: actionBaseline.length,
|
|
368
|
-
usable: verdicts.length > 0 && agreed / verdicts.length >= opusAgreed / opus.length && actionAgreed / actions.length >= actionBaselineAgreed / actionBaseline.length,
|
|
369
|
-
file,
|
|
370
|
-
};
|
|
371
|
-
yield* writeJson(file, { ...result, verdicts: verdicts.map((v) => ({ id: v.id, ...v.a.answers, ...v.verdict })), actions });
|
|
372
|
-
return result;
|
|
373
|
-
});
|
|
374
|
-
/** Every (case, version, sample) to write: version 0 is the current text, then each candidate that applies under its number from Draft. */
|
|
375
|
-
const abJobs = (input) => {
|
|
376
|
-
const versions = [{ version: 0, text: input.current }, ...testedVersions(input)];
|
|
377
|
-
return input.cases.flatMap((c) => versions.flatMap(({ version, text }) => Array.from({ length: SAMPLES }, (_, sample) => ({ c, version, text, sample }))));
|
|
378
|
-
};
|
|
379
|
-
/** A/B for one round; given an earlier round's replies, the current text's are taken from them, not written again. */
|
|
380
|
-
const ab = (input) => Effect.gen(function* () {
|
|
381
|
-
const writer = yield* Writer;
|
|
382
|
-
const reply = yield* readJson(input.drafted.file);
|
|
383
|
-
const current = (yield* (yield* SkillFiles).skillText(input.drafted.skill)).text;
|
|
384
|
-
const all = yield* readCases(evalCasesFolder(deps.folders));
|
|
385
|
-
const cases = [...all.filter((c) => input.chosen.cases.includes(c.id)), itemCase(reply, `this item (${input.item})`)];
|
|
386
|
-
const reused = input.currentTextReplies === null ? [] : (yield* readJson(input.currentTextReplies)).filter((w) => w.version === 0);
|
|
387
|
-
const jobs = abJobs({ reply, drafted: input.drafted, current, cases }).filter((j) => reused.length === 0 || j.version !== 0);
|
|
388
|
-
const written = yield* Effect.forEach(jobs, (j) => Effect.gen(function* () {
|
|
389
|
-
const w = yield* writer.write({ system: writerSystem(input.drafted.skill, j.text), prompt: writerPrompt(j.c) });
|
|
390
|
-
yield* recordWriterSpend(`${input.round === 1 ? '' : `round ${input.round} `}${j.c.id} v${j.version} s${j.sample}`, w.spend);
|
|
391
|
-
return { case: j.c, version: j.version, sample: j.sample, reply: w.text, costUsd: w.spend.costUsd };
|
|
392
|
-
}), { concurrency: WRITERS_AT_ONCE });
|
|
393
|
-
const file = path.join(runFolder(deps.folders, input.run), roundFile('replies', input.round));
|
|
394
|
-
yield* writeJson(file, [...reused, ...written]);
|
|
395
|
-
return { file, replies: written.length, costUsd: written.reduce((s, w) => s + w.costUsd, 0) };
|
|
396
|
-
});
|
|
397
|
-
/**
|
|
398
|
-
* Ranks the versions by passes. A candidate wins only if it fails no case the current text passes (a case passes for a
|
|
399
|
-
* version when most of its samples pass) and passes the item's own case; of those, the most passes wins.
|
|
400
|
-
*/
|
|
401
|
-
const judge = (input) => Effect.gen(function* () {
|
|
402
|
-
const jev = yield* Jev;
|
|
403
|
-
const written = yield* readJson(input.replies.file);
|
|
404
|
-
// An earlier round's verdicts on the current text's replies stand: those replies are the same ones.
|
|
405
|
-
const reused = input.currentTextVerdicts === null ? [] : (yield* readJson(input.currentTextVerdicts)).judged.filter((j) => j.version === 0);
|
|
406
|
-
const fresh = yield* Effect.forEach(written.filter((w) => reused.length === 0 || w.version !== 0), (w) => Effect.gen(function* () {
|
|
407
|
-
const id = `judge ${input.round === 1 ? '' : `round ${input.round} `}${w.case.id} v${w.version} s${w.sample}`;
|
|
408
|
-
// Each case goes to its kind's judge (`judgeOf`): an action case to the Opus judge, a reply case to Jev.
|
|
409
|
-
if (judgeOf(w.case) === 'opus') {
|
|
410
|
-
const verdict = yield* opusGrade({ id, c: w.case, reply: w.reply });
|
|
411
|
-
return { caseId: w.case.id, version: w.version, sample: w.sample, judge: 'opus', verdict, pass: actionPasses(verdict), score: actionPasses(verdict) ? 1 : 0 };
|
|
412
|
-
}
|
|
413
|
-
const a = yield* jev.ask({ state: judgeState(w.case, w.reply), questions: judgeQuestions() });
|
|
414
|
-
yield* recordJevSpend(id, a.model, a.inputTokens, a.outputTokens);
|
|
415
|
-
return { caseId: w.case.id, version: w.version, sample: w.sample, judge: 'jev', answers: a.answers, ...verdictOf(a.answers) };
|
|
416
|
-
}), { concurrency: WRITERS_AT_ONCE });
|
|
417
|
-
const judged = [...reused, ...fresh];
|
|
418
|
-
const ranked = rank(judged);
|
|
419
|
-
const file = path.join(runFolder(deps.folders, input.run), roundFile('judged', input.round));
|
|
420
|
-
yield* writeJson(file, { judged, ranked });
|
|
421
|
-
// The outcome note the next Draft of this item reads: with no winner, why each failing reply failed, so it can aim there.
|
|
422
|
-
const failures = failureReasons(judged);
|
|
423
|
-
const why = ranked.winner === null && failures.length > 0 ? `${ranked.why}. Why replies failed: ${failures.join(' | ')}` : ranked.why;
|
|
424
|
-
return { file, ...ranked, why };
|
|
425
|
-
});
|
|
426
|
-
/**
|
|
427
|
-
* Installs the winner on the skill's text as it is now, which may hold edits made since Draft read it (here, or pulled
|
|
428
|
-
* from another machine): the winner's exact replacements are applied to that text, so every edit outside the lines it
|
|
429
|
-
* replaces is kept. On rebased text the rule-deletion check runs again. An edit landing during the install, or one from
|
|
430
|
-
* another machine replacing it afterwards, rebases it again, up to `INSTALL_TRIES` times. A winner whose lines another
|
|
431
|
-
* edit has changed no longer applies, and the item's next run drafts on the new text.
|
|
432
|
-
*/
|
|
433
|
-
const install = (input) => Effect.gen(function* () {
|
|
434
|
-
const files = yield* SkillFiles;
|
|
435
|
-
const reply = yield* readJson(input.drafted.file);
|
|
436
|
-
const winner = reply.candidates[input.winner - 1];
|
|
437
|
-
if (winner === undefined)
|
|
438
|
-
return yield* Effect.fail(`Draft wrote no candidate ${input.winner}`);
|
|
439
|
-
// Chosen once, so a retry rewrites this case instead of taking the next free number.
|
|
440
|
-
const caseFile = nextCaseFile(fs.readdirSync(evalCasesFolder(deps.folders)));
|
|
441
|
-
const evalCase = itemCase(reply, caseFile.replace(/\.json$/, ''));
|
|
442
|
-
for (let tries = 1;; tries++) {
|
|
443
|
-
const current = yield* files.skillText(input.drafted.skill);
|
|
444
|
-
const rebased = current.sha !== input.drafted.draftedOn;
|
|
445
|
-
const applied = rebased
|
|
446
|
-
? yield* checkCandidate({ current: current.text, elsewhere: (yield* files.sections()).filter((x) => x.skill !== input.drafted.skill).map((x) => x.text).join('\n'), edits: winner.edits })
|
|
447
|
-
: { applies: true, why: 'applies' };
|
|
448
|
-
if (!applied.applies)
|
|
449
|
-
return yield* Effect.fail(`skill ${input.drafted.skill} changed since Draft, and the winner, candidate ${input.winner}, no longer applies to it: ${applied.why}`);
|
|
450
|
-
const newText = Result.getOrThrow(applyEdits(current.text, winner.edits));
|
|
451
|
-
const before = Date.now();
|
|
452
|
-
const attempt = yield* Effect.result(Effect.gen(function* () {
|
|
453
|
-
const installed = yield* files.install({ skill: input.drafted.skill, onSha: current.sha, newText, history: reply.history, example: reply.example, evalCase: { file: caseFile, json: JSON.stringify(evalCase, null, 1) } });
|
|
454
|
-
const syncListLine = yield* files.confirmSynced({ skill: input.drafted.skill, since: before, afterSha: installed.afterSha });
|
|
455
|
-
return { installed, syncListLine };
|
|
456
|
-
}));
|
|
457
|
-
if (Result.isFailure(attempt)) {
|
|
458
|
-
if (attempt.failure._tag === 'SkillChangedError' && tries < INSTALL_TRIES)
|
|
459
|
-
continue;
|
|
460
|
-
return yield* Effect.fail(attempt.failure.reason);
|
|
461
|
-
}
|
|
462
|
-
const { installed, syncListLine } = attempt.success;
|
|
463
|
-
yield* deps.intake.recordInstalled(input.item, { item: input.item, run: input.run, rule: winner.summary, draftedOn: input.drafted.draftedOn, rebased, ...installed, syncListLine, at: new Date().toISOString() });
|
|
464
|
-
return { skill: installed.skill, wordsBefore: installed.wordsBefore, wordsAfter: installed.wordsAfter, files: installed.files, rebased, syncListLine };
|
|
465
|
-
}
|
|
466
|
-
});
|
|
467
|
-
/** An item's outcomes, oldest first, as the platform noted them. */
|
|
468
|
-
const outcomesOf = (item) => Effect.map(Effect.orElseSucceed(deps.platform.tasks.taskNotes({ workflow: WORKFLOW, item }), () => []), (notes) => notes.filter((n) => n.kind === 'outcome').map((n) => ({ why: n.why, at: n.at })));
|
|
469
|
-
/** Whether an open item's retry time falls after `from` and by `to` (unix seconds): its waiting Pick is woken then, as nothing else wakes it. */
|
|
470
|
-
const retryFallsDue = (input) => Effect.orElseSucceed(Effect.gen(function* () {
|
|
471
|
-
for (const id of yield* deps.intake.open()) {
|
|
472
|
-
const newest = (yield* outcomesOf(id)).at(-1);
|
|
473
|
-
const at = newest === undefined ? null : retryAt(newest);
|
|
474
|
-
if (at !== null && at > input.from && at <= input.to)
|
|
475
|
-
return true;
|
|
476
|
-
}
|
|
477
|
-
return false;
|
|
478
|
-
}), () => false);
|
|
479
|
-
const workflow = defineWorkflow({
|
|
480
|
-
at, item: 'feedback item', finishedWord: 'installed',
|
|
481
|
-
// Draft, A/B and the Opus judge in Calibrate and Judge run on the writer; Classify, Choose examples and Install set Jev.
|
|
482
|
-
model: deps.models.writer,
|
|
483
|
-
description: WORKFLOW_DESCRIPTION,
|
|
484
|
-
steps: () => [PICK, CALIBRATE, CLASSIFY, DRAFT, CHOOSE, AB, JUDGE, INSTALL],
|
|
485
|
-
picksQueueHead: true,
|
|
486
|
-
pending: () => Effect.orElseSucceed(Effect.map(deps.intake.open(), (open) => ({ lines: [{ label: 'feedback items to install', items: open }], waits: [] })), () => ({ lines: [], waits: [] })),
|
|
487
|
-
// An item that failed on Jev or the generative model also runs on the retry time that failure gives it (`retry.ts`).
|
|
488
|
-
taskInputs: () => Effect.orElseSucceed(Effect.gen(function* () {
|
|
489
|
-
const open = yield* deps.intake.open();
|
|
490
|
-
const now = deps.now();
|
|
491
|
-
const out = new Map();
|
|
492
|
-
for (const id of open) {
|
|
493
|
-
const retry = retryInput({ outcomes: yield* outcomesOf(id), now });
|
|
494
|
-
out.set(id, { fingerprint: shaOf(`${yield* deps.intake.text(id)}\n${deps.configFingerprint()}${retry === null ? '' : `\nretry at ${retry}`}`) });
|
|
495
|
-
}
|
|
496
|
-
return out;
|
|
497
|
-
}), () => new Map()),
|
|
498
|
-
outcomes: (o) => (o.item === null ? [] : [{ subject: o.item, outcome: o.outcome, finished: o.finished }]),
|
|
499
|
-
graph: () => [
|
|
500
|
-
{ from: RUN_STARTS, to: PICK.name, when: 'each run' },
|
|
501
|
-
{ from: PICK.name, to: CALIBRATE.name, when: 'an item picked' },
|
|
502
|
-
{ from: CALIBRATE.name, to: CLASSIFY.name, when: 'the judge agrees with him at least as often as the Opus judge did' },
|
|
503
|
-
{ from: CALIBRATE.name, to: RUN_ENDS, when: 'the judge agrees with him less often: no model call is spent on the item' },
|
|
504
|
-
{ from: PICK.name, to: RUN_ENDS, when: 'the run is past its run limit' },
|
|
505
|
-
{ from: CLASSIFY.name, to: DRAFT.name, when: 'always' },
|
|
506
|
-
{ from: DRAFT.name, to: CHOOSE.name, when: 'a candidate applies, in the first round that has one' },
|
|
507
|
-
{ from: DRAFT.name, to: AB.name, when: 'a candidate applies, once the examples are chosen' },
|
|
508
|
-
{ from: DRAFT.name, to: DRAFT.name, when: 'no candidate applies, and a round is left: the next reads why' },
|
|
509
|
-
{ from: DRAFT.name, to: RUN_ENDS, when: 'no candidate applies in the last round' },
|
|
510
|
-
{ from: CHOOSE.name, to: AB.name, when: 'always' },
|
|
511
|
-
{ from: AB.name, to: JUDGE.name, when: 'always' },
|
|
512
|
-
{ from: JUDGE.name, to: INSTALL.name, when: 'a candidate wins' },
|
|
513
|
-
{ from: JUDGE.name, to: DRAFT.name, when: 'no candidate wins, and a round is left: the next reads why each reply failed' },
|
|
514
|
-
{ from: JUDGE.name, to: RUN_ENDS, when: 'no candidate wins in the last round' },
|
|
515
|
-
{ from: INSTALL.name, to: RUN_ENDS, when: 'always' },
|
|
516
|
-
{ from: A_STEP_FAILS, to: RUN_ENDS, when: 'a step fails' },
|
|
517
|
-
],
|
|
518
|
-
})(WORKFLOW, {
|
|
519
|
-
payload: { run: Schema.String },
|
|
520
|
-
idempotencyKey: ({ run }) => run,
|
|
521
|
-
success: Outcome,
|
|
522
|
-
error: Schema.String,
|
|
523
|
-
});
|
|
524
|
-
const layer = workflow.toLayer((payload) => Effect.gen(function* () {
|
|
525
|
-
const { run } = payload;
|
|
526
|
-
const picked = yield* PICK.run(pick(run));
|
|
527
|
-
if (Option.isNone(picked))
|
|
528
|
-
return { item: null, outcome: 'nothing picked', finished: false };
|
|
529
|
-
const item = picked.value;
|
|
530
|
-
// The judge is checked first: a judge that cannot be trusted would waste every model call after it.
|
|
531
|
-
const calibration = yield* CALIBRATE.run(calibrate());
|
|
532
|
-
if (!calibration.usable)
|
|
533
|
-
return { item, outcome: `the judge is not calibrated: ${calibrationLine(calibration)}`, finished: false };
|
|
534
|
-
const classified = yield* CLASSIFY.run(classify(item));
|
|
535
|
-
let chosen = null;
|
|
536
|
-
let previous = null;
|
|
537
|
-
// The current text's replies and verdicts, once a round has written them: a later round drafted on the same text compares against these.
|
|
538
|
-
let currentText = null;
|
|
539
|
-
let outcome = '';
|
|
540
|
-
for (let round = 1; round <= DRAFT_ROUNDS; round++) {
|
|
541
|
-
// The first round's steps keep their plain names, so a run started before rounds existed resumes from its records.
|
|
542
|
-
const call = round === 1 ? undefined : { call: round };
|
|
543
|
-
const drafted = yield* DRAFT.run(draft({ item, run, classified, round, previous }), call);
|
|
544
|
-
if (!drafted.candidates.some((c) => c.applies)) {
|
|
545
|
-
const why = drafted.candidates.map((c) => c.why).join('; ');
|
|
546
|
-
outcome = `no candidate applies: ${why}`;
|
|
547
|
-
previous = { drafted, whyNoneWon: `none applied: ${why}` };
|
|
548
|
-
continue;
|
|
549
|
-
}
|
|
550
|
-
chosen = chosen ?? (yield* CHOOSE.run(choose(classified)));
|
|
551
|
-
// Replies to a text that has changed since (another machine's edit pulled between rounds) are not this text's: written again.
|
|
552
|
-
const reuse = currentText !== null && currentText.sha === drafted.draftedOn ? currentText : null;
|
|
553
|
-
const replies = yield* AB.run(ab({ run, drafted, chosen, item, round, currentTextReplies: reuse === null ? null : reuse.replies }), call);
|
|
554
|
-
const ranked = yield* JUDGE.run(judge({ run, replies, round, currentTextVerdicts: reuse === null ? null : reuse.verdicts }), call);
|
|
555
|
-
if (ranked.winner !== null) {
|
|
556
|
-
const installed = yield* INSTALL.run(install({ item, run, drafted, winner: ranked.winner }));
|
|
557
|
-
return { item, outcome: `installed in ${installed.skill} (${installed.wordsBefore} to ${installed.wordsAfter} words)${round === 1 ? '' : `, from draft ${round}`}`, finished: true };
|
|
558
|
-
}
|
|
559
|
-
outcome = `no candidate wins: ${ranked.why}`;
|
|
560
|
-
previous = { drafted, whyNoneWon: ranked.why };
|
|
561
|
-
currentText = { sha: drafted.draftedOn, replies: replies.file, verdicts: ranked.file };
|
|
562
|
-
}
|
|
563
|
-
return { item, outcome: `after ${DRAFT_ROUNDS} drafts, ${outcome}`, finished: false };
|
|
564
|
-
}));
|
|
565
|
-
return { workflow, layer, retryFallsDue, steps: { PICK, CLASSIFY, DRAFT, CHOOSE, CALIBRATE, AB, JUDGE, INSTALL } };
|
|
566
|
-
};
|
|
567
|
-
/** Why each failing reply failed: the Opus judge's sentence, or the reply criteria Jev failed it on. Pure. */
|
|
568
|
-
export const failureReasons = (judged) => judged.filter((j) => !j.pass).map((j) => {
|
|
569
|
-
const failed = j.judge === 'opus'
|
|
570
|
-
? j.verdict.why
|
|
571
|
-
: `failed ${Object.entries(REPLY_CRITERIA).filter(([k, c]) => { const yes = j.answers[k] ?? 0; return (c.passesWhen === 'yes' ? yes : 1 - yes) < 0.5; }).map(([k]) => k).join(', ')}`;
|
|
572
|
-
return `${j.caseId} ${j.version === 0 ? 'current' : `candidate ${j.version}`} sample ${j.sample}: ${failed}`;
|
|
573
|
-
});
|
|
574
|
-
/** Each version's passes, the cases it loses that the current text wins, and the winner. Pure. */
|
|
575
|
-
export const rank = (judged) => {
|
|
576
|
-
const versions = [...new Set(judged.map((j) => j.version))].sort((a, b) => a - b);
|
|
577
|
-
const caseIds = [...new Set(judged.map((j) => j.caseId))];
|
|
578
|
-
const passesCase = (version, caseId) => {
|
|
579
|
-
const samples = judged.filter((j) => j.version === version && j.caseId === caseId);
|
|
580
|
-
return samples.filter((s) => s.pass).length * 2 > samples.length;
|
|
581
|
-
};
|
|
582
|
-
const rows = versions.map((v) => ({
|
|
583
|
-
version: v === 0 ? 'current' : `candidate ${v}`,
|
|
584
|
-
passes: caseIds.filter((c) => passesCase(v, c)).length,
|
|
585
|
-
of: caseIds.length,
|
|
586
|
-
score: judged.filter((j) => j.version === v).reduce((s, j) => s + j.score, 0),
|
|
587
|
-
losesTo: v === 0 ? [] : caseIds.filter((c) => passesCase(0, c) && !passesCase(v, c)),
|
|
588
|
-
}));
|
|
589
|
-
const ownCase = caseIds.find((c) => c.startsWith('this item'));
|
|
590
|
-
const eligible = versions.filter((v) => v !== 0 && rows[versions.indexOf(v)].losesTo.length === 0 && (ownCase === undefined || passesCase(v, ownCase)));
|
|
591
|
-
const best = eligible.sort((a, b) => rows[versions.indexOf(b)].passes - rows[versions.indexOf(a)].passes || rows[versions.indexOf(b)].score - rows[versions.indexOf(a)].score)[0];
|
|
592
|
-
const why = best === undefined
|
|
593
|
-
? `no candidate passes the item's own case without failing a case the current text passes (${rows.map((r) => `${r.version}: ${r.passes}/${r.of}`).join(', ')})`
|
|
594
|
-
: `candidate ${best} passes ${rows[versions.indexOf(best)].passes} of ${caseIds.length} cases, the current text ${rows[0].passes}`;
|
|
595
|
-
return { versions: rows, winner: best ?? null, why };
|
|
596
|
-
};
|
|
597
|
-
//# sourceMappingURL=skill-refinement.js.map
|