mirror of
https://github.com/multica-ai/multica.git
synced 2026-08-05 17:40:11 +02:00
* fix(mcp): treat agent mcp_config as an authoritative allowlist
An agent's saved mcp_config was silently widened with the runtime host's
own user-level MCP servers, so an explicitly empty `{"mcpServers":{}}`
resolved to the COMPLETE host set instead of no servers at all — the
opposite of what the operator configured (GitHub #6283).
`--strict-mcp-config` was being passed correctly; the merge happened
before it, in the daemon, so strict mode constrained an already-widened
set. Introduced by #5277 and present in v0.4.16 through main.
Restore the three-state contract in resolveEffectiveMcpConfig:
null / unset -> inherit the provider's native MCP configuration
{"mcpServers":{}} -> strict empty, no host servers
non-empty object -> strict allowlist, exactly those servers
Two explicit inherit paths keep the additive behaviour reachable without
weakening the default:
- runtime_config.mcp.inherit_runtime = true opts an agent back in.
- The claim response now carries mcp_config_overlay_only so the daemon
can tell an agent-authored config from a per-task Composio overlay.
Without it, enabling an integration on an agent that never configured
MCP would have stripped the host servers it was already inheriting.
Both decode paths fail closed: malformed runtime_config never enables
inheritance, and a failed runtime merge falls back to the agent's own
config.
The web MCP tab and the `agent create/update --mcp-config` help text
described the old additive behaviour, which is how a tightened config
could look correct while exposing every host server; both now state
which mode is in effect.
Note for rollout: the fix lives in the daemon, so self-hosted users must
upgrade the daemon — a server/UI upgrade alone does not apply it.
Co-authored-by: multica-agent <github@multica.ai>
* fix(mcp): close review gaps in the authoritative mcp_config change
Addresses the four must-fix findings from review of #6292.
1. Deleting the last managed server no longer widens access.
removeManagedMcpServer cleared the config to null, which now means
"inherit the host's MCP servers" — so a delete took the agent from one
allowed server to every server on the host. It now leaves an explicit
`{"mcpServers":{}}`. Restoring inheritance moved to a separate
clearManagedMcpConfig action behind its own confirmation that states the
widening. The delete dialog no longer claims "Runtime servers are not
affected", which was the opposite of the truth.
2. The UI no longer promises a boundary an old daemon does not enforce.
The strict semantics live in the daemon, so a config saved against an
older daemon is not yet in effect. Adds the authoritative-mcp-v1 daemon
capability:
- The daemon advertises it and reports authoritative_mcp on the
runtime-capabilities response.
- The claim path fails closed: a managed, non-inheriting mcp_config
claimed by a daemon without the capability cancels the task and
returns 412 with an actionable message, instead of letting that daemon
merge the host's servers in. runtime_config.mcp.inherit_runtime is the
documented escape hatch, and it is honest — it declares that the
operator accepts the host's servers.
- The MCP tab shows "needs upgrade" rather than "Not exposed" while the
bound runtime lacks the capability.
3. Saving OpenClaw settings no longer drops the inherit opt-in.
parseOpenclawRuntimeConfig discarded unknown keys and the tab persisted
the result as the whole runtime_config, so one unrelated routing save
silently deleted mcp.inherit_runtime. Unknown keys now round-trip
through OpenclawRuntimeConfig.passthrough, excluded from the dirty check
so they cannot make the form look edited.
4. Documents the new semantics in the built-in creating-agents skill and
its source map: the three states, the persisted
runtime_config.mcp.inherit_runtime field, and the claim-time capability
gate.
Also corrects the PR's rollout claim: there is no database migration, but
this does add a persisted JSON field and change the meaning of an existing
one.
Co-authored-by: multica-agent <github@multica.ai>
* fix(mcp): stop the authoritative-daemon gate from blocking valid claims
CI's backend job failed three handler claim tests with the new 412. Two
distinct problems, both real:
1. The gate fired for a non-object mcp_config. 66 handler fixtures seed
`[]`, which is not a valid MCP config and cannot carry `mcpServers`, so
it expresses no boundary to protect. An old daemon does not widen it
either: mergeRuntimeAndAgentMcpConfig fails to unmarshal a non-object
and falls back to the agent config alone (verified directly). Gating
these blocked tasks with no security benefit, so the gate now requires a
JSON object.
2. The shared daemon test-request helper advertised no capabilities, so
every claim test was accidentally simulating a pre-#6283 daemon. It now
defaults authoritative-mcp-v1 on, matching what every current daemon
sends. Only that capability — skill-bundles / coalesced-comments / rpc
are feature negotiations whose absence tests real legacy behaviour, so
they stay opt-in per test.
Adds claim-level coverage for the gate itself, which is what the unit tests
alone could not catch: an outdated daemon gets 412 with an actionable
message and the task is cancelled; a capability-advertising daemon gets
200; the inherit_runtime opt-in lets an outdated daemon through; and an
unmanaged or non-object config is never gated.
Verified against a real migrated schema this time (throwaway Postgres),
which is how the three failures were reproduced locally and confirmed
fixed: `go test ./internal/handler ./internal/daemon` both ok.
Co-authored-by: multica-agent <github@multica.ai>
* fix(mcp): surface the daemon-upgrade refusal and stop gating safe providers
Addresses the second review round on #6292.
1. The refusal is now visible wherever the operator looks. The default
claim path is the machine-level BATCH endpoint, which skips build
failures and still answers 200 {"tasks":[]}, so the previous bare
CancelTask showed a task that vanished with no stated reason — turning an
explicit upgrade requirement into an unexplained failure. The claim path
now fails the task with a new classified reason,
mcp_config_daemon_outdated, plus the actionable message. That reaches the
user on all three claim paths and on any daemon version, which a new
response field could not: the audience is by definition a daemon too old
to read one. The per-runtime path keeps its 412.
The reason is deliberately not auto-retryable — the same outdated daemon
would claim the retry and fail it again.
2. The gate no longer cancels safe tasks. It applied to every provider, but
only claude / codebuddy / codex / cursor / opencode / openclaw were ever
merged with host MCP by an old daemon (loadRuntimeMcpServerConfigs).
Qwen was never merged and already had strict semantics, so its tasks were
being failed for a risk that does not exist. Scoped via
providersOldDaemonsMergedRuntimeMcp; an unknown provider does not gate,
because the gate should only fire where the old behaviour is concrete.
3. The new authoritative_mcp flag now goes through the API schema layer.
Both local-skills responses were returning raw network JSON, so the flag
that decides whether the UI may assert an MCP boundary rested on an
unchecked type assertion. Adds RuntimeLocalSkillListRequestSchema with
authoritative_mcp and mcp_supported defaulting to FALSE — the fail-closed
direction — and a MALFORMED_ fallback that cannot express a guarantee.
Claim-level tests now cover all three paths, which is what the previous
helper-only tests missed: per-runtime 412, batch recording the refusal on
the task while still delivering the healthy tasks in the same batch, WS RPC
refusing and accepting, the qwen negative case, the inherit_runtime escape
hatch, and unmanaged / non-object configs.
Verified against a real migrated schema (throwaway Postgres):
go test ./internal/handler ./internal/daemon ./pkg/agent ./pkg/taskfailure
./internal/service all ok.
Co-authored-by: multica-agent <github@multica.ai>
* fix(mcp): register the new failure reason and wire its copy into the UI
Addresses the third review round on #6292.
1. mcp_config_daemon_outdated was declared but never registered in
taskfailure.allReasons, so metrics.NormalizeFailureReason missed the
known-value map and fell through to free-text Classify() — relabelling a
platform-side refusal as `agent_error.unknown` (verified directly) and
leaving the Prometheus series un-pre-warmed. Registered it, canonical
count 22 → 23 (platform 8 → 9), with the wire value and IsAgentError split
pinned. New test pins the WHOLE canonical set through
NormalizeFailureReason so forgetting the next reason fails a test instead
of quietly mislabelling a metric; NormalizeFailureReason had no coverage
at all before.
2. The upgrade copy was dead. The locale strings landed last round but
neither consumer mapped the reason: chatFailureCopy fell back to generic
failure text with the actionable detail buried in the collapsed raw
error, and task-failure.ts rendered the bare wire value
`mcp_config_daemon_outdated` in the agent activity list and issue
execution log. Both are mapped now, with regression tests, plus the
runtime class pinned in failure-class.test.ts. This directly contradicted
the claim in the claim-path comment that every path reaches the user, so
that is now actually true.
3. providersOldDaemonsMergedRuntimeMcp is documented as what it is: a FROZEN
record of what pre-capability daemons merged, not a mirror of the daemon's
current provider switch. The old "keep the two lists in lockstep" note was
actively harmful advice — runtime MCP discovery for a new provider can only
ship in a daemon that already advertises the capability (never gated), so
adding it here would fail tasks on old daemons that never merged for it,
re-creating the qwen false-positive. Pinned with a test.
Also corrects a stale count in task-failure.ts (7 → 9 platform reasons).
Verified against a real migrated schema (throwaway Postgres): full backend
suite green apart from the pre-existing environmental cmd/multica guard; all
9 TestMcpGate_* integration tests pass.
Co-authored-by: multica-agent <github@multica.ai>
* docs(taskfailure): correct taxonomy counts and finish the reason registration
Non-blocking nits from the fourth review round on #6292.
- Taxonomy counts now say 23 reasons / 9 platform-side. Registering
mcp_config_daemon_outdated last round updated the assertions but not the
prose. Swept the whole repo rather than only the flagged lines, which
turned up four more that were already stale at 21 and drifted further:
handler/dashboard.go, daemon/poisoned.go, core/types/agent.ts, and the
db/queries/task_usage.sql comment sqlc copies into the generated file.
The generated file's comment was updated by hand to match its source.
Running `sqlc generate` churned 58 lines across 47 unrelated files — the
local sqlc version differs from the one that produced the checked-in
output — so that churn was reverted and only the one intended line kept.
- failure_test.go's `required` list now includes
ReasonMcpConfigDaemonOutdated. Length and label assertions already covered
the reason, but the list is documented as the complete canonical set, so
the omission contradicted its own comment.
- Restored the line break in chat-message-list.test.tsx that a previous edit
of mine collapsed.
Comment, test-fixture and formatting only; no behaviour change.
Co-authored-by: multica-agent <github@multica.ai>
---------
Co-authored-by: Eve <eve@multica-ai.local>
Co-authored-by: multica-agent <github@multica.ai>
628 lines
23 KiB
Go
628 lines
23 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: task_usage.sql
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const getIssueUsageSummary = `-- name: GetIssueUsageSummary :one
|
|
SELECT
|
|
COALESCE(SUM(tu.input_tokens), 0)::bigint AS total_input_tokens,
|
|
COALESCE(SUM(tu.output_tokens), 0)::bigint AS total_output_tokens,
|
|
COALESCE(SUM(tu.cache_read_tokens), 0)::bigint AS total_cache_read_tokens,
|
|
COALESCE(SUM(tu.cache_write_tokens), 0)::bigint AS total_cache_write_tokens,
|
|
COALESCE(SUM(tu.cost_usd_ticks), 0)::bigint AS total_cost_usd_ticks,
|
|
COALESCE(SUM(tu.input_tokens) FILTER (WHERE tu.cost_usd_ticks IS NULL), 0)::bigint AS uncosted_input_tokens,
|
|
COALESCE(SUM(tu.output_tokens) FILTER (WHERE tu.cost_usd_ticks IS NULL), 0)::bigint AS uncosted_output_tokens,
|
|
COALESCE(SUM(tu.cache_read_tokens) FILTER (WHERE tu.cost_usd_ticks IS NULL), 0)::bigint AS uncosted_cache_read_tokens,
|
|
COALESCE(SUM(tu.cache_write_tokens) FILTER (WHERE tu.cost_usd_ticks IS NULL), 0)::bigint AS uncosted_cache_write_tokens,
|
|
COUNT(DISTINCT tu.task_id)::int AS task_count
|
|
FROM task_usage tu
|
|
JOIN agent_task_queue atq ON atq.id = tu.task_id
|
|
WHERE atq.issue_id = $1
|
|
`
|
|
|
|
type GetIssueUsageSummaryRow struct {
|
|
TotalInputTokens int64 `json:"total_input_tokens"`
|
|
TotalOutputTokens int64 `json:"total_output_tokens"`
|
|
TotalCacheReadTokens int64 `json:"total_cache_read_tokens"`
|
|
TotalCacheWriteTokens int64 `json:"total_cache_write_tokens"`
|
|
TotalCostUsdTicks int64 `json:"total_cost_usd_ticks"`
|
|
UncostedInputTokens int64 `json:"uncosted_input_tokens"`
|
|
UncostedOutputTokens int64 `json:"uncosted_output_tokens"`
|
|
UncostedCacheReadTokens int64 `json:"uncosted_cache_read_tokens"`
|
|
UncostedCacheWriteTokens int64 `json:"uncosted_cache_write_tokens"`
|
|
TaskCount int32 `json:"task_count"`
|
|
}
|
|
|
|
func (q *Queries) GetIssueUsageSummary(ctx context.Context, issueID pgtype.UUID) (GetIssueUsageSummaryRow, error) {
|
|
row := q.db.QueryRow(ctx, getIssueUsageSummary, issueID)
|
|
var i GetIssueUsageSummaryRow
|
|
err := row.Scan(
|
|
&i.TotalInputTokens,
|
|
&i.TotalOutputTokens,
|
|
&i.TotalCacheReadTokens,
|
|
&i.TotalCacheWriteTokens,
|
|
&i.TotalCostUsdTicks,
|
|
&i.UncostedInputTokens,
|
|
&i.UncostedOutputTokens,
|
|
&i.UncostedCacheReadTokens,
|
|
&i.UncostedCacheWriteTokens,
|
|
&i.TaskCount,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getTaskUsage = `-- name: GetTaskUsage :many
|
|
SELECT id, task_id, provider, model, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, created_at, updated_at, cost_usd_ticks FROM task_usage
|
|
WHERE task_id = $1
|
|
ORDER BY model
|
|
`
|
|
|
|
func (q *Queries) GetTaskUsage(ctx context.Context, taskID pgtype.UUID) ([]TaskUsage, error) {
|
|
rows, err := q.db.Query(ctx, getTaskUsage, taskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []TaskUsage{}
|
|
for rows.Next() {
|
|
var i TaskUsage
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.TaskID,
|
|
&i.Provider,
|
|
&i.Model,
|
|
&i.InputTokens,
|
|
&i.OutputTokens,
|
|
&i.CacheReadTokens,
|
|
&i.CacheWriteTokens,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.CostUsdTicks,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listDashboardAgentRunTime = `-- name: ListDashboardAgentRunTime :many
|
|
SELECT
|
|
atq.agent_id,
|
|
COALESCE(
|
|
SUM(EXTRACT(EPOCH FROM (atq.completed_at - atq.started_at)))::bigint,
|
|
0
|
|
)::bigint AS total_seconds,
|
|
COUNT(*)::int AS task_count,
|
|
COUNT(*) FILTER (WHERE atq.status = 'failed')::int AS failed_count
|
|
FROM agent_task_queue atq
|
|
JOIN agent a ON a.id = atq.agent_id
|
|
LEFT JOIN issue i ON i.id = atq.issue_id
|
|
WHERE a.workspace_id = $1
|
|
AND atq.status IN ('completed', 'failed')
|
|
AND atq.started_at IS NOT NULL
|
|
AND atq.completed_at IS NOT NULL
|
|
AND atq.completed_at >= $2::timestamptz
|
|
AND ($3::uuid IS NULL OR i.project_id = $3)
|
|
GROUP BY atq.agent_id
|
|
ORDER BY total_seconds DESC
|
|
`
|
|
|
|
type ListDashboardAgentRunTimeParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
Since pgtype.Timestamptz `json:"since"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
}
|
|
|
|
type ListDashboardAgentRunTimeRow struct {
|
|
AgentID pgtype.UUID `json:"agent_id"`
|
|
TotalSeconds int64 `json:"total_seconds"`
|
|
TaskCount int32 `json:"task_count"`
|
|
FailedCount int32 `json:"failed_count"`
|
|
}
|
|
|
|
// Per-agent total task run time and task count for the workspace, optionally
|
|
// scoped to a single project. Counts only terminal runs (completed or failed)
|
|
// with both started_at and completed_at populated — queued/running tasks have
|
|
// no finite duration. Anchored on completed_at so the window matches the
|
|
// token cost window (which is anchored on tu.created_at, ~= completion time).
|
|
//
|
|
// No date bucketing, so no @tz — but @since is the viewer's local
|
|
// start-of-day for the EXACT N-day window (parseExactSinceParamInTZ), so the
|
|
// "last N days" window lines up with the per-agent cost card and the daily
|
|
// charts the client trims to the same span; passed straight through without
|
|
// re-truncation.
|
|
func (q *Queries) ListDashboardAgentRunTime(ctx context.Context, arg ListDashboardAgentRunTimeParams) ([]ListDashboardAgentRunTimeRow, error) {
|
|
rows, err := q.db.Query(ctx, listDashboardAgentRunTime, arg.WorkspaceID, arg.Since, arg.ProjectID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListDashboardAgentRunTimeRow{}
|
|
for rows.Next() {
|
|
var i ListDashboardAgentRunTimeRow
|
|
if err := rows.Scan(
|
|
&i.AgentID,
|
|
&i.TotalSeconds,
|
|
&i.TaskCount,
|
|
&i.FailedCount,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listDashboardFailuresByAgent = `-- name: ListDashboardFailuresByAgent :many
|
|
SELECT
|
|
atq.agent_id,
|
|
CASE
|
|
WHEN atq.status = 'failed'
|
|
THEN COALESCE(NULLIF(atq.failure_reason, ''), 'unclassified')
|
|
ELSE ''
|
|
END AS failure_reason,
|
|
COUNT(*)::int AS task_count
|
|
FROM agent_task_queue atq
|
|
JOIN agent a ON a.id = atq.agent_id
|
|
LEFT JOIN issue i ON i.id = atq.issue_id
|
|
WHERE a.workspace_id = $1
|
|
AND atq.status IN ('completed', 'failed')
|
|
AND atq.completed_at IS NOT NULL
|
|
AND atq.completed_at >= $2::timestamptz
|
|
AND ($3::uuid IS NULL OR i.project_id = $3)
|
|
GROUP BY atq.agent_id, 2
|
|
ORDER BY atq.agent_id, 2
|
|
`
|
|
|
|
type ListDashboardFailuresByAgentParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
Since pgtype.Timestamptz `json:"since"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
}
|
|
|
|
type ListDashboardFailuresByAgentRow struct {
|
|
AgentID pgtype.UUID `json:"agent_id"`
|
|
FailureReason string `json:"failure_reason"`
|
|
TaskCount int32 `json:"task_count"`
|
|
}
|
|
|
|
// Per-(agent, failure_reason) terminal-task counts — the "top offenders"
|
|
// half of the dashboard's errors breakdown. Same `failure_reason = ”`
|
|
// succeeded-bucket convention as ListDashboardFailuresDaily, so the client
|
|
// can rank agents by failure rate rather than raw count.
|
|
//
|
|
// No date bucketing, so no @tz — @since is the viewer's local
|
|
// start-of-day-(N) so the window lines up with the per-agent run-time card.
|
|
func (q *Queries) ListDashboardFailuresByAgent(ctx context.Context, arg ListDashboardFailuresByAgentParams) ([]ListDashboardFailuresByAgentRow, error) {
|
|
rows, err := q.db.Query(ctx, listDashboardFailuresByAgent, arg.WorkspaceID, arg.Since, arg.ProjectID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListDashboardFailuresByAgentRow{}
|
|
for rows.Next() {
|
|
var i ListDashboardFailuresByAgentRow
|
|
if err := rows.Scan(&i.AgentID, &i.FailureReason, &i.TaskCount); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listDashboardFailuresDaily = `-- name: ListDashboardFailuresDaily :many
|
|
SELECT
|
|
DATE(atq.completed_at AT TIME ZONE $2::text) AS date,
|
|
CASE
|
|
WHEN atq.status = 'failed'
|
|
THEN COALESCE(NULLIF(atq.failure_reason, ''), 'unclassified')
|
|
ELSE ''
|
|
END AS failure_reason,
|
|
COUNT(*)::int AS task_count
|
|
FROM agent_task_queue atq
|
|
JOIN agent a ON a.id = atq.agent_id
|
|
LEFT JOIN issue i ON i.id = atq.issue_id
|
|
WHERE a.workspace_id = $1
|
|
AND atq.status IN ('completed', 'failed')
|
|
AND atq.completed_at IS NOT NULL
|
|
AND atq.completed_at >= $3::timestamptz
|
|
AND ($4::uuid IS NULL OR i.project_id = $4)
|
|
GROUP BY 1, 2
|
|
ORDER BY 1 DESC, 2
|
|
`
|
|
|
|
type ListDashboardFailuresDailyParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
Tz string `json:"tz"`
|
|
Since pgtype.Timestamptz `json:"since"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
}
|
|
|
|
type ListDashboardFailuresDailyRow struct {
|
|
Date pgtype.Date `json:"date"`
|
|
FailureReason string `json:"failure_reason"`
|
|
TaskCount int32 `json:"task_count"`
|
|
}
|
|
|
|
// Daily per-(date, failure_reason) terminal-task counts for the workspace,
|
|
// optionally scoped to a single project. Powers the workspace dashboard's
|
|
// "Errors" trend and the errors-by-class breakdown.
|
|
//
|
|
// Shape note: this returns EVERY terminal task, not just the failures. The
|
|
// `failure_reason = ”` row of each date carries that date's succeeded
|
|
// count, which is the denominator the client needs for an error rate. A
|
|
// failed row whose failure_reason column is NULL or empty (pre-MUL-1949
|
|
// rows, or a failure path that forgot to classify) collapses into the
|
|
// 'unclassified' bucket so it stays countable instead of masquerading as a
|
|
// success. Cardinality is bounded by days x (23 reasons + 2), so the whole
|
|
// window fits in one small payload.
|
|
//
|
|
// Unlike ListDashboardRunTimeDaily this does NOT require started_at — a task
|
|
// that expired in the queue (failure_reason='queued_expired') never started
|
|
// but is unambiguously a failure, and dropping it would under-report exactly
|
|
// the outage the Errors chart exists to surface. Every failure path sets
|
|
// completed_at, so bucketing on it covers all of them.
|
|
//
|
|
// @since is already the viewer's local start-of-day-(N) (parseSinceParamInTZ)
|
|
// — passed straight through, NOT re-truncated; see ListDashboardUsageDaily.
|
|
func (q *Queries) ListDashboardFailuresDaily(ctx context.Context, arg ListDashboardFailuresDailyParams) ([]ListDashboardFailuresDailyRow, error) {
|
|
rows, err := q.db.Query(ctx, listDashboardFailuresDaily,
|
|
arg.WorkspaceID,
|
|
arg.Tz,
|
|
arg.Since,
|
|
arg.ProjectID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListDashboardFailuresDailyRow{}
|
|
for rows.Next() {
|
|
var i ListDashboardFailuresDailyRow
|
|
if err := rows.Scan(&i.Date, &i.FailureReason, &i.TaskCount); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listDashboardRunTimeDaily = `-- name: ListDashboardRunTimeDaily :many
|
|
SELECT
|
|
DATE(atq.completed_at AT TIME ZONE $2::text) AS date,
|
|
COALESCE(
|
|
SUM(EXTRACT(EPOCH FROM (atq.completed_at - atq.started_at)))::bigint,
|
|
0
|
|
)::bigint AS total_seconds,
|
|
COUNT(*)::int AS task_count,
|
|
COUNT(*) FILTER (WHERE atq.status = 'failed')::int AS failed_count
|
|
FROM agent_task_queue atq
|
|
JOIN agent a ON a.id = atq.agent_id
|
|
LEFT JOIN issue i ON i.id = atq.issue_id
|
|
WHERE a.workspace_id = $1
|
|
AND atq.status IN ('completed', 'failed')
|
|
AND atq.started_at IS NOT NULL
|
|
AND atq.completed_at IS NOT NULL
|
|
AND atq.completed_at >= $3::timestamptz
|
|
AND ($4::uuid IS NULL OR i.project_id = $4)
|
|
GROUP BY DATE(atq.completed_at AT TIME ZONE $2::text)
|
|
ORDER BY DATE(atq.completed_at AT TIME ZONE $2::text) DESC
|
|
`
|
|
|
|
type ListDashboardRunTimeDailyParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
Tz string `json:"tz"`
|
|
Since pgtype.Timestamptz `json:"since"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
}
|
|
|
|
type ListDashboardRunTimeDailyRow struct {
|
|
Date pgtype.Date `json:"date"`
|
|
TotalSeconds int64 `json:"total_seconds"`
|
|
TaskCount int32 `json:"task_count"`
|
|
FailedCount int32 `json:"failed_count"`
|
|
}
|
|
|
|
// Daily per-date run time + task counts for the workspace, optionally
|
|
// scoped to a single project. Powers the workspace dashboard's "Time"
|
|
// and "Tasks" metrics on the same toggle as Tokens / Cost. Bucketed by
|
|
// completed_at (terminal time) sliced into calendar days under the
|
|
// caller-supplied @tz — same Viewing-tz treatment as ListDashboardUsageDaily
|
|
// so the Time / Tasks tabs cut their day boundary identically to the
|
|
// Cost / Tokens tabs (a viewer east of UTC would otherwise see the four
|
|
// tabs disagree on a "1d" window). Only terminal tasks (completed or
|
|
// failed) with both started_at and completed_at populated contribute.
|
|
//
|
|
// @since is already the viewer's local start-of-day-(N) (parseSinceParamInTZ)
|
|
// — passed straight through, NOT re-truncated; see ListDashboardUsageDaily.
|
|
func (q *Queries) ListDashboardRunTimeDaily(ctx context.Context, arg ListDashboardRunTimeDailyParams) ([]ListDashboardRunTimeDailyRow, error) {
|
|
rows, err := q.db.Query(ctx, listDashboardRunTimeDaily,
|
|
arg.WorkspaceID,
|
|
arg.Tz,
|
|
arg.Since,
|
|
arg.ProjectID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListDashboardRunTimeDailyRow{}
|
|
for rows.Next() {
|
|
var i ListDashboardRunTimeDailyRow
|
|
if err := rows.Scan(
|
|
&i.Date,
|
|
&i.TotalSeconds,
|
|
&i.TaskCount,
|
|
&i.FailedCount,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listDashboardUsageByAgent = `-- name: ListDashboardUsageByAgent :many
|
|
SELECT
|
|
agent_id,
|
|
LOWER(provider) AS provider,
|
|
model,
|
|
SUM(input_tokens)::bigint AS input_tokens,
|
|
SUM(output_tokens)::bigint AS output_tokens,
|
|
SUM(cache_read_tokens)::bigint AS cache_read_tokens,
|
|
SUM(cache_write_tokens)::bigint AS cache_write_tokens,
|
|
SUM(cost_usd_ticks)::bigint AS cost_usd_ticks,
|
|
SUM(COALESCE(uncosted_input_tokens, input_tokens))::bigint AS uncosted_input_tokens,
|
|
SUM(COALESCE(uncosted_output_tokens, output_tokens))::bigint AS uncosted_output_tokens,
|
|
SUM(COALESCE(uncosted_cache_read_tokens, cache_read_tokens))::bigint AS uncosted_cache_read_tokens,
|
|
SUM(COALESCE(uncosted_cache_write_tokens, cache_write_tokens))::bigint AS uncosted_cache_write_tokens,
|
|
SUM(task_count)::int AS task_count
|
|
FROM task_usage_hourly
|
|
WHERE workspace_id = $1
|
|
AND bucket_hour >= $2::timestamptz
|
|
AND ($3::uuid IS NULL OR project_id = $3)
|
|
GROUP BY agent_id, LOWER(provider), model
|
|
ORDER BY agent_id, LOWER(provider), model
|
|
`
|
|
|
|
type ListDashboardUsageByAgentParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
Since pgtype.Timestamptz `json:"since"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
}
|
|
|
|
type ListDashboardUsageByAgentRow struct {
|
|
AgentID pgtype.UUID `json:"agent_id"`
|
|
Provider string `json:"provider"`
|
|
Model string `json:"model"`
|
|
InputTokens int64 `json:"input_tokens"`
|
|
OutputTokens int64 `json:"output_tokens"`
|
|
CacheReadTokens int64 `json:"cache_read_tokens"`
|
|
CacheWriteTokens int64 `json:"cache_write_tokens"`
|
|
CostUsdTicks int64 `json:"cost_usd_ticks"`
|
|
UncostedInputTokens int64 `json:"uncosted_input_tokens"`
|
|
UncostedOutputTokens int64 `json:"uncosted_output_tokens"`
|
|
UncostedCacheReadTokens int64 `json:"uncosted_cache_read_tokens"`
|
|
UncostedCacheWriteTokens int64 `json:"uncosted_cache_write_tokens"`
|
|
TaskCount int32 `json:"task_count"`
|
|
}
|
|
|
|
// Per-(agent, provider, model) token aggregates from `task_usage_hourly`. No
|
|
// date grouping in the result, so this query takes no `@tz` — the
|
|
// @since cutoff is a raw timestamptz the Go layer has already computed
|
|
// in the viewer's tz. Model dimension is preserved so the client can
|
|
// compute cost from its per-model pricing table; the client folds rows
|
|
// by agent for the "by agent" list on the dashboard.
|
|
//
|
|
// task_count is summed across hourly buckets — one task that spans
|
|
// multiple hours lands in multiple buckets, so this over-counts by
|
|
// hour the same way the daily version over-counted by day. The
|
|
// frontend prefers `ListDashboardAgentRunTime` for the user-facing
|
|
// "tasks" column, so this stays informational only.
|
|
// provider is LOWER()-normalized so mixed-case historical rows merge with
|
|
// new rows (see ListDashboardUsageDaily).
|
|
func (q *Queries) ListDashboardUsageByAgent(ctx context.Context, arg ListDashboardUsageByAgentParams) ([]ListDashboardUsageByAgentRow, error) {
|
|
rows, err := q.db.Query(ctx, listDashboardUsageByAgent, arg.WorkspaceID, arg.Since, arg.ProjectID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListDashboardUsageByAgentRow{}
|
|
for rows.Next() {
|
|
var i ListDashboardUsageByAgentRow
|
|
if err := rows.Scan(
|
|
&i.AgentID,
|
|
&i.Provider,
|
|
&i.Model,
|
|
&i.InputTokens,
|
|
&i.OutputTokens,
|
|
&i.CacheReadTokens,
|
|
&i.CacheWriteTokens,
|
|
&i.CostUsdTicks,
|
|
&i.UncostedInputTokens,
|
|
&i.UncostedOutputTokens,
|
|
&i.UncostedCacheReadTokens,
|
|
&i.UncostedCacheWriteTokens,
|
|
&i.TaskCount,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listDashboardUsageDaily = `-- name: ListDashboardUsageDaily :many
|
|
SELECT
|
|
DATE(bucket_hour AT TIME ZONE $2::text) AS date,
|
|
LOWER(provider) AS provider,
|
|
model,
|
|
SUM(input_tokens)::bigint AS input_tokens,
|
|
SUM(output_tokens)::bigint AS output_tokens,
|
|
SUM(cache_read_tokens)::bigint AS cache_read_tokens,
|
|
SUM(cache_write_tokens)::bigint AS cache_write_tokens,
|
|
SUM(cost_usd_ticks)::bigint AS cost_usd_ticks,
|
|
SUM(COALESCE(uncosted_input_tokens, input_tokens))::bigint AS uncosted_input_tokens,
|
|
SUM(COALESCE(uncosted_output_tokens, output_tokens))::bigint AS uncosted_output_tokens,
|
|
SUM(COALESCE(uncosted_cache_read_tokens, cache_read_tokens))::bigint AS uncosted_cache_read_tokens,
|
|
SUM(COALESCE(uncosted_cache_write_tokens, cache_write_tokens))::bigint AS uncosted_cache_write_tokens,
|
|
SUM(task_count)::int AS task_count
|
|
FROM task_usage_hourly
|
|
WHERE workspace_id = $1
|
|
AND bucket_hour >= $3::timestamptz
|
|
AND ($4::uuid IS NULL OR project_id = $4)
|
|
GROUP BY DATE(bucket_hour AT TIME ZONE $2::text), LOWER(provider), model
|
|
ORDER BY DATE(bucket_hour AT TIME ZONE $2::text) DESC, LOWER(provider), model
|
|
`
|
|
|
|
type ListDashboardUsageDailyParams struct {
|
|
WorkspaceID pgtype.UUID `json:"workspace_id"`
|
|
Tz string `json:"tz"`
|
|
Since pgtype.Timestamptz `json:"since"`
|
|
ProjectID pgtype.UUID `json:"project_id"`
|
|
}
|
|
|
|
type ListDashboardUsageDailyRow struct {
|
|
Date pgtype.Date `json:"date"`
|
|
Provider string `json:"provider"`
|
|
Model string `json:"model"`
|
|
InputTokens int64 `json:"input_tokens"`
|
|
OutputTokens int64 `json:"output_tokens"`
|
|
CacheReadTokens int64 `json:"cache_read_tokens"`
|
|
CacheWriteTokens int64 `json:"cache_write_tokens"`
|
|
CostUsdTicks int64 `json:"cost_usd_ticks"`
|
|
UncostedInputTokens int64 `json:"uncosted_input_tokens"`
|
|
UncostedOutputTokens int64 `json:"uncosted_output_tokens"`
|
|
UncostedCacheReadTokens int64 `json:"uncosted_cache_read_tokens"`
|
|
UncostedCacheWriteTokens int64 `json:"uncosted_cache_write_tokens"`
|
|
TaskCount int32 `json:"task_count"`
|
|
}
|
|
|
|
// Daily per-(date, provider, model) token aggregates for the workspace, served
|
|
// from the UTC-bucketed `task_usage_hourly` table and
|
|
// sliced to calendar days under the caller-supplied @tz. Optionally
|
|
// scoped to a single project via sqlc.narg('project_id'). Powers the
|
|
// workspace dashboard's daily cost chart.
|
|
// The viewer's tz is applied here at query time, so a viewer in
|
|
// Asia/Shanghai gets their "today" cut at +08 and one in
|
|
// America/Los_Angeles gets theirs at -08 against the same UTC rows.
|
|
//
|
|
// @since is already the viewer's local start-of-day-(N) as a UTC
|
|
// instant (computed by parseSinceParamInTZ). It must NOT be re-truncated
|
|
// with DATE_TRUNC here — DATE_TRUNC operates in the session tz and would
|
|
// snap the cutoff back to UTC midnight, dragging in an extra partial
|
|
// local day for any non-UTC viewer.
|
|
// provider is LOWER()-normalized so mixed-case historical rows (written
|
|
// before the handler lowercased provider on write) merge with new rows
|
|
// instead of forming a separate case-variant bucket.
|
|
func (q *Queries) ListDashboardUsageDaily(ctx context.Context, arg ListDashboardUsageDailyParams) ([]ListDashboardUsageDailyRow, error) {
|
|
rows, err := q.db.Query(ctx, listDashboardUsageDaily,
|
|
arg.WorkspaceID,
|
|
arg.Tz,
|
|
arg.Since,
|
|
arg.ProjectID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListDashboardUsageDailyRow{}
|
|
for rows.Next() {
|
|
var i ListDashboardUsageDailyRow
|
|
if err := rows.Scan(
|
|
&i.Date,
|
|
&i.Provider,
|
|
&i.Model,
|
|
&i.InputTokens,
|
|
&i.OutputTokens,
|
|
&i.CacheReadTokens,
|
|
&i.CacheWriteTokens,
|
|
&i.CostUsdTicks,
|
|
&i.UncostedInputTokens,
|
|
&i.UncostedOutputTokens,
|
|
&i.UncostedCacheReadTokens,
|
|
&i.UncostedCacheWriteTokens,
|
|
&i.TaskCount,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const upsertTaskUsage = `-- name: UpsertTaskUsage :exec
|
|
INSERT INTO task_usage (task_id, provider, model, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd_ticks, updated_at)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, now())
|
|
ON CONFLICT (task_id, provider, model)
|
|
DO UPDATE SET
|
|
input_tokens = EXCLUDED.input_tokens,
|
|
output_tokens = EXCLUDED.output_tokens,
|
|
cache_read_tokens = EXCLUDED.cache_read_tokens,
|
|
cache_write_tokens = EXCLUDED.cache_write_tokens,
|
|
cost_usd_ticks = EXCLUDED.cost_usd_ticks,
|
|
updated_at = now()
|
|
`
|
|
|
|
type UpsertTaskUsageParams struct {
|
|
TaskID pgtype.UUID `json:"task_id"`
|
|
Provider string `json:"provider"`
|
|
Model string `json:"model"`
|
|
InputTokens int64 `json:"input_tokens"`
|
|
OutputTokens int64 `json:"output_tokens"`
|
|
CacheReadTokens int64 `json:"cache_read_tokens"`
|
|
CacheWriteTokens int64 `json:"cache_write_tokens"`
|
|
CostUsdTicks pgtype.Int8 `json:"cost_usd_ticks"`
|
|
}
|
|
|
|
// Bumps `updated_at` on INSERT and on conflict so the hourly-rollup worker
|
|
// detects the row as dirty and re-aggregates its bucket.
|
|
// Without the conflict-side bump, a correction to historical token counts
|
|
// would never propagate to the rollup.
|
|
// cost_usd_ticks is the provider's own price for this usage (1e-10 USD), NULL
|
|
// when it reports none. It is overwritten like the token counters so a
|
|
// corrected report replaces the previous figure rather than accumulating.
|
|
func (q *Queries) UpsertTaskUsage(ctx context.Context, arg UpsertTaskUsageParams) error {
|
|
_, err := q.db.Exec(ctx, upsertTaskUsage,
|
|
arg.TaskID,
|
|
arg.Provider,
|
|
arg.Model,
|
|
arg.InputTokens,
|
|
arg.OutputTokens,
|
|
arg.CacheReadTokens,
|
|
arg.CacheWriteTokens,
|
|
arg.CostUsdTicks,
|
|
)
|
|
return err
|
|
}
|