feat(v4, ci): the fault dump on a node's stop, and the EMERGENCY diagnostic build after a failed stage

Captain Bob's CI directive (2026-11-17) and the design he ruled point by point
(docs/v4.0.0/plans/2026-11-17-emergency-diagnostic-build.md).

- v4/system/dump.c, v4/include/v4/dump.h: what a node was doing when it stopped -- the fault's kind,
  address and count; P A B; the data stack, T first; the return stack, R first, each entry named by
  the word it falls in (the backtrace; ROM and below-every-word said so); the running line. Bounded
  dictionary walk; no C library. Ruled: every build, on a stop only (a handled fault keeps its line).
- kernel/src/v4/sk_v4.c: the dump before each of the three halts (not started; POST's blocks not
  let go; a node stopped on a fault) -- Hera and any stopped node. kernel/Makefile links dump.o.
- scripts/nightly.sh diagnose [<stage>]: after a failed stage, that configuration built again with
  EMERGENCY_CONSOLE_ENABLED=1 -- build only, nothing booted (ruled) -- its artifacts kept in
  logs/<TS>/diagnose/ only from a build that passed, with a README saying how to launch it with
  --log-level=debug. Always exits 0: evidence, never a result. The protocol runs it after a failed
  stage and still exits with that stage's code; acceptance.yml, nightly.yml and release.yml run it
  `if: failure()`, before the logs are kept. Any failed stage triggers it (ruled).
- capsules/BLOCK_MAP.md: as the build writes it -- the v4:physics.4th row, missing since 28ffed8f
  (ruled: commit the regenerated map).
- Test first: test_dump.c failed with no dump.c (logs/20261117-dump), then 15 checks at both widths
  and under ASan+UBSan. The diagnose stage: a real EMERGENCY amd64 build; a failing build exits 0 and
  keeps no artifacts (a defect found and fixed this way); the protocol path keeps the stage's exit.
- Also the write-up of the regression at 046ce0be, for ruling.

