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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .agents/skills/ship/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ When the user runs `/ship`:
```
Then `git status --short` to see what regenerated — those files must be staged in step 7 alongside your own changes.

**Do NOT blanket-run the domain generators here.** `mship:generate` (`generate-mship-contracts.ts`) is an **umbrella** that drives all nine mothership contract generators (`mship-contracts`, `billing-protocol-contract`, `mship-tools`, the four `trace-*`, `metrics-contract`, `vfs-snapshot-contract`) and biome-formats `apps/sim/lib/mothership/generated/` — never run it *and* its constituents (they write the same files and corrupt each other in parallel), and never run it on an ordinary ship: it reads an **external** copilot-contract source that isn't checked out in most worktrees, so it hard-fails with `ENOENT` and would abort ship for an unrelated reason. `generate:pi-model-catalog` (under `apps/sim`) likewise regenerates from the installed Pi package, not repo source. `scripts/generate-docs.ts` rewrites the integration docs and client-safe catalog; run it when this PR changes their block/icon/landing-content inputs or when `check:integration-catalog` reports drift, then review its broad generated diff. Only when **this PR's diff actually touches** a domain generator's input do you regenerate it deliberately and run its matching `:check` (`bun run mship:check` / the individual `*:check`) — with the external source present.
**Do NOT blanket-run the domain generators here.** `mship-tools:generate` merges Sim's tool contracts into the base tool catalog in the **external** Mothership worker checkout (`<mothership>/worker/packages/contracts/tool-catalog-v1.json`), which most worktrees lack, so it hard-fails with `ENOENT` and would abort ship for an unrelated reason. The other mothership contracts in `apps/sim/lib/mothership/generated/` are copied in by the worker's `contracts:sync`, never generated here. `generate:pi-model-catalog` (under `apps/sim`) likewise regenerates from the installed Pi package, not repo source. `scripts/generate-docs.ts` rewrites the integration docs and client-safe catalog; run it when this PR changes their block/icon/landing-content inputs or when `check:integration-catalog` reports drift, then review its broad generated diff. Only when **this PR's diff actually touches** a domain generator's input do you regenerate it deliberately and run its matching `:check` (e.g. `bun run mship-tools:check`) — with the external source present.

**Phase B — run lint + every audit CI enforces, in parallel, and abort ship if any fails.** Before running the commands, compare this list with `.github/workflows/checks.yml`; when CI adds an audit, run it and update this skill instead of trusting a stale snapshot. The env-flag audit is currently an inline workflow block rather than a package script: when `apps/sim/lib/core/config/env-flags.ts` changed, run that current workflow block verbatim instead of copying a second version into this skill. Run `bun run lint` first (it autofixes formatting and mutates files, so don't parallelize it with the read-only audits), then run the base-sensitive block-registry check, then fan the independent audits out and collect exit codes:
```bash
Expand Down
27 changes: 8 additions & 19 deletions apps/sim/lib/model-router/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { createLogger } from '@sim/logger'
import { getErrorMessage } from '@sim/utils/errors'
import { env } from '@/lib/core/config/env'
import { getCostMultiplier, isHosted } from '@/lib/core/config/env-flags'
import type {
ModelRouterRequest,
ModelRouterResponse,
ModelRouterUsage,
} from '@/lib/mothership/generated/model-router'
import { fetchGo } from '@/lib/mothership/request/go/fetch'
import { getMothershipBaseURL } from '@/lib/mothership/server/agent-url'
import { validateModelProvider } from '@/ee/access-control/utils/permission-check'
Expand Down Expand Up @@ -100,22 +105,6 @@ export interface AutoRoutingSignals {
approxInputTokens: number
}

/** Mirrors mothership's model-router-v1 response usage block. */
interface ModelRouterUsage {
model: string
inputTokens: number
cachedInputTokens: number
outputTokens: number
cost: number
}

interface ModelRouterResponse {
choice?: string
decidedBy?: string
usage?: ModelRouterUsage
billable?: boolean
}

