@henols/c64-re-tools 2.8.0-rc.5 → 2.8.0-rc.7
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/package.json +1 -1
- package/skills/c64-assembler/SKILL.md +1 -1
- package/skills/c64-assembler/scripts/assembler.ts +78 -24
- package/skills/c64-docs/scripts/docs.ts +31 -10
- package/skills/c64-knowledge/SKILL.md +4 -3
- package/skills/c64-knowledge/scripts/knowledge.ts +31 -12
- package/skills/c64-packer/scripts/packer.ts +9 -2
- package/skills/c64-provenance/SKILL.md +1 -1
- package/skills/c64-provenance/scripts/lib/runtime.ts +5 -16
- package/skills/c64-source/references/loader.md +8 -7
- package/skills/c64-source/scripts/lib/loader.ts +4 -111
- package/skills/c64-source/scripts/source.ts +49 -78
- package/skills/c64-static-analysis/SKILL.md +2 -2
- package/skills/c64-static-analysis/scripts/lib/coverage.ts +56 -4
- package/skills/c64-static-analysis/scripts/static-analysis.ts +32 -21
- package/skills/c64-testing/SKILL.md +6 -0
- package/skills/c64-testing/scripts/lib/play.ts +8 -7
- package/skills/c64-testing/scripts/lib/recording.ts +218 -12
- package/skills/c64-testing/scripts/lib/replay.ts +7 -3
- package/skills/c64-testing/scripts/lib/runs.ts +5 -1
- package/skills/c64-testing/scripts/lib/scenario.ts +4 -0
- package/skills/c64-testing/scripts/testing.ts +24 -11
- package/skills/c64-unpacker/SKILL.md +6 -4
- package/skills/c64-unpacker/scripts/lib/loads.ts +4 -2
- package/skills/c64-unpacker/scripts/lib/machine.ts +128 -5
- package/skills/c64-unpacker/scripts/lib/stages.ts +5 -1
- package/skills/c64-unpacker/scripts/unpacker.ts +13 -12
- package/skills/emulator/SKILL.md +2 -2
- package/src/host/vice/adapter.ts +2 -1
- package/src/host-client/image.ts +33 -4
- package/src/host-client/tools.ts +1 -1
- package/src/host-client/transfer.ts +48 -26
- package/src/host-client/vice-session.ts +15 -0
- package/src/knowledge/database.ts +72 -19
- package/src/knowledge/import.ts +31 -8
- package/src/knowledge/names.ts +2 -2
- package/src/knowledge/program.ts +67 -7
- package/src/knowledge/schema.ts +6 -0
- package/src/knowledge/start-screen.ts +55 -2
- package/src/knowledge/write.ts +18 -12
- package/src/loader.ts +101 -33
- package/src/mcp/output.ts +13 -12
- package/src/mcp/server.ts +33 -22
- package/src/mcp/tools/debug.ts +21 -24
- package/src/mcp/tools/media.ts +27 -27
- package/src/mcp/tools/memory.ts +13 -17
- package/src/mcp/tools/video.ts +10 -12
- package/src/native/c64run.ts +39 -1
- package/src/native/decompiler/analyze.ts +1 -1
- package/src/native/dxa.ts +2 -2
- package/src/native/types.ts +26 -7
- package/src/program-code.ts +4 -2
- package/src/project.ts +224 -71
- package/src/protocol/vice.ts +5 -3
- package/src/script.ts +60 -15
- package/{skills/c64-source/scripts/lib → src}/stage.ts +142 -11
- package/tools/c64run.c +18 -5
package/src/project.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
// The project: the nearest folder at or above the working directory that
|
|
2
2
|
// holds .c64-re-tools/, below the home folder, the way git finds .git. A skill
|
|
3
|
-
// script
|
|
4
|
-
//
|
|
5
|
-
// the
|
|
3
|
+
// script can name it with --project <folder>; a project path of a script is
|
|
4
|
+
// relative to that folder, or absolute inside it. An MCP tool takes absolute
|
|
5
|
+
// paths only, and the project of a file is the project at or above it
|
|
6
|
+
// (fileProject). .c64-re-tools/local/ holds what the agent saves for itself
|
|
7
|
+
// and ignores itself in git.
|
|
6
8
|
|
|
7
9
|
import { Refusal } from "./errors.ts";
|
|
8
10
|
import { accessSync, constants, existsSync, lstatSync, mkdirSync, readdirSync, realpathSync, statSync, writeFileSync } from "node:fs";
|
|
9
11
|
import { homedir } from "node:os";
|
|
10
|
-
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
12
|
+
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep, win32 } from "node:path";
|
|
11
13
|
|
|
12
14
|
/** The folder that marks a project and holds its knowledge. */
|
|
13
15
|
export const PROJECT_DIRECTORY = ".c64-re-tools";
|
|
@@ -36,11 +38,7 @@ const SEARCH_DEPTH = 3;
|
|
|
36
38
|
/** At most this many projects are named in a refusal. */
|
|
37
39
|
const LISTED_PROJECTS = 20;
|
|
38
40
|
|
|
39
|
-
/** How a refusal tells the caller to name a project
|
|
40
|
-
export interface ProjectRemedy {
|
|
41
|
-
remedy?: string;
|
|
42
|
-
}
|
|
43
|
-
|
|
41
|
+
/** How a refusal of a script tells the caller to name a project. */
|
|
44
42
|
const SCRIPT_REMEDY = "Give --project <folder>";
|
|
45
43
|
|
|
46
44
|
/** The folder that --project chose for this process, or undefined. */
|
|
@@ -61,14 +59,56 @@ function isProject(folder: string): boolean {
|
|
|
61
59
|
}
|
|
62
60
|
}
|
|
63
61
|
|
|
62
|
+
/**
|
|
63
|
+
* The home folder as the environment gives it and at its real location: the
|
|
64
|
+
* home folder can be a symbolic link, and the working directory of a process
|
|
65
|
+
* is a real location.
|
|
66
|
+
*/
|
|
67
|
+
function homeFolders(): string[] {
|
|
68
|
+
const home = homedir();
|
|
69
|
+
if (homes?.home !== home) {
|
|
70
|
+
let real = home;
|
|
71
|
+
try {
|
|
72
|
+
real = realLocation(home);
|
|
73
|
+
} catch {
|
|
74
|
+
// The home folder as the environment gives it.
|
|
75
|
+
}
|
|
76
|
+
homes = { home, folders: real === home ? [home] : [home, real] };
|
|
77
|
+
}
|
|
78
|
+
return homes.folders;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The spellings of the home folder, found once for each home folder that the environment gives. */
|
|
82
|
+
let homes: { home: string; folders: string[] } | undefined;
|
|
83
|
+
|
|
84
|
+
/** True when `folder` is the home folder, in either spelling. */
|
|
85
|
+
function isHome(folder: string): boolean {
|
|
86
|
+
return homeFolders().includes(folder);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* True for the root of a file system, the home folder and each folder above
|
|
91
|
+
* it: they hold the files of the whole machine or user, so they are never a
|
|
92
|
+
* project without .c64-re-tools/. By text and by real location.
|
|
93
|
+
*/
|
|
94
|
+
function isTop(folder: string): boolean {
|
|
95
|
+
let spellings = [folder];
|
|
96
|
+
try {
|
|
97
|
+
spellings = [folder, realLocation(folder)];
|
|
98
|
+
} catch {
|
|
99
|
+
// The text alone.
|
|
100
|
+
}
|
|
101
|
+
return spellings.some((each) => dirname(each) === each || homeFolders().some((home) => isInside(each, home)));
|
|
102
|
+
}
|
|
103
|
+
|
|
64
104
|
/** `start` and each folder above it, nearest first, below the home folder when `start` is under it. */
|
|
65
105
|
function foldersUpFrom(start: string): string[] {
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
106
|
+
const homes = homeFolders();
|
|
107
|
+
if (homes.includes(start)) return [];
|
|
108
|
+
const underHome = homes.some((home) => isInside(home, start));
|
|
69
109
|
const folders: string[] = [];
|
|
70
110
|
for (let folder = start; ; folder = dirname(folder)) {
|
|
71
|
-
if (underHome && folder
|
|
111
|
+
if (underHome && homes.includes(folder)) break;
|
|
72
112
|
folders.push(folder);
|
|
73
113
|
if (dirname(folder) === folder) break;
|
|
74
114
|
}
|
|
@@ -102,37 +142,119 @@ function listed(projects: readonly string[]): string {
|
|
|
102
142
|
}
|
|
103
143
|
|
|
104
144
|
/**
|
|
105
|
-
* The
|
|
106
|
-
* folder
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* project.
|
|
145
|
+
* The nearest entry at or above `path` that exists: `path` itself, or a
|
|
146
|
+
* folder above it. ENOENT and ENOTDIR (a file where a folder must be) mean that
|
|
147
|
+
* an entry does not exist. Any other error of the file system, for example no
|
|
148
|
+
* permission, is thrown for the caller to report.
|
|
110
149
|
*/
|
|
111
|
-
export function
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
150
|
+
export function nearestEntry(path: string): string {
|
|
151
|
+
for (let entry = path; ; entry = dirname(entry)) {
|
|
152
|
+
try {
|
|
153
|
+
lstatSync(entry);
|
|
154
|
+
return entry;
|
|
155
|
+
} catch (error) {
|
|
156
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
157
|
+
if ((code !== "ENOENT" && code !== "ENOTDIR") || dirname(entry) === entry) throw error;
|
|
158
|
+
}
|
|
116
159
|
}
|
|
117
|
-
|
|
118
|
-
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* What the project rules find from the folder `start`: the nearest folder at
|
|
164
|
+
* or above it with .c64-re-tools/, below the home folder. Without one, `start`
|
|
165
|
+
* itself, unless it is the root of a file system, the home folder or a folder
|
|
166
|
+
* above it (top, isTop), has projects below it, or is a folder of a group that is no
|
|
167
|
+
* project: then why it is in no project, so no write lands outside a project.
|
|
168
|
+
* `plain` marks `start` as a project without .c64-re-tools/.
|
|
169
|
+
*/
|
|
170
|
+
type Lookup = { root: string; plain?: true } | { top: true } | { below: string[] } | { group: ProjectGroup };
|
|
171
|
+
|
|
172
|
+
function lookUp(start: string): Lookup {
|
|
173
|
+
for (const folder of foldersUpFrom(start)) {
|
|
174
|
+
if (isProject(folder)) return { root: folder };
|
|
119
175
|
}
|
|
176
|
+
if (isTop(start)) return { top: true };
|
|
120
177
|
const below = projectsBelow(start);
|
|
121
|
-
if (below.length > 0) {
|
|
122
|
-
throw new Refusal("invalid-input", `${start} is not a project folder. These folders below it are projects: ${listed(below)}. ${remedy} with one of them.`);
|
|
123
|
-
}
|
|
178
|
+
if (below.length > 0) return { below };
|
|
124
179
|
// A folder of a group that is no project, for example its docs/: a write there would make it a project of the group.
|
|
125
|
-
const group =
|
|
180
|
+
const group = groupAbove(start);
|
|
181
|
+
return group === undefined ? { root: start, plain: true } : { group };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** The nearest group folder above `start`, or undefined. The search stops at the first one. */
|
|
185
|
+
function groupAbove(start: string): ProjectGroup | undefined {
|
|
186
|
+
for (const folder of foldersUpFrom(start).slice(1)) {
|
|
187
|
+
const group = groupAt(folder);
|
|
188
|
+
if (group !== undefined) return group;
|
|
189
|
+
}
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The projects of a group, relative to `start` and with /. */
|
|
194
|
+
function groupProjects(group: ProjectGroup, start: string): string {
|
|
195
|
+
return listed([...group.projects.values()].map((project) => relative(start, project).split(sep).join("/")));
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* The project of the working directory `cwd` by the project rules (lookUp).
|
|
200
|
+
* A folder in no project is a Refusal that names the projects and the remedy.
|
|
201
|
+
*/
|
|
202
|
+
export function findProject(cwd: string = process.cwd()): string {
|
|
203
|
+
const start = resolve(cwd);
|
|
204
|
+
const found = lookUp(start);
|
|
205
|
+
if ("root" in found) return found.root;
|
|
206
|
+
if ("top" in found) throw new Refusal("invalid-input", `${isHome(start) ? "The home folder" : start} is not a project folder. Start in a project folder, or give --project <folder>.`);
|
|
207
|
+
if ("below" in found) throw new Refusal("invalid-input", `${start} is not a project folder. These folders below it are projects: ${listed(found.below)}. ${SCRIPT_REMEDY} with one of them.`);
|
|
208
|
+
throw new Refusal("invalid-input", `${start} is in the group folder ${found.group.root}, and it is not a project folder. The projects of the group are ${groupProjects(found.group, start)}. ${SCRIPT_REMEDY} with one of them. To make this folder a new project of the group, give the folder itself: .`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The project of the absolute path `file` that an MCP tool reads or writes:
|
|
213
|
+
* the nearest folder at or above the file with .c64-re-tools/, below the home
|
|
214
|
+
* folder. The lookup starts at the folder of the file, or at the first folder
|
|
215
|
+
* above it that exists, because a new file can be in a new folder. Without one,
|
|
216
|
+
* the project is the folder where the MCP server started, where the agent
|
|
217
|
+
* started, when the project rules (lookUp) make that folder a project without
|
|
218
|
+
* .c64-re-tools/, and the file is in it. A file that a tool only reads can also
|
|
219
|
+
* be in a group folder outside its projects: then the project is the first
|
|
220
|
+
* project of the group, whose reads reach the group folder. Any other file is
|
|
221
|
+
* a Refusal with the remedy.
|
|
222
|
+
*/
|
|
223
|
+
export function fileProject(file: string, access: "read" | "write"): string {
|
|
224
|
+
let start: string;
|
|
225
|
+
try {
|
|
226
|
+
start = nearestEntry(dirname(resolve(file)));
|
|
227
|
+
// A file, or a link to nothing, where a folder must be: the folder above it.
|
|
228
|
+
if (statSync(start, { throwIfNoEntry: false })?.isDirectory() !== true) start = dirname(start);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
throw new Refusal("invalid-input", `${file} cannot be reached: the file system refuses a folder of its path (${(error as NodeJS.ErrnoException).code ?? "an error"}). Give a file in a project folder that you can read.`);
|
|
231
|
+
}
|
|
232
|
+
const marked = foldersUpFrom(start).find(isProject);
|
|
233
|
+
if (marked !== undefined) {
|
|
234
|
+
ensureLocal(marked);
|
|
235
|
+
return marked;
|
|
236
|
+
}
|
|
237
|
+
// Each call looks again: a project made below the start folder ends its place as a project.
|
|
238
|
+
const cwd = resolve(process.cwd());
|
|
239
|
+
const started = lookUp(cwd);
|
|
240
|
+
const plain = "root" in started && started.plain === true ? started.root : undefined;
|
|
241
|
+
if (plain !== undefined && insideReally(plain, file) !== undefined) return plain;
|
|
242
|
+
if (isTop(start)) throw new Refusal("invalid-input", `${file} is not in a project folder. Give a file in a folder that has .c64-re-tools/, or start the agent in a project folder.`);
|
|
243
|
+
const group = groupAt(start) ?? groupAbove(start);
|
|
126
244
|
if (group !== undefined) {
|
|
127
|
-
|
|
128
|
-
throw new Refusal("invalid-input", `${
|
|
245
|
+
if (access === "read") return [...group.projects.values()][0]!;
|
|
246
|
+
throw new Refusal("invalid-input", `${file} is in the group folder ${group.root}, and not in a project of the group. The projects of the group are ${groupProjects(group, start)}. Give a file in one of them.`);
|
|
129
247
|
}
|
|
130
|
-
|
|
248
|
+
// The projects below the folder name the remedy, but only in the start folder: a search elsewhere can read a large part of the disk.
|
|
249
|
+
const below = insideReally(cwd, start) === undefined ? [] : projectsBelow(start);
|
|
250
|
+
if (below.length > 0) throw new Refusal("invalid-input", `${file} is in ${start}, and it is not a project folder. These folders below it are projects: ${listed(below)}. Give a file in one of them.`);
|
|
251
|
+
const where = plain === undefined ? "" : `, or is the folder where the agent started, ${plain}, or a folder in it`;
|
|
252
|
+
throw new Refusal("invalid-input", `${file} is not in a project folder. A project folder has .c64-re-tools/${where}.`);
|
|
131
253
|
}
|
|
132
254
|
|
|
133
255
|
/** The refusal of a group folder as a project: a write there would make it a project, and then it is no group. */
|
|
134
|
-
function groupRefusal(path: string, group: ProjectGroup
|
|
135
|
-
return new Refusal("invalid-input", `${path} is a group folder, not a project folder. These folders in it are projects: ${listed([...group.projects.keys()])}. ${
|
|
256
|
+
function groupRefusal(path: string, group: ProjectGroup): Refusal {
|
|
257
|
+
return new Refusal("invalid-input", `${path} is a group folder, not a project folder. These folders in it are projects: ${listed([...group.projects.keys()])}. ${SCRIPT_REMEDY} with one of them.`);
|
|
136
258
|
}
|
|
137
259
|
|
|
138
260
|
/**
|
|
@@ -147,7 +269,8 @@ export function namedProject(folder: string, cwd: string = process.cwd()): strin
|
|
|
147
269
|
const stat = statSync(path, { throwIfNoEntry: false });
|
|
148
270
|
if (stat === undefined) throw new Refusal("not-found", `There is no folder ${folder}.`);
|
|
149
271
|
if (!stat.isDirectory()) throw new Refusal("invalid-input", `${folder} is not a folder.`);
|
|
150
|
-
if (path
|
|
272
|
+
if (isHome(path)) throw new Refusal("invalid-input", "The home folder is not a project folder.");
|
|
273
|
+
if (isTop(path) && !isProject(path)) throw new Refusal("invalid-input", `${folder} is the root of a file system or a folder above the home folder, and it is not a project folder. Give a project folder.`);
|
|
151
274
|
// A project inside another project is a project of its own, as the lookup from a working directory in it finds.
|
|
152
275
|
const outer = isProject(path) ? undefined : foldersUpFrom(path).slice(1).find(isProject);
|
|
153
276
|
if (outer !== undefined) {
|
|
@@ -179,7 +302,7 @@ function inRepository(folder: string): boolean {
|
|
|
179
302
|
*/
|
|
180
303
|
export function groupAt(folder: string): ProjectGroup | undefined {
|
|
181
304
|
const root = resolve(folder);
|
|
182
|
-
if (root
|
|
305
|
+
if (isHome(root) || dirname(root) === root) return undefined;
|
|
183
306
|
if (foldersUpFrom(root).some(isProject) || !inRepository(root)) return undefined;
|
|
184
307
|
let entries;
|
|
185
308
|
try {
|
|
@@ -215,7 +338,7 @@ export function newProjectFolder(folder: string, currentRoot: string): { root: s
|
|
|
215
338
|
const root = resolve(currentRoot, folder);
|
|
216
339
|
const zip = `${root}.zip`;
|
|
217
340
|
if (dirname(root) === root) throw new Refusal("invalid-input", `${folder} is the root of a file system. Give a folder for the new project.`);
|
|
218
|
-
if (root
|
|
341
|
+
if (isHome(root)) throw new Refusal("invalid-input", "The home folder cannot be the folder of a new project.");
|
|
219
342
|
if (isInside(currentRoot, root)) throw new Refusal("invalid-input", `${folder} is inside the project ${currentRoot}. Give a folder outside it, for example ../${basename(root)}.`);
|
|
220
343
|
if (isInside(root, currentRoot)) throw new Refusal("invalid-input", `${folder} holds the project ${currentRoot}. Give a folder outside it.`);
|
|
221
344
|
const outer = foldersUpFrom(root).slice(1).find(isProject);
|
|
@@ -245,15 +368,6 @@ export function ensureLocal(root: string): void {
|
|
|
245
368
|
}
|
|
246
369
|
}
|
|
247
370
|
|
|
248
|
-
/** The project of an MCP call or another caller without process state: `folder` when given, else the project lookup from `cwd`. */
|
|
249
|
-
export function resolveProject(folder: string | undefined, cwd: string = process.cwd(), options: ProjectRemedy = {}): string {
|
|
250
|
-
const root = folder === undefined ? findProject(cwd, options) : namedProject(folder, cwd);
|
|
251
|
-
const group = folder === undefined ? undefined : groupAt(root);
|
|
252
|
-
if (group !== undefined) throw groupRefusal(root, group, options.remedy ?? SCRIPT_REMEDY);
|
|
253
|
-
ensureLocal(root);
|
|
254
|
-
return root;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
371
|
/** Sets the project of this process from --project. Refuses a folder that the rules refuse. */
|
|
258
372
|
export function useProject(folder: string): void {
|
|
259
373
|
chosen = namedProject(folder);
|
|
@@ -267,7 +381,7 @@ export function namedGroup(): ProjectGroup | undefined {
|
|
|
267
381
|
|
|
268
382
|
/** The project folder of this process: the --project choice, else the project lookup from the working directory. A group folder is refused. */
|
|
269
383
|
export function projectRoot(): string {
|
|
270
|
-
if (chosenGroup !== undefined) throw groupRefusal(chosenGroup.root, chosenGroup
|
|
384
|
+
if (chosenGroup !== undefined) throw groupRefusal(chosenGroup.root, chosenGroup);
|
|
271
385
|
let root = chosen;
|
|
272
386
|
if (root === undefined) {
|
|
273
387
|
const cwd = process.cwd();
|
|
@@ -279,17 +393,50 @@ export function projectRoot(): string {
|
|
|
279
393
|
return root;
|
|
280
394
|
}
|
|
281
395
|
|
|
396
|
+
/** The real location of `path`: the real location of its longest part that exists, and the rest as it is. Throws an error of the file system as nearestEntry does. */
|
|
397
|
+
export function realLocation(path: string): string {
|
|
398
|
+
const existing = nearestEntry(path);
|
|
399
|
+
return join(realpathSync(existing), relative(existing, path));
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* The absolute path `candidate` in the spelling of the folder `root`, when it
|
|
404
|
+
* is inside `root`: by its text, or by its real location. An absolute path can
|
|
405
|
+
* reach a folder through a symbolic link, for example /tmp to /private/tmp on
|
|
406
|
+
* macOS, or a linked work folder. The last part of `candidate` stays as it
|
|
407
|
+
* is: the caller checks a link there. Undefined when it is outside.
|
|
408
|
+
*/
|
|
409
|
+
function insideReally(root: string, candidate: string): string | undefined {
|
|
410
|
+
if (isInside(root, candidate)) return candidate;
|
|
411
|
+
let realRoot: string;
|
|
412
|
+
let real: string;
|
|
413
|
+
try {
|
|
414
|
+
realRoot = realLocation(root);
|
|
415
|
+
real = join(realLocation(dirname(candidate)), basename(candidate));
|
|
416
|
+
} catch {
|
|
417
|
+
// A path that the file system refuses (no permission, a link to nothing) is not inside.
|
|
418
|
+
return undefined;
|
|
419
|
+
}
|
|
420
|
+
return isInside(realRoot, real) ? join(root, relative(realRoot, real)) : undefined;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** The remedy of a project path that leads out of its folder: the two forms that a script takes. */
|
|
424
|
+
const INSIDE_REMEDY = "Give a path inside it, relative to the project folder or absolute.";
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* The absolute path of a project path: relative to the project folder `root`,
|
|
428
|
+
* or absolute. It must stay inside the project folder. An absolute path that
|
|
429
|
+
* reaches the project folder through a symbolic link comes back in the
|
|
430
|
+
* spelling of `root`.
|
|
431
|
+
*/
|
|
282
432
|
export function resolveProjectPath(path: string, root = projectRoot()): string {
|
|
283
433
|
if (path.length === 0) {
|
|
284
434
|
throw new Refusal("invalid-input", "A project path must not be empty.");
|
|
285
435
|
}
|
|
286
|
-
if (isAbsolute(path)) {
|
|
287
|
-
throw new Refusal("invalid-input", `${path} is not relative: give a path relative to the project folder.`);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
436
|
const candidate = resolve(root, path);
|
|
291
|
-
|
|
292
|
-
|
|
437
|
+
const inside = isAbsolute(path) ? insideReally(root, candidate) : isInside(root, candidate) ? candidate : undefined;
|
|
438
|
+
if (inside !== undefined) return inside;
|
|
439
|
+
throw new Refusal("invalid-input", `${path} leads out of the project folder ${root}. ${INSIDE_REMEDY}`);
|
|
293
440
|
}
|
|
294
441
|
|
|
295
442
|
/** The folder of the committed knowledge text in PROJECT_DIRECTORY. */
|
|
@@ -353,7 +500,7 @@ export function writeProblem(error: unknown, path: string, what?: string): strin
|
|
|
353
500
|
export function checkOutputPath(path: string, kind: OutputKind, options: { root?: string; what?: string } = {}): string {
|
|
354
501
|
const root = options.root ?? projectRoot();
|
|
355
502
|
const what = options.what;
|
|
356
|
-
const outside = () => new Refusal("invalid-input", `${what
|
|
503
|
+
const outside = () => new Refusal("invalid-input", `${what === undefined ? "" : `${what}: `}${path} leads out of the project folder ${root}. Give a path that stays inside it.`);
|
|
357
504
|
const problem = (error: unknown) => {
|
|
358
505
|
const message = writeProblem(error, path, what);
|
|
359
506
|
return message === undefined ? error : new Refusal("invalid-input", message);
|
|
@@ -376,16 +523,11 @@ export function checkOutputPath(path: string, kind: OutputKind, options: { root?
|
|
|
376
523
|
}
|
|
377
524
|
inKnowledge(relative(root, target));
|
|
378
525
|
// The nearest entry that exists: the path itself, or a folder above it.
|
|
379
|
-
let existing
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
} catch (error) {
|
|
385
|
-
throw problem(error);
|
|
386
|
-
}
|
|
387
|
-
if (found !== undefined) break;
|
|
388
|
-
existing = dirname(existing);
|
|
526
|
+
let existing: string;
|
|
527
|
+
try {
|
|
528
|
+
existing = nearestEntry(target);
|
|
529
|
+
} catch (error) {
|
|
530
|
+
throw problem(error);
|
|
389
531
|
}
|
|
390
532
|
const realRoot = realpathSync(root);
|
|
391
533
|
if (existing === target && kind === "file" && lstatSync(target).isSymbolicLink()) throw outside();
|
|
@@ -408,6 +550,16 @@ export function checkOutputPath(path: string, kind: OutputKind, options: { root?
|
|
|
408
550
|
return target;
|
|
409
551
|
}
|
|
410
552
|
|
|
553
|
+
/**
|
|
554
|
+
* Refuses with `fail` an absolute path in a file that the project keeps, for
|
|
555
|
+
* example the program of a run file: such a file must work in each copy of
|
|
556
|
+
* the project. A path that is absolute on any system counts, so a file from
|
|
557
|
+
* Windows is refused on Linux too. `field` names the path in the refusal.
|
|
558
|
+
*/
|
|
559
|
+
export function refuseAbsoluteInFile(value: unknown, field: string, fail: (message: string) => never): void {
|
|
560
|
+
if (typeof value === "string" && win32.isAbsolute(value)) fail(`${field}: give the path relative to the project folder, so the file works in each copy of the project, not ${value}`);
|
|
561
|
+
}
|
|
562
|
+
|
|
411
563
|
/** A project path in one spelling: relative to the project folder, with / and without ./, .. or a double /. Refuses a path as resolveProjectPath does. */
|
|
412
564
|
export function projectRelative(path: string, root = projectRoot()): string {
|
|
413
565
|
return relative(root, resolveProjectPath(path, root)).split(sep).join("/");
|
|
@@ -443,17 +595,18 @@ export function readGroup(root: string): ProjectGroup | undefined {
|
|
|
443
595
|
}
|
|
444
596
|
|
|
445
597
|
/**
|
|
446
|
-
* Resolves a path that a command only reads, relative to the project folder
|
|
447
|
-
* as resolveProjectPath does. In a group the path can also lead
|
|
448
|
-
* folder, so a version reads the files of the other versions,
|
|
449
|
-
* ../v1/releases/game.prg. A write stays in the project.
|
|
598
|
+
* Resolves a path that a command only reads, relative to the project folder
|
|
599
|
+
* or absolute, as resolveProjectPath does. In a group the path can also lead
|
|
600
|
+
* into the group folder, so a version reads the files of the other versions,
|
|
601
|
+
* for example ../v1/releases/game.prg. A write stays in the project.
|
|
450
602
|
*/
|
|
451
603
|
export function resolveReadPath(path: string, root = projectRoot()): string {
|
|
452
604
|
const group = readGroup(root);
|
|
453
|
-
if (group === undefined || path.length === 0
|
|
605
|
+
if (group === undefined || path.length === 0) return resolveProjectPath(path, root);
|
|
454
606
|
const candidate = resolve(root, path);
|
|
455
|
-
|
|
456
|
-
|
|
607
|
+
const inside = isAbsolute(path) ? insideReally(group.root, candidate) : isInside(group.root, candidate) ? candidate : undefined;
|
|
608
|
+
if (inside !== undefined) return inside;
|
|
609
|
+
throw new Refusal("invalid-input", `${path} leads out of the group folder ${group.root} of the project. ${INSIDE_REMEDY}`);
|
|
457
610
|
}
|
|
458
611
|
|
|
459
612
|
/** resolveReadPath to an existing entry whose real location is inside the project, or in a group inside the group folder, as resolveProjectFile does. */
|
package/src/protocol/vice.ts
CHANGED
|
@@ -76,7 +76,8 @@ export const CONDITION_REGISTERS = ["a", "x", "y", "sp"] as const;
|
|
|
76
76
|
|
|
77
77
|
export type Condition =
|
|
78
78
|
| { kind: "register"; register: (typeof CONDITION_REGISTERS)[number]; operator: Comparison; value: number }
|
|
79
|
-
|
|
79
|
+
/** `mask`: the bits of the byte that the comparison takes; all of them when it is not given. */
|
|
80
|
+
| { kind: "memory"; address: number; operator: Comparison; value: number; space: Space; view: MemoryView; mask?: number }
|
|
80
81
|
| { kind: "raster"; line: number; cycle?: number };
|
|
81
82
|
|
|
82
83
|
/** Raster lines and cycles per line of each video standard. */
|
|
@@ -995,12 +996,13 @@ function validateCondition(value: unknown): Condition {
|
|
|
995
996
|
return { kind: "register", register: value.register, operator: value.operator, value: byte(value.value, "value") };
|
|
996
997
|
}
|
|
997
998
|
case "memory": {
|
|
998
|
-
onlyFields(value, ["kind", "address", "operator", "value", "space", "view"], "the condition");
|
|
999
|
+
onlyFields(value, ["kind", "address", "operator", "value", "space", "view", "mask"], "the condition");
|
|
999
1000
|
const address = validAddress(value.address, "condition address");
|
|
1000
1001
|
if (!isOneOf(COMPARISONS, value.operator)) invalid(`condition operator must be one of ${COMPARISONS.join(", ")}`);
|
|
1001
1002
|
const space = validSpace(value.space, "condition space");
|
|
1002
1003
|
const view = validView(value.view, "condition view");
|
|
1003
|
-
|
|
1004
|
+
const condition: Condition = { kind: "memory", address, operator: value.operator, value: byte(value.value, "value"), space, view };
|
|
1005
|
+
return value.mask === undefined ? condition : { ...condition, mask: byte(value.mask, "mask") };
|
|
1004
1006
|
}
|
|
1005
1007
|
case "raster": {
|
|
1006
1008
|
onlyFields(value, ["kind", "line", "cycle"], "the condition");
|
package/src/script.ts
CHANGED
|
@@ -24,7 +24,7 @@ import { parseArgs } from "node:util";
|
|
|
24
24
|
import { c64AddressOf, formatC64Address, isAddressText, SPACE_PATTERN, SYMBOL_NAME, type AddressRange } from "./c64.ts";
|
|
25
25
|
import { describeError, isRefusal, Refusal, splitMessage, type ErrorCode } from "./errors.ts";
|
|
26
26
|
import { checkOutputPath, projectRoot, resolveProjectPath, resolveReadPath, useProject, type OutputKind } from "./project.ts";
|
|
27
|
-
import {
|
|
27
|
+
import { STOP_AND_ASK } from "./protocol/messages.ts";
|
|
28
28
|
import { elapsedMs, startTrace, trace, type TraceFields } from "./trace.ts";
|
|
29
29
|
|
|
30
30
|
/** A wrong command line: an invalid-input refusal that also shows the usage of the command. */
|
|
@@ -85,7 +85,7 @@ function fault(message: string): Refusal {
|
|
|
85
85
|
export type OptionKind =
|
|
86
86
|
| "flag" // no value: true when given
|
|
87
87
|
| "text" // any text
|
|
88
|
-
| "path" // a path relative to the project folder
|
|
88
|
+
| "path" // a path relative to the project folder, or absolute inside it
|
|
89
89
|
| "count" // a whole number in decimal
|
|
90
90
|
| "number" // a value: $ and hex digits, decimal, or % and binary digits
|
|
91
91
|
| "address" // $ and 1 to 4 hex digits, or a name
|
|
@@ -339,7 +339,7 @@ export function listed<T, U = T>(items: readonly T[], max: number, show: (item:
|
|
|
339
339
|
|
|
340
340
|
/** The conventions in the help of every script. */
|
|
341
341
|
export const CONVENTIONS = `Addresses are $ and 1 to 4 hex digits, for example '$c000' or '$fb', or a name: a symbol, else an analyzer name that only one analyzer gives. A range is <start>-<end>, for example '$c000-$cfff'. Quote them in a shell.
|
|
342
|
-
|
|
342
|
+
A path is relative to the project folder, or an absolute path inside it. Every script takes --project <folder>: the project folder, relative to the working directory. Without it, the script uses the nearest folder at or above the working directory that holds .c64-re-tools/. In a group folder, give --project with a project of the group. A path that a command only reads can go into another project of the group, for example ../v1/releases/game.prg.
|
|
343
343
|
Each command prints one JSON envelope: {ok: true, command, body, hint?, nextAction?} with exit status 0, or {ok: false, command?, code, error, hint?, nextAction?} with exit status 2.
|
|
344
344
|
Every script takes --fields <path>,<path>: the body then has only those dotted paths, for example --fields revision,regions.listed.start. A path through a list gives that field of each item.`;
|
|
345
345
|
|
|
@@ -458,41 +458,87 @@ export function takeFields(argv: readonly string[]): { fields?: string[]; argv:
|
|
|
458
458
|
return { fields, argv: rest };
|
|
459
459
|
}
|
|
460
460
|
|
|
461
|
+
/** A path of --fields that no part of the body has, and the deepest part of the path that the body has. */
|
|
462
|
+
export interface MissingField {
|
|
463
|
+
path: string;
|
|
464
|
+
/** That part as a dotted path: "" is the body. */
|
|
465
|
+
at: string;
|
|
466
|
+
/** True when that part is a list: then `fields` are the fields of its items. */
|
|
467
|
+
list: boolean;
|
|
468
|
+
/** The fields of that part. */
|
|
469
|
+
fields: string[];
|
|
470
|
+
}
|
|
471
|
+
|
|
461
472
|
/**
|
|
462
473
|
* The parts of a body at the dotted paths, with their nesting. A path through
|
|
463
474
|
* a list takes the rest of the path from each item, and two paths through one
|
|
464
|
-
* list give one list. `missing` holds the paths that no part of the body has
|
|
475
|
+
* list give one list. `missing` holds the paths that no part of the body has,
|
|
476
|
+
* each with the fields of the deepest part of the path that the body has.
|
|
465
477
|
*/
|
|
466
|
-
export function pickFields(body: unknown, paths: readonly string[]): { body: unknown; missing:
|
|
478
|
+
export function pickFields(body: unknown, paths: readonly string[]): { body: unknown; missing: MissingField[] } {
|
|
467
479
|
const found = new Set<string>();
|
|
480
|
+
const reached = new Map<string, { at: string[]; list: boolean; fields: Set<string> }>();
|
|
468
481
|
interface Wanted {
|
|
469
482
|
path: string;
|
|
470
483
|
rest: readonly string[];
|
|
471
484
|
}
|
|
472
|
-
|
|
485
|
+
// The path stops at `at`: keep the deepest place, and the fields of each item of a list there.
|
|
486
|
+
const reach = (wanted: Wanted, at: string[], list: boolean, fields: readonly string[]) => {
|
|
487
|
+
const known = reached.get(wanted.path);
|
|
488
|
+
if (known === undefined || at.length > known.at.length) reached.set(wanted.path, { at, list, fields: new Set(fields) });
|
|
489
|
+
else if (at.length === known.at.length) for (const field of fields) known.fields.add(field);
|
|
490
|
+
};
|
|
491
|
+
const pick = (node: unknown, wanted: readonly Wanted[], at: string[], list: boolean): unknown => {
|
|
473
492
|
if (Array.isArray(node)) {
|
|
474
493
|
// An empty list has no item that lacks the field.
|
|
475
494
|
if (node.length === 0) for (const item of wanted) found.add(item.path);
|
|
476
|
-
return node.map((item) => pick(item, wanted));
|
|
495
|
+
return node.map((item) => pick(item, wanted, at, true));
|
|
496
|
+
}
|
|
497
|
+
if (typeof node !== "object" || node === null) {
|
|
498
|
+
for (const item of wanted) reach(item, at, list, []);
|
|
499
|
+
return undefined;
|
|
477
500
|
}
|
|
478
|
-
if (typeof node !== "object" || node === null) return undefined;
|
|
479
501
|
const out: Record<string, unknown> = {};
|
|
480
502
|
const groups = new Map<string, Wanted[]>();
|
|
481
503
|
for (const item of wanted) groups.set(item.rest[0]!, [...(groups.get(item.rest[0]!) ?? []), item]);
|
|
482
504
|
for (const [key, group] of groups) {
|
|
483
|
-
if (!(key in node))
|
|
505
|
+
if (!(key in node)) {
|
|
506
|
+
for (const item of group) reach(item, at, list, Object.keys(node));
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
484
509
|
const value = (node as Record<string, unknown>)[key];
|
|
485
510
|
const ends = group.filter((item) => item.rest.length === 1);
|
|
486
511
|
for (const item of ends) found.add(item.path);
|
|
487
512
|
const deeper = group.filter((item) => item.rest.length > 1).map((item) => ({ path: item.path, rest: item.rest.slice(1) }));
|
|
488
|
-
const part = deeper.length > 0 ? pick(value, deeper) : undefined;
|
|
513
|
+
const part = deeper.length > 0 ? pick(value, deeper, [...at, key], false) : undefined;
|
|
489
514
|
if (ends.length > 0) out[key] = value;
|
|
490
515
|
else if (part !== undefined) out[key] = part;
|
|
491
516
|
}
|
|
492
517
|
return out;
|
|
493
518
|
};
|
|
494
|
-
const picked = pick(body, paths.map((path) => ({ path, rest: path.split(".") })));
|
|
495
|
-
|
|
519
|
+
const picked = pick(body, paths.map((path) => ({ path, rest: path.split(".") })), [], false);
|
|
520
|
+
const missing = paths
|
|
521
|
+
.filter((path) => !found.has(path))
|
|
522
|
+
.map((path) => {
|
|
523
|
+
const place = reached.get(path)!;
|
|
524
|
+
return { path, at: place.at.join("."), list: place.list, fields: [...place.fields] };
|
|
525
|
+
});
|
|
526
|
+
return { body: picked ?? {}, missing };
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/** The hint of the paths of --fields that the body lacks: the paths, then the fields of each place where a path stops. */
|
|
530
|
+
function missingHint(missing: readonly MissingField[]): string {
|
|
531
|
+
const places = new Map<string, MissingField>();
|
|
532
|
+
for (const item of missing) if (!places.has(item.at)) places.set(item.at, item);
|
|
533
|
+
const fields = (item: MissingField) => {
|
|
534
|
+
if (item.at === "") return item.fields.length === 0 ? "It has no fields." : `Its fields are ${wordList(item.fields)}.`;
|
|
535
|
+
const what = item.list ? `The items of ${item.at}` : item.at;
|
|
536
|
+
const verb = item.list ? "have" : "has";
|
|
537
|
+
return item.fields.length === 0 ? `${what} ${verb} no fields.` : `${what} ${verb} the fields ${wordList(item.fields)}.`;
|
|
538
|
+
};
|
|
539
|
+
// The body comes first, so "Its fields" follows the sentence about the body. The other places keep the order of the paths.
|
|
540
|
+
const sorted = [...places.values()].sort((a, b) => Number(b.at === "") - Number(a.at === ""));
|
|
541
|
+
return [`The body has no ${wordList(missing.map((item) => item.path))}.`, ...sorted.map(fields)].join(" ");
|
|
496
542
|
}
|
|
497
543
|
|
|
498
544
|
/** True when --help comes before "--". */
|
|
@@ -502,7 +548,7 @@ function asksHelp(argv: readonly string[]): boolean {
|
|
|
502
548
|
}
|
|
503
549
|
|
|
504
550
|
/**
|
|
505
|
-
* Refuses a path
|
|
551
|
+
* Refuses a path, relative to the project folder or absolute, that leads out
|
|
506
552
|
* of it; a path that the command only reads can also lead into the group folder
|
|
507
553
|
* of the project. A refusal of the project lookup itself passes on.
|
|
508
554
|
*/
|
|
@@ -785,8 +831,7 @@ async function envelope(script: Envelope): Promise<void> {
|
|
|
785
831
|
const picked = pickFields(shownBody, fields);
|
|
786
832
|
shownBody = picked.body;
|
|
787
833
|
if (picked.missing.length > 0) {
|
|
788
|
-
const
|
|
789
|
-
const lacks = `The body has no ${wordList(picked.missing)}. ${top.length === 0 ? "It has no fields." : `Its fields are ${wordList(top)}.`}`;
|
|
834
|
+
const lacks = missingHint(picked.missing);
|
|
790
835
|
shownHint = shownHint === undefined ? lacks : `${sentence(shownHint)} ${lacks}`;
|
|
791
836
|
}
|
|
792
837
|
}
|