feat(backend): add per-run cost/token ledger for task usage (#2960)
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package backendapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
commoncosts "github.com/kandev/kandev/internal/common/costs"
|
||||
officeshared "github.com/kandev/kandev/internal/office/shared"
|
||||
)
|
||||
|
||||
// modelsDevLookup is the narrow slice of *modelsdev.Client (internal/office/
|
||||
// costs/modelsdev) this adapter needs, defined locally so it can be
|
||||
// exercised with a stub in tests without constructing a real Client.
|
||||
type modelsDevLookup interface {
|
||||
LookupForModelWithVersion(ctx context.Context, modelID string) (officeshared.ModelPricing, string, bool)
|
||||
}
|
||||
|
||||
// usagePricingAdapter adapts *modelsdev.Client's office/shared.ModelPricing-
|
||||
// returning LookupForModelWithVersion to internal/task/usage's own
|
||||
// PricingLookup interface (docs/specs/task-cost-ledger/spec.md AC-26): the
|
||||
// ledger writer's package boundary must never import internal/office/**, so
|
||||
// this conversion lives in the composition root instead.
|
||||
type usagePricingAdapter struct {
|
||||
lookup modelsDevLookup
|
||||
}
|
||||
|
||||
func (a usagePricingAdapter) LookupForModelWithVersion(ctx context.Context, modelID string) (commoncosts.ModelPricing, string, bool) {
|
||||
pricing, version, ok := a.lookup.LookupForModelWithVersion(ctx, modelID)
|
||||
return commoncosts.ModelPricing{
|
||||
InputPerMillion: pricing.InputPerMillion,
|
||||
CachedReadPerMillion: pricing.CachedReadPerMillion,
|
||||
CachedWritePerMillion: pricing.CachedWritePerMillion,
|
||||
OutputPerMillion: pricing.OutputPerMillion,
|
||||
}, version, ok
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package backendapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
officeshared "github.com/kandev/kandev/internal/office/shared"
|
||||
)
|
||||
|
||||
type stubModelsDevLookup struct {
|
||||
pricing officeshared.ModelPricing
|
||||
version string
|
||||
ok bool
|
||||
}
|
||||
|
||||
func (s stubModelsDevLookup) LookupForModelWithVersion(_ context.Context, _ string) (officeshared.ModelPricing, string, bool) {
|
||||
return s.pricing, s.version, s.ok
|
||||
}
|
||||
|
||||
// TestUsagePricingAdapter_ConvertsSharedModelPricingFieldForField pins
|
||||
// docs/specs/task-cost-ledger/spec.md AC-26: the adapter must translate
|
||||
// *modelsdev.Client's office/shared.ModelPricing into internal/task/usage's
|
||||
// own commoncosts.ModelPricing verbatim, field for field, so the ledger
|
||||
// writer's pricing resolution never depends on internal/office/** types.
|
||||
func TestUsagePricingAdapter_ConvertsSharedModelPricingFieldForField(t *testing.T) {
|
||||
stub := stubModelsDevLookup{
|
||||
pricing: officeshared.ModelPricing{
|
||||
InputPerMillion: 111,
|
||||
CachedReadPerMillion: 222,
|
||||
CachedWritePerMillion: 333,
|
||||
OutputPerMillion: 444,
|
||||
},
|
||||
version: "models-dev-2026-08-23",
|
||||
ok: true,
|
||||
}
|
||||
adapter := usagePricingAdapter{lookup: stub}
|
||||
|
||||
got, version, ok := adapter.LookupForModelWithVersion(context.Background(), "claude-sonnet-5")
|
||||
if !ok {
|
||||
t.Fatal("ok = false, want true")
|
||||
}
|
||||
if version != "models-dev-2026-08-23" {
|
||||
t.Errorf("version = %q, want models-dev-2026-08-23", version)
|
||||
}
|
||||
if got.InputPerMillion != 111 || got.CachedReadPerMillion != 222 || got.CachedWritePerMillion != 333 || got.OutputPerMillion != 444 {
|
||||
t.Errorf("got = %+v, want fields copied verbatim from office/shared.ModelPricing", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUsagePricingAdapter_PassesThroughNotOK pins the miss path: an unknown
|
||||
// model must surface as ok=false, not a zero-valued "success".
|
||||
func TestUsagePricingAdapter_PassesThroughNotOK(t *testing.T) {
|
||||
stub := stubModelsDevLookup{ok: false}
|
||||
adapter := usagePricingAdapter{lookup: stub}
|
||||
|
||||
_, _, ok := adapter.LookupForModelWithVersion(context.Background(), "unknown-model")
|
||||
if ok {
|
||||
t.Fatal("ok = true, want false when the underlying lookup misses")
|
||||
}
|
||||
}
|
||||
@@ -1181,6 +1181,31 @@ func initOfficeServices(
|
||||
// at all: blocked_by on create failed and list_related_tasks reported nothing.
|
||||
services.Task.SetBlockerRepository(repos.Office)
|
||||
|
||||
// office-costs Wave B: lazy models.dev pricing lookup. The Client
|
||||
// allocates no resources at startup — the first non-claude-acp cost
|
||||
// event triggers a disk read; the first missing cache file triggers
|
||||
// a background fetch. Workspaces running only claude-acp stay
|
||||
// untouched because Layer A handles every event before lookup.
|
||||
//
|
||||
// Constructed here, above the Office early return, because
|
||||
// task_usage_events's ledger writer (docs/specs/task-cost-ledger/spec.md
|
||||
// AC-10, AC-26) needs it in every install, not just Office-enabled ones.
|
||||
// SetPricingLookup/SetModelInfoLookup below stay gated - the Office
|
||||
// service and orchestrator model-info surface those Office features
|
||||
// widen only apply when the feature is on.
|
||||
modelsdevCachePath := filepath.Join(cfg.ResolvedHomeDir(), "cache", "models-dev.json")
|
||||
pricingLookup := officemodelsdev.New(officemodelsdev.Config{
|
||||
CachePath: modelsdevCachePath,
|
||||
}, log)
|
||||
|
||||
// The ledger writer is the sole writer of task_sessions' usage rollup
|
||||
// columns (AC-10) and must run in every install, so it too is
|
||||
// constructed and started before the Office early return.
|
||||
if err := startTaskUsageWriter(repos.Task, usagePricingAdapter{lookup: pricingLookup}, eventBus, log, addCleanup); err != nil {
|
||||
log.Error("Failed to subscribe task usage ledger writer", zap.Error(err))
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if !cfg.Features.Office {
|
||||
log.Info("Office feature disabled; Office services skipped while global run scheduling remains enabled")
|
||||
return runProcessorSvc, true
|
||||
@@ -1189,18 +1214,8 @@ func initOfficeServices(
|
||||
services.Office = runProcessorSvc
|
||||
log.Info("Office service constructed with all dependencies")
|
||||
|
||||
// office-costs Wave B: lazy models.dev pricing lookup. The Client
|
||||
// allocates no resources at startup — the first non-claude-acp cost
|
||||
// event triggers a disk read; the first missing cache file triggers
|
||||
// a background fetch. Workspaces running only claude-acp stay
|
||||
// untouched because Layer A handles every event before lookup.
|
||||
modelsdevCachePath := filepath.Join(cfg.ResolvedHomeDir(), "cache", "models-dev.json")
|
||||
pricingLookup := officemodelsdev.New(officemodelsdev.Config{
|
||||
CachePath: modelsdevCachePath,
|
||||
}, log)
|
||||
services.Office.SetPricingLookup(pricingLookup)
|
||||
orchestratorSvc.SetModelInfoLookup(pricingLookup)
|
||||
services.Office.SetSessionUsageWriter(repos.Task)
|
||||
|
||||
// ADR 0005 Wave E: plug the runtime-tier skill deployer into the
|
||||
// lifecycle manager. The deployer reads office's skills repo +
|
||||
|
||||
@@ -144,6 +144,7 @@ func provideServices(cfg *config.Config, log *logger.Logger, repos *Repositories
|
||||
StatusSummaries: repos.Task,
|
||||
TaskActivity: repos.Task,
|
||||
SubagentContexts: repos.Task,
|
||||
Usage: repos.Task,
|
||||
},
|
||||
eventBus,
|
||||
log,
|
||||
|
||||
@@ -70,13 +70,6 @@ func provideRepositories(ctx context.Context, cfg *config.Config, log *logger.Lo
|
||||
return nil, nil, nil, fmt.Errorf("office repo: %w", err)
|
||||
}
|
||||
cleanups = append(cleanups, officeCleanup)
|
||||
// Must run after office.Provide succeeds: it reconciles task_sessions
|
||||
// against office's office_cost_events ledger, and construction order is
|
||||
// what guarantees that table exists now - see the doc comment on
|
||||
// sqlite.Repository.BackfillSessionTokensCachedIn.
|
||||
if err := backfillSessionCachedTokens(ctx, taskRepoImpl, log); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
terminalRepoImpl, err := terminalrepo.NewWithDB(writer, reader, log)
|
||||
if err != nil {
|
||||
return nil, nil, nil, fmt.Errorf("terminal repo: %w", err)
|
||||
@@ -133,30 +126,6 @@ func provideRepositories(ctx context.Context, cfg *config.Config, log *logger.Lo
|
||||
return pool, repos, cleanups, nil
|
||||
}
|
||||
|
||||
// sessionCachedTokenBackfiller is the narrow slice of *sqlite.Repository
|
||||
// (internal/task/repository/sqlite) this composition root needs, defined
|
||||
// locally so this file doesn't have to import that subpackage just for one
|
||||
// call. See sqlite.Repository.BackfillSessionTokensCachedIn's doc comment
|
||||
// for why this call lives here rather than inside the task repository's own
|
||||
// runMigrations(): it reconciles task_sessions against the office
|
||||
// repository's office_cost_events ledger, and only this composition root
|
||||
// knows both repositories have finished initializing.
|
||||
type sessionCachedTokenBackfiller interface {
|
||||
BackfillSessionTokensCachedIn(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
// backfillSessionCachedTokens triggers the task repository's cached-token
|
||||
// reconciliation and logs how many task_sessions rows it reconciled.
|
||||
// Extracted out of provideRepositories to keep it within the funlen limit.
|
||||
func backfillSessionCachedTokens(ctx context.Context, taskRepo sessionCachedTokenBackfiller, log *logger.Logger) error {
|
||||
affected, err := taskRepo.BackfillSessionTokensCachedIn(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("reconciled task_sessions.tokens_cached_in against office_cost_events", zap.Int64("rows", affected))
|
||||
return nil
|
||||
}
|
||||
|
||||
// supportRepositorySet groups the lighter-weight support repositories
|
||||
// that share a common (writer, reader) wire-up pattern.
|
||||
type supportRepositorySet struct {
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
package backendapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/config"
|
||||
)
|
||||
|
||||
// TestProvideRepositoriesBackfillsSessionCachedTokens is the composition-level
|
||||
// regression test for moving task_sessions.tokens_cached_in's backfill
|
||||
// trigger out of the task repository's own runMigrations() and into this
|
||||
// package (see the "PR Fixup" plan section on carlosflorencio's PR #2521
|
||||
// architecture comment). Neither repository's own package tests can see the
|
||||
// other's construction order, so only a test that boots the real repository
|
||||
// graph through provideRepositories can prove the backfill still actually
|
||||
// runs — a regression here would silently resurrect the original card's bug
|
||||
// (task_sessions.tokens_cached_in drifting from the office_cost_events
|
||||
// ledger) without any single-package test failing.
|
||||
func TestProvideRepositoriesBackfillsSessionCachedTokens(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
cfg := &config.Config{
|
||||
HomeDir: t.TempDir(),
|
||||
Database: config.DatabaseConfig{Driver: "sqlite"},
|
||||
}
|
||||
log := newTestLogger()
|
||||
|
||||
// First boot: creates the schema (both repositories' tables, including
|
||||
// office_cost_events) against a real on-disk SQLite file. Then seed a
|
||||
// drifted task_sessions row directly - bypassing IncrementTaskSessionUsage
|
||||
// - the same way a missed live increment would leave one, plus a ledger
|
||||
// row establishing the true value the next boot's backfill must recover.
|
||||
pool, _, cleanups, err := provideRepositories(ctx, cfg, log, "test")
|
||||
if err != nil {
|
||||
t.Fatalf("provideRepositories (first boot): %v", err)
|
||||
}
|
||||
writer := pool.Writer()
|
||||
now := time.Now().UTC()
|
||||
if _, err := writer.Exec(writer.Rebind(
|
||||
`INSERT INTO workspaces (id, name, created_at, updated_at) VALUES (?, 'test workspace', ?, ?)`),
|
||||
"ws-cached-boot", now, now); err != nil {
|
||||
t.Fatalf("seed workspace: %v", err)
|
||||
}
|
||||
if _, err := writer.Exec(writer.Rebind(
|
||||
`INSERT INTO tasks (id, workspace_id, title, created_at, updated_at) VALUES (?, ?, 'test', ?, ?)`),
|
||||
"task-cached-boot", "ws-cached-boot", now, now); err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
if _, err := writer.Exec(writer.Rebind(
|
||||
`INSERT INTO task_sessions (id, task_id, started_at, updated_at, tokens_cached_in) VALUES (?, ?, ?, ?, ?)`),
|
||||
"sess-cached-boot", "task-cached-boot", now, now, 2_000); err != nil {
|
||||
t.Fatalf("seed drifted session: %v", err)
|
||||
}
|
||||
if _, err := writer.Exec(writer.Rebind(
|
||||
`INSERT INTO office_cost_events (id, session_id, tokens_cached_in, occurred_at, created_at) VALUES (?, ?, ?, ?, ?)`),
|
||||
"cost-cached-boot", "sess-cached-boot", 98_805_109, now, now); err != nil {
|
||||
t.Fatalf("seed ledger row: %v", err)
|
||||
}
|
||||
for i := len(cleanups) - 1; i >= 0; i-- {
|
||||
if cleanups[i] != nil {
|
||||
_ = cleanups[i]()
|
||||
}
|
||||
}
|
||||
|
||||
// Second boot against the same on-disk DB (same cfg.HomeDir, so the same
|
||||
// kandev.db file) simulates a restart. The composition-level backfill
|
||||
// call in provideRepositories must reconcile the drifted row above.
|
||||
pool2, _, cleanups2, err := provideRepositories(ctx, cfg, log, "test")
|
||||
if err != nil {
|
||||
t.Fatalf("provideRepositories (second boot): %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for i := len(cleanups2) - 1; i >= 0; i-- {
|
||||
if cleanups2[i] != nil {
|
||||
_ = cleanups2[i]()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
var got int64
|
||||
writer2 := pool2.Writer()
|
||||
if err := writer2.QueryRowx(writer2.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-cached-boot",
|
||||
).Scan(&got); err != nil {
|
||||
t.Fatalf("read row after second boot: %v", err)
|
||||
}
|
||||
if got != 98_805_109 {
|
||||
t.Errorf("tokens_cached_in after reboot = %d, want 98805109 (the drifted 2000 must be "+
|
||||
"reconciled against the office_cost_events ledger by the composition-level backfill "+
|
||||
"call in provideRepositories, after office.Provide succeeds)", got)
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,10 @@ import (
|
||||
// still calls it. A regression that dropped the call site, or moved it ahead
|
||||
// of a repository's initSchema, would leave telemetry_activations silently
|
||||
// missing rows on every real boot with the rest of the suite fully green —
|
||||
// mirrors TestProvideRepositoriesBackfillsSessionCachedTokens's rationale for
|
||||
// the neighboring backfillSessionCachedTokens call.
|
||||
// the same class of composition-level boot regression coverage this package
|
||||
// uses elsewhere for provideRepositories' other post-initSchema side effects
|
||||
// (see, for example, TestPostgresBootInitializesRepositories in
|
||||
// postgres_boot_test.go).
|
||||
//
|
||||
// telemetry_activations itself is created unconditionally by
|
||||
// telemetrycontract.NewWithDB, independent of ordering, so a row-count
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package backendapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/config"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
// TestProvideRepositoriesPreservesUsageRollupAcrossReboot closes Review
|
||||
// Round 2's M3 finding: the task plan's AC-36 row claimed
|
||||
// TestBackfillSkippedWhenTaskSessionsAlreadyHasUsageColumns (a boot-replay
|
||||
// test) covered "rollup survives a reboot", but that test was deleted along
|
||||
// with the one-time backfill migration it exercised (the backfill became
|
||||
// unnecessary once task_usage_events shipped as a day-one feature with no
|
||||
// pre-existing rollup data to migrate) - leaving no replacement proving a
|
||||
// real second boot doesn't touch already-populated rollup columns.
|
||||
//
|
||||
// This test boots provideRepositories on a temp on-disk SQLite database,
|
||||
// writes one ledger row through the real CreateTaskUsageEvent path (so
|
||||
// task_sessions.tokens_in/tokens_cached_in/tokens_out/cost_subcents are
|
||||
// populated exactly as production sets them), fully tears the boot down via
|
||||
// its cleanups (closing the database), then calls provideRepositories again
|
||||
// on the same on-disk file and asserts all four rollup columns are
|
||||
// unchanged - schema init and migrations on the second boot must not reset,
|
||||
// re-derive, or re-backfill them.
|
||||
func TestProvideRepositoriesPreservesUsageRollupAcrossReboot(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
cfg := &config.Config{
|
||||
HomeDir: t.TempDir(),
|
||||
Database: config.DatabaseConfig{Driver: "sqlite"},
|
||||
}
|
||||
|
||||
firstPool, firstRepos, firstCleanups, err := provideRepositories(ctx, cfg, nil, "test")
|
||||
if err != nil {
|
||||
t.Fatalf("first provideRepositories: %v", err)
|
||||
}
|
||||
_ = firstPool
|
||||
firstClosed := false
|
||||
closeFirst := func() {
|
||||
if firstClosed {
|
||||
return
|
||||
}
|
||||
firstClosed = true
|
||||
for i := len(firstCleanups) - 1; i >= 0; i-- {
|
||||
if firstCleanups[i] != nil {
|
||||
_ = firstCleanups[i]()
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Cleanup(closeFirst)
|
||||
|
||||
if err := firstRepos.Task.CreateTask(ctx, &models.Task{
|
||||
ID: "task-reboot-rollup",
|
||||
WorkspaceID: "ws-1",
|
||||
Title: "Usage rollup reboot test task",
|
||||
Priority: "medium",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateTask: %v", err)
|
||||
}
|
||||
if err := firstRepos.Task.CreateTaskSession(ctx, &models.TaskSession{
|
||||
ID: "session-reboot-rollup",
|
||||
TaskID: "task-reboot-rollup",
|
||||
State: models.TaskSessionStateWaitingForInput,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateTaskSession: %v", err)
|
||||
}
|
||||
|
||||
tokensCachedRead := int64(20)
|
||||
tokensCachedWrite := int64(5)
|
||||
tokensOut := int64(30)
|
||||
if err := firstRepos.Task.CreateTaskUsageEvent(ctx, &models.TaskUsageEvent{
|
||||
UsageEventID: "evt-reboot-rollup",
|
||||
TaskID: "task-reboot-rollup",
|
||||
SessionID: "session-reboot-rollup",
|
||||
AgentType: "claude",
|
||||
Model: "claude-sonnet-5",
|
||||
Provider: "anthropic",
|
||||
TokensIn: 100,
|
||||
TokensCachedRead: &tokensCachedRead,
|
||||
TokensCachedWrite: &tokensCachedWrite,
|
||||
TokensOut: &tokensOut,
|
||||
TokensTotal: 155,
|
||||
CostSubcents: 42,
|
||||
CostSource: "actual",
|
||||
ContractVersion: 1,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
before, err := firstRepos.Task.GetTaskSession(ctx, "session-reboot-rollup")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskSession before reboot: %v", err)
|
||||
}
|
||||
if before.TokensIn != 100 || before.TokensCachedIn != 25 || before.TokensOut != 30 || before.CostSubcents != 42 {
|
||||
t.Fatalf("rollup before reboot = (%d,%d,%d,%d), want (100,25,30,42)",
|
||||
before.TokensIn, before.TokensCachedIn, before.TokensOut, before.CostSubcents)
|
||||
}
|
||||
|
||||
// Tear the first boot fully down (closes the database) before reopening
|
||||
// the same on-disk file, so the second provideRepositories call is a
|
||||
// genuine cold reboot rather than a second handle onto a live pool.
|
||||
closeFirst()
|
||||
|
||||
_, secondRepos, secondCleanups, err := provideRepositories(ctx, cfg, nil, "test")
|
||||
if err != nil {
|
||||
t.Fatalf("second provideRepositories: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for i := len(secondCleanups) - 1; i >= 0; i-- {
|
||||
if secondCleanups[i] != nil {
|
||||
_ = secondCleanups[i]()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
after, err := secondRepos.Task.GetTaskSession(ctx, "session-reboot-rollup")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskSession after reboot: %v", err)
|
||||
}
|
||||
if after.TokensIn != before.TokensIn || after.TokensCachedIn != before.TokensCachedIn ||
|
||||
after.TokensOut != before.TokensOut || after.CostSubcents != before.CostSubcents {
|
||||
t.Errorf("rollup after reboot = (%d,%d,%d,%d), want unchanged (%d,%d,%d,%d): "+
|
||||
"a second boot's schema init/migrations must not reset or re-derive already-populated usage rollup columns",
|
||||
after.TokensIn, after.TokensCachedIn, after.TokensOut, after.CostSubcents,
|
||||
before.TokensIn, before.TokensCachedIn, before.TokensOut, before.CostSubcents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package backendapp
|
||||
|
||||
import (
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
taskusage "github.com/kandev/kandev/internal/task/usage"
|
||||
)
|
||||
|
||||
func startTaskUsageWriter(
|
||||
repo taskusage.Repository,
|
||||
pricing taskusage.PricingLookup,
|
||||
eventBus bus.EventBus,
|
||||
log *logger.Logger,
|
||||
addCleanup func(func() error),
|
||||
) error {
|
||||
writer := taskusage.NewWriter(repo, pricing, log)
|
||||
writer.Start()
|
||||
if err := writer.Subscribe(eventBus); err != nil {
|
||||
writer.Stop()
|
||||
return err
|
||||
}
|
||||
addCleanup(func() error {
|
||||
writer.Stop()
|
||||
return nil
|
||||
})
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package backendapp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
type taskUsageWiringRepo struct {
|
||||
recorded chan *models.TaskUsageEvent
|
||||
}
|
||||
|
||||
func (r *taskUsageWiringRepo) CreateTaskUsageEvent(_ context.Context, event *models.TaskUsageEvent) error {
|
||||
r.recorded <- event
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestInitOfficeServicesUsesTaskUsageWiringHelper(t *testing.T) {
|
||||
fset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fset, "main.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("parse main.go: %v", err)
|
||||
}
|
||||
|
||||
var initOffice *ast.FuncDecl
|
||||
for _, decl := range file.Decls {
|
||||
fn, ok := decl.(*ast.FuncDecl)
|
||||
if ok && fn.Name.Name == "initOfficeServices" {
|
||||
initOffice = fn
|
||||
break
|
||||
}
|
||||
}
|
||||
if initOffice == nil {
|
||||
t.Fatal("initOfficeServices not found in main.go")
|
||||
}
|
||||
|
||||
var helperCalls int
|
||||
ast.Inspect(initOffice, func(node ast.Node) bool {
|
||||
call, ok := node.(*ast.CallExpr)
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
if name, ok := call.Fun.(*ast.Ident); ok && name.Name == "startTaskUsageWriter" {
|
||||
helperCalls++
|
||||
}
|
||||
return true
|
||||
})
|
||||
if helperCalls != 1 {
|
||||
t.Fatalf("startTaskUsageWriter calls = %d, want exactly one composition call", helperCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartTaskUsageWriter_PublishesIntoRepository(t *testing.T) {
|
||||
repo := &taskUsageWiringRepo{recorded: make(chan *models.TaskUsageEvent, 1)}
|
||||
eventBus := bus.NewMemoryEventBus(logger.Default())
|
||||
var cleanups []func() error
|
||||
addCleanup := func(cleanup func() error) {
|
||||
cleanups = append(cleanups, cleanup)
|
||||
}
|
||||
if err := startTaskUsageWriter(repo, nil, eventBus, nil, addCleanup); err != nil {
|
||||
t.Fatalf("startTaskUsageWriter: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
for i := len(cleanups) - 1; i >= 0; i-- {
|
||||
if err := cleanups[i](); err != nil {
|
||||
t.Errorf("cleanup: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if err := eventBus.Publish(context.Background(), events.BuildSessionPromptUsageSubject("session-1"), &bus.Event{
|
||||
Data: map[string]any{
|
||||
"task_id": "task-1",
|
||||
"session_id": "session-1",
|
||||
"usage_event_id": "evt-wiring",
|
||||
"usage": map[string]any{"input_tokens": int64(7)},
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
|
||||
select {
|
||||
case event := <-repo.recorded:
|
||||
if event.UsageEventID != "evt-wiring" || event.TokensIn != 7 {
|
||||
t.Fatalf("recorded event = %+v, want usage event evt-wiring with 7 input tokens", event)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("task usage writer did not receive the published event")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
// Package costs holds cost-arithmetic and provider-derivation logic shared
|
||||
// across the backend and agentctl tiers (docs/specs/task-cost-ledger/spec.md).
|
||||
// It must not import internal/office/** or internal/task/**: both the Office
|
||||
// cost pipeline and the task usage ledger delegate to this package, not the
|
||||
// other way around.
|
||||
package costs
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ModelPricing holds per-million-token pricing for a model. All units
|
||||
// are hundredths of a cent (subcents) per million tokens — keeps the math
|
||||
// integer-only and matches the storage unit on office_cost_events.cost_subcents
|
||||
// and task_usage_events.cost_subcents.
|
||||
type ModelPricing struct {
|
||||
InputPerMillion int64
|
||||
CachedReadPerMillion int64
|
||||
CachedWritePerMillion int64
|
||||
OutputPerMillion int64
|
||||
}
|
||||
|
||||
// CalculateCostSubcents computes estimated cost from token counts and pricing.
|
||||
// All token counts are int64 to match the wire types from streams.PromptUsage.
|
||||
// Returns 0 if pricing is the zero value or an intermediate operation
|
||||
// overflows int64. Cached read and cached write are passed separately because
|
||||
// Anthropic charges different rates (cached writes cost ~25% more than the
|
||||
// base input rate). Call CalculateCostSubcentsChecked when the caller needs
|
||||
// to distinguish overflow from a legitimate zero cost.
|
||||
func CalculateCostSubcents(
|
||||
tokensIn, tokensCachedRead, tokensCachedWrite, tokensOut int64,
|
||||
pricing ModelPricing,
|
||||
) int64 {
|
||||
cost, ok := CalculateCostSubcentsChecked(tokensIn, tokensCachedRead, tokensCachedWrite, tokensOut, pricing)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return cost
|
||||
}
|
||||
|
||||
// CalculateCostSubcentsChecked computes estimated cost and reports whether
|
||||
// every intermediate operation fits in int64. A false result prevents a
|
||||
// wrapped value from entering a durable cost ledger.
|
||||
func CalculateCostSubcentsChecked(
|
||||
tokensIn, tokensCachedRead, tokensCachedWrite, tokensOut int64,
|
||||
pricing ModelPricing,
|
||||
) (int64, bool) {
|
||||
terms := [][2]int64{
|
||||
{tokensIn, pricing.InputPerMillion},
|
||||
{tokensCachedRead, pricing.CachedReadPerMillion},
|
||||
{tokensCachedWrite, pricing.CachedWritePerMillion},
|
||||
{tokensOut, pricing.OutputPerMillion},
|
||||
}
|
||||
var total int64
|
||||
for _, term := range terms {
|
||||
product, ok := checkedMul(term[0], term[1])
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
total, ok = checkedAdd(total, product)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
return total / 1_000_000, true
|
||||
}
|
||||
|
||||
func checkedAdd(a, b int64) (int64, bool) {
|
||||
if b > 0 && a > math.MaxInt64-b {
|
||||
return 0, false
|
||||
}
|
||||
if b < 0 && a < math.MinInt64-b {
|
||||
return 0, false
|
||||
}
|
||||
return a + b, true
|
||||
}
|
||||
|
||||
func checkedMul(a, b int64) (int64, bool) {
|
||||
if a == 0 || b == 0 {
|
||||
return 0, true
|
||||
}
|
||||
if (a == math.MinInt64 && b == -1) || (b == math.MinInt64 && a == -1) {
|
||||
return 0, false
|
||||
}
|
||||
product := a * b
|
||||
if product/b != a {
|
||||
return 0, false
|
||||
}
|
||||
return product, true
|
||||
}
|
||||
|
||||
// ProviderForModel returns a best-guess provider id for a model name, used
|
||||
// when the CLI payload doesn't already carry a provider (it does for claude-acp
|
||||
// once the subscriber sets it from the CLI id). Returns "" when the prefix is
|
||||
// unknown.
|
||||
func ProviderForModel(model string) string {
|
||||
switch {
|
||||
case strings.HasPrefix(model, "claude"):
|
||||
return "anthropic"
|
||||
case strings.HasPrefix(model, "gpt") || strings.HasPrefix(model, "o3") || strings.HasPrefix(model, "o4"):
|
||||
return "openai"
|
||||
case strings.HasPrefix(model, "gemini"):
|
||||
return "google"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ProviderFromCLI maps the upstream CLI id (the agent_id/agent_type stream
|
||||
// field) to a provider name. Used because claude-acp emits logical model
|
||||
// aliases (sonnet / haiku) that can't be matched on prefix.
|
||||
func ProviderFromCLI(cli string) string {
|
||||
switch cli {
|
||||
case "claude-acp":
|
||||
return "anthropic"
|
||||
case "codex-acp", "openai-acp":
|
||||
return "openai"
|
||||
case "gemini", "gemini-acp":
|
||||
return "google"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package costs_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/costs"
|
||||
)
|
||||
|
||||
func TestCalculateCostSubcents(t *testing.T) {
|
||||
// Anthropic-ish rates: $3/M input, $0.30/M cached-read, $3.75/M cached-write,
|
||||
// $15/M output. In subcents/M that's 30000, 3000, 37500, 150000.
|
||||
pricing := costs.ModelPricing{
|
||||
InputPerMillion: 30000,
|
||||
CachedReadPerMillion: 3000,
|
||||
CachedWritePerMillion: 37500,
|
||||
OutputPerMillion: 150000,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
in, cachedRead, cachedWrite, out int64
|
||||
want int64
|
||||
}{
|
||||
{"zero tokens => zero cost", 0, 0, 0, 0, 0},
|
||||
{"input only 1M => 30000 subcents = $3", 1_000_000, 0, 0, 0, 30000},
|
||||
{"output only 1M => 150000 subcents = $15", 0, 0, 0, 1_000_000, 150000},
|
||||
{"cached read vs cached write priced separately",
|
||||
0, 1_000_000, 1_000_000, 0,
|
||||
3000 + 37500},
|
||||
{"mixed: realistic turn",
|
||||
500, 1000, 2000, 100,
|
||||
(500*30000 + 1000*3000 + 2000*37500 + 100*150000) / 1_000_000},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := costs.CalculateCostSubcents(tc.in, tc.cachedRead, tc.cachedWrite, tc.out, pricing)
|
||||
if got != tc.want {
|
||||
t.Errorf("CalculateCostSubcents() = %d, want %d", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateCostSubcents_ZeroPricing(t *testing.T) {
|
||||
if got := costs.CalculateCostSubcents(1_000_000, 0, 0, 1_000_000, costs.ModelPricing{}); got != 0 {
|
||||
t.Errorf("zero pricing should yield zero cost, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateCostSubcents_OverflowReturnsZero(t *testing.T) {
|
||||
pricing := costs.ModelPricing{InputPerMillion: 3}
|
||||
checked, ok := costs.CalculateCostSubcentsChecked(math.MaxInt64/2+1, 0, 0, 0, pricing)
|
||||
if ok || checked != 0 {
|
||||
t.Fatalf("checked overflow result = (%d, %t), want (0, false)", checked, ok)
|
||||
}
|
||||
got := costs.CalculateCostSubcents(math.MaxInt64/2+1, 0, 0, 0, pricing)
|
||||
if got != 0 {
|
||||
t.Fatalf("overflowing cost = %d, want zero instead of a wrapped value", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderForModel(t *testing.T) {
|
||||
tests := []struct {
|
||||
model, want string
|
||||
}{
|
||||
{"claude-opus-4-7", "anthropic"},
|
||||
{"claude-sonnet-4-5", "anthropic"},
|
||||
{"gpt-5-mini", "openai"},
|
||||
{"gpt-4.1", "openai"},
|
||||
{"o3-mini", "openai"},
|
||||
{"o4-mini", "openai"},
|
||||
{"gemini-2.5-pro", "google"},
|
||||
{"gemini-3-pro-preview", "google"},
|
||||
{"butler_a", ""},
|
||||
{"kimi-k2p6", ""},
|
||||
{"", ""},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.model, func(t *testing.T) {
|
||||
if got := costs.ProviderForModel(tc.model); got != tc.want {
|
||||
t.Errorf("ProviderForModel(%q) = %q, want %q", tc.model, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderFromCLI(t *testing.T) {
|
||||
tests := []struct {
|
||||
cli, want string
|
||||
}{
|
||||
{"claude-acp", "anthropic"},
|
||||
{"codex-acp", "openai"},
|
||||
{"openai-acp", "openai"},
|
||||
{"gemini", "google"},
|
||||
{"gemini-acp", "google"},
|
||||
{"unknown-cli", ""},
|
||||
{"", ""},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.cli, func(t *testing.T) {
|
||||
if got := costs.ProviderFromCLI(tc.cli); got != tc.want {
|
||||
t.Errorf("ProviderFromCLI(%q) = %q, want %q", tc.cli, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,17 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
postgresDuplicateColumn = "42701"
|
||||
postgresDuplicateTable = "42P07"
|
||||
postgresDuplicateObject = "42710"
|
||||
postgresUndefinedTable = "42P01"
|
||||
sqliteAlreadyExistsText = " already exists"
|
||||
postgresDuplicateColumn = "42701"
|
||||
postgresDuplicateTable = "42P07"
|
||||
postgresDuplicateObject = "42710"
|
||||
postgresUndefinedTable = "42P01"
|
||||
postgresForeignKeyViolation = "23503"
|
||||
postgresSerializationFailure = "40001"
|
||||
postgresDeadlockDetected = "40P01"
|
||||
sqliteAlreadyExistsText = " already exists"
|
||||
sqliteForeignKeyViolationMsg = "FOREIGN KEY constraint failed"
|
||||
sqliteBusyText = "database is locked"
|
||||
sqliteLockedText = "database table is locked"
|
||||
)
|
||||
|
||||
// IsDuplicateColumnError reports whether err means an ADD COLUMN migration has
|
||||
@@ -72,3 +78,37 @@ func IsMissingTableError(err error) bool {
|
||||
}
|
||||
return strings.Contains(err.Error(), "no such table")
|
||||
}
|
||||
|
||||
// IsForeignKeyViolation reports whether err means a write failed because it
|
||||
// referenced a row that does not exist (or no longer exists) in a
|
||||
// foreign-keyed table.
|
||||
func IsForeignKeyViolation(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
return pgErr.Code == postgresForeignKeyViolation
|
||||
}
|
||||
return strings.Contains(err.Error(), sqliteForeignKeyViolationMsg)
|
||||
}
|
||||
|
||||
// IsTransientError reports whether err means a write failed for a reason
|
||||
// that is expected to clear on retry: a serialization failure or deadlock on
|
||||
// PostgreSQL, or the single-writer connection being busy/locked on SQLite.
|
||||
func IsTransientError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
switch pgErr.Code {
|
||||
case postgresSerializationFailure, postgresDeadlockDetected:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
s := err.Error()
|
||||
return strings.Contains(s, sqliteBusyText) || strings.Contains(s, sqliteLockedText)
|
||||
}
|
||||
|
||||
@@ -195,3 +195,127 @@ func TestIsMissingTableError(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsForeignKeyViolation(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "postgres foreign key violation",
|
||||
err: &pgconn.PgError{Code: postgresForeignKeyViolation},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped postgres foreign key violation",
|
||||
err: fmt.Errorf("insert usage event: %w", &pgconn.PgError{Code: postgresForeignKeyViolation}),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "postgres unique violation is not a foreign key violation",
|
||||
err: &pgconn.PgError{Code: "23505"},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "sqlite foreign key constraint failed",
|
||||
err: errors.New("FOREIGN KEY constraint failed"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped sqlite foreign key constraint failed",
|
||||
err: fmt.Errorf("insert usage event: %w", errors.New("FOREIGN KEY constraint failed")),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "sqlite unique constraint is not a foreign key violation",
|
||||
err: errors.New("UNIQUE constraint failed: task_usage_events.usage_event_id"),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "unrelated",
|
||||
err: errors.New("no such table: task_usage_events"),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "nil",
|
||||
err: nil,
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsForeignKeyViolation(tt.err); got != tt.want {
|
||||
t.Fatalf("IsForeignKeyViolation() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTransientError(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "postgres serialization failure",
|
||||
err: &pgconn.PgError{Code: postgresSerializationFailure},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "postgres deadlock detected",
|
||||
err: &pgconn.PgError{Code: postgresDeadlockDetected},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped postgres serialization failure",
|
||||
err: fmt.Errorf("insert usage event: %w", &pgconn.PgError{Code: postgresSerializationFailure}),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "postgres foreign key violation is not transient",
|
||||
err: &pgconn.PgError{Code: postgresForeignKeyViolation},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "sqlite busy",
|
||||
err: errors.New("database is locked"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "sqlite locked",
|
||||
err: errors.New("database table is locked"),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "wrapped sqlite busy",
|
||||
err: fmt.Errorf("insert usage event: %w", errors.New("database is locked")),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "sqlite constraint error is not transient",
|
||||
err: errors.New("UNIQUE constraint failed: task_usage_events.usage_event_id"),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "unrelated",
|
||||
err: errors.New("no such table: task_usage_events"),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "nil",
|
||||
err: nil,
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsTransientError(tt.err); got != tt.want {
|
||||
t.Fatalf("IsTransientError() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,7 +262,7 @@ func (c *Client) warmFromDisk(ctx context.Context) {
|
||||
}
|
||||
// File missing on first boot — schedule a refresh; lookup
|
||||
// returns miss this turn.
|
||||
c.startBackgroundRefresh(ctx)
|
||||
c.startBackgroundRefresh(context.WithoutCancel(ctx))
|
||||
return
|
||||
}
|
||||
buf, err := os.ReadFile(c.cachePath)
|
||||
|
||||
@@ -1,45 +1,33 @@
|
||||
package costs
|
||||
|
||||
import "strings"
|
||||
import commoncosts "github.com/kandev/kandev/internal/common/costs"
|
||||
|
||||
// ModelPricing holds per-million-token pricing for a model. All units
|
||||
// are hundredths of a cent (subcents) per million tokens — keeps the math
|
||||
// integer-only and matches the storage unit on office_cost_events.cost_subcents.
|
||||
type ModelPricing struct {
|
||||
InputPerMillion int64
|
||||
CachedReadPerMillion int64
|
||||
CachedWritePerMillion int64
|
||||
OutputPerMillion int64
|
||||
}
|
||||
// ModelPricing is an alias of the shared costs package's type
|
||||
// (docs/specs/task-cost-ledger/spec.md) so existing costs.ModelPricing{...}
|
||||
// call sites and struct literals in this package keep working unchanged.
|
||||
type ModelPricing = commoncosts.ModelPricing
|
||||
|
||||
// CalculateCostSubcents computes estimated cost from token counts and pricing.
|
||||
// All token counts are int64 to match the wire types from streams.PromptUsage.
|
||||
// Returns 0 if pricing is the zero value. Cached read and cached write are
|
||||
// passed separately because Anthropic charges different rates (cached writes
|
||||
// cost ~25% more than the base input rate).
|
||||
// CalculateCostSubcents delegates to internal/common/costs, the single
|
||||
// source of truth for the cost formula shared with the task usage ledger
|
||||
// writer.
|
||||
func CalculateCostSubcents(
|
||||
tokensIn, tokensCachedRead, tokensCachedWrite, tokensOut int64,
|
||||
pricing ModelPricing,
|
||||
) int64 {
|
||||
cost := tokensIn*pricing.InputPerMillion +
|
||||
tokensCachedRead*pricing.CachedReadPerMillion +
|
||||
tokensCachedWrite*pricing.CachedWritePerMillion +
|
||||
tokensOut*pricing.OutputPerMillion
|
||||
return cost / 1_000_000
|
||||
return commoncosts.CalculateCostSubcents(tokensIn, tokensCachedRead, tokensCachedWrite, tokensOut, pricing)
|
||||
}
|
||||
|
||||
// ProviderForModel returns a best-guess provider id for a model name, used
|
||||
// when the CLI payload doesn't already carry a provider (it does for claude-acp
|
||||
// once the subscriber sets it from the CLI id). Returns "" when the prefix is
|
||||
// unknown.
|
||||
func ProviderForModel(model string) string {
|
||||
switch {
|
||||
case strings.HasPrefix(model, "claude"):
|
||||
return "anthropic"
|
||||
case strings.HasPrefix(model, "gpt") || strings.HasPrefix(model, "o3") || strings.HasPrefix(model, "o4"):
|
||||
return "openai"
|
||||
case strings.HasPrefix(model, "gemini"):
|
||||
return "google"
|
||||
}
|
||||
return ""
|
||||
// CalculateCostSubcentsChecked exposes the shared overflow-aware calculation
|
||||
// to Office callers that need to distinguish an unpriced result from a real
|
||||
// zero-cost calculation.
|
||||
func CalculateCostSubcentsChecked(
|
||||
tokensIn, tokensCachedRead, tokensCachedWrite, tokensOut int64,
|
||||
pricing ModelPricing,
|
||||
) (int64, bool) {
|
||||
return commoncosts.CalculateCostSubcentsChecked(tokensIn, tokensCachedRead, tokensCachedWrite, tokensOut, pricing)
|
||||
}
|
||||
|
||||
// ProviderForModel delegates to internal/common/costs.
|
||||
func ProviderForModel(model string) string {
|
||||
return commoncosts.ProviderForModel(model)
|
||||
}
|
||||
|
||||
@@ -109,17 +109,6 @@ func (r *Repository) ExecRaw(ctx context.Context, query string, args ...interfac
|
||||
// package should keep using typed methods.
|
||||
func (r *Repository) ReaderDB() *sqlx.DB { return r.ro }
|
||||
|
||||
// BeginTx starts a transaction on the shared writer connection. Exposed so
|
||||
// the office service layer can make CreateCostEventTx atomic with another
|
||||
// package's write (e.g. task.Repository.IncrementTaskSessionUsageTx via
|
||||
// shared.SessionUsageWriterTx) — safe because both repositories are
|
||||
// constructed from the same underlying *sqlx.DB (internal/backendapp/storage.go),
|
||||
// so a transaction begun here can execute statements against tables either
|
||||
// package owns.
|
||||
func (r *Repository) BeginTx(ctx context.Context) (*sqlx.Tx, error) {
|
||||
return r.db.BeginTxx(ctx, nil)
|
||||
}
|
||||
|
||||
// initSchema creates all office tables if they don't exist.
|
||||
func (r *Repository) initSchema() error {
|
||||
if err := r.createCoreTables(); err != nil {
|
||||
@@ -311,13 +300,7 @@ func (r *Repository) createCostTables() error {
|
||||
CREATE INDEX IF NOT EXISTS idx_office_cost_agent ON office_cost_events(agent_profile_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_office_cost_occurred ON office_cost_events(occurred_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_office_cost_task ON office_cost_events(task_id);
|
||||
-- Indexes the join column internal/task/repository/sqlite's
|
||||
-- BackfillSessionTokensCachedIn correlates task_sessions against. Lives
|
||||
-- here (not in the task repository) because it indexes a table this
|
||||
-- repository owns. Without it that correlated subquery falls back to a
|
||||
-- full table scan per task_sessions row - measured at 76.72s at a modest
|
||||
-- 4,000 sessions / 80,000 events versus 0.17s indexed.
|
||||
CREATE INDEX IF NOT EXISTS idx_office_cost_events_session_id ON office_cost_events(session_id);
|
||||
DROP INDEX IF EXISTS idx_office_cost_events_session_id;
|
||||
-- uniq_office_cost_usage_event is created by migrateCostEventContract
|
||||
-- (base_migrations.go), not here: schema init runs before migrations,
|
||||
-- so indexing usage_event_id inline would crash a pre-migration
|
||||
|
||||
@@ -1,144 +0,0 @@
|
||||
package sqlite_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
||||
"github.com/kandev/kandev/internal/office/models"
|
||||
"github.com/kandev/kandev/internal/office/repository/sqlite"
|
||||
taskrepo "github.com/kandev/kandev/internal/task/repository/sqlite"
|
||||
)
|
||||
|
||||
// TestCostEventTxAtomicity_RealReposShareTransaction is the PR #2606 review
|
||||
// round 1 follow-up (F2): the only prior coverage of
|
||||
// recordCostEventAndRollup's shared-transaction path
|
||||
// (event_subscribers.go) used a test double that discarded its *sqlx.Tx
|
||||
// parameter, so neither session.go's `tx != nil` branch nor
|
||||
// CreateCostEventTx executing against a real transaction was ever exercised.
|
||||
//
|
||||
// This builds the real office and task repositories over one *sqlx.DB (the
|
||||
// NewWithDB(db, db, nil) pattern already used by
|
||||
// cost_event_contract_migration_test.go and workflow_test.go — safe because
|
||||
// office.Repository.BeginTx and task.Repository.IncrementTaskSessionUsageTx
|
||||
// are constructed from the same underlying writer connection in production,
|
||||
// see BeginTx's doc comment), then drives BeginTx -> CreateCostEventTx ->
|
||||
// IncrementTaskSessionUsageTx -> Rollback and asserts both office_cost_events
|
||||
// and task_sessions are unchanged, followed by a committed retry that
|
||||
// updates both.
|
||||
func TestCostEventTxAtomicity_RealReposShareTransaction(t *testing.T) {
|
||||
db, err := sqlx.Open("sqlite3", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
taskRepo, err := taskrepo.NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init task repo: %v", err)
|
||||
}
|
||||
officeRepo, err := sqlite.NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init office repo: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
now := time.Now().UTC()
|
||||
if _, err := db.ExecContext(ctx, `
|
||||
INSERT INTO workspaces (id, name, created_at, updated_at) VALUES ('ws-atomic', 'ws', ?, ?)
|
||||
`, now, now); err != nil {
|
||||
t.Fatalf("seed workspace: %v", err)
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, `
|
||||
INSERT INTO tasks (id, workspace_id, title, created_at, updated_at) VALUES ('task-atomic', 'ws-atomic', 't', ?, ?)
|
||||
`, now, now); err != nil {
|
||||
t.Fatalf("seed task: %v", err)
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, `
|
||||
INSERT INTO task_sessions (id, task_id, started_at, updated_at) VALUES ('session-atomic', 'task-atomic', ?, ?)
|
||||
`, now, now); err != nil {
|
||||
t.Fatalf("seed session: %v", err)
|
||||
}
|
||||
|
||||
countCostEvents := func() int {
|
||||
t.Helper()
|
||||
var n int
|
||||
if err := db.GetContext(ctx, &n, `SELECT COUNT(*) FROM office_cost_events`); err != nil {
|
||||
t.Fatalf("count cost events: %v", err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
rollupTotals := func() (tokensIn, tokensCachedIn, tokensOut, costSubcents int64) {
|
||||
t.Helper()
|
||||
row := db.QueryRowContext(ctx, `
|
||||
SELECT tokens_in, tokens_cached_in, tokens_out, cost_subcents
|
||||
FROM task_sessions WHERE id = 'session-atomic'
|
||||
`)
|
||||
if err := row.Scan(&tokensIn, &tokensCachedIn, &tokensOut, &costSubcents); err != nil {
|
||||
t.Fatalf("read rollup totals: %v", err)
|
||||
}
|
||||
return tokensIn, tokensCachedIn, tokensOut, costSubcents
|
||||
}
|
||||
buildEvent := func(usageEventID string) *models.CostEvent {
|
||||
return &models.CostEvent{
|
||||
SessionID: "session-atomic",
|
||||
TaskID: "task-atomic",
|
||||
CostSubcents: 500,
|
||||
UsageEventID: &usageEventID,
|
||||
OccurredAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
// Rollback: both writes land in the same transaction; rolling it back
|
||||
// must undo both, not just the one whose caller happened to fail.
|
||||
tx, err := officeRepo.BeginTx(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("begin tx: %v", err)
|
||||
}
|
||||
if err := officeRepo.CreateCostEventTx(ctx, tx, buildEvent("usage-evt-rollback")); err != nil {
|
||||
t.Fatalf("create cost event in tx: %v", err)
|
||||
}
|
||||
if err := taskRepo.IncrementTaskSessionUsageTx(ctx, tx, "session-atomic", 100, 40, 50, 500); err != nil {
|
||||
t.Fatalf("increment rollup in tx: %v", err)
|
||||
}
|
||||
if err := tx.Rollback(); err != nil {
|
||||
t.Fatalf("rollback: %v", err)
|
||||
}
|
||||
|
||||
if n := countCostEvents(); n != 0 {
|
||||
t.Fatalf("cost events after rollback = %d, want 0", n)
|
||||
}
|
||||
if tokensIn, tokensCachedIn, tokensOut, costSubcents := rollupTotals(); tokensIn != 0 || tokensCachedIn != 0 || tokensOut != 0 || costSubcents != 0 {
|
||||
t.Fatalf("rollup totals after rollback = (in=%d cached=%d out=%d cost=%d), want all zero",
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
|
||||
// Commit: a fresh transaction with the same shape commits both writes
|
||||
// together, proving the office-begun transaction can actually reach
|
||||
// task_sessions, a table the task package owns.
|
||||
tx2, err := officeRepo.BeginTx(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("begin tx (commit path): %v", err)
|
||||
}
|
||||
if err := officeRepo.CreateCostEventTx(ctx, tx2, buildEvent("usage-evt-commit")); err != nil {
|
||||
t.Fatalf("create cost event in tx (commit path): %v", err)
|
||||
}
|
||||
if err := taskRepo.IncrementTaskSessionUsageTx(ctx, tx2, "session-atomic", 100, 40, 50, 500); err != nil {
|
||||
t.Fatalf("increment rollup in tx (commit path): %v", err)
|
||||
}
|
||||
if err := tx2.Commit(); err != nil {
|
||||
t.Fatalf("commit: %v", err)
|
||||
}
|
||||
|
||||
if n := countCostEvents(); n != 1 {
|
||||
t.Fatalf("cost events after commit = %d, want 1", n)
|
||||
}
|
||||
if tokensIn, tokensCachedIn, tokensOut, costSubcents := rollupTotals(); tokensIn != 100 || tokensCachedIn != 40 || tokensOut != 50 || costSubcents != 500 {
|
||||
t.Fatalf("rollup totals after commit = (in=%d cached=%d out=%d cost=%d), want (100, 40, 50, 500)",
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,12 @@ package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/kandev/kandev/internal/office/models"
|
||||
)
|
||||
@@ -59,39 +57,19 @@ func isUsageEventUniqueViolation(err error) bool {
|
||||
// CreateCostEvent records a new cost event. A UsageEventID collision
|
||||
// (redelivery of the same prompt-usage event) is reported as
|
||||
// ErrDuplicateUsageEvent rather than the raw driver error, so callers can
|
||||
// treat it as an idempotent no-op.
|
||||
//
|
||||
// Delegates to CreateCostEventTx using r.db as the executor; a caller that
|
||||
// needs this atomic with another write (e.g. the office cost subscriber's
|
||||
// session-usage rollup) should call the Tx variant directly with a shared
|
||||
// transaction instead.
|
||||
// treat it as an idempotent no-op. Office's cost subscriber no longer
|
||||
// shares this insert with a task_sessions rollup write (that pairing now
|
||||
// happens entirely inside internal/task/usage's writer via its own
|
||||
// insertUsageEventAndRollup — docs/specs/task-cost-ledger/spec.md AC-10,
|
||||
// AC-21), so this always executes against r.db directly with no
|
||||
// transaction parameter to plumb through.
|
||||
func (r *Repository) CreateCostEvent(ctx context.Context, event *models.CostEvent) error {
|
||||
return r.CreateCostEventTx(ctx, nil, event)
|
||||
}
|
||||
|
||||
// CreateCostEventTx is CreateCostEvent's transactional twin: executes
|
||||
// against tx when non-nil (falling back to r.db, the shared writer
|
||||
// connection, when tx is nil) so a caller can make this atomic with another
|
||||
// write in the same transaction — see BeginTx and
|
||||
// shared.SessionUsageWriterTx's doc comment for why (docs/specs/office/costs.md,
|
||||
// PR #2606 review).
|
||||
func (r *Repository) CreateCostEventTx(ctx context.Context, tx *sqlx.Tx, event *models.CostEvent) error {
|
||||
if event.ID == "" {
|
||||
event.ID = uuid.New().String()
|
||||
}
|
||||
event.CreatedAt = time.Now().UTC()
|
||||
|
||||
var exec interface {
|
||||
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
|
||||
Rebind(query string) string
|
||||
}
|
||||
if tx != nil {
|
||||
exec = tx
|
||||
} else {
|
||||
exec = r.db
|
||||
}
|
||||
|
||||
_, err := exec.ExecContext(ctx, exec.Rebind(`
|
||||
_, err := r.db.ExecContext(ctx, r.db.Rebind(`
|
||||
INSERT INTO office_cost_events (
|
||||
id, session_id, task_id, agent_profile_id, project_id,
|
||||
model, provider, tokens_in, tokens_cached_in,
|
||||
|
||||
@@ -2,6 +2,7 @@ package sqlite_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -36,22 +37,15 @@ func seedTasksTable(t *testing.T, repo *sqlite.Repository, taskID, workspaceID s
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateCostTables_IndexesSessionID guards the boot-time cost of
|
||||
// internal/task/repository/sqlite's BackfillSessionTokensCachedIn, which
|
||||
// correlates task_sessions against office_cost_events on session_id. Without
|
||||
// this index that correlated subquery falls back to a full table scan per
|
||||
// task_sessions row (a measured ~450x slowdown at 4,000 sessions / 80,000
|
||||
// events - 76.72s vs 0.17s). The index lives here, not in the task
|
||||
// repository, because it indexes a table this repository owns.
|
||||
func TestCreateCostTables_IndexesSessionID(t *testing.T) {
|
||||
func TestCreateCostTables_DoesNotCreateUnusedSessionIDIndex(t *testing.T) {
|
||||
repo := newTestRepo(t)
|
||||
|
||||
var name string
|
||||
err := repo.ReaderDB().QueryRow(
|
||||
`SELECT name FROM sqlite_master WHERE type = 'index' AND tbl_name = 'office_cost_events' AND sql LIKE '%session_id%'`,
|
||||
).Scan(&name)
|
||||
if err != nil {
|
||||
t.Fatalf("expected an index on office_cost_events(session_id) to exist after repo init, got: %v", err)
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatalf("session_id index = %q, err=%v, want no index", name, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -98,11 +98,17 @@ func newTestService(t *testing.T, overrides ...service.ServiceOptions) *service.
|
||||
t.Fatalf("create workflow_step_participants: %v", err)
|
||||
}
|
||||
// Stub of task_sessions, mirroring the columns GetSessionAgentProfileID
|
||||
// (RunnerProjection's fallback source, see prompt_usage_cost.go) reads.
|
||||
// (RunnerProjection's fallback source, see prompt_usage_cost.go) reads,
|
||||
// plus the AC-10 rollup columns so tests can assert the Office
|
||||
// subscriber never writes them (AC-21).
|
||||
if _, err := db.Exec(`CREATE TABLE IF NOT EXISTS task_sessions (
|
||||
id TEXT PRIMARY KEY,
|
||||
task_id TEXT NOT NULL,
|
||||
agent_profile_id TEXT,
|
||||
tokens_in INTEGER NOT NULL DEFAULT 0,
|
||||
tokens_cached_in INTEGER NOT NULL DEFAULT 0,
|
||||
tokens_out INTEGER NOT NULL DEFAULT 0,
|
||||
cost_subcents INTEGER NOT NULL DEFAULT 0,
|
||||
started_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)`); err != nil {
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/kandev/kandev/internal/agent/runtime/routingerr"
|
||||
"github.com/kandev/kandev/internal/agentctl/types/streams"
|
||||
commoncosts "github.com/kandev/kandev/internal/common/costs"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
"github.com/kandev/kandev/internal/office/costs"
|
||||
@@ -701,14 +702,18 @@ func (s *Service) tryPostStartFallback(
|
||||
//
|
||||
// buildCostEvent (prompt_usage_cost.go) also records the cache read/write
|
||||
// split when the usage frame reports cache data, and turn_id /
|
||||
// usage_event_id threaded from the publish site. The ledger insert and the
|
||||
// task_sessions rollup increment (tokens_in / tokens_cached_in / tokens_out /
|
||||
// cost_subcents) are written atomically by recordCostEventAndRollup — see its
|
||||
// doc comment for why non-atomic writes here are a real defect, not a
|
||||
// theoretical one — and any applicable budget policy is evaluated afterward.
|
||||
// Estimated rows count toward budget totals at face value. Every early
|
||||
// return and the insert path record a writer-health metric (cost_metrics.go)
|
||||
// so a silently-stopped writer is observable.
|
||||
// usage_event_id threaded from the publish site. This function only inserts
|
||||
// the office_cost_events row; it no longer increments the task_sessions
|
||||
// rollup columns (docs/specs/task-cost-ledger/spec.md AC-21):
|
||||
// internal/task/usage's writer is now the sole writer of those columns
|
||||
// (AC-10), inserting task_usage_events and incrementing task_sessions
|
||||
// atomically in its own transaction (internal/task/repository/sqlite's
|
||||
// insertUsageEventAndRollup). Before this change, Office incremented the
|
||||
// same columns here too, so an Office-enabled install (the dev/e2e default)
|
||||
// double-counted every turn. Any applicable budget policy is evaluated
|
||||
// after the insert. Estimated rows count toward budget totals at face
|
||||
// value. Every early return and the insert path record a writer-health
|
||||
// metric (cost_metrics.go) so a silently-stopped writer is observable.
|
||||
func (s *Service) handlePromptUsage(ctx context.Context, event *bus.Event) error {
|
||||
data, err := decodeEventData[PromptUsageData](event)
|
||||
if err != nil {
|
||||
@@ -748,10 +753,7 @@ func (s *Service) handlePromptUsage(ctx context.Context, event *bus.Event) error
|
||||
*data, fields, s.projectIDForTask(ctx, data.TaskID), provider, resolution, sessionAgentProfileID,
|
||||
)
|
||||
|
||||
if err := s.recordCostEventAndRollup(
|
||||
ctx, costEvent, data.SessionID,
|
||||
data.Usage.InputTokens, costEvent.TokensCachedIn, data.Usage.OutputTokens, resolution.costSubcents,
|
||||
); err != nil {
|
||||
if err := s.repo.CreateCostEvent(ctx, costEvent); err != nil {
|
||||
if errors.Is(err, sqlite.ErrDuplicateUsageEvent) {
|
||||
s.recordCostEventDropped(costDropReasonDuplicate, data.TaskID)
|
||||
return nil
|
||||
@@ -772,61 +774,6 @@ func (s *Service) handlePromptUsage(ctx context.Context, event *bus.Event) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// recordCostEventAndRollup inserts costEvent and increments the
|
||||
// task_sessions rollup atomically when the wired sessionUsageWriter
|
||||
// supports it (shared.SessionUsageWriterTx) — every production wiring does
|
||||
// (backendapp's SetSessionUsageWriter(repos.Task)).
|
||||
//
|
||||
// Atomicity matters because of how this card's own usage_event_id
|
||||
// idempotency guard interacts with a partial failure: without a shared
|
||||
// transaction, a rollup-increment failure after a successful ledger insert
|
||||
// was logged and swallowed, and a later redelivery of the same completion
|
||||
// (see ErrDuplicateUsageEvent's doc comment for why redelivery is real, not
|
||||
// hypothetical) would hit the unique index and be dropped as a duplicate
|
||||
// before the rollup ever got a second chance — task_sessions permanently
|
||||
// behind office_cost_events with no recovery path (PR #2606 review). Wrapping
|
||||
// both writes in one transaction closes that: any failure, including the
|
||||
// rollup increment, rolls back the ledger insert too, so nothing is
|
||||
// committed and a redelivered completion with the same usage_event_id
|
||||
// retries the whole operation cleanly instead of colliding with a
|
||||
// half-landed row.
|
||||
//
|
||||
// Falls back to the pre-existing non-atomic two-step write when the wired
|
||||
// writer doesn't implement SessionUsageWriterTx (e.g. a simplified test
|
||||
// double that doesn't need transactional coupling, or sessionUsageWriter
|
||||
// left unset).
|
||||
func (s *Service) recordCostEventAndRollup(
|
||||
ctx context.Context, costEvent *models.CostEvent, sessionID string,
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents int64,
|
||||
) error {
|
||||
txWriter, ok := s.sessionUsageWriter.(shared.SessionUsageWriterTx)
|
||||
if !ok {
|
||||
if err := s.repo.CreateCostEvent(ctx, costEvent); err != nil {
|
||||
return err
|
||||
}
|
||||
s.incrementSessionUsageTotals(ctx, sessionID, tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
return nil
|
||||
}
|
||||
|
||||
tx, err := s.repo.BeginTx(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.repo.CreateCostEventTx(ctx, tx, costEvent); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if err := txWriter.IncrementTaskSessionUsageTx(
|
||||
ctx, tx, sessionID, tokensIn, tokensCachedIn, tokensOut, costSubcents,
|
||||
); err != nil {
|
||||
_ = tx.Rollback()
|
||||
s.logger.Warn("increment task_session usage failed, rolled back cost event insert",
|
||||
zap.String("session_id", sessionID), zap.Error(err))
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// resolveProvider derives the provider id for the cost row. AgentType
|
||||
// (the CLI engine slug — claude-acp, codex-acp, ...) is the most reliable
|
||||
// source; AgentID is checked as a fallback for legacy bus events that
|
||||
@@ -848,31 +795,11 @@ func resolveProvider(data PromptUsageData) string {
|
||||
|
||||
// providerFromCLI maps the upstream CLI id (the agent_id stream field)
|
||||
// to a provider name. Used because claude-acp emits logical model
|
||||
// aliases (sonnet / haiku) that can't be matched on prefix.
|
||||
// aliases (sonnet / haiku) that can't be matched on prefix. Delegates to
|
||||
// internal/common/costs, the single source of truth shared with the task
|
||||
// usage ledger writer (docs/specs/task-cost-ledger/spec.md).
|
||||
func providerFromCLI(cli string) string {
|
||||
switch cli {
|
||||
case "claude-acp":
|
||||
return "anthropic"
|
||||
case "codex-acp", "openai-acp":
|
||||
return "openai"
|
||||
case "gemini", "gemini-acp":
|
||||
return "google"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (s *Service) incrementSessionUsageTotals(
|
||||
ctx context.Context, sessionID string, tokensIn, tokensCachedIn, tokensOut, costSubcents int64,
|
||||
) {
|
||||
if s.sessionUsageWriter == nil || sessionID == "" {
|
||||
return
|
||||
}
|
||||
if err := s.sessionUsageWriter.IncrementTaskSessionUsage(
|
||||
ctx, sessionID, tokensIn, tokensCachedIn, tokensOut, costSubcents,
|
||||
); err != nil {
|
||||
s.logger.Warn("increment task_session usage failed",
|
||||
zap.String("session_id", sessionID), zap.Error(err))
|
||||
}
|
||||
return commoncosts.ProviderFromCLI(cli)
|
||||
}
|
||||
|
||||
func (s *Service) projectIDForTask(ctx context.Context, taskID string) string {
|
||||
|
||||
@@ -1,136 +0,0 @@
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
)
|
||||
|
||||
// errInjectedRollupFailure is returned by txSessionUsageWriter while armed,
|
||||
// standing in for a real rollup-side failure (e.g. a constraint violation or
|
||||
// a dropped connection) that happens after the cost-event insert has
|
||||
// already been staged in the same transaction.
|
||||
var errInjectedRollupFailure = errors.New("injected rollup failure")
|
||||
|
||||
// txSessionUsageWriter is a shared.SessionUsageWriterTx test double whose
|
||||
// Tx-aware increment can be armed to fail a fixed number of times before
|
||||
// succeeding. It exists to prove recordCostEventAndRollup
|
||||
// (event_subscribers.go) rolls the cost-event insert back when the rollup
|
||||
// increment fails inside the shared transaction, and that a redelivery of
|
||||
// the same usage_event_id afterward is not treated as a duplicate — the
|
||||
// recovery path the PR #2606 review asked to see covered, since neither
|
||||
// half of the write had actually committed.
|
||||
type txSessionUsageWriter struct {
|
||||
mu sync.Mutex
|
||||
failNextN int
|
||||
calls int
|
||||
|
||||
tokensIn int64
|
||||
tokensCachedIn int64
|
||||
tokensOut int64
|
||||
costSubcents int64
|
||||
}
|
||||
|
||||
func (w *txSessionUsageWriter) IncrementTaskSessionUsage(
|
||||
ctx context.Context, sessionID string, tokensIn, tokensCachedIn, tokensOut, costSubcents int64,
|
||||
) error {
|
||||
return w.IncrementTaskSessionUsageTx(ctx, nil, sessionID, tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
|
||||
func (w *txSessionUsageWriter) IncrementTaskSessionUsageTx(
|
||||
_ context.Context, _ *sqlx.Tx, _ string, tokensIn, tokensCachedIn, tokensOut, costSubcents int64,
|
||||
) error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
w.calls++
|
||||
if w.failNextN > 0 {
|
||||
w.failNextN--
|
||||
return errInjectedRollupFailure
|
||||
}
|
||||
w.tokensIn += tokensIn
|
||||
w.tokensCachedIn += tokensCachedIn
|
||||
w.tokensOut += tokensOut
|
||||
w.costSubcents += costSubcents
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestPromptUsage_RollupFailureRollsBackLedgerInsertThenRetrySucceeds covers
|
||||
// the recovery path the PR #2606 review specifically asked for: force the
|
||||
// rollup update to fail, then retry the same usage_event_id. Before
|
||||
// recordCostEventAndRollup wrapped both writes in one transaction, the
|
||||
// ledger insert committed regardless of the rollup outcome; a redelivery
|
||||
// carrying the same usage_event_id would then hit the unique index and be
|
||||
// dropped as a duplicate, leaving task_sessions permanently behind
|
||||
// office_cost_events with no way to catch up.
|
||||
func TestPromptUsage_RollupFailureRollsBackLedgerInsertThenRetrySucceeds(t *testing.T) {
|
||||
svc, eb := newTestServiceWithBus(t)
|
||||
ctx := context.Background()
|
||||
writer := &txSessionUsageWriter{failNextN: 1}
|
||||
svc.SetSessionUsageWriter(writer)
|
||||
|
||||
createTestAgent(t, svc, "ws-1", "worker-atomic")
|
||||
insertTestTask(t, svc, "task-atomic", "ws-1")
|
||||
setTestTaskAssignee(t, svc, "task-atomic", "worker-atomic")
|
||||
|
||||
buildEvent := func() *bus.Event {
|
||||
return bus.NewEvent(events.SessionPromptUsageUpdated, "test", map[string]interface{}{
|
||||
"task_id": "task-atomic",
|
||||
"session_id": "session-atomic",
|
||||
"agent_id": "claude-acp",
|
||||
"model": "default",
|
||||
"usage_event_id": "usage-evt-atomic-1",
|
||||
"usage": map[string]interface{}{
|
||||
"input_tokens": 100,
|
||||
"output_tokens": 50,
|
||||
},
|
||||
})
|
||||
}
|
||||
subject := events.BuildSessionPromptUsageSubject("session-atomic")
|
||||
|
||||
// First delivery: the rollup increment is armed to fail. If the ledger
|
||||
// insert and the rollup increment were not atomic, the insert would
|
||||
// already be committed here.
|
||||
if err := eb.Publish(ctx, subject, buildEvent()); err != nil {
|
||||
t.Fatalf("publish (failing rollup): %v", err)
|
||||
}
|
||||
|
||||
costs, err := svc.ListCostEvents(ctx, "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("list costs after failed rollup: %v", err)
|
||||
}
|
||||
if len(costs) != 0 {
|
||||
t.Fatalf("cost events after failed rollup = %d, want 0 (ledger insert must roll back with the failed rollup)",
|
||||
len(costs))
|
||||
}
|
||||
if writer.tokensIn != 0 || writer.tokensOut != 0 {
|
||||
t.Fatalf("rollup totals after failed attempt = (in=%d out=%d), want (0, 0): a failed increment must not partially apply",
|
||||
writer.tokensIn, writer.tokensOut)
|
||||
}
|
||||
|
||||
// Redelivery of the same usage_event_id, this time with the rollup
|
||||
// writer succeeding. Because nothing committed on the first attempt,
|
||||
// the unique index must not treat this as a duplicate.
|
||||
if err := eb.Publish(ctx, subject, buildEvent()); err != nil {
|
||||
t.Fatalf("publish (retry): %v", err)
|
||||
}
|
||||
|
||||
costs, err = svc.ListCostEvents(ctx, "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("list costs after retry: %v", err)
|
||||
}
|
||||
if len(costs) != 1 {
|
||||
t.Fatalf("cost events after retry = %d, want 1", len(costs))
|
||||
}
|
||||
if writer.tokensIn != 100 || writer.tokensOut != 50 {
|
||||
t.Errorf("rollup totals after retry = (in=%d out=%d), want (in=100 out=50)", writer.tokensIn, writer.tokensOut)
|
||||
}
|
||||
if writer.calls != 2 {
|
||||
t.Errorf("rollup writer called %d times, want 2 (one failed attempt, one successful retry)", writer.calls)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package service_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
@@ -322,6 +323,41 @@ func TestPromptUsage_CostSourceModelsDevList(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPromptUsage_PricingOverflowFallsBackToUnpriced(t *testing.T) {
|
||||
svc, eb := newTestServiceWithBus(t)
|
||||
ctx := context.Background()
|
||||
svc.SetPricingLookup(&fakePricingLookup{
|
||||
hit: true,
|
||||
pricing: shared.ModelPricing{InputPerMillion: 3},
|
||||
})
|
||||
|
||||
createTestAgent(t, svc, "ws-1", "worker-overflow")
|
||||
insertTestTask(t, svc, "task-overflow", "ws-1")
|
||||
setTestTaskAssignee(t, svc, "task-overflow", "worker-overflow")
|
||||
|
||||
event := bus.NewEvent(events.SessionPromptUsageUpdated, "test", map[string]interface{}{
|
||||
"task_id": "task-overflow",
|
||||
"session_id": "session-overflow",
|
||||
"agent_id": "claude-acp",
|
||||
"model": "sonnet",
|
||||
"usage": map[string]interface{}{
|
||||
"input_tokens": math.MaxInt64/2 + 1,
|
||||
},
|
||||
})
|
||||
if err := eb.Publish(ctx, events.BuildSessionPromptUsageSubject("session-overflow"), event); err != nil {
|
||||
t.Fatalf("publish: %v", err)
|
||||
}
|
||||
|
||||
costs, err := svc.ListCostEvents(ctx, "ws-1")
|
||||
if err != nil || len(costs) != 1 {
|
||||
t.Fatalf("list costs: %v (len=%d)", err, len(costs))
|
||||
}
|
||||
row := costs[0]
|
||||
if row.CostSource == nil || *row.CostSource != models.CostSourceUnpriced || row.CostSubcents != 0 {
|
||||
t.Fatalf("cost = (%v, %d), want (unpriced, 0)", row.CostSource, row.CostSubcents)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPromptUsage_ThoughtTokensDoNotAffectCost is a regression test for the
|
||||
// triage correction on the card: reasoning_output_tokens is a SUBSET of
|
||||
// output_tokens (OpenAI's own accounting: last_total = last_in + last_out
|
||||
@@ -789,6 +825,50 @@ func TestPromptUsage_DuplicateUsageEventIDIsIdempotent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPromptUsage_DoesNotWriteTaskSessionsRollup pins AC-21: the Office cost
|
||||
// subscriber inserts its own office_cost_events row but SHALL NOT touch
|
||||
// task_sessions' tokens_in/tokens_cached_in/tokens_out/cost_subcents rollup
|
||||
// columns — internal/task/usage's writer is the sole writer of those columns
|
||||
// (AC-10). The session row is seeded with distinctive, nonzero rollup values
|
||||
// before the event is published; if handlePromptUsage still incremented (or
|
||||
// otherwise touched) those columns, they would change.
|
||||
func TestPromptUsage_DoesNotWriteTaskSessionsRollup(t *testing.T) {
|
||||
svc, eb := newTestServiceWithBus(t)
|
||||
ctx := context.Background()
|
||||
|
||||
createTestAgent(t, svc, "ws-1", "worker-rollup")
|
||||
insertTestTask(t, svc, "task-rollup", "ws-1")
|
||||
setTestTaskAssignee(t, svc, "task-rollup", "worker-rollup")
|
||||
insertTestTaskSession(t, svc, "session-rollup", "task-rollup", "worker-rollup")
|
||||
svc.ExecSQL(t,
|
||||
`UPDATE task_sessions SET tokens_in = ?, tokens_cached_in = ?, tokens_out = ?, cost_subcents = ? WHERE id = ?`,
|
||||
111, 22, 33, 44, "session-rollup")
|
||||
|
||||
event := bus.NewEvent(events.SessionPromptUsageUpdated, "test", map[string]interface{}{
|
||||
"task_id": "task-rollup",
|
||||
"session_id": "session-rollup",
|
||||
"model": "sonnet",
|
||||
"usage": map[string]interface{}{
|
||||
"input_tokens": 100,
|
||||
"output_tokens": 30,
|
||||
},
|
||||
})
|
||||
if err := eb.Publish(ctx, events.BuildSessionPromptUsageSubject("session-rollup"), event); err != nil {
|
||||
t.Fatalf("publish: %v", err)
|
||||
}
|
||||
|
||||
costs, err := svc.ListCostEvents(ctx, "ws-1")
|
||||
if err != nil || len(costs) != 1 {
|
||||
t.Fatalf("list costs: %v (len=%d) - the handler must still have run and inserted its own row", err, len(costs))
|
||||
}
|
||||
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := svc.GetTaskSessionRollupForTest(t, "session-rollup")
|
||||
if tokensIn != 111 || tokensCachedIn != 22 || tokensOut != 33 || costSubcents != 44 {
|
||||
t.Errorf("task_sessions rollup = (%d,%d,%d,%d), want unchanged (111,22,33,44) - Office must not write these columns",
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPromptUsage_BudgetCheckUsesSessionFallbackProfile is a regression test
|
||||
// for the companion budget-check gap: buildCostEvent already resolves the
|
||||
// session fallback into costEvent.AgentProfileID (see
|
||||
|
||||
@@ -49,7 +49,7 @@ func (s *Service) resolveCostForUsage(ctx context.Context, data PromptUsageData)
|
||||
if !ok {
|
||||
return costResolution{estimated: data.Usage.Estimated, source: models.CostSourceUnpriced}
|
||||
}
|
||||
cost := costs.CalculateCostSubcents(
|
||||
cost, ok := costs.CalculateCostSubcentsChecked(
|
||||
data.Usage.InputTokens,
|
||||
data.Usage.CachedReadTokens,
|
||||
data.Usage.CachedWriteTokens,
|
||||
@@ -61,6 +61,9 @@ func (s *Service) resolveCostForUsage(ctx context.Context, data PromptUsageData)
|
||||
OutputPerMillion: pricing.OutputPerMillion,
|
||||
},
|
||||
)
|
||||
if !ok {
|
||||
return costResolution{estimated: data.Usage.Estimated, source: models.CostSourceUnpriced}
|
||||
}
|
||||
return costResolution{
|
||||
costSubcents: cost,
|
||||
estimated: data.Usage.Estimated,
|
||||
|
||||
@@ -256,10 +256,6 @@ type Service struct {
|
||||
// misses and rows get cost_subcents=0 + estimated=true.
|
||||
pricingLookup shared.PricingLookup
|
||||
|
||||
// sessionUsageWriter accumulates tokens / cost onto task_sessions
|
||||
// when a cost event lands. Optional in tests.
|
||||
sessionUsageWriter shared.SessionUsageWriter
|
||||
|
||||
// budgetChecker evaluates budget policies. Wired to the
|
||||
// costs.CostService at startup; nil in tests that don't exercise
|
||||
// the budget pathways. Both CheckBudget and CheckPreExecutionBudget
|
||||
@@ -287,9 +283,6 @@ func (s *Service) SetBudgetChecker(b BudgetEvaluator) { s.budgetChecker = b }
|
||||
// SetPricingLookup wires the models.dev pricing lookup.
|
||||
func (s *Service) SetPricingLookup(p shared.PricingLookup) { s.pricingLookup = p }
|
||||
|
||||
// SetSessionUsageWriter wires the task-session usage incrementer.
|
||||
func (s *Service) SetSessionUsageWriter(w shared.SessionUsageWriter) { s.sessionUsageWriter = w }
|
||||
|
||||
// SetAgentTokenMinter wires the runtime token minter after feature services are constructed.
|
||||
func (s *Service) SetAgentTokenMinter(minter AgentTokenMinter) {
|
||||
s.agentTokenMinter = minter
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
)
|
||||
|
||||
// fakeSessionUsageWriter accumulates the deltas handlePromptUsage sends to
|
||||
// the task-session rollup, mirroring what the real SQLite repo's
|
||||
// IncrementTaskSessionUsage does with UPDATE ... SET x = x + ?.
|
||||
type fakeSessionUsageWriter struct {
|
||||
tokensIn int64
|
||||
tokensCachedIn int64
|
||||
tokensOut int64
|
||||
costSubcents int64
|
||||
}
|
||||
|
||||
func (f *fakeSessionUsageWriter) IncrementTaskSessionUsage(
|
||||
_ context.Context, _ string, tokensIn, tokensCachedIn, tokensOut, costSubcents int64,
|
||||
) error {
|
||||
f.tokensIn += tokensIn
|
||||
f.tokensCachedIn += tokensCachedIn
|
||||
f.tokensOut += tokensOut
|
||||
f.costSubcents += costSubcents
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestPromptUsage_RollupReconcilesWithCostLedger asserts the actual
|
||||
// invariant this rollup exists to maintain: task_sessions' cumulative
|
||||
// totals must equal the sum of the office_cost_events rows they were
|
||||
// derived from. Before the fix, tokensCachedIn silently never reaches the
|
||||
// writer even though every event row carries it, which is exactly what let
|
||||
// task_sessions.tokens_in under-report cache-heavy sessions by orders of
|
||||
// magnitude in production.
|
||||
func TestPromptUsage_RollupReconcilesWithCostLedger(t *testing.T) {
|
||||
svc, eb := newTestServiceWithBus(t)
|
||||
ctx := context.Background()
|
||||
writer := &fakeSessionUsageWriter{}
|
||||
svc.SetSessionUsageWriter(writer)
|
||||
|
||||
createTestAgent(t, svc, "ws-1", "worker-reconcile")
|
||||
svc.ExecSQL(t, `INSERT INTO tasks (
|
||||
id, workspace_id, project_id, title, created_at, updated_at
|
||||
) VALUES (
|
||||
'task-reconcile', 'ws-1', 'project-1', 'Reconcile task', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
|
||||
)`)
|
||||
setTestTaskAssignee(t, svc, "task-reconcile", "worker-reconcile")
|
||||
|
||||
const sessionID = "session-reconcile-1"
|
||||
|
||||
// Cache-heavy shape mirroring the reported bug: cached read tokens
|
||||
// dominate input tokens by several orders of magnitude.
|
||||
usages := []map[string]interface{}{
|
||||
{
|
||||
"input_tokens": 100,
|
||||
"cached_read_tokens": 50_000_000,
|
||||
"cached_write_tokens": 1_000_000,
|
||||
"output_tokens": 200,
|
||||
},
|
||||
{
|
||||
"input_tokens": 10,
|
||||
"cached_read_tokens": 2_000,
|
||||
"cached_write_tokens": 0,
|
||||
"output_tokens": 5,
|
||||
},
|
||||
}
|
||||
for _, usage := range usages {
|
||||
event := bus.NewEvent(events.SessionPromptUsageUpdated, "test", map[string]interface{}{
|
||||
"task_id": "task-reconcile",
|
||||
"session_id": sessionID,
|
||||
"agent_id": "claude-acp",
|
||||
"model": "default",
|
||||
"usage": usage,
|
||||
})
|
||||
if err := eb.Publish(ctx, events.BuildSessionPromptUsageSubject(sessionID), event); err != nil {
|
||||
t.Fatalf("publish prompt usage: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
costs, err := svc.ListCostEvents(ctx, "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("list costs: %v", err)
|
||||
}
|
||||
|
||||
var wantIn, wantCachedIn, wantOut, wantCost int64
|
||||
var ledgerRows int
|
||||
for _, c := range costs {
|
||||
if c.SessionID != sessionID {
|
||||
continue
|
||||
}
|
||||
ledgerRows++
|
||||
wantIn += c.TokensIn
|
||||
wantCachedIn += c.TokensCachedIn
|
||||
if c.TokensOut != nil {
|
||||
wantOut += *c.TokensOut
|
||||
}
|
||||
wantCost += c.CostSubcents
|
||||
}
|
||||
if ledgerRows != len(usages) {
|
||||
t.Fatalf("ledger rows for session = %d, want %d", ledgerRows, len(usages))
|
||||
}
|
||||
if wantCachedIn == 0 {
|
||||
t.Fatal("test setup bug: ledger recorded zero cached tokens, reconciliation would be vacuous")
|
||||
}
|
||||
|
||||
if writer.tokensIn != wantIn {
|
||||
t.Errorf("rollup tokens_in = %d, want %d (sum of office_cost_events.tokens_in)", writer.tokensIn, wantIn)
|
||||
}
|
||||
if writer.tokensCachedIn != wantCachedIn {
|
||||
t.Errorf("rollup tokens_cached_in = %d, want %d (sum of office_cost_events.tokens_cached_in)",
|
||||
writer.tokensCachedIn, wantCachedIn)
|
||||
}
|
||||
if writer.tokensOut != wantOut {
|
||||
t.Errorf("rollup tokens_out = %d, want %d (sum of office_cost_events.tokens_out)", writer.tokensOut, wantOut)
|
||||
}
|
||||
if writer.costSubcents != wantCost {
|
||||
t.Errorf("rollup cost_subcents = %d, want %d (sum of office_cost_events.cost_subcents)", writer.costSubcents, wantCost)
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,20 @@ func (s *Service) ExecSQL(t *testing.T, query string, args ...interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// GetTaskSessionRollupForTest reads task_sessions' AC-10 rollup columns
|
||||
// directly, so tests can assert the Office cost subscriber never writes them
|
||||
// (docs/specs/task-cost-ledger/spec.md AC-21).
|
||||
func (s *Service) GetTaskSessionRollupForTest(t *testing.T, sessionID string) (tokensIn, tokensCachedIn, tokensOut, costSubcents int64) {
|
||||
t.Helper()
|
||||
if err := s.repo.ReaderDB().QueryRowx(
|
||||
`SELECT tokens_in, tokens_cached_in, tokens_out, cost_subcents FROM task_sessions WHERE id = ?`,
|
||||
sessionID,
|
||||
).Scan(&tokensIn, &tokensCachedIn, &tokensOut, &costSubcents); err != nil {
|
||||
t.Fatalf("read task_sessions rollup: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetWorkspaceGroupForTest exposes workspace-group rows for deletion-order tests.
|
||||
func (s *Service) GetWorkspaceGroupForTest(ctx context.Context, id string) (*models.WorkspaceGroup, error) {
|
||||
return s.repo.GetWorkspaceGroup(ctx, id)
|
||||
|
||||
@@ -5,8 +5,6 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/kandev/kandev/internal/office/models"
|
||||
"github.com/kandev/kandev/internal/workflow/engine"
|
||||
)
|
||||
@@ -184,29 +182,3 @@ type PricingLookupWithVersion interface {
|
||||
// catalogue states.
|
||||
LookupForModelWithVersion(ctx context.Context, modelID string) (pricing ModelPricing, version string, ok bool)
|
||||
}
|
||||
|
||||
// SessionUsageWriter increments the cumulative tokens/cost columns on
|
||||
// task_sessions when a cost event lands. Implemented by the task repo.
|
||||
type SessionUsageWriter interface {
|
||||
IncrementTaskSessionUsage(ctx context.Context, sessionID string,
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents int64) error
|
||||
}
|
||||
|
||||
// SessionUsageWriterTx is an optional capability a SessionUsageWriter
|
||||
// implementation may satisfy to run the increment inside a caller-supplied
|
||||
// transaction, so it can be made atomic with the office_cost_events insert
|
||||
// that produced the deltas (docs/specs/office/costs.md, PR #2606 review):
|
||||
// without this, a rollup-increment failure after a successful cost-event
|
||||
// insert is logged and swallowed, and any later redelivery of the same
|
||||
// completion is caught by the usage_event_id unique index and dropped as a
|
||||
// duplicate before the rollup gets another chance — a permanent desync with
|
||||
// no recovery path. Callers type-assert and fall back to the plain
|
||||
// (non-atomic) SessionUsageWriter method when unavailable — e.g. a
|
||||
// simplified test double that doesn't need transactional coupling. Every
|
||||
// production wiring (backendapp's SetSessionUsageWriter(repos.Task))
|
||||
// satisfies it, since office_cost_events and task_sessions share one
|
||||
// underlying *sqlx.DB (see internal/backendapp/storage.go).
|
||||
type SessionUsageWriterTx interface {
|
||||
IncrementTaskSessionUsageTx(ctx context.Context, tx *sqlx.Tx, sessionID string,
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents int64) error
|
||||
}
|
||||
|
||||
@@ -1785,6 +1785,58 @@ func TestPublishPromptUsage_RepublishedCompletionReusesUsageEventID(t *testing.T
|
||||
"republishing the same completion must reuse the same usage_event_id so the DB unique index catches the duplicate")
|
||||
}
|
||||
|
||||
// TestPublishPromptUsage_RepublishedCompletionWithoutPromptGenerationMintsDistinctUsageEventIDs
|
||||
// is the integration-level counterpart to TestUsageEventIDFor's unit-level
|
||||
// promptGeneration==0 assertion, and the random-identifier-class counterpart
|
||||
// to TestPublishPromptUsage_RepublishedCompletionReusesUsageEventID's
|
||||
// deterministic-class coverage (docs/specs/task-cost-ledger/spec.md AC-22).
|
||||
// A generation-less transport's "same" redelivered completion frame mints a
|
||||
// fresh random usage_event_id on every publish, so the two publishes carry
|
||||
// DIFFERENT ids: office/costs already documents that those rows are
|
||||
// intentionally not deduplicated, and this proves that end to end rather
|
||||
// than just at usageEventIDFor's own return value.
|
||||
func TestPublishPromptUsage_RepublishedCompletionWithoutPromptGenerationMintsDistinctUsageEventIDs(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := setupTestRepo(t)
|
||||
seedSession(t, repo, "t1", "s1", "")
|
||||
|
||||
svc := createTestService(repo, newMockStepGetter(), newMockTaskRepo())
|
||||
svc.turnService = &repoTurnService{repo: repo}
|
||||
eb := &recordingEventBus{}
|
||||
svc.eventBus = eb
|
||||
|
||||
_, err := svc.turnService.StartTurn(ctx, "s1")
|
||||
require.NoError(t, err)
|
||||
svc.markExecutionCompleted("s1", "exec-1")
|
||||
svc.completeTurnForSession(ctx, "s1")
|
||||
|
||||
frame := func() *lifecycle.AgentStreamEventPayload {
|
||||
return &lifecycle.AgentStreamEventPayload{
|
||||
TaskID: "t1",
|
||||
SessionID: "s1",
|
||||
ExecutionID: "exec-1",
|
||||
Data: &lifecycle.AgentStreamEventData{
|
||||
Type: agentEventComplete,
|
||||
PromptGeneration: 0,
|
||||
Usage: &streams.PromptUsage{InputTokens: 10, OutputTokens: 5},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Simulates the same buffered stream frame being delivered twice on a
|
||||
// generation-less transport - same session and execution, but no
|
||||
// prompt-generation tracking either time.
|
||||
svc.handleAgentStreamEvent(ctx, frame())
|
||||
svc.handleAgentStreamEvent(ctx, frame())
|
||||
|
||||
published := findAllPromptUsageEvents(t, eb, "s1")
|
||||
require.Len(t, published, 2, "expected both republished frames to publish a prompt-usage event")
|
||||
require.NotEmpty(t, published[0].UsageEventID)
|
||||
require.NotEmpty(t, published[1].UsageEventID)
|
||||
require.NotEqual(t, published[0].UsageEventID, published[1].UsageEventID,
|
||||
"a generation-less transport must mint a fresh id per publish, so this redelivery is recorded as a new row and its rollup applied - not deduplicated")
|
||||
}
|
||||
|
||||
func TestCompleteStreamFromCompletedExecutionSkipsDuplicateOfficeTeardown(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
repo := setupTestRepo(t)
|
||||
|
||||
@@ -155,6 +155,55 @@ func TestFromTaskSession_IncludesAllWorktrees(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestFromTaskSession_CopiesRollupColumns pins docs/specs/task-cost-ledger/
|
||||
// spec.md AC-28/AC-29: TaskSessionDTO (the full session detail shape) must
|
||||
// surface the four usage/cost rollup columns internal/task/usage's writer
|
||||
// maintains on task_sessions.
|
||||
func TestFromTaskSession_CopiesRollupColumns(t *testing.T) {
|
||||
session := &models.TaskSession{
|
||||
ID: "session-1",
|
||||
TaskID: "task-1",
|
||||
CostSubcents: 79118,
|
||||
TokensIn: 80,
|
||||
TokensCachedIn: 8203943,
|
||||
TokensOut: 44979,
|
||||
}
|
||||
|
||||
got := FromTaskSession(session)
|
||||
if got.CostSubcents != 79118 || got.TokensIn != 80 || got.TokensCachedIn != 8203943 || got.TokensOut != 44979 {
|
||||
t.Fatalf("got rollup fields = %+v, want copied verbatim from session", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFromTaskSessionSummary_ExcludesRollupColumns pins the deliberate scope
|
||||
// decision (task-cost-ledger plan #11): the cross-task summary projection is
|
||||
// NOT widened by this card, only the full TaskSessionDTO is.
|
||||
func TestFromTaskSessionSummary_ExcludesRollupColumns(t *testing.T) {
|
||||
session := &models.TaskSession{
|
||||
ID: "session-1",
|
||||
TaskID: "task-1",
|
||||
CostSubcents: 79118,
|
||||
TokensIn: 80,
|
||||
TokensCachedIn: 8203943,
|
||||
TokensOut: 44979,
|
||||
}
|
||||
|
||||
got := FromTaskSessionSummary(session)
|
||||
raw, err := json.Marshal(got)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
var decoded map[string]interface{}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
for _, key := range []string{"cost_subcents", "tokens_in", "tokens_cached_in", "tokens_out"} {
|
||||
if _, ok := decoded[key]; ok {
|
||||
t.Errorf("TaskSessionSummaryDTO JSON contains %q, want it absent", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromTask_DerivesInterruptedFromMetadata(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
|
||||
|
||||
@@ -356,6 +356,13 @@ type TaskSessionDTO struct {
|
||||
// the newest message the frontend has marked as read. Used by the
|
||||
// transcript to position the unread ("New") divider.
|
||||
LastReadMessageID string `json:"last_read_message_id,omitempty"`
|
||||
// Usage/cost rollup (docs/specs/task-cost-ledger/spec.md AC-28, AC-29).
|
||||
// Deliberately not on TaskSessionSummaryDTO - the summary projection used
|
||||
// by cross-task views is not widened by this card.
|
||||
CostSubcents int64 `json:"cost_subcents"`
|
||||
TokensIn int64 `json:"tokens_in"`
|
||||
TokensCachedIn int64 `json:"tokens_cached_in"`
|
||||
TokensOut int64 `json:"tokens_out"`
|
||||
}
|
||||
|
||||
// TaskSessionSummaryDTO is a lightweight version of TaskSessionDTO without snapshot fields.
|
||||
@@ -923,6 +930,10 @@ func FromTaskSession(session *models.TaskSession) TaskSessionDTO {
|
||||
ReviewStatus: session.ReviewStatus,
|
||||
TaskEnvironmentID: session.TaskEnvironmentID,
|
||||
LastReadMessageID: session.LastReadMessageID,
|
||||
CostSubcents: session.CostSubcents,
|
||||
TokensIn: session.TokensIn,
|
||||
TokensCachedIn: session.TokensCachedIn,
|
||||
TokensOut: session.TokensOut,
|
||||
}
|
||||
if worktrees := session.WorktreesAPI(); len(worktrees) > 0 {
|
||||
result.WorktreeID = session.Worktrees[0].WorktreeID
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
// TaskUsageTotalsScope identifies whether a TaskUsageTotalsDTO aggregates an
|
||||
// entire task or a single session within it (docs/specs/task-cost-ledger/
|
||||
// spec.md API surface).
|
||||
type TaskUsageTotalsScope string
|
||||
|
||||
const (
|
||||
TaskUsageTotalsScopeTask TaskUsageTotalsScope = "task"
|
||||
TaskUsageTotalsScopeSession TaskUsageTotalsScope = "session"
|
||||
)
|
||||
|
||||
// TaskUsageTotalsDTO is the exact JSON shape both usage-totals HTTP routes
|
||||
// return (AC-18, AC-19, AC-20). FirstEventAt/LastEventAt serialize to JSON
|
||||
// null, never omitted, when the scope has no contributing rows.
|
||||
type TaskUsageTotalsDTO struct {
|
||||
Scope TaskUsageTotalsScope `json:"scope"`
|
||||
ScopeID string `json:"scope_id"`
|
||||
TokensIn int64 `json:"tokens_in"`
|
||||
TokensCachedRead int64 `json:"tokens_cached_read"`
|
||||
TokensCachedWrite int64 `json:"tokens_cached_write"`
|
||||
TokensOut int64 `json:"tokens_out"`
|
||||
TokensThought int64 `json:"tokens_thought"`
|
||||
TokensTotal int64 `json:"tokens_total"`
|
||||
CostSubcents int64 `json:"cost_subcents"`
|
||||
EventCount int64 `json:"event_count"`
|
||||
EstimatedEventCount int64 `json:"estimated_event_count"`
|
||||
UnpricedEventCount int64 `json:"unpriced_event_count"`
|
||||
OutputTokensComplete bool `json:"output_tokens_complete"`
|
||||
FirstEventAt *time.Time `json:"first_event_at"`
|
||||
LastEventAt *time.Time `json:"last_event_at"`
|
||||
}
|
||||
|
||||
// ToTaskUsageTotalsDTO combines a repository-layer aggregate with the scope
|
||||
// and scope ID the HTTP route resolved it for.
|
||||
func ToTaskUsageTotalsDTO(scope TaskUsageTotalsScope, scopeID string, totals *models.TaskUsageTotals) TaskUsageTotalsDTO {
|
||||
return TaskUsageTotalsDTO{
|
||||
Scope: scope,
|
||||
ScopeID: scopeID,
|
||||
TokensIn: totals.TokensIn,
|
||||
TokensCachedRead: totals.TokensCachedRead,
|
||||
TokensCachedWrite: totals.TokensCachedWrite,
|
||||
TokensOut: totals.TokensOut,
|
||||
TokensThought: totals.TokensThought,
|
||||
TokensTotal: totals.TokensTotal,
|
||||
CostSubcents: totals.CostSubcents,
|
||||
EventCount: totals.EventCount,
|
||||
EstimatedEventCount: totals.EstimatedEventCount,
|
||||
UnpricedEventCount: totals.UnpricedEventCount,
|
||||
OutputTokensComplete: totals.OutputTokensComplete,
|
||||
FirstEventAt: totals.FirstEventAt,
|
||||
LastEventAt: totals.LastEventAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
func TestToTaskUsageTotalsDTO_ZeroUsage_NullTimestamps(t *testing.T) {
|
||||
totals := &models.TaskUsageTotals{OutputTokensComplete: true}
|
||||
|
||||
got := ToTaskUsageTotalsDTO(TaskUsageTotalsScopeTask, "task-1", totals)
|
||||
if got.Scope != TaskUsageTotalsScopeTask || got.ScopeID != "task-1" {
|
||||
t.Fatalf("scope = (%q, %q), want (task, task-1)", got.Scope, got.ScopeID)
|
||||
}
|
||||
if got.EventCount != 0 || got.TokensTotal != 0 {
|
||||
t.Fatalf("got = %+v, want every sum zero", got)
|
||||
}
|
||||
if !got.OutputTokensComplete {
|
||||
t.Error("OutputTokensComplete = false, want true")
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(got)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
var decoded map[string]interface{}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
firstEventAt, ok := decoded["first_event_at"]
|
||||
if !ok {
|
||||
t.Fatal("first_event_at must be present in the JSON body, not omitted")
|
||||
}
|
||||
if firstEventAt != nil {
|
||||
t.Errorf("first_event_at = %v, want JSON null", firstEventAt)
|
||||
}
|
||||
lastEventAt, ok := decoded["last_event_at"]
|
||||
if !ok {
|
||||
t.Fatal("last_event_at must be present in the JSON body, not omitted")
|
||||
}
|
||||
if lastEventAt != nil {
|
||||
t.Errorf("last_event_at = %v, want JSON null", lastEventAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToTaskUsageTotalsDTO_SessionScope_CopiesEveryField(t *testing.T) {
|
||||
first := time.Date(2026, 8, 23, 4, 0, 0, 0, time.UTC)
|
||||
last := first.Add(10 * time.Minute)
|
||||
totals := &models.TaskUsageTotals{
|
||||
TokensIn: 80,
|
||||
TokensCachedRead: 8203943,
|
||||
TokensCachedWrite: 264177,
|
||||
TokensOut: 44979,
|
||||
TokensThought: 0,
|
||||
TokensTotal: 8513179,
|
||||
CostSubcents: 79118,
|
||||
EventCount: 1,
|
||||
EstimatedEventCount: 0,
|
||||
UnpricedEventCount: 0,
|
||||
OutputTokensComplete: true,
|
||||
FirstEventAt: &first,
|
||||
LastEventAt: &last,
|
||||
}
|
||||
|
||||
got := ToTaskUsageTotalsDTO(TaskUsageTotalsScopeSession, "sess-1", totals)
|
||||
if got.Scope != TaskUsageTotalsScopeSession || got.ScopeID != "sess-1" {
|
||||
t.Fatalf("scope = (%q, %q), want (session, sess-1)", got.Scope, got.ScopeID)
|
||||
}
|
||||
if got.TokensCachedRead != 8203943 || got.TokensTotal != 8513179 || got.CostSubcents != 79118 {
|
||||
t.Fatalf("got = %+v, want fields copied verbatim from totals", got)
|
||||
}
|
||||
if got.FirstEventAt == nil || !got.FirstEventAt.Equal(first) {
|
||||
t.Errorf("FirstEventAt = %v, want %v", got.FirstEventAt, first)
|
||||
}
|
||||
if got.LastEventAt == nil || !got.LastEventAt.Equal(last) {
|
||||
t.Errorf("LastEventAt = %v, want %v", got.LastEventAt, last)
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(got)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal: %v", err)
|
||||
}
|
||||
var decoded map[string]interface{}
|
||||
if err := json.Unmarshal(raw, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if decoded["first_event_at"] != "2026-08-23T04:00:00Z" {
|
||||
t.Errorf("first_event_at = %v, want 2026-08-23T04:00:00Z", decoded["first_event_at"])
|
||||
}
|
||||
}
|
||||
@@ -166,6 +166,11 @@ func (h *TaskHandlers) registerHTTP(router *gin.Engine) {
|
||||
api.POST("/tasks/:id/unarchive", h.httpUnarchiveTask)
|
||||
api.GET("/tasks/:id/subtask-count", h.httpTaskSubtaskCount)
|
||||
|
||||
// Task-cost-ledger read surface (docs/specs/task-cost-ledger/spec.md
|
||||
// AC-18): per-task and per-session usage/cost totals.
|
||||
api.GET("/tasks/:id/usage", h.httpGetTaskUsageTotals)
|
||||
api.GET("/tasks/:id/sessions/:sessionId/usage", h.httpGetTaskSessionUsageTotals)
|
||||
|
||||
// Task dependencies ("this task is blocked by that one"). Task-scoped
|
||||
// equivalents of the Office-only blocker routes; both go through the single
|
||||
// validator in the task service.
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/dto"
|
||||
)
|
||||
|
||||
// httpGetTaskUsageTotals serves GET /api/v1/tasks/:id/usage
|
||||
// (docs/specs/task-cost-ledger/spec.md AC-18, AC-19, AC-20): the task-scoped
|
||||
// token/cost aggregate, including rows whose session_id was cleared by
|
||||
// session deletion. A known task with no usage returns HTTP 200 with zeroed
|
||||
// totals (AC-20), never an error.
|
||||
func (h *TaskHandlers) httpGetTaskUsageTotals(c *gin.Context) {
|
||||
taskID := c.Param("id")
|
||||
totals, err := h.service.GetTaskUsageTotals(c.Request.Context(), taskID)
|
||||
if err != nil {
|
||||
handleNotFound(c, h.logger, err, "task not found")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.ToTaskUsageTotalsDTO(dto.TaskUsageTotalsScopeTask, taskID, totals))
|
||||
}
|
||||
|
||||
// httpGetTaskSessionUsageTotals serves
|
||||
// GET /api/v1/tasks/:id/sessions/:sessionId/usage (AC-18, AC-19, AC-20): the
|
||||
// session-scoped token/cost aggregate. Returns 404 when the task or session
|
||||
// does not exist, or when the session exists but does not belong to the
|
||||
// task in the path.
|
||||
func (h *TaskHandlers) httpGetTaskSessionUsageTotals(c *gin.Context) {
|
||||
taskID := c.Param("id")
|
||||
sessionID := c.Param("sessionId")
|
||||
totals, err := h.service.GetTaskSessionUsageTotals(c.Request.Context(), taskID, sessionID)
|
||||
if err != nil {
|
||||
handleNotFound(c, h.logger, err, "task session not found")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.ToTaskUsageTotalsDTO(dto.TaskUsageTotalsScopeSession, sessionID, totals))
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
"github.com/kandev/kandev/internal/task/dto"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
"github.com/kandev/kandev/internal/task/service"
|
||||
)
|
||||
|
||||
// usageTotalsRepo backs the task-cost-ledger HTTP handler tests
|
||||
// (docs/specs/task-cost-ledger/spec.md AC-18). It reuses foreignSessionRepo's
|
||||
// task-b/sess-b-owned-by-user-b fixture and stubs the two aggregate reads.
|
||||
type usageTotalsRepo struct {
|
||||
foreignSessionRepo
|
||||
taskTotals *models.TaskUsageTotals
|
||||
sessionTotals *models.TaskUsageTotals
|
||||
}
|
||||
|
||||
func (r *usageTotalsRepo) GetTaskUsageTotals(context.Context, string) (*models.TaskUsageTotals, error) {
|
||||
return r.taskTotals, nil
|
||||
}
|
||||
|
||||
func (r *usageTotalsRepo) GetSessionUsageTotals(context.Context, string) (*models.TaskUsageTotals, error) {
|
||||
return r.sessionTotals, nil
|
||||
}
|
||||
|
||||
func newUsageTotalsHandlers(t *testing.T, taskTotals, sessionTotals *models.TaskUsageTotals) *TaskHandlers {
|
||||
t.Helper()
|
||||
log := newTestLogger(t)
|
||||
repo := &usageTotalsRepo{taskTotals: taskTotals, sessionTotals: sessionTotals}
|
||||
svc := service.NewService(service.Repos{
|
||||
Workspaces: repo, Tasks: repo, TaskRepos: repo,
|
||||
Workflows: repo, Messages: repo, Turns: repo,
|
||||
Sessions: repo, GitSnapshots: repo, RepoEntities: repo,
|
||||
Executors: repo, Environments: repo, TaskEnvironments: repo,
|
||||
Reviews: repo, Usage: repo,
|
||||
}, nil, log, service.RepositoryDiscoveryConfig{})
|
||||
return &TaskHandlers{service: svc, logger: log}
|
||||
}
|
||||
|
||||
func taskUsageRequestAs(t *testing.T, path, userID string, params gin.Params) (*gin.Context, *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(rec)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, path, nil)
|
||||
c.Request = c.Request.WithContext(
|
||||
authn.WithIdentity(c.Request.Context(), authn.Identity{UserID: userID, Role: authn.RoleMember}),
|
||||
)
|
||||
c.Params = params
|
||||
return c, rec
|
||||
}
|
||||
|
||||
// TestHTTPGetTaskUsageTotalsDeniesForeignTaskWith404 pins the scoping
|
||||
// requirement CLAUDE.md's backend security-boundary rule adds to any new
|
||||
// task-keyed route: a foreign owner is denied, the real owner passes.
|
||||
func TestHTTPGetTaskUsageTotalsDeniesForeignTaskWith404(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
h := newUsageTotalsHandlers(t, &models.TaskUsageTotals{OutputTokensComplete: true}, nil)
|
||||
|
||||
c, rec := taskUsageRequestAs(t, "/api/v1/tasks/task-b/usage", "user-a", gin.Params{{Key: "id", Value: "task-b"}})
|
||||
h.httpGetTaskUsageTotals(c)
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404 (body: %s)", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
ownerCtx, ownerRec := taskUsageRequestAs(t, "/api/v1/tasks/task-b/usage", "user-b", gin.Params{{Key: "id", Value: "task-b"}})
|
||||
h.httpGetTaskUsageTotals(ownerCtx)
|
||||
if ownerRec.Code != http.StatusOK {
|
||||
t.Fatalf("owner status = %d, want 200 (body: %s)", ownerRec.Code, ownerRec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestHTTPGetTaskUsageTotalsZeroUsageReturns200WithZeroedBody pins AC-20: a
|
||||
// known task with no recorded usage answers 200 with zeroed totals, not an
|
||||
// error, and the response identifies its own scope.
|
||||
func TestHTTPGetTaskUsageTotalsZeroUsageReturns200WithZeroedBody(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
h := newUsageTotalsHandlers(t, &models.TaskUsageTotals{OutputTokensComplete: true}, nil)
|
||||
|
||||
c, rec := taskUsageRequestAs(t, "/api/v1/tasks/task-b/usage", "user-b", gin.Params{{Key: "id", Value: "task-b"}})
|
||||
h.httpGetTaskUsageTotals(c)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200 (body: %s)", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var body dto.TaskUsageTotalsDTO
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if body.Scope != dto.TaskUsageTotalsScopeTask || body.ScopeID != "task-b" {
|
||||
t.Fatalf("scope = (%q, %q), want (task, task-b)", body.Scope, body.ScopeID)
|
||||
}
|
||||
if body.EventCount != 0 {
|
||||
t.Fatalf("EventCount = %d, want 0", body.EventCount)
|
||||
}
|
||||
if !body.OutputTokensComplete {
|
||||
t.Error("OutputTokensComplete = false, want true for zero usage")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHTTPGetTaskSessionUsageTotalsDeniesForeignSessionWith404 mirrors the
|
||||
// task-scoped denial test for the session-scoped route.
|
||||
func TestHTTPGetTaskSessionUsageTotalsDeniesForeignSessionWith404(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
h := newUsageTotalsHandlers(t, nil, &models.TaskUsageTotals{OutputTokensComplete: true})
|
||||
params := gin.Params{{Key: "id", Value: "task-b"}, {Key: "sessionId", Value: "sess-b"}}
|
||||
|
||||
c, rec := taskUsageRequestAs(t, "/api/v1/tasks/task-b/sessions/sess-b/usage", "user-a", params)
|
||||
h.httpGetTaskSessionUsageTotals(c)
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404 (body: %s)", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
ownerCtx, ownerRec := taskUsageRequestAs(t, "/api/v1/tasks/task-b/sessions/sess-b/usage", "user-b", params)
|
||||
h.httpGetTaskSessionUsageTotals(ownerCtx)
|
||||
if ownerRec.Code != http.StatusOK {
|
||||
t.Fatalf("owner status = %d, want 200 (body: %s)", ownerRec.Code, ownerRec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestHTTPGetTaskSessionUsageTotalsReturnsSessionScopedBody proves the
|
||||
// session route stamps scope=session and the path's sessionId, not the
|
||||
// task's, into the response.
|
||||
func TestHTTPGetTaskSessionUsageTotalsReturnsSessionScopedBody(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
h := newUsageTotalsHandlers(t, nil, &models.TaskUsageTotals{EventCount: 3, OutputTokensComplete: true})
|
||||
params := gin.Params{{Key: "id", Value: "task-b"}, {Key: "sessionId", Value: "sess-b"}}
|
||||
|
||||
c, rec := taskUsageRequestAs(t, "/api/v1/tasks/task-b/sessions/sess-b/usage", "user-b", params)
|
||||
h.httpGetTaskSessionUsageTotals(c)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200 (body: %s)", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var body dto.TaskUsageTotalsDTO
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
|
||||
t.Fatalf("unmarshal: %v", err)
|
||||
}
|
||||
if body.Scope != dto.TaskUsageTotalsScopeSession || body.ScopeID != "sess-b" {
|
||||
t.Fatalf("scope = (%q, %q), want (session, sess-b)", body.Scope, body.ScopeID)
|
||||
}
|
||||
if body.EventCount != 3 {
|
||||
t.Fatalf("EventCount = %d, want 3", body.EventCount)
|
||||
}
|
||||
}
|
||||
@@ -1450,6 +1450,15 @@ type TaskSession struct {
|
||||
// panel; the frontend snapshots the PRIOR value at that moment to draw
|
||||
// the divider before overwriting it.
|
||||
LastReadMessageID string `json:"last_read_message_id,omitempty"`
|
||||
|
||||
// Usage/cost rollup columns (docs/specs/task-cost-ledger/spec.md AC-28,
|
||||
// AC-29). task_usage_events is the source of truth; these are the
|
||||
// running totals internal/task/usage's writer maintains transactionally
|
||||
// alongside each ledger insert via IncrementTaskSessionUsageTx.
|
||||
CostSubcents int64 `json:"cost_subcents"`
|
||||
TokensIn int64 `json:"tokens_in"`
|
||||
TokensCachedIn int64 `json:"tokens_cached_in"`
|
||||
TokensOut int64 `json:"tokens_out"`
|
||||
}
|
||||
|
||||
// ToAPI converts internal TaskSession to API type
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
// TaskUsageEvent is one immutable row of internal/task/repository/sqlite's
|
||||
// task_usage_events ledger (docs/specs/task-cost-ledger/spec.md). A pointer
|
||||
// token/rate field means the value is *not recorded*, distinct from a
|
||||
// measured zero (AC-4, AC-30); SessionID, TurnID, and PricingCatalogVersion
|
||||
// use "" for SQL NULL since they are always either absent or a non-empty
|
||||
// identifier.
|
||||
type TaskUsageEvent struct {
|
||||
ID int64
|
||||
UsageEventID string
|
||||
TaskID string
|
||||
SessionID string
|
||||
TurnID string
|
||||
AgentProfileID string
|
||||
AgentType string
|
||||
Model string
|
||||
Provider string
|
||||
|
||||
TokensIn int64
|
||||
TokensCachedRead *int64
|
||||
TokensCachedWrite *int64
|
||||
TokensOut *int64
|
||||
TokensThought *int64
|
||||
TokensTotal int64
|
||||
|
||||
CostSubcents int64
|
||||
CostSource string
|
||||
Estimated bool
|
||||
|
||||
RateInputPerMillion *int64
|
||||
RateCachedReadPerMillion *int64
|
||||
RateCachedWritePerMillion *int64
|
||||
RateOutputPerMillion *int64
|
||||
PricingCatalogVersion string
|
||||
|
||||
ContractVersion int
|
||||
OccurredAt time.Time
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
// TaskUsageTotals is the read-side aggregate over task_usage_events for one
|
||||
// scope (a task, or a single session within a task) - docs/specs/task-cost-ledger/spec.md
|
||||
// AC-18, AC-19, AC-20. TokensTotal is the sum of each row's stored
|
||||
// tokens_total column, never recomputed from the per-kind sums at read time.
|
||||
// OutputTokensComplete is false when any contributing row has a nil
|
||||
// TokensOut (AC-12): the total is then a lower bound, not an exact count. A
|
||||
// scope with EventCount zero reports every sum as zero,
|
||||
// OutputTokensComplete true, and both timestamps nil (AC-20).
|
||||
type TaskUsageTotals struct {
|
||||
TokensIn int64
|
||||
TokensCachedRead int64
|
||||
TokensCachedWrite int64
|
||||
TokensOut int64
|
||||
TokensThought int64
|
||||
TokensTotal int64
|
||||
CostSubcents int64
|
||||
EventCount int64
|
||||
EstimatedEventCount int64
|
||||
UnpricedEventCount int64
|
||||
OutputTokensComplete bool
|
||||
FirstEventAt *time.Time
|
||||
LastEventAt *time.Time
|
||||
}
|
||||
@@ -588,3 +588,14 @@ type SubagentContextRepository interface {
|
||||
ListSubagentContextsBySession(ctx context.Context, sessionID string) ([]*models.SubagentContext, error)
|
||||
ListSubagentContextsByTurn(ctx context.Context, turnID string) ([]*models.SubagentContext, error)
|
||||
}
|
||||
|
||||
// UsageRepository serves the task-cost-ledger read surface
|
||||
// (docs/specs/task-cost-ledger/spec.md AC-18, AC-19, AC-20): per-task and
|
||||
// per-session aggregate totals over task_usage_events. The ledger write path
|
||||
// (CreateTaskUsageEvent, ListTaskUsageEvents) is deliberately not part of
|
||||
// this interface - it is consumed only by internal/task/usage's own narrow
|
||||
// Repository interface, never through the Service layer.
|
||||
type UsageRepository interface {
|
||||
GetTaskUsageTotals(ctx context.Context, taskID string) (*models.TaskUsageTotals, error)
|
||||
GetSessionUsageTotals(ctx context.Context, sessionID string) (*models.TaskUsageTotals, error)
|
||||
}
|
||||
|
||||
@@ -31,6 +31,47 @@ type Repository struct {
|
||||
// cutover: when set to a cutover step name, the migration aborts at that
|
||||
// step so tests can prove rollback restores the pre-upgrade state.
|
||||
failCutoverAfter string
|
||||
// failUsageEventAttempts/failUsageEventErr are a test-only failpoint for
|
||||
// CreateTaskUsageEvent's AC-32 transient-retry loop: while
|
||||
// failUsageEventAttempts > 0, insertUsageEventAndRollup returns
|
||||
// failUsageEventErr before opening a transaction, without touching the
|
||||
// database, and decrements the counter. A genuine SQLITE_BUSY/serialization
|
||||
// race cannot be triggered deterministically against this package's
|
||||
// single-connection test repositories, so this stands in for one. Use this
|
||||
// (not the mid-transaction seam below) when a scenario needs the injected
|
||||
// failure to happen before any real driver-level activity - e.g. before a
|
||||
// same-attempt foreign-key violation the test also wants to observe.
|
||||
failUsageEventAttempts int
|
||||
failUsageEventErr error
|
||||
// failUsageEventRollupAttempts/failUsageEventRollupErr are a test-only
|
||||
// failpoint inside insertUsageEventAndRollup's transaction, checked after
|
||||
// the ledger row insert succeeds but before the task_sessions rollup
|
||||
// increment: while failUsageEventRollupAttempts > 0, the function returns
|
||||
// failUsageEventRollupErr and decrements the counter, so the deferred
|
||||
// tx.Rollback() unwinds a real, already-attempted INSERT. Unlike
|
||||
// failUsageEventAttempts (which fires before BeginTxx and so cannot tell
|
||||
// an atomic single-transaction implementation from a hypothetical
|
||||
// two-transaction one, since neither has touched the database yet), this
|
||||
// seam proves AC-11's atomicity: an implementation that committed the
|
||||
// insert in its own transaction before attempting the rollup would leave a
|
||||
// row behind here, while the real single-transaction implementation rolls
|
||||
// it back with everything else.
|
||||
failUsageEventRollupAttempts int
|
||||
failUsageEventRollupErr error
|
||||
// usageEventPreRollupHook is a test-only synchronization seam, called (if
|
||||
// set) inside insertUsageEventAndRollup's transaction at the same point as
|
||||
// the failUsageEventRollup* failpoint - after the ledger row insert
|
||||
// succeeds, before the task_sessions rollup increment. Unlike the
|
||||
// failpoints, it does not alter control flow; it exists so a Postgres test
|
||||
// can pause a real production transaction at this exact boundary and open
|
||||
// a second, genuinely concurrent connection to construct a real lock
|
||||
// conflict there (a real SQLITE_BUSY/serialization race cannot be
|
||||
// provoked deterministically against SQLite's single test connection, but
|
||||
// Postgres supports true concurrent connections, so this proves AC-32's
|
||||
// retry classification against a real driver-reported 40001/40P01 rather
|
||||
// than only the injected failpoint errors). Nil in production and in
|
||||
// every test but the one that sets it.
|
||||
usageEventPreRollupHook func()
|
||||
}
|
||||
|
||||
func (r *Repository) nowUTC() time.Time {
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -45,81 +44,11 @@ func (r *Repository) migrateSessionsAddCostColumns() {
|
||||
// session (the reported bug measured up to 98,805,109 on one already-
|
||||
// completed task). SQLite's INTEGER is 64-bit regardless, but on Postgres
|
||||
// INTEGER is int4 - an overflowing session would abort the single
|
||||
// multi-column UPDATE in IncrementTaskSessionUsage and the single
|
||||
// table-wide UPDATE in BackfillSessionTokensCachedIn, silently taking
|
||||
// multi-column UPDATE in IncrementTaskSessionUsage, silently taking
|
||||
// tokens_in/tokens_out/cost_subcents down with it for that session.
|
||||
r.migrate.Apply("task_sessions.tokens_cached_in", `ALTER TABLE task_sessions ADD COLUMN tokens_cached_in BIGINT NOT NULL DEFAULT 0`)
|
||||
}
|
||||
|
||||
// BackfillSessionTokensCachedIn recomputes task_sessions.tokens_cached_in from
|
||||
// the office_cost_events ledger. The rollup is purely derived from the ledger,
|
||||
// so recomputing from it is restoring data, not inventing it.
|
||||
//
|
||||
// Deliberately NOT called from runMigrations(): office_cost_events is owned and
|
||||
// created by internal/office/repository/sqlite, a separate repository that
|
||||
// shares this database but initializes independently, and on a fresh boot the
|
||||
// task repository's own migrations run first (internal/backendapp/storage.go).
|
||||
// An earlier version of this method guarded on the ledger table's existence to
|
||||
// tolerate that ordering from inside runMigrations(); that guard, and the table
|
||||
// existence it checked, both went away in favor of the caller in
|
||||
// internal/backendapp/storage.go, which only invokes this after office.Provide
|
||||
// has already succeeded — construction order guarantees office_cost_events
|
||||
// exists by then, so no guard is needed here. This keeps the task repository
|
||||
// from having to know office's schema/table-existence details, and lets the
|
||||
// office_cost_events(session_id) index it depends on live with the table it
|
||||
// indexes (internal/office/repository/sqlite/base.go createCostTables).
|
||||
//
|
||||
// Deliberately unconditional per session it touches (assignment, not
|
||||
// increment, so it is idempotent across replays) rather than gated on
|
||||
// "already nonzero": the rollup can go out of sync with the ledger without
|
||||
// erroring — a live IncrementTaskSessionUsage call is a best-effort
|
||||
// UPDATE ... WHERE id = ? that silently matches zero rows if the session row
|
||||
// doesn't exist yet (see the "missing row" case in
|
||||
// TestIncrementTaskSessionUsage_UnknownSessionNoError), and
|
||||
// event_subscribers.go only logs a Warn when the rollup write fails while the
|
||||
// ledger insert has already committed. A "skip if nonzero" guard would make
|
||||
// exactly that drift permanent instead of self-healing it on the next boot.
|
||||
// (If the ledger for a session was deleted separately from the session row
|
||||
// itself - see the two-transaction delete in workspace_deletion.go - this
|
||||
// recompute correctly zeroes that session's tokens_cached_in, since an empty
|
||||
// ledger sums to zero; the rollup's only contract is to equal the ledger sum.)
|
||||
//
|
||||
// The WHERE clause below is a boot-cost optimization, not a correctness gate:
|
||||
// it restricts the write to sessions that either have ledger rows (need a
|
||||
// possible recompute) or already hold a nonzero value (need a possible
|
||||
// zeroing, per the paragraph above). Every row it excludes has
|
||||
// tokens_cached_in = 0 AND no ledger rows, so the unconditional
|
||||
// COALESCE(NULL, 0) = 0 this statement would otherwise compute for it is
|
||||
// already what the row holds — excluding it is a provable no-op, never a
|
||||
// skip of a row that needs correcting. This runs unconditionally on every
|
||||
// boot (no run-once tracking - see MigrateLogger.Apply, which this method
|
||||
// deliberately does not use so a real failure propagates instead of being
|
||||
// swallowed as a Warn), so office_cost_events(session_id) must already be
|
||||
// indexed: an unindexed correlated subquery here is quadratic in
|
||||
// (sessions x ledger rows), measured at 76.72s at a modest 4,000 sessions /
|
||||
// 80,000 events versus 0.17s indexed.
|
||||
//
|
||||
// Returns the number of task_sessions rows the WHERE clause matched, so a
|
||||
// caller can log it for boot-time observability; not otherwise used for
|
||||
// correctness.
|
||||
func (r *Repository) BackfillSessionTokensCachedIn(ctx context.Context) (int64, error) {
|
||||
result, err := r.db.ExecContext(ctx, `
|
||||
UPDATE task_sessions
|
||||
SET tokens_cached_in = COALESCE(
|
||||
(SELECT SUM(e.tokens_cached_in) FROM office_cost_events e
|
||||
WHERE e.session_id = task_sessions.id), 0)
|
||||
WHERE EXISTS (SELECT 1 FROM office_cost_events e WHERE e.session_id = task_sessions.id)
|
||||
OR tokens_cached_in <> 0`)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("backfill task_sessions.tokens_cached_in: %w", err)
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("backfill task_sessions.tokens_cached_in: rows affected: %w", err)
|
||||
}
|
||||
return affected, nil
|
||||
}
|
||||
|
||||
// runMigrations applies idempotent ALTER TABLE migrations for schema evolution.
|
||||
func (r *Repository) runMigrations() error {
|
||||
if err := r.migrateTaskPriorityToTextPostgres(); err != nil {
|
||||
@@ -259,10 +188,10 @@ func (r *Repository) runMigrations() error {
|
||||
// task_sessions rebuilds above so it repairs legacy DBs whose schema can no
|
||||
// longer trigger a rebuild (see migrateSessionsAddCostColumns).
|
||||
r.migrateSessionsAddCostColumns()
|
||||
// BackfillSessionTokensCachedIn is deliberately NOT called here - see its
|
||||
// doc comment. It runs from internal/backendapp/storage.go, after both
|
||||
// this repository and the office repository (which owns office_cost_events)
|
||||
// have finished initializing.
|
||||
// AC-28: widen the three still-INTEGER rollup columns to BIGINT (Postgres
|
||||
// only). Must run after migrateSessionsAddCostColumns so a legacy DB has
|
||||
// the columns to widen before this ALTERs their type.
|
||||
r.migrateTaskSessionsRollupColumnsToBigint()
|
||||
|
||||
// Office task extensions - net-new columns on existing main tables.
|
||||
// Idempotent ALTERs; main upgrades pick them up at first boot.
|
||||
|
||||
@@ -23,6 +23,7 @@ func (r *Repository) initSchema() error {
|
||||
r.initSessionSchema,
|
||||
r.initDynamicRoutingSchema,
|
||||
r.initStepTransitionsSchema,
|
||||
r.initTaskUsageEventsSchema,
|
||||
r.initAttachmentsSchema,
|
||||
r.initTaskResourceCleanupSchema,
|
||||
r.initGitSchema,
|
||||
|
||||
@@ -475,7 +475,8 @@ const taskSessionSelectCols = `ts.id, ts.task_id,
|
||||
COALESCE(NULLIF(te.workspace_path, ''), ts.workspace_path),
|
||||
ts.agent_profile_snapshot, ts.executor_snapshot, ts.environment_snapshot, ts.repository_snapshot,
|
||||
ts.state, ts.error_message, ts.metadata, ts.started_at, ts.completed_at, ts.updated_at,
|
||||
ts.is_primary, ts.review_status, ts.is_passthrough, ts.task_environment_id, ts.name, ts.last_read_message_id`
|
||||
ts.is_primary, ts.review_status, ts.is_passthrough, ts.task_environment_id, ts.name, ts.last_read_message_id,
|
||||
ts.cost_subcents, ts.tokens_in, ts.tokens_cached_in, ts.tokens_out`
|
||||
|
||||
// taskSessionFromClause is the FROM clause that pairs with taskSessionSelectCols.
|
||||
// Always reference task_sessions as `ts` and executors_running as `er` in WHERE/ORDER.
|
||||
@@ -987,6 +988,7 @@ func (r *Repository) scanTaskSession(ctx context.Context, row *sql.Row, noRowsEr
|
||||
&agentProfileSnapshotJSON, &executorSnapshotJSON, &environmentSnapshotJSON, &repositorySnapshotJSON,
|
||||
&state, &session.ErrorMessage, &metadataJSON, &session.StartedAt, &completedAt, &session.UpdatedAt,
|
||||
&isPrimary, &reviewStatus, &isPassthrough, &session.TaskEnvironmentID, &name, &lastReadMessageID,
|
||||
&session.CostSubcents, &session.TokensIn, &session.TokensCachedIn, &session.TokensOut,
|
||||
)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -2142,29 +2144,16 @@ func (r *Repository) GetLastAgentMessage(ctx context.Context, sessionID string)
|
||||
return content, nil
|
||||
}
|
||||
|
||||
// IncrementTaskSessionUsage adds the given deltas to the cumulative
|
||||
// IncrementTaskSessionUsageTx adds the given deltas to the cumulative
|
||||
// tokens / cost columns on task_sessions, including cached input tokens
|
||||
// (tokens_cached_in mirrors office_cost_events.tokens_cached_in and is kept
|
||||
// separate from tokens_in because it is priced differently). Used by the
|
||||
// office cost subscriber after a cost event lands so the per-session totals
|
||||
// stay in sync without re-summing office_cost_events. The model + DTO
|
||||
// don't surface these columns yet (DB-only per the office-costs
|
||||
// wedge); the cost explorer follow-up will expose them.
|
||||
//
|
||||
// Delegates to IncrementTaskSessionUsageTx using r.db as the executor; a
|
||||
// caller that needs this atomic with another write (e.g. the office cost
|
||||
// subscriber's ledger insert) should call the Tx variant directly with a
|
||||
// shared transaction instead.
|
||||
func (r *Repository) IncrementTaskSessionUsage(
|
||||
ctx context.Context, sessionID string, tokensIn, tokensCachedIn, tokensOut, costSubcents int64,
|
||||
) error {
|
||||
return r.IncrementTaskSessionUsageTx(ctx, nil, sessionID, tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
|
||||
// IncrementTaskSessionUsageTx implements shared.SessionUsageWriterTx: same
|
||||
// write as IncrementTaskSessionUsage, but executed against tx when non-nil
|
||||
// (falling back to r.db, the shared writer connection, when tx is nil) so a
|
||||
// caller can make this atomic with another write in the same transaction.
|
||||
// separate from tokens_in because it is priced differently). Surfaced on
|
||||
// models.TaskSession and dto.TaskSessionDTO. internal/task/usage's writer is
|
||||
// the sole production caller (docs/specs/task-cost-ledger/spec.md AC-10,
|
||||
// AC-21) — it inserts a task_usage_events row and increments this rollup in
|
||||
// one transaction (insertUsageEventAndRollup in this package), so this
|
||||
// method is executed against tx when non-nil (falling back to r.db, the
|
||||
// shared writer connection, when tx is nil).
|
||||
func (r *Repository) IncrementTaskSessionUsageTx(
|
||||
ctx context.Context, tx *sqlx.Tx, sessionID string, tokensIn, tokensCachedIn, tokensOut, costSubcents int64,
|
||||
) error {
|
||||
@@ -2621,6 +2610,7 @@ func scanTaskSessionRow(rows *sql.Rows) (*models.TaskSession, error) {
|
||||
&agentProfileSnapshotJSON, &executorSnapshotJSON, &environmentSnapshotJSON, &repositorySnapshotJSON,
|
||||
&state, &session.ErrorMessage, &metadataJSON, &session.StartedAt, &completedAt, &session.UpdatedAt,
|
||||
&isPrimary, &reviewStatus, &isPassthrough, &session.TaskEnvironmentID, &name, &lastReadMessageID,
|
||||
&session.CostSubcents, &session.TokensIn, &session.TokensCachedIn, &session.TokensOut,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -1101,10 +1101,10 @@ func TestIncrementTaskSessionUsage_AccumulatesAcrossCalls(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-usage", "sess-usage", "turn-usage")
|
||||
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-usage", 100, 0, 200, 50); err != nil {
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-usage", 100, 0, 200, 50); err != nil {
|
||||
t.Fatalf("first increment: %v", err)
|
||||
}
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-usage", 10, 0, 20, 5); err != nil {
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-usage", 10, 0, 20, 5); err != nil {
|
||||
t.Fatalf("second increment: %v", err)
|
||||
}
|
||||
|
||||
@@ -1124,7 +1124,7 @@ func TestIncrementTaskSessionUsage_AccumulatesAcrossCalls(t *testing.T) {
|
||||
// missing row (subscriber may race against session creation).
|
||||
func TestIncrementTaskSessionUsage_UnknownSessionNoError(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
if err := repo.IncrementTaskSessionUsage(context.Background(), "no-such", 1, 1, 2, 3); err != nil {
|
||||
if err := repo.IncrementTaskSessionUsageTx(context.Background(), nil, "no-such", 1, 1, 2, 3); err != nil {
|
||||
t.Errorf("expected no error for unknown session, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1133,7 +1133,7 @@ func TestIncrementTaskSessionUsage_UnknownSessionNoError(t *testing.T) {
|
||||
// orchestrator publishing a usage event before SessionID is set.
|
||||
func TestIncrementTaskSessionUsage_EmptySessionIDNoOp(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
if err := repo.IncrementTaskSessionUsage(context.Background(), "", 1, 1, 2, 3); err != nil {
|
||||
if err := repo.IncrementTaskSessionUsageTx(context.Background(), nil, "", 1, 1, 2, 3); err != nil {
|
||||
t.Errorf("empty session id should be a no-op, got %v", err)
|
||||
}
|
||||
}
|
||||
@@ -1189,7 +1189,7 @@ func rebuildSessionsWithoutCostColumns(t *testing.T, repo *Repository) {
|
||||
// TestMigrateSessionsAddCostColumns_BackfillsLegacySchema reproduces the office
|
||||
// cost subscriber failure ("no such column: tokens_in"): a task_sessions table
|
||||
// that predates the cost columns and no longer contains the rebuild trigger
|
||||
// columns can never gain them, so IncrementTaskSessionUsage fails. The additive
|
||||
// columns can never gain them, so IncrementTaskSessionUsageTx fails. The additive
|
||||
// migration must backfill the columns idempotently.
|
||||
func TestMigrateSessionsAddCostColumns_BackfillsLegacySchema(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
@@ -1199,20 +1199,20 @@ func TestMigrateSessionsAddCostColumns_BackfillsLegacySchema(t *testing.T) {
|
||||
seedForMsgTest(t, repo, "task-mig", "sess-mig", "turn-mig")
|
||||
|
||||
// Precondition: this is the reported bug on a legacy schema.
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-mig", 1, 1, 2, 3); err == nil {
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-mig", 1, 1, 2, 3); err == nil {
|
||||
t.Fatal("expected missing-column error before backfill")
|
||||
}
|
||||
|
||||
repo.migrateSessionsAddCostColumns()
|
||||
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-mig", 1, 1, 2, 3); err != nil {
|
||||
t.Fatalf("IncrementTaskSessionUsage after backfill: %v", err)
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-mig", 1, 1, 2, 3); err != nil {
|
||||
t.Fatalf("IncrementTaskSessionUsageTx after backfill: %v", err)
|
||||
}
|
||||
|
||||
// Idempotent: a second pass over a table that already has the columns is a no-op.
|
||||
repo.migrateSessionsAddCostColumns()
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-mig", 10, 10, 20, 30); err != nil {
|
||||
t.Fatalf("IncrementTaskSessionUsage after second pass: %v", err)
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-mig", 10, 10, 20, 30); err != nil {
|
||||
t.Fatalf("IncrementTaskSessionUsageTx after second pass: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,22 +2,20 @@ package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/testutil"
|
||||
)
|
||||
|
||||
// The cached-token rollup (BackfillSessionTokensCachedIn,
|
||||
// IncrementTaskSessionUsage) is dialect-sensitive in a way the SQLite tests in
|
||||
// session_usage_test.go and session_test.go cannot exercise: SQLite's INTEGER
|
||||
// is 64-bit, so a column declared INTEGER never overflows there. On Postgres,
|
||||
// INTEGER is int4 (max 2,147,483,647), and office_cost_events.tokens_cached_in
|
||||
// routinely accumulates well past that for a single long-running session (the
|
||||
// reported bug measured up to 98,805,109 on one already-completed task).
|
||||
// Without this file the type of the new column was never exercised against
|
||||
// Postgres at all.
|
||||
// The cached-token rollup (IncrementTaskSessionUsageTx) is dialect-sensitive in
|
||||
// a way the SQLite tests in session_usage_test.go and session_test.go cannot
|
||||
// exercise: SQLite's INTEGER is 64-bit, so a column declared INTEGER never
|
||||
// overflows there. On Postgres, INTEGER is int4 (max 2,147,483,647), and
|
||||
// office_cost_events.tokens_cached_in routinely accumulates well past that
|
||||
// for a single long-running session (the reported bug measured up to
|
||||
// 98,805,109 on one already-completed task). Without this file the type of
|
||||
// the new column was never exercised against Postgres at all.
|
||||
//
|
||||
// Skips unless KANDEV_TEST_POSTGRES_DSN is set.
|
||||
|
||||
@@ -38,98 +36,8 @@ func seedPostgresTaskSession(t *testing.T, repo *Repository, taskID, sessionID s
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostgresBackfillSessionTokensCachedIn_SumsLedger is the Postgres
|
||||
// counterpart to TestBackfillSessionTokensCachedIn_SumsLedger: proves the
|
||||
// correlated-subquery UPDATE and its CREATE INDEX IF NOT EXISTS both work
|
||||
// against a real Postgres instance, not just SQLite.
|
||||
func TestPostgresBackfillSessionTokensCachedIn_SumsLedger(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-backfill-pg", "sess-backfill-pg")
|
||||
createOfficeCostEventsTable(t, repo)
|
||||
insertOfficeCostEvent(t, repo, "cost-1", "sess-backfill-pg", 98_805_109)
|
||||
insertOfficeCostEvent(t, repo, "cost-2", "sess-backfill-pg", 1_194_891)
|
||||
insertOfficeCostEvent(t, repo, "cost-other", "sess-other-pg", 555)
|
||||
|
||||
if _, err := repo.BackfillSessionTokensCachedIn(context.Background()); err != nil {
|
||||
t.Fatalf("backfill: %v", err)
|
||||
}
|
||||
|
||||
var got int64
|
||||
if err := repo.ro.QueryRowx(repo.ro.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-backfill-pg",
|
||||
).Scan(&got); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if got != 100_000_000 {
|
||||
t.Errorf("tokens_cached_in = %d, want 100000000 (sum of office_cost_events for this session)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostgresBackfillSessionTokensCachedIn_HandlesValuesBeyondInt32Range is
|
||||
// the regression test for F8: task_sessions.tokens_cached_in was declared
|
||||
// INTEGER (int4 on Postgres, max 2,147,483,647). Because the backfill is a
|
||||
// single UPDATE across the whole task_sessions table, one session whose
|
||||
// ledger sum exceeds the int4 ceiling made the entire statement fail with
|
||||
// "integer out of range" — swallowed to a Warn by MigrateLogger.Apply, so the
|
||||
// self-healing recompute silently stopped working for every session, not just
|
||||
// the overflowing one. Fails on an INTEGER column, passes once the column is
|
||||
// BIGINT.
|
||||
func TestPostgresBackfillSessionTokensCachedIn_HandlesValuesBeyondInt32Range(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-overflow-pg", "sess-overflow-pg")
|
||||
seedPostgresTaskSession(t, repo, "task-normal-pg", "sess-normal-pg")
|
||||
createOfficeCostEventsTable(t, repo)
|
||||
|
||||
// 200 events of 14,000,000 cached tokens each = 2,800,000,000, well past
|
||||
// the int32 ceiling of 2,147,483,647 - the shape the card's own live-instance
|
||||
// measurement (98,805,109 on a single completed task) extrapolates to over
|
||||
// a long-running session.
|
||||
const perEvent = int64(14_000_000)
|
||||
const eventCount = 200
|
||||
const wantOverflow = perEvent * eventCount
|
||||
for i := 0; i < eventCount; i++ {
|
||||
insertOfficeCostEvent(t, repo, fmt.Sprintf("cost-overflow-%d", i), "sess-overflow-pg", perEvent)
|
||||
}
|
||||
// A normal-sized session must still backfill correctly in the same pass -
|
||||
// the whole point of F8 is that one overflowing row previously took the
|
||||
// entire UPDATE down with it.
|
||||
insertOfficeCostEvent(t, repo, "cost-normal", "sess-normal-pg", 1_000)
|
||||
|
||||
if _, err := repo.BackfillSessionTokensCachedIn(context.Background()); err != nil {
|
||||
t.Fatalf("backfill must not error on a ledger sum beyond int32 range: %v", err)
|
||||
}
|
||||
|
||||
var gotOverflow, gotNormal int64
|
||||
if err := repo.ro.QueryRowx(repo.ro.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-overflow-pg",
|
||||
).Scan(&gotOverflow); err != nil {
|
||||
t.Fatalf("read overflow row: %v", err)
|
||||
}
|
||||
if gotOverflow != wantOverflow {
|
||||
t.Errorf("tokens_cached_in = %d, want %d (sum beyond int32 range must survive the backfill)",
|
||||
gotOverflow, wantOverflow)
|
||||
}
|
||||
if err := repo.ro.QueryRowx(repo.ro.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-normal-pg",
|
||||
).Scan(&gotNormal); err != nil {
|
||||
t.Fatalf("read normal row: %v", err)
|
||||
}
|
||||
if gotNormal != 1_000 {
|
||||
t.Errorf("tokens_cached_in for the non-overflowing session = %d, want 1000 "+
|
||||
"(a sibling session's overflow must not abort the whole backfill statement)", gotNormal)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostgresIncrementTaskSessionUsage_AccumulatesValuesBeyondInt32Range
|
||||
// covers F8's other failure mode: IncrementTaskSessionUsage sets tokens_in,
|
||||
// covers F8's other failure mode: IncrementTaskSessionUsageTx sets tokens_in,
|
||||
// tokens_cached_in, tokens_out and cost_subcents in one UPDATE. On an INTEGER
|
||||
// column that UPDATE fails once tokens_cached_in crosses the int32 ceiling,
|
||||
// silently stopping tokens_in/tokens_out/cost_subcents from updating too -
|
||||
@@ -145,8 +53,8 @@ func TestPostgresIncrementTaskSessionUsage_AccumulatesValuesBeyondInt32Range(t *
|
||||
seedPostgresTaskSession(t, repo, "task-inc-overflow-pg", "sess-inc-overflow-pg")
|
||||
|
||||
const beyondInt32 = int64(2_800_000_000)
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-inc-overflow-pg", 100, beyondInt32, 200, 50); err != nil {
|
||||
t.Fatalf("IncrementTaskSessionUsage must not error on a cached-token delta beyond int32 range: %v", err)
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-inc-overflow-pg", 100, beyondInt32, 200, 50); err != nil {
|
||||
t.Fatalf("IncrementTaskSessionUsageTx must not error on a cached-token delta beyond int32 range: %v", err)
|
||||
}
|
||||
|
||||
var tokensIn, tokensCachedIn, tokensOut, costSubcents int64
|
||||
|
||||
@@ -3,7 +3,6 @@ package sqlite
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestIncrementTaskSessionUsage_AccumulatesCachedInAcrossCalls confirms
|
||||
@@ -15,10 +14,10 @@ func TestIncrementTaskSessionUsage_AccumulatesCachedInAcrossCalls(t *testing.T)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-cached-usage", "sess-cached-usage", "turn-cached-usage")
|
||||
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-cached-usage", 100, 50_000_000, 200, 50); err != nil {
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-cached-usage", 100, 50_000_000, 200, 50); err != nil {
|
||||
t.Fatalf("first increment: %v", err)
|
||||
}
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-cached-usage", 10, 2_000, 20, 5); err != nil {
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-cached-usage", 10, 2_000, 20, 5); err != nil {
|
||||
t.Fatalf("second increment: %v", err)
|
||||
}
|
||||
|
||||
@@ -35,201 +34,36 @@ func TestIncrementTaskSessionUsage_AccumulatesCachedInAcrossCalls(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
// createOfficeCostEventsTable creates a minimal standalone copy of
|
||||
// office_cost_events (owned by internal/office/repository/sqlite) so this
|
||||
// package's migration tests can exercise the guarded backfill without
|
||||
// importing the office package (which would create an import cycle back
|
||||
// into internal/task). Column set mirrors office/repository/sqlite/base.go
|
||||
// createCostTables.
|
||||
func createOfficeCostEventsTable(t *testing.T, repo *Repository) {
|
||||
t.Helper()
|
||||
if _, err := repo.db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS office_cost_events (
|
||||
id TEXT PRIMARY KEY,
|
||||
session_id TEXT DEFAULT '',
|
||||
task_id TEXT DEFAULT '',
|
||||
agent_profile_id TEXT DEFAULT '',
|
||||
project_id TEXT DEFAULT '',
|
||||
model TEXT DEFAULT '',
|
||||
provider TEXT DEFAULT '',
|
||||
tokens_in INTEGER DEFAULT 0,
|
||||
tokens_cached_in INTEGER DEFAULT 0,
|
||||
tokens_out INTEGER DEFAULT 0,
|
||||
cost_subcents INTEGER NOT NULL DEFAULT 0,
|
||||
estimated INTEGER NOT NULL DEFAULT 0,
|
||||
occurred_at TIMESTAMP NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL
|
||||
)`); err != nil {
|
||||
t.Fatalf("create office_cost_events: %v", err)
|
||||
}
|
||||
}
|
||||
// TestGetTaskSessionAndListTaskSessions_SurfaceRollupColumns pins
|
||||
// docs/specs/task-cost-ledger/spec.md AC-28/AC-29: both session read paths
|
||||
// (GetTaskSession's single-row scan and ListTaskSessions' multi-row scan)
|
||||
// must return the four usage/cost rollup columns IncrementTaskSessionUsageTx
|
||||
// maintains, not just the underlying SQL row.
|
||||
func TestGetTaskSessionAndListTaskSessions_SurfaceRollupColumns(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-rollup-surface", "sess-rollup-surface", "turn-rollup-surface")
|
||||
|
||||
func insertOfficeCostEvent(t *testing.T, repo *Repository, id, sessionID string, tokensCachedIn int64) {
|
||||
t.Helper()
|
||||
now := time.Now().UTC()
|
||||
_, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO office_cost_events (id, session_id, tokens_cached_in, occurred_at, created_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
`), id, sessionID, tokensCachedIn, now, now)
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, nil, "sess-rollup-surface", 80, 8_203_943, 44979, 79118); err != nil {
|
||||
t.Fatalf("increment: %v", err)
|
||||
}
|
||||
|
||||
got, err := repo.GetTaskSession(ctx, "sess-rollup-surface")
|
||||
if err != nil {
|
||||
t.Fatalf("insert office_cost_event %s: %v", id, err)
|
||||
t.Fatalf("GetTaskSession: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBackfillSessionTokensCachedIn_SumsLedger proves the backfill invariant
|
||||
// directly: task_sessions.tokens_cached_in must equal the sum of
|
||||
// office_cost_events.tokens_cached_in for that session. This is the
|
||||
// reconciliation the card asked for, exercised at the SQL layer.
|
||||
func TestBackfillSessionTokensCachedIn_SumsLedger(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-backfill", "sess-backfill", "turn-backfill")
|
||||
createOfficeCostEventsTable(t, repo)
|
||||
insertOfficeCostEvent(t, repo, "cost-1", "sess-backfill", 98_805_109)
|
||||
insertOfficeCostEvent(t, repo, "cost-2", "sess-backfill", 1_194_891)
|
||||
// A row for a different session must not leak into this session's total.
|
||||
insertOfficeCostEvent(t, repo, "cost-other", "sess-other", 555)
|
||||
|
||||
if _, err := repo.BackfillSessionTokensCachedIn(ctx); err != nil {
|
||||
t.Fatalf("backfill: %v", err)
|
||||
if got.TokensIn != 80 || got.TokensCachedIn != 8_203_943 || got.TokensOut != 44979 || got.CostSubcents != 79118 {
|
||||
t.Errorf("GetTaskSession rollup = %+v, want (80, 8203943, 44979, 79118)", got)
|
||||
}
|
||||
|
||||
var got int64
|
||||
if err := repo.ro.QueryRowx(repo.ro.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-backfill",
|
||||
).Scan(&got); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if got != 100_000_000 {
|
||||
t.Errorf("tokens_cached_in = %d, want 100000000 (sum of office_cost_events for this session)", got)
|
||||
}
|
||||
|
||||
// Idempotent: re-running against an unchanged ledger must not
|
||||
// double-count the already-correct total.
|
||||
if _, err := repo.BackfillSessionTokensCachedIn(ctx); err != nil {
|
||||
t.Fatalf("second backfill: %v", err)
|
||||
}
|
||||
if err := repo.ro.QueryRowx(repo.ro.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-backfill",
|
||||
).Scan(&got); err != nil {
|
||||
t.Fatalf("read row after second pass: %v", err)
|
||||
}
|
||||
if got != 100_000_000 {
|
||||
t.Errorf("tokens_cached_in after second backfill pass = %d, want unchanged 100000000", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBackfillSessionTokensCachedIn_CorrectsDriftedRollup covers the case an
|
||||
// unconditional recompute exists to repair: a session whose rollup went out
|
||||
// of sync with the ledger without ever erroring. IncrementTaskSessionUsage
|
||||
// is a best-effort `UPDATE ... WHERE id = ?` that silently matches zero rows
|
||||
// if the session row doesn't exist yet (see
|
||||
// TestIncrementTaskSessionUsage_UnknownSessionNoError), and the office
|
||||
// subscriber only logs a Warn when that write fails while the ledger insert
|
||||
// has already committed (event_subscribers.go handlePromptUsage). A backfill
|
||||
// gated on "already nonzero" would treat this drifted-but-nonzero row as
|
||||
// already correct and skip it forever; the unconditional recompute must
|
||||
// instead overwrite it with the true ledger sum.
|
||||
func TestBackfillSessionTokensCachedIn_CorrectsDriftedRollup(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-drifted", "sess-drifted", "turn-drifted")
|
||||
createOfficeCostEventsTable(t, repo)
|
||||
insertOfficeCostEvent(t, repo, "cost-1", "sess-drifted", 98_805_109)
|
||||
insertOfficeCostEvent(t, repo, "cost-2", "sess-drifted", 1_194_891)
|
||||
|
||||
// Simulate the drift directly: the rollup already holds a nonzero but
|
||||
// wrong value, as it would after a missed increment.
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-drifted", 0, 2_000, 0, 0); err != nil {
|
||||
t.Fatalf("seed drifted rollup: %v", err)
|
||||
}
|
||||
|
||||
var before int64
|
||||
if err := repo.ro.QueryRowx(repo.ro.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-drifted",
|
||||
).Scan(&before); err != nil {
|
||||
t.Fatalf("read row before backfill: %v", err)
|
||||
}
|
||||
if before != 2_000 {
|
||||
t.Fatalf("test setup bug: rollup = %d, want the drifted seed value 2000", before)
|
||||
}
|
||||
|
||||
if _, err := repo.BackfillSessionTokensCachedIn(ctx); err != nil {
|
||||
t.Fatalf("backfill: %v", err)
|
||||
}
|
||||
|
||||
var got int64
|
||||
if err := repo.ro.QueryRowx(repo.ro.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-drifted",
|
||||
).Scan(&got); err != nil {
|
||||
t.Fatalf("read row after backfill: %v", err)
|
||||
}
|
||||
if got != 100_000_000 {
|
||||
t.Errorf("tokens_cached_in after backfill = %d, want 100000000 (the drifted 2000 must be "+
|
||||
"overwritten with the full ledger sum, not skipped as already-nonzero)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBackfillSessionTokensCachedIn_ZeroesRollupWithNoMatchingLedgerRows pins
|
||||
// the behaviour when the ledger table exists but has no rows for a session
|
||||
// whose rollup is already nonzero (e.g. the ledger rows for that session were
|
||||
// deleted separately from the session itself - see workspace_deletion.go,
|
||||
// which deletes office_cost_events before the task-side workspace in two
|
||||
// separate transactions). The unconditional recompute is a COALESCE(...,0),
|
||||
// so it deliberately zeroes tokens_cached_in in this case rather than leaving
|
||||
// the stale value: the rollup is supposed to always equal the ledger sum for
|
||||
// that session, and an empty ledger sums to zero.
|
||||
func TestBackfillSessionTokensCachedIn_ZeroesRollupWithNoMatchingLedgerRows(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-orphaned", "sess-orphaned", "turn-orphaned")
|
||||
createOfficeCostEventsTable(t, repo)
|
||||
|
||||
// Rollup already holds a nonzero value, but no office_cost_events rows
|
||||
// exist for this session (simulates the ledger having been deleted).
|
||||
if err := repo.IncrementTaskSessionUsage(ctx, "sess-orphaned", 0, 12_345, 0, 0); err != nil {
|
||||
t.Fatalf("seed rollup: %v", err)
|
||||
}
|
||||
|
||||
if _, err := repo.BackfillSessionTokensCachedIn(ctx); err != nil {
|
||||
t.Fatalf("backfill: %v", err)
|
||||
}
|
||||
|
||||
var got int64
|
||||
if err := repo.ro.QueryRowx(repo.ro.Rebind(
|
||||
`SELECT tokens_cached_in FROM task_sessions WHERE id = ?`), "sess-orphaned",
|
||||
).Scan(&got); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if got != 0 {
|
||||
t.Errorf("tokens_cached_in = %d, want 0 (no matching ledger rows sums to zero)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBackfillSessionTokensCachedIn_SkipsAlreadyCorrectUntouchedSessions pins
|
||||
// the boot-cost optimization: a session with no office_cost_events rows and a
|
||||
// tokens_cached_in already at its default of 0 must be excluded by the
|
||||
// UPDATE's WHERE clause, not just happen to compute the same value. Proven
|
||||
// via the reported affected-row count (matches SQL UPDATE semantics on both
|
||||
// SQLite and Postgres: rows matched by WHERE, not rows whose value changed),
|
||||
// comparing a session the WHERE clause must match (has ledger rows) against
|
||||
// one it must not (no ledger rows, untouched default).
|
||||
func TestBackfillSessionTokensCachedIn_SkipsAlreadyCorrectUntouchedSessions(t *testing.T) {
|
||||
repo := newRepoForSessionTests(t)
|
||||
ctx := context.Background()
|
||||
seedForMsgTest(t, repo, "task-untouched", "sess-untouched", "turn-untouched")
|
||||
seedForMsgTest(t, repo, "task-with-ledger", "sess-with-ledger", "turn-with-ledger")
|
||||
createOfficeCostEventsTable(t, repo)
|
||||
insertOfficeCostEvent(t, repo, "cost-1", "sess-with-ledger", 500)
|
||||
// sess-untouched: no office_cost_events rows, tokens_cached_in stays at
|
||||
// its untouched default of 0.
|
||||
|
||||
affected, err := repo.BackfillSessionTokensCachedIn(ctx)
|
||||
list, err := repo.ListTaskSessions(ctx, "task-rollup-surface")
|
||||
if err != nil {
|
||||
t.Fatalf("backfill: %v", err)
|
||||
t.Fatalf("ListTaskSessions: %v", err)
|
||||
}
|
||||
if affected != 1 {
|
||||
t.Errorf("rows affected = %d, want 1 (only sess-with-ledger should match the WHERE clause; "+
|
||||
"sess-untouched has no ledger rows and was already 0)", affected)
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("ListTaskSessions returned %d sessions, want 1", len(list))
|
||||
}
|
||||
if list[0].TokensIn != 80 || list[0].TokensCachedIn != 8_203_943 || list[0].TokensOut != 44979 || list[0].CostSubcents != 79118 {
|
||||
t.Errorf("ListTaskSessions rollup = %+v, want (80, 8203943, 44979, 79118)", list[0])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,305 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
|
||||
internaldb "github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/db/dialect"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
// usageEventTransientBackoffs is AC-32's fixed exponential backoff between
|
||||
// transient-error retry attempts: 50 ms then 100 ms, base 50 ms, factor 2, no
|
||||
// jitter. A single serial writer (AC-34) has no fleet of peers to
|
||||
// desynchronize from, so jitter would only add variance.
|
||||
var usageEventTransientBackoffs = [2]time.Duration{50 * time.Millisecond, 100 * time.Millisecond}
|
||||
|
||||
// maxUsageEventTransientAttempts is AC-32's cap on transient-classified
|
||||
// insert attempts (the whole AC-11 transaction, not the INSERT statement
|
||||
// alone) before giving up.
|
||||
const maxUsageEventTransientAttempts = 3
|
||||
|
||||
// CreateTaskUsageEvent inserts one task_usage_events row and atomically
|
||||
// increments the owning session's task_sessions rollup columns in the same
|
||||
// transaction (AC-11), classifying and reacting to insert failures per
|
||||
// AC-32:
|
||||
//
|
||||
// - a unique violation on usage_event_id is reported as
|
||||
// ErrDuplicateUsageEvent, with no retry and no rollup increment (AC-14,
|
||||
// AC-32) - this is the one insert failure that is an expected outcome,
|
||||
// not a fault;
|
||||
// - a foreign-key violation is retried exactly once with session_id
|
||||
// forced to "" (no rollup increment on that retry, since
|
||||
// IncrementTaskSessionUsageTx no-ops on an empty session id); a second
|
||||
// failure of any kind is returned as-is;
|
||||
// - a transient failure (serialization failure, deadlock, SQLITE_BUSY /
|
||||
// SQLITE_LOCKED) is retried in a fresh transaction, waiting per
|
||||
// usageEventTransientBackoffs between attempts, up to
|
||||
// maxUsageEventTransientAttempts total attempts;
|
||||
// - anything else is returned as-is, uncounted against either retry
|
||||
// budget.
|
||||
//
|
||||
// Before the first attempt, if event.SessionID is set, CreateTaskUsageEvent
|
||||
// verifies the session actually belongs to event.TaskID (AC-33): a
|
||||
// same-task session proceeds unchanged; a session belonging to a different
|
||||
// task is recorded under event.TaskID with session_id cleared and no rollup
|
||||
// increment - deliberately the same end state as the foreign-key retry
|
||||
// above, so the two share one reaction even though only the FK case is a
|
||||
// genuine driver-level retry. A session lookup that fails for any reason
|
||||
// other than "no such session" is returned immediately, uncounted against
|
||||
// either retry budget: it means the writer could not determine ownership at
|
||||
// all, not that a mismatch was found.
|
||||
func (r *Repository) CreateTaskUsageEvent(ctx context.Context, event *models.TaskUsageEvent) error {
|
||||
working := *event
|
||||
|
||||
if err := r.clearMismatchedUsageEventSession(ctx, &working); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fkRetried := false
|
||||
transientAttempts := 0
|
||||
|
||||
for {
|
||||
err := r.insertUsageEventAndRollup(ctx, &working)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch {
|
||||
case isUsageEventUniqueViolation(err):
|
||||
return ErrDuplicateUsageEvent
|
||||
case internaldb.IsForeignKeyViolation(err) && !fkRetried:
|
||||
fkRetried = true
|
||||
working.SessionID = ""
|
||||
continue
|
||||
case internaldb.IsTransientError(err):
|
||||
transientAttempts++
|
||||
if transientAttempts >= maxUsageEventTransientAttempts {
|
||||
return err
|
||||
}
|
||||
if sleepErr := sleepOrDone(ctx, usageEventTransientBackoffs[transientAttempts-1]); sleepErr != nil {
|
||||
return sleepErr
|
||||
}
|
||||
continue
|
||||
default:
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// clearMismatchedUsageEventSession implements AC-33's pre-insert ownership
|
||||
// check: if event.SessionID is set, it must belong to event.TaskID. A
|
||||
// same-task session (or a session that does not exist at all - the real
|
||||
// insert's foreign-key violation handles that case) is left unchanged; a
|
||||
// session belonging to a different task has its SessionID cleared, deliberately
|
||||
// the same terminal handling as the foreign-key retry in CreateTaskUsageEvent's
|
||||
// loop. A lookup error other than "no such session" is returned immediately,
|
||||
// uncounted against any retry budget: it means ownership could not be
|
||||
// determined at all, not that a mismatch was found.
|
||||
func (r *Repository) clearMismatchedUsageEventSession(ctx context.Context, event *models.TaskUsageEvent) error {
|
||||
if event.SessionID == "" {
|
||||
return nil
|
||||
}
|
||||
ownerTaskID, err := r.taskIDForSession(ctx, event.SessionID)
|
||||
switch {
|
||||
case err != nil && !errors.Is(err, sql.ErrNoRows):
|
||||
return fmt.Errorf("verify usage event session ownership: %w", err)
|
||||
case err == nil && ownerTaskID != event.TaskID:
|
||||
if r.log != nil {
|
||||
r.log.Warn("usage event session does not belong to its task; recording without session attribution",
|
||||
zap.String("usage_event_id", event.UsageEventID),
|
||||
zap.String("task_id", event.TaskID),
|
||||
zap.String("session_id", event.SessionID),
|
||||
zap.String("session_owner_task_id", ownerTaskID))
|
||||
}
|
||||
event.SessionID = ""
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sleepOrDone waits d, returning ctx.Err() early if ctx is cancelled first,
|
||||
// so a shutdown drain deadline (AC-34) can interrupt a queued backoff
|
||||
// instead of blocking the writer's worker goroutine past it.
|
||||
func sleepOrDone(ctx context.Context, d time.Duration) error {
|
||||
timer := time.NewTimer(d)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-timer.C:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// taskIDForSession returns the task_id owning sessionID, or sql.ErrNoRows if
|
||||
// no such session exists.
|
||||
func (r *Repository) taskIDForSession(ctx context.Context, sessionID string) (string, error) {
|
||||
var taskID string
|
||||
err := r.db.QueryRowxContext(ctx, r.db.Rebind(
|
||||
`SELECT task_id FROM task_sessions WHERE id = ?`), sessionID).Scan(&taskID)
|
||||
return taskID, err
|
||||
}
|
||||
|
||||
// insertUsageEventAndRollup is AC-11's atomic unit: the ledger insert and
|
||||
// the task_sessions rollup increment, applied in a single transaction so no
|
||||
// committed state exists in which one landed and the other did not. Returns
|
||||
// the raw, unclassified driver error on failure - classification and
|
||||
// reaction belong to CreateTaskUsageEvent.
|
||||
func (r *Repository) insertUsageEventAndRollup(ctx context.Context, event *models.TaskUsageEvent) error {
|
||||
if r.failUsageEventAttempts > 0 {
|
||||
r.failUsageEventAttempts--
|
||||
return r.failUsageEventErr
|
||||
}
|
||||
|
||||
tx, err := r.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback() }()
|
||||
|
||||
if err := r.insertUsageEventRowTx(ctx, tx, event); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r.failUsageEventRollupAttempts > 0 {
|
||||
r.failUsageEventRollupAttempts--
|
||||
return r.failUsageEventRollupErr
|
||||
}
|
||||
|
||||
if r.usageEventPreRollupHook != nil {
|
||||
r.usageEventPreRollupHook()
|
||||
}
|
||||
|
||||
if event.SessionID != "" {
|
||||
cachedIn := coalesceInt64(event.TokensCachedRead) + coalesceInt64(event.TokensCachedWrite)
|
||||
if err := r.IncrementTaskSessionUsageTx(ctx, tx, event.SessionID,
|
||||
event.TokensIn, cachedIn, coalesceInt64(event.TokensOut), event.CostSubcents); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// coalesceInt64 treats a nil *int64 as zero (AC-12): a not-recorded token
|
||||
// count contributes zero to the rollup rather than nulling out the whole
|
||||
// sum.
|
||||
func coalesceInt64(v *int64) int64 {
|
||||
if v == nil {
|
||||
return 0
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func (r *Repository) insertUsageEventRowTx(ctx context.Context, tx *sqlx.Tx, event *models.TaskUsageEvent) error {
|
||||
_, err := tx.ExecContext(ctx, r.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
rate_input_per_million, rate_cached_read_per_million,
|
||||
rate_cached_write_per_million, rate_output_per_million,
|
||||
pricing_catalog_version, contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`), event.UsageEventID, event.TaskID, nullableString(event.SessionID), nullableString(event.TurnID),
|
||||
event.AgentProfileID, event.AgentType, event.Model, event.Provider,
|
||||
event.TokensIn, event.TokensCachedRead, event.TokensCachedWrite, event.TokensOut, event.TokensThought,
|
||||
event.TokensTotal, event.CostSubcents, event.CostSource, dialect.BoolToInt(event.Estimated),
|
||||
event.RateInputPerMillion, event.RateCachedReadPerMillion,
|
||||
event.RateCachedWritePerMillion, event.RateOutputPerMillion,
|
||||
nullableString(event.PricingCatalogVersion), event.ContractVersion, event.OccurredAt, event.CreatedAt)
|
||||
return err
|
||||
}
|
||||
|
||||
// ListTaskUsageEvents returns taskID's ledger rows ordered by
|
||||
// (occurred_at, id) ascending (AC-16), which is a total order over the
|
||||
// table: no two rows compare equal, so repeated reads return an identical
|
||||
// sequence. limit <= 0 means no limit. An unknown task and a task with no
|
||||
// rows both return an empty, non-nil slice and a nil error.
|
||||
func (r *Repository) ListTaskUsageEvents(ctx context.Context, taskID string, limit int) ([]*models.TaskUsageEvent, error) {
|
||||
query := `
|
||||
SELECT id, usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
rate_input_per_million, rate_cached_read_per_million,
|
||||
rate_cached_write_per_million, rate_output_per_million,
|
||||
pricing_catalog_version, contract_version, occurred_at, created_at
|
||||
FROM task_usage_events
|
||||
WHERE task_id = ?
|
||||
ORDER BY occurred_at ASC, id ASC
|
||||
`
|
||||
args := []interface{}{taskID}
|
||||
if limit > 0 {
|
||||
query += ` LIMIT ?`
|
||||
args = append(args, limit)
|
||||
}
|
||||
|
||||
rows, err := r.ro.QueryxContext(ctx, r.ro.Rebind(query), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
events := make([]*models.TaskUsageEvent, 0)
|
||||
for rows.Next() {
|
||||
event, err := scanTaskUsageEventRow(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
events = append(events, event)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func scanTaskUsageEventRow(row *sqlx.Rows) (*models.TaskUsageEvent, error) {
|
||||
var (
|
||||
event models.TaskUsageEvent
|
||||
sessionID, turnID, pricingCatalogVersion sql.NullString
|
||||
tokensCachedRead, tokensCachedWrite, tokensOut, tokensThought sql.NullInt64
|
||||
rateInput, rateCachedRead, rateCachedWrite, rateOutput sql.NullInt64
|
||||
)
|
||||
if err := row.Scan(
|
||||
&event.ID, &event.UsageEventID, &event.TaskID, &sessionID, &turnID,
|
||||
&event.AgentProfileID, &event.AgentType, &event.Model, &event.Provider,
|
||||
&event.TokensIn, &tokensCachedRead, &tokensCachedWrite, &tokensOut, &tokensThought,
|
||||
&event.TokensTotal, &event.CostSubcents, &event.CostSource, &event.Estimated,
|
||||
&rateInput, &rateCachedRead, &rateCachedWrite, &rateOutput,
|
||||
&pricingCatalogVersion, &event.ContractVersion, &event.OccurredAt, &event.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
event.SessionID = sessionID.String
|
||||
event.TurnID = turnID.String
|
||||
event.PricingCatalogVersion = pricingCatalogVersion.String
|
||||
event.TokensCachedRead = nullInt64ToPtr(tokensCachedRead)
|
||||
event.TokensCachedWrite = nullInt64ToPtr(tokensCachedWrite)
|
||||
event.TokensOut = nullInt64ToPtr(tokensOut)
|
||||
event.TokensThought = nullInt64ToPtr(tokensThought)
|
||||
event.RateInputPerMillion = nullInt64ToPtr(rateInput)
|
||||
event.RateCachedReadPerMillion = nullInt64ToPtr(rateCachedRead)
|
||||
event.RateCachedWritePerMillion = nullInt64ToPtr(rateCachedWrite)
|
||||
event.RateOutputPerMillion = nullInt64ToPtr(rateOutput)
|
||||
|
||||
return &event, nil
|
||||
}
|
||||
|
||||
func nullInt64ToPtr(v sql.NullInt64) *int64 {
|
||||
if !v.Valid {
|
||||
return nil
|
||||
}
|
||||
value := v.Int64
|
||||
return &value
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package sqlite
|
||||
|
||||
// Non-transient rollup-failure atomicity for CreateTaskUsageEvent
|
||||
// (docs/specs/task-cost-ledger/spec.md AC-11, AC-32(d), AC-36): the ledger
|
||||
// insert and the task_sessions rollup increment are applied in one
|
||||
// transaction, so a failure on either side rolls both back rather than
|
||||
// leaving a partial write. This is the ledger-writer replacement for the
|
||||
// deleted office/service test
|
||||
// TestPromptUsage_RollupFailureRollsBackLedgerInsertThenRetrySucceeds (AC-21,
|
||||
// AC-36): the recovery path PR #2606 review asked to see covered - force a
|
||||
// rollup failure, then redeliver the same usage_event_id - now proven at the
|
||||
// writer that actually owns the transaction, since internal/task/usage's
|
||||
// Writer no longer shares one with an Office caller.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateTaskUsageEvent_NonTransientFailureRollsBackThenRedeliveryOfSameIDSucceeds(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-nontransient")
|
||||
createUsageEventsTestSession(t, repo, "session-nontransient", "task-nontransient")
|
||||
|
||||
repo.failUsageEventRollupAttempts = 1
|
||||
repo.failUsageEventRollupErr = errors.New("injected rollup failure")
|
||||
|
||||
event := newTestUsageEvent("evt-nontransient", "task-nontransient", "session-nontransient")
|
||||
|
||||
// First delivery: the failpoint fires after the ledger row insert has
|
||||
// already been attempted inside the transaction, but before the rollup
|
||||
// increment - an error that is neither a duplicate, a foreign-key
|
||||
// violation, nor transient, so CreateTaskUsageEvent returns it
|
||||
// immediately with no internal retry. Because the insert and the rollup
|
||||
// share one transaction, the deferred rollback unwinds the already
|
||||
// -attempted insert along with everything else; a non-atomic
|
||||
// implementation that committed the insert in its own transaction before
|
||||
// attempting the rollup would leave the row behind here.
|
||||
err := repo.CreateTaskUsageEvent(context.Background(), event)
|
||||
if err == nil {
|
||||
t.Fatal("expected the injected rollup failure to surface, got nil")
|
||||
}
|
||||
if errors.Is(err, ErrDuplicateUsageEvent) {
|
||||
t.Fatalf("error = ErrDuplicateUsageEvent, want the raw injected failure")
|
||||
}
|
||||
if got := countTaskUsageEventRows(t, repo); got != 0 {
|
||||
t.Fatalf("row count after failed attempt = %d, want 0 (ledger insert must roll back with the failed rollup)", got)
|
||||
}
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := readTaskSessionRollup(t, repo, "session-nontransient")
|
||||
if tokensIn != 0 || tokensCachedIn != 0 || tokensOut != 0 || costSubcents != 0 {
|
||||
t.Fatalf("rollup after failed attempt = (%d,%d,%d,%d), want all zero: a rolled-back attempt must not partially apply",
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
|
||||
// Redelivery of the same usage_event_id, this time with nothing armed to
|
||||
// fail. Because nothing committed on the first attempt, the unique index
|
||||
// must not treat this as a duplicate.
|
||||
redelivered := newTestUsageEvent("evt-nontransient", "task-nontransient", "session-nontransient")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), redelivered); err != nil {
|
||||
t.Fatalf("redelivered CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 1 {
|
||||
t.Fatalf("row count after retry = %d, want 1", got)
|
||||
}
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents = readTaskSessionRollup(t, repo, "session-nontransient")
|
||||
if tokensIn != 100 || tokensCachedIn != 25 || tokensOut != 30 || costSubcents != 42 {
|
||||
t.Errorf("rollup after retry = (%d,%d,%d,%d), want (100,25,30,42)", tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package sqlite
|
||||
|
||||
// TestPostgresCreateTaskUsageEvent_RealDeadlockIsClassifiedTransientAndRetried
|
||||
// closes Review Round 2's M2 finding: the other AC-32 transient-retry tests
|
||||
// (usage_events_transient_test.go) only prove the retry loop's control flow
|
||||
// against an injected failpoint error, never against a genuine
|
||||
// driver-reported 40001/40P01. internal/db's classifier already has focused
|
||||
// unit coverage for those codes via synthetic *pgconn.PgError values
|
||||
// (migration_errors_test.go); what was missing was proof that a real
|
||||
// Postgres deadlock, raised against the exact unmodified production write
|
||||
// path, actually reaches that classifier and gets retried rather than
|
||||
// surfaced as a fatal error.
|
||||
//
|
||||
// Constructing a genuine deadlock here needs two real, separate connections
|
||||
// (SQLite's single test connection can't produce true concurrent contention)
|
||||
// and a lock-escalation pattern that survives Postgres's row-lock
|
||||
// compatibility rules:
|
||||
//
|
||||
// - CreateTaskUsageEvent's ledger INSERT references task_sessions via a
|
||||
// foreign key, which takes FOR KEY SHARE on the referenced session row.
|
||||
// - Its rollup UPDATE (IncrementTaskSessionUsageTx) never touches the
|
||||
// session's primary key, so it only ever requests FOR NO KEY UPDATE -
|
||||
// the weakest lock that still conflicts with a concurrent FOR NO KEY
|
||||
// UPDATE (row-lock modes, strongest to weakest: FOR UPDATE > FOR NO KEY
|
||||
// UPDATE > FOR SHARE > FOR KEY SHARE; FOR NO KEY UPDATE conflicts with
|
||||
// itself but not with FOR KEY SHARE).
|
||||
// - A plain antagonist UPDATE that also avoids the primary key only ever
|
||||
// takes FOR NO KEY UPDATE too, which is compatible with the production
|
||||
// transaction's held FOR KEY SHARE - it will not deadlock with N
|
||||
// concurrent real writers alone. The antagonist here deliberately
|
||||
// escalates to FOR UPDATE via a second, explicit `SELECT ... FOR
|
||||
// UPDATE`, which does conflict with production's held FOR KEY SHARE,
|
||||
// closing the cycle.
|
||||
//
|
||||
// The hook is paused only on the antagonist's side of the transaction
|
||||
// timeline: production's ledger insert runs first and pauses inside
|
||||
// insertUsageEventAndRollup (before the rollup UPDATE) so the antagonist's
|
||||
// first statement can be issued and its lock granted; production is then
|
||||
// released into its rollup UPDATE (which blocks on the antagonist's FOR NO
|
||||
// KEY UPDATE) before the antagonist's FOR UPDATE is issued (which then
|
||||
// blocks on production's held FOR KEY SHARE) - so production's wait
|
||||
// registers first and Postgres's deadlock detector reliably picks it as the
|
||||
// victim, exercising exactly the retry path AC-32(c) describes. Verified via
|
||||
// the real Postgres server log during development: a genuine `deadlock
|
||||
// detected` (40P01) naming production's own rollup UPDATE statement as one
|
||||
// of the two waiters.
|
||||
//
|
||||
// Skips unless KANDEV_TEST_POSTGRES_DSN is set.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/kandev/kandev/internal/testutil"
|
||||
)
|
||||
|
||||
func TestPostgresCreateTaskUsageEvent_RealDeadlockIsClassifiedTransientAndRetried(t *testing.T) {
|
||||
dsn := testutil.PostgresDSNFromEnv(t)
|
||||
db := testutil.OpenIsolatedPostgres(t, dsn)
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-deadlock-pg", "session-deadlock-pg")
|
||||
|
||||
// The antagonist needs its own physical connection: repo.db (used by
|
||||
// testutil.OpenIsolatedPostgres) caps MaxOpenConns at 1, so sharing it
|
||||
// would starve production of a connection entirely instead of
|
||||
// constructing genuine cross-connection Postgres lock contention.
|
||||
var searchPath string
|
||||
if err := repo.db.Get(&searchPath, `SHOW search_path`); err != nil {
|
||||
t.Fatalf("show search_path: %v", err)
|
||||
}
|
||||
antagonistDB, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("open antagonist connection: %v", err)
|
||||
}
|
||||
defer func() { _ = antagonistDB.Close() }()
|
||||
if _, err := antagonistDB.Exec("SET search_path TO " + searchPath); err != nil {
|
||||
t.Fatalf("set antagonist search_path: %v", err)
|
||||
}
|
||||
|
||||
antagonistTx, err := antagonistDB.BeginTx(context.Background(), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("begin antagonist transaction: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = antagonistTx.Rollback() })
|
||||
if _, err := antagonistTx.Exec(
|
||||
`UPDATE task_sessions SET tokens_in = tokens_in WHERE id = $1`, "session-deadlock-pg",
|
||||
); err != nil {
|
||||
t.Fatalf("antagonist FOR NO KEY UPDATE statement: %v", err)
|
||||
}
|
||||
|
||||
insertedCh := make(chan struct{})
|
||||
releaseCh := make(chan struct{})
|
||||
attempts := 0
|
||||
var hookPaused bool
|
||||
repo.usageEventPreRollupHook = func() {
|
||||
attempts++
|
||||
// Only pause on the first attempt: if production loses the
|
||||
// deadlock and retries, insertUsageEventAndRollup calls this hook
|
||||
// again on every subsequent attempt, and closing insertedCh a
|
||||
// second time would panic.
|
||||
if hookPaused {
|
||||
return
|
||||
}
|
||||
hookPaused = true
|
||||
close(insertedCh)
|
||||
<-releaseCh
|
||||
}
|
||||
|
||||
mainErrCh := make(chan error, 1)
|
||||
go func() {
|
||||
event := newTestUsageEvent("evt-deadlock-pg", "task-deadlock-pg", "session-deadlock-pg")
|
||||
mainErrCh <- repo.CreateTaskUsageEvent(context.Background(), event)
|
||||
}()
|
||||
|
||||
<-insertedCh
|
||||
close(releaseCh) // production proceeds into its rollup UPDATE, blocking on the antagonist's held FOR NO KEY UPDATE
|
||||
|
||||
time.Sleep(200 * time.Millisecond) // ensure production's wait registers before the antagonist's escalation
|
||||
|
||||
antagonistErrCh := make(chan error, 1)
|
||||
go func() {
|
||||
_, escalateErr := antagonistTx.Exec(`SELECT id FROM task_sessions WHERE id = $1 FOR UPDATE`, "session-deadlock-pg")
|
||||
antagonistErrCh <- escalateErr
|
||||
}()
|
||||
|
||||
var mainErr, antagonistErr error
|
||||
var mainDone, antagonistDone bool
|
||||
for !mainDone || !antagonistDone {
|
||||
select {
|
||||
case mainErr = <-mainErrCh:
|
||||
mainDone = true
|
||||
case antagonistErr = <-antagonistErrCh:
|
||||
antagonistDone = true
|
||||
// Release the antagonist's transaction the instant its own call
|
||||
// returns rather than after production's: if the deadlock
|
||||
// detector picks production as the victim, production's retry
|
||||
// needs this FOR UPDATE lock released to proceed, so waiting on
|
||||
// mainErrCh first would self-deadlock the test harness (not
|
||||
// Postgres).
|
||||
if antagonistErr != nil {
|
||||
_ = antagonistTx.Rollback()
|
||||
} else {
|
||||
_ = antagonistTx.Commit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if mainErr != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent must transparently retry past a real deadlock, got: %v", mainErr)
|
||||
}
|
||||
if attempts < 2 {
|
||||
t.Fatalf("insertUsageEventAndRollup ran %d attempt(s), want at least 2 - "+
|
||||
"either no real deadlock occurred or the transient retry did not fire", attempts)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM task_usage_events`); err != nil {
|
||||
t.Fatalf("count rows: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("row count = %d, want 1 (the aborted first attempt must not leave a row)", count)
|
||||
}
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := readTaskSessionRollup(t, repo, "session-deadlock-pg")
|
||||
if tokensIn != 100 || tokensCachedIn != 25 || tokensOut != 30 || costSubcents != 42 {
|
||||
t.Errorf("rollup = (%d,%d,%d,%d), want (100,25,30,42) - "+
|
||||
"the aborted first attempt must not have partially or doubly applied", tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package sqlite
|
||||
|
||||
// Behavioral coverage for ListTaskUsageEvents (docs/specs/task-cost-ledger/spec.md
|
||||
// AC-16): (occurred_at, id) ascending total order, limit semantics, and the
|
||||
// empty-non-nil-slice contract for an unknown task or a task with no rows.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
func TestListTaskUsageEvents_OrdersByOccurredAtThenID(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-order")
|
||||
|
||||
base := time.Date(2026, 8, 23, 12, 0, 0, 0, time.UTC)
|
||||
// Two rows share the same occurred_at, so only insertion-order id can
|
||||
// break the tie; the third row is deliberately inserted with an earlier
|
||||
// occurred_at than the first two, to prove ordering is by occurred_at
|
||||
// first, not by insertion/id order.
|
||||
first := newTestUsageEvent("evt-order-a", "task-order", "")
|
||||
first.OccurredAt = base
|
||||
second := newTestUsageEvent("evt-order-b", "task-order", "")
|
||||
second.OccurredAt = base
|
||||
third := newTestUsageEvent("evt-order-c", "task-order", "")
|
||||
third.OccurredAt = base.Add(-time.Hour)
|
||||
|
||||
for _, event := range []*models.TaskUsageEvent{first, second, third} {
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent(%s): %v", event.UsageEventID, err)
|
||||
}
|
||||
}
|
||||
|
||||
events, err := repo.ListTaskUsageEvents(context.Background(), "task-order", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents: %v", err)
|
||||
}
|
||||
if len(events) != 3 {
|
||||
t.Fatalf("len(events) = %d, want 3", len(events))
|
||||
}
|
||||
wantOrder := []string{"evt-order-c", "evt-order-a", "evt-order-b"}
|
||||
for i, want := range wantOrder {
|
||||
if events[i].UsageEventID != want {
|
||||
t.Errorf("events[%d].UsageEventID = %q, want %q", i, events[i].UsageEventID, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListTaskUsageEvents_LimitNonPositiveMeansNoLimit(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-nolimit")
|
||||
for _, id := range []string{"evt-nl-1", "evt-nl-2", "evt-nl-3"} {
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), newTestUsageEvent(id, "task-nolimit", "")); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent(%s): %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, limit := range []int{0, -1} {
|
||||
events, err := repo.ListTaskUsageEvents(context.Background(), "task-nolimit", limit)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents(limit=%d): %v", limit, err)
|
||||
}
|
||||
if len(events) != 3 {
|
||||
t.Errorf("ListTaskUsageEvents(limit=%d) len = %d, want 3", limit, len(events))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListTaskUsageEvents_LimitAppliedInAscendingOrder(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-limit")
|
||||
|
||||
base := time.Date(2026, 8, 23, 12, 0, 0, 0, time.UTC)
|
||||
for i, id := range []string{"evt-lim-1", "evt-lim-2", "evt-lim-3"} {
|
||||
event := newTestUsageEvent(id, "task-limit", "")
|
||||
event.OccurredAt = base.Add(time.Duration(i) * time.Minute)
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent(%s): %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
events, err := repo.ListTaskUsageEvents(context.Background(), "task-limit", 2)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents: %v", err)
|
||||
}
|
||||
if len(events) != 2 {
|
||||
t.Fatalf("len(events) = %d, want 2", len(events))
|
||||
}
|
||||
if events[0].UsageEventID != "evt-lim-1" || events[1].UsageEventID != "evt-lim-2" {
|
||||
t.Errorf("events = [%s, %s], want [evt-lim-1, evt-lim-2]", events[0].UsageEventID, events[1].UsageEventID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListTaskUsageEvents_UnknownTask_ReturnsEmptyNonNilSlice(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
|
||||
events, err := repo.ListTaskUsageEvents(context.Background(), "task-does-not-exist", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents: %v", err)
|
||||
}
|
||||
if events == nil {
|
||||
t.Fatal("events = nil, want a non-nil empty slice")
|
||||
}
|
||||
if len(events) != 0 {
|
||||
t.Errorf("len(events) = %d, want 0", len(events))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListTaskUsageEvents_TaskWithNoRows_ReturnsEmptyNonNilSlice(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-empty")
|
||||
|
||||
events, err := repo.ListTaskUsageEvents(context.Background(), "task-empty", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents: %v", err)
|
||||
}
|
||||
if events == nil {
|
||||
t.Fatal("events = nil, want a non-nil empty slice")
|
||||
}
|
||||
if len(events) != 0 {
|
||||
t.Errorf("len(events) = %d, want 0", len(events))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package sqlite
|
||||
|
||||
// AC-28 coverage: task_sessions.cost_subcents, .tokens_in and .tokens_out
|
||||
// must be BIGINT on Postgres, mirroring the same 64-bit rule as
|
||||
// task_usage_events and the already-BIGINT tokens_cached_in. SQLite's
|
||||
// INTEGER is already 64-bit, so this dialect-sensitive widening is only
|
||||
// observable against a real Postgres instance.
|
||||
//
|
||||
// Skips unless KANDEV_TEST_POSTGRES_DSN is set.
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/testutil"
|
||||
)
|
||||
|
||||
func postgresColumnDataType(t *testing.T, repo *Repository, table, column string) string {
|
||||
t.Helper()
|
||||
var dataType string
|
||||
err := repo.db.Get(&dataType, `
|
||||
SELECT data_type
|
||||
FROM information_schema.columns
|
||||
WHERE table_schema = current_schema()
|
||||
AND table_name = $1
|
||||
AND column_name = $2
|
||||
`, table, column)
|
||||
if err != nil {
|
||||
t.Fatalf("inspect %s.%s type: %v", table, column, err)
|
||||
}
|
||||
return dataType
|
||||
}
|
||||
|
||||
// TestPostgresTaskSessionsRollupColumns_WidenedToBigint proves a fresh
|
||||
// Postgres database ends up with all three columns as BIGINT after schema
|
||||
// init runs migrateSessionsAddCostColumns (creates them as INTEGER) followed
|
||||
// by migrateTaskSessionsRollupColumnsToBigint (widens them).
|
||||
func TestPostgresTaskSessionsRollupColumns_WidenedToBigint(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
|
||||
for _, column := range []string{"cost_subcents", "tokens_in", "tokens_out"} {
|
||||
if got := postgresColumnDataType(t, repo, "task_sessions", column); got != "bigint" {
|
||||
t.Errorf("task_sessions.%s data type = %q, want bigint", column, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostgresTaskSessionsRollupColumns_LegacyIntegerSurvivesWidening
|
||||
// simulates a pre-AC-28 database where these columns are still INTEGER
|
||||
// (int4), and proves the migration widens them in place without erroring and
|
||||
// without losing existing data - the same table-rebuild-avoidance shape as
|
||||
// the priority-to-text migration.
|
||||
func TestPostgresTaskSessionsRollupColumns_LegacyIntegerSurvivesWidening(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
|
||||
// Rewind to a pre-AC-28 schema: narrow the columns back to INTEGER, as a
|
||||
// legacy database would have them before this migration ever ran.
|
||||
for _, column := range []string{"cost_subcents", "tokens_in", "tokens_out"} {
|
||||
if _, err := repo.db.Exec(`ALTER TABLE task_sessions ALTER COLUMN ` + column + ` TYPE INTEGER`); err != nil {
|
||||
t.Fatalf("rewind %s to integer: %v", column, err)
|
||||
}
|
||||
}
|
||||
|
||||
seedPostgresTaskSession(t, repo, "task-legacy-widen-pg", "session-legacy-widen-pg")
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(`
|
||||
UPDATE task_sessions SET cost_subcents = ?, tokens_in = ?, tokens_out = ?
|
||||
WHERE id = ?
|
||||
`), int64(123_456), int64(789), int64(321), "session-legacy-widen-pg"); err != nil {
|
||||
t.Fatalf("seed legacy values: %v", err)
|
||||
}
|
||||
|
||||
// Re-run the migration as a new-binary boot would.
|
||||
repo.migrateTaskSessionsRollupColumnsToBigint()
|
||||
|
||||
for _, column := range []string{"cost_subcents", "tokens_in", "tokens_out"} {
|
||||
if got := postgresColumnDataType(t, repo, "task_sessions", column); got != "bigint" {
|
||||
t.Errorf("task_sessions.%s data type after re-migration = %q, want bigint", column, got)
|
||||
}
|
||||
}
|
||||
|
||||
var costSubcents, tokensIn, tokensOut int64
|
||||
if err := repo.db.QueryRowx(repo.db.Rebind(
|
||||
`SELECT cost_subcents, tokens_in, tokens_out FROM task_sessions WHERE id = ?`), "session-legacy-widen-pg",
|
||||
).Scan(&costSubcents, &tokensIn, &tokensOut); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if costSubcents != 123_456 || tokensIn != 789 || tokensOut != 321 {
|
||||
t.Errorf("values after widening = (%d,%d,%d), want (123456,789,321) - existing data must survive",
|
||||
costSubcents, tokensIn, tokensOut)
|
||||
}
|
||||
|
||||
// The scenario this covers: a value beyond int4's ceiling must now commit
|
||||
// where a pre-widening column would reject it with "integer out of range".
|
||||
const beyondInt32 = int64(9_000_000_000)
|
||||
if _, err := repo.db.Exec(repo.db.Rebind(
|
||||
`UPDATE task_sessions SET cost_subcents = ? WHERE id = ?`), beyondInt32, "session-legacy-widen-pg",
|
||||
); err != nil {
|
||||
t.Fatalf("update with a value beyond int32 range must commit on a BIGINT column: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"github.com/kandev/kandev/internal/db/dialect"
|
||||
)
|
||||
|
||||
// migrateTaskSessionsRollupColumnsToBigint widens
|
||||
// task_sessions.cost_subcents, .tokens_in and .tokens_out from INTEGER to
|
||||
// BIGINT (AC-28, docs/specs/task-cost-ledger/spec.md), so all four rollup
|
||||
// columns - these three plus the already-BIGINT tokens_cached_in - satisfy
|
||||
// the same 64-bit rule as task_usage_events. Idempotent and dialect-aware in
|
||||
// the established runMigrations() shape: on SQLite this is a no-op, because
|
||||
// SQLite's INTEGER is already 64-bit; on Postgres, widening INTEGER (or
|
||||
// SMALLINT) to BIGINT is a lossless type change that Postgres accepts with no
|
||||
// USING clause, and re-running ALTER COLUMN ... TYPE BIGINT against a column
|
||||
// that is already BIGINT is itself a no-op, so no pre-check is needed to make
|
||||
// replay safe.
|
||||
func (r *Repository) migrateTaskSessionsRollupColumnsToBigint() {
|
||||
if !dialect.IsPostgres(r.db.DriverName()) {
|
||||
return
|
||||
}
|
||||
r.migrate.Apply("task_sessions.cost_subcents.bigint",
|
||||
`ALTER TABLE task_sessions ALTER COLUMN cost_subcents TYPE BIGINT`)
|
||||
r.migrate.Apply("task_sessions.tokens_in.bigint",
|
||||
`ALTER TABLE task_sessions ALTER COLUMN tokens_in TYPE BIGINT`)
|
||||
r.migrate.Apply("task_sessions.tokens_out.bigint",
|
||||
`ALTER TABLE task_sessions ALTER COLUMN tokens_out TYPE BIGINT`)
|
||||
}
|
||||
@@ -0,0 +1,222 @@
|
||||
package sqlite
|
||||
|
||||
// Postgres parity coverage for CreateTaskUsageEvent (docs/specs/task-cost-ledger/spec.md
|
||||
// AC-11, AC-32): the insert-failure classification calls internaldb.IsForeignKeyViolation
|
||||
// and internaldb.IsTransientError, which branch on a typed pgconn.PgError on
|
||||
// Postgres versus a string-matched go-sqlite3 error on SQLite - this file
|
||||
// proves the classification (and the resulting retry behavior) also holds
|
||||
// against a real Postgres driver error, not just SQLite's.
|
||||
//
|
||||
// Skips unless KANDEV_TEST_POSTGRES_DSN is set.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/testutil"
|
||||
)
|
||||
|
||||
func TestPostgresCreateTaskUsageEvent_HappyPath_InsertsRowAndIncrementsRollup(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-happy-pg", "session-happy-pg")
|
||||
|
||||
event := newTestUsageEvent("evt-happy-pg", "task-happy-pg", "session-happy-pg")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM task_usage_events`); err != nil {
|
||||
t.Fatalf("count rows: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("row count = %d, want 1", count)
|
||||
}
|
||||
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := readTaskSessionRollup(t, repo, "session-happy-pg")
|
||||
if tokensIn != 100 || tokensCachedIn != 25 || tokensOut != 30 || costSubcents != 42 {
|
||||
t.Errorf("rollup = (%d,%d,%d,%d), want (100,25,30,42)", tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostgresListTaskUsageEvents_EstimatedRoundTrips pins the INTEGER-to-bool
|
||||
// scan used by the read path. The insert stores the flag as 0/1 for both
|
||||
// dialects, so this keeps the Postgres driver conversion explicit.
|
||||
func TestPostgresListTaskUsageEvents_EstimatedRoundTrips(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTask(t, repo, "task-estimated-pg")
|
||||
|
||||
event := newTestUsageEvent("evt-estimated-pg", "task-estimated-pg", "")
|
||||
event.Estimated = true
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
events, err := repo.ListTaskUsageEvents(context.Background(), "task-estimated-pg", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents: %v", err)
|
||||
}
|
||||
if len(events) != 1 || !events[0].Estimated {
|
||||
t.Fatalf("events = %+v, want one estimated event", events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresCreateTaskUsageEvent_DuplicateUsageEventID_ReturnsErrDuplicateNoRollup(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-dup-pg", "session-dup-create-pg")
|
||||
|
||||
first := newTestUsageEvent("evt-dup-create-pg", "task-dup-pg", "session-dup-create-pg")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), first); err != nil {
|
||||
t.Fatalf("first CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
redelivered := newTestUsageEvent("evt-dup-create-pg", "task-dup-pg", "session-dup-create-pg")
|
||||
err = repo.CreateTaskUsageEvent(context.Background(), redelivered)
|
||||
if err != ErrDuplicateUsageEvent {
|
||||
t.Fatalf("redelivered CreateTaskUsageEvent error = %v, want ErrDuplicateUsageEvent", err)
|
||||
}
|
||||
|
||||
tokensIn, _, _, _ := readTaskSessionRollup(t, repo, "session-dup-create-pg")
|
||||
if tokensIn != 100 {
|
||||
t.Errorf("tokens_in = %d, want 100 (redelivery must not double-increment the rollup)", tokensIn)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresCreateTaskUsageEvent_SessionForeignKeyViolation_RetriesOnceWithSessionCleared(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTask(t, repo, "task-fk-retry-pg")
|
||||
|
||||
event := newTestUsageEvent("evt-fk-retry-pg", "task-fk-retry-pg", "session-does-not-exist-pg")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
var sessionID *string
|
||||
if err := repo.db.QueryRowx(repo.db.Rebind(
|
||||
`SELECT session_id FROM task_usage_events WHERE usage_event_id = ?`), "evt-fk-retry-pg",
|
||||
).Scan(&sessionID); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if sessionID != nil {
|
||||
t.Errorf("session_id = %v, want nil (FK retry must clear it)", *sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresCreateTaskUsageEvent_SecondForeignKeyFailure_ReturnsError(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
|
||||
event := newTestUsageEvent("evt-fk-double-pg", "task-does-not-exist-pg", "session-does-not-exist-pg")
|
||||
createErr := repo.CreateTaskUsageEvent(context.Background(), event)
|
||||
if createErr == nil {
|
||||
t.Fatal("expected an error when both task_id and session_id are invalid, got nil")
|
||||
}
|
||||
if createErr == ErrDuplicateUsageEvent {
|
||||
t.Fatalf("error = ErrDuplicateUsageEvent, want a foreign-key failure")
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM task_usage_events`); err != nil {
|
||||
t.Fatalf("count rows: %v", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Errorf("row count = %d, want 0 (both attempts must roll back)", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostgresCreateTaskUsageEvent_TokensOutCrossesInt32ThroughRealWriter
|
||||
// covers AC-28 for tokens_out specifically: session_usage_postgres_test.go
|
||||
// only pushes tokens_cached_in (already BIGINT before this feature) past
|
||||
// int32, and TestPostgresTaskSessionsRollupColumns_LegacyIntegerSurvivesWidening
|
||||
// pushes cost_subcents past int32 via a raw UPDATE that bypasses the ledger
|
||||
// writer entirely - a version of either test passing before the migration
|
||||
// widened tokens_out/cost_subcents to BIGINT would prove nothing about this
|
||||
// column. This test seeds the rollup near the int32 ceiling via the real
|
||||
// IncrementTaskSessionUsageTx path, then commits a further ledger event
|
||||
// through CreateTaskUsageEvent (the insert+rollup transaction AC-11
|
||||
// requires) whose tokens_out delta crosses the boundary, and asserts that
|
||||
// transaction commits with the correct summed value.
|
||||
func TestPostgresCreateTaskUsageEvent_TokensOutCrossesInt32ThroughRealWriter(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-out-overflow-pg", "session-out-overflow-pg")
|
||||
|
||||
const nearInt32Ceiling = int64(2_147_483_600) // int32 max is 2,147,483,647
|
||||
if err := repo.IncrementTaskSessionUsageTx(
|
||||
context.Background(), nil, "session-out-overflow-pg", 0, 0, nearInt32Ceiling, 0,
|
||||
); err != nil {
|
||||
t.Fatalf("seed rollup near int32 ceiling: %v", err)
|
||||
}
|
||||
|
||||
event := newTestUsageEvent("evt-out-overflow-pg", "task-out-overflow-pg", "session-out-overflow-pg")
|
||||
crossingDelta := int64(200)
|
||||
event.TokensOut = &crossingDelta
|
||||
event.TokensTotal = event.TokensIn + crossingDelta
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent must not error when tokens_out crosses int32: %v", err)
|
||||
}
|
||||
|
||||
_, _, tokensOut, _ := readTaskSessionRollup(t, repo, "session-out-overflow-pg")
|
||||
want := nearInt32Ceiling + crossingDelta
|
||||
if tokensOut != want {
|
||||
t.Errorf("tokens_out = %d, want %d (a delta crossing int32 must commit through the real writer)",
|
||||
tokensOut, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostgresCreateTaskUsageEvent_CostSubcentsCrossesInt32ThroughRealWriter
|
||||
// is TestPostgresCreateTaskUsageEvent_TokensOutCrossesInt32ThroughRealWriter's
|
||||
// twin for cost_subcents, closing the other half of the gap: the only
|
||||
// existing beyond-int32 coverage for this column
|
||||
// (TestPostgresTaskSessionsRollupColumns_LegacyIntegerSurvivesWidening) uses
|
||||
// a raw UPDATE, not CreateTaskUsageEvent.
|
||||
func TestPostgresCreateTaskUsageEvent_CostSubcentsCrossesInt32ThroughRealWriter(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-cost-overflow-pg", "session-cost-overflow-pg")
|
||||
|
||||
const nearInt32Ceiling = int64(2_147_483_600) // int32 max is 2,147,483,647
|
||||
if err := repo.IncrementTaskSessionUsageTx(
|
||||
context.Background(), nil, "session-cost-overflow-pg", 0, 0, 0, nearInt32Ceiling,
|
||||
); err != nil {
|
||||
t.Fatalf("seed rollup near int32 ceiling: %v", err)
|
||||
}
|
||||
|
||||
event := newTestUsageEvent("evt-cost-overflow-pg", "task-cost-overflow-pg", "session-cost-overflow-pg")
|
||||
event.CostSubcents = 200
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent must not error when cost_subcents crosses int32: %v", err)
|
||||
}
|
||||
|
||||
_, _, _, costSubcents := readTaskSessionRollup(t, repo, "session-cost-overflow-pg")
|
||||
want := nearInt32Ceiling + 200
|
||||
if costSubcents != want {
|
||||
t.Errorf("cost_subcents = %d, want %d (a delta crossing int32 must commit through the real writer)",
|
||||
costSubcents, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kandev/kandev/internal/db/dialect"
|
||||
)
|
||||
|
||||
// initTaskUsageEventsSchema creates task_usage_events: one immutable row per
|
||||
// observed agent usage event, carrying token counts, resolved dollar cost,
|
||||
// and the inputs needed to re-derive that cost
|
||||
// (docs/specs/task-cost-ledger/spec.md). This is a new table, not a column
|
||||
// added to an existing one, so CREATE TABLE IF NOT EXISTS in the init block
|
||||
// is correct and complete.
|
||||
//
|
||||
// usage_event_id's uniqueness is a NAMED unique index
|
||||
// (uniq_task_usage_events_usage_event_id), not an inline column constraint,
|
||||
// because AC-32's Postgres unique-violation detector matches on
|
||||
// pgErr.ConstraintName - the same shape as uniq_tasks_external_id
|
||||
// (task_external_id.go).
|
||||
//
|
||||
// task_id CASCADEs: task deletion is complete deletion of its spend record.
|
||||
// session_id is ON DELETE SET NULL, mirroring task_step_transitions'
|
||||
// rationale - the historical fact that this spend happened must survive a
|
||||
// session being pruned. turn_id carries no FK for the same reason (AC-33,
|
||||
// "Corrections to the task brief" #3).
|
||||
//
|
||||
// Every token/cost column is BIGINT, never INTEGER: a single turn in the
|
||||
// spec's live-database sample reported 8,203,943 cached-read tokens, and a
|
||||
// merged per-session cached-input total hit 98,805,109 - both already past
|
||||
// Postgres's int4 ceiling.
|
||||
func (r *Repository) initTaskUsageEventsSchema() error {
|
||||
idCol := "id INTEGER PRIMARY KEY AUTOINCREMENT"
|
||||
if dialect.IsPostgres(r.db.DriverName()) {
|
||||
idCol = "id BIGSERIAL PRIMARY KEY"
|
||||
}
|
||||
_, err := r.db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS task_usage_events (
|
||||
` + idCol + `,
|
||||
usage_event_id TEXT NOT NULL,
|
||||
task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
|
||||
session_id TEXT REFERENCES task_sessions(id) ON DELETE SET NULL,
|
||||
turn_id TEXT,
|
||||
agent_profile_id TEXT NOT NULL DEFAULT '',
|
||||
agent_type TEXT NOT NULL DEFAULT '',
|
||||
model TEXT NOT NULL DEFAULT '',
|
||||
provider TEXT NOT NULL DEFAULT '',
|
||||
tokens_in BIGINT NOT NULL DEFAULT 0,
|
||||
tokens_cached_read BIGINT,
|
||||
tokens_cached_write BIGINT,
|
||||
tokens_out BIGINT,
|
||||
tokens_thought BIGINT,
|
||||
tokens_total BIGINT NOT NULL DEFAULT 0,
|
||||
cost_subcents BIGINT NOT NULL DEFAULT 0,
|
||||
cost_source TEXT NOT NULL,
|
||||
estimated INTEGER NOT NULL DEFAULT 0,
|
||||
rate_input_per_million BIGINT,
|
||||
rate_cached_read_per_million BIGINT,
|
||||
rate_cached_write_per_million BIGINT,
|
||||
rate_output_per_million BIGINT,
|
||||
pricing_catalog_version TEXT,
|
||||
contract_version INTEGER NOT NULL,
|
||||
occurred_at TIMESTAMP NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS uniq_task_usage_events_usage_event_id
|
||||
ON task_usage_events(usage_event_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_task_usage_events_task
|
||||
ON task_usage_events(task_id, occurred_at, id);
|
||||
CREATE INDEX IF NOT EXISTS idx_task_usage_events_session_turn
|
||||
ON task_usage_events(session_id, turn_id);
|
||||
DROP INDEX IF EXISTS idx_task_usage_events_occurred;
|
||||
`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("init task usage events schema: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
package sqlite
|
||||
|
||||
// Schema-level coverage for task_usage_events (docs/specs/task-cost-ledger/spec.md):
|
||||
// table shape, the named unique index AC-32's detector matches on, and the two
|
||||
// FK behaviors that make this table an append-only durable record rather than
|
||||
// a table Cascade quietly loses attribution from.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
dbutil "github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
func newUsageEventsTestRepo(t *testing.T) *Repository {
|
||||
t.Helper()
|
||||
dbPath := filepath.Join(t.TempDir(), "usage-events.db")
|
||||
dbConn, err := dbutil.OpenSQLite(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
db := sqlx.NewDb(dbConn, "sqlite3")
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("initialize schema: %v", err)
|
||||
}
|
||||
return repo
|
||||
}
|
||||
|
||||
func createUsageEventsTestTask(t *testing.T, repo *Repository, taskID string) *models.Task {
|
||||
t.Helper()
|
||||
task := &models.Task{
|
||||
ID: taskID,
|
||||
WorkspaceID: "ws-1",
|
||||
Title: "Usage events test task",
|
||||
Priority: "medium",
|
||||
}
|
||||
if err := repo.CreateTask(context.Background(), task); err != nil {
|
||||
t.Fatalf("CreateTask: %v", err)
|
||||
}
|
||||
return task
|
||||
}
|
||||
|
||||
func createUsageEventsTestSession(t *testing.T, repo *Repository, sessionID, taskID string) {
|
||||
t.Helper()
|
||||
if err := repo.CreateTaskSession(context.Background(), &models.TaskSession{
|
||||
ID: sessionID, TaskID: taskID, State: models.TaskSessionStateWaitingForInput,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateTaskSession: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func insertUsageEventRow(t *testing.T, repo *Repository, eventID, taskID, sessionID string) {
|
||||
t.Helper()
|
||||
now := time.Now().UTC()
|
||||
var sessionArg interface{}
|
||||
if sessionID != "" {
|
||||
sessionArg = sessionID
|
||||
}
|
||||
_, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, NULL, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 'unpriced', 0, 1, ?, ?)
|
||||
`), eventID, taskID, sessionArg, now, now)
|
||||
if err != nil {
|
||||
t.Fatalf("insert task_usage_events row %s: %v", eventID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskUsageEventsSchema_FreshDB proves the table, its named unique index,
|
||||
// and its three supporting indexes exist on a fresh database.
|
||||
func TestTaskUsageEventsSchema_FreshDB(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-1")
|
||||
|
||||
insertUsageEventRow(t, repo, "evt-1", "task-1", "")
|
||||
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM task_usage_events`); err != nil {
|
||||
t.Fatalf("count rows: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("row count = %d, want 1", count)
|
||||
}
|
||||
|
||||
var indexNames []string
|
||||
if err := repo.db.Select(&indexNames, `
|
||||
SELECT name FROM sqlite_master
|
||||
WHERE type = 'index' AND tbl_name = 'task_usage_events'
|
||||
`); err != nil {
|
||||
t.Fatalf("list indexes: %v", err)
|
||||
}
|
||||
want := map[string]bool{
|
||||
"uniq_task_usage_events_usage_event_id": false,
|
||||
"idx_task_usage_events_task": false,
|
||||
"idx_task_usage_events_session_turn": false,
|
||||
}
|
||||
for _, name := range indexNames {
|
||||
if name == "idx_task_usage_events_occurred" {
|
||||
t.Errorf("unexpected standalone occurred_at index %s, got indexes %v", name, indexNames)
|
||||
}
|
||||
if _, ok := want[name]; ok {
|
||||
want[name] = true
|
||||
}
|
||||
}
|
||||
for name, found := range want {
|
||||
if !found {
|
||||
t.Errorf("expected index %s to exist, got indexes %v", name, indexNames)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskUsageEventsSchema_Replay proves re-running schema init against an
|
||||
// already-initialized database (a restart) does not error - CREATE TABLE IF
|
||||
// NOT EXISTS / CREATE INDEX IF NOT EXISTS must both be idempotent - and that
|
||||
// a usage event written before the replay, along with the rollup it drove,
|
||||
// survives the replay untouched, and that the ledger still accepts new
|
||||
// writes afterward (docs/specs/task-cost-ledger/spec.md AC-29, AC-36 row 3).
|
||||
func TestTaskUsageEventsSchema_Replay(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "usage-events-replay.db")
|
||||
dbConn, err := dbutil.OpenSQLite(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
db := sqlx.NewDb(dbConn, "sqlite3")
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("first schema init: %v", err)
|
||||
}
|
||||
createUsageEventsTestTask(t, repo, "task-replay")
|
||||
createUsageEventsTestSession(t, repo, "session-replay", "task-replay")
|
||||
preReplayEvent := newTestUsageEvent("evt-replay-pre", "task-replay", "session-replay")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), preReplayEvent); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent before replay: %v", err)
|
||||
}
|
||||
|
||||
wantRows := countTaskUsageEventRows(t, repo)
|
||||
wantTokensIn, wantTokensCachedIn, wantTokensOut, wantCostSubcents := readTaskSessionRollup(t, repo, "session-replay")
|
||||
|
||||
repo, err = NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("replayed schema init must be a no-op, got error: %v", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != wantRows {
|
||||
t.Errorf("row count after replay = %d, want %d (replay must not touch existing rows)", got, wantRows)
|
||||
}
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := readTaskSessionRollup(t, repo, "session-replay")
|
||||
if tokensIn != wantTokensIn || tokensCachedIn != wantTokensCachedIn || tokensOut != wantTokensOut || costSubcents != wantCostSubcents {
|
||||
t.Errorf("rollup after replay = (%d,%d,%d,%d), want (%d,%d,%d,%d) (replay must not touch the existing rollup)",
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents, wantTokensIn, wantTokensCachedIn, wantTokensOut, wantCostSubcents)
|
||||
}
|
||||
|
||||
postReplayEvent := newTestUsageEvent("evt-replay-post", "task-replay", "session-replay")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), postReplayEvent); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent after replay: %v", err)
|
||||
}
|
||||
if got := countTaskUsageEventRows(t, repo); got != wantRows+1 {
|
||||
t.Errorf("row count after post-replay write = %d, want %d (the ledger must still accept writes after replay)", got, wantRows+1)
|
||||
}
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents = readTaskSessionRollup(t, repo, "session-replay")
|
||||
if tokensIn != wantTokensIn*2 || tokensCachedIn != wantTokensCachedIn*2 || tokensOut != wantTokensOut*2 || costSubcents != wantCostSubcents*2 {
|
||||
t.Errorf("rollup after post-replay write = (%d,%d,%d,%d), want (%d,%d,%d,%d) (the post-replay write must still increment the rollup)",
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents, wantTokensIn*2, wantTokensCachedIn*2, wantTokensOut*2, wantCostSubcents*2)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskUsageEventsSchema_UniqueIndexRejectsDuplicateUsageEventID proves the
|
||||
// named unique index actually enforces uniqueness at insert - AC-32's
|
||||
// unique-violation detector depends on this constraint existing under this
|
||||
// exact name.
|
||||
func TestTaskUsageEventsSchema_UniqueIndexRejectsDuplicateUsageEventID(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-1")
|
||||
|
||||
insertUsageEventRow(t, repo, "evt-dup", "task-1", "")
|
||||
|
||||
now := time.Now().UTC()
|
||||
_, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, ?, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 'unpriced', 0, 1, ?, ?)
|
||||
`), "evt-dup", "task-1", nil, nil, now, now)
|
||||
if err == nil {
|
||||
t.Fatal("expected a unique-constraint error inserting a duplicate usage_event_id, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskUsageEventsSchema_TaskDeleteCascades proves task_id CASCADEs: task
|
||||
// deletion is complete deletion of its spend record.
|
||||
func TestTaskUsageEventsSchema_TaskDeleteCascades(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-cascade")
|
||||
insertUsageEventRow(t, repo, "evt-cascade", "task-cascade", "")
|
||||
|
||||
if err := repo.DeleteTask(context.Background(), "task-cascade"); err != nil {
|
||||
t.Fatalf("DeleteTask: %v", err)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM task_usage_events WHERE task_id = ?`, "task-cascade"); err != nil {
|
||||
t.Fatalf("count rows: %v", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Errorf("row count after task deletion = %d, want 0 (task_id must CASCADE)", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskUsageEventsSchema_SessionDeleteSetsNull proves session_id is
|
||||
// ON DELETE SET NULL: the historical fact that this spend happened must
|
||||
// survive a session being pruned, mirroring task_step_transitions'
|
||||
// rationale.
|
||||
func TestTaskUsageEventsSchema_SessionDeleteSetsNull(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-setnull")
|
||||
createUsageEventsTestSession(t, repo, "session-setnull", "task-setnull")
|
||||
insertUsageEventRow(t, repo, "evt-setnull", "task-setnull", "session-setnull")
|
||||
|
||||
if err := repo.DeleteTaskSession(context.Background(), "session-setnull"); err != nil {
|
||||
t.Fatalf("DeleteTaskSession: %v", err)
|
||||
}
|
||||
|
||||
var sessionID *string
|
||||
if err := repo.db.Get(&sessionID, `SELECT session_id FROM task_usage_events WHERE usage_event_id = ?`, "evt-setnull"); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if sessionID != nil {
|
||||
t.Errorf("session_id after session deletion = %v, want nil (row must survive with session_id cleared)", *sessionID)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM task_usage_events WHERE usage_event_id = ?`, "evt-setnull"); err != nil {
|
||||
t.Fatalf("count rows: %v", err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Errorf("row count after session deletion = %d, want 1 (row must be retained)", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTaskUsageEventsSchema_TaskForeignKeyRejectsUnknownTask proves task_id's
|
||||
// FK is enforced: an insert naming a task that doesn't exist is rejected
|
||||
// outright rather than committing, which is what makes AC-32's foreign-key
|
||||
// classification reachable.
|
||||
func TestTaskUsageEventsSchema_TaskForeignKeyRejectsUnknownTask(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
now := time.Now().UTC()
|
||||
_, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, ?, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 'unpriced', 0, 1, ?, ?)
|
||||
`), "evt-no-task", "task-does-not-exist", nil, nil, now, now)
|
||||
if err == nil {
|
||||
t.Fatal("expected a foreign-key error inserting a row against an unknown task_id, got nil")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package sqlite
|
||||
|
||||
// TestUsageEventInsertAndRollupShareOneTransaction is the repointed
|
||||
// replacement (docs/specs/task-cost-ledger/spec.md AC-21, AC-36) for the
|
||||
// deleted office/repository/sqlite test
|
||||
// TestCostEventTxAtomicity_RealReposShareTransaction. That test drove
|
||||
// office.Repository.BeginTx -> office.Repository.CreateCostEventTx ->
|
||||
// task.Repository.IncrementTaskSessionUsageTx to prove the transaction
|
||||
// office's recordCostEventAndRollup began could really reach task_sessions,
|
||||
// a table the task package owns - not just a test double that discarded its
|
||||
// *sqlx.Tx parameter (PR #2606 review round 1, F2).
|
||||
//
|
||||
// AC-21 removes that caller: recordCostEventAndRollup no longer begins a
|
||||
// shared transaction at all. IncrementTaskSessionUsageTx is retained because
|
||||
// the ledger writer's own insertUsageEventAndRollup (usage_events.go) is now
|
||||
// the real caller of this exact capability, so this test repoints the same
|
||||
// rollback-then-commit shape at the ledger writer's own two writes -
|
||||
// insertUsageEventRowTx and IncrementTaskSessionUsageTx - sharing one
|
||||
// *sqlx.Tx, driven manually rather than through CreateTaskUsageEvent so the
|
||||
// tx != nil branch is exercised explicitly, the same guarantee the original
|
||||
// test existed to pin.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUsageEventInsertAndRollupShareOneTransaction(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
ctx := context.Background()
|
||||
createUsageEventsTestTask(t, repo, "task-shared-tx")
|
||||
createUsageEventsTestSession(t, repo, "session-shared-tx", "task-shared-tx")
|
||||
|
||||
// Rollback: both writes land in the same transaction; rolling it back
|
||||
// must undo both, not just the one whose caller happened to fail.
|
||||
rollbackEvent := newTestUsageEvent("evt-shared-tx-rollback", "task-shared-tx", "session-shared-tx")
|
||||
tx, err := repo.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("begin tx: %v", err)
|
||||
}
|
||||
if err := repo.insertUsageEventRowTx(ctx, tx, rollbackEvent); err != nil {
|
||||
t.Fatalf("insert usage event row in tx: %v", err)
|
||||
}
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, tx, "session-shared-tx", 100, 25, 30, 42); err != nil {
|
||||
t.Fatalf("increment rollup in tx: %v", err)
|
||||
}
|
||||
if err := tx.Rollback(); err != nil {
|
||||
t.Fatalf("rollback: %v", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 0 {
|
||||
t.Fatalf("row count after rollback = %d, want 0", got)
|
||||
}
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := readTaskSessionRollup(t, repo, "session-shared-tx")
|
||||
if tokensIn != 0 || tokensCachedIn != 0 || tokensOut != 0 || costSubcents != 0 {
|
||||
t.Fatalf("rollup after rollback = (%d,%d,%d,%d), want all zero", tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
|
||||
// Commit: a fresh transaction with the same shape commits both writes
|
||||
// together.
|
||||
commitEvent := newTestUsageEvent("evt-shared-tx-commit", "task-shared-tx", "session-shared-tx")
|
||||
tx2, err := repo.db.BeginTxx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("begin tx (commit path): %v", err)
|
||||
}
|
||||
if err := repo.insertUsageEventRowTx(ctx, tx2, commitEvent); err != nil {
|
||||
t.Fatalf("insert usage event row in tx (commit path): %v", err)
|
||||
}
|
||||
if err := repo.IncrementTaskSessionUsageTx(ctx, tx2, "session-shared-tx", 100, 25, 30, 42); err != nil {
|
||||
t.Fatalf("increment rollup in tx (commit path): %v", err)
|
||||
}
|
||||
if err := tx2.Commit(); err != nil {
|
||||
t.Fatalf("commit: %v", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 1 {
|
||||
t.Fatalf("row count after commit = %d, want 1", got)
|
||||
}
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents = readTaskSessionRollup(t, repo, "session-shared-tx")
|
||||
if tokensIn != 100 || tokensCachedIn != 25 || tokensOut != 30 || costSubcents != 42 {
|
||||
t.Fatalf("rollup after commit = (%d,%d,%d,%d), want (100,25,30,42)", tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,381 @@
|
||||
package sqlite
|
||||
|
||||
// Behavioral coverage for CreateTaskUsageEvent (docs/specs/task-cost-ledger/spec.md
|
||||
// AC-11, AC-12, AC-13, AC-14, AC-30, AC-32, AC-33): the atomic ledger-insert +
|
||||
// rollup transaction and its insert-failure classification/reaction.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
func newTestUsageEvent(usageEventID, taskID, sessionID string) *models.TaskUsageEvent {
|
||||
now := time.Now().UTC()
|
||||
tokensCachedRead := int64(20)
|
||||
tokensCachedWrite := int64(5)
|
||||
tokensOut := int64(30)
|
||||
return &models.TaskUsageEvent{
|
||||
UsageEventID: usageEventID,
|
||||
TaskID: taskID,
|
||||
SessionID: sessionID,
|
||||
AgentType: "claude",
|
||||
Model: "claude-sonnet-5",
|
||||
Provider: "anthropic",
|
||||
TokensIn: 100,
|
||||
TokensCachedRead: &tokensCachedRead,
|
||||
TokensCachedWrite: &tokensCachedWrite,
|
||||
TokensOut: &tokensOut,
|
||||
TokensTotal: 155,
|
||||
CostSubcents: 42,
|
||||
CostSource: "actual",
|
||||
ContractVersion: 1,
|
||||
OccurredAt: now,
|
||||
CreatedAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
func readTaskSessionRollup(t *testing.T, repo *Repository, sessionID string) (tokensIn, tokensCachedIn, tokensOut, costSubcents int64) {
|
||||
t.Helper()
|
||||
if err := repo.db.QueryRowx(repo.db.Rebind(`
|
||||
SELECT COALESCE(tokens_in, 0), COALESCE(tokens_cached_in, 0), COALESCE(tokens_out, 0), COALESCE(cost_subcents, 0)
|
||||
FROM task_sessions WHERE id = ?
|
||||
`), sessionID).Scan(&tokensIn, &tokensCachedIn, &tokensOut, &costSubcents); err != nil {
|
||||
t.Fatalf("read task_sessions rollup: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func countTaskUsageEventRows(t *testing.T, repo *Repository) int {
|
||||
t.Helper()
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM task_usage_events`); err != nil {
|
||||
t.Fatalf("count rows: %v", err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func TestCreateTaskUsageEvent_HappyPath_InsertsRowAndIncrementsRollup(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-happy")
|
||||
createUsageEventsTestSession(t, repo, "session-happy", "task-happy")
|
||||
|
||||
event := newTestUsageEvent("evt-happy", "task-happy", "session-happy")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 1 {
|
||||
t.Fatalf("row count = %d, want 1", got)
|
||||
}
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := readTaskSessionRollup(t, repo, "session-happy")
|
||||
if tokensIn != 100 || tokensCachedIn != 25 || tokensOut != 30 || costSubcents != 42 {
|
||||
t.Errorf("rollup = (%d,%d,%d,%d), want (100,25,30,42)", tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_SameSessionAndTurn_BothRowsCommitAndRollupSums
|
||||
// pins AC-15: (session_id, turn_id) is an aggregation key for reads, not a
|
||||
// uniqueness constraint on writes - a single turn producing multiple ledger
|
||||
// rows (e.g. more than one prompt-usage event in one turn) must not be
|
||||
// rejected or collapsed.
|
||||
func TestCreateTaskUsageEvent_SameSessionAndTurn_BothRowsCommitAndRollupSums(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-same-turn")
|
||||
createUsageEventsTestSession(t, repo, "session-same-turn", "task-same-turn")
|
||||
|
||||
first := newTestUsageEvent("evt-same-turn-1", "task-same-turn", "session-same-turn")
|
||||
first.TurnID = "turn-shared"
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), first); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent (first): %v", err)
|
||||
}
|
||||
|
||||
second := newTestUsageEvent("evt-same-turn-2", "task-same-turn", "session-same-turn")
|
||||
second.TurnID = "turn-shared"
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), second); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent (second, same session_id+turn_id): %v", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 2 {
|
||||
t.Fatalf("row count = %d, want 2 (both rows for the shared turn must commit)", got)
|
||||
}
|
||||
|
||||
events, err := repo.ListTaskUsageEvents(context.Background(), "task-same-turn", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents: %v", err)
|
||||
}
|
||||
if len(events) != 2 {
|
||||
t.Fatalf("ListTaskUsageEvents returned %d events, want 2", len(events))
|
||||
}
|
||||
if events[0].TurnID != "turn-shared" || events[1].TurnID != "turn-shared" {
|
||||
t.Errorf("turn IDs = [%q, %q], want both rows to carry turn_id=turn-shared", events[0].TurnID, events[1].TurnID)
|
||||
}
|
||||
if events[0].UsageEventID == events[1].UsageEventID {
|
||||
t.Errorf("both rows have usage_event_id %q, want two distinct rows", events[0].UsageEventID)
|
||||
}
|
||||
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := readTaskSessionRollup(t, repo, "session-same-turn")
|
||||
if tokensIn != 200 || tokensCachedIn != 50 || tokensOut != 60 || costSubcents != 84 {
|
||||
t.Errorf("rollup = (%d,%d,%d,%d), want (200,50,60,84) (both rows must contribute)", tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_NilTokenPointersCoalesceToZeroInRollup pins AC-12:
|
||||
// a not-recorded token count (nil pointer) contributes zero to the rollup
|
||||
// rather than propagating SQL NULL through the sum.
|
||||
func TestCreateTaskUsageEvent_NilTokenPointersCoalesceToZeroInRollup(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-nil-tokens")
|
||||
createUsageEventsTestSession(t, repo, "session-nil-tokens", "task-nil-tokens")
|
||||
|
||||
event := newTestUsageEvent("evt-nil-tokens", "task-nil-tokens", "session-nil-tokens")
|
||||
event.TokensCachedRead = nil
|
||||
event.TokensCachedWrite = nil
|
||||
event.TokensOut = nil
|
||||
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
tokensIn, tokensCachedIn, tokensOut, costSubcents := readTaskSessionRollup(t, repo, "session-nil-tokens")
|
||||
if tokensIn != 100 || tokensCachedIn != 0 || tokensOut != 0 || costSubcents != 42 {
|
||||
t.Errorf("rollup = (%d,%d,%d,%d), want (100,0,0,42)", tokensIn, tokensCachedIn, tokensOut, costSubcents)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_DuplicateUsageEventID_ReturnsErrDuplicateNoRollup
|
||||
// pins AC-13/AC-14/AC-32: a redelivered usage_event_id records no new row
|
||||
// and increments the rollup no further, reported as a duplicate rather than
|
||||
// an error.
|
||||
func TestCreateTaskUsageEvent_DuplicateUsageEventID_ReturnsErrDuplicateNoRollup(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-dup")
|
||||
createUsageEventsTestSession(t, repo, "session-dup", "task-dup")
|
||||
|
||||
first := newTestUsageEvent("evt-dup", "task-dup", "session-dup")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), first); err != nil {
|
||||
t.Fatalf("first CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
redelivered := newTestUsageEvent("evt-dup", "task-dup", "session-dup")
|
||||
err := repo.CreateTaskUsageEvent(context.Background(), redelivered)
|
||||
if err != ErrDuplicateUsageEvent {
|
||||
t.Fatalf("redelivered CreateTaskUsageEvent error = %v, want ErrDuplicateUsageEvent", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 1 {
|
||||
t.Fatalf("row count = %d, want 1 (redelivery must not insert a second row)", got)
|
||||
}
|
||||
tokensIn, _, _, _ := readTaskSessionRollup(t, repo, "session-dup")
|
||||
if tokensIn != 100 {
|
||||
t.Errorf("tokens_in = %d, want 100 (redelivery must not double-increment the rollup)", tokensIn)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_ConcurrentInsertsSameUsageEventID_ExactlyOneCommits
|
||||
// pins AC-32's unique-violation branch under a genuine race, not a
|
||||
// sequential redelivery: two deliveries of the same event, racing each
|
||||
// other, with the producer's deterministic identifier (spec's "Concurrency"
|
||||
// scenario). Both goroutines are released together via a start barrier so
|
||||
// they race for real; SQLITE_BUSY contention is expected and handled by
|
||||
// CreateTaskUsageEvent's own transient-retry loop, not by this test.
|
||||
// Exactly one call must succeed and the other must observe
|
||||
// ErrDuplicateUsageEvent - no other error, and no silent double-drop - with
|
||||
// the rollup incremented exactly once, matching the loser's increment
|
||||
// sharing the loser's rolled-back transaction.
|
||||
func TestCreateTaskUsageEvent_ConcurrentInsertsSameUsageEventID_ExactlyOneCommits(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-race")
|
||||
createUsageEventsTestSession(t, repo, "session-race", "task-race")
|
||||
|
||||
start := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
errs := make([]error, 2)
|
||||
for i := range errs {
|
||||
wg.Add(1)
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
event := newTestUsageEvent("evt-race", "task-race", "session-race")
|
||||
<-start
|
||||
errs[i] = repo.CreateTaskUsageEvent(context.Background(), event)
|
||||
}(i)
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
|
||||
successes, duplicates := 0, 0
|
||||
for _, err := range errs {
|
||||
switch err {
|
||||
case nil:
|
||||
successes++
|
||||
case ErrDuplicateUsageEvent:
|
||||
duplicates++
|
||||
default:
|
||||
t.Fatalf("unexpected error from a racing insert: %v", err)
|
||||
}
|
||||
}
|
||||
if successes != 1 || duplicates != 1 {
|
||||
t.Fatalf("successes=%d duplicates=%d, want exactly one of each", successes, duplicates)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 1 {
|
||||
t.Fatalf("row count = %d, want 1 (the losing insert must not leave a row behind)", got)
|
||||
}
|
||||
tokensIn, _, _, _ := readTaskSessionRollup(t, repo, "session-race")
|
||||
if tokensIn != 100 {
|
||||
t.Errorf("tokens_in = %d, want 100 (the rollup must be incremented exactly once, not twice or zero times)", tokensIn)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_SessionForeignKeyViolation_RetriesOnceWithSessionCleared
|
||||
// pins AC-32(b): an insert naming a session that does not exist is retried
|
||||
// exactly once with session_id cleared, and that retry does not increment
|
||||
// any session's rollup.
|
||||
func TestCreateTaskUsageEvent_SessionForeignKeyViolation_RetriesOnceWithSessionCleared(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-fk-retry")
|
||||
|
||||
event := newTestUsageEvent("evt-fk-retry", "task-fk-retry", "session-does-not-exist")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 1 {
|
||||
t.Fatalf("row count = %d, want 1", got)
|
||||
}
|
||||
var sessionID *string
|
||||
if err := repo.db.Get(&sessionID, `SELECT session_id FROM task_usage_events WHERE usage_event_id = ?`, "evt-fk-retry"); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if sessionID != nil {
|
||||
t.Errorf("session_id = %v, want nil (FK retry must clear it)", *sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_SecondForeignKeyFailure_ReturnsError pins AC-32(b)'s
|
||||
// tail: a second failure after the one allotted FK retry is returned as-is,
|
||||
// not retried again.
|
||||
func TestCreateTaskUsageEvent_SecondForeignKeyFailure_ReturnsError(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
|
||||
event := newTestUsageEvent("evt-fk-double", "task-does-not-exist", "session-does-not-exist")
|
||||
err := repo.CreateTaskUsageEvent(context.Background(), event)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error when both task_id and session_id are invalid, got nil")
|
||||
}
|
||||
if err == ErrDuplicateUsageEvent {
|
||||
t.Fatalf("error = ErrDuplicateUsageEvent, want a foreign-key failure")
|
||||
}
|
||||
if got := countTaskUsageEventRows(t, repo); got != 0 {
|
||||
t.Errorf("row count = %d, want 0 (both attempts must roll back)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_SessionBelongsToDifferentTask_ClearsSessionNoRollup
|
||||
// pins AC-33: a session that exists but belongs to a different task is
|
||||
// recorded under the payload's task_id with session_id cleared, the same
|
||||
// terminal handling as the foreign-key retry case.
|
||||
func TestCreateTaskUsageEvent_SessionBelongsToDifferentTask_ClearsSessionNoRollup(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-owner")
|
||||
createUsageEventsTestSession(t, repo, "session-owner", "task-owner")
|
||||
createUsageEventsTestTask(t, repo, "task-other")
|
||||
|
||||
event := newTestUsageEvent("evt-mismatch", "task-other", "session-owner")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
var gotTaskID string
|
||||
var sessionID *string
|
||||
if err := repo.db.QueryRowx(`SELECT task_id, session_id FROM task_usage_events WHERE usage_event_id = ?`, "evt-mismatch").
|
||||
Scan(&gotTaskID, &sessionID); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if gotTaskID != "task-other" {
|
||||
t.Errorf("task_id = %q, want %q", gotTaskID, "task-other")
|
||||
}
|
||||
if sessionID != nil {
|
||||
t.Errorf("session_id = %v, want nil (mismatched session must be cleared)", *sessionID)
|
||||
}
|
||||
|
||||
tokensIn, _, _, _ := readTaskSessionRollup(t, repo, "session-owner")
|
||||
if tokensIn != 0 {
|
||||
t.Errorf("session-owner tokens_in = %d, want 0 (mismatched session must not be rolled up into)", tokensIn)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_SessionOwnershipLookupHardError_ReturnsImmediately
|
||||
// pins R5-F1: a session-ownership lookup failure that is not "no such
|
||||
// session" is returned immediately, without attempting any insert.
|
||||
func TestCreateTaskUsageEvent_SessionOwnershipLookupHardError_ReturnsImmediately(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-hard-error")
|
||||
if _, err := repo.db.Exec(`DROP TABLE task_sessions`); err != nil {
|
||||
t.Fatalf("drop task_sessions to force a hard ownership-lookup error: %v", err)
|
||||
}
|
||||
|
||||
event := newTestUsageEvent("evt-hard-error", "task-hard-error", "session-whatever")
|
||||
err := repo.CreateTaskUsageEvent(context.Background(), event)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error when the ownership lookup itself fails, got nil")
|
||||
}
|
||||
if err == ErrDuplicateUsageEvent {
|
||||
t.Fatalf("error = ErrDuplicateUsageEvent, want the raw ownership-lookup error")
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := repo.db.Get(&count, `SELECT COUNT(*) FROM task_usage_events`); err != nil {
|
||||
t.Fatalf("count rows: %v", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Errorf("row count = %d, want 0 (a hard ownership-lookup error must not attempt an insert)", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_NullableFieldsRoundTripThroughList pins AC-30: a
|
||||
// nil token pointer round-trips as nil (not a measured zero), and a
|
||||
// measured-zero pointer round-trips distinctly as a non-nil zero.
|
||||
func TestCreateTaskUsageEvent_NullableFieldsRoundTripThroughList(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-roundtrip")
|
||||
createUsageEventsTestSession(t, repo, "session-roundtrip", "task-roundtrip")
|
||||
|
||||
event := newTestUsageEvent("evt-roundtrip", "task-roundtrip", "session-roundtrip")
|
||||
measuredZero := int64(0)
|
||||
event.TokensThought = nil
|
||||
event.TokensOut = &measuredZero
|
||||
event.PricingCatalogVersion = "catalog-v3"
|
||||
event.TurnID = "turn-1"
|
||||
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
events, err := repo.ListTaskUsageEvents(context.Background(), "task-roundtrip", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents: %v", err)
|
||||
}
|
||||
if len(events) != 1 {
|
||||
t.Fatalf("len(events) = %d, want 1", len(events))
|
||||
}
|
||||
got := events[0]
|
||||
if got.TokensThought != nil {
|
||||
t.Errorf("TokensThought = %v, want nil (not recorded)", *got.TokensThought)
|
||||
}
|
||||
if got.TokensOut == nil || *got.TokensOut != 0 {
|
||||
t.Errorf("TokensOut = %v, want a non-nil measured zero", got.TokensOut)
|
||||
}
|
||||
if got.PricingCatalogVersion != "catalog-v3" {
|
||||
t.Errorf("PricingCatalogVersion = %q, want %q", got.PricingCatalogVersion, "catalog-v3")
|
||||
}
|
||||
if got.TurnID != "turn-1" {
|
||||
t.Errorf("TurnID = %q, want %q", got.TurnID, "turn-1")
|
||||
}
|
||||
if got.SessionID != "session-roundtrip" {
|
||||
t.Errorf("SessionID = %q, want %q", got.SessionID, "session-roundtrip")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package sqlite
|
||||
|
||||
// Transient-error retry sequencing for CreateTaskUsageEvent (AC-32(c)): a
|
||||
// transient failure is retried in a fresh transaction, waiting per
|
||||
// usageEventTransientBackoffs, up to maxUsageEventTransientAttempts total
|
||||
// attempts. A genuine SQLITE_BUSY/serialization race cannot be triggered
|
||||
// deterministically against this package's single-connection test
|
||||
// repositories, so these tests drive the retry loop through the failpoint
|
||||
// seams in base.go instead. They run inside synctest.Test so
|
||||
// usageEventTransientBackoffs' real time.Timer waits (sleepOrDone) advance on
|
||||
// the fake clock instead of costing real wall time.
|
||||
//
|
||||
// TestCreateTaskUsageEvent_TransientError_RetriesAndSucceeds and
|
||||
// TestCreateTaskUsageEvent_TransientErrorExhaustsRetries_ReturnsError use
|
||||
// failUsageEventRollupAttempts/failUsageEventRollupErr, which fires after
|
||||
// insertUsageEventRowTx has actually attempted the row insert on each
|
||||
// retried attempt - proving the retry loop rolls back and re-attempts a real
|
||||
// insert, not just a pre-transaction error return.
|
||||
// TestCreateTaskUsageEvent_TransientErrorDoesNotConsumeForeignKeyRetryBudget
|
||||
// keeps the older failUsageEventAttempts/failUsageEventErr seam (fires
|
||||
// before BeginTxx) because its scenario depends on the injected transient
|
||||
// failure preceding a real foreign-key violation on the same attempt; the
|
||||
// FK-violating session ("session-does-not-exist") is never created, so a
|
||||
// post-insert failpoint would never fire on that attempt at all.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func transientSQLiteBusyErr() error {
|
||||
return sqlite3.Error{Code: sqlite3.ErrBusy}
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_TransientError_RetriesAndSucceeds pins that a
|
||||
// single transient failure is retried (not surfaced as an error), and that
|
||||
// the retry actually inserts the row.
|
||||
func TestCreateTaskUsageEvent_TransientError_RetriesAndSucceeds(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-transient-retry")
|
||||
createUsageEventsTestSession(t, repo, "session-transient-retry", "task-transient-retry")
|
||||
|
||||
repo.failUsageEventRollupAttempts = 1
|
||||
repo.failUsageEventRollupErr = transientSQLiteBusyErr()
|
||||
|
||||
event := newTestUsageEvent("evt-transient-retry", "task-transient-retry", "session-transient-retry")
|
||||
start := time.Now()
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if elapsed < usageEventTransientBackoffs[0] {
|
||||
t.Errorf("elapsed = %v, want at least the first backoff (%v)", elapsed, usageEventTransientBackoffs[0])
|
||||
}
|
||||
if got := countTaskUsageEventRows(t, repo); got != 1 {
|
||||
t.Fatalf("row count = %d, want 1", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_TransientErrorExhaustsRetries_ReturnsError pins
|
||||
// AC-32(c)'s cap: maxUsageEventTransientAttempts total attempts, after which
|
||||
// the last transient error is returned as-is and no row is inserted.
|
||||
func TestCreateTaskUsageEvent_TransientErrorExhaustsRetries_ReturnsError(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-transient-exhaust")
|
||||
createUsageEventsTestSession(t, repo, "session-transient-exhaust", "task-transient-exhaust")
|
||||
|
||||
repo.failUsageEventRollupAttempts = maxUsageEventTransientAttempts
|
||||
repo.failUsageEventRollupErr = transientSQLiteBusyErr()
|
||||
|
||||
event := newTestUsageEvent("evt-transient-exhaust", "task-transient-exhaust", "session-transient-exhaust")
|
||||
start := time.Now()
|
||||
err := repo.CreateTaskUsageEvent(context.Background(), event)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected an error after exhausting all transient-retry attempts, got nil")
|
||||
}
|
||||
if !errors.Is(err, repo.failUsageEventRollupErr) && err.Error() != transientSQLiteBusyErr().Error() {
|
||||
t.Errorf("returned error = %v, want the injected transient error surfaced as-is", err)
|
||||
}
|
||||
|
||||
var wantMinWait time.Duration
|
||||
for _, backoff := range usageEventTransientBackoffs {
|
||||
wantMinWait += backoff
|
||||
}
|
||||
if elapsed < wantMinWait {
|
||||
t.Errorf("elapsed = %v, want at least the sum of all backoffs (%v)", elapsed, wantMinWait)
|
||||
}
|
||||
if got := countTaskUsageEventRows(t, repo); got != 0 {
|
||||
t.Errorf("row count = %d, want 0 (exhausted retries must not leave a row)", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestCreateTaskUsageEvent_TransientErrorDoesNotConsumeForeignKeyRetryBudget
|
||||
// pins that the transient-retry budget and the one-shot foreign-key retry
|
||||
// are independent: a transient failure followed by a real foreign-key
|
||||
// violation still gets its single FK retry.
|
||||
func TestCreateTaskUsageEvent_TransientErrorDoesNotConsumeForeignKeyRetryBudget(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-transient-then-fk")
|
||||
|
||||
repo.failUsageEventAttempts = 1
|
||||
repo.failUsageEventErr = transientSQLiteBusyErr()
|
||||
|
||||
event := newTestUsageEvent("evt-transient-then-fk", "task-transient-then-fk", "session-does-not-exist")
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent: %v", err)
|
||||
}
|
||||
|
||||
if got := countTaskUsageEventRows(t, repo); got != 1 {
|
||||
t.Fatalf("row count = %d, want 1", got)
|
||||
}
|
||||
var sessionID *string
|
||||
if err := repo.db.Get(&sessionID, `SELECT session_id FROM task_usage_events WHERE usage_event_id = ?`, "evt-transient-then-fk"); err != nil {
|
||||
t.Fatalf("read row: %v", err)
|
||||
}
|
||||
if sessionID != nil {
|
||||
t.Errorf("session_id = %v, want nil (the FK retry must still be available after a transient retry)", *sessionID)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
// ErrDuplicateUsageEvent is returned when an insert into task_usage_events
|
||||
// collides with an existing row's usage_event_id. The real duplicate source
|
||||
// is the same underlying usage frame reaching the ledger writer twice (e.g.
|
||||
// a reconnecting WS client replaying a buffered stream event), not
|
||||
// double-spend, so callers treat this as an idempotent no-op (AC-13, AC-14,
|
||||
// AC-32) rather than a failure. Mirrors
|
||||
// internal/office/repository/sqlite/costs.go's ErrDuplicateUsageEvent.
|
||||
var ErrDuplicateUsageEvent = errors.New("duplicate usage_event_id")
|
||||
|
||||
// usageEventIDIndexName is the named unique index enforcing at most one
|
||||
// task_usage_events row per usage_event_id
|
||||
// (docs/specs/task-cost-ledger/spec.md, "task_usage_events (new table)").
|
||||
// Naming it explicitly here keeps isUsageEventUniqueViolation attributable
|
||||
// to this constraint specifically, not to the table's primary key or any
|
||||
// other unique constraint.
|
||||
const usageEventIDIndexName = "uniq_task_usage_events_usage_event_id"
|
||||
|
||||
// sqliteUsageEventIDViolationMessage is the substring go-sqlite3 puts in a
|
||||
// UNIQUE-constraint error for this index. SQLite exposes no typed access to
|
||||
// the violated index's name, only the column list of the failing index
|
||||
// ("UNIQUE constraint failed: task_usage_events.usage_event_id"); that
|
||||
// column is unique to uniq_task_usage_events_usage_event_id in this table,
|
||||
// so matching it attributes the violation correctly - a bare "UNIQUE
|
||||
// constraint failed" match would also fire on the table's primary key.
|
||||
const sqliteUsageEventIDViolationMessage = "UNIQUE constraint failed: task_usage_events.usage_event_id"
|
||||
|
||||
// isUsageEventUniqueViolation reports whether err is a violation of
|
||||
// uniq_task_usage_events_usage_event_id specifically, not any unique
|
||||
// violation (AC-32). On PostgreSQL it inspects the typed pgconn.PgError's
|
||||
// constraint name; on SQLite (no typed access to the constraint name) it
|
||||
// matches the column-list message documented above. Mirrors
|
||||
// isExternalIDUniqueViolation (task_external_id.go) and
|
||||
// internal/office/repository/sqlite/costs.go's isUsageEventUniqueViolation.
|
||||
func isUsageEventUniqueViolation(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
return pgErr.Code == "23505" && pgErr.ConstraintName == usageEventIDIndexName
|
||||
}
|
||||
return strings.Contains(err.Error(), sqliteUsageEventIDViolationMessage)
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package sqlite
|
||||
|
||||
// AC-32 coverage against a real Postgres instance: the detector's typed
|
||||
// pgconn.PgError branch (ConstraintName == uniq_task_usage_events_usage_event_id)
|
||||
// is only reachable through an actual PostgreSQL driver error, never through
|
||||
// SQLite's string-matched fallback.
|
||||
//
|
||||
// Skips unless KANDEV_TEST_POSTGRES_DSN is set.
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/testutil"
|
||||
)
|
||||
|
||||
func insertPostgresUsageEventRow(t *testing.T, repo *Repository, eventID, taskID string) error {
|
||||
t.Helper()
|
||||
now := time.Now().UTC()
|
||||
_, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, ?, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 'unpriced', 0, 1, ?, ?)
|
||||
`), eventID, taskID, nil, nil, now, now)
|
||||
return err
|
||||
}
|
||||
|
||||
func TestPostgresIsUsageEventUniqueViolation_DuplicateUsageEventID(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-dup-unique-pg", "session-dup-unique-pg")
|
||||
|
||||
if err := insertPostgresUsageEventRow(t, repo, "evt-dup-pg", "task-dup-unique-pg"); err != nil {
|
||||
t.Fatalf("first insert: %v", err)
|
||||
}
|
||||
err = insertPostgresUsageEventRow(t, repo, "evt-dup-pg", "task-dup-unique-pg")
|
||||
if err == nil {
|
||||
t.Fatal("expected a unique-constraint error inserting a duplicate usage_event_id, got nil")
|
||||
}
|
||||
if !isUsageEventUniqueViolation(err) {
|
||||
t.Errorf("isUsageEventUniqueViolation(%v) = false, want true", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgresIsUsageEventUniqueViolation_ForeignKeyNotMisclassified(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
|
||||
err = insertPostgresUsageEventRow(t, repo, "evt-no-task-pg", "task-does-not-exist-pg")
|
||||
if err == nil {
|
||||
t.Fatal("expected a foreign-key error inserting a row against an unknown task_id, got nil")
|
||||
}
|
||||
if isUsageEventUniqueViolation(err) {
|
||||
t.Errorf("isUsageEventUniqueViolation(%v) = true, want false (this is a foreign-key violation)", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package sqlite
|
||||
|
||||
// Detector-level coverage for isUsageEventUniqueViolation (AC-32): it must
|
||||
// fire only for uniq_task_usage_events_usage_event_id, not for the table's
|
||||
// primary key or its foreign keys, so a non-duplicate insert failure is
|
||||
// never silently swallowed as "expected" duplicate behaviour.
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestIsUsageEventUniqueViolation_Nil(t *testing.T) {
|
||||
if isUsageEventUniqueViolation(nil) {
|
||||
t.Error("isUsageEventUniqueViolation(nil) = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsUsageEventUniqueViolation_SQLiteDuplicateUsageEventID(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-1")
|
||||
insertUsageEventRow(t, repo, "evt-dup", "task-1", "")
|
||||
|
||||
now := time.Now().UTC()
|
||||
_, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, ?, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 'unpriced', 0, 1, ?, ?)
|
||||
`), "evt-dup", "task-1", nil, nil, now, now)
|
||||
if err == nil {
|
||||
t.Fatal("expected a unique-constraint error inserting a duplicate usage_event_id, got nil")
|
||||
}
|
||||
if !isUsageEventUniqueViolation(err) {
|
||||
t.Errorf("isUsageEventUniqueViolation(%v) = false, want true", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsUsageEventUniqueViolation_SQLitePrimaryKeyConflictNotMisclassified
|
||||
// proves the detector is constraint-specific: a duplicate on the table's own
|
||||
// primary key must NOT be reported as a usage_event_id violation, even
|
||||
// though both are "UNIQUE constraint failed" errors.
|
||||
func TestIsUsageEventUniqueViolation_SQLitePrimaryKeyConflictNotMisclassified(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-1")
|
||||
now := time.Now().UTC()
|
||||
|
||||
_, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
id, usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, ?, ?, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 'unpriced', 0, 1, ?, ?)
|
||||
`), int64(9001), "evt-pk-1", "task-1", nil, nil, now, now)
|
||||
if err != nil {
|
||||
t.Fatalf("seed row with explicit id: %v", err)
|
||||
}
|
||||
|
||||
_, err = repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
id, usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, ?, ?, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 'unpriced', 0, 1, ?, ?)
|
||||
`), int64(9001), "evt-pk-2", "task-1", nil, nil, now, now)
|
||||
if err == nil {
|
||||
t.Fatal("expected a primary-key conflict inserting a duplicate id, got nil")
|
||||
}
|
||||
if isUsageEventUniqueViolation(err) {
|
||||
t.Errorf("isUsageEventUniqueViolation(%v) = true, want false (this is a primary-key conflict, not a usage_event_id conflict)", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsUsageEventUniqueViolation_SQLiteForeignKeyNotMisclassified proves a
|
||||
// foreign-key rejection (AC-32's other classified failure kind) is never
|
||||
// mistaken for a unique violation.
|
||||
func TestIsUsageEventUniqueViolation_SQLiteForeignKeyNotMisclassified(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
now := time.Now().UTC()
|
||||
|
||||
_, err := repo.db.Exec(repo.db.Rebind(`
|
||||
INSERT INTO task_usage_events (
|
||||
usage_event_id, task_id, session_id, turn_id,
|
||||
agent_profile_id, agent_type, model, provider,
|
||||
tokens_in, tokens_cached_read, tokens_cached_write, tokens_out, tokens_thought,
|
||||
tokens_total, cost_subcents, cost_source, estimated,
|
||||
contract_version, occurred_at, created_at
|
||||
) VALUES (?, ?, ?, ?, '', '', '', '', 0, 0, 0, 0, 0, 0, 0, 'unpriced', 0, 1, ?, ?)
|
||||
`), "evt-no-task", "task-does-not-exist", nil, nil, now, now)
|
||||
if err == nil {
|
||||
t.Fatal("expected a foreign-key error inserting a row against an unknown task_id, got nil")
|
||||
}
|
||||
if isUsageEventUniqueViolation(err) {
|
||||
t.Errorf("isUsageEventUniqueViolation(%v) = true, want false (this is a foreign-key violation)", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
// costSourceUnpriced mirrors internal/task/usage.CostSourceUnpriced. It is
|
||||
// redeclared here rather than imported: internal/task/usage already imports
|
||||
// this package to satisfy its Repository interface, so importing back would
|
||||
// cycle.
|
||||
const costSourceUnpriced = "unpriced"
|
||||
|
||||
// usageTotalsAggregateQuery is AC-18/AC-19's read-side aggregation: every sum
|
||||
// is order-independent (SUM, COUNT, MIN, MAX), so it needs none of
|
||||
// ListTaskUsageEvents' (occurred_at, id) ordering (AC-16). A nullable token
|
||||
// column is coalesced to zero before summing (AC-12) so one not-recorded
|
||||
// sample can never null out the whole total. tokens_total sums the stored
|
||||
// per-row column verbatim - it is never recomputed from the per-kind sums at
|
||||
// read time (AC-19).
|
||||
const usageTotalsAggregateQuery = `
|
||||
SELECT
|
||||
COUNT(*),
|
||||
COALESCE(SUM(tokens_in), 0),
|
||||
COALESCE(SUM(COALESCE(tokens_cached_read, 0)), 0),
|
||||
COALESCE(SUM(COALESCE(tokens_cached_write, 0)), 0),
|
||||
COALESCE(SUM(COALESCE(tokens_out, 0)), 0),
|
||||
COALESCE(SUM(COALESCE(tokens_thought, 0)), 0),
|
||||
COALESCE(SUM(tokens_total), 0),
|
||||
COALESCE(SUM(cost_subcents), 0),
|
||||
COALESCE(SUM(CASE WHEN estimated = 1 THEN 1 ELSE 0 END), 0),
|
||||
COALESCE(SUM(CASE WHEN cost_source = ? THEN 1 ELSE 0 END), 0),
|
||||
COALESCE(SUM(CASE WHEN tokens_out IS NULL THEN 1 ELSE 0 END), 0),
|
||||
MIN(occurred_at),
|
||||
MAX(occurred_at)
|
||||
FROM task_usage_events
|
||||
WHERE %s = ?
|
||||
`
|
||||
|
||||
// GetTaskUsageTotals aggregates every ledger row for taskID, including rows
|
||||
// whose session_id has been cleared by session deletion (AC-18, AC-19).
|
||||
// An unknown task, and a known task with no rows, both return a zeroed
|
||||
// TaskUsageTotals and a nil error (AC-20) - the caller enforces the 404 for
|
||||
// an unknown task, since the aggregate query alone cannot distinguish the
|
||||
// two.
|
||||
func (r *Repository) GetTaskUsageTotals(ctx context.Context, taskID string) (*models.TaskUsageTotals, error) {
|
||||
return r.scanUsageTotals(ctx, "task_id", taskID)
|
||||
}
|
||||
|
||||
// GetSessionUsageTotals aggregates only the ledger rows still bound to
|
||||
// sessionID (AC-18, AC-19). A session with no rows returns a zeroed
|
||||
// TaskUsageTotals and a nil error (AC-20).
|
||||
func (r *Repository) GetSessionUsageTotals(ctx context.Context, sessionID string) (*models.TaskUsageTotals, error) {
|
||||
return r.scanUsageTotals(ctx, "session_id", sessionID)
|
||||
}
|
||||
|
||||
// scanUsageTotals runs usageTotalsAggregateQuery scoped to one column.
|
||||
// scopeColumn is always one of the two internal literals passed by
|
||||
// GetTaskUsageTotals/GetSessionUsageTotals, never caller input, so
|
||||
// interpolating it into the query text carries no injection risk.
|
||||
func (r *Repository) scanUsageTotals(ctx context.Context, scopeColumn, scopeValue string) (*models.TaskUsageTotals, error) {
|
||||
if scopeColumn != "task_id" && scopeColumn != "session_id" {
|
||||
return nil, fmt.Errorf("scanUsageTotals: unsupported scope column %q", scopeColumn)
|
||||
}
|
||||
query := r.ro.Rebind(fmt.Sprintf(usageTotalsAggregateQuery, scopeColumn))
|
||||
|
||||
var (
|
||||
totals models.TaskUsageTotals
|
||||
rawFirstEventAt interface{}
|
||||
rawLastEventAt interface{}
|
||||
unpricedEventCount sql.NullInt64
|
||||
outputIncompleteEventCount sql.NullInt64
|
||||
)
|
||||
err := r.ro.QueryRowxContext(ctx, query, costSourceUnpriced, scopeValue).Scan(
|
||||
&totals.EventCount,
|
||||
&totals.TokensIn,
|
||||
&totals.TokensCachedRead,
|
||||
&totals.TokensCachedWrite,
|
||||
&totals.TokensOut,
|
||||
&totals.TokensThought,
|
||||
&totals.TokensTotal,
|
||||
&totals.CostSubcents,
|
||||
&totals.EstimatedEventCount,
|
||||
&unpricedEventCount,
|
||||
&outputIncompleteEventCount,
|
||||
&rawFirstEventAt,
|
||||
&rawLastEventAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
totals.UnpricedEventCount = unpricedEventCount.Int64
|
||||
totals.OutputTokensComplete = outputIncompleteEventCount.Int64 == 0
|
||||
|
||||
firstEventAt, err := parseUsageTotalsTimestamp(rawFirstEventAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse first_event_at: %w", err)
|
||||
}
|
||||
totals.FirstEventAt = firstEventAt
|
||||
lastEventAt, err := parseUsageTotalsTimestamp(rawLastEventAt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse last_event_at: %w", err)
|
||||
}
|
||||
totals.LastEventAt = lastEventAt
|
||||
|
||||
return &totals, nil
|
||||
}
|
||||
|
||||
// parseUsageTotalsTimestamp converts a MIN/MAX(occurred_at) scan result to a
|
||||
// *time.Time, or nil for SQL NULL (no contributing rows). Scanned as
|
||||
// interface{} because SQLite returns an aggregate over a TIMESTAMP column as
|
||||
// TEXT/[]byte while PostgreSQL returns a native time.Time; reuses
|
||||
// parseTaskActivityTime (task_status_summary.go), which already handles both.
|
||||
func parseUsageTotalsTimestamp(raw interface{}) (*time.Time, error) {
|
||||
if raw == nil {
|
||||
return nil, nil
|
||||
}
|
||||
parsed, err := parseTaskActivityTime(raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &parsed, nil
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/testutil"
|
||||
)
|
||||
|
||||
// GetTaskUsageTotals'/GetSessionUsageTotals' MIN/MAX(occurred_at) columns are
|
||||
// dialect-sensitive: SQLite returns an aggregate over a TIMESTAMP column as
|
||||
// TEXT, PostgreSQL returns a native time.Time, and parseUsageTotalsTimestamp
|
||||
// branches on the scanned Go type to handle both. The SQLite-only tests in
|
||||
// usage_totals_test.go cannot exercise the PostgreSQL branch at all.
|
||||
//
|
||||
// Skips unless KANDEV_TEST_POSTGRES_DSN is set.
|
||||
|
||||
// TestPostgresGetTaskUsageTotals_SumsAndTimestampsRoundTrip is the Postgres
|
||||
// counterpart to TestGetTaskUsageTotals_SumsAcrossSessionsAndClearedSessionRows
|
||||
// and TestGetTaskUsageTotals_FirstAndLastEventAt_MinAndMaxOccurredAt: proves
|
||||
// the aggregate SUM/COUNT columns and the MIN/MAX(occurred_at) timestamp scan
|
||||
// both work against a real PostgreSQL instance, not just SQLite.
|
||||
func TestPostgresGetTaskUsageTotals_SumsAndTimestampsRoundTrip(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-totals-pg", "session-totals-pg")
|
||||
ctx := context.Background()
|
||||
|
||||
base := time.Date(2026, 8, 23, 4, 0, 0, 0, time.UTC)
|
||||
earliest := newTestUsageEvent("evt-totals-pg-1", "task-totals-pg", "session-totals-pg")
|
||||
earliest.OccurredAt = base
|
||||
earliest.CreatedAt = base
|
||||
if err := repo.CreateTaskUsageEvent(ctx, earliest); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent(evt-totals-pg-1): %v", err)
|
||||
}
|
||||
latest := newTestUsageEvent("evt-totals-pg-2", "task-totals-pg", "session-totals-pg")
|
||||
latest.OccurredAt = base.Add(10 * time.Minute)
|
||||
latest.CreatedAt = latest.OccurredAt
|
||||
if err := repo.CreateTaskUsageEvent(ctx, latest); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent(evt-totals-pg-2): %v", err)
|
||||
}
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(ctx, "task-totals-pg")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.EventCount != 2 {
|
||||
t.Fatalf("EventCount = %d, want 2", totals.EventCount)
|
||||
}
|
||||
if totals.TokensIn != 200 {
|
||||
t.Errorf("TokensIn = %d, want 200", totals.TokensIn)
|
||||
}
|
||||
if totals.FirstEventAt == nil || !totals.FirstEventAt.Equal(base) {
|
||||
t.Errorf("FirstEventAt = %v, want %v", totals.FirstEventAt, base)
|
||||
}
|
||||
if totals.LastEventAt == nil || !totals.LastEventAt.Equal(latest.OccurredAt) {
|
||||
t.Errorf("LastEventAt = %v, want %v", totals.LastEventAt, latest.OccurredAt)
|
||||
}
|
||||
|
||||
sessionTotals, err := repo.GetSessionUsageTotals(ctx, "session-totals-pg")
|
||||
if err != nil {
|
||||
t.Fatalf("GetSessionUsageTotals: %v", err)
|
||||
}
|
||||
if sessionTotals.EventCount != 2 {
|
||||
t.Errorf("session EventCount = %d, want 2", sessionTotals.EventCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPostgresGetTaskUsageTotals_NoRows_ReturnsZeroedTotalsWithNilTimestamps
|
||||
// is the Postgres counterpart to TestGetTaskUsageTotals_NoRows_ReturnsZeroedTotals:
|
||||
// a scope with no rows must scan a SQL NULL MIN/MAX into nil timestamps
|
||||
// against PostgreSQL's native time.Time driver values too, not just SQLite's
|
||||
// TEXT NULL.
|
||||
func TestPostgresGetTaskUsageTotals_NoRows_ReturnsZeroedTotalsWithNilTimestamps(t *testing.T) {
|
||||
db := testutil.OpenIsolatedPostgres(t, testutil.PostgresDSNFromEnv(t))
|
||||
repo, err := NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init postgres schema: %v", err)
|
||||
}
|
||||
seedPostgresTaskSession(t, repo, "task-totals-empty-pg", "session-totals-empty-pg")
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-totals-empty-pg")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.EventCount != 0 {
|
||||
t.Errorf("EventCount = %d, want 0", totals.EventCount)
|
||||
}
|
||||
if totals.FirstEventAt != nil || totals.LastEventAt != nil {
|
||||
t.Errorf("timestamps = (%v, %v), want (nil, nil)", totals.FirstEventAt, totals.LastEventAt)
|
||||
}
|
||||
if !totals.OutputTokensComplete {
|
||||
t.Error("OutputTokensComplete = false, want true for a scope with no rows")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
package sqlite
|
||||
|
||||
// Behavioral coverage for GetTaskUsageTotals and GetSessionUsageTotals
|
||||
// (docs/specs/task-cost-ledger/spec.md AC-12, AC-18, AC-19, AC-20): the
|
||||
// read-side aggregation the two HTTP usage routes surface.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
func TestScanUsageTotals_RejectsUnsupportedScopeColumn(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
_, err := repo.scanUsageTotals(context.Background(), "task_id; DROP TABLE tasks;--", "task-1")
|
||||
if err == nil || !strings.Contains(err.Error(), "unsupported scope column") {
|
||||
t.Fatalf("scanUsageTotals error = %v, want an unsupported-scope error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func mustCreateUsageEvent(t *testing.T, repo *Repository, event *models.TaskUsageEvent) {
|
||||
t.Helper()
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("CreateTaskUsageEvent(%s): %v", event.UsageEventID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskUsageTotals_NoRows_ReturnsZeroedTotals(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-empty")
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-empty")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.EventCount != 0 {
|
||||
t.Errorf("EventCount = %d, want 0", totals.EventCount)
|
||||
}
|
||||
if totals.TokensIn != 0 || totals.TokensTotal != 0 || totals.CostSubcents != 0 {
|
||||
t.Errorf("totals = %+v, want every sum zero", totals)
|
||||
}
|
||||
if !totals.OutputTokensComplete {
|
||||
t.Error("OutputTokensComplete = false, want true for a scope with no rows")
|
||||
}
|
||||
if totals.FirstEventAt != nil || totals.LastEventAt != nil {
|
||||
t.Errorf("timestamps = (%v, %v), want (nil, nil)", totals.FirstEventAt, totals.LastEventAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskUsageTotals_UnknownTask_ReturnsZeroedTotals(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-does-not-exist")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.EventCount != 0 {
|
||||
t.Errorf("EventCount = %d, want 0", totals.EventCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetTaskUsageTotals_SumsAcrossSessionsAndClearedSessionRows pins that a
|
||||
// task-scoped total includes every row for the task regardless of session_id,
|
||||
// including a row whose session_id is NULL (e.g. cleared by session
|
||||
// deletion or an AC-33 ownership mismatch).
|
||||
func TestGetTaskUsageTotals_SumsAcrossSessionsAndClearedSessionRows(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-multi")
|
||||
createUsageEventsTestSession(t, repo, "session-a", "task-multi")
|
||||
createUsageEventsTestSession(t, repo, "session-b", "task-multi")
|
||||
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-a", "task-multi", "session-a"))
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-b", "task-multi", "session-b"))
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-c", "task-multi", ""))
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-multi")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.EventCount != 3 {
|
||||
t.Fatalf("EventCount = %d, want 3", totals.EventCount)
|
||||
}
|
||||
if totals.TokensIn != 300 {
|
||||
t.Errorf("TokensIn = %d, want 300 (3 rows of 100 each)", totals.TokensIn)
|
||||
}
|
||||
if totals.CostSubcents != 126 {
|
||||
t.Errorf("CostSubcents = %d, want 126 (3 rows of 42 each)", totals.CostSubcents)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetTaskUsageTotals_TokensTotalIsStoredSumNeverRecomputed pins AC-19: the
|
||||
// response's tokens_total is the sum of each row's stored tokens_total
|
||||
// column, not a value recomputed from the per-kind sums at read time.
|
||||
func TestGetTaskUsageTotals_TokensTotalIsStoredSumNeverRecomputed(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-stored-total")
|
||||
|
||||
event := newTestUsageEvent("evt-stored-total", "task-stored-total", "")
|
||||
event.TokensIn = 10
|
||||
event.TokensCachedRead = nil
|
||||
event.TokensCachedWrite = nil
|
||||
event.TokensOut = nil
|
||||
event.TokensThought = nil
|
||||
event.TokensTotal = 999999 // deliberately inconsistent with the per-kind sum (10)
|
||||
mustCreateUsageEvent(t, repo, event)
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-stored-total")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.TokensTotal != 999999 {
|
||||
t.Errorf("TokensTotal = %d, want the stored value 999999 verbatim, not a recomputed 10", totals.TokensTotal)
|
||||
}
|
||||
if totals.TokensIn != 10 {
|
||||
t.Errorf("TokensIn = %d, want 10", totals.TokensIn)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetTaskUsageTotals_NilTokenColumnsCoalesceToZero pins AC-12: a
|
||||
// not-recorded (nil) token column contributes zero to the sum rather than
|
||||
// nulling out the whole aggregate.
|
||||
func TestGetTaskUsageTotals_NilTokenColumnsCoalesceToZero(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-nil-cols")
|
||||
|
||||
first := newTestUsageEvent("evt-nil-cols-1", "task-nil-cols", "")
|
||||
first.TokensCachedRead = nil
|
||||
first.TokensCachedWrite = nil
|
||||
first.TokensThought = nil
|
||||
measuredZero := int64(0)
|
||||
first.TokensOut = &measuredZero
|
||||
mustCreateUsageEvent(t, repo, first)
|
||||
|
||||
second := newTestUsageEvent("evt-nil-cols-2", "task-nil-cols", "")
|
||||
mustCreateUsageEvent(t, repo, second)
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-nil-cols")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.TokensCachedRead != 20 {
|
||||
t.Errorf("TokensCachedRead = %d, want 20 (nil row contributes 0, not NULL)", totals.TokensCachedRead)
|
||||
}
|
||||
if totals.TokensOut != 30 {
|
||||
t.Errorf("TokensOut = %d, want 30 (measured-zero row contributes 0, non-nil row contributes 30)", totals.TokensOut)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetTaskUsageTotals_OutputTokensComplete_FalseWhenAnyRowMissingTokensOut
|
||||
// pins AC-12/AC-19: a single contributing row with an unrecorded output
|
||||
// count marks the whole total a lower bound.
|
||||
func TestGetTaskUsageTotals_OutputTokensComplete_FalseWhenAnyRowMissingTokensOut(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-incomplete-output")
|
||||
|
||||
complete := newTestUsageEvent("evt-complete", "task-incomplete-output", "")
|
||||
mustCreateUsageEvent(t, repo, complete)
|
||||
|
||||
incomplete := newTestUsageEvent("evt-incomplete", "task-incomplete-output", "")
|
||||
incomplete.TokensOut = nil
|
||||
mustCreateUsageEvent(t, repo, incomplete)
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-incomplete-output")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.OutputTokensComplete {
|
||||
t.Error("OutputTokensComplete = true, want false (one row has an unrecorded output count)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskUsageTotals_OutputTokensComplete_TrueWhenEveryRowHasTokensOut(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-complete-output")
|
||||
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-1", "task-complete-output", ""))
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-2", "task-complete-output", ""))
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-complete-output")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if !totals.OutputTokensComplete {
|
||||
t.Error("OutputTokensComplete = false, want true (every row has a recorded output count)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetTaskUsageTotals_EstimatedAndUnpricedCounts pins AC-19's confidence
|
||||
// flags: estimated_event_count and unpriced_event_count let a caller
|
||||
// distinguish a confident total from a partial one.
|
||||
func TestGetTaskUsageTotals_EstimatedAndUnpricedCounts(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-flags")
|
||||
|
||||
priced := newTestUsageEvent("evt-priced", "task-flags", "")
|
||||
priced.CostSource = "models_dev_list"
|
||||
mustCreateUsageEvent(t, repo, priced)
|
||||
|
||||
unpriced := newTestUsageEvent("evt-unpriced", "task-flags", "")
|
||||
unpriced.CostSource = "unpriced"
|
||||
mustCreateUsageEvent(t, repo, unpriced)
|
||||
|
||||
estimated := newTestUsageEvent("evt-estimated", "task-flags", "")
|
||||
estimated.Estimated = true
|
||||
mustCreateUsageEvent(t, repo, estimated)
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-flags")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.EventCount != 3 {
|
||||
t.Fatalf("EventCount = %d, want 3", totals.EventCount)
|
||||
}
|
||||
if totals.UnpricedEventCount != 1 {
|
||||
t.Errorf("UnpricedEventCount = %d, want 1", totals.UnpricedEventCount)
|
||||
}
|
||||
if totals.EstimatedEventCount != 1 {
|
||||
t.Errorf("EstimatedEventCount = %d, want 1", totals.EstimatedEventCount)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskUsageTotals_FirstAndLastEventAt_MinAndMaxOccurredAt(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-timestamps")
|
||||
|
||||
base := time.Date(2026, 8, 23, 4, 0, 0, 0, time.UTC)
|
||||
earliest := newTestUsageEvent("evt-earliest", "task-timestamps", "")
|
||||
earliest.OccurredAt = base
|
||||
earliest.CreatedAt = base
|
||||
mustCreateUsageEvent(t, repo, earliest)
|
||||
|
||||
latest := newTestUsageEvent("evt-latest", "task-timestamps", "")
|
||||
latest.OccurredAt = base.Add(10 * time.Minute)
|
||||
latest.CreatedAt = latest.OccurredAt
|
||||
mustCreateUsageEvent(t, repo, latest)
|
||||
|
||||
totals, err := repo.GetTaskUsageTotals(context.Background(), "task-timestamps")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.FirstEventAt == nil || !totals.FirstEventAt.Equal(base) {
|
||||
t.Errorf("FirstEventAt = %v, want %v", totals.FirstEventAt, base)
|
||||
}
|
||||
if totals.LastEventAt == nil || !totals.LastEventAt.Equal(latest.OccurredAt) {
|
||||
t.Errorf("LastEventAt = %v, want %v", totals.LastEventAt, latest.OccurredAt)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetSessionUsageTotals_OnlyIncludesRowsForThatSession pins the
|
||||
// session-scoped read's isolation: one task with two sessions, each
|
||||
// session-scoped read returns only its own rows and their sum, never the
|
||||
// other session's or the task's session-less rows.
|
||||
func TestGetSessionUsageTotals_OnlyIncludesRowsForThatSession(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-two-sessions")
|
||||
createUsageEventsTestSession(t, repo, "session-x", "task-two-sessions")
|
||||
createUsageEventsTestSession(t, repo, "session-y", "task-two-sessions")
|
||||
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-x1", "task-two-sessions", "session-x"))
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-x2", "task-two-sessions", "session-x"))
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-y1", "task-two-sessions", "session-y"))
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-task-only", "task-two-sessions", ""))
|
||||
|
||||
totalsX, err := repo.GetSessionUsageTotals(context.Background(), "session-x")
|
||||
if err != nil {
|
||||
t.Fatalf("GetSessionUsageTotals(session-x): %v", err)
|
||||
}
|
||||
if totalsX.EventCount != 2 {
|
||||
t.Errorf("session-x EventCount = %d, want 2", totalsX.EventCount)
|
||||
}
|
||||
if totalsX.TokensIn != 200 {
|
||||
t.Errorf("session-x TokensIn = %d, want 200", totalsX.TokensIn)
|
||||
}
|
||||
|
||||
totalsY, err := repo.GetSessionUsageTotals(context.Background(), "session-y")
|
||||
if err != nil {
|
||||
t.Fatalf("GetSessionUsageTotals(session-y): %v", err)
|
||||
}
|
||||
if totalsY.EventCount != 1 {
|
||||
t.Errorf("session-y EventCount = %d, want 1", totalsY.EventCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetSessionUsageTotals_ZeroUsageSessionInsideTaskWithUsage_ReturnsZeroed
|
||||
// pins the explicitly-flagged Round-5 scenario: a session with zero rows,
|
||||
// inside a task that does have usage, still reports zeroed totals rather
|
||||
// than picking up the task's other rows.
|
||||
func TestGetSessionUsageTotals_ZeroUsageSessionInsideTaskWithUsage_ReturnsZeroed(t *testing.T) {
|
||||
repo := newUsageEventsTestRepo(t)
|
||||
createUsageEventsTestTask(t, repo, "task-has-usage")
|
||||
createUsageEventsTestSession(t, repo, "session-busy", "task-has-usage")
|
||||
createUsageEventsTestSession(t, repo, "session-idle", "task-has-usage")
|
||||
|
||||
mustCreateUsageEvent(t, repo, newTestUsageEvent("evt-busy", "task-has-usage", "session-busy"))
|
||||
|
||||
totals, err := repo.GetSessionUsageTotals(context.Background(), "session-idle")
|
||||
if err != nil {
|
||||
t.Fatalf("GetSessionUsageTotals(session-idle): %v", err)
|
||||
}
|
||||
if totals.EventCount != 0 {
|
||||
t.Errorf("EventCount = %d, want 0", totals.EventCount)
|
||||
}
|
||||
if totals.TokensIn != 0 || totals.TokensTotal != 0 {
|
||||
t.Errorf("totals = %+v, want every sum zero", totals)
|
||||
}
|
||||
if !totals.OutputTokensComplete {
|
||||
t.Error("OutputTokensComplete = false, want true for a scope with no rows")
|
||||
}
|
||||
}
|
||||
@@ -289,6 +289,7 @@ type Repos struct {
|
||||
StatusSummaries repository.TaskStatusSummaryRepository
|
||||
TaskActivity repository.TaskActivityRepository
|
||||
SubagentContexts repository.SubagentContextRepository
|
||||
Usage repository.UsageRepository
|
||||
}
|
||||
|
||||
// Service provides task business logic
|
||||
@@ -314,6 +315,7 @@ type Service struct {
|
||||
statusSummaries repository.TaskStatusSummaryRepository
|
||||
taskActivity repository.TaskActivityRepository
|
||||
subagentContexts repository.SubagentContextRepository
|
||||
usage repository.UsageRepository
|
||||
attachmentSvc *AttachmentService
|
||||
statusSummaryPRs TaskStatusSummaryPRReader
|
||||
statusSummaryProjector TaskStatusSummaryEventProjector
|
||||
@@ -457,6 +459,7 @@ func NewService(repos Repos, eventBus bus.EventBus, log *logger.Logger, discover
|
||||
statusSummaries: repos.StatusSummaries,
|
||||
taskActivity: repos.TaskActivity,
|
||||
subagentContexts: repos.SubagentContexts,
|
||||
usage: repos.Usage,
|
||||
eventBus: eventBus,
|
||||
logger: log,
|
||||
discoveryConfig: discoveryConfig,
|
||||
|
||||
@@ -205,6 +205,7 @@ func createTestServiceWithSessionsRepo(
|
||||
TaskEnvironments: repo,
|
||||
Reviews: repo,
|
||||
ResourceCleanups: repo,
|
||||
Usage: repo,
|
||||
}, eventBus, log, RepositoryDiscoveryConfig{})
|
||||
svc.SetWorkspaceBootstrapper(repo)
|
||||
svc.SetWorkflowStepGetter(&testWorkflowStepGetter{repo: repo})
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
// GetTaskUsageTotals returns the task-cost-ledger aggregate for taskID
|
||||
// (docs/specs/task-cost-ledger/spec.md AC-18, AC-19), including rows whose
|
||||
// session_id was cleared by session deletion. authorizeTaskID alone does not
|
||||
// prove the task exists - it is a no-op for unscoped (internal/synthetic)
|
||||
// callers - so an explicit GetTask call supplies the 404 for an unknown task
|
||||
// in every caller scope.
|
||||
func (s *Service) GetTaskUsageTotals(ctx context.Context, taskID string) (*models.TaskUsageTotals, error) {
|
||||
if err := s.authorizeTaskID(ctx, taskID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := s.tasks.GetTask(ctx, taskID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.usage.GetTaskUsageTotals(ctx, taskID)
|
||||
}
|
||||
|
||||
// GetTaskSessionUsageTotals returns the task-cost-ledger aggregate scoped to
|
||||
// sessionID (AC-18, AC-19). AuthorizeTaskSessionAccess always fetches the
|
||||
// session and checks that it belongs to taskID regardless of caller scope,
|
||||
// so it alone supplies both the unknown-session and mismatched-pair 404s.
|
||||
func (s *Service) GetTaskSessionUsageTotals(ctx context.Context, taskID, sessionID string) (*models.TaskUsageTotals, error) {
|
||||
if err := s.AuthorizeTaskSessionAccess(ctx, taskID, sessionID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.usage.GetSessionUsageTotals(ctx, sessionID)
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
"github.com/kandev/kandev/internal/task/repository/repoerrors"
|
||||
)
|
||||
|
||||
// Service-layer authorization coverage for the task-cost-ledger read surface
|
||||
// (docs/specs/task-cost-ledger/spec.md AC-18): GetTaskUsageTotals and
|
||||
// GetTaskSessionUsageTotals must apply the same per-user workspace scoping as
|
||||
// every other task/session-keyed entry point (see service_access_test.go).
|
||||
|
||||
func seedUsageEvent(t *testing.T, repo interface {
|
||||
CreateTaskUsageEvent(context.Context, *models.TaskUsageEvent) error
|
||||
}, usageEventID, taskID, sessionID string) {
|
||||
t.Helper()
|
||||
tokensCachedRead := int64(20)
|
||||
tokensCachedWrite := int64(5)
|
||||
tokensOut := int64(30)
|
||||
event := &models.TaskUsageEvent{
|
||||
UsageEventID: usageEventID,
|
||||
TaskID: taskID,
|
||||
SessionID: sessionID,
|
||||
AgentType: "claude",
|
||||
Model: "claude-sonnet-5",
|
||||
Provider: "anthropic",
|
||||
TokensIn: 100,
|
||||
TokensCachedRead: &tokensCachedRead,
|
||||
TokensCachedWrite: &tokensCachedWrite,
|
||||
TokensOut: &tokensOut,
|
||||
TokensTotal: 155,
|
||||
CostSubcents: 42,
|
||||
CostSource: "actual",
|
||||
ContractVersion: 1,
|
||||
}
|
||||
if err := repo.CreateTaskUsageEvent(context.Background(), event); err != nil {
|
||||
t.Fatalf("seed usage event %s: %v", usageEventID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskUsageTotals_ScopingAndAggregation(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
seedScopedWorkspaces(t, repo)
|
||||
seedUsageEvent(t, repo, "evt-task-usage-1", "task-b", "sess-b")
|
||||
|
||||
if _, err := svc.GetTaskUsageTotals(ctxAs("user-a"), "task-b"); !errors.Is(err, repoerrors.ErrTaskNotFound) {
|
||||
t.Fatalf("foreign GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
|
||||
for name, ctx := range map[string]context.Context{
|
||||
"owner": ctxAs("user-b"),
|
||||
"internal": context.Background(),
|
||||
} {
|
||||
totals, err := svc.GetTaskUsageTotals(ctx, "task-b")
|
||||
if err != nil {
|
||||
t.Fatalf("%s GetTaskUsageTotals: %v", name, err)
|
||||
}
|
||||
if totals.EventCount != 1 {
|
||||
t.Fatalf("%s EventCount = %d, want 1", name, totals.EventCount)
|
||||
}
|
||||
if totals.TokensIn != 100 {
|
||||
t.Fatalf("%s TokensIn = %d, want 100", name, totals.TokensIn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskUsageTotals_UnknownTask(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
seedScopedWorkspaces(t, repo)
|
||||
|
||||
for name, ctx := range map[string]context.Context{
|
||||
"scoped": ctxAs("user-b"),
|
||||
"internal": context.Background(),
|
||||
} {
|
||||
if _, err := svc.GetTaskUsageTotals(ctx, "task-does-not-exist"); !errors.Is(err, repoerrors.ErrTaskNotFound) {
|
||||
t.Fatalf("%s unknown task: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskSessionUsageTotals_ScopingAndAggregation(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
seedScopedWorkspaces(t, repo)
|
||||
seedUsageEvent(t, repo, "evt-session-usage-1", "task-b", "sess-b")
|
||||
|
||||
if _, err := svc.GetTaskSessionUsageTotals(ctxAs("user-a"), "task-b", "sess-b"); !errors.Is(err, repoerrors.ErrTaskNotFound) {
|
||||
t.Fatalf("foreign GetTaskSessionUsageTotals: %v", err)
|
||||
}
|
||||
|
||||
for name, ctx := range map[string]context.Context{
|
||||
"owner": ctxAs("user-b"),
|
||||
"internal": context.Background(),
|
||||
} {
|
||||
totals, err := svc.GetTaskSessionUsageTotals(ctx, "task-b", "sess-b")
|
||||
if err != nil {
|
||||
t.Fatalf("%s GetTaskSessionUsageTotals: %v", name, err)
|
||||
}
|
||||
if totals.EventCount != 1 {
|
||||
t.Fatalf("%s EventCount = %d, want 1", name, totals.EventCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskSessionUsageTotals_MismatchedPairAndUnknownSession(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
seedScopedWorkspaces(t, repo)
|
||||
if err := repo.CreateTask(context.Background(), &models.Task{
|
||||
ID: "task-b-other", WorkspaceID: "ws-b", WorkflowID: "wf-b", WorkflowStepID: "step-1",
|
||||
Title: "B's other task", State: "created", Priority: "medium",
|
||||
}); err != nil {
|
||||
t.Fatalf("create other task: %v", err)
|
||||
}
|
||||
|
||||
for name, ctx := range map[string]context.Context{
|
||||
"owner": ctxAs("user-b"),
|
||||
"internal": context.Background(),
|
||||
} {
|
||||
if _, err := svc.GetTaskSessionUsageTotals(ctx, "task-b-other", "sess-b"); !errors.Is(err, repoerrors.ErrTaskNotFound) {
|
||||
t.Fatalf("%s mismatched pair: %v", name, err)
|
||||
}
|
||||
// An outright unknown session ID surfaces models.ErrTaskSessionNotFound
|
||||
// (not repoerrors.ErrTaskNotFound) - AuthorizeTaskSessionAccess only
|
||||
// substitutes the task sentinel for a mismatched real session/task pair,
|
||||
// per its own doc comment. handlers.isNotFound's "not found" substring
|
||||
// fallback still maps this to HTTP 404.
|
||||
if _, err := svc.GetTaskSessionUsageTotals(ctx, "task-b", "sess-does-not-exist"); !errors.Is(err, models.ErrTaskSessionNotFound) {
|
||||
t.Fatalf("%s unknown session: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTaskUsageTotals_ZeroUsageReturnsZeroedTotals(t *testing.T) {
|
||||
svc, _, repo := createTestService(t)
|
||||
seedScopedWorkspaces(t, repo)
|
||||
|
||||
totals, err := svc.GetTaskUsageTotals(ctxAs("user-b"), "task-b")
|
||||
if err != nil {
|
||||
t.Fatalf("GetTaskUsageTotals: %v", err)
|
||||
}
|
||||
if totals.EventCount != 0 {
|
||||
t.Fatalf("EventCount = %d, want 0", totals.EventCount)
|
||||
}
|
||||
if !totals.OutputTokensComplete {
|
||||
t.Error("OutputTokensComplete = false, want true for zero usage")
|
||||
}
|
||||
if totals.FirstEventAt != nil || totals.LastEventAt != nil {
|
||||
t.Errorf("timestamps = (%v, %v), want (nil, nil)", totals.FirstEventAt, totals.LastEventAt)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
commoncosts "github.com/kandev/kandev/internal/common/costs"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
// Cost-source labels recorded on task_usage_events.cost_source (AC-6, AC-7,
|
||||
// AC-8).
|
||||
const (
|
||||
CostSourceProviderReported = "provider_reported"
|
||||
CostSourceModelsDevList = "models_dev_list"
|
||||
CostSourceUnpriced = "unpriced"
|
||||
)
|
||||
|
||||
// pricingLookupTimeout is AC-8's writer-owned deadline on a pricing call,
|
||||
// independent of and much shorter than the lookup implementation's own
|
||||
// HTTP timeout, so a pricing-catalogue outage degrades to an unpriced row
|
||||
// instead of stalling the single serial worker.
|
||||
const pricingLookupTimeout = 2 * time.Second
|
||||
|
||||
// PricingLookup is the narrow interface the writer declares for pricing
|
||||
// resolution (AC-26): a single call returning both the rates and the
|
||||
// catalogue version they came from, so a background catalogue refresh
|
||||
// between two separate calls can never pair one snapshot's rates with
|
||||
// another snapshot's version. No internal/office/** type crosses this
|
||||
// boundary - production wiring adapts whatever concrete lookup it uses
|
||||
// (e.g. modelsdev.Client) to this shape outside this package.
|
||||
type PricingLookup interface {
|
||||
LookupForModelWithVersion(ctx context.Context, modelID string) (pricing commoncosts.ModelPricing, version string, ok bool)
|
||||
}
|
||||
|
||||
// buildRow constructs a fully-populated task_usage_events row from a
|
||||
// validated payload (AC-2, AC-5, AC-6..AC-9, AC-23, AC-30, AC-31, AC-35).
|
||||
// occurredAt is supplied by the caller so it is acquired exactly once, on
|
||||
// the worker, immediately before the first repository attempt.
|
||||
func (w *Writer) buildRow(ctx context.Context, p *usageEventPayload, occurredAt time.Time) *models.TaskUsageEvent {
|
||||
usage := p.Usage
|
||||
if usage == nil {
|
||||
usage = &promptUsagePayload{}
|
||||
}
|
||||
|
||||
event := &models.TaskUsageEvent{
|
||||
UsageEventID: p.UsageEventID,
|
||||
TaskID: p.TaskID,
|
||||
SessionID: p.SessionID,
|
||||
TurnID: p.TurnID,
|
||||
AgentProfileID: p.AgentProfileID,
|
||||
AgentType: p.AgentType,
|
||||
Model: p.Model,
|
||||
Provider: resolveProvider(p.AgentType, p.AgentID, p.Model),
|
||||
Estimated: usage.Estimated,
|
||||
ContractVersion: ContractVersion,
|
||||
OccurredAt: occurredAt,
|
||||
CreatedAt: occurredAt,
|
||||
}
|
||||
|
||||
event.TokensIn = usage.InputTokens
|
||||
cachedRead := usage.CachedReadTokens
|
||||
cachedWrite := usage.CachedWriteTokens
|
||||
thought := usage.ThoughtTokens
|
||||
event.TokensCachedRead = &cachedRead
|
||||
event.TokensCachedWrite = &cachedWrite
|
||||
event.TokensThought = &thought
|
||||
if usage.OutputTokensPresent {
|
||||
out := usage.OutputTokens
|
||||
event.TokensOut = &out
|
||||
}
|
||||
var ok bool
|
||||
event.TokensTotal, ok = sumTokenCounts(
|
||||
event.TokensIn, cachedRead, cachedWrite, optionalInt64(event.TokensOut), thought,
|
||||
)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
w.resolveCost(ctx, event, usage)
|
||||
|
||||
return event
|
||||
}
|
||||
|
||||
// resolveProvider derives event.Provider (AC-31): the first non-empty of
|
||||
// the CLI-id mapping applied to agent_type, the same mapping applied to
|
||||
// agent_id, a model-prefix match, then "". Office's resolveProvider applies
|
||||
// the same two extracted helpers in the same order, so the two writers
|
||||
// label the same event identically.
|
||||
func resolveProvider(agentType, agentID, model string) string {
|
||||
if provider := commoncosts.ProviderFromCLI(agentType); provider != "" {
|
||||
return provider
|
||||
}
|
||||
if provider := commoncosts.ProviderFromCLI(agentID); provider != "" {
|
||||
return provider
|
||||
}
|
||||
return commoncosts.ProviderForModel(model)
|
||||
}
|
||||
|
||||
// resolveCost implements AC-6/AC-7/AC-8/AC-9's three-way cost-resolution
|
||||
// ladder: a provider-reported cost is recorded verbatim (including an
|
||||
// explicit zero); otherwise a models.dev catalogue lookup within
|
||||
// pricingLookupTimeout computes cost_subcents from a single pricing
|
||||
// snapshot; otherwise the row is still recorded, unpriced.
|
||||
func (w *Writer) resolveCost(ctx context.Context, event *models.TaskUsageEvent, usage *promptUsagePayload) {
|
||||
if usage.ProviderReportedCostPresent {
|
||||
event.CostSubcents = usage.ProviderReportedCostSubcents
|
||||
event.CostSource = CostSourceProviderReported
|
||||
return
|
||||
}
|
||||
if w.pricing == nil {
|
||||
event.CostSource = CostSourceUnpriced
|
||||
return
|
||||
}
|
||||
|
||||
lookupCtx, cancel := context.WithTimeout(ctx, pricingLookupTimeout)
|
||||
defer cancel()
|
||||
pricing, version, ok := w.pricing.LookupForModelWithVersion(lookupCtx, event.Model)
|
||||
if !ok {
|
||||
event.CostSource = CostSourceUnpriced
|
||||
return
|
||||
}
|
||||
|
||||
cost, ok := commoncosts.CalculateCostSubcentsChecked(
|
||||
event.TokensIn, optionalInt64(event.TokensCachedRead), optionalInt64(event.TokensCachedWrite),
|
||||
optionalInt64(event.TokensOut), pricing,
|
||||
)
|
||||
if !ok {
|
||||
event.CostSource = CostSourceUnpriced
|
||||
return
|
||||
}
|
||||
event.CostSubcents = cost
|
||||
event.CostSource = CostSourceModelsDevList
|
||||
event.RateInputPerMillion = int64Ptr(pricing.InputPerMillion)
|
||||
event.RateCachedReadPerMillion = int64Ptr(pricing.CachedReadPerMillion)
|
||||
event.RateCachedWritePerMillion = int64Ptr(pricing.CachedWritePerMillion)
|
||||
event.RateOutputPerMillion = int64Ptr(pricing.OutputPerMillion)
|
||||
event.PricingCatalogVersion = version
|
||||
}
|
||||
|
||||
func optionalInt64(v *int64) int64 {
|
||||
if v == nil {
|
||||
return 0
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
func sumTokenCounts(values ...int64) (int64, bool) {
|
||||
var total int64
|
||||
for _, value := range values {
|
||||
if value < 0 || total > math.MaxInt64-value {
|
||||
return 0, false
|
||||
}
|
||||
total += value
|
||||
}
|
||||
return total, true
|
||||
}
|
||||
|
||||
func int64Ptr(v int64) *int64 {
|
||||
return &v
|
||||
}
|
||||
@@ -0,0 +1,292 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
commoncosts "github.com/kandev/kandev/internal/common/costs"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
)
|
||||
|
||||
var fixedOccurredAt = time.Date(2026, 8, 23, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
// TestBuildRow_TokensTotal_SumsAllFiveClasses pins AC-23: tokens_total is
|
||||
// computed by the writer, never copied from the payload's own total_tokens.
|
||||
func TestBuildRow_TokensTotal_SumsAllFiveClasses(t *testing.T) {
|
||||
w := &Writer{}
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1", TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{
|
||||
InputTokens: 100, OutputTokens: 30, OutputTokensPresent: true,
|
||||
CachedReadTokens: 25, CachedWriteTokens: 5, ThoughtTokens: 10,
|
||||
TotalTokens: 999, // deliberately wrong, to prove it's ignored
|
||||
},
|
||||
}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event.TokensTotal != 170 {
|
||||
t.Errorf("TokensTotal = %d, want 170 (100+30+25+5+10), payload total_tokens must be ignored", event.TokensTotal)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRow_TokensTotalOverflow_ReturnsNil(t *testing.T) {
|
||||
w := &Writer{}
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-overflow", TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{InputTokens: math.MaxInt64, CachedReadTokens: 1},
|
||||
}
|
||||
if event := w.buildRow(context.Background(), p, fixedOccurredAt); event != nil {
|
||||
t.Fatalf("buildRow returned %+v, want nil for an overflowing token total", event)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRow_OutputTokensNotPresent_LeavesTokensOutNil pins AC-4/AC-30:
|
||||
// an unsampled output-token count stores as NULL (nil), not zero, and
|
||||
// contributes zero to tokens_total either way.
|
||||
func TestBuildRow_OutputTokensNotPresent_LeavesTokensOutNil(t *testing.T) {
|
||||
w := &Writer{}
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1", TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{InputTokens: 50, OutputTokensPresent: false},
|
||||
}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event.TokensOut != nil {
|
||||
t.Errorf("TokensOut = %v, want nil", event.TokensOut)
|
||||
}
|
||||
if event.TokensTotal != 50 {
|
||||
t.Errorf("TokensTotal = %d, want 50", event.TokensTotal)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRow_OccurredAtAndCreatedAt_EqualTheSuppliedInstant pins that
|
||||
// both timestamps are stamped once, from the caller-supplied occurredAt,
|
||||
// and are equal on a first write.
|
||||
func TestBuildRow_OccurredAtAndCreatedAt_EqualTheSuppliedInstant(t *testing.T) {
|
||||
w := &Writer{}
|
||||
p := &usageEventPayload{UsageEventID: "evt-1", TaskID: "task-1", Usage: &promptUsagePayload{}}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if !event.OccurredAt.Equal(fixedOccurredAt) || !event.CreatedAt.Equal(fixedOccurredAt) {
|
||||
t.Errorf("OccurredAt=%v CreatedAt=%v, want both %v", event.OccurredAt, event.CreatedAt, fixedOccurredAt)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRow_ContractVersion_IsOne pins AC-5.
|
||||
func TestBuildRow_ContractVersion_IsOne(t *testing.T) {
|
||||
w := &Writer{}
|
||||
p := &usageEventPayload{UsageEventID: "evt-1", TaskID: "task-1", Usage: &promptUsagePayload{}}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event.ContractVersion != 1 {
|
||||
t.Errorf("ContractVersion = %d, want 1", event.ContractVersion)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRow_CarriesModelAgentTypeAgentProfileIDVerbatim pins AC-35.
|
||||
func TestBuildRow_CarriesModelAgentTypeAgentProfileIDVerbatim(t *testing.T) {
|
||||
w := &Writer{}
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1", TaskID: "task-1",
|
||||
Model: "claude-sonnet-5", AgentType: "claude-acp", AgentProfileID: "profile-1",
|
||||
Usage: &promptUsagePayload{},
|
||||
}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event.Model != "claude-sonnet-5" || event.AgentType != "claude-acp" || event.AgentProfileID != "profile-1" {
|
||||
t.Errorf("event = %+v, fields did not carry through verbatim", event)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRow_MissingModelAgentTypeAgentProfileID_DefaultToEmptyString
|
||||
// pins AC-35's "default to ” only when the payload's own field is empty"
|
||||
// rule.
|
||||
func TestBuildRow_MissingModelAgentTypeAgentProfileID_DefaultToEmptyString(t *testing.T) {
|
||||
w := &Writer{}
|
||||
p := &usageEventPayload{UsageEventID: "evt-1", TaskID: "task-1", Usage: &promptUsagePayload{}}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event.Model != "" || event.AgentType != "" || event.AgentProfileID != "" {
|
||||
t.Errorf("event = %+v, want empty-string defaults", event)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveProvider pins AC-31's fixed fallback order.
|
||||
func TestResolveProvider(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
agentType, agentID, model string
|
||||
want string
|
||||
}{
|
||||
{"agent_type CLI mapping wins first", "claude-acp", "", "gpt-4", "anthropic"},
|
||||
{"agent_id CLI mapping used when agent_type doesn't map", "unknown-cli", "codex-acp", "claude-sonnet-5", "openai"},
|
||||
{"model prefix used when neither CLI id maps", "unknown-cli", "unknown-cli", "gemini-1.5", "google"},
|
||||
{"empty string when nothing matches", "unknown-cli", "unknown-cli", "unknown-model", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := resolveProvider(tt.agentType, tt.agentID, tt.model); got != tt.want {
|
||||
t.Errorf("resolveProvider(%q,%q,%q) = %q, want %q", tt.agentType, tt.agentID, tt.model, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// fakePricingLookup is a test double for PricingLookup. If block is true,
|
||||
// LookupForModelWithVersion blocks until ctx is done and then reports not
|
||||
// ok, modeling a pricing backend that never itself respects a deadline
|
||||
// shorter than the writer's own (AC-8's "never returns" scenario).
|
||||
type fakePricingLookup struct {
|
||||
pricing commoncosts.ModelPricing
|
||||
version string
|
||||
ok bool
|
||||
block bool
|
||||
calls int
|
||||
}
|
||||
|
||||
func (f *fakePricingLookup) LookupForModelWithVersion(ctx context.Context, _ string) (commoncosts.ModelPricing, string, bool) {
|
||||
f.calls++
|
||||
if f.block {
|
||||
<-ctx.Done()
|
||||
return commoncosts.ModelPricing{}, "", false
|
||||
}
|
||||
return f.pricing, f.version, f.ok
|
||||
}
|
||||
|
||||
// TestResolveCost_ProviderReportedCostPresent_RecordsVerbatimSkipsPricing
|
||||
// pins AC-6, including the explicit-zero case.
|
||||
func TestResolveCost_ProviderReportedCostPresent_RecordsVerbatimSkipsPricing(t *testing.T) {
|
||||
pricing := &fakePricingLookup{ok: true, pricing: commoncosts.ModelPricing{InputPerMillion: 300}}
|
||||
w := &Writer{pricing: pricing}
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1", TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{ProviderReportedCostPresent: true, ProviderReportedCostSubcents: 0},
|
||||
}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event.CostSource != CostSourceProviderReported {
|
||||
t.Errorf("CostSource = %q, want %q", event.CostSource, CostSourceProviderReported)
|
||||
}
|
||||
if event.CostSubcents != 0 {
|
||||
t.Errorf("CostSubcents = %d, want 0 (explicit provider-reported zero must be recorded, not treated as absent)", event.CostSubcents)
|
||||
}
|
||||
if pricing.calls != 0 {
|
||||
t.Errorf("pricing lookup was called %d times, want 0 (provider-reported cost must skip pricing entirely)", pricing.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveCost_PricingResolves_ComputesCostAndPopulatesRates pins AC-7,
|
||||
// AC-9, AC-26: a single lookup call populates cost_subcents, cost_source,
|
||||
// all four rate columns, and the pricing catalogue version together.
|
||||
func TestResolveCost_PricingResolves_ComputesCostAndPopulatesRates(t *testing.T) {
|
||||
pricing := &fakePricingLookup{
|
||||
ok: true, version: "catalogue-v7",
|
||||
pricing: commoncosts.ModelPricing{InputPerMillion: 3_000_000, CachedReadPerMillion: 300_000, CachedWritePerMillion: 3_750_000, OutputPerMillion: 15_000_000},
|
||||
}
|
||||
w := &Writer{pricing: pricing}
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1", TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{
|
||||
InputTokens: 100, OutputTokens: 30, OutputTokensPresent: true,
|
||||
CachedReadTokens: 25, CachedWriteTokens: 5, Estimated: true,
|
||||
},
|
||||
}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
|
||||
if event.CostSource != CostSourceModelsDevList {
|
||||
t.Errorf("CostSource = %q, want %q", event.CostSource, CostSourceModelsDevList)
|
||||
}
|
||||
wantCost := (int64(100)*3_000_000 + 25*300_000 + 5*3_750_000 + 30*15_000_000) / 1_000_000
|
||||
if event.CostSubcents != wantCost {
|
||||
t.Errorf("CostSubcents = %d, want %d", event.CostSubcents, wantCost)
|
||||
}
|
||||
if event.RateInputPerMillion == nil || *event.RateInputPerMillion != 3_000_000 {
|
||||
t.Errorf("RateInputPerMillion = %v, want 3000000", event.RateInputPerMillion)
|
||||
}
|
||||
if event.RateCachedReadPerMillion == nil || *event.RateCachedReadPerMillion != 300_000 {
|
||||
t.Errorf("RateCachedReadPerMillion = %v, want 300000", event.RateCachedReadPerMillion)
|
||||
}
|
||||
if event.RateCachedWritePerMillion == nil || *event.RateCachedWritePerMillion != 3_750_000 {
|
||||
t.Errorf("RateCachedWritePerMillion = %v, want 3750000", event.RateCachedWritePerMillion)
|
||||
}
|
||||
if event.RateOutputPerMillion == nil || *event.RateOutputPerMillion != 15_000_000 {
|
||||
t.Errorf("RateOutputPerMillion = %v, want 15000000", event.RateOutputPerMillion)
|
||||
}
|
||||
if event.PricingCatalogVersion != "catalogue-v7" {
|
||||
t.Errorf("PricingCatalogVersion = %q, want %q", event.PricingCatalogVersion, "catalogue-v7")
|
||||
}
|
||||
if !event.Estimated {
|
||||
t.Error("Estimated = false, want true (AC-9: recorded independently of cost_source, straight from the payload)")
|
||||
}
|
||||
if pricing.calls != 1 {
|
||||
t.Errorf("pricing lookup was called %d times, want exactly 1 (a single call must supply both rates and version)", pricing.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveCost_PricingNotOK_FallsBackToUnpriced pins AC-8: a pricing miss
|
||||
// still records the row, with cost_subcents=0 and no rate columns.
|
||||
func TestResolveCost_PricingNotOK_FallsBackToUnpriced(t *testing.T) {
|
||||
pricing := &fakePricingLookup{ok: false}
|
||||
w := &Writer{pricing: pricing}
|
||||
p := &usageEventPayload{UsageEventID: "evt-1", TaskID: "task-1", Usage: &promptUsagePayload{InputTokens: 10}}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event.CostSource != CostSourceUnpriced {
|
||||
t.Errorf("CostSource = %q, want %q", event.CostSource, CostSourceUnpriced)
|
||||
}
|
||||
if event.CostSubcents != 0 {
|
||||
t.Errorf("CostSubcents = %d, want 0", event.CostSubcents)
|
||||
}
|
||||
if event.RateInputPerMillion != nil {
|
||||
t.Errorf("RateInputPerMillion = %v, want nil", event.RateInputPerMillion)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCost_OverflowFallsBackToUnpriced(t *testing.T) {
|
||||
pricing := &fakePricingLookup{
|
||||
ok: true,
|
||||
pricing: commoncosts.ModelPricing{InputPerMillion: math.MaxInt64},
|
||||
}
|
||||
w := &Writer{pricing: pricing}
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-cost-overflow", TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{InputTokens: 2},
|
||||
}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event == nil {
|
||||
t.Fatal("buildRow returned nil for a cost overflow; token total still fits")
|
||||
}
|
||||
if event.CostSource != CostSourceUnpriced || event.CostSubcents != 0 {
|
||||
t.Fatalf("overflowing cost = (%q, %d), want (unpriced, 0)", event.CostSource, event.CostSubcents)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveCost_NilPricingLookup_FallsBackToUnpriced pins AC-8's
|
||||
// no-lookup-wired degradation (relevant before Task 12 wires production
|
||||
// pricing).
|
||||
func TestResolveCost_NilPricingLookup_FallsBackToUnpriced(t *testing.T) {
|
||||
w := &Writer{pricing: nil}
|
||||
p := &usageEventPayload{UsageEventID: "evt-1", TaskID: "task-1", Usage: &promptUsagePayload{InputTokens: 10}}
|
||||
event := w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
if event.CostSource != CostSourceUnpriced {
|
||||
t.Errorf("CostSource = %q, want %q", event.CostSource, CostSourceUnpriced)
|
||||
}
|
||||
}
|
||||
|
||||
// TestResolveCost_PricingNeverReturns_HonorsWriterOwnedDeadline pins AC-8's
|
||||
// 2-second writer-owned deadline: a pricing lookup that only respects
|
||||
// context cancellation must not stall buildRow past pricingLookupTimeout.
|
||||
// Runs inside synctest so the deadline's real time.Timer advances on the
|
||||
// fake clock instead of costing real wall time.
|
||||
func TestResolveCost_PricingNeverReturns_HonorsWriterOwnedDeadline(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
pricing := &fakePricingLookup{block: true}
|
||||
w := &Writer{pricing: pricing}
|
||||
p := &usageEventPayload{UsageEventID: "evt-1", TaskID: "task-1", Usage: &promptUsagePayload{InputTokens: 10}}
|
||||
|
||||
done := make(chan *models.TaskUsageEvent, 1)
|
||||
go func() {
|
||||
done <- w.buildRow(context.Background(), p, fixedOccurredAt)
|
||||
}()
|
||||
|
||||
event := <-done
|
||||
if event.CostSource != CostSourceUnpriced {
|
||||
t.Errorf("CostSource = %q, want %q", event.CostSource, CostSourceUnpriced)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// Package usage subscribes to the session-prompt-usage bus subject and
|
||||
// records an append-only task_usage_events ledger row per observed event
|
||||
// (docs/specs/task-cost-ledger/spec.md). It must not import any
|
||||
// internal/office/** package (AC-25): Office records its own, separate
|
||||
// cost_events ledger off the same bus subject, and this package is a
|
||||
// sibling consumer, not a dependent.
|
||||
package usage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
)
|
||||
|
||||
// usageEventPayload mirrors lifecycle.SessionPromptUsageEventPayload's JSON
|
||||
// shape field-for-field (AC-25). It is declared locally rather than
|
||||
// importing that type because internal/agent/runtime/lifecycle imports
|
||||
// internal/task/models, so importing it here would point a dependency edge
|
||||
// back up the stack the wrong way; the wire contract is the JSON, not a
|
||||
// shared Go type.
|
||||
type usageEventPayload struct {
|
||||
TaskID string `json:"task_id"`
|
||||
SessionID string `json:"session_id"`
|
||||
AgentID string `json:"agent_id"`
|
||||
AgentProfileID string `json:"agent_profile_id,omitempty"`
|
||||
AgentType string `json:"agent_type,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Usage *promptUsagePayload `json:"usage"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
TurnID string `json:"turn_id,omitempty"`
|
||||
UsageEventID string `json:"usage_event_id,omitempty"`
|
||||
}
|
||||
|
||||
// promptUsagePayload mirrors streams.PromptUsage's JSON shape (AC-30).
|
||||
// OutputTokensPresent is always serialized by the producer, which is what
|
||||
// makes an unsampled output-token count distinguishable from a measured
|
||||
// zero; CachedReadTokens, CachedWriteTokens and ThoughtTokens are plain
|
||||
// omitempty ints, so for those three an absent field and a measured zero
|
||||
// are indistinguishable on the wire by design (AC-30).
|
||||
type promptUsagePayload struct {
|
||||
InputTokens int64 `json:"input_tokens"`
|
||||
OutputTokens int64 `json:"output_tokens"`
|
||||
OutputTokensPresent bool `json:"output_tokens_present"`
|
||||
CachedReadTokens int64 `json:"cached_read_tokens,omitempty"`
|
||||
CachedWriteTokens int64 `json:"cached_write_tokens,omitempty"`
|
||||
ThoughtTokens int64 `json:"thought_tokens,omitempty"`
|
||||
TotalTokens int64 `json:"total_tokens,omitempty"`
|
||||
ProviderReportedCostSubcents int64 `json:"provider_reported_cost_subcents,omitempty"`
|
||||
ProviderReportedCostPresent bool `json:"provider_reported_cost_present,omitempty"`
|
||||
Estimated bool `json:"estimated,omitempty"`
|
||||
}
|
||||
|
||||
// decodePayload extracts a typed usageEventPayload from a bus event via a
|
||||
// JSON marshal/unmarshal round trip (mirrors office/service.decodeEventData
|
||||
// for the same wire event, without importing that unexported helper).
|
||||
func decodePayload(event *bus.Event) (*usageEventPayload, error) {
|
||||
raw, err := json.Marshal(event.Data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var payload usageEventPayload
|
||||
if err := json.Unmarshal(raw, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &payload, nil
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
)
|
||||
|
||||
// TestDecodePayload_ValidEvent_PopulatesAllFields pins that decodePayload's
|
||||
// locally-declared struct mirrors lifecycle.SessionPromptUsageEventPayload's
|
||||
// JSON tags (AC-25) well enough to round-trip every field the writer needs.
|
||||
func TestDecodePayload_ValidEvent_PopulatesAllFields(t *testing.T) {
|
||||
event := &bus.Event{
|
||||
Data: map[string]any{
|
||||
"task_id": "task-1",
|
||||
"session_id": "session-1",
|
||||
"agent_id": "claude-acp",
|
||||
"agent_profile_id": "profile-1",
|
||||
"agent_type": "claude-acp",
|
||||
"model": "claude-sonnet-5",
|
||||
"turn_id": "turn-1",
|
||||
"usage_event_id": "evt-1",
|
||||
"timestamp": "2026-08-23T12:00:00Z",
|
||||
"usage": map[string]any{
|
||||
"input_tokens": int64(100),
|
||||
"output_tokens": int64(30),
|
||||
"output_tokens_present": true,
|
||||
"cached_read_tokens": int64(25),
|
||||
"cached_write_tokens": int64(5),
|
||||
"thought_tokens": int64(10),
|
||||
"total_tokens": int64(170),
|
||||
"provider_reported_cost_present": false,
|
||||
"estimated": false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
payload, err := decodePayload(event)
|
||||
if err != nil {
|
||||
t.Fatalf("decodePayload: %v", err)
|
||||
}
|
||||
|
||||
if payload.TaskID != "task-1" || payload.SessionID != "session-1" ||
|
||||
payload.AgentID != "claude-acp" || payload.AgentProfileID != "profile-1" ||
|
||||
payload.AgentType != "claude-acp" || payload.Model != "claude-sonnet-5" ||
|
||||
payload.TurnID != "turn-1" || payload.UsageEventID != "evt-1" {
|
||||
t.Fatalf("payload = %+v, fields did not round-trip", payload)
|
||||
}
|
||||
|
||||
if payload.Usage == nil {
|
||||
t.Fatal("payload.Usage = nil, want populated")
|
||||
}
|
||||
u := payload.Usage
|
||||
if u.InputTokens != 100 || u.OutputTokens != 30 || !u.OutputTokensPresent ||
|
||||
u.CachedReadTokens != 25 || u.CachedWriteTokens != 5 || u.ThoughtTokens != 10 {
|
||||
t.Errorf("usage = %+v, fields did not round-trip", u)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDecodePayload_OutputTokensPresentFalse_PinsAbsenceDistinctFromZero
|
||||
// pins AC-4/AC-30's NULL-vs-zero distinction for output tokens: when the
|
||||
// producer omits an output-token sample, output_tokens_present decodes to
|
||||
// false even though output_tokens itself defaults to the JSON zero value.
|
||||
func TestDecodePayload_OutputTokensPresentFalse_PinsAbsenceDistinctFromZero(t *testing.T) {
|
||||
event := &bus.Event{
|
||||
Data: map[string]any{
|
||||
"task_id": "task-1",
|
||||
"usage_event_id": "evt-2",
|
||||
"usage": map[string]any{
|
||||
"input_tokens": int64(50),
|
||||
// output_tokens_present omitted entirely - never sampled.
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
payload, err := decodePayload(event)
|
||||
if err != nil {
|
||||
t.Fatalf("decodePayload: %v", err)
|
||||
}
|
||||
if payload.Usage.OutputTokensPresent {
|
||||
t.Error("OutputTokensPresent = true, want false when the field is absent from the wire payload")
|
||||
}
|
||||
if payload.Usage.OutputTokens != 0 {
|
||||
t.Errorf("OutputTokens = %d, want 0 (JSON zero value, not meaningful when Present is false)", payload.Usage.OutputTokens)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDecodePayload_UndecodableData_ReturnsError pins the dropped:decode_error
|
||||
// state-machine transition's precondition: a payload that cannot round-trip
|
||||
// through JSON at all is a decode error, not a validation error.
|
||||
func TestDecodePayload_UndecodableData_ReturnsError(t *testing.T) {
|
||||
event := &bus.Event{Data: func() {}}
|
||||
|
||||
if _, err := decodePayload(event); err == nil {
|
||||
t.Fatal("decodePayload: got nil error, want an error for unmarshalable Data")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.uber.org/goleak"
|
||||
)
|
||||
|
||||
// TestMain asserts that no goroutines from this package outlive the test
|
||||
// process. Writer owns a single worker goroutine guarded by Start/Stop;
|
||||
// goleak catches regressions where Start forgets to register on the
|
||||
// WaitGroup, or Stop returns before the worker actually exits.
|
||||
func TestMain(m *testing.M) {
|
||||
goleak.VerifyTestMain(m)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"expvar"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// expvar maps published at package init, exposed via stdlib's /debug/vars
|
||||
// handler (dev mode). Mirrors internal/office/service/cost_metrics.go's
|
||||
// shape for the same reason: this ledger's writer has no other observable
|
||||
// failure surface between "an event arrived on the bus" and "a row exists",
|
||||
// so every terminal outcome bumps exactly one of these (AC-27).
|
||||
var (
|
||||
eventsWrittenTotal = expvar.NewMap("task_usage_events_written_total")
|
||||
eventsDroppedTotal = expvar.NewMap("task_usage_events_dropped_total")
|
||||
)
|
||||
|
||||
// Reasons a session-prompt-usage event never became a stored
|
||||
// task_usage_events row (AC-27). Every terminal state-machine transition
|
||||
// increments exactly one of these, via whichever of the fixed
|
||||
// decode/admit/validate/ownership/insert stages the event fails first.
|
||||
const (
|
||||
dropReasonDecodeError = "decode_error"
|
||||
dropReasonUnattributable = "unattributable"
|
||||
dropReasonInvalid = "invalid"
|
||||
dropReasonDuplicate = "duplicate"
|
||||
dropReasonOverflow = "overflow"
|
||||
dropReasonDrainTimeout = "drain_timeout"
|
||||
dropReasonShutdown = "shutdown"
|
||||
dropReasonError = "error"
|
||||
)
|
||||
|
||||
const (
|
||||
metricEventWritten = "task_usage.metric.event_written"
|
||||
metricEventDropped = "task_usage.metric.event_dropped"
|
||||
)
|
||||
|
||||
func usageMetricLabel(pairs ...string) string {
|
||||
if len(pairs)%2 != 0 {
|
||||
return ""
|
||||
}
|
||||
parts := make([]string, 0, len(pairs)/2)
|
||||
for i := 0; i < len(pairs); i += 2 {
|
||||
parts = append(parts, pairs[i]+"="+pairs[i+1])
|
||||
}
|
||||
return strings.Join(parts, ";")
|
||||
}
|
||||
|
||||
// recordWritten bumps task_usage_events_written_total, keyed by the row's
|
||||
// resolved cost source and provider.
|
||||
func (w *Writer) recordWritten(source, provider string) {
|
||||
eventsWrittenTotal.Add(usageMetricLabel("source", source, "provider", provider), 1)
|
||||
if w.log == nil {
|
||||
return
|
||||
}
|
||||
w.log.Debug(metricEventWritten, zap.String("cost_source", source), zap.String("provider", provider))
|
||||
}
|
||||
|
||||
// recordDropped bumps task_usage_events_dropped_total, keyed by reason.
|
||||
func (w *Writer) recordDropped(reason, taskID string) {
|
||||
eventsDroppedTotal.Add(usageMetricLabel("reason", reason), 1)
|
||||
if w.log == nil {
|
||||
return
|
||||
}
|
||||
w.log.Warn(metricEventDropped, zap.String("reason", reason), zap.String("task_id", taskID))
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package usage
|
||||
|
||||
// validateStage implements AC-27's validate stage: four sub-checks run in
|
||||
// a fixed order, so an event failing more than one is still classified
|
||||
// deterministically by whichever check it fails first. Returns "" when the
|
||||
// payload passes every check.
|
||||
func validateStage(p *usageEventPayload) string {
|
||||
switch {
|
||||
case p.UsageEventID == "":
|
||||
return dropReasonInvalid
|
||||
case p.TaskID == "":
|
||||
return dropReasonUnattributable
|
||||
case hasNegativeValue(p.Usage):
|
||||
return dropReasonInvalid
|
||||
case hasTokenTotalOverflow(p.Usage):
|
||||
return dropReasonOverflow
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// hasNegativeValue reports whether any token or cost value on u is
|
||||
// negative (AC-27, sub-check 3). A nil Usage is a defensive-only case -
|
||||
// publishPromptUsage never publishes with a nil usage object (AC-24) - and
|
||||
// contributes no negative value.
|
||||
func hasNegativeValue(u *promptUsagePayload) bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
if u.InputTokens < 0 || u.OutputTokens < 0 || u.CachedReadTokens < 0 ||
|
||||
u.CachedWriteTokens < 0 || u.ThoughtTokens < 0 {
|
||||
return true
|
||||
}
|
||||
return u.ProviderReportedCostPresent && u.ProviderReportedCostSubcents < 0
|
||||
}
|
||||
|
||||
func hasTokenTotalOverflow(u *promptUsagePayload) bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := sumTokenCounts(u.InputTokens, u.CachedReadTokens, u.CachedWriteTokens, optionalOutputTokens(u), u.ThoughtTokens)
|
||||
return !ok
|
||||
}
|
||||
|
||||
func optionalOutputTokens(u *promptUsagePayload) int64 {
|
||||
if !u.OutputTokensPresent {
|
||||
return 0
|
||||
}
|
||||
return u.OutputTokens
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestValidateStage_AllChecksPass_ReturnsEmptyReason pins the happy path:
|
||||
// a fully-formed payload passes validate with no drop reason.
|
||||
func TestValidateStage_AllChecksPass_ReturnsEmptyReason(t *testing.T) {
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1",
|
||||
TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{InputTokens: 100},
|
||||
}
|
||||
if reason := validateStage(p); reason != "" {
|
||||
t.Errorf("validateStage = %q, want empty (valid)", reason)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateStage_MissingUsageEventID_ReturnsInvalid pins sub-check (1):
|
||||
// checked first, regardless of what else is wrong with the payload.
|
||||
func TestValidateStage_MissingUsageEventID_ReturnsInvalid(t *testing.T) {
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "",
|
||||
TaskID: "",
|
||||
Usage: &promptUsagePayload{InputTokens: -1},
|
||||
}
|
||||
if reason := validateStage(p); reason != dropReasonInvalid {
|
||||
t.Errorf("validateStage = %q, want %q (usage_event_id missing wins over every other failure)", reason, dropReasonInvalid)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateStage_MissingTaskID_ReturnsUnattributable pins sub-check (2).
|
||||
func TestValidateStage_MissingTaskID_ReturnsUnattributable(t *testing.T) {
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1",
|
||||
TaskID: "",
|
||||
Usage: &promptUsagePayload{InputTokens: 10},
|
||||
}
|
||||
if reason := validateStage(p); reason != dropReasonUnattributable {
|
||||
t.Errorf("validateStage = %q, want %q", reason, dropReasonUnattributable)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateStage_MissingTaskIDAndNegativeValue_ReturnsUnattributable pins
|
||||
// the specific (2)-vs-(3) ordering AC-27 calls out by name: a payload
|
||||
// missing task_id AND carrying a negative token value counts
|
||||
// unattributable, never reaching the negative-value check.
|
||||
func TestValidateStage_MissingTaskIDAndNegativeValue_ReturnsUnattributable(t *testing.T) {
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1",
|
||||
TaskID: "",
|
||||
Usage: &promptUsagePayload{InputTokens: -5},
|
||||
}
|
||||
if reason := validateStage(p); reason != dropReasonUnattributable {
|
||||
t.Errorf("validateStage = %q, want %q (task_id-missing must win over negative-value)", reason, dropReasonUnattributable)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateStage_NegativeValue_ReturnsInvalid pins sub-check (3) across
|
||||
// every field it covers, including the provider-reported cost.
|
||||
func TestValidateStage_NegativeValue_ReturnsInvalid(t *testing.T) {
|
||||
tests := map[string]*promptUsagePayload{
|
||||
"negative input tokens": {InputTokens: -1},
|
||||
"negative output tokens": {OutputTokens: -1},
|
||||
"negative cached read tokens": {CachedReadTokens: -1},
|
||||
"negative cached write tokens": {CachedWriteTokens: -1},
|
||||
"negative thought tokens": {ThoughtTokens: -1},
|
||||
"negative provider-reported cost": {ProviderReportedCostPresent: true, ProviderReportedCostSubcents: -1},
|
||||
}
|
||||
for name, usage := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
p := &usageEventPayload{UsageEventID: "evt-1", TaskID: "task-1", Usage: usage}
|
||||
if reason := validateStage(p); reason != dropReasonInvalid {
|
||||
t.Errorf("validateStage = %q, want %q", reason, dropReasonInvalid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateStage_NegativeProviderCostButNotPresent_PassesValidation pins
|
||||
// that an unset provider-reported cost is never inspected for sign, since
|
||||
// ProviderReportedCostPresent=false means the field simply wasn't sent.
|
||||
func TestValidateStage_NegativeProviderCostButNotPresent_PassesValidation(t *testing.T) {
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1",
|
||||
TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{ProviderReportedCostPresent: false, ProviderReportedCostSubcents: -1},
|
||||
}
|
||||
if reason := validateStage(p); reason != "" {
|
||||
t.Errorf("validateStage = %q, want empty (unset provider cost is not sign-checked)", reason)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateStage_NilUsage_PassesValidation pins the defensive-only nil
|
||||
// guard: publishPromptUsage never publishes a nil Usage (AC-24), but the
|
||||
// decode stage doesn't reject one either, so validate must not panic.
|
||||
func TestValidateStage_NilUsage_PassesValidation(t *testing.T) {
|
||||
p := &usageEventPayload{UsageEventID: "evt-1", TaskID: "task-1", Usage: nil}
|
||||
if reason := validateStage(p); reason != "" {
|
||||
t.Errorf("validateStage = %q, want empty", reason)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateStage_TokenTotalOverflow_ReturnsOverflow(t *testing.T) {
|
||||
p := &usageEventPayload{
|
||||
UsageEventID: "evt-1",
|
||||
TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{InputTokens: math.MaxInt64, CachedReadTokens: 1},
|
||||
}
|
||||
if reason := validateStage(p); reason != dropReasonOverflow {
|
||||
t.Errorf("validateStage = %q, want %q for an overflowing token total", reason, dropReasonOverflow)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
sqliterepo "github.com/kandev/kandev/internal/task/repository/sqlite"
|
||||
)
|
||||
|
||||
// ContractVersion is AC-5's per-row contract version: it advances only when
|
||||
// a column's meaning changes, never when a column is merely added.
|
||||
const ContractVersion = 1
|
||||
|
||||
// eventQueueCapacity is AC-34's fixed bounded-channel size between the bus
|
||||
// callback and the single worker goroutine.
|
||||
const eventQueueCapacity = 1024
|
||||
|
||||
// drainDeadline is AC-34's bound on how long Stop waits for buffered events
|
||||
// to finish draining before abandoning whatever remains.
|
||||
const drainDeadline = 5 * time.Second
|
||||
|
||||
// Repository is the narrow persistence capability the writer needs.
|
||||
// Implemented by *sqlite.Repository in production; AC-33's ownership check
|
||||
// and AC-32's retry/backoff classification already live inside it (Task 7),
|
||||
// so the writer only builds a complete row and classifies the error it
|
||||
// gets back.
|
||||
type Repository interface {
|
||||
CreateTaskUsageEvent(ctx context.Context, event *models.TaskUsageEvent) error
|
||||
}
|
||||
|
||||
// Writer subscribes to the session-prompt-usage bus subject and records one
|
||||
// task_usage_events row per observed event. The bus callback only decodes
|
||||
// and hands off (AC-34); a single long-lived worker goroutine does the
|
||||
// database work serially, off the publisher's own goroutine, so
|
||||
// occurred_at acquisition stays monotonic with arrival order.
|
||||
type Writer struct {
|
||||
repo Repository
|
||||
pricing PricingLookup
|
||||
log *logger.Logger
|
||||
|
||||
events chan *usageEventPayload
|
||||
|
||||
mu sync.Mutex
|
||||
started bool
|
||||
draining bool
|
||||
|
||||
workCtx context.Context
|
||||
cancelWork context.CancelFunc
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
// NewWriter constructs a Writer. pricing may be nil, in which case every
|
||||
// event resolves cost_source=unpriced (AC-8's no-lookup-wired degradation).
|
||||
// log may be nil; metrics are still recorded, just not logged.
|
||||
func NewWriter(repo Repository, pricing PricingLookup, log *logger.Logger) *Writer {
|
||||
workCtx, cancel := context.WithCancel(context.Background())
|
||||
return &Writer{
|
||||
repo: repo,
|
||||
pricing: pricing,
|
||||
log: log,
|
||||
events: make(chan *usageEventPayload, eventQueueCapacity),
|
||||
workCtx: workCtx,
|
||||
cancelWork: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
// Start launches the single worker goroutine. Idempotent.
|
||||
func (w *Writer) Start() {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
if w.started {
|
||||
return
|
||||
}
|
||||
w.started = true
|
||||
w.wg.Add(1)
|
||||
go w.run()
|
||||
}
|
||||
|
||||
// Stop stops accepting new events, drains whatever is already buffered, and
|
||||
// returns once the buffer is empty or drainDeadline has elapsed, whichever
|
||||
// comes first (AC-34). Anything still buffered at the deadline - including
|
||||
// the event in flight, whose context is cancelled at that instant - is
|
||||
// counted dropped:drain_timeout and never written. Idempotent.
|
||||
func (w *Writer) Stop() {
|
||||
w.mu.Lock()
|
||||
if !w.started || w.draining {
|
||||
w.mu.Unlock()
|
||||
return
|
||||
}
|
||||
w.draining = true
|
||||
close(w.events)
|
||||
w.mu.Unlock()
|
||||
|
||||
timer := time.AfterFunc(drainDeadline, w.cancelWork)
|
||||
defer timer.Stop()
|
||||
w.wg.Wait()
|
||||
}
|
||||
|
||||
// Subscribe registers the writer's bus callback on the session-prompt-usage
|
||||
// subject (AC-24). Call after Start.
|
||||
func (w *Writer) Subscribe(eb bus.EventBus) error {
|
||||
_, err := eb.Subscribe(events.BuildSessionPromptUsageWildcardSubject(), w.handleBusEvent)
|
||||
return err
|
||||
}
|
||||
|
||||
// run is the single serial worker (AC-34): it drains w.events until the
|
||||
// channel is closed and empty, or until workCtx is cancelled (only ever by
|
||||
// Stop's drain-deadline timer), whichever comes first.
|
||||
func (w *Writer) run() {
|
||||
defer w.wg.Done()
|
||||
for {
|
||||
select {
|
||||
case payload, ok := <-w.events:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if w.workCtx.Err() != nil {
|
||||
w.recordDropped(dropReasonDrainTimeout, payload.TaskID)
|
||||
continue
|
||||
}
|
||||
w.processEvent(w.workCtx, payload)
|
||||
case <-w.workCtx.Done():
|
||||
for payload := range w.events {
|
||||
w.recordDropped(dropReasonDrainTimeout, payload.TaskID)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleBusEvent is the bus callback (AC-34): decode only, then hand off to
|
||||
// the worker over the bounded channel. It always returns nil immediately -
|
||||
// the bus delivers synchronously on the publisher's own goroutine, and this
|
||||
// callback must never block an agent turn or fail the publish.
|
||||
func (w *Writer) handleBusEvent(_ context.Context, event *bus.Event) error {
|
||||
payload, err := decodePayload(event)
|
||||
if err != nil {
|
||||
w.recordDropped(dropReasonDecodeError, "")
|
||||
return nil
|
||||
}
|
||||
w.admit(payload)
|
||||
return nil
|
||||
}
|
||||
|
||||
// admit hands payload to the worker over the bounded channel without
|
||||
// blocking (AC-34): a full channel is abandoned as dropped:overflow; an
|
||||
// event offered after Stop has begun draining is refused as
|
||||
// dropped:shutdown. The send and the shutdown check share w.mu with Stop's
|
||||
// close(w.events), so a send can never race a close.
|
||||
func (w *Writer) admit(p *usageEventPayload) {
|
||||
w.mu.Lock()
|
||||
if w.draining {
|
||||
w.mu.Unlock()
|
||||
w.recordDropped(dropReasonShutdown, p.TaskID)
|
||||
return
|
||||
}
|
||||
select {
|
||||
case w.events <- p:
|
||||
w.mu.Unlock()
|
||||
default:
|
||||
w.mu.Unlock()
|
||||
w.recordDropped(dropReasonOverflow, p.TaskID)
|
||||
}
|
||||
}
|
||||
|
||||
// processEvent implements AC-27's validate/ownership/insert stages for one
|
||||
// dequeued event. occurred_at is acquired exactly once here, on the single
|
||||
// serial worker, before the first (and only) repository call - the
|
||||
// repository's own AC-32 retries never re-acquire it.
|
||||
func (w *Writer) processEvent(ctx context.Context, p *usageEventPayload) {
|
||||
if reason := validateStage(p); reason != "" {
|
||||
w.recordDropped(reason, p.TaskID)
|
||||
return
|
||||
}
|
||||
|
||||
occurredAt := time.Now().UTC()
|
||||
event := w.buildRow(ctx, p, occurredAt)
|
||||
if event == nil {
|
||||
w.recordDropped(dropReasonOverflow, p.TaskID)
|
||||
return
|
||||
}
|
||||
|
||||
err := w.repo.CreateTaskUsageEvent(ctx, event)
|
||||
switch {
|
||||
case err == nil:
|
||||
w.recordWritten(event.CostSource, event.Provider)
|
||||
case errors.Is(err, sqliterepo.ErrDuplicateUsageEvent):
|
||||
w.recordDropped(dropReasonDuplicate, p.TaskID)
|
||||
default:
|
||||
w.recordDropped(dropReasonError, p.TaskID)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package usage
|
||||
|
||||
// End-to-end coverage through the real bus -> Writer -> *sqlite.Repository
|
||||
// path (docs/specs/task-cost-ledger/spec.md AC-16, AC-34), as opposed to the
|
||||
// fakeUsageRepo test double used everywhere else in this package.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
dbutil "github.com/kandev/kandev/internal/db"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
sqliterepo "github.com/kandev/kandev/internal/task/repository/sqlite"
|
||||
)
|
||||
|
||||
// TestWriter_EndToEnd_RealRepoPreservesArrivalOrder pins AC-16 (occurred_at
|
||||
// ascending, id as tiebreak) together with AC-34 (the bus callback hands off
|
||||
// to a single serial worker, off the publisher's own goroutine). occurred_at
|
||||
// is acquired by the worker at processing time (buildRow), not carried on
|
||||
// the payload, so fast back-to-back events can legitimately collide on that
|
||||
// column; proving arrival order survives end to end through a real
|
||||
// repository also proves the id tiebreak is doing its job, not just that
|
||||
// the column exists in isolation.
|
||||
func TestWriter_EndToEnd_RealRepoPreservesArrivalOrder(t *testing.T) {
|
||||
dbPath := filepath.Join(t.TempDir(), "writer-e2e.db")
|
||||
dbConn, err := dbutil.OpenSQLite(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
db := sqlx.NewDb(dbConn, "sqlite3")
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
repo, err := sqliterepo.NewWithDB(db, db, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("init schema: %v", err)
|
||||
}
|
||||
if err := repo.CreateTask(context.Background(), &models.Task{
|
||||
ID: "task-e2e", WorkspaceID: "ws-1", Title: "e2e", Priority: "medium",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateTask: %v", err)
|
||||
}
|
||||
if err := repo.CreateTaskSession(context.Background(), &models.TaskSession{
|
||||
ID: "session-e2e", TaskID: "task-e2e", State: models.TaskSessionStateWaitingForInput,
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateTaskSession: %v", err)
|
||||
}
|
||||
|
||||
w := NewWriter(repo, nil, nil)
|
||||
w.Start()
|
||||
|
||||
eb := bus.NewMemoryEventBus(logger.Default())
|
||||
if err := w.Subscribe(eb); err != nil {
|
||||
t.Fatalf("Subscribe: %v", err)
|
||||
}
|
||||
|
||||
subject := events.BuildSessionPromptUsageSubject("session-e2e")
|
||||
const n = 8
|
||||
for i := 0; i < n; i++ {
|
||||
payload := map[string]any{
|
||||
"task_id": "task-e2e",
|
||||
"session_id": "session-e2e",
|
||||
"usage_event_id": fmt.Sprintf("evt-e2e-%d", i),
|
||||
"usage": map[string]any{"input_tokens": int64(i)},
|
||||
}
|
||||
if err := eb.Publish(context.Background(), subject, &bus.Event{Data: payload}); err != nil {
|
||||
t.Fatalf("Publish %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
w.Stop()
|
||||
|
||||
got, err := repo.ListTaskUsageEvents(context.Background(), "task-e2e", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListTaskUsageEvents: %v", err)
|
||||
}
|
||||
if len(got) != n {
|
||||
t.Fatalf("row count = %d, want %d", len(got), n)
|
||||
}
|
||||
for i, row := range got {
|
||||
want := fmt.Sprintf("evt-e2e-%d", i)
|
||||
if row.UsageEventID != want {
|
||||
t.Errorf("row %d usage_event_id = %q, want %q (arrival order must survive the occurred_at+id ordering)", i, row.UsageEventID, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/kandev/kandev/internal/common/logger"
|
||||
"github.com/kandev/kandev/internal/events"
|
||||
"github.com/kandev/kandev/internal/events/bus"
|
||||
"github.com/kandev/kandev/internal/task/models"
|
||||
sqliterepo "github.com/kandev/kandev/internal/task/repository/sqlite"
|
||||
)
|
||||
|
||||
// fakeUsageRepo is a Repository test double. If block is true,
|
||||
// CreateTaskUsageEvent blocks until ctx is done and returns ctx.Err(),
|
||||
// modeling a wedged database that only responds to context cancellation
|
||||
// (R5-F2's Test B).
|
||||
type fakeUsageRepo struct {
|
||||
mu sync.Mutex
|
||||
created []*models.TaskUsageEvent
|
||||
err error
|
||||
block bool
|
||||
calls int
|
||||
}
|
||||
|
||||
func (f *fakeUsageRepo) CreateTaskUsageEvent(ctx context.Context, event *models.TaskUsageEvent) error {
|
||||
f.mu.Lock()
|
||||
f.calls++
|
||||
f.mu.Unlock()
|
||||
if f.block {
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
}
|
||||
if f.err != nil {
|
||||
return f.err
|
||||
}
|
||||
f.mu.Lock()
|
||||
f.created = append(f.created, event)
|
||||
f.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeUsageRepo) rowCount() int {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return len(f.created)
|
||||
}
|
||||
|
||||
func (f *fakeUsageRepo) callCount() int {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return f.calls
|
||||
}
|
||||
|
||||
// expvarMapValue reads one key's counter out of an expvar.Map, returning 0
|
||||
// for an absent key. Metrics vars are package-level and shared across every
|
||||
// test in this binary, so assertions use before/after deltas rather than
|
||||
// absolute values.
|
||||
func expvarMapValue(m *expvar.Map, key string) int64 {
|
||||
var v int64
|
||||
m.Do(func(kv expvar.KeyValue) {
|
||||
if kv.Key == key {
|
||||
if iv, ok := kv.Value.(*expvar.Int); ok {
|
||||
v = iv.Value()
|
||||
}
|
||||
}
|
||||
})
|
||||
return v
|
||||
}
|
||||
|
||||
func validPayload(usageEventID string) *usageEventPayload {
|
||||
return &usageEventPayload{
|
||||
UsageEventID: usageEventID,
|
||||
TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{InputTokens: 1},
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleBusEvent_UndecodableData_RecordsDecodeErrorAndReturnsNil pins
|
||||
// AC-27's decode stage and AC-34's "callback always returns nil" rule.
|
||||
func TestHandleBusEvent_UndecodableData_RecordsDecodeErrorAndReturnsNil(t *testing.T) {
|
||||
w := NewWriter(&fakeUsageRepo{}, nil, nil)
|
||||
before := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonDecodeError))
|
||||
|
||||
err := w.handleBusEvent(context.Background(), &bus.Event{Data: func() {}})
|
||||
if err != nil {
|
||||
t.Fatalf("handleBusEvent returned %v, want nil (must never fail the publish)", err)
|
||||
}
|
||||
|
||||
after := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonDecodeError))
|
||||
if after-before != 1 {
|
||||
t.Errorf("decode_error count delta = %d, want 1", after-before)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdmit_ChannelFull_DropsOverflowWithoutBlocking pins AC-34: a send
|
||||
// that would block is abandoned, not waited on, and counted
|
||||
// dropped:overflow. The worker is never started, so nothing drains the
|
||||
// channel and it fills deterministically.
|
||||
func TestAdmit_ChannelFull_DropsOverflowWithoutBlocking(t *testing.T) {
|
||||
w := NewWriter(&fakeUsageRepo{}, nil, nil)
|
||||
for i := 0; i < eventQueueCapacity; i++ {
|
||||
w.admit(validPayload(fmt.Sprintf("evt-%d", i)))
|
||||
}
|
||||
|
||||
before := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonOverflow))
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
w.admit(validPayload("evt-overflow"))
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("admit blocked instead of dropping when the channel is full")
|
||||
}
|
||||
|
||||
after := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonOverflow))
|
||||
if after-before != 1 {
|
||||
t.Errorf("overflow count delta = %d, want 1", after-before)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdmit_AfterShutdownBegins_DropsShutdownWithoutBlocking pins AC-34: an
|
||||
// event offered after Stop has begun draining is refused, not queued, and
|
||||
// counted dropped:shutdown (distinct from overflow).
|
||||
func TestAdmit_AfterShutdownBegins_DropsShutdownWithoutBlocking(t *testing.T) {
|
||||
w := NewWriter(&fakeUsageRepo{}, nil, nil)
|
||||
w.Start()
|
||||
w.Stop()
|
||||
|
||||
before := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonShutdown))
|
||||
w.admit(validPayload("evt-late"))
|
||||
after := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonShutdown))
|
||||
if after-before != 1 {
|
||||
t.Errorf("shutdown count delta = %d, want 1", after-before)
|
||||
}
|
||||
}
|
||||
|
||||
// TestProcessEvent_InvalidPayload_NeverReachesRepository pins that the
|
||||
// validate stage's rejection is terminal - an invalid event never reaches
|
||||
// the ownership/insert stages the repository owns.
|
||||
func TestProcessEvent_InvalidPayload_NeverReachesRepository(t *testing.T) {
|
||||
repo := &fakeUsageRepo{}
|
||||
w := NewWriter(repo, nil, nil)
|
||||
w.processEvent(context.Background(), &usageEventPayload{UsageEventID: "", TaskID: "task-1"})
|
||||
if repo.callCount() != 0 {
|
||||
t.Errorf("repo.calls = %d, want 0", repo.callCount())
|
||||
}
|
||||
}
|
||||
|
||||
func TestProcessEvent_TokenTotalOverflow_DropsOverflowWithoutRepositoryCall(t *testing.T) {
|
||||
repo := &fakeUsageRepo{}
|
||||
w := NewWriter(repo, nil, nil)
|
||||
before := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", "overflow"))
|
||||
w.processEvent(context.Background(), &usageEventPayload{
|
||||
UsageEventID: "evt-overflow", TaskID: "task-1",
|
||||
Usage: &promptUsagePayload{InputTokens: math.MaxInt64, CachedReadTokens: 1},
|
||||
})
|
||||
after := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", "overflow"))
|
||||
if after-before != 1 {
|
||||
t.Errorf("overflow count delta = %d, want 1", after-before)
|
||||
}
|
||||
if repo.callCount() != 0 {
|
||||
t.Errorf("repo.calls = %d, want 0 for an overflowing token total", repo.callCount())
|
||||
}
|
||||
}
|
||||
|
||||
// TestProcessEvent_RepoSucceeds_RecordsWritten pins the nil-error
|
||||
// classification branch.
|
||||
func TestProcessEvent_RepoSucceeds_RecordsWritten(t *testing.T) {
|
||||
repo := &fakeUsageRepo{}
|
||||
w := NewWriter(repo, nil, nil)
|
||||
label := usageMetricLabel("source", CostSourceUnpriced, "provider", "")
|
||||
before := expvarMapValue(eventsWrittenTotal, label)
|
||||
|
||||
w.processEvent(context.Background(), validPayload("evt-1"))
|
||||
|
||||
after := expvarMapValue(eventsWrittenTotal, label)
|
||||
if after-before != 1 {
|
||||
t.Errorf("written count delta = %d, want 1", after-before)
|
||||
}
|
||||
if repo.rowCount() != 1 {
|
||||
t.Fatalf("repo rows = %d, want 1", repo.rowCount())
|
||||
}
|
||||
}
|
||||
|
||||
// TestProcessEvent_RepoReturnsErrDuplicateUsageEvent_RecordsDropDuplicate
|
||||
// pins that AC-33/AC-32's already-implemented redelivery outcome is
|
||||
// classified as dropped:duplicate, not dropped:error.
|
||||
func TestProcessEvent_RepoReturnsErrDuplicateUsageEvent_RecordsDropDuplicate(t *testing.T) {
|
||||
repo := &fakeUsageRepo{err: sqliterepo.ErrDuplicateUsageEvent}
|
||||
w := NewWriter(repo, nil, nil)
|
||||
before := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonDuplicate))
|
||||
|
||||
w.processEvent(context.Background(), validPayload("evt-1"))
|
||||
|
||||
after := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonDuplicate))
|
||||
if after-before != 1 {
|
||||
t.Errorf("duplicate count delta = %d, want 1", after-before)
|
||||
}
|
||||
}
|
||||
|
||||
// TestProcessEvent_RepoReturnsOtherError_RecordsDropError pins that every
|
||||
// other repository failure - including R5-F1's ownership-lookup hard error
|
||||
// and transient-retry exhaustion, both already handled inside
|
||||
// CreateTaskUsageEvent - falls through to dropped:error.
|
||||
func TestProcessEvent_RepoReturnsOtherError_RecordsDropError(t *testing.T) {
|
||||
repo := &fakeUsageRepo{err: errors.New("boom")}
|
||||
w := NewWriter(repo, nil, nil)
|
||||
before := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonError))
|
||||
|
||||
w.processEvent(context.Background(), validPayload("evt-1"))
|
||||
|
||||
after := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonError))
|
||||
if after-before != 1 {
|
||||
t.Errorf("error count delta = %d, want 1", after-before)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStop_HealthyRepo_DrainsBufferedEventsAndReturnsPromptly is R5-F2's
|
||||
// Test A: buffered events drain and are recorded, and Stop returns well
|
||||
// under the 5-second deadline.
|
||||
func TestStop_HealthyRepo_DrainsBufferedEventsAndReturnsPromptly(t *testing.T) {
|
||||
repo := &fakeUsageRepo{}
|
||||
w := NewWriter(repo, nil, nil)
|
||||
w.Start()
|
||||
|
||||
const n = 5
|
||||
for i := 0; i < n; i++ {
|
||||
w.admit(validPayload(fmt.Sprintf("evt-%d", i)))
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
w.Stop()
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if elapsed >= drainDeadline {
|
||||
t.Errorf("Stop took %v, want well under the %v drain deadline for a healthy repository", elapsed, drainDeadline)
|
||||
}
|
||||
if repo.rowCount() != n {
|
||||
t.Errorf("repo rows = %d, want %d (every buffered event must drain before Stop returns)", repo.rowCount(), n)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStop_WedgedRepo_HonorsDrainDeadlineAndDropsEverythingAsError is
|
||||
// R5-F2's Test B: a repository that only responds to context cancellation
|
||||
// forces Stop to wait out the full drain deadline, after which the
|
||||
// in-flight event is counted dropped:error, buffered events are counted
|
||||
// dropped:drain_timeout, and nothing is written. Once workCtx is cancelled, a buffered event still
|
||||
// waiting in the select may itself reach the repository with an
|
||||
// already-cancelled context (returning instantly, still an error) rather
|
||||
// than being classified without a repository call at all - both outcomes
|
||||
// are dropped:error and neither writes a row, so repo.calls is not pinned
|
||||
// to an exact count here. Runs inside synctest so the 5-second deadline
|
||||
// advances on the fake clock instead of costing real wall time.
|
||||
func TestStop_WedgedRepo_HonorsDrainDeadlineAndDropsEverythingAsError(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
repo := &fakeUsageRepo{block: true}
|
||||
w := NewWriter(repo, nil, nil)
|
||||
w.Start()
|
||||
|
||||
// The first admitted event is dequeued by the worker and blocks
|
||||
// forever inside the repository call (in flight); the fake repo
|
||||
// blocks every call, so it can never dequeue a second one until
|
||||
// workCtx is cancelled - these three stay buffered in the channel.
|
||||
const buffered = 3
|
||||
w.admit(validPayload("evt-inflight"))
|
||||
for i := 0; i < buffered; i++ {
|
||||
w.admit(validPayload(fmt.Sprintf("evt-buffered-%d", i)))
|
||||
}
|
||||
|
||||
beforeError := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonError))
|
||||
beforeDrain := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", "drain_timeout"))
|
||||
|
||||
start := time.Now()
|
||||
w.Stop()
|
||||
elapsed := time.Since(start)
|
||||
|
||||
if elapsed < drainDeadline {
|
||||
t.Errorf("Stop returned after %v, want at least the %v drain deadline", elapsed, drainDeadline)
|
||||
}
|
||||
if repo.rowCount() != 0 {
|
||||
t.Errorf("repo rows = %d, want 0 (nothing may be written once the drain deadline has expired)", repo.rowCount())
|
||||
}
|
||||
if calls := repo.callCount(); calls < 1 {
|
||||
t.Errorf("repo.calls = %d, want at least 1 (the in-flight event must reach the repository)", calls)
|
||||
}
|
||||
afterError := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonError))
|
||||
afterDrain := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", "drain_timeout"))
|
||||
if afterError-beforeError != 1 {
|
||||
t.Errorf("error-drop count delta = %d, want 1 for the in-flight event", afterError-beforeError)
|
||||
}
|
||||
if afterDrain-beforeDrain != buffered {
|
||||
t.Errorf("drain-timeout count delta = %d, want %d for buffered events", afterDrain-beforeDrain, buffered)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestSubscribe_PublishedEvent_IsRecordedWithoutBlockingThePublisher pins
|
||||
// AC-24 (subscribes on the real session-prompt-usage wildcard subject) and
|
||||
// AC-34 (Publish, which delivers synchronously on the publisher's own
|
||||
// goroutine, must return promptly regardless of downstream processing).
|
||||
// The repository blocks every call until its context is cancelled, so this
|
||||
// is the assertion that actually distinguishes the two designs: a handler
|
||||
// that wrote synchronously on the callback goroutine would make Publish
|
||||
// itself hang for as long as the repository blocks, and a repo that
|
||||
// returns instantly (the prior version of this test) can't tell that apart
|
||||
// from the real async design. synctest.Wait pins that the worker has
|
||||
// genuinely reached the blocking repository call - not merely that the
|
||||
// payload is still sitting unconsumed in the channel - before asserting
|
||||
// Publish already returned. Runs inside synctest so Stop's 5-second drain
|
||||
// deadline advances on the fake clock instead of costing real wall time.
|
||||
func TestSubscribe_PublishedEvent_IsRecordedWithoutBlockingThePublisher(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
repo := &fakeUsageRepo{block: true}
|
||||
w := NewWriter(repo, nil, nil)
|
||||
w.Start()
|
||||
|
||||
eb := bus.NewMemoryEventBus(logger.Default())
|
||||
if err := w.Subscribe(eb); err != nil {
|
||||
t.Fatalf("Subscribe: %v", err)
|
||||
}
|
||||
|
||||
subject := events.BuildSessionPromptUsageSubject("session-1")
|
||||
payload := map[string]any{
|
||||
"task_id": "task-1",
|
||||
"session_id": "session-1",
|
||||
"usage_event_id": "evt-published",
|
||||
"usage": map[string]any{"input_tokens": int64(7)},
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
if err := eb.Publish(context.Background(), subject, &bus.Event{Data: payload}); err != nil {
|
||||
t.Fatalf("Publish: %v", err)
|
||||
}
|
||||
if elapsed := time.Since(start); elapsed > 100*time.Millisecond {
|
||||
t.Errorf("Publish took %v, want near-instant (the callback must never block on the worker)", elapsed)
|
||||
}
|
||||
|
||||
synctest.Wait()
|
||||
if calls := repo.callCount(); calls != 1 {
|
||||
t.Fatalf("repo.calls = %d, want 1 (the worker must have reached the repository despite Publish already returning)", calls)
|
||||
}
|
||||
if repo.rowCount() != 0 {
|
||||
t.Fatalf("repo rows = %d, want 0 (the fake never returns success while its call is still blocked)", repo.rowCount())
|
||||
}
|
||||
|
||||
before := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonError))
|
||||
w.Stop()
|
||||
after := expvarMapValue(eventsDroppedTotal, usageMetricLabel("reason", dropReasonError))
|
||||
if after-before != 1 {
|
||||
t.Errorf("error-drop count delta = %d, want 1 (Stop's drain deadline cancels the in-flight call)", after-before)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestStartStop_Idempotent pins that calling Start or Stop more than once
|
||||
// is a safe no-op (matches the internal/integrations/healthpoll shape).
|
||||
func TestStartStop_Idempotent(t *testing.T) {
|
||||
w := NewWriter(&fakeUsageRepo{}, nil, nil)
|
||||
w.Start()
|
||||
w.Start()
|
||||
w.Stop()
|
||||
w.Stop()
|
||||
}
|
||||
+2
-1
@@ -136,12 +136,13 @@ Per-workspace credentials and triage triggers for external services.
|
||||
|
||||
## costs/ — cost tracking & budgets
|
||||
|
||||
Subscription quota tracking and per-agent cheap-model profile routing.
|
||||
Subscription quota tracking, per-agent cheap-model profile routing, and the production per-turn usage ledger.
|
||||
|
||||
| Spec | Status |
|
||||
|---|---|
|
||||
| [subscription-usage](costs/subscription-usage.md) | draft |
|
||||
| [cheap-model-profiles](costs/cheap-model-profiles.md) | shipped |
|
||||
| [task cost & token ledger](task-cost-ledger/spec.md) | draft |
|
||||
|
||||
## ui/ — cross-cutting UI features
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ Multiple policies can apply to the same scope (e.g. monthly + total).
|
||||
|
||||
### `TaskSession` additions
|
||||
|
||||
`cost_subcents`, `tokens_in`, `tokens_out`, and `tokens_cached_in` are incrementally updated as cost events arrive. They provide quick per-session totals without a scan of `office_cost_events`. An unknown ledger `tokens_out` value contributes zero to the numeric rollup. Therefore, the session output total is a lower bound when at least one ledger row has `tokens_out=NULL`. Consumers that need completeness must read the raw ledger. `tokens_cached_in` is DB-only today (no model/DTO/API/web reader). It exists so the rollup reconciles column-for-column against the ledger. The rollup consumes only the merged `tokens_cached_in` sum, not the `tokens_cached_read` / `tokens_cached_write` split. The split exists solely on the ledger row for cost-per-token-type analysis (contract v2).
|
||||
`cost_subcents`, `tokens_in`, `tokens_out`, and `tokens_cached_in` are maintained by the task cost ledger writer, not this Office subscriber ([task-cost-ledger](../task-cost-ledger/spec.md) AC-10/AC-21). They provide quick per-session totals without a scan of `task_usage_events`. An unknown ledger `tokens_out` value contributes zero to the numeric rollup, so the session output total is a lower bound when at least one ledger row has `tokens_out=NULL`. Consumers needing completeness must read the raw ledger. `tokens_cached_in` is in TaskSession and DTO, without a reader, so the rollup reconciles column-for-column against that ledger. The rollup consumes only the merged `tokens_cached_in` sum, not the `tokens_cached_read`/`tokens_cached_write` split, which exists solely on the ledger row for cost-per-token-type analysis (contract v2).
|
||||
|
||||
### Per-agent budget
|
||||
|
||||
@@ -200,7 +200,7 @@ There is no per-field permission model. Conformance tests should assert that cos
|
||||
- **models.dev miss**: row recorded with `cost_subcents=0` and `cost_source=unpriced`; `estimated` is unaffected (tracks token-synthesis independently, see the `office_cost_events` table). UI shows "pricing unavailable".
|
||||
- **Prompt-usage event redelivered** (e.g. a reconnecting WS client replaying a buffered stream event): the second `CreateCostEvent` call collides on the unique `usage_event_id` partial index and is dropped as a no-op rather than double-inserting or double-counting the session rollup.
|
||||
- **Writer silently stops**: every path a prompt-usage event can take (decode failure, missing task/session ids, task-fields lookup failure, insert failure or duplicate, or a successful write) increments an `expvar` counter (`cost_events_written_total` / `cost_events_dropped_total`, dev-mode `/debug/vars`) and logs, so a stopped writer is observable instead of silently producing zero rows.
|
||||
- **Ledger insert and rollup increment commit or roll back together**: `CreateCostEvent` (the `office_cost_events` insert) and `IncrementTaskSessionUsage` (the `task_sessions` rollup) are wrapped in one `*sqlx.Tx` by `recordCostEventAndRollup` whenever the wired session-usage writer supports it (every production wiring does). If the rollup increment fails after the ledger insert, the whole transaction rolls back - nothing is committed - so a redelivery of the same event with the same `usage_event_id` retries the operation cleanly instead of being dropped as a duplicate by the unique index above while the rollup stays short. Falls back to the pre-existing non-atomic two-step write only when the wired writer doesn't implement the transactional capability (for example a simplified test double).
|
||||
- **`recordCostEventAndRollup` only inserts `office_cost_events`** ([task-cost-ledger](../task-cost-ledger/spec.md) AC-10/AC-21): it no longer increments the `task_sessions` rollup or shares a transaction with it. The ledger writer's own insert (`task_usage_events`) and its `task_sessions` rollup increment are wrapped in one transaction on its side, independent of this call, so a redelivery is idempotent on each ledger's own unique index without the two ledgers coordinating a shared transaction.
|
||||
- **models.dev fetch fails or is canceled**: background refresh falls back to
|
||||
existing on-disk and in-memory data; no crash. The in-flight guard is released
|
||||
and a later refresh can retry.
|
||||
@@ -219,7 +219,7 @@ There is no per-field permission model. Conformance tests should assert that cos
|
||||
- `office_cost_events` rows (full history, never trimmed). Disk-runner rows keyed by `(session_id, provider_event_id)` survive re-ingestion without duplicating; wire-side rows are additionally deduplicated by the unique `usage_event_id` partial index (contract v2). The wire-side `estimated=true` rows for codex are deleted once the matching aggregate row lands.
|
||||
- **Column semantics never change value mid-series across a contract bump.** Legacy rows (written before a column's semantics changed) read NULL, never `0`, for a newly nullable value - the downstream point-in-time extract has no schema versioning, so a column whose meaning flips from "not recorded" to "recorded as zero" (or vice versa) partway through the series would be silently discontinuous. This applies both to columns introduced by contract v2 and to `tokens_out`, an existing column whose contract became nullable in v3: pre-v3 rows read `0`, not NULL, for `tokens_out`. Contract v3 changes the meaning of `estimated` for new rows only; legacy rows keep their recorded value. `cost_contract_version` on each row is the in-band marker a consumer checks instead of inferring from a date.
|
||||
- `office_budget_policies` rows.
|
||||
- `TaskSession.cost_subcents` / `tokens_in` / `tokens_out` / `tokens_cached_in` running totals. The ledger insert and rollup increment are atomic (see `Failure modes` above), so they cannot diverge on a partial write. The numeric `tokens_out` rollup is a lower bound when a source row has an unknown output count.
|
||||
- `TaskSession.cost_subcents` / `tokens_in` / `tokens_out` / `tokens_cached_in` running totals, maintained by the task cost ledger writer, not this Office subscriber ([task-cost-ledger](../task-cost-ledger/spec.md) AC-10/AC-21); its ledger insert and rollup increment are atomic, so they cannot diverge on a partial write. The numeric `tokens_out` rollup is a lower bound when a source row has an unknown output count.
|
||||
- Per-agent `budget_monthly_cents` stored on the agent instance row.
|
||||
- Per-model pricing overrides stored in workspace settings.
|
||||
- The on-disk `models.dev` cache at `<data-dir>/cache/models-dev.json`. Recovery on next boot: the in-memory pricing map is empty until first query; queries fall back to the on-disk file when the background refresh has not yet completed.
|
||||
@@ -257,7 +257,7 @@ No TTL or retention is applied to `office_cost_events`; rows accumulate for the
|
||||
|
||||
- **GIVEN** a prompt-usage event for an in-progress turn, **WHEN** the cost event is recorded at turn completion, **THEN** `turn_id` is populated with the turn that was resolved at completion time and `usage_event_id` is set to a deterministic id derived from the underlying completion's identity.
|
||||
|
||||
- **GIVEN** the same underlying prompt-usage completion is delivered twice (e.g. a reconnecting WS client replaying a buffered stream event), **WHEN** the second copy is processed, **THEN** it derives the same `usage_event_id`, the insert is rejected by the unique partial index, and the session rollup is not incremented a second time.
|
||||
- **GIVEN** the same underlying prompt-usage completion is delivered twice (e.g. a reconnecting WS client replaying a buffered stream event), **WHEN** the second copy is processed, **THEN** it derives the same `usage_event_id` and the `office_cost_events` insert is rejected by the unique partial index; the `task_sessions` rollup is unaffected, per the ledger writer's idempotent insert ([task-cost-ledger](../task-cost-ledger/spec.md) AC-10/AC-21).
|
||||
|
||||
- **GIVEN** an `office_cost_events` table that predates contract v2, **WHEN** the repository boots and runs its migration, **THEN** the new columns are added nullable, every pre-existing row reads NULL (never `0`) for all of them, and running the migration again on the same database is a no-op.
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
"docs/specs/lsp-file-intelligence/spec.md": 38089,
|
||||
"docs/specs/office/agents.md": 61654,
|
||||
"docs/specs/office/automations-settings.md": 33607,
|
||||
"docs/specs/office/costs.md": 33263,
|
||||
"docs/specs/office/costs.md": 33239,
|
||||
"docs/specs/office/live-updates.md": 38568,
|
||||
"docs/specs/office/overview.md": 34375,
|
||||
"docs/specs/office/routing.md": 40782,
|
||||
@@ -36,6 +36,7 @@
|
||||
"docs/specs/plugins/spec.md": 77806,
|
||||
"docs/specs/subagent-context-persistence/spec.md": 160552,
|
||||
"docs/specs/system-page/storage-maintenance.md": 54320,
|
||||
"docs/specs/task-cost-ledger/spec.md": 91931,
|
||||
"docs/specs/ui/ci-pr-automation.md": 57643
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user