SLOPSHOPPER

wire

The Wire Framework — AI-accelerated data platform delivery for the full lifecycle: requirements, design, development, testing, deployment, enablement

guardcommandprocessnetworktimer
★ 13v4.4.4FSL-1.1-ALv2updated 2026-10-22rittmananalytics/wire-plugin
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · wire
› fix the failing auth test and add an audit log call ⏺ Read(src/auth.ts) ⎿ Read 6 lines ⏺ Update(src/auth.ts) ⎿ Added 2 lines, removed 1 line ⏺ Bash(bun test) ⎿ 3 pass, 1 fail ● Done. refresh now rejects expired claims and logs an audit event. ✻ Worked for 42s · done 4:20 PM › /wire-usage ⎿ wire: No Wire commands have run in this session yet. ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts
README

<img src="docs/images/wire_logo_transparent.png" alt="Wire Framework" width="220">

Wire Framework v4.4.4

Wire is a structured delivery system for data platform engagements, built on top of Claude Code and Gemini CLI. It encodes analytics engineering methodology as workflow specifications that the AI reads before generating anything so that output follows consistent patterns, traces back to requirements and can be validated automatically rather than having to be manually eyeballed.

Instead of prompting an AI to write a dbt model and hoping it follows your conventions, you run /wire:dbt-generate and the AI receives a specification that tells it exactly which upstream design decisions to read, which naming patterns to apply, which tests to include, and how to update the project status tracker when it's done.

Full documentation at docs.rittmananalytics.com


The Problem It Solves

AI code generation can produce syntactically valid SQL. Where it falls down is methodology: consistent naming conventions across 15+ models, correct surrogate key patterns, relationship test coverage on every foreign key, traceability from business requirement to warehouse column. These failures are not knowledge failures, as the models typically do know the conventions. They are, however, context and control failures as without a structured methodology constraining generation, LLMs improvise and the accumulated inconsistencies across a project erode the value of using AI at all.

Wire closes this gap by encoding the methodology as workflow specifications that the AI reads before generating anything. Each specification tells the AI which upstream artifacts to read, which templates to follow, which validation checks to apply, and how to update the project state tracker. The result is of typically of an equivalent level of quality of a senior analytics engineer who has been on the project for months, because it was generated by an AI that read every design decision and requirement that a senior analytics engineer would have absorbed.

Wire does not replace consultants or developers. It gives them an AI that works quickly and consistently, freeing them to focus on client relationships, design decisions and the judgement calls that automation cannot make.


Key Features

  • Direct the work, don't memorise the commands (v4.0.0) — on Claude Code, say what you want done and Wire works out which command that is from the release type's own definition, runs it, reports what it did in plain words with the command named in the closing line, and stops where a decision is yours. Every step runs the real command, so status.md, the execution log, the precondition gate and the artifacts on disk are identical to typing it. Reviews are never run without your ruling; parked decisions are listed at the start of every session; and orchestration.mode: manual in the engagement context restores the pre-4.0 behaviour exactly. Three tiers: one human directs, one session orchestrates, and the specialist agents run as flat lanes with their own state files
  • 361 slash commands covering the full delivery lifecycle: Discovery (Shape Up + RA Canonical SOP), Requirements, Design, Development, Testing, Deployment, Enablement, Platform Migration, Agentic Data Stack
  • 12 release types matching common engagement shapes: Shape Up discovery, SOP / Canonical discovery (sponsor-facing Findings Playback), full platform builds, pipeline-only, dbt development, dashboard extensions, dashboard-first rapid dev, enablement, platform migration (BigQuery ↔ Snowflake), agentic data stack (governed self-service analytics with eval suite), droughty (schema introspection and base-layer generation from live warehouses), and custom (bespoke deliverables defined from SoW documents) — every release type is now backed by a machine-readable process definition (see Precondition Gate below), not just documentation
  • Two-tier engagement structure separating long-running client context from individual scoped releases
  • Generate / validate / review lifecycle for every artifact: structured generation, automated checks, stakeholder sign-off
  • Precondition gate (v4.0.0) — every command blocks by default on an unmet prerequisite; overriding requires a recorded name and reason, so skipping a step on purpose is always a visible, attributable decision rather than something that silently happened
  • Process and data model registries (v4.0.0) — release-type sequencing and command specs are sourced from a private, branch-protected wire-process-registry; an optional, automatically-detected canonical data model registry (wire-data-model-registry) proposes industry-standard entity structures without ever bundling proprietary content into this public plugin — see docs.rittmananalytics.com/en/latest/docs/advanced/registries
  • Business rules discovery (v4.0.0) — an optional first step on any development release that establishes what a metric actually means before design starts. One register per business domain, holding every competing definition with the file it came from, what they disagree on, the decision and who approved it. A rule nobody has decided is recorded as unknown rather than left out, and each rule with a legacy definition generates a reconciliation query that runs immediately rather than surfacing as a mismatch in testing
  • Modality models as a design input (v4.0.0) — where a client already models their data in Modality, /wire:utils-modality-link points the release at it and the conceptual model, logical model and pipeline design read entities, sources and cardinality from the existing .mml files rather than deriving them. The requirements are still read, and the difference between the two is raised as a finding rather than resolved silently
  • Modelling-led discovery (v4.0.0) — sop_discovery now offers two routes through the same three pillars. diagnostic is the canonical playbook; modelling_led replaces the three analyses with a current-state appraisal and a signed-off conceptual and logical model, and produces the roadmap before the playback because it is one of the five things the sponsor signs off. Release types can now declare profiles that enable or disable phases and override a gate, so the ordering is enforced rather than requested
  • Logical model (v4.0.0) — the step between the conceptual and the physical model that Wire previously skipped: keys, cardinality, identity resolution with attributed precedence, normalisation, and attribution rules with their remainder handling. Those decisions were being made implicitly inside data_model-generate and arriving already expressed as dbt models. Optional in full_platform, standard in a modelling-led discovery
  • Plain Language by default: the plugin ships a Plain Language output style that activates automatically while Wire is enabled, so every response is written in simple, concise, jargon-free English. Generated artifacts are unaffected (they follow their own templates and the reference-legibility convention); override per project in .claude/settings.local.json or by editing the style
  • Status reconciliation (/wire:status-sync) for work done outside command runs: diffs recorded state against git history, files on disk, and the execution log, then repairs status files, sprint-plan story states, and session history with consultant confirmation
  • 27 ad-hoc development skills that activate automatically during coding work (dbt, LookML, Dagster, Python, Fivetran, Airbyte, Coupler.io, RudderStack, Segment, Looker, Snowflake, Hightouch, BigQuery, Cloud Run, gcloud) without any explicit invocation, plus 26 Amplitude product-analytics skills for working with an Amplitude instance
  • Wire Agents — 13 specialist subagents (dbt developer, semantic layer developer, pipeline engineer, migration specialist, and 9 others) dispatched automatically on every generate and validate command. /wire:delegate computes a full parallel/sequential execution plan across all pending work, with fan-out parallelism for large model sets (layers stay sequential; agents within each layer run in parallel). Under the director model they run as lanes: own tree, own state file rewritten after each completed item, and no writes to status.md — the orchestrating session is the single writer of the record
  • Release claim and parked decisions (v4.0.0) — a release records who is driving it, so a second session offers to join as reviewer or take over after a 30-minute stall rather than dispatching into work someone else is running. Decisions waiting on you are a list in status.md, reported first thing every session
  • Attribution (v4.0.0) — execution-log rows carry By and Session (typed, orchestrator, a lane label, or autopilot), and telemetry carries the same as invoked_by. Older four-column log rows stay valid and are never rewritten
  • Autopilot mode for autonomous end-to-end delivery
  • Jira and Linear integration for issue tracking synced to the artifact lifecycle
  • Confluence and Notion integration for client-facing document review
  • Fathom integration for surfacing relevant meeting transcript context during reviews
  • Runs on Claude Code (Anthropic) and Gemini CLI (Google)