export interface AutoRoutingResult {
model: string
tier: AutoTierId | null
Expand Down Expand Up @@ -219,7 +208,7 @@ async function callModelRouter(
signals: AutoRoutingSignals,
ctx: ExecutionContext,
blockId: string
): Promise<ModelRouterResponse | null> {
): Promise<Partial<ModelRouterResponse> | null> {
const baseURL = await getMothershipBaseURL({ userId: ctx.userId ?? '' })

const res = await fetchGo(`${baseURL}/api/model-router`, {
Expand All @@ -244,7 +233,7 @@ async function callModelRouter(
workflowId: ctx.workflowId,
blockId,
executionId: ctx.executionId,
}),
} satisfies ModelRouterRequest),
signal: AbortSignal.timeout(MODEL_ROUTER_TIMEOUT_MS),
spanName: 'sim → go /api/model-router',
operation: 'model_router',
Expand All @@ -254,7 +243,7 @@ async function callModelRouter(
logger.warn('Model router returned non-OK status', { status: res.status })
return null
}
return (await res.json().catch(() => null)) as ModelRouterResponse | null
return (await res.json().catch(() => null)) as Partial<ModelRouterResponse> | null
}

/**
Expand Down
96 changes: 45 additions & 51 deletions apps/sim/lib/mothership/generated/billing-protocol-v1.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,75 @@
/**
* AUTO-GENERATED FILE. DO NOT EDIT.
*
* Source: copilot/copilot/contracts/billing-protocol-v1.schema.json
* Regenerate with: bun run billing-protocol-contract:generate
*/
// GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/billing-protocol-v1.ts
// Regenerate with `bun run contracts:sync` in the worker.

// The trusted Sim-to-worker billing protocol: header names, protocol modes, bounds, safe
// API-key denials, and callback and analytics outcomes.

export const BILLING_PROTOCOL_HEADERS = {
accountDecision: 'x-sim-billing-account-decision',
attribution: 'x-sim-billing-attribution',
protocol: 'x-sim-billing-protocol',
requestId: 'x-sim-billing-request-id',
} as const
accountDecision: "x-sim-billing-account-decision",
attribution: "x-sim-billing-attribution",
protocol: "x-sim-billing-protocol",
requestId: "x-sim-billing-request-id",
} as const;

export const BILLING_ACCOUNT_DECISION_HEADER = BILLING_PROTOCOL_HEADERS.accountDecision
export const BILLING_ATTRIBUTION_HEADER = BILLING_PROTOCOL_HEADERS.attribution
export const COPILOT_BILLING_PROTOCOL_HEADER = BILLING_PROTOCOL_HEADERS.protocol
export const BILLING_REQUEST_ID_HEADER = BILLING_PROTOCOL_HEADERS.requestId
export const BILLING_ACCOUNT_DECISION_HEADER = BILLING_PROTOCOL_HEADERS.accountDecision;
export const BILLING_ATTRIBUTION_HEADER = BILLING_PROTOCOL_HEADERS.attribution;
export const COPILOT_BILLING_PROTOCOL_HEADER = BILLING_PROTOCOL_HEADERS.protocol;
export const BILLING_REQUEST_ID_HEADER = BILLING_PROTOCOL_HEADERS.requestId;

export const COPILOT_BILLING_PROTOCOL = {
attributed: 'attribution-v1',
direct: 'direct-v1',
legacy: 'legacy-v0',
} as const
attributed: "attribution-v1",
direct: "direct-v1",
legacy: "legacy-v0",
} as const;

export type CopilotBillingProtocol =
(typeof COPILOT_BILLING_PROTOCOL)[keyof typeof COPILOT_BILLING_PROTOCOL]
export type CopilotBillingProtocol = (typeof COPILOT_BILLING_PROTOCOL)[keyof typeof COPILOT_BILLING_PROTOCOL];

export const COPILOT_BILLING_PROTOCOL_VALUES = [
COPILOT_BILLING_PROTOCOL.attributed,
COPILOT_BILLING_PROTOCOL.direct,
COPILOT_BILLING_PROTOCOL.legacy,
] as const
] as const;

export const COPILOT_VALIDATION_PURPOSE = {
newTurn: 'new-turn',
continuation: 'continuation',
cancellation: 'cancellation',
} as const
newTurn: "new-turn",
continuation: "continuation",
cancellation: "cancellation",
} as const;

export const COPILOT_VALIDATION_PURPOSE_VALUES = [
'new-turn',
'continuation',
'cancellation',
] as const
export const COPILOT_VALIDATION_PURPOSE_VALUES = ["new-turn", "continuation", "cancellation"] as const;

export type CopilotValidationPurpose = (typeof COPILOT_VALIDATION_PURPOSE_VALUES)[number]
export type CopilotValidationPurpose = (typeof COPILOT_VALIDATION_PURPOSE_VALUES)[number];

