Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 5aranx

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
299 changes: 253 additions & 46 deletions README.md

Large diffs are not rendered by default.

62 changes: 37 additions & 25 deletions docs/architecture.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Architecture

```
+-------------------+ HTTPS/mTLS +-------------------+
operator | shadowgate | <------------------> | shadowgate agent |
dashboard | server (FastAPI) | | (daemon) |
| | | executor |
| Store (sqlite) | | plugin loader |
| qjobs (WAL) | +-------------------+
| RunEngine |
+-------------------+ HTTP + Agent Token +-------------------+
operator | shadowgate | <----------------------> | shadowgate agent |
dashboard | server (FastAPI) | (mTLS deferred to T14) | (library; CLI |
| | | not yet wired) |
| Store (sqlite) | | executor |
| qjobs (WAL) | | builtins |
| RunEngine | +-------------------+
| AuditLog (chain) |
+-------------------+
```
Expand All @@ -17,40 +17,52 @@
| Module | Responsibility |
|---|---|
| `server/app.py` | FastAPI surface: tokens, enroll, jobs, poll, result, runs, ui |
| `server/store.py` | One WAL sqlite connection for agents, tokens, queue, runs |
| `server/queue.py` | Atomic exactly-once claim via `UPDATE … RETURNING` |
| `server/store.py` | One WAL sqlite connection shared by agents, tokens, queue and runs (enrollment uses a short-lived dedicated connection). The shared connection is not yet safe under concurrent requests; see Queue concurrency. |
| `server/queue.py` | Atomic per-row claim via `UPDATE … RETURNING`; completion is owner- and status-checked |
| `server/runs.py` | DAG materialization, concurrency cap, compensation |
| `server/audit.py` | Append-only SHA-256 hash chain, tamper-evident |
| `server/audit.py` | Append-only SHA-256 hash chain; detects edited or reordered entries (limits below) |
| `server/orchestrator.py` | Runbook YAML, validation, cycle rejection, targeting |
| `plugins/loader.py` | Builtin + `entry_points` discovery |
| `plugins/gate.py` | ed25519 signature gate for external plugins |
| `crypto/mtls.py` | Local CA, per-agent cert issuance |
| `plugins/gate.py` | ed25519 signature gate for external plugins (not used at runtime yet) |
| `crypto/mtls.py` | Local CA, per-agent cert issuance (not used at runtime yet) |
| `crypto/signing.py` | Org key generation, manifest sign/verify |

## Data flow

1. Operator mints a one-time enrollment token → agent burns it once → agent is in `agents`.
2. Operator enqueues a job (`/jobs`) or starts a runbook (`/runs`).
3. Agent long-polls `/agent/poll`; server claims that agent's next queued job atomically and returns it.
4. Agent executes the action through its signed plugin registry, posts `/agent/result`.
5. Server completes the job, updates the run state machine, and every transition is appended to the audit chain.
1. Operator mints a one-time enrollment token (`POST /tokens`, optionally bound to an `agent_id`) → agent burns it once (`POST /enroll`) → `agents` stores `secret_hash` (SHA-256) and returns a one-time `agent_token`. (mTLS cert issuance/pinning is not yet implemented; deferred to T14.)
2. Operator enqueues a job (`POST /jobs`) or starts a runbook (`POST /runs`).
3. Agent short-polls `POST /agent/poll` with `X-ShadowGate-Agent-Token` (long-poll is not yet implemented); server authenticates the agent and claims that agent's next queued job atomically (`claim_next(agent_id=...)`).
4. Agent executes the action (builtins; signed third-party plugin registry is not yet wired into an agent CLI) and posts `POST /agent/result` with `X-ShadowGate-Agent-Token`; server enforces job ownership (`id`, `agent_id`, `status='running'`).
5. Server completes the job and updates the run state machine; each accepted transition is appended to the audit chain. A retried identical result is acknowledged as `duplicate` without a new audit entry or engine call.

## Queue concurrency

`SqlQueue` claims with a single `UPDATE` constrained to `status='queued'` and
`RETURNING`, so two workers cannot claim the same row. WAL + `busy_timeout`
make multi-connection reads live. Test: `tests/test_queue.py` runs 8 threads
over 40 jobs and asserts zero duplicates.
make multi-connection reads live. `tests/test_queue.py` runs 8 threads over 40
jobs, each on its **own** connection, and asserts zero duplicate claims.

The running server, however, shares **one** connection across FastAPI's request
threads. Under concurrent agent polls this produces sqlite3 errors and HTTP 500s,
can leave jobs `running` without delivering them, and can transiently reject a
valid agent token with 401. Serializing or isolating connection use is planned
work and is not implemented.

## Plugin trust

Builtin actions ship with the package and are implicitly trusted. Third-party
actions are discovered via the `shadowgate.actions` entry-point group but are
loaded only after `plugins/gate.make_verifier` validates a signed
`shadowgate_plugin.json` manifest against the pinned org public key.
discovery (`plugins/loader.load_entry_points`) and ed25519 manifest
verification (`plugins/gate.make_verifier` for `shadowgate_plugin.json`) exist
in the codebase, but are not yet wired into an agent CLI.

## Audit integrity

`AuditLog.append` writes `prev = sha256(last_entry)` into each JSONL line, and
`verify_chain()` recomputes the chain. Any edit, deletion, or reorder breaks
the head — tested in `tests/test_audit.py`.
`AuditLog.append` writes `prev = sha256(canonical(last_entry))` into each JSONL
line and `verify_chain()` recomputes the chain. Editing or reordering an existing
line is detected (`tests/test_audit.py` covers the edit case).

Known limits:
- **Tail truncation is not detected.** Removing the newest line(s), or emptying
the file, still verifies, because nothing anchors the head.
- **Concurrent appends fork the chain.** `append` is not serialized, so
`verify=true` can report `false` without any tampering.
Loading
Loading