Skip to content

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs

Comment 1, line 1

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:1

//! Shared SQLite helpers for OpenCode A17 and MiMo Code A14 (M3).

Comment 2, line 2

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:2

//!

Comment 3, line 3

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:3

//! Products in the OpenCode engine family retain separate identities, data roots,

Comment 4, line 4

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:4

//! instances, and registries; see adapters.md. This root-level module, like

Comment 5, line 5

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:5

//! kimi_wire.rs, shares serialized structures checked separately in each product's pinned source.

Comment 6, line 6

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:6

//! Each product supplies its namespace, agent name, parser version, and field policies.

Comment 7, line 7

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:7

//!

Comment 8, line 8

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:8

//! - Per-step part usage: data.type="step-finish" with cost and tokens fields,

Comment 9, line 9

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:9

//! tokens{input, output, reasoning, cache{read, write}},

Comment 10, line 10

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:10

//! and optional total. References: OpenCode commit 0027387,

Comment 11, line 11

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:11

//! packages/core/src/session/projector.ts usage extraction,

Comment 12, line 12

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:12

//! applyUsage counters, migration 20260510033149_session_usage.ts

Comment 13, line 13

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:13

//! json_extract queries, and vendored client

Comment 14, line 14

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:14

//! packages/app/vendor/opencode-ai-client-1.17.13-v2.tgz

Comment 15, line 15

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:15

//! StepFinishPart/AssistantMessage types, with optional total.

Comment 16, line 16

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:16

//! MiMo commit 456678b packages/opencode/src/session/message-v2.ts

Comment 17, line 17

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:17

//! defines StepFinishPart with five required numeric fields and optional total.

Comment 18, line 18

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:18

//! - OpenCode field rules, verified independently in its pinned source:

Comment 19, line 19

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:19

//! packages/core/src/session/runner/publish-llm-event.ts tokens()

Comment 20, line 20

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:20

//! maps input=usage.nonCachedInputTokens, excluding cache;

Comment 21, line 21

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:21

//! output=usage.visibleOutputTokens, with separate reasoning/cache read/write.

Comment 22, line 22

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:22

//! packages/llm/src/protocols/anthropic-messages.ts mapUsage

Comment 23, line 23

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:23

//! defines inclusive inputTokens=nonCached+cacheRead+cacheWrite through sumTokens.

Comment 24, line 24

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:24

//! Anthropic does not split reasoning: outputTokens includes it, while reasoning

Comment 25, line 25

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:25

//! defaults from undefined to zero through safe(). packages/llm/src/protocols/shared.ts

Comment 26, line 26

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:26

//! derives totalTokens=inputTokens+outputTokens when the provider omits total.

Comment 27, line 27

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:27

//! Derived input_total=input+cache.read+cache.write;

Comment 28, line 28

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:28

//! output_total=output+reasoning works with the inspected provider mappings,

Comment 29, line 29

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:29

//! where Anthropic reasoning is already in output and the separate reasoning field is zero.

Comment 30, line 30

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:30

//! Total is the sum of five components. Compare an optional total field

Comment 31, line 31

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:31

//! as source_total and diagnose differences; shared arithmetic does not verify another product.

Comment 32, line 32

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:32

//! - Both products' Timestamps default to Date.now(): epoch milliseconds.

Comment 33, line 33

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:33

//! - Pinned sql.ts table structures:

Comment 34, line 34

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:34

//! part(id, message_id, session_id, time_created, time_updated, data),

Comment 35, line 35

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:35

//! session(id, parent_id, version, …), message(id, …, data). MiMo has

Comment 36, line 36

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:36

//! message.agent_id and lacks session tokens_* columns; OpenCode differs.

Comment 37, line 37

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:37

//! Each product's common.rs defines its distinct detection fingerprint.

Comment 38, line 38

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:38

//!

Comment 39, line 39

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:39

//! Collection rules (adapters.md A14/A17):

Comment 40, line 40

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:40

