test: gate the pool witness on the production readiness signal
Co-Authored-By: GPT-6 Astra <noreply@openai.com>
2 files changed,
+258
−9
internal/host/region_ha_g2_isolation_test.go+138 −0internal/host/region_ha_g2_root_fixture_test.go+120 −9
Intent
Human
<task-notification> <task-id>b3dzl3aws</task-id> <tool-use-id>toolu_01DSgg4DXK79axjYVRB15KaZ</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/5555438b-7fa2-408d-b2c4-9fa87ea89332/tasks/b3dzl3aws.output</output-file> <status>completed</status> <summary>Background command "Run F-B witness live, run 6" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/host/region_ha_g2_isolation_test.go b/internal/host/region_ha_g2_isolation_test.go| index c29c5f921..6e5461642 100644 |
| --- a/internal/host/region_ha_g2_isolation_test.go |
| +++ b/internal/host/region_ha_g2_isolation_test.go |
| @@ -4,18 +4,156 @@ package host |
| import ( |
| "bufio" |
| + "bytes" |
| "context" |
| + "encoding/json" |
| "fmt" |
| + "log" |
| "net" |
| "net/http" |
| + "os" |
| "strings" |
| "testing" |
| "time" |
| "github.com/carlosframework/platform/internal/controlsock" |
| "github.com/carlosframework/platform/internal/instancectl" |
| + "github.com/carlosframework/platform/internal/layout" |
| + "github.com/carlosframework/platform/internal/release" |
| ) |
| +func TestG2PoolIsolationProductionVerdict(t *testing.T) { |
| + resetLayoutGlobals(t) |
| + t.Setenv(layoutDefaultEnv, "nested") |
| + previousBuild := layoutBuild |
| + layoutBuild = func() string { return "v0.34.7" } |
| + t.Cleanup(func() { layoutBuild = previousBuild }) |
| + var output bytes.Buffer |
| + previousWriter := log.Writer() |
| + log.SetOutput(&output) |
| + t.Cleanup(func() { log.SetOutput(previousWriter) }) |
| + store := release.NewDirStore(t.TempDir()) |
| + const invocation = "0123456789abcdef0123456789abcdef" |
| + for _, ready := range []bool{false, true, false} { |
| + var failure error |
| + if !ready { |
| + failure = fmt.Errorf("instance control: box not ready: cgroup-ro fail edge not loaded") |
| + } |
| + armDefaultGate(t, failure) |
| + output.Reset() |
| + layoutLegWith(t.Context(), store, "fixture", func(context.Context, layout.Mode, bool) []layout.Result { return nil }) |
| + raw, err := json.Marshal(map[string]string{ |
| + "MESSAGE": strings.TrimSpace(output.String()), "_SYSTEMD_INVOCATION_ID": invocation, |
| + "_PID": "42", "_UID": "990", "_SYSTEMD_UNIT": "carlos-edge.service", |
| + }) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + err = g2IsolationJournal(raw, invocation, "42", 990) |
| + if (err == nil) != ready { |
| + t.Fatalf("production gate ready=%t log=%q: %v", ready, output.String(), err) |
| + } |
| + } |
| +} |
| + |
| +func TestG2PoolIsolationPublishedSignal(t *testing.T) { |
| + const invocation = "0123456789abcdef0123456789abcdef" |
| + const ready = "layout: no intent recorded -> nested by default (CARLOS_LAYOUT_DEFAULT unset or nested; record `carlos system layout revert` to opt out)" |
| + entry := func(message, id, pid, uid, unit string) string { |
| + raw, err := json.Marshal(map[string]string{"MESSAGE": message, "_SYSTEMD_INVOCATION_ID": id, "_PID": pid, "_UID": uid, "_SYSTEMD_UNIT": unit}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return string(raw) + "\n" |
| + } |
| + good := entry("2026/11/06 12:00:00 "+ready, invocation, "42", "990", "carlos-edge.service") |
| + blocked := entry("layout: no intent recorded; nested by default is not in force -> isolation not ready: instance control: box not ready", invocation, "42", "990", "carlos-edge.service") |
| + for _, tc := range []struct { |
| + name, journal string |
| + want bool |
| + }{ |
| + {"ready", good, true}, |
| + {"pending", "", false}, |
| + {"blocked", blocked, false}, |
| + {"became ready", good + blocked, true}, |
| + {"became blocked", blocked + good, false}, |
| + {"previous invocation", entry(ready, "abcdef0123456789abcdef0123456789", "42", "990", "carlos-edge.service"), false}, |
| + {"other PID", entry(ready, invocation, "43", "990", "carlos-edge.service"), false}, |
| + {"root caller", entry(ready, invocation, "42", "0", "carlos-edge.service"), false}, |
| + {"other unit", entry(ready, invocation, "42", "990", "carlos-instance-control.service"), false}, |
| + {"malformed", good + "{", false}, |
| + {"partial message", entry("layout: no intent recorded -> nested by default", invocation, "42", "990", "carlos-edge.service"), false}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + err := g2IsolationJournal([]byte(tc.journal), invocation, "42", 990) |
| + if (err == nil) != tc.want { |
| + t.Fatalf("ready=%t, want %t: %v", err == nil, tc.want, err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestG2PoolIsolationPublishedProcess(t *testing.T) { |
| + const invocation = "0123456789abcdef0123456789abcdef" |
| + state := "ActiveState=active\nMainPID=42\nInvocationID=" + invocation + "\n" |
| + ready := `{"MESSAGE":"layout: no intent recorded -> nested by default (CARLOS_LAYOUT_DEFAULT unset or nested; record ` + "`carlos system layout revert`" + ` to opt out)","_SYSTEMD_INVOCATION_ID":"` + invocation + `","_SYSTEMD_UNIT":"carlos-edge.service","_PID":"42","_UID":"990"}` |
| + for _, mode := range []string{"ready", "inactive", "missing PID", "missing invocation", "restarted", "journal error", "cancelled"} { |
| + t.Run(mode, func(t *testing.T) { |
| + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| + defer cancel() |
| + states := 0 |
| + run := func(ctx context.Context, name string, args ...string) ([]byte, error) { |
| + if _, ok := ctx.Deadline(); !ok { |
| + t.Fatal("missing command deadline") |
| + } |
| + if name == "systemctl" { |
| + states++ |
| + switch mode { |
| + case "inactive": |
| + return []byte(strings.ReplaceAll(state, "=active", "=inactive")), nil |
| + case "missing PID": |
| + return []byte(strings.ReplaceAll(state, "MainPID=42", "MainPID=0")), nil |
| + case "missing invocation": |
| + return []byte(strings.ReplaceAll(state, invocation, "")), nil |
| + case "restarted": |
| + if states == 2 { |
| + return []byte(strings.ReplaceAll(state, "MainPID=42", "MainPID=43")), nil |
| + } |
| + } |
| + return []byte(state), nil |
| + } |
| + if name != "journalctl" || !strings.Contains(strings.Join(args, " "), "_SYSTEMD_INVOCATION_ID="+invocation) || !strings.Contains(strings.Join(args, " "), "--reverse") { |
| + t.Fatalf("unexpected command: %s %v", name, args) |
| + } |
| + if mode == "journal error" { |
| + return nil, fmt.Errorf("journal unavailable") |
| + } |
| + if mode == "cancelled" { |
| + cancel() |
| + } |
| + return []byte(ready), nil |
| + } |
| + err := g2PublishedIsolation(ctx, 990, run) |
| + if (err == nil) != (mode == "ready") { |
| + t.Fatalf("%s: %v", mode, err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestG2PoolIsolationSocketActivation(t *testing.T) { |
| + raw, err := os.ReadFile("region_ha_g2_root_fixture_test.go") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if strings.Contains(string(raw), `"start", "carlos-instance-control.socket", "carlos-instance-control.service"`) { |
| + t.Fatal("fixture starts control before the edge exists instead of using production socket activation") |
| + } |
| + if strings.Contains(string(raw), "instancectl.NewClient(controlsock.Path).Ready") { |
| + t.Fatal("root fixture dials a socket that requires the edge MainPID") |
| + } |
| +} |
| + |
| func TestG2PoolIsolationReadiness(t *testing.T) { |
| client := instancectl.NewClient(controlsock.Path) |
| calls := 0 |
diff --git a/internal/host/region_ha_g2_root_fixture_test.go b/internal/host/region_ha_g2_root_fixture_test.go| index 83bfdffab..fb6074297 100644 |
| --- a/internal/host/region_ha_g2_root_fixture_test.go |
| +++ b/internal/host/region_ha_g2_root_fixture_test.go |
| @@ -42,7 +42,6 @@ import ( |
| "github.com/aws/aws-sdk-go-v2/aws" |
| "github.com/aws/aws-sdk-go-v2/credentials" |
| "github.com/aws/aws-sdk-go-v2/service/s3" |
| - "github.com/carlosframework/platform/internal/controlsock" |
| "github.com/carlosframework/platform/internal/hafresh" |
| "github.com/carlosframework/platform/internal/instancectl" |
| "github.com/carlosframework/platform/internal/provision" |
| @@ -93,11 +92,12 @@ type g2ProbeSample struct { |
| } |
| type g2IsolationEvidence struct { |
| - Role, IID, Action string |
| - StartedAt time.Time `json:"started_at"` |
| - Elapsed time.Duration `json:"elapsed_ns"` |
| - Ready bool `json:"ready"` |
| - History []g2ProbeSample `json:"history"` |
| + Role, IID, Action string |
| + Source, AgentState string |
| + StartedAt time.Time `json:"started_at"` |
| + Elapsed time.Duration `json:"elapsed_ns"` |
| + Ready bool `json:"ready"` |
| + History []g2ProbeSample `json:"history"` |
| } |
| func g2WaitIsolation(ctx context.Context, interval time.Duration, ready func(context.Context) error) (g2IsolationEvidence, error) { |
| @@ -135,6 +135,85 @@ func g2WaitIsolation(ctx context.Context, interval time.Duration, ready func(con |
| return evidence, fmt.Errorf("%s: %v; %s", g2EnvironmentNotReady, ctx.Err(), raw) |
| } |
| +func g2IsolationJournal(raw []byte, invocation, pid string, uid int) error { |
| + const passed = "layout: no intent recorded -> nested by default (CARLOS_LAYOUT_DEFAULT unset or nested; record `carlos system layout revert` to opt out)" |
| + const blocked = "layout: no intent recorded; nested by default is not in force -> " |
| + verdict := fmt.Errorf("no isolation verdict from edge invocation %s pid %s", invocation, pid) |
| + seen := false |
| + dec := json.NewDecoder(bytes.NewReader(raw)) |
| + for { |
| + var entry struct { |
| + Message string `json:"MESSAGE"` |
| + Invocation string `json:"_SYSTEMD_INVOCATION_ID"` |
| + PID string `json:"_PID"` |
| + UID string `json:"_UID"` |
| + Unit string `json:"_SYSTEMD_UNIT"` |
| + } |
| + if err := dec.Decode(&entry); errors.Is(err, io.EOF) { |
| + return verdict |
| + } else if err != nil { |
| + return fmt.Errorf("isolation journal: %w", err) |
| + } |
| + if entry.Invocation != invocation || entry.PID != pid || entry.UID != strconv.Itoa(uid) || entry.Unit != "carlos-edge.service" { |
| + continue |
| + } |
| + // journalctl --reverse puts the current verdict before older ones. |
| + if seen { |
| + continue |
| + } |
| + message := entry.Message |
| + if len(message) >= 20 { |
| + if _, err := time.Parse("2006/01/02 15:04:05 ", message[:20]); err == nil { |
| + message = message[20:] |
| + } |
| + } |
| + switch { |
| + case message == passed: |
| + verdict, seen = nil, true |
| + case strings.HasPrefix(message, blocked): |
| + verdict, seen = errors.New(message), true |
| + } |
| + } |
| +} |
| + |
| +func g2PublishedIsolation(ctx context.Context, uid int, run func(context.Context, string, ...string) ([]byte, error)) error { |
| + state := func() ([]byte, error) { |
| + return run(ctx, "systemctl", "show", "carlos-edge.service", "--property=ActiveState", "--property=MainPID", "--property=InvocationID") |
| + } |
| + before, err := state() |
| + if err != nil { |
| + return fmt.Errorf("edge state: %w", err) |
| + } |
| + props := map[string]string{} |
| + for _, line := range strings.Split(string(before), "\n") { |
| + if key, value, ok := strings.Cut(line, "="); ok { |
| + props[key] = value |
| + } |
| + } |
| + pid, err := strconv.Atoi(props["MainPID"]) |
| + id, idErr := hex.DecodeString(props["InvocationID"]) |
| + if props["ActiveState"] != "active" || err != nil || pid <= 1 || idErr != nil || len(id) != 16 { |
| + return fmt.Errorf("edge has no active invocation: %s", before) |
| + } |
| + raw, err := run(ctx, "journalctl", "--no-pager", "--all", "--output=json", "--reverse", "-n", "200", |
| + "_SYSTEMD_UNIT=carlos-edge.service", "_SYSTEMD_INVOCATION_ID="+props["InvocationID"], |
| + "_PID="+props["MainPID"], "_UID="+strconv.Itoa(uid), "--grep=layout: no intent recorded") |
| + if err != nil { |
| + return fmt.Errorf("edge journal: %w", err) |
| + } |
| + if err := g2IsolationJournal(raw, props["InvocationID"], props["MainPID"], uid); err != nil { |
| + return err |
| + } |
| + after, err := state() |
| + if err != nil { |
| + return fmt.Errorf("edge state after journal: %w", err) |
| + } |
| + if !bytes.Equal(before, after) { |
| + return fmt.Errorf("edge changed while reading isolation verdict: %s -> %s", before, after) |
| + } |
| + return ctx.Err() |
| +} |
| + |
| type g2Box struct { |
| t *testing.T |
| role, iid, ip string |
| @@ -355,7 +434,8 @@ END;`) |
| } |
| } |
| b.seedRows() |
| - g2Command(t, "systemctl", "start", "carlos-instance-control.socket", "carlos-instance-control.service") |
| + // Bootstrap arms the socket; the edge's first control request starts the service. |
| + g2Command(t, "systemctl", "start", "carlos-instance-control.socket") |
| return b |
| } |
| func (b *g2Box) writeEnv() { |
| @@ -410,8 +490,39 @@ func (b *g2Box) listening() { |
| func (b *g2Box) isolationReady(action string) error { |
| ctx, cancel := context.WithTimeout(context.Background(), g2IsolationTimeout) |
| defer cancel() |
| - // This is the same /ready verdict used by the activator's NestedOK gate. |
| - evidence, err := g2WaitIsolation(ctx, time.Second, instancectl.NewClient(controlsock.Path).Ready) |
| + // Only the edge MainPID can query /ready. Its layout verdict records the |
| + // same NestedOK gate, including mount, self-check and control-build checks. |
| + var agentState string |
| + run := func(ctx context.Context, name string, args ...string) ([]byte, error) { |
| + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| + defer cancel() |
| + cmd := exec.CommandContext(ctx, name, args...) |
| + cmd.Dir = g2Root |
| + out, err := cmd.Output() |
| + if name == "systemctl" { |
| + agentState = string(out) |
| + } |
| + return out, err |
| + } |
| + client := s3.New(s3.Options{Region: "eu-west-1", Credentials: credentials.NewStaticCredentialsProvider(g2Access, g2Secret, ""), BaseEndpoint: aws.String(b.env["CARLOS_S3_ENDPOINT"]), UsePathStyle: true, RequestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired, ResponseChecksumValidation: aws.ResponseChecksumValidationWhenRequired}) |
| + store := &release.S3Store{Client: client, Bucket: g2Deploy} |
| + var lastBump time.Time |
| + evidence, err := g2WaitIsolation(ctx, time.Second, func(ctx context.Context) error { |
| + err := g2PublishedIsolation(ctx, b.uid, run) |
| + // The layout leg retries on a serial change or after ten minutes. |
| + // Wake that existing leg through the fixture's MinIO deployment store. |
| + if err != nil && ctx.Err() == nil && time.Since(lastBump) >= 5*time.Second { |
| + lastBump = time.Now() |
| + bumpCtx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| + bumpErr := release.BumpSerial(bumpCtx, store, lastBump) |
| + cancel() |
| + if bumpErr != nil { |
| + return fmt.Errorf("%v; fixture serial: %w", err, bumpErr) |
| + } |
| + } |
| + return err |
| + }) |
| + evidence.Source, evidence.AgentState = "edge journal: defaultLayoutIntent -> NestedOK -> instancectl.Client.Ready", agentState |
| evidence.Role, evidence.IID, evidence.Action = b.role, b.iid, action |
| b.isolation = append(b.isolation, evidence) |
| b.t.Logf("ISOLATION_READINESS %s", g2JSON(b.t, evidence)) |