@henols/c64-re-tools 2.8.0-rc.2 → 2.8.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/package.json +1 -1
  2. package/skills/c64-assembler/SKILL.md +5 -2
  3. package/skills/c64-assembler/scripts/assembler.ts +55 -48
  4. package/skills/c64-disk/scripts/disk.ts +3 -3
  5. package/skills/c64-disk/scripts/lib/containers.ts +2 -2
  6. package/skills/c64-docs/SKILL.md +1 -1
  7. package/skills/c64-docs/scripts/docs.ts +12 -20
  8. package/skills/c64-docs/scripts/lib/links.ts +1 -1
  9. package/skills/c64-docs/scripts/lib/markdown.ts +1 -1
  10. package/skills/c64-docs/scripts/lib/reference.ts +59 -13
  11. package/skills/c64-knowledge/SKILL.md +17 -8
  12. package/skills/c64-knowledge/scripts/knowledge.ts +200 -218
  13. package/skills/c64-knowledge/scripts/lib/check.ts +2 -3
  14. package/skills/c64-knowledge/scripts/lib/listing.ts +56 -24
  15. package/skills/c64-packer/scripts/packer.ts +3 -3
  16. package/skills/c64-provenance/SKILL.md +25 -16
  17. package/skills/c64-provenance/scripts/lib/compare.ts +31 -21
  18. package/skills/c64-provenance/scripts/lib/leftovers.ts +17 -35
  19. package/skills/c64-provenance/scripts/lib/library.ts +10 -10
  20. package/skills/c64-provenance/scripts/lib/runtime.ts +3 -14
  21. package/skills/c64-provenance/scripts/provenance.ts +129 -98
  22. package/skills/c64-source/references/loader.md +2 -1
  23. package/skills/c64-source/scripts/lib/export.ts +54 -303
  24. package/skills/c64-source/scripts/lib/loader.ts +31 -51
  25. package/skills/c64-source/scripts/lib/source.ts +51 -41
  26. package/skills/c64-source/scripts/lib/stage.ts +9 -25
  27. package/skills/c64-source/scripts/source.ts +134 -294
  28. package/skills/c64-static-analysis/SKILL.md +10 -5
  29. package/skills/c64-static-analysis/scripts/lib/coverage.ts +1 -3
  30. package/skills/c64-static-analysis/scripts/lib/findings.ts +16 -3
  31. package/skills/c64-static-analysis/scripts/lib/idioms.ts +82 -10
  32. package/skills/c64-static-analysis/scripts/lib/triage.ts +112 -0
  33. package/skills/c64-static-analysis/scripts/static-analysis.ts +239 -147
  34. package/skills/c64-testing/SKILL.md +13 -7
  35. package/skills/c64-testing/scripts/lib/diff-files.ts +6 -13
  36. package/skills/c64-testing/scripts/lib/recording.ts +47 -66
  37. package/skills/c64-testing/scripts/lib/replay.ts +53 -9
  38. package/skills/c64-testing/scripts/lib/runs.ts +11 -29
  39. package/skills/c64-testing/scripts/lib/scenario.ts +30 -31
  40. package/skills/c64-testing/scripts/lib/start-screen.ts +42 -5
  41. package/skills/c64-testing/scripts/testing.ts +32 -7
  42. package/skills/c64-unpacker/SKILL.md +9 -7
  43. package/skills/c64-unpacker/scripts/lib/evidence.ts +23 -38
  44. package/skills/c64-unpacker/scripts/lib/loads.ts +24 -39
  45. package/skills/c64-unpacker/scripts/lib/machine.ts +38 -18
  46. package/skills/c64-unpacker/scripts/lib/stages.ts +124 -40
  47. package/skills/c64-unpacker/scripts/unpacker.ts +81 -68
  48. package/skills/emulator/SKILL.md +1 -0
  49. package/src/c64.ts +117 -10
  50. package/src/cli/ap-sdk.ts +1 -1
  51. package/src/cli/cleanup.ts +4 -2
  52. package/src/compare.ts +36 -16
  53. package/src/errors.ts +10 -1
  54. package/src/host/main.ts +2 -1
  55. package/src/host/server.ts +7 -11
  56. package/src/host/tools/c1541.ts +9 -3
  57. package/src/host/vice/adapter.ts +20 -43
  58. package/src/host/vice/binary-monitor.ts +22 -21
  59. package/src/host/vice/process.ts +31 -40
  60. package/src/host/vice/session.ts +187 -247
  61. package/src/host/vice/text-monitor.ts +3 -3
  62. package/src/host-client/autostart.ts +1 -1
  63. package/src/host-client/connect.ts +2 -1
  64. package/src/host-client/coverage.ts +0 -3
  65. package/src/host-client/entry-state.ts +4 -11
  66. package/src/host-client/image.ts +6 -11
  67. package/src/host-client/steps.ts +13 -113
  68. package/src/host-client/tools.ts +4 -4
  69. package/src/host-client/transfer.ts +10 -0
  70. package/src/host-client/vice-session.ts +10 -6
  71. package/src/knowledge/code-reach.ts +364 -0
  72. package/src/knowledge/conflicts.ts +34 -72
  73. package/src/knowledge/database.ts +40 -28
  74. package/src/knowledge/evidence.ts +2 -2
  75. package/src/knowledge/graph.ts +3 -3
  76. package/src/knowledge/history.ts +50 -90
  77. package/src/knowledge/import.ts +28 -68
  78. package/src/knowledge/join.ts +1 -1
  79. package/src/knowledge/names.ts +12 -7
  80. package/src/knowledge/program.ts +25 -46
  81. package/src/knowledge/read.ts +104 -100
  82. package/src/knowledge/schema.ts +9 -3
  83. package/src/knowledge/separate.ts +47 -19
  84. package/src/knowledge/start-screen.ts +7 -5
  85. package/src/knowledge/text.ts +56 -33
  86. package/src/knowledge/write.ts +274 -515
  87. package/src/loader.ts +7 -25
  88. package/src/mcp/inputs.ts +3 -3
  89. package/src/mcp/server.ts +19 -38
  90. package/src/mcp/session-source.ts +2 -1
  91. package/src/mcp/tools/debug.ts +97 -24
  92. package/src/mcp/tools/execution.ts +1 -1
  93. package/src/mcp/tools/input.ts +3 -3
  94. package/src/mcp/tools/machine.ts +3 -3
  95. package/src/mcp/tools/media.ts +4 -4
  96. package/src/mcp/tools/memory.ts +13 -17
  97. package/src/mcp/tools/video.ts +11 -25
  98. package/src/native/acme.ts +3 -3
  99. package/src/native/c64run.ts +2 -1
  100. package/src/native/decompiler/analyze.ts +10 -7
  101. package/src/native/decompiler/walk.ts +3 -4
  102. package/src/native/decompiler/wasm.ts +2 -2
  103. package/src/native/discover.ts +1 -1
  104. package/src/native/dxa.ts +9 -11
  105. package/src/native/exomizer.ts +1 -1
  106. package/src/native/processes.ts +4 -1
  107. package/src/native/run.ts +3 -3
  108. package/src/native/types.ts +5 -6
  109. package/src/opcodes.ts +3 -0
  110. package/src/platform.ts +9 -10
  111. package/src/program-code.ts +151 -0
  112. package/src/project.ts +8 -5
  113. package/src/protocol/framing.ts +51 -18
  114. package/src/protocol/messages.ts +1 -11
  115. package/src/protocol/tools.ts +0 -3
  116. package/src/protocol/vice.ts +588 -773
  117. package/src/script.ts +9 -14
  118. package/src/time.ts +1 -1
  119. package/src/trace.ts +3 -2
