Skip to content

Automation runs always report back: trigger provenance, every outcome (not just success), offline queue, env-var key #241

Backlog

Description

@vsn411

Part of #240. Every automation sub-issue depends on this one.

Problem

The automations in #240 run secfoo without anyone typing a command: before the agent commits, when it finishes a turn, on a schedule in a cloud agent. The platform has to hear about every one of those runs, or teams can't see what was checked, what was blocked and what silently failed. Today it doesn't:

  1. Only successful runs are pushed. src/secfoo/runner.py calls _try_cloud_sync() inside if result.status == "success":. A run that fails, times out, hits --max-cost or crashes (_mark_run_crashed) never reaches the portal.
  2. Push failures are invisible. _try_cloud_sync() swallows every error at logger.debug. The only way to notice is secfoo cloud status and a manual secfoo cloud sync. That's acceptable for a person at a terminal, but nobody watches debug logs from a hook.
  3. The portal can't tell what triggered a run. The payload in cloud.push_run() has skill_id, agent_id, tokens and cost, but nothing about which tool, which automation, which event. A commit-guard run in Claude Code looks the same as a manual secfoo run.
  4. Cloud agents can't report at all. The ingest key is read only from ~/.secfoo/cloud.toml (secfoo cloud login). Cloud agents (Copilot cloud agent, Grok Build/Devin cloud automations, Claude Code cloud scheduled tasks) start from a fresh machine with secrets in environment variables. The runtime sensor already accepts SECFOO_API_KEY / SECFOO_API_KEY_FILE / SECFOO_SERVER (see "Runtime ingest API" in docs/REFERENCE.md), but run sync does not.
  5. Some hosts block the network. Copilot cloud agent only reaches GitHub and Copilot hosts by default. Codex's workspace-write sandbox fails network calls. A push from inside those environments fails, and today the run is dropped from the portal's view.

Goal

Every automation invocation that runs a skill ends as exactly one run record. It is always stored locally, and it is pushed to the portal when a key is configured. If it can't be pushed now, it is queued and pushed later, and the user can see that. The rules from #224 still hold: local by default, a key only adds central reporting, and a view key never uploads.

Spec

1. Trigger provenance on every run

Add a trigger object to the run (new nullable column(s) in src/secfoo/storage/schema.sql plus a migration in repository.py, the same pattern as the existing ALTER TABLE list), to RunRecord, to push_run()'s payload, and to secfoo run --json.

Field Values / notes
source cli (default for a manual run), hook, scheduled, ci (the GitHub Action and examples/ci), mcp (a host agent submitting through secfoo mcp serve, #224)
host claude-code, grok-build, copilot, codex, gemini-cli, antigravity, devin, droid, opencode, kiro, cline, or null
host_version As reported by the host's hook payload or env, else null
recipe / recipe_version The automation recipe id and version from #242, e.g. commit-guard / 1
event Normalized: pre_shell, post_edit, turn_end, session_start, session_end, file_save, schedule, pull_request, manual
mode delegate (secfoo ran its own agent) or host (the host agent did the review and submitted it)
decision What the automation did with the result: allowed, blocked, followup (findings handed back to the agent), none
session_ref SHA-256 of the host's session/conversation id with a per-install salt from ~/.secfoo. Never the raw id. Lets the portal group runs from one agent session

Never send anything else from a hook payload: no prompts, transcripts or transcript_path, user emails (Grok Build's hook stdin includes user_email), tool arguments, or file contents. Add a test that builds a payload from a recorded hook stdin fixture and asserts that none of those keys or values appear.

2. Push every outcome, not only success

  • New terminal statuses: skipped (the recipe ran but nothing was in scope, e.g. no changed files; record it with zero cost so coverage is visible) and not_submitted (host mode: the hook opened a run and the host agent never submitted a report before the session or turn ended; set by secfoo hook + secfoo automations: one tested entrypoint for every AI tool, recipes enabled on the fly #242).
  • Call the sync for every terminal status: success, failed, timeout, cost cap (exit 3), crashed, skipped, not_submitted.
  • ⚠️ Findings lifecycle safety. A non-success run has no usable report. It must never close findings, either locally (the runner already only updates findings on success; keep it that way, with a test) or on the portal. The portal is not in this repo, so gate pushing non-success runs on a capability the portal advertises in GET /api/v1/whoami (for example "capabilities": ["run_outcomes_v1"]). If the capability is absent, keep today's behaviour (push success only) and leave other runs local. Maintainers will add the portal side. Keep the payload change additive so older portals keep working.

3. Outbox with retry

repo.list_unsynced_runs() / mark_run_synced() already exist. Build on them:

  • Track sync_attempts, last_sync_error and next_sync_at (exponential backoff, capped). Store the error text with the key redacted.
  • Opportunistic flush: at the start of any secfoo command and at the end of every secfoo hook invocation, push up to N queued runs within a short time budget (say 2 s total). Never block or fail the command because of it.
  • secfoo cloud sync keeps working as the manual flush and reports per-run errors, as it does now.
  • Replays are safe: the portal keys runs by client_run_uuid. Add a test that pushes the same run twice.

4. Key from the environment for non-interactive hosts

When ~/.secfoo/cloud.toml is absent, read SECFOO_API_KEY or SECFOO_API_KEY_FILE, plus optional SECFOO_SERVER, for run sync. This mirrors the sensor config. The ingest/view rules in cloud.py stay exactly as they are (_require_ingest_key, whoami check before upload). The key must never appear in logs, --json output, hook output or error messages.

5. Make the state visible

  • secfoo run --json gains "cloud": {"state": "synced" | "queued" | "local_only" | "refused", "error": null | "<short reason>"}.
  • secfoo cloud status lists queued runs grouped by last error, so "firewall blocked app.secfoo.com" is obvious.
  • secfoo hook (secfoo hook + secfoo automations: one tested entrypoint for every AI tool, recipes enabled on the fly #242) shows one short user-facing line when a run was queued or refused, using the host's user-message field, so users know reporting is behind.
  • Local dashboard (secfoo serve): show host + recipe on the run page and allow filtering runs by them.

Where to look

  • src/secfoo/cloud.py: push_run, sync_run, load_cloud_config, key rules
  • src/secfoo/runner.py: _try_cloud_sync and its single call site under if result.status == "success", _mark_run_crashed
  • src/secfoo/storage/schema.sql, models.py, repository.py (list_unsynced_runs, mark_run_synced, column migrations)
  • src/secfoo/cli.py: cloud status / cloud sync, run --json
  • docs/REFERENCE.md: "Runtime ingest API" (env-var key precedent) and "CI gates and machine-readable output"

Tests (required)

Done when

The tests above pass. docs/REFERENCE.md documents the trigger object, outcome statuses, the outbox and the env-var key. secfoo run --json and secfoo cloud status show the sync state. The portal-side capability flag is coordinated with maintainers before the non-success push is enabled.

Activity

  1. added
    enhancement
    help wanted
    difficulty: hard
    on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty: hardenhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions