audit

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package audit writes a JSONL trail of every executed MyComputer action to $XDG_STATE_HOME/mycomputer/audit/YYYY-MM-DD.jsonl (falling back to ~/.local/state/mycomputer/audit). One line per action; clipboard content is redacted to bytes+mime only.

Writes are best-effort: a failure to write the audit line MUST never block the action itself. Callers log the failure to stderr (or pass a logger) and continue.

Optional opt-in: when Writer.FullPayloads is true the writer also stores per-batch payload JSON under <Dir>/payloads/<batch_id>.json (mode 0600) so `mycomputer audit replay` can reconstruct the original ActionBatch instead of the type-only fallback. Clipboard content is STILL redacted inside payload files — the opt-in only removes redaction on non-clipboard inputs (click coords, selectors, find queries, etc.). The privacy invariant on clipboard content is non-negotiable: it never reaches disk under any flag.

Index

Constants

View Source
const DefaultRetentionDays = 14

DefaultRetentionDays is the number of days of JSONL files retained in the audit directory. Configurable per-Writer.

Variables

This section is empty.

Functions

func DefaultDir

func DefaultDir() string

DefaultDir returns the audit directory MC writes to by default. Precedence: $XDG_STATE_HOME/mycomputer/audit > ~/.local/state/mycomputer/audit > /tmp/mycomputer-audit as a last resort.

func PayloadScrubBytes

func PayloadScrubBytes(raw []byte) ([]byte, error)

PayloadScrubBytes enforces the clipboard-content redaction invariant on a marshaled ActionBatch (or any JSON object containing actions[]). Every action with type=="clipboard_write" has its "content" field replaced with {"content_redacted":true,"bytes":N,"mime":M}. The original byte count and MIME are preserved so debugging tools can see "what shape went out" without ever recovering the content.

The function is JSON-shape agnostic on the rest of the payload: it only inspects top-level "actions" (an array). Non-clipboard actions pass through untouched.

Types

type ClipboardSummary

type ClipboardSummary struct {
	Selection string `json:"selection,omitempty"`
	Mime      string `json:"mime,omitempty"`
	Bytes     int    `json:"bytes"`
}

ClipboardSummary is the redacted clipboard envelope written to audit. Bytes and Mime only — never Content.

type Point

type Point struct {
	X int `json:"x"`
	Y int `json:"y"`
}

Point is a tiny screen-space coord written into the audit log.

type ProbeResult

type ProbeResult struct {
	Dir           string
	Writable      bool
	RetentionDays int
	TodayBytes    int64
	Error         string
}

Probe returns audit-writer health metadata for doctor.

type Record

type Record struct {
	TS             time.Time      `json:"ts"`
	BatchID        string         `json:"batch_id,omitempty"`
	ActionIndex    int            `json:"action_index"`
	Type           string         `json:"type"`
	Target         map[string]any `json:"target,omitempty"`
	ResolvedCoords *Point         `json:"resolved_coords,omitempty"`
	Backend        string         `json:"backend,omitempty"`
	OK             bool           `json:"ok"`
	Error          string         `json:"error,omitempty"`
	ElapsedMS      int64          `json:"elapsed_ms"`
	// DryRun is OMIT-WHEN-FALSE by design: dry-run is rare in routine
	// audit streams, and keeping the JSONL dense matters more than
	// always-present bool symmetry. Callers MUST treat a missing field
	// as false; never emit dry_run:null or dry_run:false. This shape
	// is the single source of truth for the audit-record schema.
	DryRun  bool `json:"dry_run,omitempty"`
	Skipped bool `json:"skipped,omitempty"`
	// ScreenshotBefore / After hold optional paths when the writer
	// is configured to record screenshots. Off by default; expensive.
	ScreenshotBefore string `json:"screenshot_before,omitempty"`
	ScreenshotAfter  string `json:"screenshot_after,omitempty"`
	// Clipboard mirrors task-5 redaction rules: bytes + mime only,
	// content NEVER appears.
	Clipboard *ClipboardSummary `json:"clipboard,omitempty"`
	// YieldEvent, when set, marks this record as a yield observation
	// (no action ran). Used for the "user input observed" trail when
	// --respect-user=false leaves execution running anyway.
	YieldEvent *YieldSummary `json:"yield_event,omitempty"`
	// PayloadRef, when set, names the per-batch payload manifest under
	// <Dir>/payloads/<batch_id>.json that contains the full ActionBatch
	// inputs (clipboard content scrubbed). Present only when the writer
	// was configured with FullPayloads=true. Absent in default v0.2
	// behavior. The string value equals the BatchID.
	PayloadRef string `json:"payload_ref,omitempty"`
}