package/src/project.ts CHANGED
@@ -16,7 +16,7 @@ export const PROJECT_DIRECTORY = ".c64-re-tools";
16
16
  export const LOCAL_DIRECTORY = "local";
17
17
 
18
18
  /** The folder in LOCAL_DIRECTORY for the reports of a generated source: its source map and the code that no run executed. */
19
- export const GENERATED_DIRECTORY = "generated";
19
+ const GENERATED_DIRECTORY = "generated";
20
20
 
21
21
  /**
22
22
  * The folder of the reports of the source that was generated into `out`, relative
@@ -292,11 +292,14 @@ export function resolveProjectPath(path: string, root = projectRoot()): string {
292
292
  throw new Refusal("invalid-input", `${path} leads out of the project folder: give a path that stays inside it.`);
293
293
  }
294
294
 
295
- /** The folder of the committed knowledge text in PROJECT_DIRECTORY. The knowledge (src/knowledge/database.ts) has the same name. */
295
+ /** The folder of the committed knowledge text in PROJECT_DIRECTORY. */
296
296
  export const KNOWLEDGE_TEXT_DIRECTORY = "knowledge";
297
297
 
298
- /** The index of the knowledge in LOCAL_DIRECTORY, and the files that SQLite keeps beside it. The knowledge has the same name. */
299
- const KNOWLEDGE_INDEX = /^knowledge\.db(?:-wal|-shm|-journal)?$/;
298
+ /** The index of the knowledge in LOCAL_DIRECTORY. */
299
+ export const KNOWLEDGE_INDEX_FILE = "knowledge.db";
300
+
301
+ /** The index and the files that SQLite keeps beside it. */
302
+ const KNOWLEDGE_INDEX_FILES = ["", "-wal", "-shm", "-journal"].map((suffix) => `${KNOWLEDGE_INDEX_FILE}${suffix}`);
300
303
 
