Personal Telegram developer console for GitHub

docs: specify safe GitHub operator phase

davoodmehraban89 committed Oct 3, 2026 at 05:20 UTC acb758a621bb1ba23cbb5b7c668304e2907aa330
1 file changed +149
docs/superpowers/specs/2026-10-03-safe-github-ops-design.md new
+149
@@ -0,0 +1,149 @@
1 +# Safe GitHub Operator Design
2 +
3 +## Purpose
4 +Turn the production-accepted read-only plugin foundation into a safe write-capable GitHub operator without weakening the existing owner-only, fail-closed security model.
5 +
6 +## Scope
7 +This phase delivers two tightly coupled capabilities:
8 +1. A reusable Telegram approval flow for `WRITE` and `DESTRUCTIVE` plugin actions.
9 +2. GitHub Advanced operations for workflow visibility/control and repository operational views.
10 +
11 +Out of scope for this phase: AI model execution, paid browser automation, Supabase, Cloudflare rollback, Mini App UI, and any new paid resource. Those remain later phases.
12 +
13 +## Success criteria
14 +- `READ` actions continue to execute immediately for the authorized owner.
15 +- `WRITE` and `DESTRUCTIVE` actions never execute on the first request.
16 +- Approval tokens are single-use, owner-bound, action-bound, short-lived, and tamper-evident.
17 +- Approval UI exposes explicit Confirm and Cancel choices.
18 +- Replayed, expired, malformed, wrong-owner, or mismatched approvals fail closed and do not execute provider code.
19 +- GitHub workflow actions can list runs, inspect jobs/steps, re-run failed jobs, and cancel an in-progress workflow when the configured GitHub token has sufficient scope.
20 +- Missing GitHub permissions produce an explicit operational error; they are not reported as success.
21 +- Every attempted and completed action emits redacted audit events.
22 +- Existing repository/file navigation remains unchanged.
23 +- All existing tests stay green, new tests cover approval and GitHub operator behavior, CI passes, production deploy succeeds, and live Telegram READ smoke remains healthy.
24 +
25 +## Approval architecture
26 +
27 +### ApprovalStore
28 +Use an in-memory request-scoped approval registry only for encoding/decoding signed approval payloads; persistence is not required because the approval state itself is carried in a signed callback token.
29 +
30 +The token payload contains:
31 +- `v`: schema version `1`
32 +- `pluginId`
33 +- `actionId`
34 +- `input`: compact action parameters
35 +- `risk`: `WRITE` or `DESTRUCTIVE`
36 +- `ownerId`
37 +- `issuedAt`
38 +- `expiresAt`
39 +- `nonce`
40 +
41 +The token is authenticated with HMAC-SHA-256 using `TELEGRAM_WEBHOOK_SECRET` as the signing secret. The raw secret is never logged or returned.
42 +
43 +Telegram callback data has a strict size budget, so approval tokens must not embed verbose objects. GitHub operations use compact numeric/string identifiers (repository ID, workflow run ID, job ID) and the token encoder rejects oversized payloads instead of truncating security-relevant data.
44 +
45 +### ApprovalService
46 +`ApprovalService.issue()` creates a signed token. `ApprovalService.verify()` validates signature, schema, owner, expiry, risk, and expected action before returning the decoded payload.
47 +
48 +A verified token does not by itself execute an action. The router receives an explicit `approved: true` context only after verification. The action policy then allows the matching `WRITE`/`DESTRUCTIVE` action.
49 +
50 +### One-time semantics
51 +Telegram duplicate callbacks are possible. A process-local nonce cache prevents duplicate execution within one Worker isolate. Provider operations chosen for this phase are also idempotent or safe to retry at the API level: re-running failed jobs may create a new attempt only once per accepted callback in the active isolate; cancel on an already completed run is surfaced as a provider error. No destructive repository mutation is introduced in this phase.
52 +
53 +## Policy changes
54 +`ActionPolicy.evaluate()` accepts `approved` in addition to `action` and `isOwner`.
55 +- non-owner: denied
56 +- unknown action/risk: denied
57 +- READ: allowed
58 +- WRITE/DESTRUCTIVE without approval: `requiresApproval`
59 +- WRITE/DESTRUCTIVE with verified approval: allowed with distinct audit reason
60 +
61 +The router remains `resolve -> policy -> execute`; approval verification occurs before the second dispatch that carries `approved: true`.
62 +
63 +## Telegram UX
64 +When a plugin route returns `requiresApproval`, the bot edits the same message to a confirmation view:
65 +
66 +- operation label
67 +- repository/workflow target
68 +- risk badge
69 +- `✅ تأیید` callback
70 +- `❌ لغو` callback
71 +
72 +Cancel returns to a safe operational view without provider execution.
73 +
74 +Confirmation callbacks use reserved prefixes:
75 +- `approve:<token>`
76 +- `cancel:<token>`
77 +
78 +Malformed/expired callbacks return a concise Persian error and a Back button.
79 +
80 +## GitHub Advanced plugin
81 +The existing GitHub plugin is extended instead of creating a second overlapping GitHub plugin.
82 +
83 +### New READ actions
84 +- `github.workflow.runs.list`
85 +- `github.workflow.run.detail`
86 +- `github.workflow.jobs.list`
87 +- `github.workflow.job.detail`
88 +
89 +### New WRITE actions
90 +- `github.workflow.run.rerun_failed`
91 +- `github.workflow.run.cancel`
92 +
93 +No branch deletion, merge, release publication, secret mutation, or repository settings mutation is added in this phase.
94 +
95 +## GitHub service interfaces
96 +Add focused methods to `GitHubService`:
97 +- `listWorkflowRuns(repoId, { perPage = 10 } = {})`
98 +- `getWorkflowRun(repoId, runId)`
99 +- `listWorkflowJobs(repoId, runId)`
100 +- `rerunFailedWorkflowRun(repoId, runId)`
101 +- `cancelWorkflowRun(repoId, runId)`
102 +
103 +All methods resolve numeric repository IDs through the existing trusted repository resolver and use the configured bearer token. Non-2xx responses throw errors containing status and a bounded provider message.
104 +
105 +## Menu changes
106 +Repository detail adds an `🧪 CI / Actions` entry. Workflow views remain bounded for Telegram:
107 +- maximum 10 runs per page in v1
108 +- concise status/conclusion/branch/SHA summary
109 +- job details show step status/conclusion without full raw logs
110 +
111 +Raw job logs and AI analysis belong to later phases.
112 +
113 +## Audit
114 +Approval lifecycle emits:
115 +- `approval_issued`
116 +- `approval_verified`
117 +- `approval_rejected`
118 +- existing `action_attempt`
119 +- existing `action_result`
120 +
121 +Audit payloads may include IDs and status but never secrets or signed approval tokens.
122 +
123 +## Error handling
124 +- Missing signing secret: fail closed for write approval; READ remains available.
125 +- Signature mismatch/expiry/owner mismatch/action mismatch: reject before provider call.
126 +- GitHub 401/403: explicit permission/authentication error.
127 +- GitHub 404: explicit not-found error.
128 +- GitHub 409/422: explicit current-state conflict; do not fabricate success.
129 +- Telegram edit failure follows existing error propagation behavior.
130 +
131 +## Testing
132 +Add unit coverage for:
133 +- policy approved/unapproved semantics
134 +- approval issue/verify/tamper/expiry/owner mismatch/oversize/replay behavior
135 +- router execute gating with approved context
136 +- GitHub REST method/path/body behavior and non-2xx failures
137 +- plugin route ownership and risk mapping
138 +- bot confirmation/cancel/approved callback flows
139 +- no secret/token leakage in audit events
140 +
141 +Regression suite must retain all existing Foundation tests.
142 +
143 +## Deployment acceptance
144 +1. PR CI green with non-zero test discovery.
145 +2. Merge to `main` and post-merge CI green.
146 +3. Cloudflare deployment succeeds for exact verified commit.
147 +4. Owner-device smoke: `/start -> Repositories -> project -> CI / Actions`.
148 +5. Live write-action smoke is only attempted if GitHub token permissions already support it; otherwise the capability is marked implemented but provider-permission smoke is `NOT VERIFIED` and no secret/token rotation is performed automatically.
149 +6. Cloudflare telemetry shows current-version webhook HTTP 200 and redacted audit events with no new webhook error.