Claude Code, Gemini, Plugins, Skills, and MCP Servers

Wire is distributed as a Claude Code plugin and a Gemini CLI extension. Installing the plugin embeds every Wire command inline — no framework files need to exist in your project repository.

Plugins provide the 261 /wire:* commands. Each command file contains its full workflow specification, so the AI receives complete instructions as context at invocation time.

Skills sit alongside commands but work differently. They activate automatically during ad-hoc coding work without any explicit invocation. When you start writing a dbt model, the dbt development skill provides naming conventions, SQL style rules, and testing patterns as background context. The following skills are included:

SkillActivates when…
dbt-developmentWriting dbt models, tests, or documentation
dbt-migrationMigrating dbt projects across platforms
dbt-fusionResolving dbt Core to Fusion migration errors
dbt-mcp-serverConfiguring the dbt MCP server
dbt-analytics-qaAnswering business questions from dbt data
dbt-dagGenerating lineage diagrams
dbt-unit-testingWriting dbt unit tests
dbt-semantic-layerWorking with the dbt Semantic Layer
dbt-troubleshootingDiagnosing dbt errors
lookml-content-authoringWriting LookML views, explores, and dashboards
looker-dashboard-mockupGenerating HTML dashboard mockups
dagsterWriting Dagster asset definitions and pipelines
dignified-pythonWriting production-quality Python
fivetranConfiguring Fivetran connectors via MCP
airbyteManaging Airbyte connections and ingestion via the Airbyte Agent MCP server
coupler-ioManaging Coupler.io dataflows (ingestion and reverse ETL) via MCP
rudderstackManaging RudderStack sources, destinations, and tracking plans via MCP
segmentWorking with Twilio Segment sources, destinations, and tracking plans
snowflake-developmentWriting queries, designing objects, auditing, and migrating Snowflake via MCP
hightouchAuditing and migrating Hightouch reverse ETL syncs via the Hightouch REST API
bigquery-basicsManaging BigQuery datasets, tables, jobs, SQL, and BigQuery ML
cloud-run-basicsDeploying Cloud Run services, jobs, and worker pools for pipelines
gcloudRunning gcloud CLI commands safely, with validation and a safety denylist
google-cloud-recipe-authAuthenticating to Google Cloud (ADC, service identities, secure access)
google-cloud-waf-cost-optimizationCost-optimization review against the Google Cloud Well-Architected Framework
google-cloud-waf-securitySecurity-posture review against the Google Cloud Well-Architected Framework
researchConducting technical research (findings auto-saved to .wire/research/)

MCP servers connect Wire to external systems. Configure them once and all commands that need them use them automatically:

MCP ServerPurpose
AtlassianJira issue tracking and Confluence document store
LinearLinear issue tracking
FathomMeeting transcript search during reviews
NotionNotion document store
Context7Up-to-date library documentation
FivetranCreate, configure, and monitor Fivetran connectors and destinations
AirbyteAI agent connector queries via the Airbyte Agent MCP server
Coupler.ioDataflow management, dataset inspection, and reverse ETL
RudderStackEvent tracking, tracking plans, and data catalog management
SnowflakeDirect SQL execution against Snowflake via the Snowflake MCP server
AmplitudeProduct analytics — charts, dashboards, experiments, session replay, instrumentation, and taxonomy

Amplitude product analytics skills. Wire bundles the official Amplitude AI skills (MIT licence) for administering and working with an Amplitude instance through the Amplitude MCP server. They activate automatically when relevant and cover seven areas:

AreaSkills
Core analyticscreate-chart, create-dashboard, analyze-chart, analyze-dashboard
Product insightsanalyze-experiment, monitor-experiments, analyze-feedback, analyze-account-health, discover-opportunities, compare-user-journeys
Session replay & debuggingdebug-replay, replay-ux-audit, diagnose-errors, monitor-reliability
AI agent analyticsanalyze-ai-topics, investigate-ai-session, monitor-ai-quality, review-agent-insights
Instrumentationdiff-intake, discover-event-surfaces, discover-analytics-patterns, instrument-events, add-analytics-instrumentation, taxonomy
Briefingsdaily-brief, weekly-brief

A typical instrumentation flow chains diff-intake → discover-event-surfaces → instrument-events, with discover-analytics-patterns keeping new tracking consistent with existing conventions. The taxonomy skill aligns naturally with Wire's existing CDP work (Segment, RudderStack).


Getting Started

Prerequisites

Installing the Claude Code Plugin

/plugin marketplace add rittmananalytics/wire-plugin
/plugin install wire@rittman-analytics
/reload-plugins

The /reload-plugins step activates the plugin in the current session — no Claude Code restart needed. All /wire:* commands are then available.

Installing the Gemini CLI Extension

gemini extensions install https://github.com/rittmananalytics/wire-extension

Commands are available as /wire * with spaces rather than colons.

Configuring MCP Servers

MCP servers are optional but enable issue tracking, document store sync, and meeting transcript context. Add whichever you need:

# Atlassian (Jira + Confluence)
claude mcp add --transport http atlassian https://mcp.atlassian.com/v1/mcp

# Linear
claude mcp add --transport http linear https://mcp.linear.app/sse

# Fathom (meeting transcripts — requires a self-hosted or managed Fathom MCP server)
claude mcp add --transport http fathom https://your-fathom-mcp-server/mcp

# Notion
claude mcp add --transport http notion https://mcp.notion.com/mcp

# Fivetran (requires API key and secret from Fivetran dashboard → Account → Settings → API Config)
claude mcp add --transport http fivetran https://fivetran-mcp-server-r6jhgfswwa-nw.a.run.app/mcp \
  -H "X-Fivetran-API-Key: YOUR_API_KEY" \
  -H "X-Fivetran-API-Secret: YOUR_API_SECRET"

# Airbyte Agent MCP (OAuth — browser sign-in on first connect)
claude mcp add --transport http airbyte-agent https://mcp.airbyte.ai/mcp

# Coupler.io (Personal Access Token from Coupler.io app → Settings → MCP)
claude mcp add --transport http coupler-io https://mcp.coupler.io/mcp/ \
  -H "Authorization: Bearer YOUR_COUPLER_TOKEN"

# RudderStack (OAuth via mcp-remote — requires Node.js / npx on PATH)
claude mcp add rudderstack --command "npx -y mcp-remote https://mcp.rudderstack.com/mcp"

# Snowflake (available via Claude.ai native connector, or self-hosted — see USER_GUIDE §MCP Tunnels)
claude mcp add snowflake --command "npx -y mcp-remote https://mcp.snowflake.com/mcp"

Run /wire:mcp at any time to check connection status, update endpoints, or force re-authentication.

Starting Your First Engagement

/wire:new

Wire asks for a client name, engagement type, first release type, and an optional Statement of Work path. It creates the .wire/ folder structure and, if you chose a discovery release, begins the scoping workflow.


How It Works

The Engagement and Release Structure

Every Wire engagement uses a two-tier layout in .wire/:

.wire/
  engagement/
    context.md        # client objectives, stakeholders, current-state architecture
    sow.md            # Statement of Work (copied at setup)
    calls/            # meeting notes and call transcripts
  releases/
    01-discovery/     # problem definition, pitch, release brief, sprint plan
    02-pipeline/      # data pipeline and dbt transformation
    03-dashboards/    # client-facing reporting layer
  research/
    sessions/         # technical research findings (auto-saved by the research skill)

The engagement folder holds everything that spans the whole client relationship. Releases are scoped, time-boxed units of delivery, each with its own status.md tracking file and execution_log.md recording every command run against it.

Two ways to run it

Direct it. Say what you want done — "new engagement from this SOW", "run what's next", "approve it and carry on" — and Wire computes what is runnable from the release type's definition, runs it, tells you what it did (naming the command in the closing line), and stops at every review gate for your decision. This is the default on Claude Code from v4.0.0.

Type it. Every command still works exactly as before, and the command name is printed before each directed run so you learn them as you go. Set orchestration.mode: manual in .wire/engagement/context.md for a whole engagement, or say "you drive" for one session. Gemini CLI stays command-driven throughout.

