carlos explain: what a block page's reference code says
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
4 files changed,
+475
−0
approved_edge_protect_copy_test.go+194 −0explain_cmd.go+131 −0explain_cmd_test.go+147 −0main.go+3 −0
CI failed — run details
diff --git a/approved_edge_protect_copy_test.go b/approved_edge_protect_copy_test.go| new file mode 100644 |
| index 000000000..d30ca2af5 |
| --- /dev/null |
| +++ b/approved_edge_protect_copy_test.go |
| @@ -0,0 +1,194 @@ |
| +// approved_edge_protect_copy_test.go |
| +package main |
| + |
| +import ( |
| + "fmt" |
| + "os" |
| + "regexp" |
| + "strconv" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// The copy gate of edge protection Plan 2 (the block page, `carlos explain` |
| +// and its docs), over docs/superpowers/plans/2026-10-25-edge-protection-2-copy.md. |
| +// It reuses the moves gate's parser and checks, as the db region forward |
| +// gate does: |
| +// |
| +// - TestEdgeProtectCopyRecordIsComplete: the record names exactly |
| +// edgeProtectCopyIDs; |
| +// - TestEdgeProtectCopyHasNoEmDash; |
| +// - TestEdgeProtectBuiltCopyIsApproved: an id in edgeProtectCopyBuilt is an |
| +// approved row. No pending string reaches docs as approved text; |
| +// - TestEdgeProtectBuiltCopyIsApplied: a built row's text is in the file it |
| +// names; |
| +// - TestEdgeProtectWiredCopyIsPendingAndApplied: a wired id is a pending, |
| +// non-docs row whose text is in its file; |
| +// - TestEdgeProtectCopyAccounted: every id but the docs row is built or |
| +// wired, once. |
| +const edgeProtectCopyRecordPath = "docs/superpowers/plans/2026-10-25-edge-protection-2-copy.md" |
| + |
| +var edgeProtectCopyIDPattern = regexp.MustCompile(`^EP-[0-9]+$`) |
| + |
| +// edgeProtectDocsID is the one docs row. Docs are public the moment the site |
| +// syncs, so the row is written only once approved, and never wired. |
| +const edgeProtectDocsID = "EP-3" |
| + |
| +// edgeProtectCopyIDs is the record's inventory, pinned: EP-1 to EP-13. |
| +var edgeProtectCopyIDs = func() []string { |
| + ids := make([]string, 0, 13) |
| + for i := 1; i <= 13; i++ { |
| + ids = append(ids, "EP-"+strconv.Itoa(i)) |
| + } |
| + return ids |
| +}() |
| + |
| +// edgeProtectCopyBuilt is every id whose approved text the code or docs |
| +// carry. The task that writes a row's text adds its id here in the same |
| +// commit, once Paul has approved the row. It is empty while the copy review |
| +// is still to come, so the two built-copy tests run over nothing until then. |
| +var edgeProtectCopyBuilt = []string{} |
| + |
| +// edgeProtectCopyWired is every id whose PENDING text the code already |
| +// carries, so the branch can be built and tested before the copy review. |
| +// The branch does not merge until every row is approved and built. The |
| +// review moves each id from here to edgeProtectCopyBuilt, applying Paul's |
| +// edits in the same commit. EP-3 is a docs row and is never wired. |
| +var edgeProtectCopyWired = []string{ |
| + "EP-1", // the block page, internal/edge/guard.go |
| + "EP-2", "EP-4", "EP-5", "EP-6", "EP-7", "EP-8", "EP-9", "EP-10", "EP-11", "EP-12", "EP-13", // carlos explain |
| +} |
| + |
| +func mustParseEdgeProtectRecord(t *testing.T) []copyRow { |
| + t.Helper() |
| + raw, err := os.ReadFile(edgeProtectCopyRecordPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + rows, err := parseCopyTable(string(raw), edgeProtectCopyRecordPath, edgeProtectCopyIDPattern, "an EP-<n>") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return rows |
| +} |
| + |
| +func TestEdgeProtectCopyRecordIsComplete(t *testing.T) { |
| + reportAll(t, checkMovesInventory(mustParseEdgeProtectRecord(t), edgeProtectCopyIDs)) |
| +} |
| + |
| +func TestEdgeProtectCopyHasNoEmDash(t *testing.T) { |
| + reportAll(t, checkMovesNoEmDash(mustParseEdgeProtectRecord(t))) |
| +} |
| + |
| +func TestEdgeProtectBuiltCopyIsApproved(t *testing.T) { |
| + reportAll(t, checkRunBuilt(mustParseEdgeProtectRecord(t), edgeProtectCopyBuilt)) |
| +} |
| + |
| +func TestEdgeProtectBuiltCopyIsApplied(t *testing.T) { |
| + rows := builtRows(mustParseEdgeProtectRecord(t), edgeProtectCopyBuilt) |
| + for i := range rows { |
| + rows[i].Text = strings.ReplaceAll(rows[i].Text, `\n`, "\n") |
| + } |
| + reportAll(t, checkRunApplied(rows, ".")) |
| +} |
| + |
| +// checkEdgeProtectWired: a wired id is a pending row the record has, named |
| +// once, not built as well, not a docs row, and its text is in the file it |
| +// names (so the review's edits must reach the code). |
| +func checkEdgeProtectWired(rows []copyRow, wired, built []string) []string { |
| + byID := map[string]copyRow{} |
| + for _, r := range rows { |
| + byID[r.ID] = r |
| + } |
| + isBuilt := map[string]bool{} |
| + for _, id := range built { |
| + isBuilt[id] = true |
| + } |
| + var out []string |
| + var apply []copyRow |
| + seen := map[string]bool{} |
| + for _, id := range wired { |
| + if seen[id] { |
| + out = append(out, fmt.Sprintf("edgeProtectCopyWired names %s twice", id)) |
| + } |
| + seen[id] = true |
| + r, ok := byID[id] |
| + switch { |
| + case !ok: |
| + out = append(out, fmt.Sprintf("edgeProtectCopyWired names %s, which the record has no row for", id)) |
| + continue |
| + case isBuilt[id]: |
| + out = append(out, fmt.Sprintf("%s is both wired and built: an approved row moves to edgeProtectCopyBuilt", id)) |
| + case r.Status != "pending": |
| + out = append(out, fmt.Sprintf("%s is %s: an approved row moves from edgeProtectCopyWired to edgeProtectCopyBuilt", id, r.Status)) |
| + case strings.HasPrefix(r.Path(), "docs/"): |
| + out = append(out, fmt.Sprintf("%s is a docs row: pending docs text is never wired", id)) |
| + } |
| + r.Status = "approved" // checkMovesApplied reads approved rows only |
| + apply = append(apply, r) |
| + } |
| + return append(out, checkMovesApplied(apply, ".")...) |
| +} |
| + |
| +func TestEdgeProtectWiredCopyIsPendingAndApplied(t *testing.T) { |
| + rows := mustParseEdgeProtectRecord(t) |
| + for i := range rows { |
| + rows[i].Text = strings.ReplaceAll(rows[i].Text, `\n`, "\n") |
| + } |
| + reportAll(t, checkEdgeProtectWired(rows, edgeProtectCopyWired, edgeProtectCopyBuilt)) |
| +} |
| + |
| +// checkEdgeProtectAccounted: every id in the inventory but the docs row is in |
| +// exactly one of built and wired, and the docs row is in neither while it is |
| +// pending (Task 6 writes it, once approved, and adds it to built). |
| +func checkEdgeProtectAccounted(ids, built, wired []string) []string { |
| + count := map[string]int{} |
| + for _, id := range built { |
| + count[id]++ |
| + } |
| + for _, id := range wired { |
| + count[id]++ |
| + } |
| + var out []string |
| + for _, id := range ids { |
| + if id == edgeProtectDocsID { |
| + if n := count[id]; n > 1 { |
| + out = append(out, fmt.Sprintf("%s is named %d times in built and wired", id, n)) |
| + } |
| + continue |
| + } |
| + if n := count[id]; n != 1 { |
| + out = append(out, fmt.Sprintf("%s is named %d times across edgeProtectCopyBuilt and edgeProtectCopyWired, want exactly 1", id, n)) |
| + } |
| + } |
| + return out |
| +} |
| + |
| +func TestEdgeProtectCopyAccounted(t *testing.T) { |
| + reportAll(t, checkEdgeProtectAccounted(edgeProtectCopyIDs, edgeProtectCopyBuilt, edgeProtectCopyWired)) |
| +} |
| + |
| +// The gate bites: a wired row whose text is not in its file, an approved row |
| +// left wired, and a docs row wired are each refused; an id in neither list |
| +// and an id in both are each refused by the accounting. |
| +func TestEdgeProtectGatesRefuse(t *testing.T) { |
| + rows := []copyRow{ |
| + {"EP-1", "internal/edge/guard.go", "nothing like this sentence is in the file.", "pending"}, |
| + {"EP-2", "internal/edge/guard.go", "x", "approved"}, |
| + {"EP-3", "docs/site/cli.md", "x", "pending"}, |
| + } |
| + got := checkEdgeProtectWired(rows, []string{"EP-1", "EP-2", "EP-3"}, nil) |
| + for _, want := range []string{"does not contain the approved text", "EP-2 is approved", "EP-3 is a docs row"} { |
| + found := false |
| + for _, g := range got { |
| + found = found || strings.Contains(g, want) |
| + } |
| + if !found { |
| + t.Errorf("no problem saying %q in %q", want, got) |
| + } |
| + } |
| + acc := checkEdgeProtectAccounted([]string{"EP-1", "EP-2", "EP-3"}, []string{"EP-1"}, []string{"EP-1"}) |
| + if len(acc) != 2 || !strings.Contains(acc[0], "EP-1") || !strings.Contains(acc[1], "EP-2") { |
| + t.Errorf("accounting problems = %q, want EP-1 twice and EP-2 missing", acc) |
| + } |
| +} |
diff --git a/explain_cmd.go b/explain_cmd.go| new file mode 100644 |
| index 000000000..14be119cd |
| --- /dev/null |
| +++ b/explain_cmd.go |
| @@ -0,0 +1,131 @@ |
| +// `carlos explain <code>`: what a reference code from a page the edge |
| +// answered says (edge protection spec, Plan 2). API mode only. Its strings |
| +// are rows EP-2 and EP-4 to EP-13 of the plan's copy record and await copy |
| +// review. |
| +package main |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "flag" |
| + "fmt" |
| + "io" |
| + "log" |
| + "net/http" |
| + "net/url" |
| + "os" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/applog" |
| +) |
| + |
| +const explainUsage = `usage: carlos explain <code> --app <a> [flags] |
| + |
| +What a reference code from a page the edge answered says: which protection rule fired, when, and the request it answered, while a record of it is kept. --json prints the console's answer as it is.` |
| + |
| +// explainNotFound is what a 404 from the console says. The console answers |
| +// the same 404 for a code that is malformed, forged or for another app, so |
| +// the message does not say which. |
| +const explainNotFound = "no reference code like that for this app. check the code, and that it came from one of this app's pages" |
| + |
| +// apiEdgeRefResponse mirrors the console's answer for one code |
| +// (internal/console/apiedgeref.go). The seal's fields are always there; Rule |
| +// is left out when the console's catalogue has no rule of that hash. |
| +type apiEdgeRefResponse struct { |
| + Code string `json:"code"` |
| + Sealed bool `json:"sealed"` |
| + Verified bool `json:"verified"` |
| + At time.Time `json:"at"` |
| + Tier string `json:"tier"` |
| + Rule string `json:"rule"` |
| + Group string `json:"group"` |
| + RuleHash string `json:"rule_hash"` |
| + RecordStatus string `json:"record_status"` |
| + Record *applog.Record `json:"record"` |
| +} |
| + |
| +func runExplain(args []string) { |
| + fs := flag.NewFlagSet("explain", flag.ExitOnError) |
| + app := appFlag(fs, "app whose credential asks") |
| + asJSON := fs.Bool("json", false, "print the console's answer as JSON") |
| + consoleFlag := consoleOption(fs, "console to ask (default: the only logged-in one)") |
| + acctRaw := fs.String("account", "", "account (sqid or name) the app belongs to (default: your only account, or the configured/chosen one)") |
| + acctIDRaw := fs.String("account-id", "", "account sqid, matched as a sqid ONLY, and supersedes --account") |
| + fs.Usage = flagUsage(fs, explainUsage) |
| + // The code leads the line in the usage text, and Go's flag package stops |
| + // at the first non-flag, so it moves after the flags like geo's address. |
| + fs.Parse(ipLast(args)) |
| + if fs.NArg() != 1 { |
| + fs.Usage() |
| + os.Exit(2) |
| + } |
| + sess := requireAPISession("explain", *consoleFlag) |
| + acct := resolvedAccount(sess, acctRaw, acctIDRaw) |
| + app2 := resolvedApp(sess, acct, *app, explainUsage) |
| + st, body, err := sess.getJSON(context.Background(), explainPath(acct, app2, fs.Arg(0))) |
| + if err != nil { |
| + log.Fatalf("explain: %v", err) |
| + } |
| + if st == http.StatusNotFound { |
| + fmt.Fprintln(os.Stderr, explainNotFound) |
| + os.Exit(1) |
| + } |
| + if st != http.StatusOK { |
| + log.Fatalf("explain: %v", apiErrorFromBody(st, body)) |
| + } |
| + if *asJSON { |
| + stdout.Write(body) |
| + fmt.Fprintln(stdout) |
| + return |
| + } |
| + var res apiEdgeRefResponse |
| + if err := json.Unmarshal(body, &res); err != nil { |
| + log.Fatalf("explain: unreadable response: %v", err) |
| + } |
| + printExplain(stdout, res) |
| +} |
| + |
| +// explainPath is the console path for one code. The code is user input, so |
| +// it goes through url.PathEscape and cannot step out of its segment. |
| +func explainPath(acct apiAcct, app, code string) string { |
| + return apiPath(acct, app, "/edge-refs/"+url.PathEscape(code)) |
| +} |
| + |
| +// explainLine writes one line of the answer. The copy record's rows carry |
| +// no trailing newline, so the line end lives here and not in each format. |
| +func explainLine(w io.Writer, format string, args ...any) { |
| + fmt.Fprintf(w, format, args...) |
| + fmt.Fprintln(w) |
| +} |
| + |
| +func printExplain(w io.Writer, res apiEdgeRefResponse) { |
| + explainLine(w, "reference %s\nwhen %s\ntier %s", res.Code, res.At.UTC().Format("2006-01-02 15:04 UTC"), res.Tier) |
| + // What the checked line says follows from the seal alone. An unsealed |
| + // code proves nothing about who wrote it, and a sealed one the console |
| + // cannot check (no key) is no better, so both tell the owner to treat |
| + // the rest as unchecked. |
| + switch { |
| + case res.Verified: |
| + explainLine(w, "checked yes. this deployment sealed the code") |
| + case res.Sealed: |
| + explainLine(w, "checked no. the console has no key to check the seal with, so treat the rest as unchecked") |
| + default: |
| + explainLine(w, "checked no. the code is not sealed, so anyone could have written it. treat the rest as unchecked") |
| + } |
| + if res.Rule != "" { |
| + explainLine(w, "rule %s", res.Rule) |
| + } else { |
| + explainLine(w, "rule not in this console's catalogue (hash %s)", res.RuleHash) |
| + } |
| + switch { |
| + case res.RecordStatus == "found" && res.Record != nil: |
| + f := res.Record.Fields |
| + explainLine(w, "request %s %s on %s, answered %s", f["method"], f["path"], res.Record.Host, f["status"]) |
| + case res.RecordStatus == "unavailable": |
| + explainLine(w, "request the records could not be read just now. try again in a minute") |
| + default: |
| + // "absent", and a "found" that carries no record or a status this |
| + // binary does not know: none has a request to show. |
| + explainLine(w, "request not kept. it was sampled away, or is older than 7 days") |
| + } |
| +} |
diff --git a/explain_cmd_test.go b/explain_cmd_test.go| new file mode 100644 |
| index 000000000..b954bc832 |
| --- /dev/null |
| +++ b/explain_cmd_test.go |
| @@ -0,0 +1,147 @@ |
| +package main |
| + |
| +import ( |
| + "bytes" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/applog" |
| +) |
| + |
| +// epText returns one row of the edge protection copy record, its line |
| +// breaks decoded and its placeholders filled, so the expected output of |
| +// printExplain is built from the record rather than retyped here. |
| +func epText(t *testing.T, id string, fill ...string) string { |
| + t.Helper() |
| + for _, r := range mustParseEdgeProtectRecord(t) { |
| + if r.ID == id { |
| + return strings.NewReplacer(fill...).Replace(strings.ReplaceAll(r.Text, `\n`, "\n")) |
| + } |
| + } |
| + t.Fatalf("copy record has no row %s", id) |
| + return "" |
| +} |
| + |
| +const explainTestCode = "abcdefghijklmnopqrstuvwxyz0123456789abcd" |
| + |
| +func explainResponse() apiEdgeRefResponse { |
| + return apiEdgeRefResponse{ |
| + Code: explainTestCode, |
| + Verified: true, |
| + Sealed: true, |
| + At: time.Date(2026, 10, 25, 14, 7, 31, 0, time.FixedZone("x", 3600)), |
| + Tier: "hard", |
| + Rule: "hard.secrets.env", |
| + Group: "secrets", |
| + RuleHash: "0a1b2c3d", |
| + RecordStatus: "found", |
| + Record: &applog.Record{ |
| + Host: "shop.example.com", |
| + Fields: map[string]string{"method": "GET", "path": "/.env", "status": "404"}, |
| + }, |
| + } |
| +} |
| + |
| +func explainOutput(res apiEdgeRefResponse) string { |
| + var out bytes.Buffer |
| + printExplain(&out, res) |
| + return out.String() |
| +} |
| + |
| +// The header names the code, the time in UTC and the tier. The response's |
| +// time is 14:07 at +01:00, so the line must say 13:07. |
| +func explainHeader(t *testing.T) string { |
| + return epText(t, "EP-8", "<code>", explainTestCode, "<time>", "2026-10-25 13:07 UTC", "<tier>", "hard") + "\n" |
| +} |
| + |
| +func explainRuleLine(t *testing.T) string { |
| + return epText(t, "EP-13", "<rule>", "hard.secrets.env") + "\n" |
| +} |
| + |
| +func explainRequestLine(t *testing.T) string { |
| + return epText(t, "EP-9", "<method>", "GET", "<path>", "/.env", "<host>", "shop.example.com", "<status>", "404") + "\n" |
| +} |
| + |
| +func TestPrintExplainVerifiedFound(t *testing.T) { |
| + want := explainHeader(t) + epText(t, "EP-4") + "\n" + explainRuleLine(t) + explainRequestLine(t) |
| + if got := explainOutput(explainResponse()); got != want { |
| + t.Fatalf("output:\n%s\nwant:\n%s", got, want) |
| + } |
| +} |
| + |
| +func TestPrintExplainSealedUnverified(t *testing.T) { |
| + res := explainResponse() |
| + res.Verified = false |
| + want := explainHeader(t) + epText(t, "EP-5") + "\n" + explainRuleLine(t) + explainRequestLine(t) |
| + if got := explainOutput(res); got != want { |
| + t.Fatalf("output:\n%s\nwant:\n%s", got, want) |
| + } |
| +} |
| + |
| +func TestPrintExplainUnsealed(t *testing.T) { |
| + res := explainResponse() |
| + res.Verified, res.Sealed = false, false |
| + want := explainHeader(t) + epText(t, "EP-6") + "\n" + explainRuleLine(t) + explainRequestLine(t) |
| + if got := explainOutput(res); got != want { |
| + t.Fatalf("output:\n%s\nwant:\n%s", got, want) |
| + } |
| +} |
| + |
| +func TestPrintExplainUnknownRule(t *testing.T) { |
| + res := explainResponse() |
| + res.Rule, res.Group = "", "" |
| + want := explainHeader(t) + epText(t, "EP-4") + "\n" + |
| + epText(t, "EP-7", "<hash>", "0a1b2c3d") + "\n" + explainRequestLine(t) |
| + if got := explainOutput(res); got != want { |
| + t.Fatalf("output:\n%s\nwant:\n%s", got, want) |
| + } |
| +} |
| + |
| +func TestPrintExplainAbsent(t *testing.T) { |
| + res := explainResponse() |
| + res.RecordStatus, res.Record = "absent", nil |
| + want := explainHeader(t) + epText(t, "EP-4") + "\n" + explainRuleLine(t) + epText(t, "EP-10") + "\n" |
| + if got := explainOutput(res); got != want { |
| + t.Fatalf("output:\n%s\nwant:\n%s", got, want) |
| + } |
| +} |
| + |
| +func TestPrintExplainUnavailable(t *testing.T) { |
| + res := explainResponse() |
| + res.RecordStatus, res.Record = "unavailable", nil |
| + want := explainHeader(t) + epText(t, "EP-4") + "\n" + explainRuleLine(t) + epText(t, "EP-11") + "\n" |
| + if got := explainOutput(res); got != want { |
| + t.Fatalf("output:\n%s\nwant:\n%s", got, want) |
| + } |
| +} |
| + |
| +// A console that says "found" without a record must not make a request |
| +// line out of empty fields. |
| +func TestPrintExplainFoundWithoutRecord(t *testing.T) { |
| + res := explainResponse() |
| + res.Record = nil |
| + if got := explainOutput(res); !strings.HasSuffix(got, epText(t, "EP-10")+"\n") { |
| + t.Fatalf("output:\n%s", got) |
| + } |
| +} |
| + |
| +// The usage text is EP-2. |
| +func TestExplainUsageIsTheRecordsText(t *testing.T) { |
| + if got, want := strings.TrimRight(explainUsage, "\n"), epText(t, "EP-2"); got != want { |
| + t.Fatalf("usage:\n%s\nwant:\n%s", got, want) |
| + } |
| +} |
| + |
| +func TestExplainNotFoundText(t *testing.T) { |
| + if got, want := explainNotFound, epText(t, "EP-12"); got != want { |
| + t.Fatalf("not found = %q, want %q", got, want) |
| + } |
| +} |
| + |
| +func TestExplainPathEscapesTheCode(t *testing.T) { |
| + got := explainPath(apiAcct{Seg: "acct"}, "shop", "a/b c") |
| + if want := "/api/cli/apps/acct/shop/edge-refs/a%2Fb%20c"; got != want { |
| + t.Fatalf("path = %q, want %q", got, want) |
| + } |
| +} |
diff --git a/main.go b/main.go| index 04f1fab6d..65963c4d6 100644 |
| --- a/main.go |
| +++ b/main.go |
| @@ -116,6 +116,8 @@ func main() { |
| runAvailability(args) |
| case "geo": |
| runGeo(args) |
| + case "explain": |
| + runExplain(args) |
| case "analytics": |
| runAnalytics(args) |
| case "channels": |
| @@ -290,6 +292,7 @@ Your apps — ship and run software on a CARLOS deployment |
| analytics an app's page views and visitors from a terminal, counted at |
| the edge as it serves your app |
| geo what the platform's GeoIP database says about an address |
| + explain what a reference code from a page the edge answered says |
| ledger open ledgers: append | head | list | publish | blob | verify |
| vet check a release against the platform contract |
| update update this carlos binary to the latest release (or name |