Simplify codebase per audit of 8b89f14

This commit is contained in:
Ian Gulliver
2026-10-23 15:10:06 -07:00
parent a53b293528
commit b5a72d713b
14 changed files with 361 additions and 820 deletions
+3 -3
View File
@@ -414,7 +414,7 @@ func (m *Manager) recoverSession(ctx context.Context, sess api.AgentSession) err
l, err := m.ensureLive(ctx, sess, task)
if err != nil {
return m.failResume(ctx, sess, task, err)
return m.failResume(ctx, sess, err)
}
notice := restartNotice
@@ -429,7 +429,7 @@ func (m *Manager) recoverSession(ctx context.Context, sess api.AgentSession) err
}
err = m.message(ctx, sess, task, l, "byre", notice, nil)
if err != nil {
return m.failResume(ctx, sess, task, err)
return m.failResume(ctx, sess, err)
}
slog.Info("resumed session", "session", sess.Id, "task", task.Id, "owner", sess.Owner)
m.logUndelivered(ctx, sess.Id, "resumed with restart notice")
@@ -508,7 +508,7 @@ func (m *Manager) Recover(ctx context.Context) error {
return nil
}
func (m *Manager) failResume(ctx context.Context, sess api.AgentSession, task api.Task, err error) error {
func (m *Manager) failResume(ctx context.Context, sess api.AgentSession, err error) error {
slog.Error("resume session", "session", sess.Id, "err", err)
if sess.Role == api.Reviewer {
r, rerr := m.store.ReviewBySession(ctx, sess.Id)
-4
View File
@@ -256,10 +256,6 @@ func (m *Manager) fileError(ctx context.Context, taskID, msg string) {
slog.Error("list sessions to file error on", "task", taskID, "error", msg, "err", err)
return
}
m.fileTaskError(ctx, task, sessions, msg)
}
func (m *Manager) fileTaskError(ctx context.Context, task api.Task, sessions []api.AgentSession, msg string) {
if len(sessions) == 0 {
slog.Error("no session to file task error on", "task", task.Id, "error", msg)
return
+7 -19
View File
@@ -77,14 +77,6 @@ func reviewProblem(def api.WorkflowDefinition) error {
return nil
}
func (m *Manager) TaskWorkflow(ctx context.Context, taskID string) (api.TaskWorkflow, error) {
frozen, err := m.store.TaskWorkflow(ctx, taskID)
if err != nil {
return api.TaskWorkflow{}, err
}
return api.TaskWorkflow{Name: frozen.Name, Definition: frozen.Definition}, nil
}
func (m *Manager) WorkflowRun(ctx context.Context, taskID string) (api.WorkflowRun, error) {
task, err := m.store.GetTask(ctx, taskID)
if err != nil {
@@ -306,14 +298,14 @@ func (m *Manager) startStage(ctx context.Context, project api.Project, creator a
if err != nil {
return api.Task{}, err
}
_, err = m.beginStage(ctx, task, project, creator, frozen, parents, actor)
err = m.beginStage(ctx, task, project, creator, frozen, parents, actor)
if err != nil {
return task, fmt.Errorf("task %s created, but its agent did not start: %w", task.Id, err)
}
return task, nil
}
func (m *Manager) beginStage(ctx context.Context, task api.Task, project api.Project, creator api.User, frozen store.FrozenWorkflow, parents []parentStage, actor string) (api.AgentSession, error) {
func (m *Manager) beginStage(ctx context.Context, task api.Task, project api.Project, creator api.User, frozen store.FrozenWorkflow, parents []parentStage, actor string) error {
stage := *task.WorkflowStage
after := []string{}
for _, p := range parents {
@@ -327,19 +319,15 @@ func (m *Manager) beginStage(ctx context.Context, task api.Task, project api.Pro
Payload: map[string]any{"id": task.Id, "workflow": frozen.Name, "run": *task.ParentTaskId, "stage": stage, "after": after},
})
if err != nil {
return api.AgentSession{}, err
return err
}
s := workflow.Get(frozen.Definition, stage)
message := stageMessage(frozen, stage, parents, project.DefaultBranch)
if s.Agents == nil {
return m.create(ctx, task, creator, "", "byre", Prompt{Text: message, Model: s.Model, Effort: s.Effort})
_, err = m.create(ctx, task, creator, "", "byre", Prompt{Text: message, Model: s.Model, Effort: s.Effort})
return err
}
err = m.startTeam(ctx, task, creator, message, *s.Agents, "byre")
if err != nil {
return api.AgentSession{}, err
}
sess, _, err := m.liveSession(ctx, task.Id)
return sess, err
return m.startTeam(ctx, task, creator, message, *s.Agents, "byre")
}
func stageMessage(frozen store.FrozenWorkflow, stage string, parents []parentStage, defaultBranch string) string {
@@ -576,7 +564,7 @@ func (m *Manager) resumeStage(ctx context.Context, task api.Task, project api.Pr
return nil, err
}
if len(sessions) == 0 {
_, err := m.beginStage(ctx, task, project, creator, frozen, parents, "byre")
err := m.beginStage(ctx, task, project, creator, frozen, parents, "byre")
if err != nil {
return nil, err
}
+329 -574
View File
File diff suppressed because it is too large Load Diff
+11 -11
View File
@@ -53,7 +53,7 @@ func Inspect(ctx context.Context, path string) (RepoInfo, error) {
return RepoInfo{}, err
}
remote, _, code, err := runCode(ctx, root, nil, "remote", "get-url", Remote)
remote, _, code, err := runCode(ctx, root, "remote", "get-url", Remote)
if err != nil {
return RepoInfo{}, err
}
@@ -65,7 +65,7 @@ func Inspect(ctx context.Context, path string) (RepoInfo, error) {
}
func HasRemote(ctx context.Context, repo string) (bool, error) {
_, _, code, err := runCode(ctx, repo, nil, "remote", "get-url", Remote)
_, _, code, err := runCode(ctx, repo, "remote", "get-url", Remote)
return err == nil && code == 0, err
}
@@ -92,7 +92,7 @@ func AddWorktree(ctx context.Context, repo, path, branch, base string) (string,
}
func EnsureBranchCommit(ctx context.Context, repo, branch string, env []string) error {
_, _, code, err := runCode(ctx, repo, nil, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch+"^{commit}")
_, _, code, err := runCode(ctx, repo, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch+"^{commit}")
if err != nil || code == 0 {
return err
}
@@ -152,7 +152,7 @@ func RemoveWorktree(ctx context.Context, repo, path, branch string) error {
}
}
_, _, code, err := runCode(ctx, repo, nil, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch)
_, _, code, err := runCode(ctx, repo, "rev-parse", "--verify", "--quiet", "refs/heads/"+branch)
if err != nil || code != 0 {
return err
}
@@ -206,7 +206,7 @@ func SquashCommit(ctx context.Context, repo, worktree, target, message string, e
return "", err
}
out, stderr, code, err := runCode(ctx, repo, nil, "merge-tree", "--write-tree", "--name-only", "--merge-base="+base, head, tree)
out, stderr, code, err := runCode(ctx, repo, "merge-tree", "--write-tree", "--name-only", "--merge-base="+base, head, tree)
if err != nil {
return "", err
}
@@ -237,7 +237,7 @@ func SquashCommit(ctx context.Context, repo, worktree, target, message string, e
if err != nil {
return "", err
}
signing, stderr, code, err := runCode(ctx, repo, nil, "config", "--type=bool", "--get", "commit.gpgsign")
signing, stderr, code, err := runCode(ctx, repo, "config", "--type=bool", "--get", "commit.gpgsign")
if err != nil {
return "", err
}
@@ -340,7 +340,7 @@ func BranchHead(ctx context.Context, repo, branch string) (string, error) {
}
func IsAncestor(ctx context.Context, dir, ancestor, descendant string) (bool, error) {
_, stderr, code, err := runCode(ctx, dir, nil, "merge-base", "--is-ancestor", ancestor, descendant)
_, stderr, code, err := runCode(ctx, dir, "merge-base", "--is-ancestor", ancestor, descendant)
if err != nil {
return false, err
}
@@ -354,7 +354,7 @@ func IsAncestor(ctx context.Context, dir, ancestor, descendant string) (bool, er
}
func MergeInProgress(ctx context.Context, dir string) (bool, error) {
_, _, code, err := runCode(ctx, dir, nil, "rev-parse", "--quiet", "--verify", "MERGE_HEAD")
_, _, code, err := runCode(ctx, dir, "rev-parse", "--quiet", "--verify", "MERGE_HEAD")
return err == nil && code == 0, err
}
@@ -368,7 +368,7 @@ func AbortMerge(ctx context.Context, dir string) error {
}
func ReadFile(ctx context.Context, repo, rev, path string) (string, bool, error) {
_, _, code, err := runCode(ctx, repo, nil, "cat-file", "-e", rev+":"+path)
_, _, code, err := runCode(ctx, repo, "cat-file", "-e", rev+":"+path)
if err != nil {
return "", false, err
}
@@ -434,8 +434,8 @@ func runRaw(ctx context.Context, dir string, env []string, input string, args ..
return out, nil
}
func runCode(ctx context.Context, dir string, env []string, args ...string) (string, string, int, error) {
out, stderr, code, err := runCodeInput(context.WithoutCancel(ctx), dir, env, nil, args...)
func runCode(ctx context.Context, dir string, args ...string) (string, string, int, error) {
out, stderr, code, err := runCodeInput(context.WithoutCancel(ctx), dir, nil, nil, args...)
return strings.TrimSpace(out), stderr, code, err
}
+3 -3
View File
@@ -19,7 +19,7 @@ func ConfigureNotesFetch(ctx context.Context, repo string) error {
return err
}
key := "remote." + Remote + ".fetch"
out, stderr, code, err := runCode(ctx, repo, nil, "config", "--get-all", key)
out, stderr, code, err := runCode(ctx, repo, "config", "--get-all", key)
if err != nil {
return err
}
@@ -39,7 +39,7 @@ func NoteCommit(ctx context.Context, repo, commit, note string, env []string) (s
return "", err
}
parent, stderr, code, err := runCode(ctx, repo, nil, "rev-parse", "--verify", "--quiet", NotesRef)
parent, stderr, code, err := runCode(ctx, repo, "rev-parse", "--verify", "--quiet", NotesRef)
if err != nil {
return "", err
}
@@ -75,7 +75,7 @@ func NoteCommit(ctx context.Context, repo, commit, note string, env []string) (s
}
func AdvanceNotes(ctx context.Context, repo, notes string) error {
current, stderr, code, err := runCode(ctx, repo, nil, "rev-parse", "--verify", "--quiet", NotesRef)
current, stderr, code, err := runCode(ctx, repo, "rev-parse", "--verify", "--quiet", NotesRef)
if err != nil {
return err
}
+1 -1
View File
@@ -54,7 +54,7 @@ func RepairWorktrees(ctx context.Context, repo string, paths []string) error {
}
func RewriteNotes(ctx context.Context, repo string, rewrite func(string) string) error {
parent, stderr, code, err := runCode(ctx, repo, nil, "rev-parse", "--verify", "--quiet", NotesRef)
parent, stderr, code, err := runCode(ctx, repo, "rev-parse", "--verify", "--quiet", NotesRef)
if err != nil {
return err
}
+2 -16
View File
@@ -7,13 +7,8 @@ import (
"g.fc.run/ai/byre/internal/store"
)
func (s *Server) ListInbox(w http.ResponseWriter, r *http.Request, params api.ListInboxParams) {
f := store.InboxFilter{State: api.Open, ProjectID: api.Deref(params.ProjectId), TaskID: api.Deref(params.TaskId)}
if params.State != nil {
f.State = *params.State
}
items, err := s.store.ListInbox(r.Context(), f)
func (s *Server) ListInbox(w http.ResponseWriter, r *http.Request) {
items, err := s.store.ListInbox(r.Context(), store.InboxFilter{State: api.Open})
if err != nil {
writeStoreError(w, err)
return
@@ -21,15 +16,6 @@ func (s *Server) ListInbox(w http.ResponseWriter, r *http.Request, params api.Li
writeJSON(w, http.StatusOK, items)
}
func (s *Server) GetInboxItem(w http.ResponseWriter, r *http.Request, itemID string) {
item, err := s.store.GetInboxItem(r.Context(), itemID)
if err != nil {
writeStoreError(w, err)
return
}
writeJSON(w, http.StatusOK, item)
}
func (s *Server) DismissInbox(w http.ResponseWriter, r *http.Request, itemID string) {
item, err := s.agents.Dismiss(r.Context(), actor(r), itemID)
if err != nil {
-13
View File
@@ -7,19 +7,6 @@ import (
"g.fc.run/ai/byre/internal/api"
)
func (s *Server) ListReviews(w http.ResponseWriter, r *http.Request, taskID string) {
if !s.taskExists(w, r, taskID) {
return
}
reviews, err := s.store.ListReviews(r.Context(), taskID)
if err != nil {
writeStoreError(w, err)
return
}
writeJSON(w, http.StatusOK, reviews)
}
func (s *Server) SetFindingState(w http.ResponseWriter, r *http.Request, reviewID string, index int) {
req := api.FindingStateUpdate{}
if !readJSON(w, r, &req) {
-31
View File
@@ -355,15 +355,6 @@ func (s *Server) createProject(ctx context.Context, name, repoPath, createdBy st
return p, nil
}
func (s *Server) GetProject(w http.ResponseWriter, r *http.Request, projectID string) {
p, err := s.store.GetProject(r.Context(), projectID)
if err != nil {
writeStoreError(w, err)
return
}
writeJSON(w, http.StatusOK, p)
}
func (s *Server) ListActiveTasks(w http.ResponseWriter, r *http.Request, projectID string) {
_, err := s.store.GetProject(r.Context(), projectID)
if err != nil {
@@ -777,19 +768,6 @@ func (s *Server) ListTaskEvents(w http.ResponseWriter, r *http.Request, taskID s
writeJSON(w, http.StatusOK, events)
}
func (s *Server) ListSnapshots(w http.ResponseWriter, r *http.Request, taskID string) {
if !s.taskExists(w, r, taskID) {
return
}
snaps, err := s.store.ListSnapshots(r.Context(), taskID)
if err != nil {
writeStoreError(w, err)
return
}
writeJSON(w, http.StatusOK, snaps)
}
func (s *Server) ListPreviews(w http.ResponseWriter, r *http.Request, taskID string) {
if !s.taskExists(w, r, taskID) {
return
@@ -922,15 +900,6 @@ func (s *Server) CreateSession(w http.ResponseWriter, r *http.Request, taskID st
writeJSON(w, http.StatusCreated, sess)
}
func (s *Server) GetSession(w http.ResponseWriter, r *http.Request, sessionID string) {
sess, err := s.store.GetSession(r.Context(), sessionID)
if err != nil {
writeStoreError(w, err)
return
}
writeJSON(w, http.StatusOK, sess)
}
func (s *Server) SendMessage(w http.ResponseWriter, r *http.Request, sessionID string) {
prompt, ok := s.readMessage(w, r)
if !ok {
-9
View File
@@ -78,15 +78,6 @@ func (s *Server) DeleteWorkflow(w http.ResponseWriter, r *http.Request, name str
w.WriteHeader(http.StatusNoContent)
}
func (s *Server) GetTaskWorkflow(w http.ResponseWriter, r *http.Request, taskID string) {
tw, err := s.agents.TaskWorkflow(r.Context(), taskID)
if err != nil {
writeStoreError(w, err)
return
}
writeJSON(w, http.StatusOK, tw)
}
func (s *Server) GetWorkflowRun(w http.ResponseWriter, r *http.Request, taskID string) {
run, err := s.agents.WorkflowRun(r.Context(), taskID)
if err != nil {
+2 -12
View File
@@ -72,28 +72,18 @@ func (s *Store) GetInboxItem(ctx context.Context, id string) (api.InboxItem, err
}
type InboxFilter struct {
ProjectID string
TaskID string
SessionID string
State api.InboxState
TaskID string
State api.InboxState
}
func (s *Store) ListInbox(ctx context.Context, f InboxFilter) ([]api.InboxItem, error) {
where := []string{"state = ?"}
args := []any{string(f.State)}
if f.ProjectID != "" {
where = append(where, "project_id = ?")
args = append(args, f.ProjectID)
}
if f.TaskID != "" {
where = append(where, "task_id = ?")
args = append(args, f.TaskID)
}
if f.SessionID != "" {
where = append(where, "agent_session_id = ?")
args = append(args, f.SessionID)
}
ids, err := queryIDs(ctx, s.db, `SELECT id FROM inbox_items WHERE `+strings.Join(where, " AND ")+` ORDER BY created_at`, args...)
if err != nil {
-18
View File
@@ -41,24 +41,6 @@ func (s *Store) CreateSnapshot(ctx context.Context, snap api.Snapshot, projectID
})
}
func (s *Store) ListSnapshots(ctx context.Context, taskID string) ([]api.Snapshot, error) {
rows, err := s.db.QueryContext(ctx, `SELECT `+snapshotColumns+` FROM snapshots WHERE task_id = ? ORDER BY n`, taskID)
if err != nil {
return nil, err
}
defer rows.Close()
snaps := []api.Snapshot{}
for rows.Next() {
snap, err := scanSnapshot(rows)
if err != nil {
return nil, err
}
snaps = append(snaps, snap)
}
return snaps, rows.Err()
}
func (s *Store) GetSnapshot(ctx context.Context, taskID string, n int) (api.Snapshot, error) {
snap, err := scanSnapshot(s.db.QueryRowContext(ctx, `SELECT `+snapshotColumns+` FROM snapshots WHERE task_id = ? AND n = ?`, taskID, n))
if errors.Is(err, sql.ErrNoRows) {