Either way the same command files run and the record on disk is identical.

The Generate / Validate / Review Cycle

Every artifact follows the same three-step lifecycle:

Generate reads upstream artifacts (requirements, design decisions, prior models), applies Wire methodology templates, and produces the artifact. Output is written to the release folder and the status tracker is updated.

Validate runs automated checks against the generated artifact. For a dbt model this covers naming convention compliance, test coverage, and relationship validation. For a requirements document it checks completeness against the SOW. The result is a structured PASS/FAIL report with specific issues identified.

Review presents the artifact for stakeholder sign-off. Wire surfaces relevant meeting transcript context from Fathom, document store comments from Confluence or Notion, and any prior reviewer feedback. The reviewer approves, requests changes, or rejects. Approval gates the next phase.

Each command has a matching validate and review counterpart: /wire:requirements-generate, /wire:requirements-validate, /wire:requirements-review.

Release Types

Typerelease_typeScopeTypical duration
Discovery (Shape Up)discoveryProblem definition, pitch, release brief, sprint plan1–2 weeks
Discovery (SOP / Canonical)sop_discoveryTwo profiles. diagnostic: stakeholder interviews, three analyses, sponsor Findings Playback. modelling_led: current-state appraisal plus a signed-off conceptual and logical model in place of the analyses, with the roadmap signed off at the playback3–6 weeks
Full Platformfull_platformPipeline through dbt, semantic layer, and dashboards2–3 weeks
Dashboard-Firstdashboard_firstVisual mocks drive the data model; seed data enables early dbt work1–2 weeks
Pipeline + dbtpipeline_onlyNew data pipeline and transformation layer1–2 weeks
dbt Developmentdbt_developmentAnalytics engineering on existing infrastructure1 week
Dashboard Extensiondashboard_extensionNew dashboards on an existing semantic layer3–5 days
EnablementenablementTraining and documentation for an existing platform2–3 days
Agentic Data Stackagentic_data_stackOverlay for an existing data platform (warehouse + dbt + BI tool) — audits governance maturity, extends the semantic layer, generates per-domain knowledge skills and a CI-wired eval suite, delivers an installable agentic data stack skill. Requires an existing dbt project; not a platform build.4–6 weeks
Platform Migrationplatform_migrationWarehouse-to-warehouse migration (BigQuery ↔ Snowflake) with source audits, batched dbt translation, equivalency validation and a gated cutover. Also covers tenant carve-outs.6–12 weeks
DroughtydroughtySchema introspection against a live warehouse: entity-relationship diagrams, field documentation, data-quality reporting, and base LookML or dbt test generation. Standalone, or an optional phase inside another release.2–5 days
CustomcustomBespoke deliverables derived from SoW — Wire generates project-scoped specsVaries

Walkthrough: A Pipeline + dbt Release

The following shows a typical command sequence for delivering a new data pipeline and dbt transformation layer.

1. Create the engagement and release

/wire:new

Select pipeline_only as the release type. Wire creates .wire/engagement/ and .wire/releases/01-pipeline/.

2. Begin work — context loads automatically

The engagement-context skill fires on your first message, reads the release status, surfaces any prior research, and outputs a brief context summary. No session command needed. Use /wire:plan for an optional structured planning ritual.

3. Extract requirements

/wire:requirements-generate

Wire reads the SOW and any call transcripts in engagement/calls/ and produces a structured requirements specification. Run /wire:requirements-validate to check it, then /wire:requirements-review for sign-off.

4. Design the pipeline architecture

/wire:pipeline_design-generate

Produces a pipeline architecture document covering source systems, replication strategy, and data flow. Validate and review as above before proceeding.

5. Generate the pipeline

/wire:pipeline-generate

For a Fivetran engagement, this configures connectors via the Fivetran MCP server and produces a pipeline_connections.md record. For a Python pipeline it generates the pipeline code.

6. Generate dbt models

/wire:dbt-generate

Wire reads the pipeline design and requirements and generates staging, integration, and warehouse dbt models following the three-layer naming convention, with tests and documentation.

7. Archive and complete

/wire:archive

Archives the completed release. Status and execution log are updated automatically throughout — no session-close command required.


Autopilot Mode

Autopilot runs the full delivery lifecycle without step-by-step prompting.

