@zenera/meta 0.0.0-stage → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +51 -2
  3. package/dist/command.d.ts +5 -0
  4. package/dist/command.js +1086 -0
  5. package/dist/dataset/anchors.d.ts +41 -0
  6. package/dist/dataset/anchors.js +239 -0
  7. package/dist/dataset/changes.d.ts +36 -0
  8. package/dist/dataset/changes.js +319 -0
  9. package/dist/dataset/command.d.ts +17 -0
  10. package/dist/dataset/command.js +1011 -0
  11. package/dist/dataset/drift.d.ts +33 -0
  12. package/dist/dataset/drift.js +71 -0
  13. package/dist/dataset/export.d.ts +26 -0
  14. package/dist/dataset/export.js +51 -0
  15. package/dist/dataset/index.d.ts +2 -0
  16. package/dist/dataset/index.js +2 -0
  17. package/dist/dataset/sample.d.ts +50 -0
  18. package/dist/dataset/sample.js +197 -0
  19. package/dist/dataset/schema.d.ts +33 -0
  20. package/dist/dataset/schema.js +216 -0
  21. package/dist/dataset/store.d.ts +35 -0
  22. package/dist/dataset/store.js +177 -0
  23. package/dist/dataset/types.d.ts +127 -0
  24. package/dist/dataset/types.js +39 -0
  25. package/dist/finetune/analyze-brief.d.ts +26 -0
  26. package/dist/finetune/analyze-brief.js +131 -0
  27. package/dist/finetune/apply-brief.d.ts +23 -0
  28. package/dist/finetune/apply-brief.js +247 -0
  29. package/dist/finetune/brief.d.ts +45 -0
  30. package/dist/finetune/brief.js +158 -0
  31. package/dist/finetune/children.d.ts +8 -0
  32. package/dist/finetune/children.js +76 -0
  33. package/dist/finetune/command.d.ts +30 -0
  34. package/dist/finetune/command.js +520 -0
  35. package/dist/finetune/cost.d.ts +46 -0
  36. package/dist/finetune/cost.js +105 -0
  37. package/dist/finetune/failure.d.ts +15 -0
  38. package/dist/finetune/failure.js +26 -0
  39. package/dist/finetune/feedback.d.ts +57 -0
  40. package/dist/finetune/feedback.js +104 -0
  41. package/dist/finetune/improvements.d.ts +98 -0
  42. package/dist/finetune/improvements.js +501 -0
  43. package/dist/finetune/index.d.ts +2 -0
  44. package/dist/finetune/index.js +2 -0
  45. package/dist/finetune/journal.d.ts +35 -0
  46. package/dist/finetune/journal.js +135 -0
  47. package/dist/finetune/live.d.ts +84 -0
  48. package/dist/finetune/live.js +272 -0
  49. package/dist/finetune/loop.d.ts +3 -0
  50. package/dist/finetune/loop.js +163 -0
  51. package/dist/finetune/memories.d.ts +47 -0
  52. package/dist/finetune/memories.js +171 -0
  53. package/dist/finetune/next-case.d.ts +18 -0
  54. package/dist/finetune/next-case.js +47 -0
  55. package/dist/finetune/recheck.d.ts +58 -0
  56. package/dist/finetune/recheck.js +311 -0
  57. package/dist/finetune/report.d.ts +40 -0
  58. package/dist/finetune/report.js +1397 -0
  59. package/dist/finetune/retry.d.ts +24 -0
  60. package/dist/finetune/retry.js +96 -0
  61. package/dist/finetune/system.d.ts +27 -0
  62. package/dist/finetune/system.js +132 -0
  63. package/dist/finetune/train.d.ts +43 -0
  64. package/dist/finetune/train.js +425 -0
  65. package/dist/finetune/tui.d.ts +16 -0
  66. package/dist/finetune/tui.js +316 -0
  67. package/dist/finetune/tuning.d.ts +193 -0
  68. package/dist/finetune/tuning.js +308 -0
  69. package/dist/finetune/usage.d.ts +66 -0
  70. package/dist/finetune/usage.js +260 -0
  71. package/dist/finetune/view.d.ts +9 -0
  72. package/dist/finetune/view.js +27 -0
  73. package/dist/host.d.ts +2 -0
  74. package/dist/host.js +14 -0
  75. package/dist/index.d.ts +10 -0
  76. package/dist/index.js +15 -0
  77. package/dist/launch.d.ts +90 -0
  78. package/dist/launch.js +310 -0
  79. package/dist/model.d.ts +61 -0
  80. package/dist/model.js +146 -0
  81. package/dist/prompts.d.ts +23 -0
  82. package/dist/prompts.js +84 -0
  83. package/dist/relay.d.ts +20 -0
  84. package/dist/relay.js +123 -0
  85. package/dist/sink.d.ts +61 -0
  86. package/dist/sink.js +198 -0
  87. package/dist/tokens.d.ts +52 -0
  88. package/dist/tokens.js +226 -0
  89. package/dist/wire.d.ts +51 -0
  90. package/dist/wire.js +159 -0
  91. package/package.json +54 -5
