package runner import ( "amadan.net/oficina/equipo/internal/runner/containment" "context" "encoding/json" ) func (r *Runtime) cachedPreflight(ctx context.Context, id, key string, probe func() error) error { if err := ctx.Err(); err != nil { return err } r.mu.Lock() defer r.mu.Unlock() if r.preflightCache == nil { r.preflightCache = map[string]string{} } if r.preflightCache[id] == key && key != "" { return nil } delete(r.preflightCache, id) if err := probe(); err != nil { return err } r.preflightCache[id] = key return nil } func (j *Journal) prunePreflights(ctx context.Context, forget func(context.Context, containment.Record) error) error { rows, err := j.db.QueryContext(ctx, `SELECT a.attempt,a.record FROM direct_attempts a WHERE contained=1 AND (attempt LIKE 'codex-preflight-%' OR attempt LIKE 'claude-preflight-%') AND NOT EXISTS(SELECT 1 FROM direct_turns t WHERE t.attempt=a.attempt) AND NOT EXISTS(SELECT 1 FROM direct_frames f WHERE f.stream=a.stream) AND NOT EXISTS(SELECT 1 FROM direct_events e WHERE e.attempt=a.attempt) AND NOT EXISTS(SELECT 1 FROM journal_supervisor s WHERE s.kind='namespace' AND json_extract(s.body,'$.Record.AttemptKey')=a.attempt)`) if err != nil { return err } var records []containment.Record for rows.Next() { var attempt string var raw []byte if err = rows.Scan(&attempt, &raw); err != nil { break } var record containment.Record if err = json.Unmarshal(raw, &record); err != nil { break } records = append(records, record) } if err == nil { err = rows.Err() } rows.Close() if err != nil { return err } for _, record := range records { if err = forget(ctx, record); err != nil { return err } if _, err = j.db.ExecContext(ctx, "DELETE FROM direct_attempts WHERE attempt=? AND contained=1", record.AttemptKey); err != nil { return err } } return nil }