@turri-dev/cli 0.4.2 → 0.4.3-next.8482fed
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/README.md +1 -0
- package/dist/background-skills.d.ts +4 -1
- package/dist/background-skills.js +9 -1
- package/dist/capture/agents.d.ts +361 -0
- package/dist/capture/agents.js +332 -0
- package/dist/capture/area.d.ts +58 -0
- package/dist/capture/area.js +197 -0
- package/dist/capture/assemble.d.ts +122 -0
- package/dist/capture/assemble.js +452 -0
- package/dist/capture/capture.d.ts +116 -0
- package/dist/capture/capture.js +1228 -0
- package/dist/capture/chat.d.ts +29 -0
- package/dist/capture/chat.js +94 -0
- package/dist/capture/checks.d.ts +142 -0
- package/dist/capture/checks.js +643 -0
- package/dist/capture/config.d.ts +13 -0
- package/dist/capture/config.js +60 -0
- package/dist/capture/interface.d.ts +71 -0
- package/dist/capture/interface.js +199 -0
- package/dist/capture/map.d.ts +56 -0
- package/dist/capture/map.js +530 -0
- package/dist/capture/progress.d.ts +108 -0
- package/dist/capture/progress.js +234 -0
- package/dist/capture/report.d.ts +75 -0
- package/dist/capture/report.js +267 -0
- package/dist/capture/scripts.d.ts +26 -0
- package/dist/capture/scripts.js +33 -0
- package/dist/capture/state.d.ts +296 -0
- package/dist/capture/state.js +43 -0
- package/dist/capture/trial.d.ts +34 -0
- package/dist/capture/trial.js +89 -0
- package/dist/capture-cli.d.ts +1 -0
- package/dist/capture-cli.js +77 -0
- package/dist/chat-commands.d.ts +2 -10
- package/dist/chat-commands.js +3 -57
- package/dist/chat-tab-header.d.ts +15 -0
- package/dist/chat-tab-header.js +78 -0
- package/dist/chat-tabs.d.ts +20 -0
- package/dist/chat-tabs.js +96 -0
- package/dist/chat-terminal.js +54 -8
- package/dist/chat-waiting.d.ts +4 -1
- package/dist/chat-waiting.js +7 -4
- package/dist/chats.d.ts +2 -0
- package/dist/cli.js +32 -14
- package/dist/config-edit.js +1 -0
- package/dist/config.d.ts +15 -2
- package/dist/config.js +57 -3
- package/dist/extension.js +140 -7
- package/dist/identity.js +2 -1
- package/dist/init-config.js +1 -0
- package/dist/loop-cli.js +5 -1
- package/dist/loop-tools.js +5 -1
- package/dist/new-project.d.ts +5 -0
- package/dist/new-project.js +11 -0
- package/dist/pi.js +2 -2
- package/dist/plain-code-blocks.d.ts +14 -0
- package/dist/plain-code-blocks.js +56 -0
- package/dist/pointer-hover.d.ts +12 -0
- package/dist/pointer-hover.js +44 -0
- package/dist/regenerate/command-check.js +80 -11
- package/dist/regenerate/jobs-tree.d.ts +1 -0
- package/dist/regenerate/jobs-tree.js +5 -1
- package/dist/regenerate/loop.js +9 -1
- package/dist/regenerate/side-panel.d.ts +4 -1
- package/dist/regenerate/side-panel.js +131 -22
- package/dist/regenerate/spec-map-view.d.ts +9 -1
- package/dist/regenerate/spec-map-view.js +22 -2
- package/dist/regenerate/test-scripts.d.ts +5 -0
- package/dist/regenerate/test-scripts.js +4 -0
- package/dist/reopen.d.ts +29 -0
- package/dist/reopen.js +47 -0
- package/dist/resume-line.d.ts +4 -1
- package/dist/resume-line.js +5 -2
- package/dist/setup.js +9 -1
- package/docs/README.md +2 -0
- package/docs/capture.md +191 -0
- package/docs/chats.md +6 -5
- package/docs/config.md +14 -2
- package/docs/development.md +4 -1
- package/docs/regenerating.md +1 -1
- package/docs/sandbox.md +2 -2
- package/docs/theming.md +2 -2
- package/docs/using.md +9 -1
- package/genome/capture/capture.md +26 -0
- package/genome/capture/check-conventions.md +5 -0
- package/genome/capture/check-reference.md +11 -0
- package/genome/capture/concepts.md +3 -0
- package/genome/capture/divide.md +9 -0
- package/genome/capture/feature.md +45 -0
- package/genome/capture/interface.md +9 -0
- package/genome/capture/modules.md +23 -0
- package/genome/capture/outline.md +11 -0
- package/genome/capture/readability.md +12 -0
- package/genome/capture/reference.md +10 -0
- package/genome/capture/split.md +9 -0
- package/genome/capture/survey.md +37 -0
- package/genome/capture/trial.md +18 -0
- package/genome/capture/verify.md +7 -0
- package/package.json +3 -3
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
import { readProjectConfig, roleTurnLimitMs } from "../config.js";
|
|
6
|
+
import { PACKAGE_ROOT } from "../environment.js";
|
|
7
|
+
import { ask, BEGIN, createExecutor, timeTurns } from "../pi.js";
|
|
8
|
+
import { withActivity } from "../regenerate/activity.js";
|
|
9
|
+
import { confinedTools } from "../regenerate/confine.js";
|
|
10
|
+
import { STDLIB_DIR } from "../regenerate/paths.js";
|
|
11
|
+
/**
|
|
12
|
+
* Capture's agents, each fresh, each with one thing to hand over through a submit tool of its own: the survey, its
|
|
13
|
+
* conventions checked, a split, an outline, a part's features divided, a feature, the interface no feature captured, a
|
|
14
|
+
* part's features gathered into modules, concepts reconciled, a module's readability pass, its interface checked against the code,
|
|
15
|
+
* its scenarios tried on the original. The harness checks what each hands over and sends it back with what is wrong, a
|
|
16
|
+
* few times at most; what still does not hold is the harness's to deal with - a feature left uncaptured, the report
|
|
17
|
+
* saying so - never a stop of the whole capture.
|
|
18
|
+
*/
|
|
19
|
+
/** How many times an answer that does not hold is sent back. */
|
|
20
|
+
export const RETRIES = 2;
|
|
21
|
+
/** How long one turn of a capture agent may take, unless the config's `models: capture:` says: exploring takes a while. */
|
|
22
|
+
export const CAPTURE_TURN_LIMIT_MS = 10 * 60 * 1000;
|
|
23
|
+
const PROMPTS_DIR = join(PACKAGE_ROOT, "genome", "capture");
|
|
24
|
+
/** One of capture's prompts, from genome/capture. */
|
|
25
|
+
export function capturePrompt(name) {
|
|
26
|
+
return readFileSync(join(PROMPTS_DIR, `${name}.md`), "utf8").trim();
|
|
27
|
+
}
|
|
28
|
+
/** What the agent's session reports, handed to its watch - and the capture's stop, which ends its turn. */
|
|
29
|
+
function watchedBy(watch, signal) {
|
|
30
|
+
return {
|
|
31
|
+
stage: () => { },
|
|
32
|
+
activity: (line) => watch?.did(line),
|
|
33
|
+
now: (label) => watch?.now(label),
|
|
34
|
+
usage: (usage) => watch?.spent(usage.cost),
|
|
35
|
+
transcript: () => { },
|
|
36
|
+
...(signal !== undefined ? { signal } : {}),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** Ask an agent for one answer, sending it back with what is wrong until it holds. Undefined, with the last problems, when it never did. */
|
|
40
|
+
export async function askAgent(options, tool, check) {
|
|
41
|
+
return watched(options.watch, options.signal, () => answerOf(options, tool, check));
|
|
42
|
+
}
|
|
43
|
+
/** Work of agents its watch sees from start to end - each tool call, each answer's cost - and the capture's stop ends. */
|
|
44
|
+
export async function watched(watch, signal, work) {
|
|
45
|
+
watch?.started();
|
|
46
|
+
try {
|
|
47
|
+
return await withActivity(watchedBy(watch, signal), work);
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
watch?.ended();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function answerOf(options, tool, check) {
|
|
54
|
+
let answer;
|
|
55
|
+
const submit = defineTool({
|
|
56
|
+
name: tool.name,
|
|
57
|
+
label: tool.label,
|
|
58
|
+
description: tool.description,
|
|
59
|
+
parameters: tool.parameters,
|
|
60
|
+
async execute(_id, params) {
|
|
61
|
+
answer = tool.convert(params);
|
|
62
|
+
return { content: [{ type: "text", text: "Recorded." }], details: {}, terminate: true };
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
const fence = options.shell !== undefined
|
|
66
|
+
? confinedTools({ workdir: options.shell.workdir, read: [options.shell.workdir], write: [options.shell.workdir], shellHome: options.shell.home })
|
|
67
|
+
: options.reads
|
|
68
|
+
? confinedTools({ workdir: options.root, read: [options.root, STDLIB_DIR], write: [], noBash: true })
|
|
69
|
+
: undefined;
|
|
70
|
+
const session = await createExecutor({
|
|
71
|
+
cwd: options.plainDir,
|
|
72
|
+
customTools: [submit, ...(fence?.tools ?? [])],
|
|
73
|
+
tools: [...(fence?.names ?? []), submit.name],
|
|
74
|
+
model: options.model,
|
|
75
|
+
rules: options.rules,
|
|
76
|
+
label: options.label,
|
|
77
|
+
role: "capture",
|
|
78
|
+
...(fence !== undefined ? { system: fence.note } : {}),
|
|
79
|
+
task: [capturePrompt("capture"), ...[options.prompt].flat().map(capturePrompt), options.context].join("\n\n"),
|
|
80
|
+
budget: { calls: options.calls, finish: [submit.name] },
|
|
81
|
+
});
|
|
82
|
+
const limit = turnLimit(options.plainDir);
|
|
83
|
+
timeTurns(session, limit === 0 ? 0 : Math.max(limit, options.turnAtLeastMs ?? 0));
|
|
84
|
+
let problems = [];
|
|
85
|
+
try {
|
|
86
|
+
await ask(session, BEGIN);
|
|
87
|
+
for (let again = 0;; again += 1) {
|
|
88
|
+
problems = answer === undefined ? [`You did not call ${submit.name}.`] : check(answer);
|
|
89
|
+
if (problems.length === 0)
|
|
90
|
+
return { answer, problems };
|
|
91
|
+
if (again >= RETRIES)
|
|
92
|
+
return { problems };
|
|
93
|
+
answer = undefined;
|
|
94
|
+
await ask(session, `That does not hold:\n- ${problems.join("\n- ")}\n\nCall ${submit.name} again, with it fixed.`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
session.dispose();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function turnLimit(plainDir) {
|
|
102
|
+
try {
|
|
103
|
+
return roleTurnLimitMs(readProjectConfig(plainDir), "capture") ?? CAPTURE_TURN_LIMIT_MS;
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return CAPTURE_TURN_LIMIT_MS;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
const Strings = (description) => Type.Array(Type.String(), { description });
|
|
110
|
+
const Definitions = Type.Array(Type.Object({
|
|
111
|
+
concept: Type.String({ description: "The concept, between colons: :Note:." }),
|
|
112
|
+
text: Type.String({ description: "One plain sentence that starts with the concept: \":Note: is a short text a :User: keeps.\"" }),
|
|
113
|
+
}));
|
|
114
|
+
const LeftOutSchema = Type.Array(Type.Object({ path: Type.String(), why: Type.String() }), { description: "What is not captured, by path, with why." });
|
|
115
|
+
/** A list of strings an agent handed over, its blanks dropped. */
|
|
116
|
+
export function strings(value) {
|
|
117
|
+
return Array.isArray(value) ? value.filter((item) => typeof item === "string").map((item) => item.trim()).filter((item) => item !== "") : [];
|
|
118
|
+
}
|
|
119
|
+
function records(value) {
|
|
120
|
+
return Array.isArray(value) ? value.filter((item) => item !== null && typeof item === "object") : [];
|
|
121
|
+
}
|
|
122
|
+
function text(value) {
|
|
123
|
+
return typeof value === "string" ? value.trim() : "";
|
|
124
|
+
}
|
|
125
|
+
export function definitionsOf(value) {
|
|
126
|
+
return records(value).map((item) => ({ concept: text(item.concept), text: text(item.text) }));
|
|
127
|
+
}
|
|
128
|
+
export function leftOutOf(value) {
|
|
129
|
+
return records(value).map((item) => ({ path: text(item.path).replace(/\/+$/, "") || ".", why: text(item.why) }));
|
|
130
|
+
}
|
|
131
|
+
const ConventionSchema = Type.Object({
|
|
132
|
+
text: Type.String({ description: "One fact every feature follows, exact - the stream, the text, the code - without the leading - ." }),
|
|
133
|
+
where: Strings("The files whose code decides it, relative to the codebase."),
|
|
134
|
+
});
|
|
135
|
+
export const CONVENTIONS_TOOL = {
|
|
136
|
+
name: "submit_capture_conventions",
|
|
137
|
+
label: "Submit the conventions",
|
|
138
|
+
description: "Every convention as the code has it - those that hold word for word, those corrected, and any the code shows that was missed - and what you changed.",
|
|
139
|
+
parameters: Type.Object({
|
|
140
|
+
conventions: Type.Array(ConventionSchema),
|
|
141
|
+
changes: Strings("What you changed, a line each, with why - citing the code. None when every convention held."),
|
|
142
|
+
}),
|
|
143
|
+
};
|
|
144
|
+
export const SURVEY_TOOL = {
|
|
145
|
+
name: "submit_capture_survey",
|
|
146
|
+
label: "Submit the survey",
|
|
147
|
+
description: "The codebase's parts, each with its stack, its implementation and test reqs and its core concepts; what is left out; what you assumed.",
|
|
148
|
+
parameters: Type.Object({
|
|
149
|
+
parts: Type.Array(Type.Object({
|
|
150
|
+
name: Type.String({ description: "Short kebab-case." }),
|
|
151
|
+
about: Type.String({ description: "What the part is, in a line." }),
|
|
152
|
+
paths: Strings("Its folders and files, relative to the codebase - . for the whole of it."),
|
|
153
|
+
stdlib_stack: Type.Optional(Type.String({ description: "A standard library stack it imports, stdlib/<kind>/<stack> - only one the original agrees with." })),
|
|
154
|
+
stdlib_conformance: Type.Optional(Type.Boolean({ description: "Import that stack's conformance template too." })),
|
|
155
|
+
implementation_reqs: Strings("How it is built, one fact each, without the leading - ."),
|
|
156
|
+
conventions: Type.Optional(Type.Array(ConventionSchema, {
|
|
157
|
+
description: "What every feature does the same way and a user or a script relies on: how errors are written, the exit code or status of each kind of failure, how input and output act when not a terminal, and what reaches the other side of every call it makes or answers - the headers it adds, how bodies and headers are encoded, when a body is read.",
|
|
158
|
+
})),
|
|
159
|
+
test_reqs: Strings("How its conformance tests run, one fact each, and what each sets up so the software acts the same on every run and machine - empty when the conformance template says it all."),
|
|
160
|
+
definitions: Definitions,
|
|
161
|
+
})),
|
|
162
|
+
left_out: LeftOutSchema,
|
|
163
|
+
assumptions: Strings("What you decided where the codebase left it open, a line each."),
|
|
164
|
+
}),
|
|
165
|
+
};
|
|
166
|
+
export const REFERENCE_TOOL = {
|
|
167
|
+
name: "submit_capture_reference",
|
|
168
|
+
label: "Submit the interface reference",
|
|
169
|
+
description: "Every piece of the interface the files you were given define - each command, argument, option, route and environment variable - exactly as the code has it, each with where it is defined; and what the map found there that is not the software's interface, with why.",
|
|
170
|
+
parameters: Type.Object({
|
|
171
|
+
entries: Type.Array(Type.Object({
|
|
172
|
+
kind: Type.Union([Type.Literal("command"), Type.Literal("argument"), Type.Literal("option"), Type.Literal("route"), Type.Literal("env"), Type.Literal("other")]),
|
|
173
|
+
name: Type.String({ description: "Exactly as the code has it: --proxy, http, httpie plugins install, GET /notes/<int:id> (the code's own parameter syntax), HTTPIE_CONFIG_DIR." }),
|
|
174
|
+
aliases: Strings("Every other name it has, as the code has each: -x."),
|
|
175
|
+
command: Type.Optional(Type.String({ description: "For an argument or an option: the command it belongs to, as its users type it - http, httpie plugins install." })),
|
|
176
|
+
takes: Type.Optional(Type.String({ description: "What it takes: its value's form or type, as the code names it - its metavar, its type, a body." })),
|
|
177
|
+
choices: Strings("The values it accepts when the code lists them, each exactly as the code has it."),
|
|
178
|
+
default: Type.Optional(Type.String({ description: "Its default, exactly as the code has it. Left out when it has none." })),
|
|
179
|
+
repeatable: Type.Optional(Type.Boolean()),
|
|
180
|
+
required: Type.Optional(Type.Boolean()),
|
|
181
|
+
hidden: Type.Optional(Type.Boolean({ description: "True when it is left out of its help." })),
|
|
182
|
+
help: Type.Optional(Type.String({ description: "Its help text, word for word as the code has it, placeholders and all - never a summary." })),
|
|
183
|
+
details: Strings("A route's parameters, fields and answers, a line each: query page: int, 1 by default; 201: the note created."),
|
|
184
|
+
where: Type.String({ description: "path:line - the file it is defined in, relative to the codebase, and the line." }),
|
|
185
|
+
also: Strings("Other files its literals come from: where a default or its choices are worked out."),
|
|
186
|
+
})),
|
|
187
|
+
excluded: Type.Array(Type.Object({ name: Type.String(), path: Type.String(), why: Type.String() }), { description: "What the map found in your files that is not the software's interface - an option of a development script, say - by name, with why. None when none." }),
|
|
188
|
+
}),
|
|
189
|
+
};
|
|
190
|
+
export const REFERENCE_CHECKED_TOOL = {
|
|
191
|
+
name: "submit_capture_reference_checked",
|
|
192
|
+
label: "Submit the checked reference",
|
|
193
|
+
description: "Every entry of the interface reference as the code has it - those that hold word for word, those corrected, those missed - every exclusion, and what you changed.",
|
|
194
|
+
parameters: Type.Object({
|
|
195
|
+
...REFERENCE_TOOL.parameters.properties,
|
|
196
|
+
changes: Strings("What you changed, a line each, with why - the file, and what its code does. None when every entry held."),
|
|
197
|
+
}),
|
|
198
|
+
};
|
|
199
|
+
export const AREAS_TOOL = {
|
|
200
|
+
name: "submit_capture_areas",
|
|
201
|
+
label: "Submit the areas",
|
|
202
|
+
description: "Your area split into smaller areas, in build order, every file in one of them or left out.",
|
|
203
|
+
parameters: Type.Object({
|
|
204
|
+
areas: Type.Array(Type.Object({
|
|
205
|
+
name: Type.String({ description: "Short kebab-case, from what the area does." }),
|
|
206
|
+
about: Type.String({ description: "What it does, in a line." }),
|
|
207
|
+
paths: Strings("Its folders and files, relative to the codebase."),
|
|
208
|
+
})),
|
|
209
|
+
left_out: LeftOutSchema,
|
|
210
|
+
}),
|
|
211
|
+
};
|
|
212
|
+
export const FEATURES_TOOL = {
|
|
213
|
+
name: "submit_capture_features",
|
|
214
|
+
label: "Submit the features",
|
|
215
|
+
description: "Your area's features, in build order, each with the folders and files where it is built.",
|
|
216
|
+
parameters: Type.Object({
|
|
217
|
+
features: Type.Array(Type.Object({
|
|
218
|
+
name: Type.String({ description: "Short kebab-case." }),
|
|
219
|
+
title: Type.String({ description: "A few words." }),
|
|
220
|
+
about: Type.String({ description: "What it lets someone do, in a line." }),
|
|
221
|
+
paths: Strings("Where it is built, relative to the codebase."),
|
|
222
|
+
})),
|
|
223
|
+
left_out: LeftOutSchema,
|
|
224
|
+
}),
|
|
225
|
+
};
|
|
226
|
+
export const OWNERS_TOOL = {
|
|
227
|
+
name: "submit_capture_owners",
|
|
228
|
+
label: "Submit the owners",
|
|
229
|
+
description: "Each behaviour given to one feature: what every feature that stays owns, and the features merged into others.",
|
|
230
|
+
parameters: Type.Object({
|
|
231
|
+
features: Type.Array(Type.Object({
|
|
232
|
+
feature: Type.String({ description: "The feature's key, as listed: <area>#<feature>." }),
|
|
233
|
+
owns: Type.String({ description: "What it alone captures, in a line: its options, commands, messages and behaviour." }),
|
|
234
|
+
}), { description: "Every feature that stays." }),
|
|
235
|
+
merged: Type.Array(Type.Object({
|
|
236
|
+
feature: Type.String({ description: "The key of the feature merged." }),
|
|
237
|
+
into: Strings("The keys of the features that take its behaviour."),
|
|
238
|
+
why: Type.String({ description: "Why, in a line." }),
|
|
239
|
+
}), { description: "Features whose behaviour others own whole - none when none." }),
|
|
240
|
+
}),
|
|
241
|
+
};
|
|
242
|
+
const ScenarioSchema = Type.Array(Type.Object({ scenario_id: Type.String(), text: Type.String() }));
|
|
243
|
+
export const FEATURE_TOOL = {
|
|
244
|
+
name: "submit_capture_feature",
|
|
245
|
+
label: "Submit the feature",
|
|
246
|
+
description: "The feature's functional specs in build order, each with its scenarios when it needs them; the new concepts they use; what you assumed, are unsure of and dropped.",
|
|
247
|
+
parameters: Type.Object({
|
|
248
|
+
functional_specs: Type.Array(Type.Object({
|
|
249
|
+
func_id: Type.String({ description: "Short kebab-case, unique in the feature." }),
|
|
250
|
+
text: Type.String({ description: "What the software does, as ***plain: its first line the behaviour, then its detail." }),
|
|
251
|
+
conformance: Type.Optional(ScenarioSchema),
|
|
252
|
+
acceptance: Type.Optional(ScenarioSchema),
|
|
253
|
+
})),
|
|
254
|
+
definitions: Definitions,
|
|
255
|
+
assumptions: Strings("What you decided where the code left it open."),
|
|
256
|
+
uncertain: Strings("What you captured without being sure of it."),
|
|
257
|
+
dropped: Strings("Behaviour judged incidental and left out."),
|
|
258
|
+
}),
|
|
259
|
+
};
|
|
260
|
+
export const INTERFACE_TOOL = {
|
|
261
|
+
name: "submit_capture_interface",
|
|
262
|
+
label: "Submit the interface",
|
|
263
|
+
description: "The interface no feature captured, as a feature of its own: its title, its functional specs with their scenarios, the new concepts they use; what you assumed and are unsure of; and each surface dropped as not the software's interface, by name, with why.",
|
|
264
|
+
parameters: Type.Object({
|
|
265
|
+
title: Type.String({ description: "A few words for what they are, as a feature of the module: Proxies and network options." }),
|
|
266
|
+
...FEATURE_TOOL.parameters.properties,
|
|
267
|
+
dropped: Strings("Each surface that is not the software's interface - a development script's option, a variable only its tests read - by its name, with why."),
|
|
268
|
+
}),
|
|
269
|
+
};
|
|
270
|
+
export const CORRECTIONS_TOOL = {
|
|
271
|
+
name: "submit_capture_corrections",
|
|
272
|
+
label: "Submit the corrections",
|
|
273
|
+
description: "Each interface fact the ***plain gets wrong or leaves out, corrected: the functional spec's func-id, a scenario's id when it is the scenario, its whole new text, and why from the code. None when everything holds.",
|
|
274
|
+
parameters: Type.Object({
|
|
275
|
+
corrections: Type.Array(Type.Object({
|
|
276
|
+
func_id: Type.String(),
|
|
277
|
+
scenario_id: Type.Optional(Type.String({ description: "Only when the correction is to this scenario of the functional spec." })),
|
|
278
|
+
text: Type.String({ description: "The whole new text of the functional spec, or of the scenario." }),
|
|
279
|
+
why: Type.String({ description: "What was wrong or missing, citing the code: file and what it does." }),
|
|
280
|
+
})),
|
|
281
|
+
}),
|
|
282
|
+
};
|
|
283
|
+
export const MODULES_TOOL = {
|
|
284
|
+
name: "submit_capture_modules",
|
|
285
|
+
label: "Submit the modules",
|
|
286
|
+
description: "The part's features gathered into modules by concern, in the order the modules are built - each with its name, what it is, its features and the modules before it that it needs; and the scenarios rewritten to need only the modules before their own.",
|
|
287
|
+
parameters: Type.Object({
|
|
288
|
+
modules: Type.Array(Type.Object({
|
|
289
|
+
name: Type.String({ description: "Short kebab-case, the concern: output-formatting, sessions." }),
|
|
290
|
+
about: Type.String({ description: "What it is, in a line." }),
|
|
291
|
+
features: Strings("Its features, by key as listed, in the order its functional specs are written."),
|
|
292
|
+
needs: Strings("The modules before it whose behaviour its functional specs build on and its scenarios run, by name - none for the first."),
|
|
293
|
+
}), { description: "Every module, in the order they are built: every feature in one of them." }),
|
|
294
|
+
rewrites: Type.Array(Type.Object({
|
|
295
|
+
feature: Type.String({ description: "The feature's key, as listed." }),
|
|
296
|
+
func_id: Type.String(),
|
|
297
|
+
scenario_id: Type.String(),
|
|
298
|
+
text: Type.String({ description: "The scenario's whole new text." }),
|
|
299
|
+
why: Type.String({ description: "The later module it needed, and how it now checks the same without it." }),
|
|
300
|
+
}), { description: "Scenarios that would need a module after their own whatever the order, rewritten - none when none." }),
|
|
301
|
+
}),
|
|
302
|
+
};
|
|
303
|
+
export const TRIAL_TOOL = {
|
|
304
|
+
name: "submit_capture_trial",
|
|
305
|
+
label: "Submit the trial",
|
|
306
|
+
description: "What the original did with each scenario you were given - held, corrected to what it does, removed as nothing outside can check it, or untried with why - the functional specs it contradicts, corrected, and what its conformance tests must set up.",
|
|
307
|
+
parameters: Type.Object({
|
|
308
|
+
scenarios: Type.Array(Type.Object({
|
|
309
|
+
func_id: Type.String(),
|
|
310
|
+
scenario_id: Type.String(),
|
|
311
|
+
outcome: Type.Union([Type.Literal("held"), Type.Literal("corrected"), Type.Literal("removed"), Type.Literal("untried")]),
|
|
312
|
+
ran: Type.String({ description: "What you ran against the original - the command or the request - and what it answered, in a line." }),
|
|
313
|
+
text: Type.Optional(Type.String({ description: "A corrected scenario's whole new text - with the setup only it needs, which is never a test req." })),
|
|
314
|
+
why: Type.Optional(Type.String({ description: "Why it was corrected, removed or not tried." })),
|
|
315
|
+
}), { description: "Every scenario you were given, each once." }),
|
|
316
|
+
functional_specs: Type.Array(Type.Object({ func_id: Type.String(), text: Type.String({ description: "Its whole new text." }), why: Type.String({ description: "What the original does, and what you ran that shows it." }) }), { description: "Functional specs whose text the original contradicts, corrected - none when none." }),
|
|
317
|
+
test_reqs: Strings("What every conformance test of the part must set up for the original to act as the scenarios say, one fact each with :ConformanceTests: as the subject - none when the test reqs say it all. What only some scenarios need goes in their own text, corrected; a test req names no scenario or functional spec."),
|
|
318
|
+
}),
|
|
319
|
+
};
|
|
320
|
+
export const CONCEPTS_TOOL = {
|
|
321
|
+
name: "submit_capture_concepts",
|
|
322
|
+
label: "Submit the concepts",
|
|
323
|
+
description: "One definition for each concept, serving every use of it.",
|
|
324
|
+
parameters: Type.Object({ definitions: Definitions }),
|
|
325
|
+
};
|
|
326
|
+
export const EDITS_TOOL = {
|
|
327
|
+
name: "submit_capture_edits",
|
|
328
|
+
label: "Submit the edits",
|
|
329
|
+
description: "The new text of each functional spec you rewrote, by its func-id - none when none needs it.",
|
|
330
|
+
parameters: Type.Object({ edits: Type.Array(Type.Object({ func_id: Type.String(), text: Type.String() })) }),
|
|
331
|
+
};
|
|
332
|
+
export { records, text };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { CodeMap, MapFile } from "./map.ts";
|
|
2
|
+
/**
|
|
3
|
+
* An area of the codebase: some of its folders and files, named. Capture splits the codebase into areas until each fits
|
|
4
|
+
* one agent's budget (capture.ts); an area that fits is outlined into features. What an agent is shown of an area is
|
|
5
|
+
* its part of the map, folded to fit a budget of characters - never the code.
|
|
6
|
+
*/
|
|
7
|
+
export interface Area {
|
|
8
|
+
/** Unique in the run: the names from the part down, joined by `/`. */
|
|
9
|
+
id: string;
|
|
10
|
+
/** Short kebab-case, from what the area does. */
|
|
11
|
+
name: string;
|
|
12
|
+
/** What it does, in a line. */
|
|
13
|
+
about: string;
|
|
14
|
+
/** Its folders and files, relative to the codebase with `/` - `.` for the whole of it. */
|
|
15
|
+
paths: string[];
|
|
16
|
+
}
|
|
17
|
+
/** Whether a file is in a folder or is the file a path names. */
|
|
18
|
+
export declare function covers(path: string, file: string): boolean;
|
|
19
|
+
/** The files of an area. */
|
|
20
|
+
export declare function filesOf(map: CodeMap, paths: string[]): MapFile[];
|
|
21
|
+
/** How many lines an area's files hold - its size, as far as capture is concerned. Assets count for nothing. */
|
|
22
|
+
export declare function linesOf(files: MapFile[]): number;
|
|
23
|
+
/** A test file of the original, as evidence of what it does: its lines and the tests the map named in it. */
|
|
24
|
+
export interface TestEvidence {
|
|
25
|
+
path: string;
|
|
26
|
+
lines: number;
|
|
27
|
+
tests: string[];
|
|
28
|
+
}
|
|
29
|
+
/** The original's tests among some paths: the test files holding at least one test the map found, largest first. */
|
|
30
|
+
export declare function testEvidence(map: CodeMap, paths: string[]): TestEvidence[];
|
|
31
|
+
/** One of the folders and files an area divides into. */
|
|
32
|
+
export interface Entry {
|
|
33
|
+
path: string;
|
|
34
|
+
dir: boolean;
|
|
35
|
+
files: MapFile[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* What an area divides into, for an agent to split it: a single folder's folders and files - going down through a
|
|
39
|
+
* folder that holds a single folder and nothing else - or the area's own paths, when it has several.
|
|
40
|
+
*/
|
|
41
|
+
export declare function entriesOf(map: CodeMap, paths: string[]): Entry[];
|
|
42
|
+
/**
|
|
43
|
+
* An area's part of the map as text, folded to fit `budget` characters: as deep and with as many surface names as fit -
|
|
44
|
+
* down to its entries with counts alone, and past that only the first entries, the rest counted.
|
|
45
|
+
*/
|
|
46
|
+
export declare function renderArea(map: CodeMap, paths: string[], budget: number): string;
|
|
47
|
+
/**
|
|
48
|
+
* Areas in build order: an area whose files import another's comes after it - a topological order of the folders'
|
|
49
|
+
* import graph - with the order they were given kept where nothing says otherwise. A cycle is broken where the given
|
|
50
|
+
* order puts its first area.
|
|
51
|
+
*/
|
|
52
|
+
export declare function buildOrder<T extends {
|
|
53
|
+
paths: string[];
|
|
54
|
+
}>(map: CodeMap, areas: T[]): T[];
|
|
55
|
+
/** A name for a path, as an area would be named: its last part, kebab-case. */
|
|
56
|
+
export declare function nameOfPath(path: string): string;
|
|
57
|
+
/** Text as kebab-case: lowercase letters, digits and single hyphens. */
|
|
58
|
+
export declare function kebab(text: string): string;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { posix } from "node:path";
|
|
2
|
+
/** Whether a file is in a folder or is the file a path names. */
|
|
3
|
+
export function covers(path, file) {
|
|
4
|
+
return path === "." || path === "" || file === path || file.startsWith(`${path}/`);
|
|
5
|
+
}
|
|
6
|
+
/** The files of an area. */
|
|
7
|
+
export function filesOf(map, paths) {
|
|
8
|
+
return map.files.filter((file) => paths.some((path) => covers(path, file.path)));
|
|
9
|
+
}
|
|
10
|
+
/** How many lines an area's files hold - its size, as far as capture is concerned. Assets count for nothing. */
|
|
11
|
+
export function linesOf(files) {
|
|
12
|
+
return files.reduce((sum, file) => sum + file.lines, 0);
|
|
13
|
+
}
|
|
14
|
+
/** The original's tests among some paths: the test files holding at least one test the map found, largest first. */
|
|
15
|
+
export function testEvidence(map, paths) {
|
|
16
|
+
return filesOf(map, paths)
|
|
17
|
+
.filter((file) => file.kind === "test")
|
|
18
|
+
.map((file) => ({ path: file.path, lines: file.lines, tests: file.surfaces.filter((surface) => surface.kind === "test").map((surface) => surface.name) }))
|
|
19
|
+
.filter((file) => file.tests.length > 0)
|
|
20
|
+
.sort((a, b) => b.lines - a.lines);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* What an area divides into, for an agent to split it: a single folder's folders and files - going down through a
|
|
24
|
+
* folder that holds a single folder and nothing else - or the area's own paths, when it has several.
|
|
25
|
+
*/
|
|
26
|
+
export function entriesOf(map, paths) {
|
|
27
|
+
const files = filesOf(map, paths);
|
|
28
|
+
const isDir = (path) => path === "." || files.some((file) => file.path.startsWith(`${path === "." ? "" : `${path}/`}`) && file.path !== path);
|
|
29
|
+
if (paths.length === 1 && isDir(paths[0])) {
|
|
30
|
+
let dir = paths[0];
|
|
31
|
+
for (;;) {
|
|
32
|
+
const children = childrenOf(files, dir);
|
|
33
|
+
if (children.length === 1 && children[0].dir) {
|
|
34
|
+
dir = children[0].path;
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
return children;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return paths.map((path) => ({ path, dir: isDir(path), files: filesOf(map, [path]) }));
|
|
41
|
+
}
|
|
42
|
+
function childrenOf(files, dir) {
|
|
43
|
+
const prefix = dir === "." || dir === "" ? "" : `${dir}/`;
|
|
44
|
+
const byChild = new Map();
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
if (!file.path.startsWith(prefix))
|
|
47
|
+
continue;
|
|
48
|
+
const rest = file.path.slice(prefix.length);
|
|
49
|
+
const slash = rest.indexOf("/");
|
|
50
|
+
const path = slash === -1 ? file.path : `${prefix}${rest.slice(0, slash)}`;
|
|
51
|
+
const entry = byChild.get(path) ?? { path, dir: slash !== -1, files: [] };
|
|
52
|
+
entry.files.push(file);
|
|
53
|
+
byChild.set(path, entry);
|
|
54
|
+
}
|
|
55
|
+
return [...byChild.values()].sort((a, b) => (a.dir === b.dir ? a.path.localeCompare(b.path) : a.dir ? -1 : 1));
|
|
56
|
+
}
|
|
57
|
+
const SURFACE_LABELS = [
|
|
58
|
+
["route", "routes"],
|
|
59
|
+
["command", "commands"],
|
|
60
|
+
["option", "options"],
|
|
61
|
+
["screen", "screens"],
|
|
62
|
+
["env", "environment variables"],
|
|
63
|
+
["table", "tables"],
|
|
64
|
+
["test", "tests"],
|
|
65
|
+
["export", "exports"],
|
|
66
|
+
];
|
|
67
|
+
const KIND_LABELS = [
|
|
68
|
+
["source", "source"],
|
|
69
|
+
["test", "test"],
|
|
70
|
+
["contract", "contract"],
|
|
71
|
+
["doc", "doc"],
|
|
72
|
+
["manifest", "manifest"],
|
|
73
|
+
["asset", "asset"],
|
|
74
|
+
["other", "other"],
|
|
75
|
+
];
|
|
76
|
+
/** A line of surfaces: each kind with as many names as `sample` allows and how many more there are. */
|
|
77
|
+
function surfaceLine(surfaces, sample) {
|
|
78
|
+
const parts = [];
|
|
79
|
+
for (const [kind, label] of SURFACE_LABELS) {
|
|
80
|
+
const names = [...new Set(surfaces.filter((surface) => surface.kind === kind).map((surface) => surface.name))];
|
|
81
|
+
if (names.length === 0)
|
|
82
|
+
continue;
|
|
83
|
+
if (sample === 0) {
|
|
84
|
+
parts.push(`${names.length} ${label}`);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const shown = names.slice(0, sample).join(", ");
|
|
88
|
+
parts.push(`${label}: ${shown}${names.length > sample ? ` (+${names.length - sample})` : ""}`);
|
|
89
|
+
}
|
|
90
|
+
return parts.join("; ");
|
|
91
|
+
}
|
|
92
|
+
function kindCounts(files) {
|
|
93
|
+
return KIND_LABELS.map(([kind, label]) => [files.filter((file) => file.kind === kind).length, label])
|
|
94
|
+
.filter(([count]) => count > 0)
|
|
95
|
+
.map(([count, label]) => `${count} ${label}`)
|
|
96
|
+
.join(", ");
|
|
97
|
+
}
|
|
98
|
+
function entryLine(entry, sample, indent) {
|
|
99
|
+
const surfaces = surfaceLine(entry.files.flatMap((file) => file.surfaces), sample);
|
|
100
|
+
if (entry.dir) {
|
|
101
|
+
return `${indent}${entry.path}/ - ${entry.files.length} files (${kindCounts(entry.files)}), ${linesOf(entry.files)} lines${surfaces !== "" ? ` - ${surfaces}` : ""}`;
|
|
102
|
+
}
|
|
103
|
+
const file = entry.files[0];
|
|
104
|
+
return `${indent}${entry.path} - ${file?.kind ?? "file"}, ${file?.lines ?? 0} lines${surfaces !== "" ? ` - ${surfaces}` : ""}`;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* An area's part of the map as text, folded to fit `budget` characters: as deep and with as many surface names as fit -
|
|
108
|
+
* down to its entries with counts alone, and past that only the first entries, the rest counted.
|
|
109
|
+
*/
|
|
110
|
+
export function renderArea(map, paths, budget) {
|
|
111
|
+
const files = filesOf(map, paths);
|
|
112
|
+
const header = `${paths.map((path) => (path === "." ? "the whole codebase" : path)).join(", ")} - ${files.length} files (${kindCounts(files)}), ${linesOf(files)} lines`;
|
|
113
|
+
const entries = entriesOf(map, paths);
|
|
114
|
+
const attempts = [
|
|
115
|
+
{ depth: 3, sample: 12 },
|
|
116
|
+
{ depth: 2, sample: 12 },
|
|
117
|
+
{ depth: 2, sample: 6 },
|
|
118
|
+
{ depth: 1, sample: 8 },
|
|
119
|
+
{ depth: 1, sample: 3 },
|
|
120
|
+
{ depth: 1, sample: 0 },
|
|
121
|
+
];
|
|
122
|
+
for (const { depth, sample } of attempts) {
|
|
123
|
+
const text = [header, "", ...renderEntries(map, entries, depth, sample, "")].join("\n");
|
|
124
|
+
if (text.length <= budget)
|
|
125
|
+
return text;
|
|
126
|
+
}
|
|
127
|
+
// Too many entries even counted: the first that fit, and the rest summed up.
|
|
128
|
+
const lines = [header, ""];
|
|
129
|
+
let used = lines.join("\n").length;
|
|
130
|
+
let shown = 0;
|
|
131
|
+
for (const entry of entries) {
|
|
132
|
+
const line = entryLine(entry, 0, "");
|
|
133
|
+
if (used + line.length + 1 > budget - 120)
|
|
134
|
+
break;
|
|
135
|
+
lines.push(line);
|
|
136
|
+
used += line.length + 1;
|
|
137
|
+
shown += 1;
|
|
138
|
+
}
|
|
139
|
+
const rest = entries.slice(shown);
|
|
140
|
+
if (rest.length > 0)
|
|
141
|
+
lines.push(`... and ${rest.length} more entries - ${rest.reduce((sum, entry) => sum + entry.files.length, 0)} files, ${linesOf(rest.flatMap((entry) => entry.files))} lines`);
|
|
142
|
+
return lines.join("\n");
|
|
143
|
+
}
|
|
144
|
+
function renderEntries(map, entries, depth, sample, indent) {
|
|
145
|
+
return entries.flatMap((entry) => {
|
|
146
|
+
const line = entryLine(entry, sample, indent);
|
|
147
|
+
if (!entry.dir || depth <= 1)
|
|
148
|
+
return [line];
|
|
149
|
+
return [line, ...renderEntries(map, childrenOf(entry.files, entry.path), depth - 1, sample, `${indent} `)];
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Areas in build order: an area whose files import another's comes after it - a topological order of the folders'
|
|
154
|
+
* import graph - with the order they were given kept where nothing says otherwise. A cycle is broken where the given
|
|
155
|
+
* order puts its first area.
|
|
156
|
+
*/
|
|
157
|
+
export function buildOrder(map, areas) {
|
|
158
|
+
const owner = new Map();
|
|
159
|
+
areas.forEach((area, index) => {
|
|
160
|
+
for (const file of filesOf(map, area.paths))
|
|
161
|
+
if (!owner.has(file.path))
|
|
162
|
+
owner.set(file.path, index);
|
|
163
|
+
});
|
|
164
|
+
const dependsOn = areas.map(() => new Set());
|
|
165
|
+
for (const file of map.files) {
|
|
166
|
+
const from = owner.get(file.path);
|
|
167
|
+
if (from === undefined)
|
|
168
|
+
continue;
|
|
169
|
+
for (const target of file.imports) {
|
|
170
|
+
const to = owner.get(target);
|
|
171
|
+
if (to !== undefined && to !== from)
|
|
172
|
+
dependsOn[from].add(to);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const placed = [];
|
|
176
|
+
const done = new Set();
|
|
177
|
+
while (placed.length < areas.length) {
|
|
178
|
+
const ready = areas.findIndex((_, index) => !done.has(index) && [...dependsOn[index]].every((dep) => done.has(dep)));
|
|
179
|
+
const next = ready !== -1 ? ready : areas.findIndex((_, index) => !done.has(index));
|
|
180
|
+
placed.push(next);
|
|
181
|
+
done.add(next);
|
|
182
|
+
}
|
|
183
|
+
return placed.map((index) => areas[index]);
|
|
184
|
+
}
|
|
185
|
+
/** A name for a path, as an area would be named: its last part, kebab-case. */
|
|
186
|
+
export function nameOfPath(path) {
|
|
187
|
+
const base = path === "." || path === "" ? "app" : posix.basename(path).replace(/\.[^.]+$/, "");
|
|
188
|
+
return kebab(base) || "app";
|
|
189
|
+
}
|
|
190
|
+
/** Text as kebab-case: lowercase letters, digits and single hyphens. */
|
|
191
|
+
export function kebab(text) {
|
|
192
|
+
return text
|
|
193
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
194
|
+
.toLowerCase()
|
|
195
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
196
|
+
.replace(/^-+|-+$/g, "");
|
|
197
|
+
}
|