301
304
  /**
302
305
  * The part of the knowledge that a path relative to the project folder is in:
@@ -308,7 +311,7 @@ export function knowledgePart(fromRoot: string): string | undefined {
308
311
  const parts = fromRoot.split(/[\\/]+/).filter((part) => part !== "" && part !== ".").map((part) => part.toLowerCase());
309
312
  if (parts[0] !== PROJECT_DIRECTORY) return undefined;
310
313
  if (parts[1] === KNOWLEDGE_TEXT_DIRECTORY) return `${PROJECT_DIRECTORY}/${KNOWLEDGE_TEXT_DIRECTORY}/`;
311
- if (parts[1] === LOCAL_DIRECTORY && parts.length === 3 && KNOWLEDGE_INDEX.test(parts[2]!)) return `${PROJECT_DIRECTORY}/${LOCAL_DIRECTORY}/${parts[2]}`;
314
+ if (parts[1] === LOCAL_DIRECTORY && parts.length === 3 && KNOWLEDGE_INDEX_FILES.includes(parts[2]!)) return `${PROJECT_DIRECTORY}/${LOCAL_DIRECTORY}/${parts[2]}`;
312
315
  return undefined;
313
316
  }
314
317
 
@@ -28,12 +28,6 @@ export class FrameTooLarge extends ProtocolError {
28
28
  }
29
29
  }
30
30
 
31
- function frame(body: Uint8Array): Buffer {
32
- const header = Buffer.allocUnsafe(HEADER_BYTES);
33
- header.writeUInt32BE(body.length, 0);
34
- return Buffer.concat([header, body]);
35
- }
36
-
37
31
  /** Encodes one message and its attachments as one buffer, so writes never interleave. */
