Run workflow stages with finish_stage handoffs and lead signals
This commit is contained in:
+28
-8
@@ -347,6 +347,16 @@ CREATE TABLE workflows (
|
||||
);
|
||||
ALTER TABLE tasks ADD COLUMN workflow_name TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE tasks ADD COLUMN workflow_definition TEXT NOT NULL DEFAULT '';
|
||||
`}, {"run-workflow-stages", `
|
||||
ALTER TABLE tasks ADD COLUMN workflow_brief TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE agent_sessions ADD COLUMN workflow_stage INTEGER NOT NULL DEFAULT 0;
|
||||
ALTER TABLE agent_sessions ADD COLUMN stage_output TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE agent_sessions ADD COLUMN stage_finished INTEGER NOT NULL DEFAULT 0;
|
||||
UPDATE workflows SET definition = json_set(definition, '$.stages',
|
||||
(SELECT json_group_array(json_remove(value, '$.vendor')) FROM (SELECT value FROM json_each(definition, '$.stages') ORDER BY key)));
|
||||
UPDATE tasks SET workflow_definition = json_set(workflow_definition, '$.stages',
|
||||
(SELECT json_group_array(json_remove(value, '$.vendor')) FROM (SELECT value FROM json_each(workflow_definition, '$.stages') ORDER BY key)))
|
||||
WHERE workflow_definition != '';
|
||||
`}}
|
||||
|
||||
const (
|
||||
@@ -615,6 +625,7 @@ func (s *Store) GetProject(ctx context.Context, id string) (api.Project, error)
|
||||
func (s *Store) CreateTask(ctx context.Context, t api.Task, frozen *FrozenWorkflow, actor string) error {
|
||||
workflowName := ""
|
||||
workflowDefinition := ""
|
||||
workflowBrief := ""
|
||||
if frozen != nil {
|
||||
b, err := json.Marshal(frozen.Definition)
|
||||
if err != nil {
|
||||
@@ -622,6 +633,7 @@ func (s *Store) CreateTask(ctx context.Context, t api.Task, frozen *FrozenWorkfl
|
||||
}
|
||||
workflowName = frozen.Name
|
||||
workflowDefinition = string(b)
|
||||
workflowBrief = frozen.Brief
|
||||
t.Workflow = &workflowName
|
||||
}
|
||||
todoItem := ""
|
||||
@@ -660,9 +672,9 @@ func (s *Store) CreateTask(ctx context.Context, t api.Task, frozen *FrozenWorkfl
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `
|
||||
INSERT INTO tasks (id, project_id, title, merge_title, branch, worktree_path, base_ref, port_base, color, status, created_by, created_at, todo_item, forked_from, forked_at, workflow_name, workflow_definition)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, t.Id, t.ProjectId, t.Title, mergeTitle, t.Branch, t.WorktreePath, t.BaseRef, t.PortBase, t.Color, string(t.Status), t.CreatedBy, formatTime(t.CreatedAt), todoItem, forkedFrom, forkedAt, workflowName, workflowDefinition)
|
||||
INSERT INTO tasks (id, project_id, title, merge_title, branch, worktree_path, base_ref, port_base, color, status, created_by, created_at, todo_item, forked_from, forked_at, workflow_name, workflow_definition, workflow_brief)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, t.Id, t.ProjectId, t.Title, mergeTitle, t.Branch, t.WorktreePath, t.BaseRef, t.PortBase, t.Color, string(t.Status), t.CreatedBy, formatTime(t.CreatedAt), todoItem, forkedFrom, forkedAt, workflowName, workflowDefinition, workflowBrief)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1005,11 +1017,15 @@ func (s *Store) CreateSession(ctx context.Context, sess api.AgentSession, projec
|
||||
forkedFrom = *sess.ForkedFrom
|
||||
forkedAt = *sess.ForkedAt
|
||||
}
|
||||
stage := 0
|
||||
if sess.WorkflowStage != nil {
|
||||
stage = *sess.WorkflowStage
|
||||
}
|
||||
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO agent_sessions (id, task_id, owner, vendor, model, vendor_session_id, state, role, created_by, created_at, forked_from, forked_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, sess.Id, nullable(sess.TaskId), sess.Owner, string(sess.Vendor), sess.Model, sess.VendorSessionId, string(sess.State), string(sess.Role), sess.CreatedBy, formatTime(sess.CreatedAt), forkedFrom, forkedAt)
|
||||
INSERT INTO agent_sessions (id, task_id, owner, vendor, model, vendor_session_id, state, role, created_by, created_at, forked_from, forked_at, workflow_stage)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`, sess.Id, nullable(sess.TaskId), sess.Owner, string(sess.Vendor), sess.Model, sess.VendorSessionId, string(sess.State), string(sess.Role), sess.CreatedBy, formatTime(sess.CreatedAt), forkedFrom, forkedAt, stage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1250,7 +1266,7 @@ const projectColumns = `id, name, repo_path, default_branch, remote, created_by,
|
||||
|
||||
const taskColumns = `id, project_id, title, merge_title, branch, worktree_path, base_ref, port_base, color, status, created_by, created_at, closed_at, merge_commit, todo_item, forked_from, forked_at, merge_queued_at, workflow_name`
|
||||
|
||||
const sessionColumns = `id, coalesce(task_id, ''), owner, vendor, model, vendor_session_id, state, role, created_by, created_at, forked_from, forked_at`
|
||||
const sessionColumns = `id, coalesce(task_id, ''), owner, vendor, model, vendor_session_id, state, role, created_by, created_at, forked_from, forked_at, workflow_stage`
|
||||
|
||||
type scanner interface {
|
||||
Scan(dest ...any) error
|
||||
@@ -1328,10 +1344,14 @@ func scanSession(row scanner) (api.AgentSession, error) {
|
||||
createdAt := ""
|
||||
forkedFrom := ""
|
||||
forkedAt := ""
|
||||
err := row.Scan(&sess.Id, &sess.TaskId, &sess.Owner, &vendor, &sess.Model, &sess.VendorSessionId, &state, &role, &sess.CreatedBy, &createdAt, &forkedFrom, &forkedAt)
|
||||
stage := 0
|
||||
err := row.Scan(&sess.Id, &sess.TaskId, &sess.Owner, &vendor, &sess.Model, &sess.VendorSessionId, &state, &role, &sess.CreatedBy, &createdAt, &forkedFrom, &forkedAt, &stage)
|
||||
if err != nil {
|
||||
return sess, err
|
||||
}
|
||||
if stage > 0 {
|
||||
sess.WorkflowStage = &stage
|
||||
}
|
||||
if forkedFrom != "" {
|
||||
sess.ForkedFrom = &forkedFrom
|
||||
sess.ForkedAt = &forkedAt
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
type FrozenWorkflow struct {
|
||||
Name string
|
||||
Definition api.WorkflowDefinition
|
||||
Brief string
|
||||
}
|
||||
|
||||
func (s *Store) ListWorkflows(ctx context.Context) ([]api.WorkflowSummary, error) {
|
||||
@@ -89,19 +90,104 @@ func (s *Store) FreezeWorkflow(ctx context.Context, name string) (FrozenWorkflow
|
||||
}
|
||||
|
||||
func (s *Store) TaskWorkflow(ctx context.Context, taskID string) (FrozenWorkflow, error) {
|
||||
name := ""
|
||||
f := FrozenWorkflow{}
|
||||
def := ""
|
||||
err := s.db.QueryRowContext(ctx, `SELECT workflow_name, workflow_definition FROM tasks WHERE id = ?`, taskID).Scan(&name, &def)
|
||||
err := s.db.QueryRowContext(ctx, `SELECT workflow_name, workflow_definition, workflow_brief FROM tasks WHERE id = ?`, taskID).Scan(&f.Name, &def, &f.Brief)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return FrozenWorkflow{}, fmt.Errorf("%w: no task %s", ErrNotFound, taskID)
|
||||
}
|
||||
if err != nil {
|
||||
return FrozenWorkflow{}, err
|
||||
}
|
||||
if name == "" {
|
||||
if f.Name == "" {
|
||||
return FrozenWorkflow{}, fmt.Errorf("%w: task %s has no workflow", ErrNotFound, taskID)
|
||||
}
|
||||
f := FrozenWorkflow{Name: name}
|
||||
err = json.Unmarshal([]byte(def), &f.Definition)
|
||||
return f, err
|
||||
}
|
||||
|
||||
func (s *Store) SetStageOutput(ctx context.Context, sessionID, output string) error {
|
||||
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
||||
_, err := tx.ExecContext(ctx, `UPDATE agent_sessions SET stage_output = ? WHERE id = ?`, output, sessionID)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) FinishStage(ctx context.Context, sessionID string) (bool, error) {
|
||||
finished := false
|
||||
err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
||||
res, err := tx.ExecContext(ctx, `UPDATE agent_sessions SET stage_finished = 1 WHERE id = ? AND stage_output != ''`, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := res.RowsAffected()
|
||||
finished = n > 0
|
||||
return err
|
||||
})
|
||||
return finished, err
|
||||
}
|
||||
|
||||
func (s *Store) DropStageOutput(ctx context.Context, sessionID string) error {
|
||||
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
||||
_, err := tx.ExecContext(ctx, `UPDATE agent_sessions SET stage_output = '' WHERE id = ? AND stage_finished = 0`, sessionID)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) StageOutput(ctx context.Context, sessionID string) (string, error) {
|
||||
output := ""
|
||||
err := s.db.QueryRowContext(ctx, `SELECT stage_output FROM agent_sessions WHERE id = ?`, sessionID).Scan(&output)
|
||||
return output, err
|
||||
}
|
||||
|
||||
func (s *Store) FinishedStages(ctx context.Context) ([]string, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT id FROM agent_sessions WHERE stage_finished = 1 ORDER BY created_at`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
id := ""
|
||||
err = rows.Scan(&id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) ClearStage(ctx context.Context, sessionID string) error {
|
||||
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
||||
return clearStage(ctx, tx, sessionID)
|
||||
})
|
||||
}
|
||||
|
||||
func clearStage(ctx context.Context, tx *sql.Tx, sessionID string) error {
|
||||
_, err := tx.ExecContext(ctx, `UPDATE agent_sessions SET stage_output = '', stage_finished = 0 WHERE id = ?`, sessionID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) FinishWorkflow(ctx context.Context, task api.Task, sessionID, workflowName, output string) error {
|
||||
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
||||
err := clearStage(ctx, tx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return emit(api.Event{
|
||||
Kind: "task.workflow_finished",
|
||||
ProjectId: &task.ProjectId,
|
||||
TaskId: &task.Id,
|
||||
Actor: "byre",
|
||||
Payload: map[string]any{"id": task.Id, "workflow": workflowName, "output": output},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) WorkflowFinished(ctx context.Context, taskID string) (bool, error) {
|
||||
finished := false
|
||||
err := s.db.QueryRowContext(ctx, `SELECT EXISTS (SELECT 1 FROM events WHERE task_id = ? AND kind = 'task.workflow_finished')`, taskID).Scan(&finished)
|
||||
return finished, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user