Record is one line of the audit JSONL stream. Keep field names stable — agents may grep/jq this file.

type Writer

type Writer struct {
	// Dir is the audit directory. When empty, DefaultDir() is used.
	Dir string
	// RetentionDays controls rotation. <= 0 means use the default.
	RetentionDays int
	// FullPayloads, when true, enables the opt-in per-batch payload
	// manifest under <Dir>/payloads/<batch_id>.json. JSONL records
	// written via Append still go through the standard scrubRecord
	// path. WriteBatchPayload is a no-op when this is false.
	// Clipboard content is redacted in payload files regardless of
	// this flag — see PayloadScrubBytes.
	FullPayloads bool
	// contains filtered or unexported fields
}

Writer appends Records to today's JSONL file. Safe for concurrent use; serializes writes under a mutex.

func New

func New() *Writer

New returns a Writer rooted at DefaultDir() with default retention.

func (*Writer) Append

func (w *Writer) Append(rec Record) error

Append writes one record and rotates expired files. Best-effort: returns the underlying error but callers should continue regardless.

func (*Writer) FileFor

func (w *Writer) FileFor(d time.Time) string

FileFor returns the JSONL path used for records on day d (UTC).

func (*Writer) PayloadFileFor

func (w *Writer) PayloadFileFor(batchID string) string

PayloadFileFor returns the per-batch payload manifest path for the given batch id: <Dir>/payloads/<batch_id>.json. The path is deterministic; callers can probe os.Stat to decide whether full replay is available.

func (*Writer) PayloadsDir

func (w *Writer) PayloadsDir() string

PayloadsDir returns the directory where per-batch payload manifests live: <Dir>/payloads/. Callers should treat absence as "no payload was written for this batch" and fall back to type-only behavior.

func (*Writer) Probe

func (w *Writer) Probe(now time.Time) ProbeResult

Probe attempts to mkdir and stat the audit directory and the current day's file. Never returns an error — surfaces failure via ProbeResult.Error.

func (*Writer) ReadBatchPayload

func (w *Writer) ReadBatchPayload(batchID string) ([]byte, error)

ReadBatchPayload reads back a payload manifest written by WriteBatchPayload. Returns the scrubbed JSON bytes, or os.ErrNotExist when the file does not exist (the caller should fall back to v0.2 type-only replay).

func (*Writer) WriteBatchPayload

func (w *Writer) WriteBatchPayload(batchID string, raw []byte) error

WriteBatchPayload persists a per-batch payload manifest when FullPayloads is enabled. raw is JSON describing the original batch inputs; it is passed through PayloadScrubBytes before being written so clipboard content is redacted into the {content_redacted,bytes, mime} envelope. The file is created with mode 0600 inside a 0700 directory so the manifest is only readable by the owning user.

When FullPayloads is false this is a no-op (returns nil) and no directory is created — the default audit shape is unchanged.

type YieldSummary

type YieldSummary struct {
	DeviceID  int    `json:"device_id"`
	SourceID  int    `json:"source_id"`
	EventType string `json:"event_type"`
}

YieldSummary is the per-line yield observation envelope.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL