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
593 changes: 339 additions & 254 deletions apps/sim/lib/mothership/generated/mothership-stream-v1.ts

Large diffs are not rendered by default.

61 changes: 34 additions & 27 deletions apps/sim/lib/mothership/generated/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,47 +271,54 @@ export const ChatPayloadSchema = z
ctx.addIssue({ code: "custom", message: "Assistant context requires Assistant mode" });
});

// The stream fields mothership-stream-v1 payloads share. Each is `?: T | undefined`, so
// `.optional()`; the stream file spreads these shapes into its payload schemas.

/** A pause or terminal acknowledges all preceding activity only after the receiver handles it. */
export interface StreamActivityCheckpoint {
activityReceipt?: StreamActivityReceipt | undefined;
}
export const StreamActivityCheckpointSchema = z.object({
activityReceipt: ActivityReceiptSchema.optional(),
});
export type StreamActivityCheckpoint = z.infer<typeof StreamActivityCheckpointSchema>;

/** Main-assistant text position within this run, measured in UTF-16 code units. */
export interface StreamTextPosition {
textOffset?: number | undefined;
}
export const StreamTextPositionSchema = z.object({
textOffset: z.number().int().nonnegative().safe().optional(),
});
export type StreamTextPosition = z.infer<typeof StreamTextPositionSchema>;

/** Successful completion states the complete main-answer size, including any replayed prefix. */
export interface StreamTextCompletion {
textLength?: number | undefined;
}
export const StreamTextCompletionSchema = z.object({
textLength: z.number().int().nonnegative().safe().optional(),
});
export type StreamTextCompletion = z.infer<typeof StreamTextCompletionSchema>;

/** Replayed tool activity is presentation only; it never authorizes execution or approval. */
export interface StreamToolReplay {
replay?: true | undefined;
/** Requested operation target, authorized independently by Sim. */
workspaceId?: string | undefined;
}
/**
* Replayed tool activity is presentation only; it never authorizes execution or approval.
* `workspaceId` is the requested operation target, authorized independently by Sim.
*/
export const StreamToolReplaySchema = z.object({
replay: z.literal(true).optional(),
workspaceId: z.string().optional(),
});
export type StreamToolReplay = z.infer<typeof StreamToolReplaySchema>;

/**
* How long the worker waits for one handed tool call's result, measured from the hand-off.
* Sim converts it to an absolute deadline on receipt, persists it with the call (a takeover
* re-run inherits what remains) and settles the call, cancellation included, by
* `deadlineMs - TOOL_DEADLINE_SETTLE_MARGIN_MS`: approval wait, pickup, execution and lease
* extensions all fit inside it. Sim policy may only end a call sooner. Absent = the
* receiver's own bounds.
* receiver's own bounds. `waitTimeoutMs`, for a workflow tool (`run_workflow`,
* `run_workflow_until_block`, `run_from_block`, `run_block`) on a Sim that declares
* {@link WORKFLOW_TOOL_WAIT_TIMEOUT_CAPABILITY}: how long Sim waits for the call's run to
* settle once it starts waiting on it. A run still going then keeps going and the call settles
* with a {@link WorkflowToolRunningResult}. Absent = held until the run settles.
*/
export interface StreamToolDeadline {
deadlineMs?: number | undefined;
/**
* For a workflow tool (`run_workflow`, `run_workflow_until_block`, `run_from_block`,
* `run_block`) on a Sim that declares {@link WORKFLOW_TOOL_WAIT_TIMEOUT_CAPABILITY}: how long
* Sim waits for the call's run to settle once it starts waiting on it. A run still going then
* keeps going and the call settles with a {@link WorkflowToolRunningResult}. Absent = held
* until the run settles.
*/
waitTimeoutMs?: number | undefined;
}
export const StreamToolDeadlineSchema = z.object({
deadlineMs: z.number().int().positive().optional(),
waitTimeoutMs: z.number().int().positive().optional(),
});
export type StreamToolDeadline = z.infer<typeof StreamToolDeadlineSchema>;

/**
* Room between Sim settling a call and the worker's own bound for it: Sim's resume-gate
Expand Down
1 change: 1 addition & 0 deletions apps/sim/lib/mothership/observability/metric-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export const Metric = {
CopilotDegradedCount: 'copilot.degraded.count',
CopilotStreamContractDisagreements: 'copilot.stream_contract.disagreements',
CopilotToolCalls: 'copilot.tool.calls',
CopilotToolDuration: 'copilot.tool.duration',
} as const
13 changes: 13 additions & 0 deletions apps/sim/lib/mothership/observability/trace-attribute-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ export const CopilotStopOutcome = {
ValidationError: 'validation_error',
} as const

/**
* Which way the generated stream-contract schema disagrees with Sim's ingest validator on one
* worker frame: `schema_rejects` frames would start failing were the schema the validator.
*/
export const CopilotStreamContractDirection = {
SchemaAccepts: 'schema_accepts',
SchemaRejects: 'schema_rejects',
} as const

/** One {@link CopilotStreamContractDirection} value, as the metric label carries it. */
export type CopilotStreamContractDirectionValue =
(typeof CopilotStreamContractDirection)[keyof typeof CopilotStreamContractDirection]

export const CopilotSurface = {
Copilot: 'copilot',
Mothership: 'mothership',
Expand Down
3 changes: 3 additions & 0 deletions apps/sim/lib/mothership/observability/trace-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ export const TraceAttr = {
CopilotStopContentLength: 'copilot.stop.content_length',
CopilotStopOutcome: 'copilot.stop.outcome',
CopilotStream: 'copilot.stream',
CopilotStreamContractDirection: 'copilot.stream_contract.direction',
CopilotStreamContractEventType: 'copilot.stream_contract.event_type',
CopilotStreamContractVariant: 'copilot.stream_contract.variant',
CopilotSurface: 'copilot.surface',
CopilotTableId: 'copilot.table.id',
CopilotTableOutcome: 'copilot.table.outcome',
Expand Down
20 changes: 20 additions & 0 deletions apps/sim/lib/mothership/request/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { TOOL_CATALOG } from '@/lib/mothership/generated/tool-catalog-v1'
import { Metric } from '@/lib/mothership/observability/metric-names'
import {
type CopilotDegradedReasonValue,
type CopilotStreamContractDirectionValue,
ToolExecutor,
} from '@/lib/mothership/observability/trace-attribute-values'
import { TraceAttr } from '@/lib/mothership/observability/trace-attributes'
Expand All @@ -25,6 +26,7 @@ interface CopilotMeterInstruments {
toolDuration: Histogram
toolCalls: Counter
degradedCount: Counter
streamContractDisagreements: Counter
}

let cached: CopilotMeterInstruments | undefined
Expand All @@ -42,6 +44,7 @@ function instruments(): CopilotMeterInstruments {
}),
toolCalls: meter.createCounter(Metric.CopilotToolCalls),
degradedCount: meter.createCounter(Metric.CopilotDegradedCount),
streamContractDisagreements: meter.createCounter(Metric.CopilotStreamContractDisagreements),
}
return cached
}
Expand Down Expand Up @@ -92,3 +95,20 @@ export function recordDegraded(reason: CopilotDegradedReasonValue): void {
[TraceAttr.CopilotDegradedReason]: reason,
})
}

/**
* Counts one worker frame on which Sim's ingest validator and the generated contract schema
* disagree. `eventType` and `variant` must already be capped to the contract's own
* vocabularies (else "other").
*/
export function recordStreamContractDisagreement(
direction: CopilotStreamContractDirectionValue,
eventType: string,
variant: string
): void {
instruments().streamContractDisagreements.add(1, {
[TraceAttr.CopilotStreamContractDirection]: direction,
[TraceAttr.CopilotStreamContractEventType]: eventType,
[TraceAttr.CopilotStreamContractVariant]: variant,
})
}
Loading