38
32
  export function encodeFrame(message: ClientMessage | HostMessage, attachments: readonly Uint8Array[] = []): Buffer {
39
33
  const total = attachments.reduce((sum, attachment) => sum + attachment.length, 0);
@@ -41,28 +35,67 @@ export function encodeFrame(message: ClientMessage | HostMessage, attachments: r
41
35
  const announced = attachments.length === 0 ? message : { ...message, attachments: attachments.map((attachment) => attachment.length) };
42
36
  const body = Buffer.from(JSON.stringify(announced), "utf8");
43
37
  if (body.length > MAX_FRAME_BYTES) throw new FrameTooLarge("frame", body.length, MAX_FRAME_BYTES);
44
- const frames = [frame(body)];
38
+ const frames = 1 + attachments.reduce((sum, attachment) => sum + Math.ceil(attachment.length / MAX_FRAME_BYTES), 0);
39
+ const encoded = Buffer.allocUnsafe(frames * HEADER_BYTES + body.length + total);
40
+ let offset = 0;
41
+ const frame = (part: Uint8Array) => {
42
+ offset = encoded.writeUInt32BE(part.length, offset);
43
+ encoded.set(part, offset);
44
+ offset += part.length;
45
+ };
46
+ frame(body);
45
47
  for (const attachment of attachments) {
46
- for (let offset = 0; offset < attachment.length; offset += MAX_FRAME_BYTES) {
47
- frames.push(frame(attachment.subarray(offset, offset + MAX_FRAME_BYTES)));
48
- }
48
+ for (let start = 0; start < attachment.length; start += MAX_FRAME_BYTES) frame(attachment.subarray(start, start + MAX_FRAME_BYTES));
49
+ }
50
+ return encoded;
51
+ }
52
+
53
+ /**
54
+ * The bytes of a stream so far. A decoder joins the chunks only once the bytes
55
+ * of its next step are all here: joining at each chunk copies a large frame
56
+ * again and again.
57
+ */
58
+ export class StreamBuffer {
59
+ #chunks: Buffer[] = [];
60
+ length = 0;
61
+
62
+ push(chunk: Buffer): void {
63
+ this.#chunks.push(chunk);
64
+ this.length += chunk.length;
65
+ }
66
+
67
+ /** All buffered bytes as one buffer. */
68
+ bytes(): Buffer {
69
+ if (this.#chunks.length !== 1) this.#chunks = [Buffer.concat(this.#chunks, this.length)];
70
+ return this.#chunks[0]!;
71
+ }
72
+
73
+ /** Drops the first `count` bytes. */
74
+ drop(count: number): void {
75
+ const rest = this.bytes().subarray(count);
76
+ this.#chunks = rest.length === 0 ? [] : [rest];
77
+ this.length = rest.length;
49
78
  }
50
- return Buffer.concat(frames);
51
79
  }
52
80
 
53
81
  /** Splits a stream into frame bodies. After a ProtocolError the decoder is unusable. */
54
82
  class RawFrameDecoder {
55
- #pending: Buffer = Buffer.alloc(0);
83
+ readonly #stream = new StreamBuffer();
84
+ /** The bytes that the frame in front needs: its header, then its header and body. */
85
+ #needed = HEADER_BYTES;
56
86
 
57
87
  push(chunk: Buffer): Buffer[] {
58
- this.#pending = this.#pending.length === 0 ? chunk : Buffer.concat([this.#pending, chunk]);
88
+ this.#stream.push(chunk);
59
89
  const bodies: Buffer[] = [];
60
- while (this.#pending.length >= HEADER_BYTES) {
61
- const length = this.#pending.readUInt32BE(0);
90
+ while (this.#stream.length >= this.#needed) {
91
+ const pending = this.#stream.bytes();
92
+ const length = pending.readUInt32BE(0);
62
93
  if (length > MAX_FRAME_BYTES) throw new ProtocolError(`frame of ${length} bytes exceeds ${MAX_FRAME_BYTES}`);
63
- if (this.#pending.length < HEADER_BYTES + length) break;
64
- bodies.push(Buffer.from(this.#pending.subarray(HEADER_BYTES, HEADER_BYTES + length)));
65
- this.#pending = this.#pending.subarray(HEADER_BYTES + length);
94
+ this.#needed = HEADER_BYTES + length;
95
+ if (pending.length < this.#needed) break;
96
+ bodies.push(Buffer.from(pending.subarray(HEADER_BYTES, HEADER_BYTES + length)));
97
+ this.#stream.drop(HEADER_BYTES + length);
98
+ this.#needed = HEADER_BYTES;
66
99
  }
67
100
  return bodies;
68
101
  }
@@ -25,7 +25,7 @@ export interface WireError {
25
25
  message: string;
26
26
  }
27
27
 
28
- export const ROLES = ["vice-session", "tool"] as const;
28
+ const ROLES = ["vice-session", "tool"] as const;
29
29
  export type Role = (typeof ROLES)[number];
30
30
 
31
31
  export const VIDEO_STANDARDS = ["pal", "ntsc"] as const;
@@ -96,7 +96,6 @@ export class ProtocolError extends Error {
96
96
  override name = "ProtocolError";
97
97
  }
98
98
 
99
- /** Thrown by parameter/result validators; carries the wire code to answer with. */
100
99
  /**
101
100
  * The instruction at the end of a refusal that only the user can fix: a
102
101
  * missing or broken tool, or a Host Runtime that does not run.
@@ -116,15 +115,6 @@ export function shownMessage(error: { code: string; message: string }): string {
116
115
  /** A refusal that crosses the Host Runtime wire. */
117
116
  export class WireFailure extends Refusal implements WireError {
118
117
  override name = "WireFailure";
119
-
120
- toWire(): WireError {
121
- return { code: this.code, message: this.message };
122
- }
123
- }
124
-
125
- /** The message of a caught value, which may not be an Error. */
126
- export function describeError(error: unknown): string {
127
- return error instanceof Error ? error.message : String(error);
128
118
  }
129
119
 
130
120
  export type Fields = Record<string, unknown>;
@@ -64,9 +64,6 @@ export type C1541Result =
64
64
  /** petcat.decode takes no fields: the program bytes are the request's one attachment. */
65
65
  export type PetcatParams = Record<string, never>;
66
66
 
67
- /** A machine-code handoff: SYS with a constant address, or SYS/USR whose target is computed at run time. */
68
- export type { BasicHandoff };
69
-
70
67
  export type PetcatResult =
71
68
  | { decoded: false; reason: string }
72
69
  | {