Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions agent/budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ func NewBudgetLedger(task, intent Budget) *BudgetLedger {
return &BudgetLedger{task: task, intent: intent}
}

// RestoreSpend adds usage that was already recorded before this process started.
// It is applied once, to a ledger that has not recorded anything yet. A nil
// ledger (no budget configured) is a no-op.
func (l *BudgetLedger) RestoreSpend(spent Spend) {
if l == nil || (spent.Tokens == 0 && spent.Turns == 0) {
return
}
l.mu.Lock()
l.spent.Tokens += spent.Tokens
l.spent.Turns += spent.Turns
l.mu.Unlock()
}

// Record meters one task-level call (the planner's own rounds).
func (l *BudgetLedger) Record(u llm.Usage) {
l.mu.Lock()
Expand Down
11 changes: 11 additions & 0 deletions agent/budget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,14 @@ func TestBudgetCountsMatchUsageFixture(t *testing.T) {
t.Fatalf("spent %+v remaining %+v estimate %+v, want {%d 5}, remaining, {10400 3}", st.Spent, st.Remaining, st.Estimate, want)
}
}

func TestRestoreSpendCountsTowardCap(t *testing.T) {
l := NewBudgetLedger(NewBudget(100, 10), NewBudget(0, 0))
l.RestoreSpend(Spend{Tokens: 40, Turns: 4})
st := l.State()
if st.Spent != (Spend{40, 4}) || st.Remaining != (Budget{60, 6}) {
t.Fatalf("spent=%+v remaining=%+v", st.Spent, st.Remaining)
}
var none *BudgetLedger
none.RestoreSpend(Spend{Tokens: 1, Turns: 1})
}
22 changes: 22 additions & 0 deletions agent/coverage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package agent

import (
"log"

"github.com/Autumn-27/artex/db"
)

