Two halves of one boundary: a credential resolves to an identity, and an action records which identity acted and whose authority it used. resource server. internal/infrastructure/oidc verifies a provider-issued bearer token against the provider's published keys, using an OSS OIDC library: archie compares no signature, validates no claim and rotates no key itself, holds no client secret and issues no session, so a compromised archie cannot mint an identity. Audience is required rather than optional -- a token minted for another service must not authenticate here -- and discovery happens at construction, so an unreachable provider is a startup failure rather than a request-time surprise. identity.Authenticate is the single place a credential becomes an identity, and it refuses in four distinguishable ways: no credential presented, a credential that did not verify (wrong key, expired, wrong issuer, wrong audience, malformed), a verified subject with no identity bound to it, and an identity that may not act. The order is deliberate: verification happens before any field of the token is used, so nothing an unverified token carries can select an identity. A suspended identity is refused even though its credential verified, because suspension is archie's decision and must not depend on the provider withdrawing a token. The binding is the second bridge beside the legacy-name alias: identity_subjects keys (issuer, subject) so the same subject string from another issuer is a different caller, and rebinding moves a subject so two identities can never claim one provider subject. The contract Authenticate needs is SubjectResolver, narrower than Repository, so a consumer that only authenticates does not take the whole identity CRUD surface -- and staterpc.Client, which asserts Repository, does not need a new RPC to satisfy it. attribution. taskactions now records two identities per action: the acting identity and the authorising principal, both on the event row as new columns added through the existing migration table. The rule that the bug turned on is enforced by the event kind describing the actor: a person's approval is human_approved, an agent's is agent_approved, and an action with no verified actor is task_approved, a task-level event that credits nobody. An agent acting under a person's standing approval records itself as the actor and that person as the principal, so "approved by came from me" is answerable from the record rather than asserted. Scope and actor are separated: scope decides which task a caller may touch and is never promoted into an actor, because the dashboard's credential is a shared token and possession of it is not evidence that a person was at the keyboard. That last point is a deliberate fidelity trade, and the reason the transport is not wired: the dashboard action path passes no actor yet, so dashboard approvals are now recorded as task_approved rather than claimed as a human's. Recording nothing false is the requirement; naming the human verifiably needs the dashboard to authenticate a token and resolve it, which needs two new state-store RPCs (ResolveIdentitySubject, BindIdentitySubject) -- the next piece, not a rewrite: the verifier is a one-method interface, so introspection would be a second implementation of it. Verified: task check exit 0; focused runs recorded in the handoff. Opaque authelia_at_ tokens were inspected rather than assumed and cannot be verified against public keys; infra is configuring access_token_signed_response_alg RS256 on the archie client, with audience https://archie.catlow.cloud and issuer https://auth.catlow.cloud.
251 lines
8.1 KiB
Go
251 lines
8.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
"github.com/samcharles93/archie-core/internal/events"
|
|
)
|
|
|
|
const eventsSchema = `
|
|
CREATE TABLE IF NOT EXISTS events (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
at TEXT NOT NULL,
|
|
kind TEXT NOT NULL,
|
|
task_id INTEGER NOT NULL DEFAULT 0,
|
|
repo TEXT NOT NULL DEFAULT '',
|
|
issue INTEGER NOT NULL DEFAULT 0,
|
|
workflow TEXT NOT NULL DEFAULT '',
|
|
stage TEXT NOT NULL DEFAULT '',
|
|
attempt INTEGER NOT NULL DEFAULT 0,
|
|
actor_id TEXT NOT NULL DEFAULT '',
|
|
actor_kind TEXT NOT NULL DEFAULT '',
|
|
principal_id TEXT NOT NULL DEFAULT '',
|
|
detail TEXT NOT NULL DEFAULT '',
|
|
data TEXT NOT NULL DEFAULT '{}'
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_events_task ON events(task_id, id);
|
|
CREATE INDEX IF NOT EXISTS idx_events_kind ON events(kind, id);
|
|
`
|
|
|
|
// InsertEvent appends an event to the log and returns its row id.
|
|
func (s *Store) InsertEvent(ctx context.Context, e events.Event) (int64, error) {
|
|
return insertEvent(ctx, s.db, e)
|
|
}
|
|
|
|
type eventExecer interface {
|
|
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
|
}
|
|
|
|
func insertEvent(ctx context.Context, execer eventExecer, e events.Event) (int64, error) {
|
|
if e.At.IsZero() {
|
|
e.At = time.Now().UTC()
|
|
}
|
|
data, err := json.Marshal(e.Data)
|
|
if err != nil {
|
|
data = fmt.Appendf(nil, `{"marshal_error":%q}`, err.Error())
|
|
}
|
|
res, err := execer.ExecContext(ctx, `
|
|
INSERT INTO events (at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
e.At.UTC().Format(time.RFC3339Nano), e.Kind, e.TaskID, e.Repo, e.Issue,
|
|
e.Workflow, e.Stage, e.Attempt, e.ActorID, e.ActorKind, e.PrincipalID,
|
|
clip(e.Detail, 4000), string(data))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
func scanEvents(rows *sql.Rows) (out []events.Event, retErr error) {
|
|
defer func() {
|
|
retErr = errors.Join(retErr, rows.Close())
|
|
}()
|
|
for rows.Next() {
|
|
var e events.Event
|
|
var at, data string
|
|
if err := rows.Scan(&e.ID, &at, &e.Kind, &e.TaskID, &e.Repo, &e.Issue,
|
|
&e.Workflow, &e.Stage, &e.Attempt, &e.ActorID, &e.ActorKind,
|
|
&e.PrincipalID, &e.Detail, &data); err != nil {
|
|
return nil, err
|
|
}
|
|
e.At, _ = time.Parse(time.RFC3339Nano, at)
|
|
_ = json.Unmarshal([]byte(data), &e.Data)
|
|
out = append(out, e)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
const eventCols = "id, at, kind, task_id, repo, issue, workflow, stage, attempt, actor_id, actor_kind, principal_id, detail, data"
|
|
|
|
// EventsSince returns up to limit events with id > sinceID, oldest
|
|
// first -- SSE catch-up and the live feed.
|
|
func (s *Store) EventsSince(ctx context.Context, sinceID int64, limit int) ([]events.Event, error) {
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT `+eventCols+` FROM events WHERE id > ? ORDER BY id LIMIT ?`, sinceID, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return scanEvents(rows)
|
|
}
|
|
|
|
// TaskEvents returns a task's full timeline, oldest first.
|
|
func (s *Store) TaskEvents(ctx context.Context, taskID int64) ([]events.Event, error) {
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT `+eventCols+` FROM events WHERE task_id = ? ORDER BY id`, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return scanEvents(rows)
|
|
}
|
|
|
|
// Tasks returns all tasks, newest first (dashboard listing).
|
|
func (s *Store) Tasks(ctx context.Context, limit int) (tasks []workflow.Task, retErr error) {
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
SELECT id, owner, repo, issue_number, title, status, workflow, stage,
|
|
pr_number, tokens_used, iterations, attempt, park_reason, retry_count,
|
|
created_at, updated_at, plan, source, identity, binding_id, binding_version
|
|
FROM tasks ORDER BY updated_at DESC LIMIT ?`, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
retErr = errors.Join(retErr, rows.Close())
|
|
}()
|
|
for rows.Next() {
|
|
var t workflow.Task
|
|
// Plan and Source are read by the dashboard: Plan gates the
|
|
// "Decision required" panel on a waiting_human task, and Source
|
|
// decides whether an issue link is rendered at all -- a chat task's
|
|
// issue number is synthetic and links nowhere.
|
|
if err := rows.Scan(&t.ID, &t.Owner, &t.Repo, &t.IssueNumber, &t.Title,
|
|
&t.Status, &t.Workflow, &t.Stage, &t.PRNumber, &t.TokensUsed,
|
|
&t.Iterations, &t.Attempt, &t.ParkReason, &t.RetryCount,
|
|
sqliteTime{&t.CreatedAt}, sqliteTime{&t.UpdatedAt},
|
|
&t.Plan, &t.Source, &t.Identity, &t.BindingID, &t.BindingVersion); err != nil {
|
|
return nil, err
|
|
}
|
|
tasks = append(tasks, t)
|
|
}
|
|
return tasks, rows.Err()
|
|
}
|
|
|
|
// StatusCounts returns task counts by status.
|
|
func (s *Store) StatusCounts(ctx context.Context) (counts map[string]int, retErr error) {
|
|
rows, err := s.db.QueryContext(ctx, `SELECT status, COUNT(*) FROM tasks GROUP BY status`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
retErr = errors.Join(retErr, rows.Close())
|
|
}()
|
|
counts = map[string]int{}
|
|
for rows.Next() {
|
|
var st string
|
|
var n int
|
|
if err := rows.Scan(&st, &n); err != nil {
|
|
return nil, err
|
|
}
|
|
counts[st] = n
|
|
}
|
|
return counts, rows.Err()
|
|
}
|
|
|
|
// WorkflowStats aggregates outcomes and spend per workflow -- the
|
|
// core "is archie getting better per dollar" table.
|
|
func (s *Store) WorkflowStats(ctx context.Context) (stats []WorkflowStat, retErr error) {
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
SELECT workflow, COUNT(*),
|
|
SUM(status='merged'), SUM(status='pr_open'), SUM(status='parked'),
|
|
CAST(AVG(tokens_used) AS INTEGER), AVG(iterations), SUM(tokens_used)
|
|
FROM tasks WHERE workflow != '' GROUP BY workflow ORDER BY COUNT(*) DESC`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
retErr = errors.Join(retErr, rows.Close())
|
|
}()
|
|
for rows.Next() {
|
|
var w WorkflowStat
|
|
if err := rows.Scan(&w.Workflow, &w.Runs, &w.Merged, &w.PROpen, &w.Parked,
|
|
&w.AvgTokens, &w.AvgSteps, &w.TotalToken); err != nil {
|
|
return nil, err
|
|
}
|
|
stats = append(stats, w)
|
|
}
|
|
return stats, rows.Err()
|
|
}
|
|
|
|
// StageStats aggregates stage_finish events -- where does time go, and
|
|
// which stages fail.
|
|
func (s *Store) StageStats(ctx context.Context) (stats []StageStat, retErr error) {
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
SELECT workflow, stage, COUNT(*),
|
|
CAST(AVG(json_extract(data,'$.duration_ms')) AS INTEGER),
|
|
SUM(CASE WHEN json_extract(data,'$.error') IS NOT NULL THEN 1 ELSE 0 END)
|
|
FROM events WHERE kind = 'stage_finish'
|
|
GROUP BY workflow, stage ORDER BY workflow, stage`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
retErr = errors.Join(retErr, rows.Close())
|
|
}()
|
|
for rows.Next() {
|
|
var st StageStat
|
|
var avg sql.NullInt64
|
|
if err := rows.Scan(&st.Workflow, &st.Stage, &st.Runs, &avg, &st.Errors); err != nil {
|
|
return nil, err
|
|
}
|
|
st.AvgMs = int(avg.Int64)
|
|
stats = append(stats, st)
|
|
}
|
|
return stats, rows.Err()
|
|
}
|
|
|
|
// TokensByDay sums task token spend per UTC day. Agent-finish events are
|
|
// the per-run accounting source; tasks.tokens_used is the durable fallback for
|
|
// older or remote runs whose events were not persisted.
|
|
func (s *Store) TokensByDay(ctx context.Context, days int) (tokens []DayTokens, retErr error) {
|
|
tokens = make([]DayTokens, 0)
|
|
rows, err := s.db.QueryContext(ctx, `
|
|
WITH event_totals AS (
|
|
SELECT task_id, substr(at,1,10) AS day,
|
|
SUM(CASE WHEN json_type(data,'$.tokens') IN ('integer','real')
|
|
THEN CAST(json_extract(data,'$.tokens') AS INTEGER) ELSE 0 END) AS tokens,
|
|
SUM(CASE WHEN json_type(data,'$.tokens') IN ('integer','real') THEN 1 ELSE 0 END) AS valid_tokens
|
|
FROM events WHERE kind = 'agent_finish'
|
|
GROUP BY task_id, day
|
|
), usage AS (
|
|
SELECT day, tokens FROM event_totals WHERE valid_tokens > 0
|
|
UNION ALL
|
|
SELECT substr(t.updated_at,1,10), t.tokens_used
|
|
FROM tasks t
|
|
WHERE t.tokens_used != 0 AND NOT EXISTS (
|
|
SELECT 1 FROM event_totals e
|
|
WHERE e.task_id = t.id AND e.valid_tokens > 0
|
|
)
|
|
)
|
|
SELECT day, CAST(SUM(tokens) AS INTEGER)
|
|
FROM usage GROUP BY day ORDER BY day DESC LIMIT ?`, days)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() {
|
|
retErr = errors.Join(retErr, rows.Close())
|
|
}()
|
|
for rows.Next() {
|
|
var d DayTokens
|
|
if err := rows.Scan(&d.Day, &d.Tokens); err != nil {
|
|
return nil, err
|
|
}
|
|
tokens = append(tokens, d)
|
|
}
|
|
return tokens, rows.Err()
|
|
}
|