fix: harden task and chat execution

Task runs now preserve real model context limits, provider usage, baseline-repair spend, commit identity, durable agent-finish telemetry, and useful timeline details without imposing a token cap. Dashboard forge links are canonical per owning GitHub or Gitea identity. Chat turn claims acquire SQLite writer intent before reading, eliminating the concurrent SQLITE_BUSY snapshot race.
This commit is contained in:
sam committed 2026-08-25 05:48:17 +10:00
1 parent 9e6f507c06
commit a825db283c
36 files changed
+874 -286

No files matched your search

+32 -14
View File
@@ -103,6 +103,12 @@ func (r *LoopRunner) Run(ctx context.Context, workspace string, req Request, rep
MaxSteps: req.Budget.MaxSteps,
WallClock: req.Budget.WallClock,
},
Compact: agentloop.CompactionConfig{
Enabled: req.ContextWindow > 0,
ContextWindow: req.ContextWindow,
ThresholdRatio: 0.5,
TargetRatio: 0.15,
},
Completion: agentloop.CompletionForceFinish,
ReadOnly: req.ReadOnly,
ProtectPaths: protectionMatcher(req.Protection, req.ReadOnly),
@@ -114,19 +120,34 @@ func (r *LoopRunner) Run(ctx context.Context, workspace string, req Request, rep
),
Logger: r.logger(req),
})
result := resultFromRun(req, res, notes.appended, captures)
if cause := context.Cause(ctx); cause != nil {
return result, cause
}
return result, err
}
func resultFromRun(req Request, res agentloop.Result, appended []string, captures map[string][]json.RawMessage) Result {
result := Result{
Version: ProtocolVersion,
TaskID: req.TaskID,
Attempt: req.Attempt,
Stage: req.Stage,
Status: string(res.Status),
StopReason: res.StopReason,
Changes: res.Changes,
Iterations: res.Iterations,
TokensUsed: res.TokensUsed,
Version: ProtocolVersion,
TaskID: req.TaskID,
Attempt: req.Attempt,
Stage: req.Stage,
Status: string(res.Status),
StopReason: res.StopReason,
Changes: res.Changes,
Iterations: res.Iterations,
TokensUsed: res.TokensUsed,
Usage: Usage{
PromptTokens: res.Usage.PromptTokens,
CompletionTokens: res.Usage.CompletionTokens,
TotalTokens: res.Usage.TotalTokens,
CachedTokens: res.Usage.CachedTokens,
CacheCreationTokens: res.Usage.CacheCreationTokens,
},
Summary: res.Summary,
Detail: res.Detail,
AppendedNotes: notes.appended,
AppendedNotes: appended,
Captures: captures,
}
// agentloop may return a zero-valued status on early exit (model
@@ -136,10 +157,7 @@ func (r *LoopRunner) Run(ctx context.Context, workspace string, req Request, rep
result.Status = "blocked"
result.Detail = "agent returned no status (possible model connectivity issue)"
}
if cause := context.Cause(ctx); cause != nil {
return result, cause
}
return result, err
return result
}
func projectScopedRules(workspace, extra string) string {
+12 -2
View File
@@ -11,6 +11,7 @@ import (
"time"
"github.com/samcharles93/ai-sdk/agentloop"
"github.com/samcharles93/ai-sdk/chat"
"github.com/samcharles93/ai-sdk/runtime"
"github.com/samcharles93/archie-core/internal/tools"
@@ -70,6 +71,9 @@ func TestLoopRunnerMapsRequestAndCapturesOutput(t *testing.T) {
if cfg.Completion != agentloop.CompletionForceFinish {
t.Fatalf("Completion = %q, want forced finish recovery", cfg.Completion)
}
if !cfg.Compact.Enabled || cfg.Compact.ContextWindow <= 0 {
t.Fatalf("Compact = %#v, want adaptive non-blocking history compaction", cfg.Compact)
}
if !strings.Contains(cfg.ExtraRules, "Confine discovery to /workspace") ||
!strings.Contains(cfg.ExtraRules, "unless the mission explicitly requests") {
t.Fatalf("project discovery rule missing from ExtraRules: %q", cfg.ExtraRules)
@@ -80,11 +84,14 @@ func TestLoopRunnerMapsRequestAndCapturesOutput(t *testing.T) {
if _, err := cfg.Extra["decide"].Execute(ctx, `{"fit":true}`); err != nil {
t.Fatal(err)
}
return agentloop.Result{Status: agentloop.StatusPassed, Summary: "done", TokensUsed: 10}, nil
return agentloop.Result{
Status: agentloop.StatusPassed, Summary: "done", TokensUsed: 10,
Usage: chat.Usage{PromptTokens: 8, CompletionTokens: 2, TotalTokens: 10, CachedTokens: 6},
}, nil
},
}
req := Request{
Version: ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "assess", Model: "provider/model", Mission: "mission",
Version: ProtocolVersion, TaskID: 1, Attempt: 1, Stage: "assess", Model: "provider/model", ContextWindow: 120_000, Mission: "mission",
Protection: Protection{Suffixes: []string{"_templ.go"}, Globs: []string{"*_test.go"}},
CaptureTools: []CaptureTool{{Name: "decide", Parameters: json.RawMessage(`{"type":"object"}`), MaxCalls: 1}},
}
@@ -95,6 +102,9 @@ func TestLoopRunnerMapsRequestAndCapturesOutput(t *testing.T) {
if got.Status != StatusPassed || got.Summary != "done" || got.TokensUsed != 10 {
t.Fatalf("unexpected result: %#v", got)
}
if got.Usage.PromptTokens != 8 || got.Usage.CompletionTokens != 2 || got.Usage.CachedTokens != 6 {
t.Fatalf("usage = %#v, want provider breakdown", got.Usage)
}
if len(got.AppendedNotes) != 1 || got.AppendedNotes[0] != "verified note" {
t.Fatalf("appended notes = %v", got.AppendedNotes)
}
+27 -15
View File
@@ -60,21 +60,22 @@ type CaptureTool struct {
// map a host path to a fixed path without exposing host layout in the wire
// protocol.
type Request struct {
Version int `json:"version"`
TaskID int64 `json:"task_id"`
Attempt int `json:"attempt"`
Stage string `json:"stage"`
Workflow string `json:"workflow,omitempty"`
Model string `json:"model"`
Mission string `json:"mission"`
ExtraRules string `json:"extra_rules,omitempty"`
ReadOnly bool `json:"read_only,omitempty"`
Budget Budget `json:"budget"`
Gate Gate `json:"gate"`
Preflight []Command `json:"preflight,omitempty"`
Protection Protection `json:"protection"`
Notes string `json:"notes,omitempty"`
CaptureTools []CaptureTool `json:"capture_tools,omitempty"`
Version int `json:"version"`
TaskID int64 `json:"task_id"`
Attempt int `json:"attempt"`
Stage string `json:"stage"`
Workflow string `json:"workflow,omitempty"`
Model string `json:"model"`
ContextWindow int `json:"context_window,omitempty"`
Mission string `json:"mission"`
ExtraRules string `json:"extra_rules,omitempty"`
ReadOnly bool `json:"read_only,omitempty"`
Budget Budget `json:"budget"`
Gate Gate `json:"gate"`
Preflight []Command `json:"preflight,omitempty"`
Protection Protection `json:"protection"`
Notes string `json:"notes,omitempty"`
CaptureTools []CaptureTool `json:"capture_tools,omitempty"`
// Plugins are bundled Yaegi plugins from the skill's plugins/
// directory. Each entry carries the name and source so the agent
// can register them as tools. PRD section 5 Layer 1.
@@ -118,12 +119,23 @@ type Result struct {
Changes []string `json:"changes,omitempty"`
Iterations int `json:"iterations,omitempty"`
TokensUsed int `json:"tokens_used,omitempty"`
Usage Usage `json:"usage"`
Summary string `json:"summary,omitempty"`
Detail string `json:"detail,omitempty"`
AppendedNotes []string `json:"appended_notes,omitempty"`
Captures map[string][]json.RawMessage `json:"captures,omitempty"`
}
// Usage preserves provider-reported token economics for evaluation. Total is
// retained separately as TokensUsed for compatibility with stored task rows.
type Usage struct {
PromptTokens int `json:"prompt_tokens,omitempty"`
CompletionTokens int `json:"completion_tokens,omitempty"`
TotalTokens int `json:"total_tokens,omitempty"`
CachedTokens int `json:"cached_tokens,omitempty"`
CacheCreationTokens int `json:"cache_creation_tokens,omitempty"`
}
// Provider describes how the worker resolves a model provider. API keys are
// referenced by environment variable name and never serialized.
type Provider struct {