//! - Assistant message.data.tokens aggregates a turn. OpenCode migrations sum its fields

Comment 41, line 41

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:41

//! into cumulative session columns. Read only part step-finish usage for per-call events;

Comment 42, line 42

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:42

//! message provides modelID/providerID attribution, without counting

Comment 43, line 43

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:43

//! its tokens or cost again.

Comment 44, line 44

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:44

//! - OpenCode session.tokens_* columns are reconciliation only:

Comment 45, line 45

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:45

//! projector applyUsage sums current step-finish parts, compensating for deleted rows.

Comment 46, line 46

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:46

//! MiMo lacks those columns, so omit this reconciliation.

Comment 47, line 47

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:47

//! - Models come from message.data.modelID/providerID (request_field), required by

Comment 48, line 48

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:48

//! the vendored AssistantMessage type and MiMo schema. Exact per-call completion time

Comment 49, line 49

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:49

//! is not available here: part timestamps describe writes to a derived database view.

Comment 50, line 50

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:50

//! Keep observed_at as time basis.

Comment 51, line 51

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:51

//! - A step-finish part has no independent call ID; use primary key part.id for event identity.

Comment 52, line 52

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:52

//!

Comment 53, line 53

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:53

//! Native samples cover OpenCode 1.18.34 and MiMo 0.1.15 under their documented route limits.

Comment 54, line 54

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:54

//! Each product selects record versions and zero-value rules independently; never infer one from the other.

Comment 55, line 71

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:71

/// Millisecond overlap before the processing position covers out-of-order concurrent-session writes.

Comment 56, line 73

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:73

/// Per-round row limit; timestamp/ID continuation resumes within the same millisecond.

Comment 57, line 76

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:76

/// Five numeric step-finish token components and an optional total.

Comment 58, line 77

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:77

/// Input is uncached in the inspected mappings; cache components are separate.

Comment 59, line 78

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:78

/// Reasoning/output separation depends on the provider; Anthropic keeps reasoning

Comment 60, line 79

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:79

/// within output with a zero separate component. Derive input_total=input+cr+cw,

Comment 61, line 80

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:80

/// output_total=output+reasoning, and total from all five components; compare optional total

Comment 62, line 81

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:81

/// as source_total and diagnose differences. Products apply their own zero-value rules.

Comment 63, line 89

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:89

/// Optional source total; absence remains unknown.

Comment 64, line 93

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:93

/// Component relationships follow the pinned mappings. Overflowing derived fields

Comment 65, line 94

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:94

/// remain None without panic or truncation; similar arithmetic does not establish another product's field rules.

Comment 66, line 151

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:151

/// MiMo 0.1.15 getUsage subtracts the normalized cache/reasoning

Comment 67, line 152

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:152

/// counters that it persists. Adding them back reconstructs positive

Comment 68, line 153

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:153

/// SDK totals even if a subcounter was defaulted. A zero subcounter alone

Comment 69, line 154

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:154

/// does not establish reported zero; this MiMo policy is independent of OpenCode.

Comment 70, line 203

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:203

/// Convert client-estimated cost from floating-point USD to micro-USD.

Comment 71, line 222

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:222

/// Extract step-finish usage from the independently inspected product structures:

Comment 72, line 223

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:223

/// - type must be step-finish;

Comment 73, line 224

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:224

/// - cost and tokens must both exist, as in OpenCode projector usage();

Comment 74, line 225

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:225

/// - five numeric tokens{input, output, reasoning, cache{read, write}} fields are required;

Comment 75, line 226

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:226

/// total is optional in MiMo's schema and the vendored client type;

Comment 76, line 227

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:227

/// - require bounded nonnegative values; invalid data returns None, leaving the caller's counted call with unknown tokens.

Comment 77, line 252

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:252

/// Product identity supplied during scanning; this module holds no product state.

Comment 78, line 254

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:254

/// Event-key namespace: adapter_id opencode or mimo-code.

