Merge branch 'work/mqti13'

# Conflicts:
#	internal/contracts/state/v1/state.pb.go
This commit is contained in:
sam committed 2026-10-11 04:03:52 +11:00
commit cac0f43b58
17 files changed
+1957 -1413

No files matched your search

+2 -1
View File
@@ -464,7 +464,7 @@ func (b *boot) setupContainers(ctx context.Context) func() {
// setupGatewayClient dials the Gateway, which owns every model call. The daemon
// only enqueues the chat tasks scheduled workflows create.
func (b *boot) setupGatewayClient() error {
func (b *boot) setupGatewayClient(ctx context.Context) error {
b.setupChatTasks(b.cfg)
contract, cleanup, err := composeChatContract(b.cfg.Services, b.secrets)
if err != nil {
@@ -478,6 +478,7 @@ func (b *boot) setupGatewayClient() error {
}
b.addCleanup(closeMessaging)
b.messaging = messaging
go postOutcomes(ctx, b.stateStore, messaging.Deliver, b.log)
b.setupReadinessProbes()
return nil
}
+1 -1
View File
@@ -185,7 +185,7 @@ func Run() int { //nolint:cyclop,funlen // the composition root's setup sequence
return 1
}
if err := b.setupGatewayClient(); err != nil {
if err := b.setupGatewayClient(ctx); err != nil {
return 1
}
if ps, ok := b.stateStore.(storecontract.PresenceStore); ok {
+72
View File
@@ -0,0 +1,72 @@
package archied
import (
"context"
"fmt"
"log/slog"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
const outcomePostInterval = 15 * time.Second
type outcomeDeliverer func(ctx context.Context, channel, chatID, text string) error
// postOutcomes reports each finished chat task to the conversation that
// spawned it. A task stays unposted until delivery succeeds, so a Messaging
// Service that is down or restarting only delays the report.
func postOutcomes(ctx context.Context, tasks storecontract.OutcomePoster, deliver outcomeDeliverer, log *slog.Logger) {
ticker := time.NewTicker(outcomePostInterval)
defer ticker.Stop()
for {
postPendingOutcomes(ctx, tasks, deliver, log)
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
func postPendingOutcomes(ctx context.Context, tasks storecontract.OutcomePoster, deliver outcomeDeliverer, log *slog.Logger) {
pending, err := tasks.UnpostedOutcomes(ctx)
if err != nil {
log.Warn("list unposted task outcomes", "err", err)
return
}
for _, t := range pending {
// An origin that names no chat can never be delivered; marking it
// posted keeps it from being retried forever.
if channel, chatID, ok := splitOrigin(t.Origin); ok {
if err := deliver(ctx, channel, chatID, outcomeText(t)); err != nil {
log.Warn("post task outcome", "task", t.ID, "err", err)
continue
}
}
if err := tasks.MarkOutcomePosted(ctx, t.ID); err != nil {
log.Warn("mark task outcome posted", "task", t.ID, "err", err)
}
}
}
// splitOrigin reads "<channel>:<chat>[/<thread>]", the form messaging.Origin
// writes. Delivery addresses the chat; threads are not addressable.
func splitOrigin(origin string) (channel, chatID string, ok bool) {
channel, conversation, found := strings.Cut(origin, ":")
chatID, _, _ = strings.Cut(conversation, "/")
return channel, chatID, found && channel != "" && chatID != ""
}
func outcomeText(t *task.Task) string {
text := fmt.Sprintf("Task %d (%s) finished: %s", t.ID, t.Title, t.Status)
if t.PRNumber > 0 {
text += fmt.Sprintf(", PR #%d", t.PRNumber)
}
if t.ParkReason != "" {
text += "\n" + t.ParkReason
}
return text
}
File diff suppressed because it is too large. Load diff
@@ -49,6 +49,8 @@ const (
StateStoreService_EnqueueIssue_FullMethodName = "/state.v1.StateStoreService/EnqueueIssue"
StateStoreService_EnqueueChatTask_FullMethodName = "/state.v1.StateStoreService/EnqueueChatTask"
StateStoreService_ActiveTasksByOrigin_FullMethodName = "/state.v1.StateStoreService/ActiveTasksByOrigin"
StateStoreService_UnpostedOutcomes_FullMethodName = "/state.v1.StateStoreService/UnpostedOutcomes"
StateStoreService_MarkOutcomePosted_FullMethodName = "/state.v1.StateStoreService/MarkOutcomePosted"
StateStoreService_ClaimNext_FullMethodName = "/state.v1.StateStoreService/ClaimNext"
StateStoreService_ClaimByIssue_FullMethodName = "/state.v1.StateStoreService/ClaimByIssue"
StateStoreService_Transition_FullMethodName = "/state.v1.StateStoreService/Transition"
@@ -212,6 +214,10 @@ type StateStoreServiceClient interface {
EnqueueIssue(ctx context.Context, in *EnqueueIssueRequest, opts ...grpc.CallOption) (*EnqueueIssueResponse, error)
EnqueueChatTask(ctx context.Context, in *EnqueueChatTaskRequest, opts ...grpc.CallOption) (*EnqueueChatTaskResponse, error)
ActiveTasksByOrigin(ctx context.Context, in *ActiveTasksByOriginRequest, opts ...grpc.CallOption) (*ActiveTasksByOriginResponse, error)
// UnpostedOutcomes lists finished chat tasks whose outcome has not yet been
// posted to their conversation; MarkOutcomePosted records that it was.
UnpostedOutcomes(ctx context.Context, in *UnpostedOutcomesRequest, opts ...grpc.CallOption) (*UnpostedOutcomesResponse, error)
MarkOutcomePosted(ctx context.Context, in *MarkOutcomePostedRequest, opts ...grpc.CallOption) (*MarkOutcomePostedResponse, error)
ClaimNext(ctx context.Context, in *ClaimNextRequest, opts ...grpc.CallOption) (*ClaimNextResponse, error)
ClaimByIssue(ctx context.Context, in *ClaimByIssueRequest, opts ...grpc.CallOption) (*ClaimByIssueResponse, error)
Transition(ctx context.Context, in *TransitionRequest, opts ...grpc.CallOption) (*TransitionResponse, error)
@@ -713,6 +719,26 @@ func (c *stateStoreServiceClient) ActiveTasksByOrigin(ctx context.Context, in *A
return out, nil
}
func (c *stateStoreServiceClient) UnpostedOutcomes(ctx context.Context, in *UnpostedOutcomesRequest, opts ...grpc.CallOption) (*UnpostedOutcomesResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(UnpostedOutcomesResponse)
err := c.cc.Invoke(ctx, StateStoreService_UnpostedOutcomes_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *stateStoreServiceClient) MarkOutcomePosted(ctx context.Context, in *MarkOutcomePostedRequest, opts ...grpc.CallOption) (*MarkOutcomePostedResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(MarkOutcomePostedResponse)
err := c.cc.Invoke(ctx, StateStoreService_MarkOutcomePosted_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *stateStoreServiceClient) ClaimNext(ctx context.Context, in *ClaimNextRequest, opts ...grpc.CallOption) (*ClaimNextResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ClaimNextResponse)
@@ -1810,6 +1836,10 @@ type StateStoreServiceServer interface {
EnqueueIssue(context.Context, *EnqueueIssueRequest) (*EnqueueIssueResponse, error)
EnqueueChatTask(context.Context, *EnqueueChatTaskRequest) (*EnqueueChatTaskResponse, error)
ActiveTasksByOrigin(context.Context, *ActiveTasksByOriginRequest) (*ActiveTasksByOriginResponse, error)
// UnpostedOutcomes lists finished chat tasks whose outcome has not yet been
// posted to their conversation; MarkOutcomePosted records that it was.
UnpostedOutcomes(context.Context, *UnpostedOutcomesRequest) (*UnpostedOutcomesResponse, error)
MarkOutcomePosted(context.Context, *MarkOutcomePostedRequest) (*MarkOutcomePostedResponse, error)
ClaimNext(context.Context, *ClaimNextRequest) (*ClaimNextResponse, error)
ClaimByIssue(context.Context, *ClaimByIssueRequest) (*ClaimByIssueResponse, error)
Transition(context.Context, *TransitionRequest) (*TransitionResponse, error)
@@ -2101,6 +2131,12 @@ func (UnimplementedStateStoreServiceServer) EnqueueChatTask(context.Context, *En
func (UnimplementedStateStoreServiceServer) ActiveTasksByOrigin(context.Context, *ActiveTasksByOriginRequest) (*ActiveTasksByOriginResponse, error) {
return nil, status.Error(codes.Unimplemented, "method ActiveTasksByOrigin not implemented")
}
func (UnimplementedStateStoreServiceServer) UnpostedOutcomes(context.Context, *UnpostedOutcomesRequest) (*UnpostedOutcomesResponse, error) {
return nil, status.Error(codes.Unimplemented, "method UnpostedOutcomes not implemented")
}
func (UnimplementedStateStoreServiceServer) MarkOutcomePosted(context.Context, *MarkOutcomePostedRequest) (*MarkOutcomePostedResponse, error) {
return nil, status.Error(codes.Unimplemented, "method MarkOutcomePosted not implemented")
}
func (UnimplementedStateStoreServiceServer) ClaimNext(context.Context, *ClaimNextRequest) (*ClaimNextResponse, error) {
return nil, status.Error(codes.Unimplemented, "method ClaimNext not implemented")
}
@@ -2965,6 +3001,42 @@ func _StateStoreService_ActiveTasksByOrigin_Handler(srv interface{}, ctx context
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_UnpostedOutcomes_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UnpostedOutcomesRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(StateStoreServiceServer).UnpostedOutcomes(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: StateStoreService_UnpostedOutcomes_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(StateStoreServiceServer).UnpostedOutcomes(ctx, req.(*UnpostedOutcomesRequest))
}
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_MarkOutcomePosted_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(MarkOutcomePostedRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(StateStoreServiceServer).MarkOutcomePosted(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: StateStoreService_MarkOutcomePosted_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(StateStoreServiceServer).MarkOutcomePosted(ctx, req.(*MarkOutcomePostedRequest))
}
return interceptor(ctx, in, info, handler)
}
func _StateStoreService_ClaimNext_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ClaimNextRequest)
if err := dec(in); err != nil {
@@ -4889,6 +4961,14 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
MethodName: "ActiveTasksByOrigin",
Handler: _StateStoreService_ActiveTasksByOrigin_Handler,
},
{
MethodName: "UnpostedOutcomes",
Handler: _StateStoreService_UnpostedOutcomes_Handler,
},
{
MethodName: "MarkOutcomePosted",
Handler: _StateStoreService_MarkOutcomePosted_Handler,
},
{
MethodName: "ClaimNext",
Handler: _StateStoreService_ClaimNext_Handler,
@@ -81,6 +81,7 @@ type HarnessSecretStore interface {
// status guard. It is separate from the already broad lifecycle contract so
// consumers that only run tasks do not acquire an operator-only capability.
type TaskArchiver interface {
OutcomePoster
ArchiveTask(ctx context.Context, taskID int64, fromStatus string, audit events.Event) (eventID int64, err error)
ClearTerminalTasks(ctx context.Context) (int64, error)
}
@@ -120,6 +121,16 @@ type RemediationStarter interface {
SetReviewCursors(ctx context.Context, taskID, reviewCursor, commentCursor int64) error
}
// OutcomePoster tracks which finished chat tasks have reported back to their
// conversation.
type OutcomePoster interface {
// UnpostedOutcomes returns finished chat tasks whose outcome has not been
// posted to the conversation that created them.
UnpostedOutcomes(ctx context.Context) ([]*task.Task, error)
// MarkOutcomePosted records that a task's outcome was posted.
MarkOutcomePosted(ctx context.Context, taskID int64) error
}
// TaskQueries groups read-only task accessors.
type TaskQueries interface {
TaskByIssue(ctx context.Context, owner, repo string, number int) (*task.Task, error)
+3
View File
@@ -88,6 +88,9 @@ type Task struct {
// deployments). Used to scope /approve and /cancel authorization so
// one identity cannot control another's chat-spawned tasks.
Identity string `json:"identity"`
// Origin names the conversation that created the task, so its outcome and
// /stop find it; empty for a task no conversation started.
Origin string `json:"origin"`
// Org is the org the task belongs to. The State Store derives it from
// the task's identity when the row is written;
// a task
@@ -0,0 +1,14 @@
-- tasks.outcome_posted marks that a chat task's terminal outcome reached the
-- conversation that created it. Tasks that finished before this column existed
-- were never promised a report.
-- +goose Up
ALTER TABLE tasks ADD COLUMN outcome_posted boolean NOT NULL DEFAULT false;
UPDATE tasks SET outcome_posted = true;
CREATE INDEX idx_tasks_outcome_unposted ON tasks (id) WHERE origin <> '' AND NOT outcome_posted;
-- +goose Down
DROP INDEX idx_tasks_outcome_unposted;
ALTER TABLE tasks DROP COLUMN outcome_posted;
@@ -24,7 +24,7 @@ func (q *Queries) CallStatusDetail(ctx context.Context, taskID int64) (string, e
const enqueueCallTask = `-- name: EnqueueCallTask :one
WITH caller AS (
SELECT t.id, t.owner, t.repo, t.issue_number, t.title, t.body, t.labels, t.status, t.workflow, t.branch, t.plan, t.notes, t.pr_number, t.tokens_used, t.iterations, t.attempt, t.park_reason, t.watch_comment_id, t.park_class, t.remediation_rounds, t.retry_count, t.source, t.identity, t.binding_id, t.binding_version, t.review_payload, t.workflow_definition_version, t.workflow_definition_digest, t.workflow_definition_yaml, t.created_at, t.updated_at, t.review_cursor, t.inputs, t.org_id, t.workspace_id, t.call_parent_task_id, t.call_depth, t.outputs, t.review_gate, t.rereview_rounds, t.retry_mode, t.resume_from, t.resume_results, t.pending_reviews, t.origin, t.call_key FROM tasks t WHERE t.id = $1 FOR UPDATE
SELECT t.id, t.owner, t.repo, t.issue_number, t.title, t.body, t.labels, t.status, t.workflow, t.branch, t.plan, t.notes, t.pr_number, t.tokens_used, t.iterations, t.attempt, t.park_reason, t.watch_comment_id, t.park_class, t.remediation_rounds, t.retry_count, t.source, t.identity, t.binding_id, t.binding_version, t.review_payload, t.workflow_definition_version, t.workflow_definition_digest, t.workflow_definition_yaml, t.created_at, t.updated_at, t.review_cursor, t.inputs, t.org_id, t.workspace_id, t.call_parent_task_id, t.call_depth, t.outputs, t.review_gate, t.rereview_rounds, t.retry_mode, t.resume_from, t.resume_results, t.pending_reviews, t.origin, t.call_key, t.outcome_posted FROM tasks t WHERE t.id = $1 FOR UPDATE
)
INSERT INTO tasks (owner, repo, issue_number, title, body, labels, workflow, source, identity, org_id, workspace_id, inputs, call_parent_task_id, call_depth, call_key)
SELECT
@@ -42,7 +42,7 @@ SELECT
FROM caller
WHERE caller.status = 'running' AND caller.call_depth + 1 <= $8::int
ON CONFLICT (call_parent_task_id, call_key) WHERE call_key <> '' AND status NOT IN ('dead', 'closed_wont_do') DO UPDATE SET call_key = EXCLUDED.call_key
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted
`
type EnqueueCallTaskParams struct {
@@ -124,6 +124,7 @@ func (q *Queries) EnqueueCallTask(ctx context.Context, arg EnqueueCallTaskParams
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
)
return i, err
}
@@ -455,6 +455,7 @@ type Task struct {
PendingReviews []byte
Origin string
CallKey string
OutcomePosted bool
}
type ToolCall struct {
@@ -13,7 +13,7 @@ import (
)
const activeTasksByOrigin = `-- name: ActiveTasksByOrigin :many
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted FROM tasks
WHERE origin = $1 AND origin <> '' AND status IN ('queued', 'running') AND ($2::text IS NULL OR org_id = $2)
ORDER BY id
`
@@ -80,6 +80,7 @@ func (q *Queries) ActiveTasksByOrigin(ctx context.Context, arg ActiveTasksByOrig
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
); err != nil {
return nil, err
}
@@ -159,7 +160,7 @@ func (q *Queries) BeginRemediationTask(ctx context.Context, arg BeginRemediation
const claimByIssue = `-- name: ClaimByIssue :one
UPDATE tasks SET status = 'running', attempt = attempt + 1, outputs = '', updated_at = now()
WHERE owner = $1 AND repo = $2 AND issue_number = $3 AND status = 'queued'
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted
`
type ClaimByIssueParams struct {
@@ -220,6 +221,7 @@ func (q *Queries) ClaimByIssue(ctx context.Context, arg ClaimByIssueParams) (Tas
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
)
return i, err
}
@@ -234,7 +236,7 @@ WHERE id = (
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted
`
// FOR UPDATE SKIP LOCKED replaces the SQLite single-writer assumption: two
@@ -293,6 +295,7 @@ func (q *Queries) ClaimNextTask(ctx context.Context) (Task, error) {
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
)
return i, err
}
@@ -406,7 +409,7 @@ FROM (SELECT COALESCE(
'org-sys'
) AS id) AS owner_org
WHERE $10::text IS NULL OR owner_org.id = $10
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key
RETURNING id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted
`
type InsertChatTaskParams struct {
@@ -492,6 +495,7 @@ func (q *Queries) InsertChatTask(ctx context.Context, arg InsertChatTaskParams)
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
)
return i, err
}
@@ -719,6 +723,15 @@ func (q *Queries) LockTaskStatus(ctx context.Context, id int64) (string, error)
return status, err
}
const markOutcomePosted = `-- name: MarkOutcomePosted :exec
UPDATE tasks SET outcome_posted = true WHERE id = $1
`
func (q *Queries) MarkOutcomePosted(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, markOutcomePosted, id)
return err
}
const parkTask = `-- name: ParkTask :execrows
UPDATE tasks
SET status = 'parked', park_reason = $2, park_class = $3, updated_at = now()
@@ -934,7 +947,7 @@ func (q *Queries) StampTaskBinding(ctx context.Context, arg StampTaskBindingPara
}
const taskByID = `-- name: TaskByID :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted FROM tasks WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
type TaskByIDParams struct {
@@ -992,12 +1005,13 @@ func (q *Queries) TaskByID(ctx context.Context, arg TaskByIDParams) (Task, error
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
)
return i, err
}
const taskByIssue = `-- name: TaskByIssue :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted FROM tasks WHERE owner = $1 AND repo = $2 AND issue_number = $3
AND ($4::text IS NULL OR org_id = $4)
`
@@ -1063,12 +1077,13 @@ func (q *Queries) TaskByIssue(ctx context.Context, arg TaskByIssueParams) (Task,
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
)
return i, err
}
const taskByPR = `-- name: TaskByPR :one
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted FROM tasks WHERE owner = $1 AND repo = $2 AND pr_number = $3 AND status = $4
AND ($5::text IS NULL OR org_id = $5)
`
@@ -1139,6 +1154,7 @@ func (q *Queries) TaskByPR(ctx context.Context, arg TaskByPRParams) (Task, error
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
)
return i, err
}
@@ -1176,6 +1192,84 @@ func (q *Queries) TransitionTask(ctx context.Context, arg TransitionTaskParams)
return result.RowsAffected(), nil
}
const unpostedOutcomes = `-- name: UnpostedOutcomes :many
SELECT id, owner, repo, issue_number, title, body, labels, status, workflow, branch, plan, notes, pr_number, tokens_used, iterations, attempt, park_reason, watch_comment_id, park_class, remediation_rounds, retry_count, source, identity, binding_id, binding_version, review_payload, workflow_definition_version, workflow_definition_digest, workflow_definition_yaml, created_at, updated_at, review_cursor, inputs, org_id, workspace_id, call_parent_task_id, call_depth, outputs, review_gate, rereview_rounds, retry_mode, resume_from, resume_results, pending_reviews, origin, call_key, outcome_posted FROM tasks
WHERE origin <> '' AND NOT outcome_posted
AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed')
AND ($1::text IS NULL OR org_id = $1)
ORDER BY id
LIMIT 50
`
// Finished chat tasks whose outcome has not reached their conversation yet.
func (q *Queries) UnpostedOutcomes(ctx context.Context, scopeOrg pgtype.Text) ([]Task, error) {
rows, err := q.db.Query(ctx, unpostedOutcomes, scopeOrg)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Task
for rows.Next() {
var i Task
if err := rows.Scan(
&i.ID,
&i.Owner,
&i.Repo,
&i.IssueNumber,
&i.Title,
&i.Body,
&i.Labels,
&i.Status,
&i.Workflow,
&i.Branch,
&i.Plan,
&i.Notes,
&i.PrNumber,
&i.TokensUsed,
&i.Iterations,
&i.Attempt,
&i.ParkReason,
&i.WatchCommentID,
&i.ParkClass,
&i.RemediationRounds,
&i.RetryCount,
&i.Source,
&i.Identity,
&i.BindingID,
&i.BindingVersion,
&i.ReviewPayload,
&i.WorkflowDefinitionVersion,
&i.WorkflowDefinitionDigest,
&i.WorkflowDefinitionYaml,
&i.CreatedAt,
&i.UpdatedAt,
&i.ReviewCursor,
&i.Inputs,
&i.OrgID,
&i.WorkspaceID,
&i.CallParentTaskID,
&i.CallDepth,
&i.Outputs,
&i.ReviewGate,
&i.RereviewRounds,
&i.RetryMode,
&i.ResumeFrom,
&i.ResumeResults,
&i.PendingReviews,
&i.Origin,
&i.CallKey,
&i.OutcomePosted,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateTask = `-- name: UpdateTask :exec
UPDATE tasks SET workflow = $1, branch = $2, plan = $3, notes = $4,
pr_number = $5, tokens_used = $6, iterations = $7, park_reason = $8,
@@ -93,6 +93,18 @@ SELECT * FROM tasks
WHERE origin = sqlc.arg(origin) AND origin <> '' AND status IN ('queued', 'running') AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
ORDER BY id;
-- name: UnpostedOutcomes :many
-- Finished chat tasks whose outcome has not reached their conversation yet.
SELECT * FROM tasks
WHERE origin <> '' AND NOT outcome_posted
AND status IN ('merged', 'rejected', 'dead', 'closed_wont_do', 'completed')
AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org))
ORDER BY id
LIMIT 50;
-- name: MarkOutcomePosted :exec
UPDATE tasks SET outcome_posted = true WHERE id = $1;
-- name: StampTaskBinding :exec
UPDATE tasks SET binding_id = $2, binding_version = $3 WHERE id = $1;
+23
View File
@@ -120,6 +120,7 @@ func taskFromRow(t postgresdb.Task) *workflow.Task {
RemediationRounds: int(t.RemediationRounds),
CallParentTaskID: t.CallParentTaskID,
CallDepth: int(t.CallDepth),
Origin: t.Origin,
CreatedAt: t.CreatedAt,
UpdatedAt: t.UpdatedAt,
}
@@ -865,6 +866,28 @@ func (s *Store) CallStatus(ctx context.Context, callerTaskID, callTaskID int64)
return callee.Status, detail, outputs, nil
}
// UnpostedOutcomes returns finished chat tasks whose outcome has not been
// posted to their conversation.
func (s *Store) UnpostedOutcomes(ctx context.Context) ([]*workflow.Task, error) {
rows, err := s.queries().UnpostedOutcomes(ctx, scopeOrg(ctx))
if err != nil {
return nil, fmt.Errorf("store: unposted outcomes: %w", err)
}
out := make([]*workflow.Task, 0, len(rows))
for _, row := range rows {
out = append(out, taskFromRow(row))
}
return out, nil
}
// MarkOutcomePosted records that a task's outcome reached its conversation.
func (s *Store) MarkOutcomePosted(ctx context.Context, taskID int64) error {
if err := s.queries().MarkOutcomePosted(ctx, taskID); err != nil {
return fmt.Errorf("store: mark outcome posted: %w", err)
}
return nil
}
// ActiveTasksByOrigin returns the queued and running tasks the conversation
// origin created.
func (s *Store) ActiveTasksByOrigin(ctx context.Context, origin string) ([]*workflow.Task, error) {
@@ -88,6 +88,19 @@ func (c *Client) ActiveTasksByOrigin(ctx context.Context, origin string) ([]*tas
return mapValues(r.Tasks, taskValue), nil
}
func (c *Client) UnpostedOutcomes(ctx context.Context) ([]*task.Task, error) {
r, err := c.client.UnpostedOutcomes(ctx, &pb.UnpostedOutcomesRequest{})
if err != nil {
return nil, unmapError(err)
}
return mapValues(r.Tasks, taskValue), nil
}
func (c *Client) MarkOutcomePosted(ctx context.Context, taskID int64) error {
_, err := c.client.MarkOutcomePosted(ctx, &pb.MarkOutcomePostedRequest{TaskId: taskID})
return unmapError(err)
}
func (c *Client) EnqueueChatTask(ctx context.Context, owner, repo, title, body, wf, identity, origin string, inputs map[string]any) (*task.Task, error) {
encoded, err := task.EncodeInputs(inputs)
if err != nil {
@@ -206,6 +206,25 @@ func (s *server) ActiveTasksByOrigin(ctx context.Context, r *pb.ActiveTasksByOri
return &pb.ActiveTasksByOriginResponse{Tasks: out}, nil
}
func (s *server) UnpostedOutcomes(ctx context.Context, _ *pb.UnpostedOutcomesRequest) (*pb.UnpostedOutcomesResponse, error) {
tasks, err := s.deps.Tasks.UnpostedOutcomes(ctx)
if err != nil {
return nil, s.logErr("UnpostedOutcomes", err)
}
out := make([]*pb.Task, 0, len(tasks))
for _, t := range tasks {
out = append(out, taskProto(t))
}
return &pb.UnpostedOutcomesResponse{Tasks: out}, nil
}
func (s *server) MarkOutcomePosted(ctx context.Context, r *pb.MarkOutcomePostedRequest) (*pb.MarkOutcomePostedResponse, error) {
if err := s.deps.Tasks.MarkOutcomePosted(ctx, r.TaskId); err != nil {
return nil, s.logErr("MarkOutcomePosted", err)
}
return &pb.MarkOutcomePostedResponse{}, nil
}
func (s *server) EnqueueChatTask(ctx context.Context, r *pb.EnqueueChatTaskRequest) (*pb.EnqueueChatTaskResponse, error) {
inputs, err := task.DecodeInputs(r.InputsJson)
if err != nil {
@@ -84,6 +84,7 @@ func taskProto(t *task.Task) *pb.Task { // mirror-image field-by-field proto<->d
Org: string(t.Org),
CallParentTaskId: t.CallParentTaskID,
CallDepth: int32(t.CallDepth),
Origin: t.Origin,
}
}
@@ -116,6 +117,7 @@ func taskValue(t *pb.Task) *task.Task { // see taskProto above
Org: org.OrgID(t.Org),
CallParentTaskID: t.CallParentTaskId,
CallDepth: int(t.CallDepth),
Origin: t.Origin,
}
}
+11
View File
@@ -62,6 +62,10 @@ service StateStoreService {
rpc EnqueueIssue(EnqueueIssueRequest) returns (EnqueueIssueResponse);
rpc EnqueueChatTask(EnqueueChatTaskRequest) returns (EnqueueChatTaskResponse);
rpc ActiveTasksByOrigin(ActiveTasksByOriginRequest) returns (ActiveTasksByOriginResponse);
// UnpostedOutcomes lists finished chat tasks whose outcome has not yet been
// posted to their conversation; MarkOutcomePosted records that it was.
rpc UnpostedOutcomes(UnpostedOutcomesRequest) returns (UnpostedOutcomesResponse);
rpc MarkOutcomePosted(MarkOutcomePostedRequest) returns (MarkOutcomePostedResponse);
rpc ClaimNext(ClaimNextRequest) returns (ClaimNextResponse);
rpc ClaimByIssue(ClaimByIssueRequest) returns (ClaimByIssueResponse);
rpc Transition(TransitionRequest) returns (TransitionResponse);
@@ -457,6 +461,8 @@ message Task {
// stage. resume_results are the step results (JSON) it starts with.
string resume_from = 42;
bytes resume_results = 43;
// origin names the conversation that created the task; empty for none.
string origin = 44;
}
// Event mirrors internal/events.Event. Data is carried as a JSON object
@@ -645,6 +651,11 @@ message EnqueueIssueResponse { bool inserted = 1; }
message ActiveTasksByOriginRequest { string origin = 1; }
message ActiveTasksByOriginResponse { repeated Task tasks = 1; }
message UnpostedOutcomesRequest {}
message UnpostedOutcomesResponse { repeated Task tasks = 1; }
message MarkOutcomePostedRequest { int64 task_id = 1; }
message MarkOutcomePostedResponse {}
message EnqueueChatTaskRequest {
string owner = 1;
string repo = 2;