Converge run persistence on SQLite through the Store port
Why: Q1 WP5 - platform's SQLite becomes an implementation of the squad Store port so run state and orchestration state live in one database, and platform's in-process execution goes through the Executor port instead of bypassing it (X6). Evidence: all five modules go vet clean and go test ok, including engine-on-SQLite (state in DB, no run-level JSON files, second engine recovers from the same DB), SQLiteStore round trip, and the full squad compatibility net. Impact: runState/hitlGate exported so the port is implementable; Options.Store wires into engineDeps; leader persistence and checkpoints route through the port; PrepareWorkspace no longer writes run-level JSON (st.save owns it); platform RunInProcess takes squad.Executor (callers updated); serve mounts the engine on platform's RunStore - single orchestration.db for both. Refs: Documents proposal 2026-10-07-agent-stack-n1q1i1-plans.md Q1 WP5
This commit is contained in:
@@ -4,10 +4,12 @@ go 1.27
|
||||
|
||||
require (
|
||||
agent-native v0.0.0
|
||||
agent-squad v0.0.0
|
||||
modernc.org/sqlite v1.37.0
|
||||
)
|
||||
|
||||
require (
|
||||
error-code-system v0.0.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"agent-native/runtime/llm"
|
||||
agentsquad "agent-squad/squad"
|
||||
)
|
||||
|
||||
func TestOrchestrationFlow(t *testing.T) {
|
||||
@@ -116,12 +116,10 @@ func TestOrchestrationFlow(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
text, err := s.RunInProcess(context.Background(), woke, work, func(ctx context.Context, model string, messages []llm.Message, td []llm.ToolDecl, opts llm.StreamOptions) <-chan llm.Event {
|
||||
ch := make(chan llm.Event, 1)
|
||||
ch <- llm.Event{Kind: llm.KindMessageEnd, Message: llm.Message{Role: llm.RoleAssistant, Content: "done", StopReason: llm.StopEnd}}
|
||||
close(ch)
|
||||
return ch
|
||||
})
|
||||
text, err := s.RunInProcess(context.Background(), woke, work, stubExecutor{result: &agentsquad.ExecResult{
|
||||
Success: true, Logs: "done",
|
||||
Patches: map[string][]byte{"output.md": []byte("done")},
|
||||
}})
|
||||
if err != nil || text != "done" {
|
||||
t.Fatalf("in-process %q %v", text, err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"agent-squad/squad"
|
||||
)
|
||||
|
||||
// SQLiteStore 把 squad 的 Store 端口落到本库(Q1 WP5):run/gate/checkpoint/leader
|
||||
// 与平台其余表同一个 SQLite 文件——运行状态与平台状态不再各记一份。
|
||||
// 表名带 squad_run_ 前缀,与平台既有 23 张表并存不冲突。
|
||||
type SQLiteStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
// OpenRunStore 在本 Store 的底层连接上提供 squad.Store 端口(schema 随 migrate 建)。
|
||||
func (s *Store) RunStore() *SQLiteStore {
|
||||
return &SQLiteStore{db: s.db}
|
||||
}
|
||||
|
||||
const runSchema = `
|
||||
CREATE TABLE IF NOT EXISTS squad_run_state (
|
||||
ws_dir TEXT PRIMARY KEY,
|
||||
meta TEXT NOT NULL, workflow TEXT NOT NULL, squad TEXT NOT NULL, bb TEXT NOT NULL,
|
||||
template TEXT, approved TEXT, repair_budget_remaining INTEGER NOT NULL DEFAULT 0,
|
||||
repair_budget_granted INTEGER NOT NULL DEFAULT 0,
|
||||
clarifications TEXT, steering TEXT, usage TEXT, updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS squad_run_gate (
|
||||
ws_dir TEXT PRIMARY KEY, gate TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS squad_run_checkpoint (
|
||||
ws_dir TEXT NOT NULL, superstep INTEGER NOT NULL, at TEXT NOT NULL,
|
||||
graph_hash TEXT NOT NULL, steps TEXT NOT NULL,
|
||||
PRIMARY KEY (ws_dir, superstep)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS squad_run_leader (
|
||||
ws_dir TEXT PRIMARY KEY, data TEXT NOT NULL
|
||||
);
|
||||
`
|
||||
|
||||
func (s *SQLiteStore) migrate() error {
|
||||
_, err := s.db.Exec(runSchema)
|
||||
return err
|
||||
}
|
||||
|
||||
// OpenSQLiteStore 独立打开一个 squad.Store(不经 platform.Store 的场景,如测试)。
|
||||
func OpenSQLiteStore(path string) (*SQLiteStore, error) {
|
||||
db, err := sql.Open("sqlite", path+"?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := &SQLiteStore{db: db}
|
||||
if err := s.migrate(); err != nil {
|
||||
_ = db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func j(v any) string {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) SaveRun(st *squad.RunState) error {
|
||||
if err := s.migrate(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := s.db.Exec(`INSERT INTO squad_run_state
|
||||
(ws_dir, meta, workflow, squad, bb, template, approved, repair_budget_remaining, repair_budget_granted, clarifications, steering, usage, updated_at)
|
||||
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||||
ON CONFLICT(ws_dir) DO UPDATE SET
|
||||
meta=excluded.meta, workflow=excluded.workflow, squad=excluded.squad, bb=excluded.bb,
|
||||
template=excluded.template, approved=excluded.approved,
|
||||
repair_budget_remaining=excluded.repair_budget_remaining,
|
||||
repair_budget_granted=excluded.repair_budget_granted,
|
||||
clarifications=excluded.clarifications, steering=excluded.steering,
|
||||
usage=excluded.usage, updated_at=excluded.updated_at`,
|
||||
st.Meta.WorkDir, j(st.Meta), j(st.Plan.Workflow), j(st.Plan.Squad), j(st.BB),
|
||||
st.Template, j(st.Approved), st.RepairBudgetRemaining, st.RepairBudgetGranted,
|
||||
j(st.Clarifications), j(st.Steering), j(st.Usage), now())
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) LoadRun(wsDir string) (*squad.RunState, error) {
|
||||
if err := s.migrate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
st := &squad.RunState{}
|
||||
var meta, wf, sq, bb, template, approved, clar, steering, usage string
|
||||
row := s.db.QueryRow(`SELECT meta, workflow, squad, bb, template, approved,
|
||||
repair_budget_remaining, repair_budget_granted, clarifications, steering, usage
|
||||
FROM squad_run_state WHERE ws_dir = ?`, wsDir)
|
||||
var granted int
|
||||
err := row.Scan(&meta, &wf, &sq, &bb, &template, &approved,
|
||||
&st.RepairBudgetRemaining, &granted, &clar, &steering, &usage)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("load run %s: %w", wsDir, os.ErrNotExist)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
st.RepairBudgetGranted = granted
|
||||
for pair, dst := range map[string]any{
|
||||
meta: &st.Meta, wf: &st.Plan.Workflow, sq: &st.Plan.Squad, bb: &st.BB,
|
||||
approved: &st.Approved, clar: &st.Clarifications, steering: &st.Steering, usage: &st.Usage,
|
||||
} {
|
||||
if err := json.Unmarshal([]byte(pair), dst); err != nil {
|
||||
return nil, fmt.Errorf("load run %s: parse: %w", wsDir, err)
|
||||
}
|
||||
}
|
||||
st.Template = template
|
||||
if st.Approved == nil {
|
||||
st.Approved = map[string]bool{}
|
||||
}
|
||||
return st, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) SaveGate(wsDir string, gate *squad.HitlGate) error {
|
||||
if err := s.migrate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if gate == nil {
|
||||
_, err := s.db.Exec(`DELETE FROM squad_run_gate WHERE ws_dir = ?`, wsDir)
|
||||
return err
|
||||
}
|
||||
_, err := s.db.Exec(`INSERT INTO squad_run_gate (ws_dir, gate) VALUES (?,?)
|
||||
ON CONFLICT(ws_dir) DO UPDATE SET gate=excluded.gate`, wsDir, j(gate))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) LoadGate(wsDir string) (*squad.HitlGate, error) {
|
||||
if err := s.migrate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var raw string
|
||||
err := s.db.QueryRow(`SELECT gate FROM squad_run_gate WHERE ws_dir = ?`, wsDir).Scan(&raw)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
gate := &squad.HitlGate{}
|
||||
if err := json.Unmarshal([]byte(raw), gate); err != nil {
|
||||
return nil, fmt.Errorf("parse gate: %w", err)
|
||||
}
|
||||
return gate, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) SaveCheckpoint(wsDir string, cp squad.Checkpoint) error {
|
||||
if err := s.migrate(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := s.db.Exec(`INSERT INTO squad_run_checkpoint (ws_dir, superstep, at, graph_hash, steps)
|
||||
VALUES (?,?,?,?,?)
|
||||
ON CONFLICT(ws_dir, superstep) DO UPDATE SET at=excluded.at, graph_hash=excluded.graph_hash, steps=excluded.steps`,
|
||||
wsDir, cp.Superstep, cp.At.UTC().Format("2006-01-02T15:04:05Z07:00"), cp.GraphHash, j(cp.StepStates))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) LoadLatestCheckpoint(wsDir string) (*squad.Checkpoint, error) {
|
||||
if err := s.migrate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cp squad.Checkpoint
|
||||
var at, steps string
|
||||
err := s.db.QueryRow(`SELECT superstep, at, graph_hash, steps FROM squad_run_checkpoint
|
||||
WHERE ws_dir = ? ORDER BY superstep DESC LIMIT 1`, wsDir).
|
||||
Scan(&cp.Superstep, &at, &cp.GraphHash, &steps)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
t, _ := parseTime(at)
|
||||
cp.At = t
|
||||
if err := json.Unmarshal([]byte(steps), &cp.StepStates); err != nil {
|
||||
return nil, fmt.Errorf("parse checkpoint steps: %w", err)
|
||||
}
|
||||
return &cp, nil
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) SaveLeader(wsDir string, data []byte) error {
|
||||
if err := s.migrate(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := s.db.Exec(`INSERT INTO squad_run_leader (ws_dir, data) VALUES (?,?)
|
||||
ON CONFLICT(ws_dir) DO UPDATE SET data=excluded.data`, wsDir, string(data))
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLiteStore) LoadLeader(wsDir string) ([]byte, error) {
|
||||
if err := s.migrate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var raw string
|
||||
err := s.db.QueryRow(`SELECT data FROM squad_run_leader WHERE ws_dir = ?`, wsDir).Scan(&raw)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return []byte(raw), nil
|
||||
}
|
||||
|
||||
// Close 只在独立打开(OpenSQLiteStore)时调用;经 platform.Store 打开时由平台统一关闭。
|
||||
func (s *SQLiteStore) Close() error { return s.db.Close() }
|
||||
|
||||
var _ squad.Store = (*SQLiteStore)(nil)
|
||||
|
||||
// parseTime 解析检查点时间(RFC3339Nano 与 RFC3339 两态)。
|
||||
func parseTime(s string) (time.Time, error) {
|
||||
for _, layout := range []string{time.RFC3339Nano, time.RFC3339} {
|
||||
if t, err := time.Parse(layout, s); err == nil {
|
||||
return t, nil
|
||||
}
|
||||
}
|
||||
return time.Time{}, fmt.Errorf("bad time %q", s)
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"agent-squad/squad"
|
||||
)
|
||||
|
||||
func newTestRunState(wsDir string) *squad.RunState {
|
||||
return &squad.RunState{
|
||||
Meta: squad.RunMetadata{
|
||||
ID: "run_test", Status: "executing", WorkDir: wsDir,
|
||||
CreatedAt: time.Now(), UpdatedAt: time.Now(),
|
||||
Metadata: map[string]string{"mode": ""},
|
||||
},
|
||||
Plan: squad.SquadPlan{
|
||||
Workflow: squad.WorkflowPlan{
|
||||
WorkflowID: "wf1", Status: "in_progress",
|
||||
Steps: []squad.StepSpec{
|
||||
{ID: "s1", Role: "researcher", Status: "completed", Attempts: 1, AttemptID: "run_test/s1/1"},
|
||||
{ID: "s2", Role: "writer", Status: "pending"},
|
||||
},
|
||||
},
|
||||
Squad: squad.SquadConfig{SquadName: "demo"},
|
||||
},
|
||||
BB: squad.BlackboardState{Facts: []squad.FactItem{{ID: "f1", Claim: "x"}}},
|
||||
Template: "tpl",
|
||||
Approved: map[string]bool{"step:s1": true},
|
||||
RepairBudgetRemaining: 1,
|
||||
RepairBudgetGranted: 0,
|
||||
Clarifications: []string{"c1"},
|
||||
Steering: []string{"st1"},
|
||||
Usage: map[string]*squad.Usage{"writer": {PromptTokens: 10, CompletionTokens: 5, Steps: 1}},
|
||||
}
|
||||
}
|
||||
|
||||
// TestSQLiteStoreRoundTrip:run/gate/checkpoint/leader 全链路(Q1 WP5 端口实现)。
|
||||
func TestSQLiteStoreRoundTrip(t *testing.T) {
|
||||
st, err := OpenSQLiteStore(filepath.Join(t.TempDir(), "runs.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer st.Close()
|
||||
ws := filepath.Join(t.TempDir(), "runs", "run_test")
|
||||
orig := newTestRunState(ws)
|
||||
if err := st.SaveRun(orig); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := st.LoadRun(ws)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Meta.ID != "run_test" || got.Plan.Workflow.WorkflowID != "wf1" || got.Template != "tpl" {
|
||||
t.Fatalf("loaded = %+v", got.Meta)
|
||||
}
|
||||
if !got.Approved["step:s1"] || got.RepairBudgetRemaining != 1 || len(got.Clarifications) != 1 || len(got.Steering) != 1 {
|
||||
t.Fatalf("scalars = %+v", got)
|
||||
}
|
||||
if got.Usage["writer"].PromptTokens != 10 {
|
||||
t.Fatalf("usage = %+v", got.Usage)
|
||||
}
|
||||
if _, err := st.LoadRun(filepath.Join(t.TempDir(), "nope")); err == nil {
|
||||
t.Fatal("missing run should error")
|
||||
}
|
||||
|
||||
// gate:写→读→删。
|
||||
gate := &squad.HitlGate{PrevStatus: squad.RunExecuting}
|
||||
if err := st.SaveGate(ws, gate); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g2, err := st.LoadGate(ws); err != nil || g2 == nil {
|
||||
t.Fatalf("load gate: %v %+v", err, g2)
|
||||
}
|
||||
if err := st.SaveGate(ws, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g2, err := st.LoadGate(ws); err != nil || g2 != nil {
|
||||
t.Fatalf("gate after delete: %v %+v", err, g2)
|
||||
}
|
||||
|
||||
// checkpoint:两层,latest 取最大 superstep。
|
||||
if err := st.SaveCheckpoint(ws, squad.Checkpoint{Superstep: 1, At: time.Now(), GraphHash: "h1", StepStates: []squad.StepSnapshot{{ID: "s1", Status: "completed", AttemptID: "run_test/s1/1"}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := st.SaveCheckpoint(ws, squad.Checkpoint{Superstep: 2, At: time.Now(), GraphHash: "h2"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
latest, err := st.LoadLatestCheckpoint(ws)
|
||||
if err != nil || latest == nil {
|
||||
t.Fatalf("latest: %v", err)
|
||||
}
|
||||
if latest.Superstep != 2 || latest.GraphHash != "h2" {
|
||||
t.Fatalf("latest = %+v", latest)
|
||||
}
|
||||
if l2, err := st.LoadLatestCheckpoint(filepath.Join(t.TempDir(), "nope")); err != nil || l2 != nil {
|
||||
t.Fatalf("latest for unknown: %v %+v", err, l2)
|
||||
}
|
||||
|
||||
// leader bytes。
|
||||
if err := st.SaveLeader(ws, []byte(`{"turns":2}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := st.LoadLeader(ws)
|
||||
if err != nil || string(data) != `{"turns":2}` {
|
||||
t.Fatalf("leader = %q %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEngineRunsOnSQLiteStore:引擎注入 SQLiteStore 后运行状态进 SQLite、
|
||||
// 不再写 run 级 JSON 文件;第二引擎可从中恢复(验收第三条的实证形态)。
|
||||
func TestEngineRunsOnSQLiteStore(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "runs.db")
|
||||
runStore, err := OpenSQLiteStore(dbPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer runStore.Close()
|
||||
dir := t.TempDir()
|
||||
reg := squad.NewRoleRegistry()
|
||||
for _, spec := range []squad.RoleSpec{{Role: "researcher", Backend: "script"}, {Role: "writer", Backend: "script"}, {Role: "reviewer", Backend: "script"}} {
|
||||
if err := reg.Register(spec); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
backends := squad.NewBackendResolver()
|
||||
backends.RegisterInstance("script", stubExecutor{result: &squad.ExecResult{Success: false, Logs: "ok", Patches: map[string][]byte{"output.md": []byte(goodReportLite), "notes.md": []byte(goodReportLite), "fields.json": []byte("{}")}}})
|
||||
blueprints := squad.NewBlueprintRegistry()
|
||||
if err := blueprints.Register(squad.Blueprint{
|
||||
Key: "demo", Title: "入库演示", Match: []string{"入库演示"},
|
||||
Steps: []squad.StepSpec{
|
||||
{ID: "s1", Role: "researcher", Title: "调研", Targets: []string{"notes.md", "fields.json"}},
|
||||
{ID: "s2", Role: "writer", Title: "成文", Targets: []string{"output.md"}},
|
||||
{ID: "s3", Role: "reviewer", Title: "终审", Targets: []string{"output.md"}},
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
eng, err := squad.NewEngine(squad.Options{
|
||||
DataDir: dir, Registry: reg, Blueprints: blueprints,
|
||||
Planner: squad.NewRulePlanner(),
|
||||
Backends: backends, Sink: squad.NopSink{},
|
||||
Store: runStore,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res, err := eng.Run(context.Background(), squad.Goal{Text: "做一个三步入库演示", HitlMode: squad.HitlAuto}, squad.RunOptions{})
|
||||
if err != nil || res.Status != squad.RunCompleted {
|
||||
t.Fatalf("run: %v %+v", err, res)
|
||||
}
|
||||
// run 级 JSON 不在盘上(状态全进了 SQLite)。
|
||||
for _, f := range []string{"workspace.json", "workflow.json", "squad.json", "blackboard.json", "runstate.json"} {
|
||||
if _, err := os.Stat(filepath.Join(res.WorkDir, f)); !os.IsNotExist(err) {
|
||||
t.Fatalf("%s should not be on disk with SQLite store (err=%v)", f, err)
|
||||
}
|
||||
}
|
||||
// 状态可经端口读回。
|
||||
st, err := runStore.LoadRun(res.WorkDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if st.Plan.Workflow.Status != "completed" {
|
||||
t.Fatalf("workflow status = %s", st.Plan.Workflow.Status)
|
||||
}
|
||||
latest, err := runStore.LoadLatestCheckpoint(res.WorkDir)
|
||||
if err != nil || latest == nil {
|
||||
t.Fatalf("latest checkpoint: %v", err)
|
||||
}
|
||||
// 第二引擎(同库)恢复可用。
|
||||
eng2, err := squad.NewEngine(squad.Options{
|
||||
DataDir: dir, Registry: reg, Blueprints: blueprints,
|
||||
Planner: squad.NewRulePlanner(),
|
||||
Backends: backends, Sink: squad.NopSink{},
|
||||
Store: runStore,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := eng2.Inspect(res.RunID); err != nil {
|
||||
t.Fatalf("inspect from second engine: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
const goodReportLite = "# 测试交付物\n\n## 概述\n\n这是由脚本后端生成的合格交付物,长度超过五十个字符以满足最小质检要求。\n"
|
||||
+35
-18
@@ -9,8 +9,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"agent-native/runtime"
|
||||
"agent-native/runtime/llm"
|
||||
"agent-squad/squad"
|
||||
)
|
||||
|
||||
// CLICommands are the external tools a daemon probes, in order.
|
||||
@@ -196,36 +195,54 @@ func (s *Store) RunCommand(ctx context.Context, task Task, command, workdir stri
|
||||
return s.FinishTask(task.ID, "completed", "")
|
||||
}
|
||||
|
||||
// RunInProcess executes the task with AgentSession. stream is required; the
|
||||
// product supplies a provider, tests supply a script.
|
||||
func (s *Store) RunInProcess(ctx context.Context, task Task, workdir string, stream llm.StreamFunc) (string, error) {
|
||||
// RunInProcess executes the task through the squad.Executor port(Q1 WP5 修 X6:
|
||||
// 不再绕过执行端口直驱 agent-native runtime)。调用方经 adapters/native 等
|
||||
// 适配器构造 Executor;nil 即 fail-closed。
|
||||
func (s *Store) RunInProcess(ctx context.Context, task Task, workdir string, exec squad.Executor) (string, error) {
|
||||
if exec == nil {
|
||||
return "", fmt.Errorf("RunInProcess requires an executor (no bypass)")
|
||||
}
|
||||
if err := s.MarkRunning(task.ID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
issue, _ := s.GetIssue(task.IssueID)
|
||||
agent := runtime.NewAgentSession(runtime.Config{
|
||||
Model: "agent-native",
|
||||
Stream: stream,
|
||||
MaxTurns: 4,
|
||||
OnEvent: func(ev runtime.Event) {
|
||||
res, err := exec.Execute(ctx, squad.ExecRequest{
|
||||
WorkDir: workdir,
|
||||
Prompt: issue.Title + "\n" + issue.Body,
|
||||
Targets: []string{"output.md"},
|
||||
OnEvent: func(ev squad.ProcessEvent) {
|
||||
_ = s.AddTaskMessage(task.ID, ev.Type, ev.Text)
|
||||
},
|
||||
})
|
||||
res, err := agent.Prompt(ctx, llm.Message{Role: llm.RoleUser, Content: issue.Title + "\n" + issue.Body})
|
||||
if err != nil {
|
||||
_ = s.FinishTask(task.ID, "failed", "config")
|
||||
return "", err
|
||||
}
|
||||
if res.Err != nil {
|
||||
_ = s.FinishTask(task.ID, "failed", "model")
|
||||
return res.Text, res.Err
|
||||
if res == nil || !res.Success {
|
||||
reason := "model"
|
||||
if res != nil && res.ErrorReason != "" {
|
||||
reason = res.ErrorReason
|
||||
}
|
||||
_ = s.FinishTask(task.ID, "failed", reason)
|
||||
if res != nil {
|
||||
return res.Logs, fmt.Errorf("executor failed: %s", res.ErrorReason)
|
||||
}
|
||||
return "", fmt.Errorf("executor failed")
|
||||
}
|
||||
_ = s.AddUsage(task.ID, "agent-native", res.Usage.PromptTokens, res.Usage.CompletionTokens, res.Usage.CacheReadTokens, res.Usage.CacheWriteTokens)
|
||||
if len(res.Patches) > 0 {
|
||||
_, _ = squad.MaterializeTargets(workdir, []string{"output.md"}, res.Patches)
|
||||
}
|
||||
_ = s.AddUsage(task.ID, exec.Name(), res.PromptTokens, res.CompletionTokens, 0, 0)
|
||||
if err := s.FinishTask(task.ID, "completed", ""); err != nil {
|
||||
return res.Text, err
|
||||
return res.Logs, err
|
||||
}
|
||||
_ = os.WriteFile(filepath.Join(workdir, "output.md"), []byte(res.Text), 0o644)
|
||||
return res.Text, nil
|
||||
if data, rerr := os.ReadFile(filepath.Join(workdir, "output.md")); rerr == nil {
|
||||
return string(data), nil
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(workdir, "output.md"), []byte(res.Logs), 0o644); err != nil {
|
||||
return res.Logs, err
|
||||
}
|
||||
return res.Logs, nil
|
||||
}
|
||||
|
||||
// AgentCLI applies a task-token command: comment, status, sub-issue, or activity.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"agent-squad/squad"
|
||||
)
|
||||
|
||||
// stubExecutor 是测试用的 squad.Executor(X6 后调用方经端口注入)。
|
||||
type stubExecutor struct {
|
||||
result *squad.ExecResult
|
||||
err error
|
||||
}
|
||||
|
||||
func (s stubExecutor) Name() string { return "stub" }
|
||||
|
||||
func (s stubExecutor) Execute(ctx context.Context, req squad.ExecRequest) (*squad.ExecResult, error) {
|
||||
if s.result == nil {
|
||||
return nil, s.err
|
||||
}
|
||||
// 每次调用返回拷贝:调用方会改写 Success 字段,共享指针会跨步骤串味。
|
||||
cp := *s.result
|
||||
if s.result.Patches != nil {
|
||||
cp.Patches = map[string][]byte{}
|
||||
for k, v := range s.result.Patches {
|
||||
cp.Patches[k] = v
|
||||
}
|
||||
}
|
||||
return &cp, s.err
|
||||
}
|
||||
Reference in New Issue
Block a user