export const BILLING_ATTRIBUTION_HEADER_MAX_BYTES = 8192
export const BILLING_ACCOUNT_DECISION_HEADER_MAX_BYTES = 2048
export const BILLING_ATTRIBUTION_HEADER_MAX_BYTES = 8192;
export const BILLING_ACCOUNT_DECISION_HEADER_MAX_BYTES = 2048;

export const BILLING_CALLBACK_OUTCOME = {
billingContextMismatch: {
code: 'BILLING_CONTEXT_MISMATCH',
message: 'Idempotency key is already bound to a different billing context',
code: "BILLING_CONTEXT_MISMATCH",
message: "Idempotency key is already bound to a different billing context",
},
billingPeriodElapsed: {
code: 'BILLING_PERIOD_ELAPSED',
message: 'Billing period has elapsed; reconciliation required',
code: "BILLING_PERIOD_ELAPSED",
message: "Billing period has elapsed; reconciliation required",
},
billingUserNotFound: {
code: 'BILLING_USER_NOT_FOUND',
message: 'Billing user no longer exists; reconciliation required',
code: "BILLING_USER_NOT_FOUND",
message: "Billing user no longer exists; reconciliation required",
},
duplicateBillingEvent: {
code: 'DUPLICATE_BILLING_EVENT',
message: 'Duplicate request: cumulative cost already recorded',
code: "DUPLICATE_BILLING_EVENT",
message: "Duplicate request: cumulative cost already recorded",
},
} as const
} as const;

export const BillingAnalyticsOutcome = {
DeadLettered: 'dead_lettered',
Duplicate: 'duplicate',
NotBillable: 'not_billable',
RetriesExhausted: 'retries_exhausted',
Success: 'success',
Unknown: 'unknown',
} as const
DeadLettered: "dead_lettered",
Duplicate: "duplicate",
NotBillable: "not_billable",
RetriesExhausted: "retries_exhausted",
Success: "success",
Unknown: "unknown",
} as const;

export type BillingAnalyticsOutcomeValue =
(typeof BillingAnalyticsOutcome)[keyof typeof BillingAnalyticsOutcome]
(typeof BillingAnalyticsOutcome)[keyof typeof BillingAnalyticsOutcome];
131 changes: 61 additions & 70 deletions apps/sim/lib/mothership/generated/metrics-v1.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,67 @@
// AUTO-GENERATED FILE. DO NOT EDIT.
//
// Source: copilot/copilot/contracts/metrics-v1.schema.json
// Regenerate with: bun run metrics-contract:generate
//
// Canonical mothership OTel metric names. Call sites should reference
// `Metric.<Identifier>` (e.g. `Metric.CopilotToolDuration`) rather than raw
// string literals, so the Go-side contract is the single source of truth and
// typos become compile errors.
//
// NAMES ONLY. Label keys and histogram bucket boundaries are NOT in this
// contract — Go owns the label-cardinality allowlist and the shared bucket
// constant, and the Sim emitter MUST mirror those by hand so the Go∪Sim metric
// union is queryable as one series set.
// GENERATED — do not edit. Source of truth: mothership worker packages/contracts/src/metrics-v1.ts
// Regenerate with `bun run contracts:sync` in the worker.

// Canonical mothership OTel metric names Sim emits. Names only: label keys and histogram
// buckets are the emitter's.

