Repository navigation
Description
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:
- Only successful runs are pushed.
src/secfoo/runner.pycalls_try_cloud_sync()insideif result.status == "success":. A run that fails, times out, hits--max-costor crashes (_mark_run_crashed) never reaches the portal. - Push failures are invisible.
_try_cloud_sync()swallows every error atlogger.debug. The only way to notice issecfoo cloud statusand a manualsecfoo cloud sync. That's acceptable for a person at a terminal, but nobody watches debug logs from a hook. - The portal can't tell what triggered a run. The payload in
cloud.push_run()hasskill_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 manualsecfoo run. - 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 acceptsSECFOO_API_KEY/SECFOO_API_KEY_FILE/SECFOO_SERVER(see "Runtime ingest API" indocs/REFERENCE.md), but run sync does not. - Some hosts block the network. Copilot cloud agent only reaches GitHub and Copilot hosts by default. Codex's
workspace-writesandbox 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) andnot_submitted(host mode: the hook opened a run and the host agent never submitted a report before the session or turn ended; set bysecfoo 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 inGET /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_errorandnext_sync_at(exponential backoff, capped). Store the error text with the key redacted. - Opportunistic flush: at the start of any
secfoocommand and at the end of everysecfoo hookinvocation, 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 synckeeps 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 --jsongains"cloud": {"state": "synced" | "queued" | "local_only" | "refused", "error": null | "<short reason>"}.secfoo cloud statuslists 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 rulessrc/secfoo/runner.py:_try_cloud_syncand its single call site underif result.status == "success",_mark_run_crashedsrc/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 --jsondocs/REFERENCE.md: "Runtime ingest API" (env-var key precedent) and "CI gates and machine-readable output"
Tests (required)
- Each terminal status is pushed exactly once when the portal advertises the capability, and only
successis pushed when it doesn't. - A failed, timed-out or crashed run never changes findings state.
- The payload includes
triggerand none of the forbidden fields, checked against recorded hook-stdin fixtures fromsecfoo hook+secfoo automations: one tested entrypoint for every AI tool, recipes enabled on the fly #242. - Outbox: a push failure queues the run, the next command flushes it, and backoff is respected. The flush never raises and never exceeds its time budget.
- Env key: an ingest key in
SECFOO_API_KEYsyncs, a view key is refused, and the key string never appears in captured stdout, stderr or logs. - Local mode (no key) makes no network calls (existing pattern: see the "local mode makes no portal calls" requirement in Secfoo in every agent marketplace: one MCP server, local by default, optional key for the org portal #224).
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