Comment 79, line 256

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:256

/// Agent name used in statistics.

Comment 80, line 258

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:258

/// Parser version for this product implementation.

Comment 81, line 260

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:260

/// Reconcile OpenCode session.tokens_*; MiMo has no cumulative columns to reconcile.

Comment 82, line 262

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:262

/// Each product explicitly selects its independently verified zero-value policy.

Comment 83, line 266

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:266

/// Cursor persisted in ingestion_checkpoints.cursor_value.

Comment 84, line 270

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:270

/// Always zero; database change detection cannot use byte offsets under WAL.

Comment 85, line 273

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:273

/// Last processed part.time_updated, inclusive; None starts a complete scan.

Comment 86, line 281

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:281

/// Parse context stores schema fingerprints and version-selection state.

Comment 87, line 302

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:302

// Store only part.id as the diagnostic position, excluding paths and message bodies.

Comment 88, line 308

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:308

/// One window row: a step-finish part and session/message attribution columns.

Comment 89, line 322

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:322

/// Query the timestamp window using part LEFT JOIN session/message;

Comment 90, line 323

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:323

/// json_valid checks message.data before extracting modelID/providerID, without loading message bodies.

Comment 91, line 376

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:376

/// Highest numeric session.version in either product's database; an empty table returns None.

Comment 92, line 386

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:386

// Compare version components numerically, using the same ordering as kilo version_max.

Comment 93, line 392

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:392

/// Numeric-component version ordering, copied within the family with kilo's ordering rules.

Comment 94, line 407

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:407

/// OpenCode reconciliation compares five per-step components with five cumulative session columns;

Comment 95, line 408

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:408

/// projector applyUsage includes current parts and compensates for deleted rows.

Comment 96, line 457

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:457

/// Shared scan: step-finish parts produce model_call events and a persisted processing position.

Comment 97, line 458

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:458

/// Product callers supply a read-only connection, schema fingerprint, and version selection.

Comment 98, line 459

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:459

/// This function handles state restoration, resets, windows, events, reconciliation, and cursor updates.

Comment 99, line 460

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:460

/// Scan context: product identity, schema fingerprint, database version, and selection basis.

Comment 100, line 485

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:485

// Do not filter the cursor by generation: same-database in-place rewrites trigger Rescan,

Comment 101, line 486

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:486

// but part IDs and revisions retain a valid processing position.

Comment 102, line 503

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:503

// A changed schema fingerprint resets the position for a complete idempotent reread by ID.

Comment 103, line 546

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:546

// SQL already filters with json_valid; protect against rows changed during this read.

Comment 104, line 557

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:557

// Row time describes a derived-view write; skip timestamps before 2000 without guessing replacements.

Comment 105, line 595

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:595

// A step-finish record identifies a completed step; count the call even when tokens are unknown.

Comment 106, line 640

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:640

// Use the part write time as observed_at; it does not establish exact upstream call time (A17).

Comment 107, line 654

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:654

// The current part value is Final; later edits can replace it through time_updated revisions.

Comment 108, line 666

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:666

// Resume by timestamp and stable key; dense overlaps and same-millisecond rows must not repeat only the first page.

Comment 109, line 772

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:772

// Missing cost or tokens returns None, matching projector's extraction rule.

Comment 110, line 778

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:778

// A type other than step-finish returns None.

Comment 111, line 785

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:785

// Missing or excessive required numeric fields return None, matching the MiMo schema.

Comment 112, line 810

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:810

// Input excludes cache in pinned publish-llm-event tokens(); derive inclusive input.

Comment 113, line 822

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:822

// Missing total leaves source_total=None while preserving the derived total.

Comment 114, line 834

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:834

// Diagnose differences between source total and derived total when component relationships permit comparison.

Comment 115, line 861

desktop/src-tauri/crates/core/src/adapters/opencode_family.rs:861

// Overflowing derived fields remain None without panic or numeric truncation.