Not yet: acceptance on a clean tree (the next run); the kernel's stop wiring is compile-tested only
-- no boot reaches a stop on purpose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm
This commit is contained in:
Claude
2026-11-17 19:14:40 +00:00
parent 3376255930
commit 45b11aec25
13 changed files with 632 additions and 7 deletions
+7 -4
View File
@@ -91,6 +91,9 @@ $(BINDIR)/san-v3blocks.o: $(V3_BLOCK_SRCS) $(wildcard $(ROOT_DIR)/v3/include/*.h
# docs/v4.0.0/NUCLEUS.md 6.3), which are the kernel's too.
blocks_src = $(if $(call uses_blocks,$(1)),$(BLOCKS_SRC) $(HERE)/system/post.c $(HERE)/system/post_cases.c)
blocks_objs = $(if $(call uses_blocks,$(1)),$(BINDIR)/v3blocks.o)
# the fault dump of a node that stopped (system/dump.c; plan 2026-11-17-emergency-diagnostic-build): the
# kernel's, linked with its own test
dump_src = $(if $(findstring /test_dump,$(1)),$(HERE)/system/dump.c)
san_blocks_objs = $(if $(call uses_blocks,$(1)),$(BINDIR)/san-v3blocks.o)
# The system's source, capsules/v4/sys (MACHINE.md 3): F18 assembler as text
@@ -318,16 +321,16 @@ endif
#
# Both binaries depend on this Makefile, so a flag change rebuilds them.
define TEST_RULE
$(BINDIR)/$(1)-$(notdir $(2)): $(2) $$(SRCS) $(call blocks_src,$(2)) $(call blocks_objs,$(2)) $(call rom_src,$(2),$(1)) $$(wildcard $(HERE)/include/v4/*.h) $$(wildcard $(HERE)/tests/*.h) $(HERE)/Makefile
$(BINDIR)/$(1)-$(notdir $(2)): $(2) $$(SRCS) $(call blocks_src,$(2)) $(call dump_src,$(2)) $(call blocks_objs,$(2)) $(call rom_src,$(2),$(1)) $$(wildcard $(HERE)/include/v4/*.h) $$(wildcard $(HERE)/tests/*.h) $(HERE)/Makefile
@mkdir -p $(BINDIR)
$$(CC) $$(CFLAGS) -I$(HERE)/include $(call blocks_inc,$(2)) -DV4_CELL_BITS=$(1) $(call node_size,$(2)) $(capsule_dir) $(call rom_sym,$(2),$(1)) \
$(2) $$(SRCS) $(call blocks_src,$(2)) $(call blocks_objs,$(2)) $(call rom_src,$(2),$(1)) -o $$@
$(2) $$(SRCS) $(call blocks_src,$(2)) $(call dump_src,$(2)) $(call blocks_objs,$(2)) $(call rom_src,$(2),$(1)) -o $$@
$(BINDIR)/san-$(1)-$(notdir $(2)): $(2) $$(SRCS) $(call blocks_src,$(2)) $(call san_blocks_objs,$(2)) $(call rom_src,$(2),$(1)) $$(wildcard $(HERE)/include/v4/*.h) $$(wildcard $(HERE)/tests/*.h) $(HERE)/Makefile
$(BINDIR)/san-$(1)-$(notdir $(2)): $(2) $$(SRCS) $(call blocks_src,$(2)) $(call dump_src,$(2)) $(call san_blocks_objs,$(2)) $(call rom_src,$(2),$(1)) $$(wildcard $(HERE)/include/v4/*.h) $$(wildcard $(HERE)/tests/*.h) $(HERE)/Makefile
@mkdir -p $(BINDIR)
$$(CC) $$(CSTD) $$(WARN) -O1 -g -fno-omit-frame-pointer \
-fsanitize=address,undefined -fno-sanitize-recover=all -I$(HERE)/include $(call blocks_inc,$(2)) -DV4_CELL_BITS=$(1) $(call node_size,$(2)) $(capsule_dir) $(call rom_sym,$(2),$(1)) \
$(2) $$(SRCS) $(call blocks_src,$(2)) $(call san_blocks_objs,$(2)) $(call rom_src,$(2),$(1)) -o $$@
$(2) $$(SRCS) $(call blocks_src,$(2)) $(call dump_src,$(2)) $(call san_blocks_objs,$(2)) $(call rom_src,$(2),$(1)) -o $$@
endef
# One run target per (width, test) and one prerequisite line per width, so
+32
View File
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* dump.h -- what a node was doing when it stopped (docs/v4.0.0/plans/2026-11-17-emergency-diagnostic-build.md;
* ruled by Captain Bob, 2026-11-17: in every build, on a stop only -- a fault the node's own handler takes
* keeps its one-line message).
*
* The dump says:
* - the latest fault's kind, its address for an address fault, how many there have been, and whether
* the node is stopped;
* - P, A and B;
* - the data stack, T first, every value it holds;
* - the return stack, R first, each entry named by the dictionary word whose code it falls in: the
* node's backtrace (an address in the ROM says so, and one below every word says so);
* - the line that was running, if one was.
*
* A word is found by walking the dictionary from LATEST (dict.f18: the link at xt-1, the name's word
* address at xt-2, its count byte first); the walk is bounded, so a damaged dictionary cannot hold the
* dump. An address is named by the newest word whose xt is at or below it: code with no header of its
* own that follows a word is named by that word. Nothing here uses the C library. */
#ifndef V4_DUMP_H
#define V4_DUMP_H
#include "v4/node.h"
#include "v4/image.h"
typedef void (*v4_dump_out)(void *self, const char *s, unsigned n);
void v4_dump_node(const v4_node *n, const v4_image *im, const char *line, unsigned len,
v4_dump_out out, void *self);
#endif /* V4_DUMP_H */
+119
View File
@@ -0,0 +1,119 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* dump.c -- what a node was doing when it stopped. See v4/dump.h. */
#include "v4/dump.h"
#include "v4/stack.h"
static void say(v4_dump_out out, void *self, const char *s)
{
unsigned n = 0;
while (s[n]) n++;
out(self, s, n);
}
static void say_num(v4_dump_out out, void *self, v4_cell v)
{
char buf[24];
unsigned i = sizeof buf;
v4_ucell u = v < 0 ? (v4_ucell)0 - (v4_ucell)v : (v4_ucell)v;
do { buf[--i] = (char)('0' + (int)(u % 10u)); u /= 10u; } while (u && i > 1);
if (v < 0) buf[--i] = '-';
out(self, buf + i, (unsigned)(sizeof buf - i));
}
static unsigned byte_at(const v4_node *n, v4_cell nfa, unsigned i)
{
v4_cell w = nfa + (v4_cell)(i / 4u);
if (w < 0 || w >= (v4_cell)V4_NODE_WORDS) return 0;
return (unsigned)(((v4_ucell)n->mem[w] >> (8u * (i % 4u))) & 0xFFu);
}
/* the newest word whose xt is at or below addr, or 0 */
static v4_cell word_of(const v4_node *n, const v4_image *im, v4_cell addr)
{
v4_cell xt;
unsigned k;
if (!im || im->latest <= 0 || im->latest >= (v4_cell)V4_NODE_WORDS) return 0;
for (xt = n->mem[im->latest], k = 0; xt > 4 && xt < (v4_cell)V4_NODE_WORDS && k < V4_NODE_WORDS;
xt = n->mem[xt - 1], k++)
if (xt <= addr) return xt;
return 0;
}
static void say_where(v4_dump_out out, void *self, const v4_node *n, const v4_image *im, v4_cell addr)
{
v4_cell xt, nfa;
unsigned len, i;
char c;
say_num(out, self, addr);
if (addr >= V4_ROM_BASE) { say(out, self, " in ROM"); return; }
xt = addr >= 0 && addr < (v4_cell)V4_NODE_WORDS ? word_of(n, im, addr) : 0;
if (xt == 0) { say(out, self, " below every word"); return; }
nfa = n->mem[xt - 2];
if (nfa < 0 || nfa >= xt) { say(out, self, " in a word with no name"); return; }
len = byte_at(n, nfa, 0);
if (len > 31u) len = 31u;
say(out, self, " in ");
for (i = 1; i <= len; i++) { c = (char)byte_at(n, nfa, i); out(self, &c, 1); }
}
static const char *kind_name(unsigned k)
{
switch (k) {
case V4_FAULT_ADDRESS: return "address fault";
case V4_FAULT_DATA_OVER: return "data overflow";
case V4_FAULT_DATA_UNDER: return "data underflow";
case V4_FAULT_RET_OVER: return "return overflow";
case V4_FAULT_RET_UNDER: return "return underflow";
case V4_FAULT_RAISED: return "error raised";
default: return "unknown fault";
}
}
void v4_dump_node(const v4_node *n, const v4_image *im, const char *line, unsigned len,
v4_dump_out out, void *self)
{
unsigned i, h;
say(out, self, "V4 DUMP: ");
if (n->faults == 0) say(out, self, "no fault");
else {
say(out, self, kind_name(n->fault_kind));
if (n->fault_kind == V4_FAULT_ADDRESS) { say(out, self, " at "); say_num(out, self, n->fault_addr); }
}
say(out, self, " faults="); say_num(out, self, (v4_cell)n->faults);
say(out, self, n->stopped ? " stopped\n" : " running\n");
say(out, self, "V4 DUMP: P="); say_num(out, self, n->p);
say(out, self, " A="); say_num(out, self, n->a);
say(out, self, " B="); say_num(out, self, n->b);
say(out, self, "\n");
say(out, self, "V4 DUMP: data stack depth="); say_num(out, self, (v4_cell)n->ds.depth); say(out, self, ":");
for (i = 0, h = n->ds.head; i < n->ds.depth; i++) {
v4_cell v;
if (i == 0) v = n->ds.t;
else if (i == 1) v = n->ds.s;
else { h = (h + V4_DATA_RING - 1u) % V4_DATA_RING; v = n->ds.ring[h]; }
say(out, self, " "); say_num(out, self, v);
}
say(out, self, "\n");
say(out, self, "V4 DUMP: return stack depth="); say_num(out, self, (v4_cell)n->rs.depth); say(out, self, ", R first:\n");
for (i = 0, h = n->rs.head; i < n->rs.depth; i++) {
v4_cell v;
if (i == 0) v = n->rs.r;
else { h = (h + V4_RET_RING - 1u) % V4_RET_RING; v = n->rs.ring[h]; }
say(out, self, "V4 DUMP: ");
say_where(out, self, n, im, v);
say(out, self, "\n");
}
if (line && len) {
say(out, self, "V4 DUMP: line: ");
out(self, line, len);
say(out, self, "\n");
}
}
+121
View File
@@ -0,0 +1,121 @@
/* SPDX-License-Identifier: LicenseRef-Starship-1.0
* Copyright (c) 2023-2026 Robert A. James
*/
/* test_dump.c -- the fault dump of a node that stopped (v4/dump.h;
* docs/v4.0.0/plans/2026-11-17-emergency-diagnostic-build.md, ruled by Captain Bob 2026-11-17:
* every build, on a stop only).
*
* A node is set by hand to a known state: registers, both stacks, a fault, and
* a dictionary of two words laid out as dict.f18 lays an entry (the name's
* word address at xt-2, the link at xt-1, the count byte first). The dump
* must say all of it: the fault's kind, address and count; P, A, B; every
* value on the data stack, T first; every return-stack entry, R first, each
* named by the word it falls in (the backtrace), the ROM's as ROM, one below
* every word as such; and the line that was running.
*
* Built and run at both V4_CELL_BITS=32 and 64; see v4/Makefile.
*/
#include "v4/dump.h"
#include "v4/node.h"
#include "v4/stack.h"
#include "v4/image.h"
#include <stdio.h>
#include <string.h>
static int failures = 0;
static int checks = 0;
#define CHECK(cond, ...) \
do { \
checks++; \
if (!(cond)) { \
failures++; \
printf(" FAIL %s:%d: ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
printf("\n"); \
} \
} while (0)
static char out[8192];
static size_t used;
static void capture(void *self, const char *s, unsigned n)
{
(void)self;
if (used + n >= sizeof out) n = (unsigned)(sizeof out - 1 - used);
memcpy(out + used, s, n);
used += n;
out[used] = 0;
}
static v4_node node;
/* an entry as dict.f18 lays one: name at nfa (count byte first, four to a cell, low byte first) */
static void entry(v4_cell xt, v4_cell nfa, v4_cell link, const char *name)
{
unsigned len = (unsigned)strlen(name), i;
for (i = 0; i <= len; i++) {
unsigned b = i == 0 ? len : (unsigned char)name[i - 1];
node.mem[nfa + (v4_cell)(i / 4u)] |= (v4_cell)((v4_ucell)b << (8u * (i % 4u)));
}
node.mem[xt - 3] = 0;
node.mem[xt - 2] = nfa;
node.mem[xt - 1] = link;
}
static int has(const char *s) { return strstr(out, s) != 0; }
int main(void)
{
v4_image im;
memset(&im, 0, sizeof im);
v4_node_reset(&node);
im.latest = 10;
entry(100, 90, 0, "ALPHA");
entry(200, 190, 100, "BETA");
node.mem[im.latest] = 200;
node.p = 123; node.a = 7; node.b = 8;
node.fault_kind = V4_FAULT_DATA_UNDER; node.fault_addr = 0; node.faults = 3; node.stopped = 1;
v4_dstack_push(&node.ds, 11);
v4_dstack_push(&node.ds, 22);
v4_dstack_push(&node.ds, 33);
v4_rstack_push(&node.rs, 50); /* below every word */
v4_rstack_push(&node.rs, (v4_cell)V4_ROM_BASE + 5); /* in the ROM */
v4_rstack_push(&node.rs, 205); /* in BETA */
v4_rstack_push(&node.rs, 150); /* in ALPHA: R */
used = 0; out[0] = 0;
v4_dump_node(&node, &im, "3 4 SPIN", 8u, capture, 0);
CHECK(has("data underflow"), "the fault's kind: %s", out);
CHECK(has("faults=3"), "the fault count: %s", out);
CHECK(has("stopped"), "it says the node stopped: %s", out);
CHECK(has("P=123") && has("A=7") && has("B=8"), "P, A, B: %s", out);
CHECK(has("data stack depth=3: 33 22 11"), "the data stack, T first: %s", out);
CHECK(has("return stack depth=4"), "the return stack's depth: %s", out);
CHECK(has("150 in ALPHA"), "R named by its word: %s", out);
CHECK(has("205 in BETA"), "the next named: %s", out);
CHECK(has("in ROM"), "a ROM address said so: %s", out);
CHECK(has("50 below every word"), "an address below the dictionary said so: %s", out);
CHECK(strstr(out, "150 in ALPHA") < strstr(out, "205 in BETA"), "R first, then down the stack: %s", out);
CHECK(has("line: 3 4 SPIN"), "the running line: %s", out);
/* an address fault says the address */
node.fault_kind = V4_FAULT_ADDRESS; node.fault_addr = 99999;
used = 0; out[0] = 0;
v4_dump_node(&node, &im, 0, 0u, capture, 0);
CHECK(has("address fault at 99999"), "the faulting address: %s", out);
CHECK(!has("line:"), "no line when none was running: %s", out);
/* a dictionary whose link goes wild does not run away: the walk is bounded */
node.mem[100 - 1] = 200; /* ALPHA links back to BETA: a loop */
used = 0; out[0] = 0;
v4_dump_node(&node, &im, 0, 0u, capture, 0);
CHECK(has("return stack depth=4"), "the dump still ends: %s", out);
printf("test_dump: %d checks, %d failures\n", checks, failures);
return failures ? 1 : 0;
}