/wire:au
Source 4 files
hooks/register.ts 555 lines
1// The Wire mod, 4.1.0: telemetry and execution-log metrics as Claude Code
2// function hooks (wire#270, release 1, sections D3 and D4). Replaces the
3// UserPromptExpansion hook (wire-telemetry.sh) and the Stop hook
4// (wire-metrics.sh + wire_metrics.py).
5//
6// - Telemetry: one `wire_command` event per Wire command, from the event that
7//   ran it, so `invoked_by` comes from the engine (typed, orchestrator, lane,
8//   autopilot, or studio for sessions Wire Studio opened), not from a scraped
9//   prompt. Sent after the hook returns, so it never delays a command.
10// - Metrics: each Wire command opens a run; every model request in its loop
11//   adds its measured usage; when the loop's turn ends the run closes and the
12//   mod fills that command's Duration / Tokens / Cost cells in
13//   execution_log.md, or in a ticket's own run log
14//   (iterations/<ticket>.execution_log.md, 4.2.0) when the command ran on a
15//   ticket branch. A row an orchestrator writes later is filled when it is
16//   written.
17// - /wire-usage prints this session's runs, with no model turn.
18// - /wire-studio start|restart|stop|status runs Wire Studio for the session's
19//   repository as a detached local process (one per repository, ports
20//   4800-4820), with no model turn.
21//
22// Off switches: the plugin's `telemetry` and `metrics` options, or
23// WIRE_TELEMETRY=false / WIRE_METRICS=false. The mod refuses nothing and
24// rewrites no tool call.
25
26import type { Register } from 'claude-code'
27
28import type { WireIdentity, WireRun } from '../types'
29import { SEGMENT_IDENTIFY, SEGMENT_TRACK, SEGMENT_WRITE_KEY, WIRE_VERSION } from './wire/constants'
30import {
31  addUsage,
32  backfillLog,
33  computeCost,
34  emptyUsage,
35  identifyBody,
36  invokedBy,
37  personaOf,
38  isOn,
39  logCommandOf,
40  logStamp,
41  parseStudioArgs,
42  parseStudioRecord,
43  releaseOf,
44  STUDIO_PORT_FIRST,
45  STUDIO_PORT_LAST,
46  studioStateName,
47  type StudioRecord,
48  totalTokens,
49  trackBody,
50  usageReport,
51} from './wire/logic'
52
53const RUNS = { plugin: 'wire', key: 'runs' } as const
54const AUTOPILOT = { plugin: 'wire', key: 'autopilot' } as const
55const IDENTITY = { plugin: 'wire', key: 'identity' } as const
56const STEP = { plugin: 'wire', key: 'stepUsage' } as const
57const MAX_RUNS = 200
58const LOG_WINDOW_MS = 30 * 60_000
59
60type Options = { telemetry?: boolean; metrics?: boolean }
61
62async function runsOf($: any): Promise<WireRun[]> {
63  const { value } = await $.state.get(RUNS)
64  return Array.isArray(value) ? value : []
65}
66
67async function saveRuns($: any, runs: WireRun[]): Promise<void> {
68  await $.state.set(RUNS, runs.slice(-MAX_RUNS))
69}
70
71async function run1($: any, argv: string[], cwd: string): Promise<string> {
72  try {
73    const r = await $.process.run(argv, { cwd, timeoutMs: 3000 })
74    return r.exitCode === 0 ? String(r.stdout ?? '').trim() : ''
75  } catch {
76    return ''
77  }
78}
79
80async function identityOf($: any): Promise<{ identity: WireIdentity; isNew: boolean }> {
81  const { value } = await $.state.get(IDENTITY)
82  if (value) {
83    return { identity: value, isNew: false }
84  }
85  const cwd = await $.session.cwd()
86  const home = (await $.env.get('HOME')) ?? ''
87  const idFile = `${home}/.wire/telemetry_id`
88  let userId = ''
89  let isNew = false
90  try {
91    userId = String(await $.fs.read(idFile)).trim()
92  } catch {
93    userId = ''
94  }
95  if (!userId) {
96    userId = crypto.randomUUID()
97    isNew = true
98    try {
99      await $.fs.write(idFile, userId)
100    } catch {
101      // No home folder to write to: the id lasts this session only.
102    }
103  }
104  const identity: WireIdentity = {
105    userId,
106    username: (await $.env.get('USER')) ?? (await $.env.get('USERNAME')) ?? '',
107    hostname: await run1($, ['hostname'], cwd),
108    os: await run1($, ['uname', '-s'], cwd),
109    gitRepo: (await run1($, ['git', 'config', '--get', 'remote.origin.url'], cwd)) || 'unknown',
110    gitBranch: (await run1($, ['git', 'rev-parse', '--abbrev-ref', 'HEAD'], cwd)) || 'unknown',
111  }
112  await $.state.set(IDENTITY, identity)
113  return { identity, isNew }
114}
115
116async function post($: any, url: string, body: string): Promise<void> {
117  try {
118    await $.http.fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body })
119  } catch {
120    // Telemetry never fails or blocks a command.
121  }
122}
123
124async function sendTrack($: any, run: WireRun): Promise<void> {
125  const { identity, isNew } = await identityOf($)
126  const timestamp = new Date(run.startedAt).toISOString().replace(/\.\d{3}Z$/, 'Z')
127  if (isNew) {
128    await post($, SEGMENT_IDENTIFY, identifyBody(SEGMENT_WRITE_KEY, identity.userId, {
129      username: identity.username, hostname: identity.hostname, os: identity.os, version: WIRE_VERSION, timestamp,
130    }))
131  }
132  let sessionId: string | null = null
133  try {
134    sessionId = await $.session.id()
135  } catch {
136    sessionId = null
137  }
138  await post($, SEGMENT_TRACK, trackBody(SEGMENT_WRITE_KEY, identity.userId, {
139    command: run.command.replace(/^\/wire:/, ''),
140    invokedBy: run.invokedBy,
141    persona: run.persona ?? '',
142    timestamp,
143    release: run.release,
144    sessionId,
145    agentId: run.agentId,
146    gitRepo: identity.gitRepo,
147    gitBranch: identity.gitBranch,
148    username: identity.username,
149    hostname: identity.hostname,
150    os: identity.os,
151    version: WIRE_VERSION,
152  }))
153}
154
155async function startRun($: any, options: Options, run: WireRun): Promise<void> {
156  if (isOn(options.metrics, await $.env.get('WIRE_METRICS'))) {
157    await saveRuns($, [...(await runsOf($)), run])
158  }
159  if (isOn(options.telemetry, await $.env.get('WIRE_TELEMETRY'))) {
160    $.clock.after(0, () => {
161      void sendTrack($, run)
162    })
163  }
164}
165
166// Usage goes to the run's own stepUsage member, never into `runs`: a request
167// whose hook finishes after the turn has closed the run cannot overwrite it.
168// A request made while no Wire run is open in its loop (an orchestrator
169// planning before its first Skill call) is kept under `loose:<turnId>`, so the
170// turn-end settlement does not charge it to a command.
171async function addStepUsage($: any, agentId: string | null, turnId: string, usage: any): Promise<void> {
172  if (!usage) {
173    return
174  }
175  const runs = await runsOf($)
176  const run = [...runs].reverse().find(r => r.endedAt === null && r.agentId === agentId)
177  const id = run ? run.id : `loose:${turnId}`
178  const { value } = await $.state.get({ ...STEP, id })
179  const held = value ?? { usage: emptyUsage(), model: null }
180  await $.state.set({ ...STEP, id }, {
181    usage: addUsage(held.usage, usage),
182    model: typeof usage.model === 'string' ? usage.model : held.model,
183  })
184}
185
186// The loop's turn ended. Its open runs close with their recorded usage; the
187// turn's own total (turn.complete's usage, every request of the turn) settles
188// what the per-request records have not caught yet, such as the last request,
189// whose hook may finish after this one: the remainder goes to the newest run.
190async function closeRuns($: any, agentId: string | null, turnId: string, turnUsage: any): Promise<void> {
191  const now = await $.clock.now()
192  const runs = await runsOf($)
193  const open = runs.map((r, i) => ({ r, i })).filter(({ r }) => r.endedAt === null && r.agentId === agentId)
194  if (open.length === 0) {
195    return
196  }
197  const recorded = []
198  for (const { r } of open) {
199    const { value } = await $.state.get({ ...STEP, id: r.id })
200    recorded.push(value ?? { usage: emptyUsage(), model: null })
201  }
202  const { value: loose } = await $.state.get({ ...STEP, id: `loose:${turnId}` })
203  const counted = recorded.reduce((sum, acc) => addUsage(sum, acc.usage), addUsage(emptyUsage(), loose?.usage))
204  const total = turnUsage ? addUsage(emptyUsage(), turnUsage) : counted
205  const remainder = {
206    input_tokens: Math.max(0, total.input_tokens - counted.input_tokens),
207    output_tokens: Math.max(0, total.output_tokens - counted.output_tokens),
208    cache_creation_input_tokens: Math.max(0, total.cache_creation_input_tokens - counted.cache_creation_input_tokens),
209    cache_read_input_tokens: Math.max(0, total.cache_read_input_tokens - counted.cache_read_input_tokens),
210  }
211  open.forEach(({ r, i }, k) => {
212    const isNewest = k === open.length - 1
213    const usage = isNewest ? addUsage(recorded[k]?.usage ?? emptyUsage(), remainder) : recorded[k]?.usage ?? emptyUsage()
214    const model = recorded[k]?.model ?? (typeof turnUsage?.model === 'string' ? turnUsage.model : r.model)
215    runs[i] = { ...r, endedAt: now, usage, model }
216  })
217  await saveRuns($, runs)
218}
219
220// The logs a run's row may be in, newest first: the release's execution log
221// and, in a release built from tickets, each ticket's own run log
222// (iterations/<ticket>.execution_log.md, specs/utils/ticket_delivery.md).
223async function logsFor($: any, cwd: string, release: string | null): Promise<string[]> {
224  const found: { path: string; mtime: number }[] = []
225  const now = await $.clock.now()
226  const add = async (path: string, windowed: boolean) => {
227    try {
228      const st = await $.fs.stat(path)
229      if (!windowed || now - st.mtimeMs <= LOG_WINDOW_MS) {
230        found.push({ path, mtime: st.mtimeMs })
231      }
232    } catch {
233      // no such log
234    }
235  }
236  const addRelease = async (dir: string, windowed: boolean) => {
237    await add(`${dir}/execution_log.md`, windowed)
238    try {
239      for (const f of await $.fs.list(`${dir}/iterations`)) {
240        if (f.kind === 'file' && f.name.endsWith('.execution_log.md')) {
241          await add(`${dir}/iterations/${f.name}`, windowed)
242        }
243      }
244    } catch {
245      // no ticket run logs in this release
246    }
247  }
248  const named = release ? `${cwd}/.wire/releases/${release}` : null
249  let namedExists = false
250  if (named) {
251    try {
252      namedExists = await $.fs.exists(`${named}/execution_log.md`) || await $.fs.exists(`${named}/iterations`)
253    } catch {
254      namedExists = false
255    }
256  }
257  if (named && namedExists) {
258    await addRelease(named, false)
259  } else {
260    try {
261      for (const dir of await $.fs.list(`${cwd}/.wire/releases`)) {
262        if (dir.kind === 'dir') {
263          await addRelease(`${cwd}/.wire/releases/${dir.name}`, true)
264        }
265      }
266    } catch {
267      return []
268    }
269  }
270  return found.sort((a, b) => b.mtime - a.mtime).map(f => f.path)
271}
272
273async function backfillPending($: any, options: Options): Promise<void> {
274  if (!isOn(options.metrics, await $.env.get('WIRE_METRICS'))) {
275    return
276  }
277  const runs = await runsOf($)
278  const cwd = await $.session.cwd()
279  let changed = false
280  for (let i = 0; i < runs.length; i++) {
281    const r = runs[i]
282    if (!r || r.filled || r.endedAt === null || totalTokens(r.usage) === 0) {
283      continue
284    }
285    for (const path of await logsFor($, cwd, r.release)) {
286      let text: string
287      try {
288        text = String(await $.fs.read(path))
289      } catch {
290        continue
291      }
292      const updated = backfillLog(text, {
293        command: r.command,
294        usage: r.usage,
295        model: r.model,
296        durationSeconds: Math.round((r.endedAt - r.startedAt) / 1000),
297        notBefore: logStamp(r.startedAt, 24 * 60).slice(0, 10),
298      })
299      if (updated !== null && updated !== text) {
300        await $.fs.write(path, updated)
301        runs[i] = { ...r, filled: true }
302        changed = true
303        break
304      }
305    }
306  }
307  if (changed) {
308    await saveRuns($, runs)
309  }
310}
311
312async function readText($: any, path: string): Promise<string | null> {
313  try {
314    return String(await $.fs.read(path))
315  } catch {
316    return null
317  }
318}
319
320async function personaFor($: any, args: string): Promise<string> {
321  const cwd = await $.session.cwd()
322  const fileText = await readText($, `${cwd}/.wire/local/persona.yml`)
323  const rosterText = await readText($, `${cwd}/.wire/engagement/team.yml`)
324  const email = rosterText ? await run1($, ['git', 'config', '--get', 'user.email'], cwd) : ''
325  return personaOf(args, await $.env.get('WIRE_PERSONA'), fileText, rosterText, email)
326}
327
328async function newRun($: any, id: string, command: string, args: string, agentId: string | null, by: WireRun['invokedBy']): Promise<WireRun> {
329  return {
330    id, command, release: releaseOf(args), agentId, invokedBy: by, persona: await personaFor($, args),
331    startedAt: await $.clock.now(), endedAt: null, usage: emptyUsage(), model: null, filled: false,
332  }
333}
334
335async function studioScript($: any): Promise<string | null> {
336  // Built plugin: studio/ at the plugin root. Source tree: wire/studio/.
337  for (const path of [`${$.plugin.root}/studio/studio.py`, `${$.plugin.root}/../../studio/studio.py`]) {
338    try {
339      if (await $.fs.exists(path)) {
340        return path
341      }
342    } catch {
343      // try the next place
344    }
345  }
346  return null
347}
348
349async function exitsZero($: any, argv: string[]): Promise<boolean> {
350  try {
351    return (await $.process.run(argv, { timeoutMs: 3000 })).exitCode === 0
352  } catch {
353    return false
354  }
355}
356
357async function isServing($: any, port: number): Promise<boolean> {
358  try {
359    return (await $.http.fetch(`http://127.0.0.1:${port}/api/summary`)).ok === true
360  } catch {
361    return false
362  }
363}
364
365async function studioPaths($: any, cwd: string): Promise<{ dir: string; state: string; log: string }> {
366  const home = (await $.env.get('HOME')) ?? ''
367  const dir = `${home}/.wire/studio`
368  const name = studioStateName(cwd)
369  return { dir, state: `${dir}/${name}`, log: `${dir}/${name.replace(/\.json$/, '.log')}` }
370}
371
372async function readStudio($: any, statePath: string): Promise<StudioRecord | null> {
373  try {
374    return parseStudioRecord(String(await $.fs.read(statePath)))
375  } catch {
376    return null
377  }
378}
379
380async function studioUp($: any, rec: StudioRecord | null): Promise<boolean> {
381  return !!rec && (await exitsZero($, ['kill', '-0', String(rec.pid)])) && (await isServing($, rec.port))
382}
383
384async function freePort($: any, wanted: number | null): Promise<number | null> {
385  const ports = wanted ? [wanted] : Array.from({ length: STUDIO_PORT_LAST - STUDIO_PORT_FIRST + 1 }, (_, i) => STUDIO_PORT_FIRST + i)
386  for (const port of ports) {
387    const listening = await exitsZero($, ['lsof', '-nP', `-iTCP:${port}`, '-sTCP:LISTEN'])
388    if (!listening && !(await isServing($, port))) {
389      return port
390    }
391  }
392  return null
393}
394
395async function startStudio($: any, cwd: string, wanted: number | null): Promise<string> {
396  const paths = await studioPaths($, cwd)
397  const running = await readStudio($, paths.state)
398  if (await studioUp($, running)) {
399    return `Wire Studio is already running for this repository: http://127.0.0.1:${running?.port}/`
400  }
401  if (!(await $.fs.exists(`${cwd}/.wire`))) {
402    return `No .wire/ folder in ${cwd}. Start Claude Code in a Wire engagement repository, then run /wire-studio.`
403  }
404  const script = await studioScript($)
405  if (!script) {
406    return 'Wire Studio is not in this copy of the plugin (studio/studio.py). It ships from Wire 4.1.0.'
407  }
408  const port = await freePort($, wanted)
409  if (!port) {
410    return wanted
411      ? `Port ${wanted} is in use. Run /wire-studio start --port <another port>.`
412      : `Ports ${STUDIO_PORT_FIRST}-${STUDIO_PORT_LAST} are all in use. Run /wire-studio start --port <n>.`
413  }
414  await $.process.run(['mkdir', '-p', paths.dir], { timeoutMs: 3000 })
415  // Arguments reach the shell as positional parameters, never spliced into the script.
416  const spawned = await $.process.run([
417    'sh', '-c', 'nohup python3 "$1" --repo "$2" --port "$3" --no-browser >"$4" 2>&1 & echo $!',
418    'sh', script, cwd, String(port), paths.log,
419  ], { timeoutMs: 5000 })
420  const pid = Number(String(spawned.stdout ?? '').trim())
421  if (!Number.isInteger(pid) || pid <= 0) {
422    return `Wire Studio did not start: ${String(spawned.stderr ?? '').trim() || 'no process id returned'}.`
423  }
424  const rec: StudioRecord = { pid, port, repo: cwd, startedAt: new Date(await $.clock.now()).toISOString() }
425  await $.fs.write(paths.state, JSON.stringify(rec))
426  for (let i = 0; i < 16 && !(await isServing($, port)); i++) {
427    await $.clock.sleep(250)
428  }
429  const url = `http://127.0.0.1:${port}/`
430  if (!(await isServing($, port))) {
431    if (!(await exitsZero($, ['kill', '-0', String(pid)]))) {
432      let tail = ''
433      try {
434        tail = String(await $.fs.read(paths.log)).trim().split('\n').slice(-3).join(' ')
435      } catch {
436        tail = ''
437      }
438      return `Wire Studio stopped as it started.${tail ? ` ${tail}` : ''} Log: ${paths.log}`
439    }
440    return `Wire Studio is starting at ${url} (still loading). Log: ${paths.log}`
441  }
442  const os = await run1($, ['uname', '-s'], cwd)
443  await exitsZero($, [os === 'Darwin' ? 'open' : 'xdg-open', url])
444  return `Wire Studio is running for this repository at ${url} (stop it with /wire-studio stop).`
445}
446
447async function stopStudio($: any, cwd: string): Promise<string> {
448  const paths = await studioPaths($, cwd)
449  const rec = await readStudio($, paths.state)
450  if (!rec || !(await exitsZero($, ['kill', '-0', String(rec.pid)]))) {
451    await $.fs.write(paths.state, '')
452    return 'Wire Studio is not running for this repository.'
453  }
454  await exitsZero($, ['kill', String(rec.pid)])
455  for (let i = 0; i < 8 && (await exitsZero($, ['kill', '-0', String(rec.pid)])); i++) {
456    await $.clock.sleep(250)
457  }
458  await $.fs.write(paths.state, '')
459  return `Wire Studio stopped (it was at http://127.0.0.1:${rec.port}/).`
460}
461
462async function studioStatus($: any, cwd: string): Promise<string> {
463  const paths = await studioPaths($, cwd)
464  const rec = await readStudio($, paths.state)
465  if (await studioUp($, rec)) {
466    return `Wire Studio is running for this repository at http://127.0.0.1:${rec?.port}/ (started ${rec?.startedAt}).`
467  }
468  return 'Wire Studio is not running for this repository. Start it with /wire-studio.'
469}
470
471export const register: Register = (on, options: Options) => {
472  on('session.start', async ($, e, next) => {
473    await $.command.register({ name: 'wire-usage', description: "Wire: this session's Wire commands with duration, tokens and cost" })
474    await $.command.register({ name: 'wire-studio', description: 'Wire: start, restart, stop or check Wire Studio for this repository', argumentHint: '[start|restart|stop|status] [--port <n>]' })
475    return next(e)
476  })
477
478  on('command.run', { command: 'wire-usage' }, async $ => {
479    const rows = (await runsOf($)).map(r => ({
480      command: r.command, release: r.release, invokedBy: r.invokedBy, tokens: totalTokens(r.usage),
481      cost: computeCost(r.model, r.usage), seconds: r.endedAt === null ? null : Math.round((r.endedAt - r.startedAt) / 1000), filled: r.filled,
482    }))
483    return { text: usageReport(rows) }
484  })
485
486  on('command.run', { command: 'wire-studio' }, async ($, e) => {
487    const parsed = parseStudioArgs(e.args)
488    if ('error' in parsed) {
489      return { text: parsed.error }
490    }
491    const cwd = await $.session.cwd()
492    if (parsed.action === 'stop') {
493      return { text: await stopStudio($, cwd) }
494    }
495    if (parsed.action === 'status') {
496      return { text: await studioStatus($, cwd) }
497    }
498    if (parsed.action === 'restart') {
499      await stopStudio($, cwd)
500    }
501    return { text: await startStudio($, cwd, parsed.port) }
502  })
503
504  // A typed Wire command (also the first prompt of a session Studio opened).
505  on('command.run', async ($, e, next) => {
506    const command = logCommandOf(e.command)
507    if (command) {
508      if (command === '/wire:autopilot') {
509        await $.state.set(AUTOPILOT, true)
510      }
511      const by = invokedBy('command', null, false, await $.env.get('WIRE_INVOKED_BY'))
512      await startRun($, options, await newRun($, `cmd-${crypto.randomUUID()}`, command, e.args, null, by))
513    }
514    return next(e)
515  })
516
517  // A Wire command run through the Skill tool: the orchestrator, Autopilot, or a lane.
518  on('tool.call', { tool: 'Skill' }, async ($, e, next) => {
519    const command = logCommandOf(String(e.skill ?? ''))
520    if (command) {
521      const agentId = e.agentId ?? null
522      const { value: autopilot } = await $.state.get(AUTOPILOT)
523      const by = invokedBy('skill', agentId, autopilot === true, undefined)
524      await startRun($, options, await newRun($, e.tool_use_id ?? `skill-${crypto.randomUUID()}`, command, String(e.args ?? ''), agentId, by))
525    }
526    return next(e)
527  })
528
529  // Each model request's measured usage goes to the Wire run open in its loop.
530  on('turn.step', async function* ($, e, next) {
531    const result = yield* next(e)
532    await addStepUsage($, e.agentId ?? null, e.turnId, result?.usage ?? null)
533    return result
534  })
535
536  // The loop's turn ended: its runs close and their log rows are filled.
537  on('turn.complete', async ($, e, next) => {
538    const result = await next(e)
539    await closeRuns($, e.agentId ?? null, e.turnId, e.usage ?? result?.usage ?? null)
540    await backfillPending($, options)
541    return result
542  })
543
544  // An orchestrator writing a lane's row after the lane's turn ended.
545  for (const tool of ['Write', 'Edit', 'MultiEdit'] as const) {
546    on('tool.call', { tool }, async ($, e, next) => {
547      const result = await next(e)
548      if (String(e.file_path ?? '').endsWith('execution_log.md')) {
549        await backfillPending($, options)
550      }
551      return result
552    })
553  }
554}
555
hooks/wire/constants.ts 8 lines
1// Substituted by wire/scripts/build-packages.sh, as the 4.0 shell hook's
2// placeholders were. wire/tests/schema/validate_telemetry_version.py checks
3// that no built package ships either placeholder raw.
4export const SEGMENT_WRITE_KEY = 'DxXwrT6ucDMRmouCsYDwthdChwDLsNYL'
5export const WIRE_VERSION = '4.4.4'
6export const SEGMENT_TRACK = 'https://api.segment.io/v1/track'
7export const SEGMENT_IDENTIFY = 'https://api.segment.io/v1/identify'
8
hooks/wire/logic.ts 406 lines
1// Pure logic for the Wire mod's telemetry and execution-log metrics.
2// No `$` here: everything takes plain values and returns plain values, so
3// tests/logic.test.ts exercises it directly. The contract it implements is
4// specs/utils/execution_log.md ("Metrics Backfill") and specs/utils/telemetry.md.
5
6export type Usage = {
7  input_tokens: number
8  output_tokens: number
9  cache_creation_input_tokens: number
10  cache_read_input_tokens: number
11}
12
13export const NA = 'n/a'
14
15// Anthropic API prices, USD per million tokens, as of 2026-09:
16// (input, output, cache_read). Cache writes are charged at 1.25x input.
17// Matched by substring against the model id, first match wins, so more
18// specific ids come before their prefixes. Carried over unchanged from the
19// retired hooks/wire_metrics.py.
20export const PRICING: ReadonlyArray<readonly [string, readonly [number, number, number]]> = [
21  ['claude-haiku-4-5', [1.0, 5.0, 0.1]],
22  ['claude-sonnet-4-6', [3.0, 15.0, 0.3]],
23  ['claude-sonnet-5', [2.0, 10.0, 0.2]],
24  ['claude-opus-4', [5.0, 25.0, 0.5]],
25  ['claude-opus-5', [5.0, 25.0, 0.5]],
26  ['claude-fable-5-1', [10.0, 50.0, 0.25]],
27  ['claude-fable-5', [10.0, 50.0, 1.0]],
28  ['claude-mythos-5', [10.0, 50.0, 1.0]],
29]
30export const CACHE_WRITE_MULTIPLIER = 1.25
31
32export const INVOKED_BY = ['typed', 'orchestrator', 'lane', 'autopilot', 'studio'] as const
33export type InvokedBy = (typeof INVOKED_BY)[number]
34
35export function emptyUsage(): Usage {
36  return { input_tokens: 0, output_tokens: 0, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 }
37}
38
39/** Adds a usage record (any object with the four counts) to a running total. */
40export function addUsage(total: Usage, more: Partial<Record<keyof Usage, unknown>> | null | undefined): Usage {
41  if (!more) {
42    return total
43  }
44  const n = (v: unknown) => (typeof v === 'number' && Number.isFinite(v) ? Math.trunc(v) : 0)
45  return {
46    input_tokens: total.input_tokens + n(more.input_tokens),
47    output_tokens: total.output_tokens + n(more.output_tokens),
48    cache_creation_input_tokens: total.cache_creation_input_tokens + n(more.cache_creation_input_tokens),
49    cache_read_input_tokens: total.cache_read_input_tokens + n(more.cache_read_input_tokens),
50  }
51}
52
53export function totalTokens(u: Usage): number {
54  return u.input_tokens + u.output_tokens + u.cache_creation_input_tokens + u.cache_read_input_tokens
55}
56
57/** Estimated USD cost, or null for an unknown model: never a guessed price. */
58export function computeCost(model: string | null | undefined, u: Usage): number | null {
59  if (!model) {
60    return null
61  }
62  const hit = PRICING.find(([key]) => model.includes(key))
63  if (!hit) {
64    return null
65  }
66  const [inRate, outRate, readRate] = hit[1]
67  const cost =
68    (u.input_tokens * inRate +
69      u.output_tokens * outRate +
70      u.cache_read_input_tokens * readRate +
71      u.cache_creation_input_tokens * inRate * CACHE_WRITE_MULTIPLIER) /
72    1_000_000
73  return Math.round(cost * 100) / 100
74}
75
76export function formatDuration(seconds: number | null | undefined): string {
77  if (seconds === null || seconds === undefined || !Number.isFinite(seconds)) {
78    return NA
79  }
80  const s = Math.max(0, Math.trunc(seconds))
81  const pad = (n: number) => String(n).padStart(2, '0')
82  if (s < 60) {
83    return `${s}s`
84  }
85  if (s < 3600) {
86    return `${Math.floor(s / 60)}m ${pad(s % 60)}s`
87  }
88  return `${Math.floor(s / 3600)}h ${pad(Math.floor((s % 3600) / 60))}m`
89}
90
91export type Measured = {
92  command: string
93  usage: Usage
94  model: string | null
95  durationSeconds: number | null
96  /**
97   * Rows dated before this day (`YYYY-MM-DD`, local) are never filled: they
98   * belong to an earlier run. A day, not a time, because commands write the
99   * row's time themselves and it is often rough (a model writing `00:00`).
100   */
101  notBefore?: string
102}
103
104function cellsOf(line: string): string[] | null {
105  const t = line.trim()
106  if (!(t.startsWith('|') && t.endsWith('|'))) {
107    return null
108  }
109  return t.slice(1, -1).split('|').map(c => c.trim())
110}
111
112/**
113 * Backfills the Duration, Tokens and Cost cells of the newest row that logs
114 * this command, whose Tokens cell is still `n/a` and whose date is not
115 * before `notBefore`. Returns the new text, or null when nothing may change:
116 * no such row, or only legacy rows without the nine metric-bearing columns.
117 * Only those three cells of that one row change; Duration only when still
118 * `n/a`. Searching upward, not only the last row, lets concurrent lanes'
119 * rows each be filled by their own run.
120 */
121export function backfillLog(text: string, run: Measured): string | null {
122  const lines = text.split('\n')
123  for (let i = lines.length - 1; i >= 0; i--) {
124    const cells = cellsOf(lines[i] ?? '')
125    if (!cells) {
126      continue
127    }
128    if (cells[0] === 'Timestamp' || /^[-:\s]+$/.test(cells[0] ?? '')) {
129      return null
130    }
131    if (cells.length < 9) {
132      continue
133    }
134    const command = (cells[1] ?? '').split(/\s+/)[0]
135    if (command !== run.command || cells[cells.length - 2] !== NA) {
136      continue
137    }
138    if (run.notBefore && (cells[0] ?? '').slice(0, 10) < run.notBefore.slice(0, 10)) {
139      return null
140    }
141    const n = cells.length
142    if (cells[n - 3] === NA) {
143      cells[n - 3] = formatDuration(run.durationSeconds)
144    }
145    cells[n - 2] = String(totalTokens(run.usage))
146    const cost = computeCost(run.model, run.usage)
147    cells[n - 1] = cost === null ? NA : `$${cost.toFixed(2)}`
148    lines[i] = `| ${cells.join(' | ')} |`
149    return lines.join('\n')
150  }
151  return null
152}
153
154/**
155 * `YYYY-MM-DD HH:MMZ` in UTC, the execution log's timestamp form
156 * (specs/utils/execution_log.md, 4.4.3), `minutes` before `ms`. Rows written
157 * before 4.4.3 have no `Z` and an unknown zone; the backfill compares only
158 * the date and allows a day's slack, so it reads both forms.
159 */
160export function logStamp(ms: number, minutes = 0): string {
161  const d = new Date(ms - minutes * 60_000)
162  const p = (n: number) => String(n).padStart(2, '0')
163  return `${d.getUTCFullYear()}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())} ${p(d.getUTCHours())}:${p(d.getUTCMinutes())}Z`
164}
165
166/** The release folder a command names as its first argument, if it looks like one. */
167export function releaseOf(args: string): string | null {
168  const first = (args ?? '').trim().split(/\s+/)[0] ?? ''
169  return /^[A-Za-z0-9][A-Za-z0-9_.-]{0,120}$/.test(first) && !first.startsWith('-') ? first : null
170}
171
172/**
173 * The persona list (specs/utils/personas.md); wire/tests/core/validate_personas.py
174 * checks this copy matches the spec.
175 */
176export const PERSONAS = ['delivery-manager', 'analytics-engineer', 'data-engineer', 'engagement-lead'] as const
177
178/**
179 * The persona in the team roster (`.wire/engagement/team.yml`) for a git email:
180 * the entry whose `emails` include it. Same format as wire/scripts/persona.py
181 * writes; inline or block email lists.
182 */
183export function rosterPersonaOf(rosterText: string | null, email: string | null): string {
184  const wanted = (email ?? '').trim().toLowerCase()
185  if (!rosterText || !wanted) {
186    return ''
187  }
188  let emails: string[] = []
189  let persona = ''
190  let inEmails = false
191  const entries: { emails: string[]; persona: string }[] = []
192  const flush = () => { if (emails.length || persona) entries.push({ emails, persona }) }
193  for (const raw of rosterText.split('\n')) {
194    const line = raw.replace(/\s#.*$/, '')
195    if (/^\s*-\s+name:/.test(line)) {
196      flush()
197      emails = []
198      persona = ''
199      inEmails = false
200      continue
201    }
202    const inline = /^\s+emails:\s*\[(.*)\]\s*$/.exec(line)
203    if (inline) {
204      emails = inline[1].split(',').map((e) => e.trim().replace(/^['"]|['"]$/g, '').toLowerCase()).filter(Boolean)
205      inEmails = false
206      continue
207    }
208    if (/^\s+emails:\s*$/.test(line)) {
209      inEmails = true
210      continue
211    }
212    const item = /^\s+-\s+(\S+@\S+)\s*$/.exec(line)
213    if (item && inEmails) {
214      emails.push(item[1].replace(/^['"]|['"]$/g, '').toLowerCase())
215      continue
216    }
217    const p = /^\s+persona:\s*['"]?([^'"\s]*)/.exec(line)
218    if (p) {
219      persona = p[1]
220      inEmails = false
221    }
222  }
223  flush()
224  return entries.find((e) => e.emails.includes(wanted))?.persona ?? ''
225}
226
227/**
228 * The persona a run is made as (specs/utils/personas.md): `--as <persona>` in
229 * the command's arguments, then WIRE_PERSONA, then the clone's override
230 * (`.wire/local/persona.yml`), then the team roster by git email. The first
231 * value set decides; an unknown one gives ''. Only the persona is taken from
232 * the arguments, never anything else typed there.
233 */
234export function personaOf(args: string, envValue: string | undefined, fileText: string | null, rosterText: string | null = null, email: string | null = null): string {
235  const asArg = /(?:^|\s)--as(?:=|\s+)([^\s]+)/.exec(args ?? '')?.[1]
236  const fromFile = /^\s*persona:\s*['"]?([^'"#\s]+)/m.exec(fileText ?? '')?.[1]
237  for (const value of [asArg, envValue, fromFile, rosterPersonaOf(rosterText, email)]) {
238    const v = (value ?? '').trim()
239    if (v) {
240      return (PERSONAS as readonly string[]).includes(v) ? v : ''
241    }
242  }
243  return ''
244}
245
246/** `wire:dbt-generate` (command.run) or `wire:dbt-generate` (Skill) to the log's `/wire:dbt-generate`. */
247export function logCommandOf(name: string): string | null {
248  const m = /^wire:([A-Za-z0-9_-]+)$/.exec((name ?? '').trim())
249  return m ? `/wire:${m[1]}` : null
250}
251
252/**
253 * Who started a run (specs/utils/telemetry.md, invoked_by):
254 * - a typed command is `typed`, or `studio` when the session was opened by
255 *   Wire Studio's "Run in Claude Code" (WIRE_INVOKED_BY=studio in its env);
256 * - a Skill call on the main loop is `orchestrator`, or `autopilot` while
257 *   /wire:autopilot runs in this session;
258 * - a Skill call inside a subagent's loop is `lane`.
259 */
260export function invokedBy(path: 'command' | 'skill', agentId: string | null, autopilot: boolean, envValue: string | undefined): InvokedBy {
261  if (path === 'command') {
262    return envValue === 'studio' ? 'studio' : 'typed'
263  }
264  if (agentId) {
265    return 'lane'
266  }
267  return autopilot ? 'autopilot' : 'orchestrator'
268}
269
270export type TrackFields = {
271  command: string
272  invokedBy: InvokedBy
273  persona?: string
274  timestamp: string
275  release: string | null
276  sessionId: string | null
277  agentId: string | null
278  gitRepo: string
279  gitBranch: string
280  username: string
281  hostname: string
282  os: string
283  version: string
284}
285
286/**
287 * The Segment track body. Keeps the properties the 4.0 hook sent and adds
288 * release, session_id and agent_id. Deliberately leaves out the command's
289 * arguments: they can hold free text a consultant typed, and telemetry has
290 * never carried prompt text.
291 */
292export function trackBody(writeKey: string, userId: string, f: TrackFields): string {
293  return JSON.stringify({
294    writeKey,
295    userId,
296    event: 'wire_command',
297    properties: {
298      command: f.command,
299      timestamp: f.timestamp,
300      git_repo: f.gitRepo,
301      git_branch: f.gitBranch,
302      username: f.username,
303      hostname: f.hostname,
304      plugin_version: f.version,
305      os: f.os,
306      runtime: 'claude',
307      invoked_by: f.invokedBy,
308      persona: f.persona ?? '',
309      release: f.release,
310      session_id: f.sessionId,
311      agent_id: f.agentId,
312    },
313  })
314}
315
316export function identifyBody(writeKey: string, userId: string, f: Pick<TrackFields, 'username' | 'hostname' | 'os' | 'version' | 'timestamp'>): string {
317  return JSON.stringify({
318    writeKey,
319    userId,
320    traits: { username: f.username, hostname: f.hostname, os: f.os, plugin_version: f.version, first_seen: f.timestamp },
321  })
322}
323
324/** Off when the option is false or the matching environment variable is "false". */
325export function isOn(option: boolean | undefined, envValue: string | undefined): boolean {
326  return option !== false && envValue !== 'false'
327}
328
329export type RunRow = { command: string; release: string | null; invokedBy: InvokedBy; tokens: number; cost: number | null; seconds: number | null; filled: boolean }
330
331/** The text /wire-usage prints: this session's Wire runs with tokens and cost. */
332export function usageReport(rows: readonly RunRow[]): string {
333  if (rows.length === 0) {
334    return 'No Wire commands have run in this session yet.'
335  }
336  const lines = ['| Command | Release | Started by | Duration | Tokens | Cost (USD) | In log |', '|---|---|---|---|---|---|---|']
337  let tokens = 0
338  let cost = 0
339  let costKnown = true
340  for (const r of rows) {
341    tokens += r.tokens
342    if (r.cost === null) {
343      costKnown = false
344    } else {
345      cost += r.cost
346    }
347    lines.push(`| ${r.command} | ${r.release ?? '-'} | ${r.invokedBy} | ${formatDuration(r.seconds)} | ${r.tokens} | ${r.cost === null ? NA : `$${r.cost.toFixed(2)}`} | ${r.filled ? 'yes' : 'not yet'} |`)
348  }
349  lines.push('', `Total: ${tokens} tokens, ${costKnown ? `$${cost.toFixed(2)}` : `at least $${cost.toFixed(2)} (some models unpriced)`}.`)
350  return lines.join('\n')
351}
352
353// ---------------------------------------------------------------------------
354// /wire-studio: start, restart, stop and report Wire Studio for this repository
355// ---------------------------------------------------------------------------
356
357export type StudioAction = 'start' | 'restart' | 'stop' | 'status'
358export type StudioArgs = { action: StudioAction; port: number | null } | { error: string }
359
360export const STUDIO_USAGE = 'Usage: /wire-studio [start|restart|stop|status] [--port <n>]'
361export const STUDIO_PORT_FIRST = 4800
362export const STUDIO_PORT_LAST = 4820
363
364/** `start` when nothing is given; `--port` between 1024 and 65535. */
365export function parseStudioArgs(args: string): StudioArgs {
366  const words = (args ?? '').trim().split(/\s+/).filter(Boolean)
367  let action: StudioAction = 'start'
368  let port: number | null = null
369  for (let i = 0; i < words.length; i++) {
370    const w = words[i] ?? ''
371    if (w === 'start' || w === 'restart' || w === 'stop' || w === 'status') {
372      action = w
373    } else if (w === '--port' || w.startsWith('--port=')) {
374      const raw = w.includes('=') ? w.split('=')[1] : words[++i]
375      const n = Number(raw)
376      if (!Number.isInteger(n) || n < 1024 || n > 65535) {
377        return { error: `--port needs a number between 1024 and 65535. ${STUDIO_USAGE}` }
378      }
379      port = n
380    } else {
381      return { error: `Unknown argument "${w}". ${STUDIO_USAGE}` }
382    }
383  }
384  return { action, port }
385}
386
387/** One state file per repository, named from its path. */
388export function studioStateName(repo: string): string {
389  const slug = repo.replace(/[^A-Za-z0-9]+/g, '-').replace(/^-+|-+$/g, '').slice(-80)
390  return `${slug || 'repo'}.json`
391}
392
393export type StudioRecord = { pid: number; port: number; repo: string; startedAt: string }
394
395export function parseStudioRecord(text: string | null): StudioRecord | null {
396  if (!text) {
397    return null
398  }
399  try {
400    const r = JSON.parse(text)
401    return Number.isInteger(r?.pid) && Number.isInteger(r?.port) && typeof r?.repo === 'string' ? r : null
402  } catch {
403    return null
404  }
405}
406
types/index.d.ts 55 lines
1// The Wire mod's $.state contract: every value it keeps for the session.
2// `claude plugin validate` holds hooks/register.ts to it.
3
4export type WireRun = {
5  /** Run id: tool_use_id for a Skill call, or a generated id for a typed command. */
6  id: string
7  /** As the execution log writes it: `/wire:dbt-generate`. */
8  command: string
9  release: string | null
10  /** The loop the run is in: a subagent's id, null on the main loop. */
11  agentId: string | null
12  invokedBy: 'typed' | 'orchestrator' | 'lane' | 'autopilot' | 'studio'
13  /** The persona the run was made as (specs/utils/personas.md), '' when none. */
14  persona?: string
15  startedAt: number
16  endedAt: number | null
17  /** Final usage, set when the run closes (zero while it is open; see stepUsage). */
18  usage: {
19    input_tokens: number
20    output_tokens: number
21    cache_creation_input_tokens: number
22    cache_read_input_tokens: number
23  }
24  model: string | null
25  /** True once its execution_log.md row carries the measured cells. */
26  filled: boolean
27}
28
29/** Usage recorded per model request for one run, while it is open. */
30export type WireStepUsage = {
31  usage: WireRun['usage']
32  model: string | null
33}
34
35export type WireIdentity = {
36  userId: string
37  username: string
38  hostname: string
39  os: string
40  gitRepo: string
41  gitBranch: string
42}
43
44declare module 'claude-code' {
45  interface PluginState {
46    wire: {
47      runs: WireRun[]
48      /** One member per run id. Kept apart from `runs` so a request's late write can never overwrite a closed run. */
49      stepUsage: StateFamily<WireStepUsage>
50      autopilot: boolean
51      identity: WireIdentity | null
52    }
53  }
54}
55