Monitoring coverage was 3 of 89 active entities. Three bugs, each hidden by discarded errors in checkdefaults: - writeCheck generated a fresh uuid, inserted the check entity ON CONFLICT (slug) DO NOTHING, then wrote a check_defs row referencing it. On any re-seed the slug already existed, the entity insert no-oped, and the FK violated — aborting the ingest transaction and surfacing as an unrelated failure several entities later. Re-seeding has been broken since; prod's coverage was frozen at its first successful seed. This is what TestSeedIngestIdempotentAndNoDuplicateEdges had been reporting. - shortSlug truncated to the last 8 chars, so all 21 ingress routes collapsed to ".network" and overwrote each other; service:jellyfin collided with lxc:jellyfin. - The ssh-script checker never read the `args` config checkdefaults wrote, so process_check.sh always ran without its unit name and returned "unknown". Coverage is now 75/89. Monitoring is declared per entity type in seeds/ontology.yaml and resolved through the is-a hierarchy, so a type can say it warrants nothing (site, lan, mesh, cluster) and never be reported as a gap. coverageSweep raises an `unmonitored` signal only where a type declares monitoring it lacks — 8 real gaps, no false positives. Also: - entity_types.attribute_schema was never ingested: the seed loader read "attribute_schema" but the YAML says "attributes", so all 60 types stored JSON null. - ListExecutions ignored its declared target/action/correlation_id filters and paginated on a non-unique target slug, dropping and repeating rows. - started_at was captured but only written at terminal state, so a running execution reported NULL for its whole life. The three MCP auto-run copies wrote no timing at all; they are now one autoRun helper. - SSH output was buffered to completion and discarded entirely on timeout. Both sshExec copies now stream through a shared execlog sink into execution_logs, and keep partial output when a command is cancelled. - executions.correlation_id was a random per-execution uuid that correlated nothing; it is now the chat session id, which is what lets the chat tail live output. - reversible_low had no auto-run branch despite policy declaring it unattended. Since computeCommandRisk never returns it, the class only arises when an agent declares it over a read_only command — so gating it penalised candor without adding safety. - backup-target gains a backup-freshness checker (portable find -mmin, since the first target is on macOS), resolving its host by walking backs-up-to backwards. The pre-deploy pg_dump is now a tracked backup target. UI: an Executions section on entity detail with live output tailing, and streamed output under a running `run` call in the chat timeline. Migrations 022-024. Ops.svelte and context.ts exclude execution.output from their refetch triggers, which would otherwise fire once a second per command. Co-Authored-By: Claude <noreply@anthropic.com>
136 lines
3.8 KiB
Go
136 lines
3.8 KiB
Go
// Package execlog persists incremental command output for an execution and
|
|
// announces it on the event stream.
|
|
//
|
|
// It exists as its own package because both SSH execution paths need it —
|
|
// internal/mcp (the agent's auto-run windows) and internal/httpapi (the
|
|
// post-approval actuator). Those two already carry near-identical copies of
|
|
// sshExec, and every bug found in this area so far has been a case of the two
|
|
// copies drifting apart; one shared sink is the cheap way not to repeat that.
|
|
package execlog
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/dtoro/oikos/internal/db"
|
|
"github.com/dtoro/oikos/internal/db/sqlcgen"
|
|
"github.com/dtoro/oikos/internal/observability"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// eventInterval throttles execution.output events. Chunks are persisted as
|
|
// they arrive, but a chatty command (apt, a long build) can produce hundreds
|
|
// per second and the SSE broker drops events for slow subscribers — flooding
|
|
// it would push out the signal.* and approval.* events that actually need to
|
|
// arrive. The event is only a "there is more output" ping; subscribers re-read
|
|
// the rows.
|
|
const eventInterval = time.Second
|
|
|
|
// Sink receives output chunks as they arrive from a remote command.
|
|
type Sink func(stream string, chunk []byte)
|
|
|
|
// New returns a Sink that writes chunks to execution_logs and emits a
|
|
// throttled execution.output event, plus a Flush to call when the command
|
|
// finishes.
|
|
//
|
|
// The returned Sink is safe for concurrent use: stdout and stderr are written
|
|
// from separate goroutines.
|
|
func New(ctx context.Context, pool *db.Pool, execID uuid.UUID, correlationID string) (Sink, func()) {
|
|
var (
|
|
mu sync.Mutex
|
|
seq int
|
|
lastEvent time.Time
|
|
pending bool
|
|
)
|
|
|
|
emit := func() {
|
|
if err := observability.Event(ctx, sqlcgen.New(pool), "execution.output", &execID,
|
|
"info", "actuator", correlationID, map[string]any{"execution_id": execID.String()}); err != nil {
|
|
slog.Debug("execlog: emit output event", "error", err, "execution_id", execID)
|
|
}
|
|
}
|
|
|
|
sink := func(stream string, chunk []byte) {
|
|
if len(chunk) == 0 {
|
|
return
|
|
}
|
|
mu.Lock()
|
|
seq++
|
|
n := seq
|
|
mu.Unlock()
|
|
|
|
// A failed log write must never fail the command: this is observability,
|
|
// and the authoritative output still lands in executions.result at the
|
|
// end. Log and carry on.
|
|
if _, err := pool.Exec(ctx,
|
|
`INSERT INTO execution_logs (execution_id, seq, stream, chunk)
|
|
VALUES ($1, $2, $3, $4)`,
|
|
execID, n, stream, string(chunk)); err != nil {
|
|
slog.Debug("execlog: persist chunk", "error", err, "execution_id", execID)
|
|
return
|
|
}
|
|
|
|
mu.Lock()
|
|
due := time.Since(lastEvent) >= eventInterval
|
|
if due {
|
|
lastEvent = time.Now()
|
|
pending = false
|
|
} else {
|
|
pending = true
|
|
}
|
|
mu.Unlock()
|
|
|
|
if due {
|
|
emit()
|
|
}
|
|
}
|
|
|
|
// Flush emits a final event when output arrived inside the throttle window,
|
|
// so the last few lines of a short command are not left unannounced.
|
|
flush := func() {
|
|
mu.Lock()
|
|
due := pending
|
|
pending = false
|
|
mu.Unlock()
|
|
if due {
|
|
emit()
|
|
}
|
|
}
|
|
|
|
return sink, flush
|
|
}
|
|
|
|
// Read returns an execution's persisted output in order.
|
|
func Read(ctx context.Context, pool *db.Pool, execID uuid.UUID, limit int) ([]Chunk, error) {
|
|
if limit <= 0 {
|
|
limit = 1000
|
|
}
|
|
rows, err := pool.Query(ctx,
|
|
`SELECT seq, stream, chunk, ts FROM execution_logs
|
|
WHERE execution_id = $1 ORDER BY seq LIMIT $2`, execID, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var out []Chunk
|
|
for rows.Next() {
|
|
var c Chunk
|
|
if err := rows.Scan(&c.Seq, &c.Stream, &c.Chunk, &c.TS); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// Chunk is one persisted slice of command output.
|
|
type Chunk struct {
|
|
Seq int `json:"seq"`
|
|
Stream string `json:"stream"`
|
|
Chunk string `json:"chunk"`
|
|
TS time.Time `json:"ts"`
|
|
}
|