// coverageOn reports whether asset-coverage tools stay available for this task.
// A nil store is the historical default (enabled). A database error is logged and
// treated as enabled so a transient read failure does not drop the tools mid-run.
func coverageOn(as *db.AssetStore, taskID int64) bool {
if as == nil {
return true
}
enabled, err := as.CoverageEnabled(taskID)
if err != nil {
log.Printf("[coverage] task %d: reading the coverage flag failed; leaving coverage on: %v", taskID, err)
return true
}
return enabled
}
2 changes: 1 addition & 1 deletion agent/mainagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, mainSeg int, as *db.
tsx.SetAssetStore(as, as.Companies())
}
tsx.SetTaskID(taskID)
tsx.SetCoverageEnabled(as == nil || as.CoverageEnabled(taskID))
tsx.SetCoverageEnabled(coverageOn(as, taskID))
tsx.SetNotify(notify) // generic wake-up (write ops without a dedicated callback go through it, debounced)
tsx.SetResumeTask(resume) // set_goals adds a goal → pull a completed/paused task back to running
tsx.SetNotifyGoal(notifyGoal) // set_goals adds a goal → record a "human added a goal: …" trigger for the planner
Expand Down
2 changes: 1 addition & 1 deletion agent/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
tsx.SetAssetStore(as, as.Companies())
}
tsx.SetTaskID(taskID)
tsx.SetCoverageEnabled(as == nil || as.CoverageEnabled(taskID))
tsx.SetCoverageEnabled(coverageOn(as, taskID))
tsx.killWork = p.killWork // enable kill_work tool (nil = unavailable)
tsx.steerWork = p.steerWork // enable steer_work tool (nil = unavailable)
if origin, _ := ts.OriginFactID(); origin > 0 {
Expand Down
2 changes: 1 addition & 1 deletion agent/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (w *Worker) execute(ctx context.Context, name string, taskID int64, as *db.
tsx := NewToolSet(ts, name)
tsx.SetFindingRecorder(w.findingRecorder)
tsx.SetTaskID(taskID)
coverageEnabled := as == nil || as.CoverageEnabled(taskID)
coverageEnabled := coverageOn(as, taskID)
tsx.SetCoverageEnabled(coverageEnabled)
if as != nil {
tsx.SetAssetStore(as, as.Companies())
Expand Down
21 changes: 19 additions & 2 deletions cmd/artex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ func run() int {
log.Printf("[config] trusted proxies: %v", trusted)
}
srv := server.New(ctx, mgr, skillDir, *dataDir, keyDir, server.WithSecretScrub(), server.WithTrustedProxies(trusted))
// ReadTimeout bounds how long the request headers and body may take to arrive.
// It does not limit how long a handler runs, so an event stream stays open.
// WriteTimeout is left unset: a write deadline would cut that stream.
httpSrv := &http.Server{
Addr: *addr,
Handler: srv.Handler(),
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Minute,
IdleTimeout: 2 * time.Minute,
}

go func() {
Expand All @@ -127,9 +132,21 @@ func run() int {
<-ctx.Done()

log.Println("shutting down...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
drainCtx, drainCancel := context.WithTimeout(context.Background(), 15*time.Second)
drainErr := srv.Wait(drainCtx)
drainCancel()
if drainErr != nil {
log.Printf("task drain: %v", drainErr)
}
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = httpSrv.Shutdown(shutdownCtx)
if err := httpSrv.Shutdown(shutdownCtx); err != nil {
log.Printf("http shutdown: %v", err)
return 1
}
if drainErr != nil {
return 1
}
return 0
}

Expand Down
90 changes: 90 additions & 0 deletions db/activity_outbox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package db

import (
"encoding/json"
"fmt"
)

// activityOutboxPayload is Activity plus Detail. Activity.Detail is omitted
// from ordinary JSON (the list payload stays small); the outbox has to keep it
// so a replayed row is the same step that failed to store.
type activityOutboxPayload struct {
Activity
Detail string `json:"detail,omitempty"`
}

func marshalActivity(a Activity) ([]byte, error) {
return json.Marshal(activityOutboxPayload{Activity: a, Detail: a.Detail})
}

func unmarshalActivity(raw []byte) (Activity, error) {
var payload activityOutboxPayload
if err := json.Unmarshal(raw, &payload); err != nil {
return Activity{}, err
}
a := payload.Activity
a.Detail = payload.Detail
return a, nil
}

// EnqueueActivity keeps one activity that AppendActivity could not store.
func (s *ExplorationStore) EnqueueActivity(a Activity, cause string) error {
if s == nil || s.db == nil {
return fmt.Errorf("activity outbox: no store")
}
raw, err := marshalActivity(a)
if err != nil {
return err
}
_, err = s.db.Exec(`INSERT INTO activity_outbox (exploration_id, payload, last_error) VALUES ($1, $2::jsonb, $3)`, s.expID, string(raw), cause)
return err
}

// FlushActivityOutbox inserts queued activities in order and deletes each row
// that lands. A row that still cannot be inserted stays, with last_error
// updated, and the flush stops so later rows keep their order. A payload that
// cannot be decoded is discarded.
func (s *ExplorationStore) FlushActivityOutbox() (int, error) {
if s == nil || s.db == nil {
return 0, fmt.Errorf("activity outbox: no store")
}
rows, err := s.db.Query(`SELECT id, payload FROM activity_outbox WHERE exploration_id=$1 ORDER BY id`, s.expID)
if err != nil {
return 0, err
}
defer rows.Close()
type item struct {
id int64
payload []byte
}
var items []item
for rows.Next() {
var it item
if err := rows.Scan(&it.id, &it.payload); err != nil {
return 0, err
}
items = append(items, it)
}
if err := rows.Err(); err != nil {
return 0, err
}
stored := 0
for _, it := range items {
a, err := unmarshalActivity(it.payload)
if err != nil {
if _, delErr := s.db.Exec(`DELETE FROM activity_outbox WHERE id=$1`, it.id); delErr != nil {
return stored, delErr
}
continue
}
if _, err := s.AppendActivity(a); err != nil {
_, _ = s.db.Exec(`UPDATE activity_outbox SET last_error=$2 WHERE id=$1`, it.id, err.Error())
return stored, err
}
if _, err := s.db.Exec(`DELETE FROM activity_outbox WHERE id=$1`, it.id); err != nil {
return stored, err
}
stored++
}
return stored, nil
}
25 changes: 25 additions & 0 deletions db/activity_outbox_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package db

import "testing"

func TestActivityOutboxKeepsDetail(t *testing.T) {
in := Activity{Worker: "worker", Kind: "tool_result", Summary: "done", Detail: "full body", IsError: true}
raw, err := marshalActivity(in)
if err != nil {
t.Fatal(err)
}
out, err := unmarshalActivity(raw)
if err != nil {
t.Fatal(err)
}
if out.Detail != in.Detail || out.Summary != in.Summary || out.Worker != in.Worker || out.Kind != in.Kind || !out.IsError {
t.Fatalf("round trip = %+v", out)
}
}

func TestCoverageEnabledWithoutTask(t *testing.T) {
on, err := (&AssetStore{}).CoverageEnabled(0)
if err != nil || !on {
t.Fatalf("enabled=%v err=%v", on, err)
}
}
5 changes: 4 additions & 1 deletion db/exploration.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ WHERE exploration_id=$1 AND kind='intent' AND state='running'
if err != nil {
return 0, err
}
n, _ := res.RowsAffected()
n, err := res.RowsAffected()
if err != nil {
return 0, err
}
return n, nil
}

Expand Down
11 changes: 11 additions & 0 deletions db/llm_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ func (d *DB) EnsureLLMUsageTable() error {
return d.execEmbeddedMigration(llmUsageMigration)
}

// TaskLLMSpend is the token and turn total already recorded for a task.
// One row is one turn. Cache read and write are not added: input_tokens already
// includes them. Every status counts, because a failed call is metered too.
func (d *DB) TaskLLMSpend(taskID string) (tokens, turns int64, err error) {
if d == nil || taskID == "" {
return 0, 0, nil
}
err = d.QueryRow(`SELECT COALESCE(SUM(input_tokens + output_tokens), 0), COUNT(*) FROM llm_usage WHERE task_id=$1`, taskID).Scan(&tokens, &turns)
return tokens, turns, err
}

// InsertLLMUsage appends one metering row. Best-effort: callers log and continue on
// error (a lost metering row must never break the LLM call).
func (d *DB) InsertLLMUsage(u *LLMUsage) error {
Expand Down
4 changes: 2 additions & 2 deletions db/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ func assertEmbeddedApplied(t *testing.T, d *DB) []appliedMigration {
want := []struct {
version int
name string
}{{1, llmRecordsMigration}, {2, llmUsageMigration}, {3, "0003_task_scope_icp_index.sql"}, {4, "0004_llm_usage_ts_index.sql"}}
}{{1, llmRecordsMigration}, {2, llmUsageMigration}, {3, "0003_task_scope_icp_index.sql"}, {4, "0004_llm_usage_ts_index.sql"}, {5, "0005_activity_outbox.sql"}}
if len(applied) != len(want) {
t.Fatalf("schema_migrations = %+v, want versions 1 to 4", applied)
t.Fatalf("schema_migrations = %+v, want versions 1 to 5", applied)
}
for i, w := range want {
content, err := fs.ReadFile(migrationsFS(), w.name)
Expand Down
14 changes: 14 additions & 0 deletions db/migrations/0005_activity_outbox.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Activities that could not be inserted (the exploration row was missing, or
-- the write failed after retries) are kept here and replayed into activity
-- once the parent row accepts them. No foreign key: the row has to survive
-- the missing exploration that caused the original failure.
CREATE TABLE IF NOT EXISTS activity_outbox (
id BIGSERIAL PRIMARY KEY,
exploration_id BIGINT NOT NULL,
payload JSONB NOT NULL,
last_error TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX IF NOT EXISTS activity_outbox_exploration_idx
ON activity_outbox (exploration_id, id);
19 changes: 12 additions & 7 deletions db/task_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,22 @@ type Coverage struct {
}

// CoverageEnabled reports whether a task has the asset-coverage feature turned on
// (tasks.coverage_enabled). Missing row / error → true (fail open to the default),
// so unknown/legacy tasks keep the historical behavior. taskID<=0 → true.
func (s *AssetStore) CoverageEnabled(taskID int64) bool {
// (tasks.coverage_enabled). A missing row stays enabled, which is the historical
// default for unknown and legacy tasks. taskID<=0 stays enabled. A query error is
// returned with enabled=true so a database blip does not turn the feature off.
func (s *AssetStore) CoverageEnabled(taskID int64) (bool, error) {
if taskID <= 0 {
return true
return true, nil
}
var enabled bool
if err := s.db.QueryRow(`SELECT COALESCE(coverage_enabled,true) FROM tasks WHERE id=$1`, taskID).Scan(&enabled); err != nil {
return true
err := s.db.QueryRow(`SELECT COALESCE(coverage_enabled,true) FROM tasks WHERE id=$1`, taskID).Scan(&enabled)
if err == sql.ErrNoRows {
return true, nil
}
return enabled
if err != nil {
return true, err
}
return enabled, nil
}

// declaredScopeFilter keeps a task_scope row ts in the coverage match only when it is
Expand Down
Loading