@henols/c64-re-tools 2.1.2 → 2.1.4
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 +2 -2
- package/skills/c64-disk/scripts/lib/containers.ts +2 -1
- package/skills/c64-docs/SKILL.md +1 -1
- package/skills/c64-docs/scripts/docs.ts +5 -3
- package/skills/c64-emulator/SKILL.md +4 -0
- package/skills/c64-knowledge/SKILL.md +7 -2
- package/skills/c64-knowledge/scripts/knowledge.ts +6 -12
- package/skills/c64-knowledge/scripts/lib/listing.ts +8 -1
- package/skills/c64-memory-map/SKILL.md +3 -1
- package/skills/c64-memory-map/scripts/memory-map.ts +26 -5
- package/skills/c64-provenance/SKILL.md +5 -4
- package/skills/c64-provenance/scripts/lib/compare.ts +12 -0
- package/skills/c64-provenance/scripts/lib/library.ts +33 -5
- package/skills/c64-provenance/scripts/lib/separate.ts +34 -6
- package/skills/c64-provenance/scripts/provenance.ts +26 -11
- package/skills/c64-source/SKILL.md +3 -2
- package/skills/c64-source/scripts/lib/source.ts +18 -4
- package/skills/c64-source/scripts/source.ts +40 -28
- package/skills/c64-static-analysis/SKILL.md +4 -3
- package/skills/c64-static-analysis/scripts/lib/findings.ts +47 -10
- package/skills/c64-static-analysis/scripts/lib/pointers.ts +38 -25
- package/skills/c64-static-analysis/scripts/static-analysis.ts +17 -14
- package/skills/c64-testing/SKILL.md +6 -3
- package/skills/c64-testing/references/scenario-format.md +7 -3
- package/skills/c64-testing/scripts/lib/diff-files.ts +27 -8
- package/skills/c64-testing/scripts/lib/runs.ts +24 -8
- package/skills/c64-testing/scripts/lib/scenario.ts +133 -83
- package/skills/c64-testing/scripts/testing.ts +6 -3
- package/skills/c64-unpacker/SKILL.md +5 -4
- package/skills/c64-unpacker/scripts/lib/loads.ts +9 -3
- package/skills/c64-unpacker/scripts/lib/machine.ts +31 -0
- package/skills/c64-unpacker/scripts/unpacker.ts +4 -1
- package/src/host/vice/session.ts +118 -11
- package/src/host-client/image.ts +22 -8
- package/src/host-client/steps.ts +2 -2
- package/src/host-client/tools.ts +4 -3
- package/src/host-client/transfer.ts +8 -3
- package/src/host-client/vice-session.ts +3 -3
- package/src/knowledge/write.ts +110 -29
- package/src/mcp/output.ts +1 -1
- package/src/mcp/server.ts +7 -0
- package/src/mcp/tools/debug.ts +2 -1
- package/src/mcp/tools/media.ts +8 -5
- package/src/media.ts +18 -0
- package/src/opcodes.ts +31 -1
- package/src/platform.ts +14 -1
- package/src/project.ts +14 -3
- package/src/script.ts +71 -2
package/src/knowledge/write.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { type AddressRange, formatC64Address, instructionLength, SPACE_PATTERN,
|
|
|
8
8
|
import { isoCet } from "../time.ts";
|
|
9
9
|
import { CONFLICT_RULES, conflicts, conflictTable, type Conflict, type ConflictRow } from "./conflicts.ts";
|
|
10
10
|
import { transaction } from "./database.ts";
|
|
11
|
-
import { labelProblem } from "../opcodes.ts";
|
|
11
|
+
import { labelProblem, MNEMONICS } from "../opcodes.ts";
|
|
12
12
|
import { checkExpression, evaluate, identifiers, operandText, replaceIdentifiers } from "./expression.ts";
|
|
13
13
|
import {
|
|
14
14
|
COMMENT_COLUMNS,
|
|
@@ -62,7 +62,7 @@ import {
|
|
|
62
62
|
type SuppressionRow,
|
|
63
63
|
type SymbolRow,
|
|
64
64
|
} from "./read.ts";
|
|
65
|
-
import { CODE_REGION_TYPES, COMMENT_PLACEMENTS, EVIDENCE_KINDS, MAX_ELEMENT, MODULE_FILE, DEFAULT_SPACE, GENERATED_FILES, isGeneratedFile, MACRO_NAME, MACRO_PARAM, SWITCH_NAME, NO_ELEMENT_TYPES, OPERAND_SELECTS, POINTER_KIND, REFERENCE_KINDS, REGION_TYPES, SYMBOL_KINDS, type CommentPlacement, type EvidenceKind, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type SymbolKind } from "./schema.ts";
|
|
65
|
+
import { CODE_REGION_TYPES, COMMENT_PLACEMENTS, EVIDENCE_KINDS, MAX_ELEMENT, MODULE_FILE, DEFAULT_SPACE, GENERATED_FILES, isGeneratedFile, isSemantic, MACRO_NAME, MACRO_PARAM, SWITCH_NAME, NO_ELEMENT_TYPES, OPERAND_SELECTS, POINTER_KIND, REFERENCE_KINDS, REGION_TYPES, SYMBOL_KINDS, type CommentPlacement, type EvidenceKind, type OperandSelect, type Origin, type ReferenceKind, type RegionType, type Space, type SymbolKind } from "./schema.ts";
|
|
66
66
|
import { newRevisionId, rowId, sqlName } from "./text.ts";
|
|
67
67
|
|
|
68
68
|
/** What a change rests on: a coverage run, a test or another project file with its SHA-256, or a note. */
|
|
@@ -353,6 +353,27 @@ const result = <T, P>(written: { revision: string | null; current: Step<T, P> })
|
|
|
353
353
|
// Each write has a check, which refuses bad input before anything opens, and a
|
|
354
354
|
// step, which runs inside a change. A single write and a batch share both.
|
|
355
355
|
|
|
356
|
+
/**
|
|
357
|
+
* True when a write of `origin` over a current row with the same content
|
|
358
|
+
* records a decision, so it replaces the row: over a row of an analyzer (the
|
|
359
|
+
* next run of the analyzer can retire its own rows, never a decision), and the
|
|
360
|
+
* user over the LLM. The LLM over the user changes nothing.
|
|
361
|
+
*/
|
|
362
|
+
function confirms(existing: Origin, origin: Origin): boolean {
|
|
363
|
+
return existing !== origin && (!isSemantic(existing) || origin === "user");
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* The bytes of the program of a space in the knowledge, by address: undefined
|
|
368
|
+
* outside it. A module and a comment are checked against them.
|
|
369
|
+
*/
|
|
370
|
+
function programBytes(db: DatabaseSync, space: Space): (address: number) => number | undefined {
|
|
371
|
+
const rows = db.prepare("SELECT address, bytes FROM current_memory WHERE space = ?").all(space) as Array<{ address: number; bytes: string }>;
|
|
372
|
+
const memory = new Map<number, number>();
|
|
373
|
+
for (const row of rows) Buffer.from(row.bytes, "hex").forEach((byte, offset) => memory.set(row.address + offset, byte));
|
|
374
|
+
return (address) => memory.get(address);
|
|
375
|
+
}
|
|
376
|
+
|
|
356
377
|
/** Every request names its space; without one it is the C64. */
|
|
357
378
|
type InSpace = { space?: Space };
|
|
358
379
|
type SymbolRequest = InSpace & { address: number; name: string; kind?: SymbolKind };
|
|
@@ -381,7 +402,7 @@ function renameStep(db: DatabaseSync, change: Change, origin: Origin, request: S
|
|
|
381
402
|
const space = spaceOf(request);
|
|
382
403
|
const existing = symbolAt(db, request.address, space);
|
|
383
404
|
const kind = request.kind ?? existing?.kind ?? "label";
|
|
384
|
-
if (existing !== undefined && existing.name === request.name && existing.kind === kind) return { current: existing };
|
|
405
|
+
if (existing !== undefined && existing.name === request.name && existing.kind === kind && !confirms(existing.origin, origin)) return { current: existing };
|
|
385
406
|
const owner = symbolNamed(db, request.name, space);
|
|
386
407
|
if (owner !== undefined && owner.address !== request.address) {
|
|
387
408
|
throw new Refusal("conflict", `The name ${request.name} already belongs to ${formatC64Address(owner.address)}. Rename or remove that symbol first.`);
|
|
@@ -459,7 +480,7 @@ function classifyStep(db: DatabaseSync, change: Change, origin: Origin, request:
|
|
|
459
480
|
const overlapping = regionsOverlapping(db, request.start, request.end, space);
|
|
460
481
|
const [only] = overlapping;
|
|
461
482
|
const same = overlapping.length === 1 && only!.start === request.start && only!.end === request.end && only!.type === request.type && only!.element === request.element;
|
|
462
|
-
if (same) return { current: overlapping };
|
|
483
|
+
if (same && !confirms(only!.origin, origin)) return { current: overlapping };
|
|
463
484
|
clearRange(db, change, request.start, request.end, space);
|
|
464
485
|
change.insertRegion({ space, start: request.start, end: request.end, type: request.type, ...(request.element === undefined ? {} : { element: request.element }), origin });
|
|
465
486
|
liftRegionSuppressions(db, change, request.start, request.end, space);
|
|
@@ -490,14 +511,17 @@ function checkComment(request: CommentRequest): string {
|
|
|
490
511
|
return text;
|
|
491
512
|
}
|
|
492
513
|
|
|
493
|
-
function commentStep(db: DatabaseSync, change: Change, origin: Origin, request: CommentRequest): Step<CommentRow> {
|
|
514
|
+
function commentStep(db: DatabaseSync, change: Change, origin: Origin, request: CommentRequest): Step<CommentRow, CommentRow> {
|
|
494
515
|
const space = spaceOf(request);
|
|
495
516
|
const text = request.text.trim();
|
|
496
517
|
const existing = commentsAt(db, request.address, space).find((comment) => comment.placement === request.placement);
|
|
497
|
-
if (existing?.text === text) return { current: existing };
|
|
518
|
+
if (existing?.text === text && !confirms(existing.origin, origin)) return { current: existing };
|
|
519
|
+
// A comment belongs to an instruction: the listing and the source show it at the start of the instruction only.
|
|
520
|
+
checkInstructionStart(db, space, programBytes(db, space), request.address, "comment");
|
|
498
521
|
if (existing !== undefined) change.close("comments", "space = ? AND address = ? AND placement = ?", space, request.address, request.placement);
|
|
499
522
|
change.insertComment({ space, address: request.address, placement: request.placement, text, origin });
|
|
500
|
-
|
|
523
|
+
const current = commentsAt(db, request.address, space).find((comment) => comment.placement === request.placement)!;
|
|
524
|
+
return existing !== undefined && existing.origin !== origin ? { current, previous: existing } : { current };
|
|
501
525
|
}
|
|
502
526
|
|
|
503
527
|
function checkPlacement(request: PlacementRequest): void {
|
|
@@ -523,14 +547,16 @@ function checkReference(request: ReferenceRequest): void {
|
|
|
523
547
|
if (request.kind === POINTER_KIND) throw new Refusal("invalid-input", "Only an operand expression writes a pointer reference: use operand and unoperand.");
|
|
524
548
|
}
|
|
525
549
|
|
|
526
|
-
function referenceStep(db: DatabaseSync, change: Change, origin: Origin, request: ReferenceRequest): Step<ReferenceRow> {
|
|
550
|
+
function referenceStep(db: DatabaseSync, change: Change, origin: Origin, request: ReferenceRequest): Step<ReferenceRow, ReferenceRow> {
|
|
527
551
|
const space = spaceOf(request);
|
|
528
552
|
const find = () => referencesFrom(db, request.from, space).find((reference) => reference.to === request.to && reference.kind === request.kind);
|
|
529
553
|
const existing = find();
|
|
530
|
-
if (existing !== undefined) return { current: existing };
|
|
554
|
+
if (existing !== undefined && !confirms(existing.origin, origin)) return { current: existing };
|
|
555
|
+
// A confirmation of a reference of an analyzer makes it a reference of the writer: the next run of the analyzer keeps it.
|
|
556
|
+
if (existing !== undefined) change.close('"references"', "space = ? AND from_address = ? AND to_address = ? AND kind = ?", space, request.from, request.to, request.kind);
|
|
531
557
|
change.insertReference({ space, from: request.from, to: request.to, kind: request.kind, origin });
|
|
532
558
|
change.close("suppressions", "space = ? AND entity = 'reference' AND start_address = ? AND to_address = ? AND kind = ?", space, request.from, request.to, request.kind);
|
|
533
|
-
return { current: find()! };
|
|
559
|
+
return existing !== undefined ? { current: find()!, previous: existing } : { current: find()! };
|
|
534
560
|
}
|
|
535
561
|
|
|
536
562
|
function unreferenceStep(db: DatabaseSync, change: Change, origin: Origin, request: ReferenceRequest): Step<null> {
|
|
@@ -581,26 +607,32 @@ const shown = (value: number, size: number) => (value < 0 ? String(value) : `$${
|
|
|
581
607
|
/**
|
|
582
608
|
* Refuses an address that is not the start of an instruction. The code
|
|
583
609
|
* regions of the knowledge give the instructions: they are decoded from the
|
|
584
|
-
* start of the region, as the listing and the source decode them.
|
|
610
|
+
* start of the region, as the listing and the source decode them. An operand
|
|
611
|
+
* expression needs an instruction, so it is refused also outside the code
|
|
612
|
+
* regions and where the program has no byte. A module and a comment are
|
|
613
|
+
* refused only at an operand byte of a known instruction: they can start data.
|
|
585
614
|
*/
|
|
586
|
-
function checkInstructionStart(db: DatabaseSync, space: Space,
|
|
615
|
+
function checkInstructionStart(db: DatabaseSync, space: Space, byteAt: (address: number) => number | undefined, address: number, write: "operand" | "module" | "comment"): void {
|
|
587
616
|
const where = formatC64Address(address);
|
|
588
617
|
const code = regionsOverlapping(db, address, address, space).find((region) => CODE_REGION_TYPES.includes(region.type));
|
|
589
618
|
if (code === undefined) {
|
|
619
|
+
if (write !== "operand") return;
|
|
590
620
|
throw new Refusal("invalid-input", `${where} is in no code region, so the knowledge does not know an instruction that starts there. Classify the code that holds the instruction first (classify <range> code), then write the operand.`);
|
|
591
621
|
}
|
|
592
622
|
let start = code.start;
|
|
593
623
|
let next = code.start;
|
|
594
624
|
while (next < address) {
|
|
595
|
-
const opcode =
|
|
625
|
+
const opcode = byteAt(next);
|
|
596
626
|
if (opcode === undefined) {
|
|
627
|
+
if (write !== "operand") return;
|
|
597
628
|
throw new Refusal("invalid-input", `The code region ${formatC64Address(code.start)}-${formatC64Address(code.end)} has no program byte at ${formatC64Address(next)}, so its instructions up to ${where} are not known. Correct the region (classify), then write the operand.`);
|
|
598
629
|
}
|
|
599
630
|
start = next;
|
|
600
631
|
next += instructionLength(opcode);
|
|
601
632
|
}
|
|
602
633
|
if (next !== address) {
|
|
603
|
-
|
|
634
|
+
const what = write === "operand" ? "" : ` A ${write} is at the start of an instruction, as the listing and the source show it.`;
|
|
635
|
+
throw new Refusal("invalid-input", `${where} is not the start of an instruction. It is an operand byte of the instruction at ${formatC64Address(start)}, decoded from the start of the code region at ${formatC64Address(code.start)}.${what} Give ${formatC64Address(start)}.`);
|
|
604
636
|
}
|
|
605
637
|
}
|
|
606
638
|
|
|
@@ -613,7 +645,7 @@ function checkOperandValue(db: DatabaseSync, space: Space, image: OperandImage,
|
|
|
613
645
|
const where = formatC64Address(request.address);
|
|
614
646
|
const opcode = image.byteAt(request.address);
|
|
615
647
|
if (opcode === undefined) throw new Refusal("invalid-input", `${where} is outside the image.`);
|
|
616
|
-
checkInstructionStart(db, space, image, request.address);
|
|
648
|
+
checkInstructionStart(db, space, (at) => image.byteAt(at), request.address, "operand");
|
|
617
649
|
const length = instructionLength(opcode);
|
|
618
650
|
if (length === 1) throw new Refusal("invalid-input", `The instruction at ${where} (opcode ${shown(opcode, 0x100)}) has no operand.`);
|
|
619
651
|
if ((opcode & 0x1f) === 0x10) throw new Refusal("invalid-input", `The instruction at ${where} is a branch: its operand is a distance, not a value.`);
|
|
@@ -646,10 +678,10 @@ function operandStep(db: DatabaseSync, change: Change, context: ChangeContext &
|
|
|
646
678
|
if (request.expression !== undefined) wanted.expression = request.expression.trim();
|
|
647
679
|
const targets = new Set(checkOperandValue(db, space, context.image, request, operandText(wanted)));
|
|
648
680
|
const existing = operandAt(db, request.address, space);
|
|
649
|
-
|
|
681
|
+
const origin = context.origin as OperandRow["origin"];
|
|
682
|
+
if (existing !== undefined && existing.select === wanted.select && existing.symbol === wanted.symbol && existing.offset === wanted.offset && existing.expression === wanted.expression && !confirms(existing.origin, origin)) {
|
|
650
683
|
return { current: existing };
|
|
651
684
|
}
|
|
652
|
-
const origin = context.origin as OperandRow["origin"];
|
|
653
685
|
if (existing !== undefined) change.close("operands", "space = ? AND address = ?", space, request.address);
|
|
654
686
|
change.insertOperand({ space, address: request.address, ...wanted, origin });
|
|
655
687
|
const pointers = referencesFrom(db, request.address, space).filter((reference) => reference.kind === POINTER_KIND);
|
|
@@ -683,19 +715,22 @@ function checkModule(request: ModuleRequest): void {
|
|
|
683
715
|
if (request.title !== undefined && (request.title.trim().length === 0 || request.title.length > MAX_COMMENT)) throw new Refusal("invalid-input", `A module title has 1 to ${MAX_COMMENT} characters.`);
|
|
684
716
|
}
|
|
685
717
|
|
|
686
|
-
function moduleStep(db: DatabaseSync, change: Change, origin: Origin, request: ModuleRequest): Step<ModuleRow> {
|
|
718
|
+
function moduleStep(db: DatabaseSync, change: Change, origin: Origin, request: ModuleRequest): Step<ModuleRow, ModuleRow> {
|
|
687
719
|
const space = spaceOf(request);
|
|
688
720
|
const existing = moduleAt(db, request.start, space);
|
|
689
721
|
// A module write without a title keeps the title it has.
|
|
690
722
|
const title = request.title === undefined ? existing?.title : request.title.trim();
|
|
691
|
-
if (existing !== undefined && existing.file === request.file && existing.title === title) return { current: existing };
|
|
723
|
+
if (existing !== undefined && existing.file === request.file && existing.title === title && !confirms(existing.origin, origin)) return { current: existing };
|
|
724
|
+
// A file of the source starts at an instruction: a start inside one splits it into bytes.
|
|
725
|
+
checkInstructionStart(db, space, programBytes(db, space), request.start, "module");
|
|
692
726
|
const owner = moduleWithFile(db, request.file, space);
|
|
693
727
|
if (owner !== undefined && owner.start !== request.start) {
|
|
694
728
|
throw new Refusal("conflict", `The file ${request.file} already belongs to the module at ${formatC64Address(owner.start)}. Give this module another file, or remove that module first.`);
|
|
695
729
|
}
|
|
696
730
|
if (existing !== undefined) change.close("modules", "space = ? AND start_address = ?", space, request.start);
|
|
697
731
|
change.insertModule({ space, start: request.start, file: request.file, ...(title === undefined ? {} : { title }), origin: origin as ModuleRow["origin"] });
|
|
698
|
-
|
|
732
|
+
const current = moduleAt(db, request.start, space)!;
|
|
733
|
+
return existing !== undefined && existing.origin !== origin ? { current, previous: existing } : { current };
|
|
699
734
|
}
|
|
700
735
|
|
|
701
736
|
function unmoduleStep(db: DatabaseSync, change: Change, _origin: Origin, request: InSpace & { start: number }): Step<null> {
|
|
@@ -752,17 +787,63 @@ function checkPatch(request: PatchRequest): void {
|
|
|
752
787
|
checkCommentText(request.comment, "A patch comment");
|
|
753
788
|
}
|
|
754
789
|
|
|
790
|
+
/** The data directives whose length the items give: the bytes of each item. */
|
|
791
|
+
const DATA_ITEM_BYTES: Record<string, number> = { "!byte": 1, "!by": 1, "!8": 1, "!08": 1, "!word": 2, "!wo": 2, "!16": 2 };
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* The least count of bytes that ACME patch code assembles to, when each line
|
|
795
|
+
* is empty, a comment, a data directive (!byte, !word, !fill with a number) or
|
|
796
|
+
* a 6510 instruction without a label. An instruction is 1 byte without an
|
|
797
|
+
* operand (or with A), else at least 2. Undefined for code with another line,
|
|
798
|
+
* for example a label, a macro use or a condition: then only the build of the
|
|
799
|
+
* switch (c64-source generate) gives the length.
|
|
800
|
+
*/
|
|
801
|
+
export function leastPatchBytes(code: string): number | undefined {
|
|
802
|
+
let bytes = 0;
|
|
803
|
+
for (const raw of code.split("\n")) {
|
|
804
|
+
if (/["']/.test(raw)) return undefined;
|
|
805
|
+
const line = raw.replace(/;.*$/, "").trim();
|
|
806
|
+
if (line === "") continue;
|
|
807
|
+
const [word, ...rest] = line.split(/\s+/);
|
|
808
|
+
const lower = word!.toLowerCase();
|
|
809
|
+
const operand = rest.join(" ");
|
|
810
|
+
const itemBytes = DATA_ITEM_BYTES[lower];
|
|
811
|
+
if (itemBytes !== undefined) {
|
|
812
|
+
if (operand === "") return undefined;
|
|
813
|
+
bytes += itemBytes * operand.split(",").length;
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
if (lower === "!fill" || lower === "!fi") {
|
|
817
|
+
const count = /^(?:\$([0-9a-f]+)|([0-9]+))\s*(?:,|$)/i.exec(operand);
|
|
818
|
+
if (count === null) return undefined;
|
|
819
|
+
bytes += count[1] === undefined ? Number.parseInt(count[2]!, 10) : Number.parseInt(count[1], 16);
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
const instruction = /^([a-z]{3})(?:\+([123]))?$/.exec(lower);
|
|
823
|
+
if (instruction === null || !MNEMONICS.has(instruction[1]!)) return undefined;
|
|
824
|
+
if (instruction[2] !== undefined) bytes += 1 + Number(instruction[2]);
|
|
825
|
+
else bytes += operand === "" || operand.toLowerCase() === "a" ? 1 : 2;
|
|
826
|
+
}
|
|
827
|
+
return bytes;
|
|
828
|
+
}
|
|
829
|
+
|
|
755
830
|
function patchStep(db: DatabaseSync, change: Change, context: ChangeContext & { image?: OperandImage }, request: PatchRequest): Step<PatchRow, PatchRow> {
|
|
756
831
|
const space = spaceOf(request);
|
|
757
832
|
const range = `${formatC64Address(request.start)}-${formatC64Address(request.end)}`;
|
|
758
833
|
if (context.image === undefined) throw new Refusal("invalid-input", "A patch is checked against the image, and no image was given.");
|
|
759
834
|
checkInImage(context.image, request.start, request.end, "a patch");
|
|
835
|
+
const least = leastPatchBytes(request.code);
|
|
836
|
+
const length = request.end - request.start + 1;
|
|
837
|
+
if (least !== undefined && least > length) {
|
|
838
|
+
throw new Refusal("invalid-input", `The patch code is at least ${least} bytes, and the range ${range} has ${length}. Make the code shorter, or make the range larger.`);
|
|
839
|
+
}
|
|
760
840
|
const symbol = symbolNamed(db, request.switch, space);
|
|
761
841
|
if (symbol !== undefined) throw new Refusal("conflict", `The switch name ${request.switch} is the name of the symbol at ${formatC64Address(symbol.address)}. Give the switch another name.`);
|
|
762
842
|
if (space === DEFAULT_SPACE && context.image.platform.has(request.switch)) throw new Refusal("conflict", `The switch name ${request.switch} is a C64 platform name. Give the switch another name.`);
|
|
763
|
-
|
|
843
|
+
// A write at the start of a patch of the same switch changes that patch; every other patch that it overlaps is a conflict.
|
|
844
|
+
const other = patchesOverlapping(db, request.start, request.end, space).find((row) => row.start !== request.start || row.switch !== request.switch);
|
|
764
845
|
if (other !== undefined) {
|
|
765
|
-
throw new Refusal("conflict", `The patch ${range} overlaps the patch of ${other.switch} at ${formatC64Address(other.start)}-${formatC64Address(other.end)}. Change or remove that patch first (unpatch).`);
|
|
846
|
+
throw new Refusal("conflict", `The patch ${range} of ${request.switch} overlaps the patch of ${other.switch} at ${formatC64Address(other.start)}-${formatC64Address(other.end)}. A patch cannot overlap another patch, and only a patch of the same switch at the same start replaces it. Change or remove that patch first (unpatch).`);
|
|
766
847
|
}
|
|
767
848
|
const [use] = macroUsesOverlapping(db, request.start, request.end, space);
|
|
768
849
|
if (use !== undefined) {
|
|
@@ -772,8 +853,8 @@ function patchStep(db: DatabaseSync, change: Change, context: ChangeContext & {
|
|
|
772
853
|
const code = request.code.trimEnd();
|
|
773
854
|
// A patch write without a comment keeps the comment it has, as a module keeps its title.
|
|
774
855
|
const comment = request.comment === undefined ? existing?.comment : request.comment.trim();
|
|
775
|
-
if (existing !== undefined && existing.end === request.end && existing.switch === request.switch && existing.code === code && existing.comment === comment) return { current: existing };
|
|
776
856
|
const origin = context.origin as PatchRow["origin"];
|
|
857
|
+
if (existing !== undefined && existing.end === request.end && existing.switch === request.switch && existing.code === code && existing.comment === comment && !confirms(existing.origin, origin)) return { current: existing };
|
|
777
858
|
if (existing !== undefined) change.close("patches", "space = ? AND start_address = ?", space, request.start);
|
|
778
859
|
change.insertPatch({ space, start: request.start, end: request.end, switch: request.switch, code, ...(comment === undefined ? {} : { comment }), origin });
|
|
779
860
|
const current = patchesOverlapping(db, request.start, request.start, space).find((row) => row.start === request.start)!;
|
|
@@ -815,7 +896,7 @@ function macroStep(db: DatabaseSync, change: Change, origin: Origin, request: Ma
|
|
|
815
896
|
const params = request.params ?? [];
|
|
816
897
|
const body = request.body.trimEnd();
|
|
817
898
|
const comment = request.comment === undefined ? existing?.comment : request.comment.trim();
|
|
818
|
-
if (existing !== undefined && listText(existing.params) === listText(params) && existing.body === body && existing.comment === comment) return { current: existing };
|
|
899
|
+
if (existing !== undefined && listText(existing.params) === listText(params) && existing.body === body && existing.comment === comment && !confirms(existing.origin, origin)) return { current: existing };
|
|
819
900
|
if (existing !== undefined && existing.params.length !== params.length) {
|
|
820
901
|
const uses = listMacroUses(db, undefined, request.name, undefined, space);
|
|
821
902
|
if (uses.length > 0) {
|
|
@@ -901,8 +982,8 @@ function useStep(db: DatabaseSync, change: Change, context: ChangeContext & { im
|
|
|
901
982
|
throw new Refusal("conflict", `The macro use ${range} overlaps the use of ${other.macro} at ${formatC64Address(other.start)}-${formatC64Address(other.end)}. Remove that use first (unuse).`);
|
|
902
983
|
}
|
|
903
984
|
const existing = macroUsesOverlapping(db, request.start, request.start, space).find((row) => row.start === request.start);
|
|
904
|
-
if (existing !== undefined && existing.end === request.end && existing.macro === request.macro && listText(existing.args) === listText(args)) return { current: existing };
|
|
905
985
|
const origin = context.origin as MacroUseRow["origin"];
|
|
986
|
+
if (existing !== undefined && existing.end === request.end && existing.macro === request.macro && listText(existing.args) === listText(args) && !confirms(existing.origin, origin)) return { current: existing };
|
|
906
987
|
if (existing !== undefined) change.close("macro_uses", "space = ? AND start_address = ?", space, request.start);
|
|
907
988
|
change.insertMacroUse({ space, start: request.start, end: request.end, macro: request.macro, args, origin });
|
|
908
989
|
const current = macroUsesOverlapping(db, request.start, request.start, space).find((row) => row.start === request.start)!;
|
|
@@ -940,7 +1021,7 @@ export function unclassifyRegion(db: DatabaseSync, context: WriteContext, reques
|
|
|
940
1021
|
return result(write(db, context, "unclassify-region", (change) => unclassifyStep(db, change, context.origin, request)));
|
|
941
1022
|
}
|
|
942
1023
|
|
|
943
|
-
export function setComment(db: DatabaseSync, context: WriteContext, request: CommentRequest): WriteResult<CommentRow> {
|
|
1024
|
+
export function setComment(db: DatabaseSync, context: WriteContext, request: CommentRequest): WriteResult<CommentRow, CommentRow> {
|
|
944
1025
|
checkComment(request);
|
|
945
1026
|
return result(write(db, context, "set-comment", (change) => commentStep(db, change, context.origin, request)));
|
|
946
1027
|
}
|
|
@@ -950,7 +1031,7 @@ export function removeComment(db: DatabaseSync, context: WriteContext, request:
|
|
|
950
1031
|
return result(write(db, context, "remove-comment", (change) => uncommentStep(db, change, context.origin, request)));
|
|
951
1032
|
}
|
|
952
1033
|
|
|
953
|
-
export function addReference(db: DatabaseSync, context: WriteContext, request: ReferenceRequest): WriteResult<ReferenceRow> {
|
|
1034
|
+
export function addReference(db: DatabaseSync, context: WriteContext, request: ReferenceRequest): WriteResult<ReferenceRow, ReferenceRow> {
|
|
954
1035
|
checkReference(request);
|
|
955
1036
|
return result(write(db, context, "add-reference", (change) => referenceStep(db, change, context.origin, request)));
|
|
956
1037
|
}
|
|
@@ -961,7 +1042,7 @@ export function removeReference(db: DatabaseSync, context: WriteContext, request
|
|
|
961
1042
|
}
|
|
962
1043
|
|
|
963
1044
|
/** Starts a file of the generated source at an address; one module per address and per file. */
|
|
964
|
-
export function setModule(db: DatabaseSync, context: WriteContext, request: ModuleRequest): WriteResult<ModuleRow> {
|
|
1045
|
+
export function setModule(db: DatabaseSync, context: WriteContext, request: ModuleRequest): WriteResult<ModuleRow, ModuleRow> {
|
|
965
1046
|
checkModule(request);
|
|
966
1047
|
return result(write(db, context, "set-module", (change) => moduleStep(db, change, context.origin, request)));
|
|
967
1048
|
}
|
|
@@ -1053,7 +1134,7 @@ export const MAX_BATCH = 10_000;
|
|
|
1053
1134
|
/** The result of each write of a batch, in order. */
|
|
1054
1135
|
export type BatchStep = Step<
|
|
1055
1136
|
SymbolRow | RegionRow[] | CommentRow | ReferenceRow | ModuleRow | OperandRow | PatchRow | MacroRow | MacroUseRow | null,
|
|
1056
|
-
SymbolRow | RegionRow[] | OperandRow | PatchRow | MacroRow | MacroUseRow
|
|
1137
|
+
SymbolRow | RegionRow[] | CommentRow | ReferenceRow | ModuleRow | OperandRow | PatchRow | MacroRow | MacroUseRow
|
|
1057
1138
|
>;
|
|
1058
1139
|
|
|
1059
1140
|
/**
|
package/src/mcp/output.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { z } from "zod";
|
|
|
7
7
|
|
|
8
8
|
import { projectFolder, writeReported, type Written } from "../host-client/transfer.ts";
|
|
9
9
|
|
|
10
|
-
export const OutInput = z.string().min(1).describe("write the
|
|
10
|
+
export const OutInput = z.string().min(1).describe("write the data to this file, relative to the project folder. An existing file is overwritten. The tool description tells what the result then gives");
|
|
11
11
|
|
|
12
12
|
/** The project of a tool with a path. */
|
|
13
13
|
export const ProjectInput = z
|
package/src/mcp/server.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { z } from "zod";
|
|
|
10
10
|
import { formatC64Address } from "../c64.ts";
|
|
11
11
|
import { Byte, TOOL_INPUTS } from "./inputs.ts";
|
|
12
12
|
import { LostBeforeAnswer, type ViceSessionClient } from "../host-client/vice-session.ts";
|
|
13
|
+
import { checkOut } from "../host-client/transfer.ts";
|
|
13
14
|
import { isRefusal } from "../errors.ts";
|
|
14
15
|
import { shownMessage, WireFailure, type WireError } from "../protocol/messages.ts";
|
|
15
16
|
import { COMPARISONS, CONDITION_REGISTERS, MEMORY_VIEWS, SPACES, type Condition } from "../protocol/vice.ts";
|
|
@@ -179,7 +180,10 @@ function errorResult(error: WireError): CallToolResult {
|
|
|
179
180
|
* An unknown field of the tool input also gets the fields that the tool takes.
|
|
180
181
|
*/
|
|
181
182
|
function describeIssues(error: z.ZodError, input?: z.ZodObject): string {
|
|
183
|
+
// An unknown field also breaks the rules that look at the whole object: name only the field.
|
|
184
|
+
const unknown = error.issues.some((issue) => issue.code === "unrecognized_keys");
|
|
182
185
|
return error.issues
|
|
186
|
+
.filter((issue) => !unknown || issue.code !== "custom")
|
|
183
187
|
.map((issue) => {
|
|
184
188
|
const where = issue.path.length === 0 ? "input" : issue.path.join(".");
|
|
185
189
|
const names = input === undefined ? [] : Object.keys(input.shape);
|
|
@@ -229,6 +233,9 @@ async function runCall(
|
|
|
229
233
|
const parsed = tool.inputSchema.safeParse(args ?? {});
|
|
230
234
|
if (!parsed.success) return errorResult({ code: "invalid-input", message: describeIssues(parsed.error, tool.inputSchema) });
|
|
231
235
|
try {
|
|
236
|
+
// An out path is checked before the tool runs: a refused path must not lose the work or leave its side effects.
|
|
237
|
+
const fields = parsed.data as { out?: unknown; project?: unknown };
|
|
238
|
+
if (typeof fields.out === "string") checkOut(fields.out, typeof fields.project === "string" ? fields.project : undefined);
|
|
232
239
|
let output: unknown;
|
|
233
240
|
try {
|
|
234
241
|
output = await tool.run(parsed.data, await session());
|
package/src/mcp/tools/debug.ts
CHANGED
|
@@ -186,7 +186,8 @@ export const c64Trace = defineTool({
|
|
|
186
186
|
"Each hit gives the instruction (pc, bytes, text) and the registers a, x, y and sp before it. " +
|
|
187
187
|
"point in a hit is the number of its point in points, 0 for the first. " +
|
|
188
188
|
"It also gives the frame from 0, and the raster line and cycle where the instruction started. " +
|
|
189
|
-
"A read or write hit also gives the address that the instruction used, and the byte
|
|
189
|
+
"A read or write hit also gives the address that the instruction used, and value, the byte that it read or wrote. " +
|
|
190
|
+
"At $D000-$DFFF only a load or a store gives value: a read after the access can give another byte. " +
|
|
190
191
|
`The trace ends after frames frames (1 to ${MAX_TRACE_FRAMES}), at maxHits hits (at most ${MAX_TRACE_HITS}), or at a breakpoint, a watchpoint or a JAM. ` +
|
|
191
192
|
`The result lists the first ${TRACE_LISTED} hits. With out, it writes all hits as JSON lines to that project file. ` +
|
|
192
193
|
"Each hit stops the machine for a short time. Thus a point with many hits in each frame makes the trace slow. Use a small range and a maxHits. " +
|
package/src/mcp/tools/media.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { extname } from "node:path";
|
|
|
7
7
|
import { z } from "zod";
|
|
8
8
|
|
|
9
9
|
import { formatC64Address } from "../../c64.ts";
|
|
10
|
+
import { refuseNotPrg } from "../../host-client/image.ts";
|
|
10
11
|
import { projectFolder, readProjectFile } from "../../host-client/transfer.ts";
|
|
11
12
|
import { WireFailure } from "../../protocol/messages.ts";
|
|
12
13
|
import { AUTOSTART_TYPES, DISK_TYPES, MAX_SNAPSHOTS, RUN_STATES } from "../../protocol/vice.ts";
|
|
@@ -44,6 +45,10 @@ export const c64Autostart = defineTool({
|
|
|
44
45
|
}),
|
|
45
46
|
readOnly: false,
|
|
46
47
|
async run(input, session) {
|
|
48
|
+
const type = extname(input.path).slice(1).toLowerCase();
|
|
49
|
+
if (input.index !== undefined && !(DISK_TYPES as readonly string[]).includes(type)) {
|
|
50
|
+
throw new WireFailure("invalid-input", `index selects an entry of a disk image (${list(DISK_TYPES)}). A .${type} file has no directory: leave out index.`);
|
|
51
|
+
}
|
|
47
52
|
// VICE numbers the directory entries from 1, and its 0 loads "*".
|
|
48
53
|
return session.autostart({ path: input.path, index: input.index === undefined ? 0 : input.index + 1, run: input.run, root: projectFolder(input.project) });
|
|
49
54
|
},
|
|
@@ -66,10 +71,8 @@ export const c64ProgramLoad = defineTool({
|
|
|
66
71
|
outputSchema: z.object({ state: z.enum(RUN_STATES), loadAddress: AddressOutput, size: z.number().int().min(1).max(0x10000) }),
|
|
67
72
|
readOnly: false,
|
|
68
73
|
async run(input, session) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
throw new WireFailure("invalid-input", `${input.path} is a .${type} image, not a PRG. Use c64_autostart, or c64_disk_attach for a disk image.`);
|
|
72
|
-
}
|
|
74
|
+
// The same rule as every PRG reader of the scripts: .prg or no extension.
|
|
75
|
+
refuseNotPrg(input.path, extname(input.path).slice(1).toLowerCase(), "Use c64_autostart, or c64_disk_attach for a disk image");
|
|
73
76
|
const root = projectFolder(input.project);
|
|
74
77
|
const result = await session.programLoad(input.address === undefined ? { path: input.path, root } : { path: input.path, address: input.address, root });
|
|
75
78
|
return { state: result.state, loadAddress: formatC64Address(result.loadAddress), size: result.size };
|
|
@@ -131,7 +134,7 @@ export const c64Snapshot = defineTool({
|
|
|
131
134
|
}
|
|
132
135
|
if (action === "restore" && fields.file !== undefined) {
|
|
133
136
|
requireFields(action, fields, ["file", "project"], ["file"]);
|
|
134
|
-
return session.snapshotRestore(readProjectFile(fields.file, { root: projectFolder(fields.project) }).bytes);
|
|
137
|
+
return session.snapshotRestore(readProjectFile(fields.file, { root: projectFolder(fields.project), what: "file" }).bytes);
|
|
135
138
|
}
|
|
136
139
|
if (action === "save") {
|
|
137
140
|
requireOut(fields);
|
package/src/media.ts
CHANGED
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
import { Refusal } from "./errors.ts";
|
|
6
6
|
|
|
7
|
+
/** The signature at the start of every VICE snapshot file (.vsf). */
|
|
8
|
+
export const VSF_SIGNATURE = "VICE Snapshot File\x1a";
|
|
9
|
+
|
|
7
10
|
/** The signature at the start of each media type that has one. */
|
|
8
11
|
const SIGNATURES: Readonly<Record<string, readonly string[]>> = {
|
|
9
12
|
crt: ["C64 CARTRIDGE "],
|
|
@@ -11,6 +14,7 @@ const SIGNATURES: Readonly<Record<string, readonly string[]>> = {
|
|
|
11
14
|
tap: ["C64-TAPE-RAW"],
|
|
12
15
|
g64: ["GCR-1541"],
|
|
13
16
|
p00: ["C64File"],
|
|
17
|
+
vsf: [VSF_SIGNATURE],
|
|
14
18
|
};
|
|
15
19
|
|
|
16
20
|
/** True when `bytes` start with a signature of `type`, or `type` has none. */
|
|
@@ -30,11 +34,25 @@ const SIZES: Readonly<Record<string, readonly number[]>> = {
|
|
|
30
34
|
/** The x64 header: 64 bytes that start with these four, then a d64 image. */
|
|
31
35
|
const X64_MAGIC = Buffer.from([0x43, 0x15, 0x41, 0x64]);
|
|
32
36
|
|
|
37
|
+
/** The refusal of a file that is no VICE snapshot file (.vsf): its signature is missing. `path` names the file. */
|
|
38
|
+
export function checkSnapshotFile(path: string, bytes: Uint8Array): void {
|
|
39
|
+
if (!hasSignature("vsf", bytes)) throw new Refusal("invalid-input", `${path} is not a VICE snapshot file (.vsf): it does not start with "VICE Snapshot File". Give a file that c64_snapshot save or a save step of a run wrote.`);
|
|
40
|
+
}
|
|
41
|
+
|
|
33
42
|
/** Refuses a file whose bytes are not of `type` (its extension), with media-error. */
|
|
34
43
|
export function checkMedia(type: string, bytes: Buffer): void {
|
|
35
44
|
const refuse = (why: string) => {
|
|
36
45
|
throw new Refusal("media-error", `The file is not a valid .${type} file: ${why}. Give a file of that type.`);
|
|
37
46
|
};
|
|
47
|
+
if (type === "crt" && hasSignature(type, bytes)) {
|
|
48
|
+
// The header gives its own length; a CHIP packet with the ROM data follows it.
|
|
49
|
+
const header = bytes.length >= 0x40 ? bytes.readUInt32BE(0x10) : 0;
|
|
50
|
+
if (bytes.length < 0x40) refuse("the header is shorter than 64 bytes");
|
|
51
|
+
if (bytes.toString("latin1", header, header + 4) !== "CHIP") refuse("it has no CHIP packet with ROM data after the header");
|
|
52
|
+
const packet = bytes.length >= header + 0x10 ? bytes.readUInt32BE(header + 4) : 0;
|
|
53
|
+
const size = bytes.length >= header + 0x10 ? bytes.readUInt16BE(header + 0x0e) : 0;
|
|
54
|
+
if (packet < 0x10 || header + packet > bytes.length || size > packet - 0x10) refuse(`the CHIP packet at offset ${header} runs past the end of the file`);
|
|
55
|
+
}
|
|
38
56
|
if (!hasSignature(type, bytes)) refuse(`it does not start with ${SIGNATURES[type]!.map((signature) => `"${signature.trimEnd()}"`).join(" or ")}`);
|
|
39
57
|
const sizes = SIZES[type];
|
|
40
58
|
if (sizes !== undefined && !sizes.includes(bytes.length)) refuse(`it has ${bytes.length} bytes, and a .${type} image has ${sizes.join(", ")}`);
|
package/src/opcodes.ts
CHANGED
|
@@ -76,6 +76,34 @@ export type UseKind = (typeof USE_KINDS)[number];
|
|
|
76
76
|
const WRITES = new Set(["sta", "stx", "sty", "sax", "sha", "shx", "shy", "tas"]);
|
|
77
77
|
const MODIFIES = new Set(["inc", "dec", "asl", "lsr", "rol", "ror", "slo", "rla", "sre", "rra", "dcp", "isc"]);
|
|
78
78
|
|
|
79
|
+
/**
|
|
80
|
+
* The byte that an instruction wrote to or read from its operand address:
|
|
81
|
+
* a store gives its register, a load the register after it. For another
|
|
82
|
+
* access the byte at the address now (`memoryNow`) is the byte, when the
|
|
83
|
+
* caller can read it back. Undefined when it is not known.
|
|
84
|
+
*/
|
|
85
|
+
export function accessedValue(opcode: number, before: { a: number; x: number; y: number }, after: { a: number; x: number; y: number }, memoryNow: number | undefined): number | undefined {
|
|
86
|
+
switch (OPCODES[opcode]!.mnemonic) {
|
|
87
|
+
case "sta":
|
|
88
|
+
return before.a;
|
|
89
|
+
case "stx":
|
|
90
|
+
return before.x;
|
|
91
|
+
case "sty":
|
|
92
|
+
return before.y;
|
|
93
|
+
case "sax":
|
|
94
|
+
return before.a & before.x;
|
|
95
|
+
case "lda":
|
|
96
|
+
case "lax":
|
|
97
|
+
return after.a;
|
|
98
|
+
case "ldx":
|
|
99
|
+
return after.x;
|
|
100
|
+
case "ldy":
|
|
101
|
+
return after.y;
|
|
102
|
+
default:
|
|
103
|
+
return memoryNow;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
79
107
|
/** The use of the operand address of an instruction that takes an address. Through (zp),y and (zp,x) the address is a pointer. */
|
|
80
108
|
export function useKind(opcode: number): UseKind {
|
|
81
109
|
const { mnemonic, mode } = OPCODES[opcode]!;
|
|
@@ -130,7 +158,9 @@ export function operandNumber(bytes: readonly number[], pc: number): string {
|
|
|
130
158
|
/** The text of the instruction in `bytes` at `pc` as the monitor shows it, for example "STA $d011" or "BNE $c002". */
|
|
131
159
|
export function instructionText(bytes: readonly number[], pc: number): string {
|
|
132
160
|
const { mnemonic } = OPCODES[bytes[0]!]!;
|
|
133
|
-
|
|
161
|
+
const operand = instructionWith(bytes[0]!, operandNumber(bytes, pc)).slice(mnemonic.length);
|
|
162
|
+
// The monitor writes the index register in capitals: LDA ($c1),Y.
|
|
163
|
+
return `${mnemonic.toUpperCase()}${operand.replace(/,([xy])\b/g, (_, index: string) => `,${index.toUpperCase()}`)}`;
|
|
134
164
|
}
|
|
135
165
|
|
|
136
166
|
/**
|
package/src/platform.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Names follow "Mapping the Commodore 64". Only platform facts live here:
|
|
4
4
|
// an address without one belongs to the application.
|
|
5
5
|
|
|
6
|
-
import { d018Offsets, formatC64Address, vicBankBase } from "./c64.ts";
|
|
6
|
+
import { COVERAGE, d018Offsets, formatC64Address, vicBankBase } from "./c64.ts";
|
|
7
7
|
import { Refusal } from "./errors.ts";
|
|
8
8
|
|
|
9
9
|
export interface Entry {
|
|
@@ -420,6 +420,19 @@ export function romUseOfPort(port: number): RomUse {
|
|
|
420
420
|
return { basic: (port & 0x03) === 0x03, kernal: (port & 0x02) !== 0 };
|
|
421
421
|
}
|
|
422
422
|
|
|
423
|
+
/**
|
|
424
|
+
* The ROMs that a program uses, and what tells it: the coverage when the
|
|
425
|
+
* knowledge has it, else $01 of the entry state, else both ROMs. c64-source and
|
|
426
|
+
* c64-static-analysis give the platform names of platformNamesFor of it, so
|
|
427
|
+
* they use the same names.
|
|
428
|
+
*/
|
|
429
|
+
export function romUseOfProgram(coverage: Uint8Array | undefined, state: Record<string, unknown> | undefined): RomUse & { from: "coverage" | "entry state" | "none" } {
|
|
430
|
+
if (coverage !== undefined) return { ...romUseOf(coverage, COVERAGE.romExecute), from: "coverage" };
|
|
431
|
+
const port = (state?.cpuPort as Record<string, unknown> | undefined)?.$01;
|
|
432
|
+
if (typeof port === "number") return { ...romUseOfPort(port), from: "entry state" };
|
|
433
|
+
return { basic: true, kernal: true, from: "none" };
|
|
434
|
+
}
|
|
435
|
+
|
|
423
436
|
export type Decoded = Record<string, unknown>;
|
|
424
437
|
|
|
425
438
|
const bits = (value: number) => [0, 1, 2, 3, 4, 5, 6, 7].filter((bit) => (value & (1 << bit)) !== 0);
|
package/src/project.ts
CHANGED
|
@@ -37,8 +37,13 @@ let chosen: string | undefined;
|
|
|
37
37
|
/** The project that the project lookup found from a working directory, kept for the next call from the same directory. */
|
|
38
38
|
let found: { cwd: string; root: string } | undefined;
|
|
39
39
|
|
|
40
|
+
/** True when `folder` holds .c64-re-tools/. A folder that cannot be read (no permission) is not a project. */
|
|
40
41
|
function isProject(folder: string): boolean {
|
|
41
|
-
|
|
42
|
+
try {
|
|
43
|
+
return statSync(join(folder, PROJECT_DIRECTORY), { throwIfNoEntry: false })?.isDirectory() === true;
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
42
47
|
}
|
|
43
48
|
|
|
44
49
|
/** `start` and each folder above it, nearest first, below the home folder when `start` is under it. */
|
|
@@ -246,9 +251,13 @@ export function checkOutputPath(path: string, kind: OutputKind, options: { root?
|
|
|
246
251
|
};
|
|
247
252
|
const inKnowledge = (fromRoot: string) => {
|
|
248
253
|
const part = knowledgePart(fromRoot);
|
|
249
|
-
if (part
|
|
250
|
-
|
|
254
|
+
if (part === undefined) return;
|
|
255
|
+
const prefix = `${what === undefined ? "" : `${what}: `}${path}`;
|
|
256
|
+
// The index is one file in the folder for local files: another name in that folder is good.
|
|
257
|
+
if (!part.endsWith("/")) {
|
|
258
|
+
throw new Refusal("invalid-input", `${prefix} is the index of the knowledge${path === part ? "" : ` (${part})`}. Only the c64-knowledge skill writes it: a write there destroys the index. Give another file name, for example ${PROJECT_DIRECTORY}/${LOCAL_DIRECTORY}/<other name>.`);
|
|
251
259
|
}
|
|
260
|
+
throw new Refusal("invalid-input", `${prefix} is in ${part}, the knowledge of the project. Only the c64-knowledge skill writes it: a write there destroys the knowledge. Give a path outside ${part}, for example ${PROJECT_DIRECTORY}/${LOCAL_DIRECTORY}/<${kind === "folder" ? "folder" : "file"}>.`);
|
|
252
261
|
};
|
|
253
262
|
let target: string;
|
|
254
263
|
try {
|
|
@@ -277,6 +286,8 @@ export function checkOutputPath(path: string, kind: OutputKind, options: { root?
|
|
|
277
286
|
const stat = statSync(real);
|
|
278
287
|
if (existing === target && kind === "file") {
|
|
279
288
|
if (stat.isDirectory()) throw problem({ code: "EISDIR" });
|
|
289
|
+
} else if (existing === target && !stat.isDirectory()) {
|
|
290
|
+
throw new Refusal("invalid-input", `${what === undefined ? "" : `${what}: `}${path} is a file, but ${what ?? "the path"} needs a folder. Give the path of a folder, or a new path for the folder.`);
|
|
280
291
|
} else if (!stat.isDirectory()) {
|
|
281
292
|
throw problem({ code: "ENOTDIR" });
|
|
282
293
|
}
|