Documentation
¶
Overview ¶
Package auditkit is the ONE append-only writer for the audit record DESIGN.md §20.1 declares.
THE CONTRACT IS NOT DEFINED HERE. `spec.AuditEvent`, `spec.AuditDetail` and `spec.Redacted` are generated from `opencharly/spec`'s `schema/audit.cue`, and this package consumes them: it holds no mirror struct and restates no shape (D-SCH-1). What this package owns is the WRITE PATH — where a record goes, that exactly one exists per act, and that nothing can change one afterwards.
THE ROWS THAT SHAPE THIS CODE (not decoration — each decides a line):
D-AUDIT-1, "every mutation and every authority decision appends EXACTLY one record". The unit is the ACT, and the caller names it: `Append(act, event)` writes once per act, and a SECOND append for the same act fails loudly with ErrDuplicateAct — neither duplicated (which inflates an audit) nor absorbed (which hides a double-apply). The contract expresses the same rule from the other side: `#AuditAppendInput` is `{event!: #AuditEvent}`, one required, singular event, so the wire has no form that carries zero or two.
THE ACT ID IS THE PROTOCOL'S `call_id`, not a name invented for this call: every `#Request` carries a `#CallHeader` whose `call_id` is a 128-bit per-invocation id (`#CallId`, `=~"^[0-9a-f]{32}$"`; DESIGN.cue:1144 and :1329-1334), and `audit_append` is a host method (DESIGN.md §4.6), so the wiring is `Append(req.Header.CallId, req.AuditAppendInput.Event)`. No field is needed on `#AuditAppendInput` for it — a second per-invocation identity beside the one the header already mandates would be exactly the duplication that header prevents.
D-AUDIT-4, "append-only". A record is ONE immutable file, created `O_CREATE|O_EXCL` and never reopened for writing. The exported surface has no update, delete or truncate, and `TestExportedSurfaceHasNoMutator` asserts that against the package's own source, because "no mutator" is a property of the SURFACE and a comment cannot enforce one.
D-AUDIT-3, "a record never holds a secret value". This is STRUCTURAL rather than checked: `spec.Redacted` is `{redacted!: true}` — a bare marker with no field a value could occupy — and a detail's `value` admits nothing but that marker. There is therefore no `ErrSecretValue` here and no name-shape rule: a type with no value-bearing field cannot be the leak, and a string check pretending to guard it would be the weaker mechanism. `TestRedactedIsAnEmptyMarker` proves the absence by enumerating the type's own fields.
WHAT THE WRITER DOES *NOT* VALIDATE, deliberately: the word sets (`event`, `outcome`, detail `code`) and the field shapes (`at`, `identity`, `action`, `revision`, `namespace`, the detail bound) are the SCHEMA's, and restating them in Go would be the duplication D-SCH-1 forbids — a second copy of a rule that drifts from `schema/audit.cue`. The writer refuses exactly what no CUE pass can see: the ZERO VALUE of a required field, which a Go caller can hand it because the value never passes through the loader.
WHERE THE RECORDS LIVE (D-DEP-5): under the state root, resolved by the ONE resolver — storekit.Dir(storekit.RootState), which re-exports the contract's storeroot package — never by hand-building an XDG path here. `Open` takes the directory so a test injects its own temporary root (D-STORE-8); `OpenDefault` is the only place that resolves the real one.
THE LOCK: D-AUDIT-1's "exactly one" is decided by a read-then-create, so two concurrent appends would race between the duplicate check and the create. The serializer is spec/lock — the contract's ONE advisory-lock primitive, the same one storekit uses for its index — and its file is not a record (List reads `*.json` and nothing else). The create still carries O_EXCL, so even a lost lock cannot overwrite a record; it can only lose a race it would have failed anyway.
ORDER: a record's file name carries a zero-padded sequence, so List returns records in append order from a directory listing alone — no index file to fall out of step with the records.
THE `value?` MARKER IS OPTIONAL ON THE WIRE, and that is the pinned contract's doing rather than this package's. `#AuditDetail.value?` projects as `*Redacted` at the spec version this module requires (`v0.2026320.2307`, which adopted opencharly/spec#345), so a detail carrying no marker writes NO `value` key at all — `TestRedactedIsAnEmptyMarker` case (c) asserts exactly that.
The history is worth one line because it is why the pin is not incidental: before that fix the field projected as a VALUE, and `encoding/json`'s `omitempty` cannot omit a struct, so an absent marker serialized as `{"redacted":false}` — asserting "not redacted" on a record that had no marker. It was never a leak (`#Redacted` can only hold `true`); it was the `?` lost on the wire. The fix landed in the contract's generator, where it belonged, NOT as a second serialization here.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDuplicateAct reports a second record for an act that already has one (D-AUDIT-1). ErrDuplicateAct = errors.New("audit: this act already has a record (D-AUDIT-1: exactly one per act)") // ErrInvalidRecord reports a record that cannot answer the question an audit exists for: an act // with no identity, no time, no event, no outcome, or a detail with no code or no text. ErrInvalidRecord = errors.New("audit: invalid record") )
The two refusals. Each is a distinct wrapped error so a caller can act on the class rather than on a message.
Functions ¶
This section is empty.
Types ¶
type Appender ¶
type Appender struct {
// contains filtered or unexported fields
}
Appender writes audit events into one directory and reads them back in append order.
It is safe for concurrent use across processes: every append serializes on a file lock and creates its record with O_EXCL.
func Open ¶
Open returns an Appender over dir, creating it when absent. Pass an injected directory in a test (D-STORE-8); use OpenDefault in production.
func OpenDefault ¶
OpenDefault returns an Appender over the audit directory under the STATE root (D-DEP-5), as the contract's ONE root resolver reports it.
func (*Appender) Append ¶
func (a *Appender) Append(act string, event spec.AuditEvent) error
Append writes EXACTLY ONE record for act.
`act` is the act's identity — the protocol's `call_id` for a host method's invocation (see the package doc), i.e. a PER-INVOCATION id, never a name. The contract's event carries no id of its own, by design: an id would be a field a record could use to point at another record, which D-AUDIT-4 forbids. The dedupe key therefore lives in the file name, where it cannot be amended.
Two calls with the same act are a programming error, not a no-op: the second returns ErrDuplicateAct, because an audit that silently absorbs a double-append cannot say how many times something happened.
An act id must be PER INVOCATION — a command run's id, a request id — never a NAME. A deployment name identifies a deployment, not one deploy of it, so using one would make every deploy of that name a single act and the second would be refused: loudly and correctly, but for the wrong reason.