Clear stale toolpaths on program replacement

This commit is contained in:
2026-09-02 13:55:45 +08:00
parent ee2c992328
commit 8b7fc5221b
4 changed files with 75 additions and 3 deletions

View File

@@ -1926,6 +1926,11 @@ export class SimSession {
case "PROGRAM_LOAD": {
if (["RUNNING", "PAUSED"].includes(this.task.state.state)) return finish(this.error(input, "task_state", "program_load_while_active"), "failed");
const source = input.payload?.source ?? input.payload?.text ?? input.payload?.program_source;
// PROGRAM_LOAD is a replacement transaction. Clear the previous
// canonical preview before asking the WASM interpreter to parse the
// new source, so a parse/subroutine error cannot re-publish the old
// toolpath as if it belonged to the requested file.
this.previewState = null;
if (this.xyzacProgram) {
this.pendingXyzacMotion = null;
this.pendingProgramControl = 0;

View File

@@ -0,0 +1,39 @@
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
import { SimSession, CONTEXT_ID } from "./sim-session.mjs";
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
const fetchImpl = async (url) => {
try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; }
catch { return { ok: false, text: async () => "" }; }
};
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
const session = new SimSession(core, { sessionId: "xyzac-program-replace" });
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
let seq = 0;
const send = (kind, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: `replace-${seq}`, payload });
send("HELLO"); send("SESSION_ATTACH");
assert.equal(send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
const first = send("PROGRAM_LOAD", { path: "first.ngc", source: "G21\nG1 X10 Y5 Z2 F100\nM2\n" });
assert.equal(first.payload.preview.segments.length, 1);
assert.equal(first.payload.panel_snapshot.visual.toolpath.length, 1);
const failed = send("PROGRAM_LOAD", { path: "invalid.ngc", source: "G999\n" });
assert.equal(failed.kind, "ERROR");
assert.equal(failed.payload.panel_snapshot.visual.toolpath.length, 0);
assert.equal(session.previewState, null);
const second = send("PROGRAM_LOAD", { path: "second.ngc", source: "G21\nG1 X1 Y2 Z3 F100\nM2\n" });
assert.equal(second.payload.program.path, "second.ngc");
assert.notEqual(second.payload.program.source_hash, first.payload.program.source_hash);
assert.equal(second.payload.panel_snapshot.visual.toolpath.length, 1);
console.log(JSON.stringify({
schema: "xyzac-program-replacement.v1",
status: "verified",
first_segments: first.payload.panel_snapshot.visual.toolpath.length,
failed_segments: failed.payload.panel_snapshot.visual.toolpath.length,
second_segments: second.payload.panel_snapshot.visual.toolpath.length,
old_preview_retained_after_failure: false,
}));