@@ -0,0 +1,41 @@
1
+ import { type Anchor } from './types.ts';
2
+ export type Found = {
3
+ found: true;
4
+ text: string;
5
+ sha256: string;
6
+ } | {
7
+ found: false;
8
+ why: 'no file' | 'not found' | 'ambiguous' | 'unreadable';
9
+ };
10
+ interface Heading {
11
+ level: number;
12
+ title: string;
13
+ path: string[];
14
+ /** first line, and one past the last line of its section */
15
+ start: number;
16
+ end: number;
17
+ }
18
+ export declare const sha256: (text: string) => string;
19
+ /** Every ATX heading outside a code fence, with the extent of its section. */
20
+ export declare function headingsOf(text: string): Heading[];
21
+ /** Sorted keys, so reformatting or reordering a document is not a change. */
22
+ export declare function canonical(value: unknown): string;
23
+ /** One read of each file per instance: a drift check asks about many cases per file. */
24
+ export declare class Sources {
25
+ #private;
26
+ readonly root: string;
27
+ constructor(root: string);
28
+ text(file: string): string | undefined;
29
+ /** Of the bytes as they are: any edit at all, even one no case sees. */
30
+ hash(file: string): string | undefined;
31
+ section(file: string, anchor: Anchor | undefined): Found;
32
+ /**
33
+ * Sections of a file no case and no ignore accounts for: headings at the
34
+ * levels the covered ones sit at, or the siblings of covered pointers. A
35
+ * section inside a covered one is part of that case, and a section around one
36
+ * is its context, so neither is reported.
37
+ */
38
+ uncovered(file: string, covered: readonly (Anchor | undefined)[]): Anchor[];
39
+ }
40
+ export {};
41
+ //# sourceMappingURL=anchors.d.ts.map
@@ -0,0 +1,239 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { readFileSync } from 'node:fs';
3
+ import { extname, resolve } from 'node:path';
4
+ import { parse as parseYaml } from 'yaml';
5
+ import {} from "./types.js";
6
+ export const sha256 = (text) => createHash('sha256').update(text).digest('hex');
7
+ const normal = (text) => text
8
+ .replace(/\r\n?/g, '\n')
9
+ .split('\n')
10
+ .map((line) => line.trimEnd())
11
+ .join('\n')
12
+ .replace(/\n+$/, '');
13
+ const title = (text) => text.replace(/\s+/g, ' ').trim();
14
+ /** Every ATX heading outside a code fence, with the extent of its section. */
15
+ export function headingsOf(text) {
16
+ const lines = text.replace(/\r\n?/g, '\n').split('\n');
17
+ const found = [];
18
+ const stack = [];
19
+ let fence;
20
+ lines.forEach((line, i) => {
21
+ const marker = /^ {0,3}(`{3,}|~{3,})/.exec(line);
22
+ if (marker) {
23
+ const char = marker[1][0];
24
+ if (!fence) {
25
+ fence = { char, size: marker[1].length };
26
+ }
27
+ else if (fence.char === char && marker[1].length >= fence.size) {
28
+ fence = undefined;
29
+ }
30
+ return;
31
+ }
32
+ if (fence) {
33
+ return;
34
+ }
35
+ const m = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/.exec(line);
36
+ if (!m || !m[2]?.trim()) {
37
+ return;
38
+ }
39
+ const level = m[1].length;
40
+ while (stack.length > 0 && stack[stack.length - 1].level >= level) {
41
+ stack.pop();
42
+ }
43
+ const heading = {
44
+ level,
45
+ title: title(m[2]),
46
+ path: [...stack.map((h) => h.title), title(m[2])],
47
+ start: i,
48
+ end: lines.length,
49
+ };
50
+ stack.push(heading);
51
+ found.push(heading);
52
+ });
53
+ for (let i = 0; i < found.length; i++) {
54
+ const next = found.slice(i + 1).find((h) => h.level <= found[i].level);
55
+ found[i].end = next ? next.start : lines.length;
56
+ }
57
+ return found;
58
+ }
59
+ /**
60
+ * The last name must be the heading's own; the names before it must appear, in
61
+ * order, among its ancestors. So `["Query: organize my day"]` is enough while it
62
+ * is unique, and an author adds a parent only to tell two apart.
63
+ */
64
+ function matches(path, heading) {
65
+ const want = path.map(title);
66
+ if (want[want.length - 1] !== heading.title) {
67
+ return false;
68
+ }
69
+ let at = 0;
70
+ for (const name of heading.path.slice(0, -1)) {
71
+ if (at < want.length - 1 && name === want[at]) {
72
+ at++;
73
+ }
74
+ }
75
+ return at === want.length - 1;
76
+ }
77
+ /** RFC 6901, over a parsed JSON or YAML document. */
78
+ function follow(doc, pointer) {
79
+ if (pointer === '') {
80
+ return { ok: true, value: doc };
81
+ }
82
+ let at = doc;
83
+ for (const raw of pointer.slice(1).split('/')) {
84
+ const key = raw.replace(/~1/g, '/').replace(/~0/g, '~');
85
+ if (Array.isArray(at) && /^\d+$/.test(key) && Number(key) < at.length) {
86
+ at = at[Number(key)];
87
+ }
88
+ else if (at !== null && typeof at === 'object' && !Array.isArray(at) && key in at) {
89
+ at = at[key];
90
+ }
91
+ else {
92
+ return { ok: false };
93
+ }
94
+ }
95
+ return { ok: true, value: at };
96
+ }
97
+ const escapeKey = (key) => key.replace(/~/g, '~0').replace(/\//g, '~1');
98
+ /** Sorted keys, so reformatting or reordering a document is not a change. */
99
+ export function canonical(value) {
100
+ if (Array.isArray(value)) {
101
+ return `[${value.map(canonical).join(',')}]`;
102
+ }
103
+ if (value !== null && typeof value === 'object') {
104
+ const keys = Object.keys(value).sort();
105
+ return `{${keys
106
+ .map((k) => `${JSON.stringify(k)}:${canonical(value[k])}`)
107
+ .join(',')}}`;
108
+ }
109
+ return JSON.stringify(value) ?? 'null';
110
+ }
111
+ /** One read of each file per instance: a drift check asks about many cases per file. */
112
+ export class Sources {
113
+ root;
114
+ #text = new Map();
115
+ #headings = new Map();
116
+ #docs = new Map();
117
+ constructor(root) {
118
+ this.root = root;
119
+ }
120
+ text(file) {
121
+ if (!this.#text.has(file)) {
122
+ let text;
123
+ try {
124
+ text = readFileSync(resolve(this.root, file), 'utf8');
125
+ }
126
+ catch {
127
+ text = undefined;
128
+ }
129
+ this.#text.set(file, text);
130
+ }
131
+ return this.#text.get(file);
132
+ }
133
+ /** Of the bytes as they are: any edit at all, even one no case sees. */
134
+ hash(file) {
135
+ const text = this.text(file);
136
+ return text === undefined ? undefined : sha256(text);
137
+ }
138
+ #headingsOf(file, text) {
139
+ let found = this.#headings.get(file);
140
+ if (!found) {
141
+ found = headingsOf(text);
142
+ this.#headings.set(file, found);
143
+ }
144
+ return found;
145
+ }
146
+ #doc(file, text) {
147
+ let parsed = this.#docs.get(file);
148
+ if (!parsed) {
149
+ try {
150
+ const ext = extname(file).toLowerCase();
151
+ parsed = { ok: true, doc: ext === '.json' ? JSON.parse(text) : parseYaml(text) };
152
+ }
153
+ catch {
154
+ parsed = { ok: false };
155
+ }
156
+ this.#docs.set(file, parsed);
157
+ }
158
+ return parsed;
159
+ }
160
+ section(file, anchor) {
161
+ const text = this.text(file);
162
+ if (text === undefined) {
163
+ return { found: false, why: 'no file' };
164
+ }
165
+ if (anchor?.heading) {
166
+ const hits = this.#headingsOf(file, text).filter((h) => matches(anchor.heading, h));
167
+ if (hits.length !== 1) {
168
+ return { found: false, why: hits.length === 0 ? 'not found' : 'ambiguous' };
169
+ }
170
+ const lines = text.replace(/\r\n?/g, '\n').split('\n');
171
+ const body = normal(lines.slice(hits[0].start, hits[0].end).join('\n'));
172
+ return { found: true, text: body, sha256: sha256(body) };
173
+ }
174
+ if (anchor?.pointer !== undefined) {
175
+ const parsed = this.#doc(file, text);
176
+ if (!parsed.ok) {
177
+ return { found: false, why: 'unreadable' };
178
+ }
179
+ const hit = follow(parsed.doc, anchor.pointer);
180
+ if (!hit.ok) {
181
+ return { found: false, why: 'not found' };
182
+ }
183
+ const body = canonical(hit.value);
184
+ return { found: true, text: body, sha256: sha256(body) };
185
+ }
186
+ const body = normal(text);
187
+ return { found: true, text: body, sha256: sha256(body) };
188
+ }
189
+ /**
190
+ * Sections of a file no case and no ignore accounts for: headings at the
191
+ * levels the covered ones sit at, or the siblings of covered pointers. A
192
+ * section inside a covered one is part of that case, and a section around one
193
+ * is its context, so neither is reported.
194
+ */
195
+ uncovered(file, covered) {
196
+ const text = this.text(file);
197
+ if (text === undefined || covered.some((a) => !a?.heading && a?.pointer === undefined)) {
198
+ return [];
199
+ }
200
+ const out = [];
201
+ const paths = covered.flatMap((a) => (a?.heading ? [a.heading] : []));
202
+ if (paths.length > 0) {
203
+ const all = this.#headingsOf(file, text);
204
+ const hit = all.filter((h) => paths.some((p) => matches(p, h)));
205
+ const levels = new Set(hit.map((h) => h.level));
206
+ const near = (h) => hit.some((c) => c === h ||
207
+ (c.start <= h.start && h.start < c.end) ||
208
+ (h.start <= c.start && c.start < h.end));
209
+ for (const h of all) {
210
+ if (levels.has(h.level) && !near(h)) {
211
+ out.push({ heading: h.path });
212
+ }
213
+ }
214
+ }
215
+ const pointers = covered.flatMap((a) => (a?.pointer ? [a.pointer] : []));
216
+ if (pointers.length > 0) {
217
+ const parsed = this.#doc(file, text);
218
+ const taken = new Set(pointers);
219
+ const parents = new Set(pointers.map((p) => p.slice(0, p.lastIndexOf('/'))));
220
+ for (const parent of parents) {
221
+ const hit = parsed.ok ? follow(parsed.doc, parent) : { ok: false };
222
+ if (!hit.ok || hit.value === null || typeof hit.value !== 'object') {
223
+ continue;
224
+ }
225
+ const keys = Array.isArray(hit.value)
226
+ ? hit.value.map((_, i) => String(i))
227
+ : Object.keys(hit.value);
228
+ for (const key of keys) {
229
+ const pointer = `${parent}/${escapeKey(key)}`;
230
+ if (!taken.has(pointer)) {
231
+ out.push({ pointer });
232
+ }
233
+ }
234
+ }
235
+ }
236
+ return out;
237
+ }
238
+ }
239
+ //# sourceMappingURL=anchors.js.map
@@ -0,0 +1,36 @@
1
+ import type { Proposed, ProposedRubric } from './schema.ts';
2
+ import type { Commit, DatasetStore } from './store.ts';
3
+ import { type By, type Case, type Manifest, type RubricItem } from './types.ts';
4
+ export interface Plan extends Commit {
5
+ /** cases that were looked at and found the same */
6
+ unchanged: string[];
7
+ }
8
+ /**
9
+ * Rubric items keep their ids across edits, so a note saying `r3 failed` still
10
+ * means the same line. An explicit id wins; otherwise the same words are the
11
+ * same item; anything else is new.
12
+ */
13
+ export declare function assignRubric(before: readonly RubricItem[], proposed: readonly ProposedRubric[]): RubricItem[];
14
+ /**
15
+ * A proposal, applied. Whole by default: an active case the proposal does not
16
+ * name is retired. `partial` touches only the cases it names — what a refresh
17
+ * of a few drifted sections writes.
18
+ */
19
+ export declare function planApply(store: DatasetStore, proposed: readonly Proposed[], options: {
20
+ partial: boolean;
21
+ why: string;
22
+ by: By;
23
+ }): Plan;
24
+ /** One case, changed by hand. `override` keeps the change through later re-extractions. */
25
+ export declare function planEdit(store: DatasetStore, id: string, edit: (c: Case) => Case, options: {
26
+ why: string;
27
+ by: By;
28
+ override?: readonly string[];
29
+ }): Plan;
30
+ export declare function planStatus(store: DatasetStore, ids: readonly string[], status: Case['status'], options: {
31
+ why: string;
32
+ by: By;
33
+ }): Plan;
34
+ /** The ignore list changed: no case moves, so no revision; the manifest is rewritten. */
35
+ export declare function planIgnored(store: DatasetStore, ignored: Manifest['ignored']): Plan;
36
+ //# sourceMappingURL=changes.d.ts.map
@@ -0,0 +1,319 @@
1
+ import { invalidError } from "../host.js";
2
+ import { canonical, Sources } from "./anchors.js";
3
+ import { anchorText, CONTENT_FIELDS, RESTART_FIELDS, sameAnchor, } from "./types.js";
4
+ const same = (a, b) => canonical(a ?? null) === canonical(b ?? null);
5
+ /**
6
+ * Rubric items keep their ids across edits, so a note saying `r3 failed` still
7
+ * means the same line. An explicit id wins; otherwise the same words are the
8
+ * same item; anything else is new.
9
+ */
10
+ export function assignRubric(before, proposed) {
11
+ const known = new Map(before.map((item) => [item.id, item]));
12
+ const taken = new Set();
13
+ let next = before.reduce((max, item) => Math.max(max, Number(/^r(\d+)$/.exec(item.id)?.[1] ?? 0)), 0);
14
+ const pending = [];
15
+ const out = proposed.map((item, index) => {
16
+ if (item.id && known.has(item.id) && !taken.has(item.id)) {
17
+ taken.add(item.id);
18
+ return { id: item.id, text: item.text };
19
+ }
20
+ pending.push({ index, text: item.text });
21
+ return undefined;
22
+ });
23
+ for (const { index, text } of pending) {
24
+ const match = before.find((item) => !taken.has(item.id) && item.text === text);
25
+ if (match) {
26
+ taken.add(match.id);
27
+ out[index] = { id: match.id, text };
28
+ }
29
+ }
30
+ return out.map((item, index) => {
31
+ if (item) {
32
+ return item;
33
+ }
34
+ let id;
35
+ do {
36
+ id = `r${++next}`;
37
+ } while (taken.has(id));
38
+ taken.add(id);
39
+ return { id, text: proposed[index].text };
40
+ });
41
+ }
42
+ function rubricChanges(before, after) {
43
+ const was = new Map(before.map((i) => [i.id, i.text]));
44
+ const now = new Map(after.map((i) => [i.id, i.text]));
45
+ const out = [];
46
+ for (const [id, text] of now) {
47
+ if (!was.has(id)) {
48
+ out.push({ path: `rubric/${id}`, to: text });
49
+ }
50
+ else if (was.get(id) !== text) {
51
+ out.push({ path: `rubric/${id}`, from: was.get(id), to: text });
52
+ }
53
+ }
54
+ for (const [id, text] of was) {
55
+ if (!now.has(id)) {
56
+ out.push({ path: `rubric/${id}`, from: text });
57
+ }
58
+ }
59
+ if (out.length === 0 &&
60
+ !same(before.map((i) => i.id), after.map((i) => i.id))) {
61
+ out.push({ path: 'rubric', from: before.map((i) => i.id), to: after.map((i) => i.id) });
62
+ }
63
+ return out;
64
+ }
65
+ const withoutHash = (c) => c.source
66
+ ? { file: c.source.file, ...(c.source.anchor ? { anchor: c.source.anchor } : {}) }
67
+ : undefined;
68
+ function fieldChanges(before, after) {
69
+ const out = [];
70
+ for (const field of CONTENT_FIELDS) {
71
+ if (field === 'rubric') {
72
+ out.push(...rubricChanges(before.rubric, after.rubric));
73
+ continue;
74
+ }
75
+ const from = field === 'source' ? withoutHash(before) : before[field];
76
+ const to = field === 'source' ? withoutHash(after) : after[field];
77
+ if (!same(from, to)) {
78
+ out.push({
79
+ path: field,
80
+ ...(from !== undefined ? { from } : {}),
81
+ ...(to !== undefined ? { to } : {}),
82
+ });
83
+ }
84
+ }
85
+ return out;
86
+ }
87
+ const restarts = (changes) => changes.some((c) => RESTART_FIELDS.some((f) => c.path === f || c.path.startsWith(`${f}/`)));
88
+ /** Fills in each case's source hash; a source that cannot be found is refused. */
89
+ function hashSources(sources, nexts) {
90
+ const issues = [];
91
+ for (const { after } of nexts) {
92
+ if (!after.source || after.status !== 'active') {
93
+ continue;
94
+ }
95
+ const found = sources.section(after.source.file, after.source.anchor);
96
+ if (!found.found) {
97
+ issues.push(`${after.id}: ${after.source.file} ${anchorText(after.source.anchor)} — ${found.why}`);
98
+ continue;
99
+ }
100
+ after.source = { ...after.source, sha256: found.sha256 };
101
+ }
102
+ if (issues.length > 0) {
103
+ throw invalidError(`${issues.length} source${issues.length === 1 ? '' : 's'} could not be found\n ${issues.join('\n ')}`, 'name a heading path or pointer that exists exactly once; nothing was written');
104
+ }
105
+ }
106
+ /**
107
+ * A file's hash is what lets a drift check skip it, so it only moves forward
108
+ * once the file is settled: every case read from it matches its section, and
109
+ * every section is a case or ignored. A partial apply that left one drifted case
110
+ * behind must leave the file looking moved, or that case is never looked at again.
111
+ */
112
+ function manifestFor(store, sources, finals, ignored = store.manifest.ignored) {
113
+ const byFile = new Map();
114
+ for (const c of finals) {
115
+ if (c.status === 'active' && c.source) {
116
+ byFile.set(c.source.file, [...(byFile.get(c.source.file) ?? []), c]);
117
+ }
118
+ }
119
+ for (const i of ignored) {
120
+ byFile.set(i.file, byFile.get(i.file) ?? []);
121
+ }
122
+ const hashed = {};
123
+ for (const [file, cases] of [...byFile].sort(([a], [b]) => a.localeCompare(b))) {
124
+ const now = sources.hash(file);
125
+ const was = store.manifest.sources[file]?.sha256;
126
+ if (!now) {
127
+ continue;
128
+ }
129
+ const settled = now === was ||
130
+ (cases.every((c) => {
131
+ const found = sources.section(file, c.source.anchor);
132
+ return found.found && found.sha256 === c.source.sha256;
133
+ }) &&
134
+ sources
135
+ .uncovered(file, [
136
+ ...cases.map((c) => c.source.anchor),
137
+ ...ignored.filter((i) => i.file === file).map((i) => i.anchor),
138
+ ])
139
+ .every((a) => ignored.some((i) => i.file === file && sameAnchor(i.anchor, a))));
140
+ if (settled) {
141
+ hashed[file] = { sha256: now };
142
+ }
143
+ else if (was) {
144
+ hashed[file] = { sha256: was };
145
+ }
146
+ }
147
+ return { ...store.manifest, sources: hashed, ignored };
148
+ }
149
+ function finish(store, sources, nexts, why, by) {
150
+ hashSources(sources, nexts);
151
+ const rev = store.manifest.revision + 1;
152
+ const at = new Date().toISOString();
153
+ const rows = [];
154
+ const refreshed = [];
155
+ const unchanged = [];
156
+ const revision = {
157
+ rev,
158
+ at,
159
+ why,
160
+ by,
161
+ added: [],
162
+ updated: [],
163
+ retired: [],
164
+ restored: [],
165
+ restarted: [],
166
+ };
167
+ for (const { before, after } of nexts) {
168
+ if (!before) {
169
+ const added = { ...after, rev };
170
+ rows.push({ type: 'change', rev, at, op: 'add', changes: [], why, by, case: added });
171
+ revision.added.push(after.id);
172
+ continue;
173
+ }
174
+ const changes = fieldChanges(before, after);
175
+ let op;
176
+ if (before.status !== after.status) {
177
+ op = after.status === 'retired' ? 'retire' : 'restore';
178
+ changes.unshift({ path: 'status', from: before.status, to: after.status });
179
+ }
180
+ else if (changes.length > 0) {
181
+ op = 'update';
182
+ }
183
+ if (!op) {
184
+ if (before.source?.sha256 !== after.source?.sha256) {
185
+ refreshed.push({ ...after, rev: before.rev });
186
+ }
187
+ else {
188
+ unchanged.push(after.id);
189
+ }
190
+ continue;
191
+ }
192
+ const written = { ...after, rev };
193
+ rows.push({ type: 'change', rev, at, op, changes, why, by, case: written });
194
+ if (op === 'retire') {
195
+ revision.retired.push(after.id);
196
+ }
197
+ else if (op === 'restore') {
198
+ revision.restored.push(after.id);
199
+ }
200
+ else {
201
+ revision.updated.push(after.id);
202
+ }
203
+ if (op !== 'retire' && restarts(changes)) {
204
+ revision.restarted.push(after.id);
205
+ }
206
+ }
207
+ const touched = new Map([...rows.map((r) => r.case), ...refreshed].map((c) => [c.id, c]));
208
+ const finals = store.all().map((c) => touched.get(c.id) ?? c);
209
+ for (const c of touched.values()) {
210
+ if (!finals.some((f) => f.id === c.id)) {
211
+ finals.push(c);
212
+ }
213
+ }
214
+ return {
215
+ rev,
216
+ rows,
217
+ refreshed,
218
+ unchanged,
219
+ revision,
220
+ manifest: manifestFor(store, sources, finals),
221
+ };
222
+ }
223
+ // ---------------------------------------------------------------------------
224
+ // The writes
225
+ // ---------------------------------------------------------------------------
226
+ function fromProposal(p, before) {
227
+ const after = {
228
+ id: p.id,
229
+ rev: before?.rev ?? 0,
230
+ status: 'active',
231
+ ...(p.class !== undefined ? { class: p.class } : {}),
232
+ ...(p.complexity !== undefined ? { complexity: p.complexity } : {}),
233
+ ...(p.tags !== undefined ? { tags: p.tags } : {}),
234
+ input: p.input,
235
+ rubric: assignRubric(before?.rubric ?? [], p.rubric),
236
+ ...(p.expected !== undefined ? { expected: p.expected } : {}),
237
+ ...(p.notes !== undefined ? { notes: p.notes } : {}),
238
+ ...(p.source !== undefined ? { source: { ...p.source } } : {}),
239
+ };
240
+ for (const field of before?.overrides ?? []) {
241
+ const kept = before[field];
242
+ if (kept === undefined) {
243
+ delete after[field];
244
+ }
245
+ else {
246
+ after[field] = kept;
247
+ }
248
+ }
249
+ if (before?.overrides?.length) {
250
+ after.overrides = before.overrides;
251
+ }
252
+ return after;
253
+ }
254
+ /**
255
+ * A proposal, applied. Whole by default: an active case the proposal does not
256
+ * name is retired. `partial` touches only the cases it names — what a refresh
257
+ * of a few drifted sections writes.
258
+ */
259
+ export function planApply(store, proposed, options) {
260
+ const sources = new Sources(store.root);
261
+ const named = new Set(proposed.map((p) => p.id));
262
+ const nexts = proposed.map((p) => {
263
+ const before = store.get(p.id);
264
+ return { before, after: fromProposal(p, before) };
265
+ });
266
+ if (!options.partial) {
267
+ for (const c of store.all()) {
268
+ if (c.status === 'active' && !named.has(c.id)) {
269
+ nexts.push({ before: c, after: { ...c, status: 'retired' } });
270
+ }
271
+ }
272
+ }
273
+ return finish(store, sources, nexts, options.why, options.by);
274
+ }
275
+ /** One case, changed by hand. `override` keeps the change through later re-extractions. */
276
+ export function planEdit(store, id, edit, options) {
277
+ const before = store.get(id);
278
+ if (!before) {
279
+ throw invalidError(`no case ${id}`, 'see: zen meta dataset ls');
280
+ }
281
+ const after = edit(structuredClone(before));
282
+ if (options.override?.length) {
283
+ after.overrides = [...new Set([...(before.overrides ?? []), ...options.override])].sort();
284
+ }
285
+ return finish(store, new Sources(store.root), [{ before, after }], options.why, options.by);
286
+ }
287
+ export function planStatus(store, ids, status, options) {
288
+ const nexts = ids.map((id) => {
289
+ const before = store.get(id);
290
+ if (!before) {
291
+ throw invalidError(`no case ${id}`, 'see: zen meta dataset ls --status all');
292
+ }
293
+ return { before, after: { ...before, status } };
294
+ });
295
+ return finish(store, new Sources(store.root), nexts, options.why, options.by);
296
+ }
297
+ /** The ignore list changed: no case moves, so no revision; the manifest is rewritten. */
298
+ export function planIgnored(store, ignored) {
299
+ const sources = new Sources(store.root);
300
+ return {
301
+ rev: store.manifest.revision,
302
+ rows: [],
303
+ refreshed: [],
304
+ unchanged: [],
305
+ revision: {
306
+ rev: store.manifest.revision,
307
+ at: new Date().toISOString(),
308
+ why: '',
309
+ by: { host: '' },
310
+ added: [],
311
+ updated: [],
312
+ retired: [],
313
+ restored: [],
314
+ restarted: [],
315
+ },
316
+ manifest: manifestFor(store, sources, store.all(), ignored),
317
+ };
318
+ }
319
+ //# sourceMappingURL=changes.js.map
@@ -0,0 +1,17 @@
1
+ export declare const DATASET_USAGE = "zen meta dataset <verb> [args] [options]";
2
+ export declare const DATASET_HELP: string[];
3
+ export declare const DATASET_SUMMARY = "The cases the project is evaluated on: read from sources, revised, sampled.";
4
+ /** The page `zen meta dataset --help` prints. */
5
+ export declare const DATASET_DETAILS: string[];
6
+ export interface DatasetContext {
7
+ readonly args: readonly string[];
8
+ readonly json: boolean;
9
+ readonly cwd: string;
10
+ }
11
+ /** Only the project flag, read ahead of resolving the project the rest is about. */
12
+ export declare function datasetProjectFlag(args: readonly string[]): string | undefined;
13
+ export declare function runDataset(ctx: DatasetContext, project: {
14
+ dir: string;
15
+ name: string;
16
+ }): Promise<void>;
17
+ //# sourceMappingURL=command.d.ts.map