export const Metric = {
CopilotBillingLongContextCount: 'copilot.billing.long_context.count',
CopilotCacheAttempted: 'copilot.cache.attempted',
CopilotCacheHit: 'copilot.cache.hit',
CopilotCacheWrite: 'copilot.cache.write',
CopilotChatBlobBytes: 'copilot.chat.blob.bytes',
CopilotChatBlobCount: 'copilot.chat.blob.count',
CopilotDegradedCount: 'copilot.degraded.count',
CopilotFileReadDuration: 'copilot.file.read.duration',
CopilotFileReadSize: 'copilot.file.read.size',
CopilotMessagesSerializeDuration: 'copilot.messages.serialize.duration',
CopilotOptionsEmittedCount: 'copilot.options.emitted.count',
CopilotPromptComponentChars: 'copilot.prompt.component.chars',
CopilotRequestCount: 'copilot.request.count',
CopilotRequestDuration: 'copilot.request.duration',
CopilotToolCalls: 'copilot.tool.calls',
CopilotToolDuration: 'copilot.tool.duration',
CopilotVfsDelta: 'copilot.vfs.delta',
CopilotVfsMaterializeDuration: 'copilot.vfs.materialize.duration',
GenAiClientCacheTokenUsage: 'gen_ai.client.cache.token.usage',
GenAiClientTokenUsage: 'gen_ai.client.token.usage',
LlmClientCompactions: 'llm.client.compactions',
LlmClientContextTokens: 'llm.client.context_tokens',
LlmClientErrors: 'llm.client.errors',
LlmClientOutputCutoff: 'llm.client.output_cutoff',
LlmClientStreamDuration: 'llm.client.stream.duration',
LlmClientTimeToFirstToken: 'llm.client.time_to_first_token',
} as const
CopilotBillingLongContextCount: "copilot.billing.long_context.count",
CopilotCacheAttempted: "copilot.cache.attempted",
CopilotCacheHit: "copilot.cache.hit",
CopilotCacheWrite: "copilot.cache.write",
CopilotChatBlobBytes: "copilot.chat.blob.bytes",
CopilotChatBlobCount: "copilot.chat.blob.count",
CopilotDegradedCount: "copilot.degraded.count",
CopilotFileReadDuration: "copilot.file.read.duration",
CopilotFileReadSize: "copilot.file.read.size",
CopilotMessagesSerializeDuration: "copilot.messages.serialize.duration",
CopilotOptionsEmittedCount: "copilot.options.emitted.count",
CopilotPromptComponentChars: "copilot.prompt.component.chars",
CopilotRequestCount: "copilot.request.count",
CopilotRequestDuration: "copilot.request.duration",
CopilotToolCalls: "copilot.tool.calls",
CopilotToolDuration: "copilot.tool.duration",
CopilotVfsDelta: "copilot.vfs.delta",
CopilotVfsMaterializeDuration: "copilot.vfs.materialize.duration",
GenAiClientCacheTokenUsage: "gen_ai.client.cache.token.usage",
GenAiClientTokenUsage: "gen_ai.client.token.usage",
LlmClientCompactions: "llm.client.compactions",
LlmClientContextTokens: "llm.client.context_tokens",
LlmClientErrors: "llm.client.errors",
LlmClientOutputCutoff: "llm.client.output_cutoff",
LlmClientStreamDuration: "llm.client.stream.duration",
LlmClientTimeToFirstToken: "llm.client.time_to_first_token",
} as const;

export type MetricKey = keyof typeof Metric
export type MetricValue = (typeof Metric)[MetricKey]
export type MetricKey = keyof typeof Metric;
export type MetricValue = (typeof Metric)[MetricKey];

/** Readonly sorted list of every canonical mothership metric name. */
export const MetricValues: readonly MetricValue[] = [
'copilot.billing.long_context.count',
'copilot.cache.attempted',
'copilot.cache.hit',
'copilot.cache.write',
'copilot.chat.blob.bytes',
'copilot.chat.blob.count',
'copilot.degraded.count',
'copilot.file.read.duration',
'copilot.file.read.size',
'copilot.messages.serialize.duration',
'copilot.options.emitted.count',
'copilot.prompt.component.chars',
'copilot.request.count',
'copilot.request.duration',
'copilot.tool.calls',
'copilot.tool.duration',
'copilot.vfs.delta',
'copilot.vfs.materialize.duration',
'gen_ai.client.cache.token.usage',
'gen_ai.client.token.usage',
'llm.client.compactions',
'llm.client.context_tokens',
'llm.client.errors',
'llm.client.output_cutoff',
'llm.client.stream.duration',
'llm.client.time_to_first_token',
] as const
"copilot.billing.long_context.count",
"copilot.cache.attempted",
"copilot.cache.hit",
"copilot.cache.write",
"copilot.chat.blob.bytes",
"copilot.chat.blob.count",
"copilot.degraded.count",
"copilot.file.read.duration",
"copilot.file.read.size",
"copilot.messages.serialize.duration",
"copilot.options.emitted.count",
"copilot.prompt.component.chars",
"copilot.request.count",
"copilot.request.duration",
"copilot.tool.calls",
"copilot.tool.duration",
"copilot.vfs.delta",
"copilot.vfs.materialize.duration",
"gen_ai.client.cache.token.usage",
"gen_ai.client.token.usage",
"llm.client.compactions",
"llm.client.context_tokens",
"llm.client.errors",
"llm.client.output_cutoff",
"llm.client.stream.duration",
"llm.client.time_to_first_token",
] as const;
Loading
Loading