diff --git a/package.json b/package.json
index b0b61a8..f5a9321 100644
--- a/package.json
+++ b/package.json
@@ -22,6 +22,7 @@
"start": "node dist/index.js",
"lint": "eslint src --ext .ts",
"test": "vitest run",
+ "prepare": "npm run build",
"prepublishOnly": "npm run build"
},
"keywords": [
diff --git a/src/cli.ts b/src/cli.ts
index 755e9f1..e74dfff 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -3,6 +3,7 @@ import { getFlags, setGlobalFlags } from "./lib/logger.js";
import { logoStatic } from "./ui/brand.js";
import { init } from "./commands/init.js";
import { test } from "./commands/test.js";
+import { verify } from "./commands/verify.js";
import { mock } from "./commands/mock.js";
import { analyze } from "./commands/analyze.js";
import { config, configList, configGet, configSet, configPaths, configGenerateSecrets, configPreflight } from "./commands/config.js";
@@ -86,6 +87,8 @@ export function createProgram(): Command {
.option("--vectors-path
", "Path to protocol/test-vectors")
.action(test);
+ program.command("verify").description("Guided first-crash self-test (inject -> capture -> spool -> decode -> delivered)").option("--verbose","Verbose output").option("--vectors-path
","Path to protocol/test-vectors").action(verify);
+
program.command("mock").description("Run mock Trace/Relay for development")
.option("--port ", "Port", "8080")
.option("--host ", "Host", "localhost")
diff --git a/src/commands/init.ts b/src/commands/init.ts
index b4db5ff..292ef18 100644
--- a/src/commands/init.ts
+++ b/src/commands/init.ts
@@ -51,7 +51,45 @@ function mainC(project: string, target: string): string {
const device = `${project.toLowerCase().replace(/[^a-z0-9]+/g, "-")}-001`;
const arch = TARGET_ARCH[target] ?? "LS_ARCH_UNKNOWN";
const heapNote = target === "arduino" ? "/* constrained profile: breadcrumbs 16 */\n" : "";
+ const host = target === "host";
+ // Board storage constructor + ready transport differ per board. On a
+ // host we can emit a fully working binary; on embedded we pre-wire
+ // everything we can and leave a single board sink to fill.
+ const storageBlock = host
+ ? `/* host: retained-RAM spool in .noinit-capable static memory */
+static uint8_t s_spool[64 * 1024];
+static ls_retained_storage_t s_retained;
+
+ /* no-op host reset/critical */
+ `
+ : `static uint8_t s_spool[64 * 1024]; /* >= ls_storage_required_size() */
+static ls_flash_storage_t s_flash; /* per-board flash; see ls_*_flash_storage */
+static uint8_t s_flash_workspace[4096];
+
+ `;
+ const transportBlock = host
+ ? `ls_sink_transport_t s_stdout;
+ /* host: ready stdout transport */
+ if (ls_stdout_transport(&s_stdout) != LS_OK) return;`
+ : `ls_sink_transport_t s_uart;
+ /* pre-wired byte sink; supply your board's UART/CDC writer:
+ * static ls_result_t sink(void *ctx, const uint8_t *d, size_t n) { ... return LS_OK; }
+ */
+ ls_result_t (*sink)(void *, const uint8_t *, size_t) = 0; /* TODO board sink */
+ if (sink && ls_uart_transport(&s_uart, sink, 0) != LS_OK) return;`;
+ const storageRegister = host
+ ? `if (ls_retained_ram_storage(&s_retained, s_spool, sizeof s_spool) != LS_OK) return;
+ ls_storage_register(&s_retained.backend);`
+ : `/* register a per-board ready backend, e.g.:
+ * ls_esp32_partition_storage(&s_flash, &dev, s_flash_workspace, sizeof s_flash_workspace, 4096);
+ * ls_stm32_flash_storage(&s_flash, &dev, s_flash_workspace, sizeof s_flash_workspace, 4096);
+ * ls_rp2040_flash_storage(&s_flash, &dev, s_flash_workspace, sizeof s_flash_workspace, 4096);
+ * ls_nrf_nvs_storage(&s_flash, &dev, s_flash_workspace, sizeof s_flash_workspace, 4096);
+ * then ls_storage_register(&s_flash.backend); */
+ (void)s_spool; (void)s_flash; (void)s_flash_workspace;`;
return `#include "laststate/latch.h"
+#include "laststate/board.h"
+#include "laststate/fault_handler.h"
${heapNote}
static ls_identity_t s_identity = {
.project_id = "${project}",
@@ -60,29 +98,54 @@ static ls_identity_t s_identity = {
.firmware_build_id = "dev-build",
};
+/* port defaults: timestamp_ms/reset/reset_info/critical. Replace with
+ * the board port (ls_board_platform(ls_board_detect()) when available),
+ * or wire your own. */
static uint32_t timestamp_ms(void *context) {
(void)context;
return 0; /* return a monotonic millisecond counter */
}
+${storageBlock.trim()}
+
void app_init(void) {
- static const ls_config_t cfg = {
- .identity = &s_identity,
- .architecture = ${arch},
- .timestamp_ms = timestamp_ms,
- };
- if (ls_init(&cfg) != LS_OK) {
- return;
- }
- /* Register the backend that reaches your Relay BEFORE ls_boot():
- * ls_transport_register(&my_uart_transport); // include/laststate/transport.h
- */
- ls_boot();
+ ls_platform_t platform = {0};
+ platform.architecture = ${arch};
+ platform.timestamp_ms = timestamp_ms;
+ /* platform.reset / reset_info / enter_critical / leave_critical:
+ wire board defaults (see ls_board_platform). */
+
+ /* one-call fault entry (Cortex-M/ESP32; no-op on host) */
+ ls_arch_install_fault_handler();
+
+ ls_config_t cfg;
+ if (ls_default_config(&cfg, &s_identity, &platform) != LS_OK) return;
+ if (ls_init(&cfg) != LS_OK) return;
+
+ ${storageRegister}
+
+ ${transportBlock}
+ /* transport registration (works alongside ls_quickstart): */
+#if ${host ? "1" : "0"}
+ ls_transport_register(&s_stdout.backend);
+#else
+ if (sink) ls_transport_register(&s_uart.backend);
+#endif
+
+ ls_capture_minimal_prepare();
+ if (ls_boot() != LS_OK) return;
ls_breadcrumb("app:init");
+
+ /* guided first-crash self-test -> inject/capture/spool/decode/ok:
+ ls_verify_first_crash(&(ls_verify_report_t){0}); // selftest.h */
+ /* what is still missing? */
+ /* ls_diagnose(diags, cap); */ // board.h
+
}
+
+${host ? `int main(void) { app_init(); return 0; }` : `/* embedded: call app_init() from your board startup. */`}
`;
}
-
function cmakePatch(target: string): string {
return `${TARGET_PRESET[target] ?? TARGET_PRESET["cortex-m"]}\n`;
}
diff --git a/src/commands/verify.ts b/src/commands/verify.ts
new file mode 100644
index 0000000..9898dfd
--- /dev/null
+++ b/src/commands/verify.ts
@@ -0,0 +1,42 @@
+import { test } from "./test.js";
+import { emitJson, isJson, log } from "../lib/logger.js";
+import { header } from "../ui/banner.js";
+
+export interface VerifyOpts {
+ verbose?: boolean;
+ json?: boolean;
+ vectorsPath?: string;
+}
+
+/**
+ * Guided first-crash self-test.
+ *
+ * Latch side `ls_verify_first_crash()` (include/laststate/selftest.h)
+ * performs inject -> capture -> spool -> decode -> delivered on the
+ * target. On a host scaffold this runs the static + vector checks and
+ * prints the guided chain; on a board, build + run `laststate verify`
+ * after `laststate init` and flashing.
+ */
+export async function verify(rawOpts: VerifyOpts) {
+ if (!isJson()) {
+ header("LastState verify - guided first crash");
+ log.dim("inject -> capture -> spool -> decode -> delivered");
+ console.log("");
+ log.dim("Runs `ls_verify_first_crash()` on target + host checks. NOT hardware-tested here.");
+ console.log("");
+ }
+
+ await test({ verbose: rawOpts.verbose, json: rawOpts.json, vectorsPath: rawOpts.vectorsPath });
+
+ if (isJson() || rawOpts.json) {
+ emitJson({
+ chain: ["inject", "capture", "spool", "decode", "delivered"],
+ latch_api: "ls_verify_first_crash",
+ header: "laststate/selftest.h",
+ });
+ return;
+ }
+ console.log("");
+ log.ok("Guided chain: inject -> capture -> spool -> decode -> delivered.");
+ log.dim("On a board, run ls_verify_first_crash(&report) after ls_boot(); report.* marks each step.");
+}