fix(daemon): claim binding dispatch and enqueue its task atomically
EnqueueBindingTask now takes the capture and claims the ledger row, inserts the task, stamps its provenance and links the ledger row in one State Store transaction. A failure anywhere leaves the delivery undispatched for the next cycle; a replay or concurrent claim is refused by the ledger's unique index. SetDispatchTask is removed.
This commit is contained in:
11 files changed
+833
-837
No files matched your search
@@ -550,15 +550,6 @@ func (s *EDA) RecordDispatch(ctx context.Context, bindingID string, bindingVersi
|
||||
return ledgerWrite(err, "edastore: record dispatch")
|
||||
}
|
||||
|
||||
// SetDispatchTask records the task a claimed dispatch started.
|
||||
func (s *EDA) SetDispatchTask(ctx context.Context, bindingID, captureID string, taskID int64) error {
|
||||
err := s.q.SetBindingDispatchTask(ctx, postgresdb.SetBindingDispatchTaskParams{Binding: bindingID, Capture: captureID, TaskID: taskID})
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: set dispatch task: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListDispatches returns the newest ledger rows matching filter.
|
||||
func (s *EDA) ListDispatches(ctx context.Context, filter storecontract.DispatchFilter) ([]storecontract.Dispatch, error) {
|
||||
params := postgresdb.ListBindingDispatchesParams{EntryLimit: int32(min(max(filter.Limit, 1), 500)), ScopeOrg: scopeOrg(ctx)} // clamped
|
||||
|
||||
@@ -156,11 +156,15 @@ func (s *Store) EnqueueIssue(ctx context.Context, owner, repo string, number int
|
||||
// EnqueueChatTask inserts a queued chat task with a store-allocated synthetic
|
||||
// issue number. origin names the conversation that created it, if any.
|
||||
func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity, origin string, inputs map[string]any) (*workflow.Task, error) {
|
||||
return insertChatTask(ctx, s.queries(), owner, repo, title, body, wf, identity, origin, inputs)
|
||||
}
|
||||
|
||||
func insertChatTask(ctx context.Context, q *postgresdb.Queries, owner, repo, title, body, wf, identity, origin string, inputs map[string]any) (*workflow.Task, error) {
|
||||
encoded, err := task.EncodeInputs(inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, err := s.queries().InsertChatTask(ctx, postgresdb.InsertChatTaskParams{
|
||||
t, err := q.InsertChatTask(ctx, postgresdb.InsertChatTaskParams{
|
||||
ScopeOrg: scopeOrg(ctx),
|
||||
Owner: owner, Repo: repo, Title: title, Body: body, Workflow: wf, Identity: identity,
|
||||
Inputs: encoded,
|
||||
@@ -176,20 +180,46 @@ func (s *Store) EnqueueChatTask(ctx context.Context, owner, repo, title, body, w
|
||||
return taskFromRow(t), nil
|
||||
}
|
||||
|
||||
// EnqueueBindingTask enqueues a binding-triggered task and stamps its binding
|
||||
// provenance in a second statement (best-effort, as in the SQLite store). The
|
||||
// inputs travel with the insert, so a failed stamp leaves a task whose inputs
|
||||
// are already right and whose provenance is absent, never the reverse.
|
||||
func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int, inputs map[string]any) (*workflow.Task, error) {
|
||||
t, err := s.EnqueueChatTask(ctx, owner, repo, title, body, wf, identity, "", inputs)
|
||||
// EnqueueBindingTask claims the dispatch ledger row for the capture's
|
||||
// delivery, inserts the task, stamps its binding provenance and records the
|
||||
// task on the ledger row in one transaction. A concurrent claim of the same
|
||||
// delivery waits on the ledger's unique index and is refused once this one
|
||||
// commits, so a replay creates no second task, and a failure anywhere leaves
|
||||
// the delivery undispatched for the next cycle.
|
||||
func (s *Store) EnqueueBindingTask(ctx context.Context, owner, repo, title, body, wf, identity, bindingID string, bindingVersion int, captureID string, inputs map[string]any) (*workflow.Task, error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.queries().StampTaskBinding(ctx, postgresdb.StampTaskBindingParams{
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
|
||||
n, err := q.InsertBindingDispatch(ctx, postgresdb.InsertBindingDispatchParams{
|
||||
Binding: bindingID, BindingVersion: int64(bindingVersion), Capture: captureID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ledgerWrite(err, "store: claim binding dispatch")
|
||||
}
|
||||
if n == 0 {
|
||||
return nil, fmt.Errorf("store: claim binding dispatch: capture %q not found", captureID)
|
||||
}
|
||||
t, err := insertChatTask(ctx, q, owner, repo, title, body, wf, identity, "", inputs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := q.StampTaskBinding(ctx, postgresdb.StampTaskBindingParams{
|
||||
ID: t.ID, BindingID: bindingID, BindingVersion: int64(bindingVersion),
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("store: stamp binding provenance: %w", err)
|
||||
}
|
||||
if err := q.SetBindingDispatchTask(ctx, postgresdb.SetBindingDispatchTaskParams{
|
||||
Binding: bindingID, Capture: captureID, TaskID: t.ID,
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("store: record dispatch task: %w", err)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.BindingID = bindingID
|
||||
t.BindingVersion = bindingVersion
|
||||
return t, nil
|
||||
|
||||
Reference in new issue
Block a user