carlos / platform Public

auditdetail: readable What for run rows and stored data

One formatter (auditWhatOf) for every feed of audit rows. run-start and
run-end rows, which store JSON, read as sentences: the command and
version; how the run ended, its exit code, duration and box, with
wording for timed out, cancelled, interrupted, refused, not picked up
and cut-off output; a service's line after it. Any other JSON object
reads as a summary. The customer console's app Activity tab and Recent
activity show the sentences in place of the raw JSON (wording only).
Copy OPS-163 to OPS-177, through Luna.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev be05dcf1a9377292f7b740c1bf91b26410367698 parent 305c510
4 files changed, +380 −2
  • internal/console/auditdetail.go +193 −0
  • internal/console/auditdetail_test.go +185 −0
  • internal/console/templates/app_activity.html +1 −1
  • internal/console/templates/app_overview.html +1 −1
diff --git a/internal/console/auditdetail.go b/internal/console/auditdetail.go
new file mode 100644
index 000000000..5aa35c694
--- /dev/null
+++ b/internal/console/auditdetail.go
@@ -0,0 +1,193 @@
+package console
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "strings"
+ "time"
+ "unicode/utf8"
+
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/runs"
+)
+
+// What an audit row says: the one formatter every feed of audit rows reads
+// its Detail through (the operator dashboard, Activity and the person page;
+// the customer console's app Activity tab and Recent activity). Most rows
+// store a sentence, written when the action happened, and show it as it is.
+// run-start and run-end store JSON (apiruns.go's runStartDetail,
+// apirunsfollow.go's auditRunEnd); they read as sentences built from it. Any
+// other row whose Detail is a JSON object reads as a short summary, and the
+// operator pages hold the data itself, indented, under it (copy record
+// 2026-10-27-operator-people-detail-copy.md, OPS-163 to OPS-177).
+
+// The words (OPS-163 to OPS-177). The run-end heads but auditRunNotPickedUp
+// are sentences the tails (after, on, truncated) are appended to.
+const (
+ auditRunStart = "Started %s (%s)"
+ auditRunStartBare = "Started %s"
+ auditRunExit = "Run ended with exit code %d"
+ auditRunSignal = "Run stopped by signal %s"
+ auditRunTimedOut = "Run timed out"
+ auditRunCancelled = "Run was cancelled"
+ auditRunInterrupted = "Run was interrupted (%s)"
+ auditRunRefused = "Run was refused (%s)"
+ auditRunNotPickedUp = "No box picked up the run"
+ auditRunEnded = "Run ended"
+ auditRunAfter = "%s after %s"
+ auditRunOn = "%s on %s"
+ auditRunTruncated = "%s. Its output was larger than 64 MB and was cut off"
+ auditRecordedAction = "Recorded %s"
+ // auditRunRequester follows a service's run sentence with the run
+ // package's own line for it (runs.Request.ServiceLine).
+ auditRunRequester = "%s. %s"
+)
+
+// The rows the formatter reads, and how it lays them out.
+const (
+ auditActionRunStart = "run-start"
+ auditActionRunEnd = "run-end"
+ // auditClipRunes is the length past which a cell that shows one line
+ // may cut Text, so Title carries the whole of it.
+ auditClipRunes = 60
+ auditJSONIndent = " "
+ // auditRunEnd's outcomes (runEndOutcome) beside provision's.
+ auditRunOutcomeLost = "lost"
+ auditRunOutcomeEnded = "result"
+)
+
+// auditWhat is how one audit row's Detail shows. Text is the sentence, the
+// summary or the stored text. Title, when set, is the whole of a Text long
+// enough that a one-line cell may cut it. JSON, when set, is the stored
+// object indented, for a <details> under Text.
+type auditWhat struct {
+ Text, Title, JSON string
+}
+
+// auditWhatOf is how the row with action and detail shows.
+func auditWhatOf(action, detail string) auditWhat {
+ var w auditWhat
+ switch text, ok := auditRunSentence(action, detail); {
+ case ok:
+ w.Text = text
+ case auditJSONObject(detail):
+ w.Text = fmt.Sprintf(auditRecordedAction, action)
+ var buf bytes.Buffer
+ if json.Indent(&buf, []byte(strings.TrimSpace(detail)), "", auditJSONIndent) == nil {
+ w.JSON = buf.String()
+ }
+ return w
+ default:
+ w.Text = detail
+ }
+ if utf8.RuneCountInString(w.Text) > auditClipRunes {
+ w.Title = w.Text
+ }
+ return w
+}
+
+// auditText is auditWhatOf's Text: the words alone, for the customer
+// console's lists, which show a row's text and nothing else.
+func auditText(action, detail string) string { return auditWhatOf(action, detail).Text }
+
+// auditJSONObject reports whether detail is one JSON object.
+func auditJSONObject(detail string) bool {
+ d := strings.TrimSpace(detail)
+ if !strings.HasPrefix(d, "{") {
+ return false
+ }
+ var v map[string]json.RawMessage
+ return json.Unmarshal([]byte(d), &v) == nil
+}
+
+// auditRunSentence is the sentence of a run-start or run-end row whose
+// Detail reads as the JSON that row stores; ok is false for any other row,
+// and for one whose Detail does not read.
+func auditRunSentence(action, detail string) (string, bool) {
+ switch action {
+ case auditActionRunStart:
+ var d struct {
+ Argv []string `json:"argv"`
+ Version string `json:"version"`
+ Requester string `json:"requester"`
+ }
+ if json.Unmarshal([]byte(detail), &d) != nil || len(d.Argv) == 0 {
+ return "", false
+ }
+ command := strings.Join(d.Argv, " ")
+ s := fmt.Sprintf(auditRunStart, command, d.Version)
+ if d.Version == "" {
+ s = fmt.Sprintf(auditRunStartBare, command)
+ }
+ return auditRunBy(s, d.Requester), true
+ case auditActionRunEnd:
+ var d struct {
+ Outcome string `json:"outcome"`
+ Result *runs.Result `json:"result"`
+ Requester string `json:"requester"`
+ }
+ if json.Unmarshal([]byte(detail), &d) != nil || d.Outcome == "" {
+ return "", false
+ }
+ return auditRunBy(auditRunEndSentence(d.Outcome, d.Result), d.Requester), true
+ }
+ return "", false
+}
+
+// auditRunBy is a run sentence followed by the service's line when a
+// service ran it ("" for a person's run, whose row's actor names them).
+func auditRunBy(sentence, requester string) string {
+ if requester == "" {
+ return sentence
+ }
+ return fmt.Sprintf(auditRunRequester, sentence, requester)
+}
+
+// auditRunEndSentence is a run-end row's sentence: how the run ended (the
+// CLI's runExit order: refused, cancelled, timed out, interrupted, a
+// signal, an exit code), how long it ran when it started and ended, the
+// box that ran it, and whether its output was cut off. outcome is
+// auditRunEnd's (runEndOutcome); res is nil when no result was recorded.
+func auditRunEndSentence(outcome string, res *runs.Result) string {
+ if res == nil {
+ switch outcome {
+ case provision.RunExpired:
+ return auditRunNotPickedUp
+ case provision.RunCancelled:
+ return auditRunCancelled
+ case auditRunOutcomeLost:
+ return fmt.Sprintf(auditRunInterrupted, runs.InterruptedLost)
+ }
+ return auditRunEnded
+ }
+ var s string
+ switch {
+ case res.Refused == runs.RefusedExpired || outcome == provision.RunExpired:
+ return auditRunNotPickedUp
+ case res.Refused != "":
+ s = fmt.Sprintf(auditRunRefused, res.Refused)
+ case res.Cancelled:
+ s = auditRunCancelled
+ case res.TimedOut:
+ s = auditRunTimedOut
+ case res.Interrupted != "":
+ s = fmt.Sprintf(auditRunInterrupted, res.Interrupted)
+ case res.Signal != "":
+ s = fmt.Sprintf(auditRunSignal, res.Signal)
+ case outcome == auditRunOutcomeEnded || outcome == "":
+ s = fmt.Sprintf(auditRunExit, res.Exit)
+ default:
+ s = auditRunEnded
+ }
+ if !res.Started.IsZero() && res.Ended.After(res.Started) {
+ s = fmt.Sprintf(auditRunAfter, s, res.Ended.Sub(res.Started).Round(time.Second))
+ }
+ if res.Box != "" {
+ s = fmt.Sprintf(auditRunOn, s, res.Box)
+ }
+ if res.Truncated {
+ s = fmt.Sprintf(auditRunTruncated, s)
+ }
+ return s
+}
diff --git a/internal/console/auditdetail_test.go b/internal/console/auditdetail_test.go
new file mode 100644
index 000000000..390bf9325
--- /dev/null
+++ b/internal/console/auditdetail_test.go
@@ -0,0 +1,185 @@
+package console
+
+import (
+ "encoding/json"
+ "html"
+ "net/http"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/provision"
+ "github.com/carlosframework/platform/internal/runs"
+)
+
+// What an audit row says (auditdetail.go).
+
+// runEndJSON is a run-end row's Detail as auditRunEnd marshals it.
+func runEndJSON(t *testing.T, outcome string, res *runs.Result) string {
+ t.Helper()
+ d := struct {
+ ID string `json:"id"`
+ Outcome string `json:"outcome"`
+ Result *runs.Result `json:"result,omitempty"`
+ }{"r-1", outcome, res}
+ b, err := json.Marshal(d)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return string(b)
+}
+
+func TestAuditWhatOfRunRows(t *testing.T) {
+ start := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
+ ran := func(r runs.Result) *runs.Result {
+ r.Started, r.Ended, r.Box = start, start.Add(2*time.Minute+3*time.Second), "uk-1"
+ return &r
+ }
+ for _, c := range []struct {
+ name, action, detail, want string
+ }{
+ {"run-start with a version", "run-start",
+ runStartDetail(runs.Request{ID: "r-1", Argv: []string{"bin/migrate", "--up"}}, "v12"),
+ "Started bin/migrate --up (v12)"},
+ {"run-start with no version", "run-start",
+ runStartDetail(runs.Request{ID: "r-1", Argv: []string{"rake", "db:seed"}}, ""),
+ "Started rake db:seed"},
+ {"an exit code", "run-end", runEndJSON(t, "result", ran(runs.Result{Exit: 3})),
+ "Run ended with exit code 3 after 2m3s on uk-1"},
+ {"exit 0 with no times or box", "run-end", runEndJSON(t, "result", &runs.Result{}),
+ "Run ended with exit code 0"},
+ {"timed out", "run-end", runEndJSON(t, "result", ran(runs.Result{TimedOut: true, Exit: -1})),
+ "Run timed out after 2m3s on uk-1"},
+ {"cancelled while running", "run-end", runEndJSON(t, provision.RunCancelled, ran(runs.Result{Cancelled: true})),
+ "Run was cancelled after 2m3s on uk-1"},
+ {"cancelled before it started", "run-end", runEndJSON(t, provision.RunCancelled, &runs.Result{Cancelled: true}),
+ "Run was cancelled"},
+ {"cancelled with no result", "run-end", runEndJSON(t, provision.RunCancelled, nil),
+ "Run was cancelled"},
+ {"interrupted", "run-end", runEndJSON(t, "result", ran(runs.Result{Interrupted: runs.InterruptedAgentRestart})),
+ "Run was interrupted (the box's agent restarted) after 2m3s on uk-1"},
+ {"lost", "run-end", runEndJSON(t, "lost", &runs.Result{Interrupted: runs.InterruptedLost, Box: "uk-1"}),
+ "Run was interrupted (its box stopped reporting) on uk-1"},
+ {"lost with no result", "run-end", runEndJSON(t, "lost", nil),
+ "Run was interrupted (its box stopped reporting)"},
+ {"refused", "run-end", runEndJSON(t, "result", &runs.Result{Refused: runs.RefusedWake, Box: "uk-1"}),
+ "Run was refused (wake) on uk-1"},
+ {"never picked up", "run-end", runEndJSON(t, provision.RunExpired, &runs.Result{Refused: runs.RefusedExpired}),
+ "No box picked up the run"},
+ {"never picked up, no result", "run-end", runEndJSON(t, provision.RunExpired, nil),
+ "No box picked up the run"},
+ {"a signal", "run-end", runEndJSON(t, "result", ran(runs.Result{Signal: "9"})),
+ "Run stopped by signal 9 after 2m3s on uk-1"},
+ {"a service's run-start", "run-start",
+ runStartDetail(runs.Request{ID: "r-1", Argv: []string{"import"}, ByService: "ci", OnBehalfOf: "ann@example.com"}, "v2"),
+ "Started import (v2). run by service ci for ann@example.com (as stated by the service)"},
+ {"a scheduled run's run-end", "run-end",
+ `{"id":"r-1","outcome":"result","result":{"exit":0},"requester":"run by service ci (scheduled)"}`,
+ "Run ended with exit code 0. run by service ci (scheduled)"},
+ {"output cut off", "run-end", runEndJSON(t, "result", ran(runs.Result{Exit: 0, Truncated: true})),
+ "Run ended with exit code 0 after 2m3s on uk-1. Its output was larger than 64 MB and was cut off"},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ w := auditWhatOf(c.action, c.detail)
+ if w.Text != c.want || w.JSON != "" {
+ t.Errorf("auditWhatOf(%s, %s) = %+v, want %q and no JSON", c.action, c.detail, w, c.want)
+ }
+ if got := auditText(c.action, c.detail); got != c.want {
+ t.Errorf("auditText = %q, want %q", got, c.want)
+ }
+ })
+ }
+}
+
+func TestAuditWhatOfOtherRows(t *testing.T) {
+ // A sentence stays as written.
+ if w := auditWhatOf("promote", "Made v2 live"); w != (auditWhat{Text: "Made v2 live"}) {
+ t.Errorf("a sentence = %+v", w)
+ }
+ // Long text: whole, with the whole of it in Title.
+ long := strings.Repeat("Set a variable that is long ", 4)
+ if w := auditWhatOf("env-set", long); w.Text != long || w.Title != long || w.JSON != "" {
+ t.Errorf("long text = %+v", w)
+ }
+ // Any other JSON object: the summary, and the object indented.
+ w := auditWhatOf("store-apply", `{"note":"<b>uploads</b>","size":3}`)
+ if w.Text != "Recorded store-apply" || w.JSON != "{\n \"note\": \"<b>uploads</b>\",\n \"size\": 3\n}" {
+ t.Errorf("a JSON row = %+v", w)
+ }
+ // A run row that does not read as its JSON, and JSON that is not an
+ // object, stay as stored.
+ for _, c := range [][2]string{{"run-start", "Started something"}, {"run-end", `{"outcome":`}, {"env-set", `["a","b"]`}, {"env-set", `{"a":1} trailing`}} {
+ if w := auditWhatOf(c[0], c[1]); w.Text != c[1] || w.JSON != "" {
+ t.Errorf("auditWhatOf(%q, %q) = %+v, want it as stored", c[0], c[1], w)
+ }
+ }
+}
+
+// TestOperatorFeedsShowWhat: on Activity, the dashboard and a person page,
+// a JSON row is its summary over the escaped, indented data in a native
+// <details>, and long text is one line with its whole in title.
+func TestOperatorFeedsShowWhat(t *testing.T) {
+ fx := newConsoleFixture(t)
+ w := seedOperatorWorld(t, fx)
+ long := strings.Repeat("Changed a long setting ", 4)
+ for _, r := range []Activity{
+ {Action: "store-apply", Detail: `{"note":"<script>alert(1)</script>"}`},
+ {Action: "env-set", Detail: long},
+ } {
+ if err := fx.st.RecordActivity(w.shop.ID, "shop", "ann@example.com", r, w.now); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for path, body := range map[string]string{
+ "/fleet/activity": peopleGet(t, fx, w.c, "/fleet/activity"),
+ "/fleet": dashGet(t, fx, w.c),
+ "/fleet/people/ann@example.com": personGet(t, fx, w.c, "ann@example.com"),
+ } {
+ details := "<details co-what><summary>Recorded store-apply</summary><pre>{\n &#34;note&#34;: &#34;&lt;script&gt;alert(1)&lt;/script&gt;&#34;\n}</pre></details>"
+ if !strings.Contains(body, details) {
+ t.Errorf("%s: the JSON row is not its summary over its data:\n%s", path, body)
+ }
+ if strings.Contains(body, "<script>alert(1)") {
+ t.Errorf("%s: the stored data reached the page unescaped", path)
+ }
+ clip := `<span co-clip title="` + html.EscapeString(long) + `">` + html.EscapeString(long) + `</span>`
+ if !strings.Contains(body, clip) {
+ t.Errorf("%s: the long row is not one line with its whole in title", path)
+ }
+ }
+}
+
+// TestRunRowsReadAsSentences is production-wired: a run started and
+// finished through the real doors writes its run-start and run-end rows,
+// and the customer console's app Activity tab and Recent activity, and the
+// operator's Activity, show them as sentences, not their JSON.
+func TestRunRowsReadAsSentences(t *testing.T) {
+ fx := runFixture(t)
+ id := fx.finishedRun(t)
+ if status, body := fx.post(t, id, "ack", `{"last_seq":1}`); status != http.StatusOK {
+ t.Fatalf("ack: %d %s", status, body)
+ }
+ if rows := fx.runEnds(t, id); len(rows) != 1 {
+ t.Fatalf("run-end rows %q, want 1", rows)
+ }
+ fx.setUser("paul@keymail.dev") // runFixture's last sign-in was alice's
+ c := signInOperator(t, fx.consoleFixture)
+ pages := map[string]string{}
+ for _, path := range []string{fx.u("/apps/shop/activity"), fx.u("/apps/shop"), fx.ts.URL + "/fleet/activity"} {
+ status, _, body := get(t, c, path)
+ if status != http.StatusOK {
+ t.Fatalf("GET %s = %d", path, status)
+ }
+ pages[path] = body
+ }
+ for path, body := range pages {
+ for _, want := range []string{"Started import x.csv", "Run ended with exit code 0"} {
+ if !strings.Contains(body, want) {
+ t.Errorf("%s has no %q", path, want)
+ }
+ }
+ if strings.Contains(body, `&#34;argv&#34;`) || strings.Contains(body, `&#34;outcome&#34;`) || strings.Contains(body, `"argv"`) {
+ t.Errorf("%s shows a run row's JSON", path)
+ }
+ }
+}
diff --git a/internal/console/templates/app_activity.html b/internal/console/templates/app_activity.html
index e4e0a00a8..6d37cfdbf 100644
--- a/internal/console/templates/app_activity.html
+++ b/internal/console/templates/app_activity.html
@@ -11,7 +11,7 @@
{{range .Events}}
<tr><td>{{fmtTime .CreatedAt}}</td><td>{{.Address}}</td>
<td>{{if .Message}}{{.Message}}{{else}}—{{end}}</td>
- <td>{{if .TargetURL}}<a href="{{.TargetURL}}">{{.Detail}}</a>{{else}}{{.Detail}}{{end}}</td>
+ <td>{{if .TargetURL}}<a href="{{.TargetURL}}">{{auditText .Action .Detail}}</a>{{else}}{{auditText .Action .Detail}}{{end}}</td>
</tr>
{{end}}
</table>
diff --git a/internal/console/templates/app_overview.html b/internal/console/templates/app_overview.html
index cf12c87c7..d3baa211f 100644
--- a/internal/console/templates/app_overview.html
+++ b/internal/console/templates/app_overview.html
@@ -86,7 +86,7 @@
<h2>Recent activity</h2>
<ul class="feed">
{{range .Events}}
- <li><span class="small" title="{{fmtTime .CreatedAt}}">{{ago .CreatedAt}}</span> {{.Address}} — {{.Detail}}</li>
+ <li><span class="small" title="{{fmtTime .CreatedAt}}">{{ago .CreatedAt}}</span> {{.Address}} — {{auditText .Action .Detail}}</li>
{{end}}
</ul>
<p class="small"><a href="{{.Nav}}/apps/{{.AppName}}/activity">All activity</a></p>