Files
archie/internal/storerpc/storerpc.go
T
sam 5c0efaceaf refactor(workflow): relocate Task/Status/Source and define workflow.Store (#754)
* refactor(workflow,store): relocate Task/Status/Source and define workflow.Store

Move store.Task, store.Status*/store.Source*, and the 3-method workflow
store interface out of internal/store into internal/domain/workflow as
workflow.Task/workflow.Status/workflow.Source plus a consumer-owned
workflow.Store, superseding store.WorkflowStore. internal/domain/workflow
now imports zero internal/store symbols; internal/store persists the
domain type instead (infrastructure depends on domain), and its Store
satisfies workflow.Store.

Updates every production and test caller of the relocated symbols across
daemon, webui, gateway, agentworker, agenttransport/nats, storerpc,
taskrun, and worktreerpc, and removes TestRecordDispatchViaExplicitTx per
docs/prds/state-store-contract.md (rev. 2c) sections 4 and 12 step 2.

Two workflow-package integration tests that exercise the real *store.Store
move to an external workflow_test package (store_integration_test.go) to
avoid a workflow(test)->store->workflow(prod) import cycle now that store
depends on workflow.

* no-mistakes(document): docs(state-store): mark §4.8 relocation done in state-store-contract
2026-09-07 00:39:26 +10:00

138 lines
4.3 KiB
Go

// Package storerpc lets archie-agent (which does not hold a database
// connection) mutate task state by calling back to archied over core NATS
// request/reply, instead of the agent process needing direct SQLite access.
// archied remains the sole owner of the store.TaskStore.
package storerpc
import (
"context"
"encoding/json"
"log/slog"
"time"
"github.com/nats-io/nats.go"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/natsrpc"
"github.com/samcharles93/archie-core/internal/store"
)
// Subjects for the two store.TaskStore methods workflow stages call mid-run.
const (
SubjectUpdate = "archie.store.update"
SubjectTransition = "archie.store.transition"
SubjectInsertEvent = "archie.store.insert_event"
)
// UpdateRequest mirrors store.TaskStore.Update's argument.
type UpdateRequest struct {
Task *workflow.Task `json:"task"`
}
// TransitionRequest mirrors store.TaskStore.Transition's arguments.
type TransitionRequest struct {
TaskID int64 `json:"task_id"`
From string `json:"from"`
To string `json:"to"`
Detail string `json:"detail"`
}
type InsertEventRequest struct {
Event events.Event `json:"event"`
}
// Response carries the result of an RPC. Error is empty on success.
type Response struct {
natsrpc.Envelope
ID int64 `json:"id,omitempty"`
}
// Server handles storerpc requests against the daemon's TaskStore.
type Server struct {
Store store.TaskStore
Log *slog.Logger
}
// Register subscribes the update and transition handlers on nc. The
// returned func unsubscribes both.
func (s *Server) Register(nc *nats.Conn) (unsubscribe func(), err error) {
return natsrpc.RegisterAll(nc, []natsrpc.Registration{
{Subject: SubjectUpdate, Handler: s.handleUpdate},
{Subject: SubjectTransition, Handler: s.handleTransition},
{Subject: SubjectInsertEvent, Handler: s.handleInsertEvent},
})
}
func (s *Server) handleInsertEvent(msg *nats.Msg) {
var req InsertEventRequest
if err := json.Unmarshal(msg.Data, &req); err != nil {
s.reply(msg, err)
return
}
id, err := s.Store.InsertEvent(context.Background(), req.Event)
natsrpc.Respond(msg, s.Log, "storerpc", Response{Envelope: natsrpc.NewEnvelope(err), ID: id})
}
func (s *Server) handleUpdate(msg *nats.Msg) {
var req UpdateRequest
if err := json.Unmarshal(msg.Data, &req); err != nil {
s.reply(msg, err)
return
}
s.reply(msg, s.Store.Update(context.Background(), req.Task))
}
func (s *Server) handleTransition(msg *nats.Msg) {
var req TransitionRequest
if err := json.Unmarshal(msg.Data, &req); err != nil {
s.reply(msg, err)
return
}
s.reply(msg, s.Store.Transition(context.Background(), req.TaskID, req.From, req.To, req.Detail))
}
func (s *Server) reply(msg *nats.Msg, err error) {
natsrpc.Respond(msg, s.Log, "storerpc", Response{Envelope: natsrpc.NewEnvelope(err)})
}
// Client calls the storerpc Server from archie-agent's process.
// It implements workflow.Store (the narrow interface workflow stages
// need -- just Update + Transition), not the full store.TaskStore.
type Client struct {
Conn *nats.Conn
Timeout time.Duration // bounds each call when ctx has no deadline
}
func (c *Client) rpc() *natsrpc.Client { return &natsrpc.Client{Conn: c.Conn, Timeout: c.Timeout} }
// Update calls store.TaskStore.Update on the daemon over NATS request/reply.
func (c *Client) Update(ctx context.Context, task *workflow.Task) error {
resp, err := natsrpc.Call[Response](ctx, c.rpc(), SubjectUpdate, UpdateRequest{Task: task})
if err != nil {
return err
}
return resp.Err()
}
// Transition calls store.TaskStore.Transition on the daemon over NATS request/reply.
func (c *Client) Transition(ctx context.Context, taskID int64, from, to, detail string) error {
req := TransitionRequest{TaskID: taskID, From: from, To: to, Detail: detail}
resp, err := natsrpc.Call[Response](ctx, c.rpc(), SubjectTransition, req)
if err != nil {
return err
}
return resp.Err()
}
func (c *Client) InsertEvent(ctx context.Context, event events.Event) (int64, error) {
resp, err := natsrpc.Call[Response](ctx, c.rpc(), SubjectInsertEvent, InsertEventRequest{Event: event})
if err != nil {
return 0, err
}
return resp.ID, resp.Err()
}
// Compile-time check: Client satisfies workflow.Store.
var _ workflow.Store = (*Client)(nil)