mnm-store 0.2.3

Postgres + pgvector storage layer for midnight-manual (sqlx-backed).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! `chunk` entity queries.
//!
//! Chunks carry the FTS vector (`tsvector`, GENERATED STORED) and the
//! embedding (`vector(1024)`); both are written at insert time. The trigger
//! `trg_chunk_embedding_model_match` ensures `chunk.embedding_model_id`
//! always matches the owning `source_version.embedding_model_id` (FR-002),
//! and that `code_embedding` is only present when the owning version
//! declares a `code_embedding_model_id` (migration 0011).

use mnm_core::types::{Chunk, ChunkStatus};
use pgvector::Vector;
use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use time::OffsetDateTime;
use uuid::Uuid;

use crate::error::{Result, StoreError};

/// Document subset bundled into chunk read responses.
///
/// Intentionally smaller than the full `Document` row — only the fields
/// useful for navigation/inspection. Spec §1.1 of the chunk+document
/// navigation design.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DocumentSummary {
    /// Document UUID.
    pub id: Uuid,
    /// Repo-relative source path.
    pub source_path: String,
    /// Public published URL, if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub published_url: Option<String>,
    /// Public source URL, if any.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source_url: Option<String>,
    /// ISO language tag, if known.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// Document kind discriminator.
    pub kind: mnm_core::types::DocumentKind,
    /// Materialized provenance JSON.
    pub provenance: serde_json::Value,
}

/// Source subset bundled into chunk read responses.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceSummary {
    /// URL-safe stable handle, e.g. `compact-docs`.
    pub slug: String,
    /// Human-readable name, e.g. `Compact Docs`.
    pub display_name: String,
}

/// Chunk + bundled document + source context returned by the navigation
/// read endpoints (`GET /v1/chunks/:id`, `/next`, `/prev`).
///
/// The existing chunk fields stay at the top level via `#[serde(flatten)]`
/// so callers that deserialize into a struct containing only chunk fields
/// still work (they ignore the extra `document` and `source` keys).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ChunkWithContext {
    /// The chunk itself.
    #[serde(flatten)]
    pub chunk: Chunk,
    /// Parent document summary.
    pub document: DocumentSummary,
    /// Owning source summary.
    pub source: SourceSummary,
}

/// Parameters for inserting a new chunk — grouped because the table is wide.
#[derive(Debug, Clone)]
pub struct NewChunk<'a> {
    /// Owning source_version.
    pub source_version_id: Uuid,
    /// Owning document.
    pub document_id: Uuid,
    /// Owning node (kind=chunk).
    pub node_id: Uuid,
    /// 0-indexed position within the document's chunks.
    pub chunk_index: i32,
    /// Total chunks in the parent document.
    pub total_chunks: i32,
    /// Chunk content (text/markdown/code).
    pub content: &'a str,
    /// SHA-256 of `content`.
    pub content_hash: &'a str,
    /// Embedding vector. Must have exactly 1024 dimensions (the `voyage-code-3`
    /// width) when present; `None` for not-yet-embedded chunks.
    pub embedding: Option<Vec<f32>>,
    /// Embedding model used to produce `embedding`. MUST match the owning
    /// source_version's embedding_model_id (trigger-enforced).
    pub embedding_model_id: Uuid,
    /// Optional code-model vector (voyage-code-3); `None` for non-code chunks
    /// or versions without code embeddings. Requires the owning
    /// source_version to declare a `code_embedding_model_id` when `Some`
    /// (trigger-enforced).
    pub code_embedding: Option<Vec<f32>>,
    /// Markdown heading path.
    pub heading_path: &'a [String],
    /// Code-symbol path (structured, persisted as JSONB).
    pub symbol_path: &'a [mnm_core::types::SymbolSegment],
    /// Start byte in source.
    pub start_byte: i32,
    /// End byte in source.
    pub end_byte: i32,
    /// Best-effort token count.
    pub token_count: i32,
    /// Lifecycle state.
    pub status: ChunkStatus,
}

/// Insert a chunk row, returning the newly-minted id.
///
/// # Errors
///
/// Returns [`crate::error::StoreError::ForeignKeyViolation`] for unknown FKs,
/// or [`crate::error::StoreError::CheckViolation`] when the embedding-model
/// trigger rejects a mismatched chunk (EC-10).
pub async fn insert(pool: &PgPool, c: NewChunk<'_>) -> Result<Uuid> {
    let status_str = match c.status {
        ChunkStatus::Ready => "ready",
        ChunkStatus::EmbedFailed => "embed_failed",
        ChunkStatus::Deprecated => "deprecated",
    };
    let row: (Uuid,) = sqlx::query_as(
        "INSERT INTO chunk ( \
            source_version_id, document_id, node_id, chunk_index, total_chunks, content, \
            content_hash, embedding, embedding_model_id, code_embedding, heading_path, \
            symbol_path, start_byte, end_byte, token_count, status \
         ) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16) RETURNING id",
    )
    .bind(c.source_version_id)
    .bind(c.document_id)
    .bind(c.node_id)
    .bind(c.chunk_index)
    .bind(c.total_chunks)
    .bind(c.content)
    .bind(c.content_hash)
    .bind(c.embedding.map(Vector::from))
    .bind(c.embedding_model_id)
    .bind(c.code_embedding.map(Vector::from))
    .bind(c.heading_path)
    .bind(sqlx::types::Json(c.symbol_path))
    .bind(c.start_byte)
    .bind(c.end_byte)
    .bind(c.token_count)
    .bind(status_str)
    .fetch_one(pool)
    .await?;
    Ok(row.0)
}

/// One row returned by [`list_embed_failed_batch`] — the minimum needed for
/// the embedder worker to encode the text and write the vector back.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmbedFailedChunk {
    /// Chunk id (PK).
    pub id: Uuid,
    /// Verbatim chunk content to embed.
    pub content: String,
    /// Owning embedding model — guards a cross-model write.
    pub embedding_model_id: Uuid,
}

/// Fetch up to `limit` `embed_failed` chunks whose `embedding_model_id`
/// matches `model_id`. Used by the background embedder worker.
///
/// `source_version_filter` is `None` in production (the worker drains every
/// pending chunk across all source_versions sharing the model). Integration
/// tests pass `Some(sv_id)` so a concurrent sibling test running against the
/// same shared CI Postgres doesn't pollute the batch.
///
/// Rows are ordered by `created_at ASC` so older work goes first
/// (fair-queue semantics — a fresh ingest doesn't starve an in-flight one).
///
/// # Errors
///
/// Returns [`crate::error::StoreError::Database`] on driver failure.
pub async fn list_embed_failed_batch(
    pool: &PgPool,
    model_id: Uuid,
    source_version_filter: Option<Uuid>,
    limit: i64,
) -> Result<Vec<EmbedFailedChunk>> {
    let rows: Vec<(Uuid, String, Uuid)> = sqlx::query_as(
        "SELECT id, content, embedding_model_id FROM chunk \
         WHERE status = 'embed_failed' AND embedding_model_id = $1 \
           AND ($2::uuid IS NULL OR source_version_id = $2::uuid) \
         ORDER BY created_at ASC \
         LIMIT $3",
    )
    .bind(model_id)
    .bind(source_version_filter)
    .bind(limit)
    .fetch_all(pool)
    .await?;
    Ok(rows
        .into_iter()
        .map(|(id, content, embedding_model_id)| EmbedFailedChunk {
            id,
            content,
            embedding_model_id,
        })
        .collect())
}

/// Set the embedding bytes and flip `status` to `ready` for one chunk.
///
/// No-ops (returns `Ok(false)`) if the row is no longer in `embed_failed` —
/// another worker may have raced us, or the chunk may have been demoted to
/// `deprecated`. The `trg_chunk_embedding_model_match` trigger is honoured
/// by only updating the embedding column (the column under cross-check is
/// never mutated by this query).
///
/// # Errors
///
/// Returns [`crate::error::StoreError::Database`] on driver failure (e.g.
/// vector dimensionality mismatch).
pub async fn set_embedding(pool: &PgPool, id: Uuid, vector: Vec<f32>) -> Result<bool> {
    let result = sqlx::query(
        "UPDATE chunk SET embedding = $1, status = 'ready' \
         WHERE id = $2 AND status = 'embed_failed'",
    )
    .bind(Vector::from(vector))
    .bind(id)
    .execute(pool)
    .await?;
    Ok(result.rows_affected() == 1)
}

/// Fetch a chunk by id (status filter applied — embed_failed rows are excluded).
///
/// # Errors
///
/// Returns [`crate::error::StoreError::NotFound`] if id is unknown OR the
/// chunk has `status = 'embed_failed'`.
pub async fn get_by_id_ready(pool: &PgPool, id: Uuid) -> Result<Chunk> {
    let row = sqlx::query_as::<_, ChunkRow>(
        "SELECT id, source_version_id, document_id, node_id, chunk_index, total_chunks, \
                content, content_hash, embedding_model_id, heading_path, symbol_path, \
                start_byte, end_byte, token_count, status, created_at \
         FROM chunk WHERE id = $1 AND status <> 'embed_failed'",
    )
    .bind(id)
    .fetch_one(pool)
    .await?;
    row.try_into()
}

/// Admin-facing variant: returns the chunk regardless of `status`. Used by
/// `mnm doctor` / `mnm chunks list --include-failed` (Phase 8).
///
/// # Errors
///
/// Returns [`crate::error::StoreError::NotFound`] if id is unknown.
pub async fn get_by_id_admin(pool: &PgPool, id: Uuid) -> Result<Chunk> {
    let row = sqlx::query_as::<_, ChunkRow>(
        "SELECT id, source_version_id, document_id, node_id, chunk_index, total_chunks, \
                content, content_hash, embedding_model_id, heading_path, symbol_path, \
                start_byte, end_byte, token_count, status, created_at \
         FROM chunk WHERE id = $1",
    )
    .bind(id)
    .fetch_one(pool)
    .await?;
    row.try_into()
}

/// Carry-forward snapshot of a chunk — all the fields the ingest handler
/// needs to clone a chunk row into a new `source_version` without re-running
/// the embedder.
#[derive(Debug, Clone, PartialEq)]
pub struct CarryForwardChunk {
    /// Verbatim chunk text.
    pub content: String,
    /// SHA-256 over `content`.
    pub content_hash: String,
    /// Existing embedding vector (may be `None` if the prior chunk was in
    /// `embed_failed`).
    pub embedding: Option<Vec<f32>>,
    /// Embedding model id from the prior version — must match the new SV's
    /// embedding model (trigger-enforced).
    pub embedding_model_id: Uuid,
    /// Existing code-model vector, carried forward so re-ingest doesn't
    /// silently drop code embeddings (`None` for non-code chunks).
    pub code_embedding: Option<Vec<f32>>,
    /// Markdown heading path.
    pub heading_path: Vec<String>,
    /// Code-symbol path (structured).
    pub symbol_path: Vec<mnm_core::types::SymbolSegment>,
    /// 0-indexed chunk position.
    pub chunk_index: i32,
    /// Total chunks in the parent document.
    pub total_chunks: i32,
    /// Start byte in source.
    pub start_byte: i32,
    /// End byte in source.
    pub end_byte: i32,
    /// Best-effort token count.
    pub token_count: i32,
    /// Lifecycle state.
    pub status: ChunkStatus,
}

/// List every chunk under a document for carry-forward cloning. Includes
/// `embed_failed` rows so a doc with a failed chunk doesn't silently lose it
/// across versions.
///
/// # Errors
///
/// Returns [`crate::error::StoreError::Database`] on driver failure.
pub async fn list_for_carry_forward(
    pool: &PgPool,
    document_id: Uuid,
) -> Result<Vec<CarryForwardChunk>> {
    let rows = sqlx::query_as::<_, CarryForwardRow>(
        "SELECT content, content_hash, embedding, embedding_model_id, code_embedding, \
                heading_path, symbol_path, chunk_index, total_chunks, start_byte, end_byte, \
                token_count, status \
         FROM chunk WHERE document_id = $1 ORDER BY chunk_index",
    )
    .bind(document_id)
    .fetch_all(pool)
    .await?;
    rows.into_iter().map(TryInto::try_into).collect()
}

/// List the next `count` chunks after `anchor` in the same document,
/// ordered by `chunk_index` ascending. Skips `embed_failed` chunks.
///
/// # Errors
///
/// Returns `StoreError::NotFound` if the anchor doesn't exist.
pub async fn list_next(pool: &PgPool, anchor: Uuid, count: usize) -> Result<Vec<ChunkWithContext>> {
    let count = i64::try_from(count.clamp(1, 100)).unwrap_or(5);
    let rows = sqlx::query_as::<_, ChunkWithContextRow>(
        "WITH a AS (SELECT document_id, chunk_index FROM chunk WHERE id = $1) \
         SELECT \
            c.id, c.source_version_id, c.document_id, c.node_id, c.chunk_index, c.total_chunks, \
            c.content, c.content_hash, c.embedding_model_id, c.heading_path, c.symbol_path, \
            c.start_byte, c.end_byte, c.token_count, c.status, c.created_at, \
            d.source_path AS d_source_path, d.published_url AS d_published_url, \
            d.source_url AS d_source_url, d.language AS d_language, d.kind AS d_kind, \
            d.provenance AS d_provenance, \
            s.slug AS s_slug, s.display_name AS s_display_name \
         FROM chunk c \
         JOIN document d ON c.document_id = d.id \
         JOIN source_version sv ON c.source_version_id = sv.id \
         JOIN source s ON sv.source_id = s.id, a \
         WHERE c.document_id = a.document_id \
           AND c.chunk_index > a.chunk_index \
           AND c.status <> 'embed_failed' \
         ORDER BY c.chunk_index ASC \
         LIMIT $2",
    )
    .bind(anchor)
    .bind(count)
    .fetch_all(pool)
    .await?;
    rows.into_iter().map(TryInto::try_into).collect()
}

/// List the previous `count` chunks before `anchor` in the same document.
///
/// Returned in ascending `chunk_index` (reading) order. SQL fetches the
/// `count` immediately-preceding rows via DESC LIMIT, then the helper
/// reverses to ascending. Skips `embed_failed` chunks.
///
/// # Errors
///
/// Returns `StoreError::NotFound` if the anchor doesn't exist.
pub async fn list_prev(pool: &PgPool, anchor: Uuid, count: usize) -> Result<Vec<ChunkWithContext>> {
    let count = i64::try_from(count.clamp(1, 100)).unwrap_or(5);
    let mut rows = sqlx::query_as::<_, ChunkWithContextRow>(
        "WITH a AS (SELECT document_id, chunk_index FROM chunk WHERE id = $1) \
         SELECT \
            c.id, c.source_version_id, c.document_id, c.node_id, c.chunk_index, c.total_chunks, \
            c.content, c.content_hash, c.embedding_model_id, c.heading_path, c.symbol_path, \
            c.start_byte, c.end_byte, c.token_count, c.status, c.created_at, \
            d.source_path AS d_source_path, d.published_url AS d_published_url, \
            d.source_url AS d_source_url, d.language AS d_language, d.kind AS d_kind, \
            d.provenance AS d_provenance, \
            s.slug AS s_slug, s.display_name AS s_display_name \
         FROM chunk c \
         JOIN document d ON c.document_id = d.id \
         JOIN source_version sv ON c.source_version_id = sv.id \
         JOIN source s ON sv.source_id = s.id, a \
         WHERE c.document_id = a.document_id \
           AND c.chunk_index < a.chunk_index \
           AND c.status <> 'embed_failed' \
         ORDER BY c.chunk_index DESC \
         LIMIT $2",
    )
    .bind(anchor)
    .bind(count)
    .fetch_all(pool)
    .await?;
    rows.reverse();
    rows.into_iter().map(TryInto::try_into).collect()
}

/// Get a chunk plus its document + source context. One JOIN query.
///
/// # Errors
///
/// Returns `StoreError::NotFound` when no chunk exists with that id (or
/// its status is `embed_failed`). Returns `StoreError::Database` on any
/// SQL failure.
pub async fn get_with_context(pool: &PgPool, id: Uuid) -> Result<ChunkWithContext> {
    let row = sqlx::query_as::<_, ChunkWithContextRow>(
        "SELECT \
            c.id, c.source_version_id, c.document_id, c.node_id, c.chunk_index, c.total_chunks, \
            c.content, c.content_hash, c.embedding_model_id, c.heading_path, c.symbol_path, \
            c.start_byte, c.end_byte, c.token_count, c.status, c.created_at, \
            d.source_path AS d_source_path, d.published_url AS d_published_url, \
            d.source_url AS d_source_url, d.language AS d_language, d.kind AS d_kind, \
            d.provenance AS d_provenance, \
            s.slug AS s_slug, s.display_name AS s_display_name \
         FROM chunk c \
         JOIN document d ON c.document_id = d.id \
         JOIN source_version sv ON c.source_version_id = sv.id \
         JOIN source s ON sv.source_id = s.id \
         WHERE c.id = $1 AND c.status <> 'embed_failed'",
    )
    .bind(id)
    .fetch_optional(pool)
    .await?
    .ok_or(StoreError::NotFound)?;
    row.try_into()
}

/// Fetch many chunks (each with document + source context) in one query.
///
/// Rows come back in arbitrary DB order; callers re-order by input order.
/// Ids that don't exist (or are `embed_failed`) are simply absent.
///
/// # Errors
///
/// Returns `StoreError::Database` on any SQL failure.
pub async fn get_many_with_context(pool: &PgPool, ids: &[Uuid]) -> Result<Vec<ChunkWithContext>> {
    let rows = sqlx::query_as::<_, ChunkWithContextRow>(
        "SELECT \
            c.id, c.source_version_id, c.document_id, c.node_id, c.chunk_index, c.total_chunks, \
            c.content, c.content_hash, c.embedding_model_id, c.heading_path, c.symbol_path, \
            c.start_byte, c.end_byte, c.token_count, c.status, c.created_at, \
            d.source_path AS d_source_path, d.published_url AS d_published_url, \
            d.source_url AS d_source_url, d.language AS d_language, d.kind AS d_kind, \
            d.provenance AS d_provenance, \
            s.slug AS s_slug, s.display_name AS s_display_name \
         FROM chunk c \
         JOIN document d ON c.document_id = d.id \
         JOIN source_version sv ON c.source_version_id = sv.id \
         JOIN source s ON sv.source_id = s.id \
         WHERE c.id = ANY($1) AND c.status <> 'embed_failed'",
    )
    .bind(ids)
    .fetch_all(pool)
    .await?;
    rows.into_iter().map(TryInto::try_into).collect()
}

#[derive(sqlx::FromRow)]
struct ChunkWithContextRow {
    // 16 chunk columns (same as ChunkRow):
    id: Uuid,
    source_version_id: Uuid,
    document_id: Uuid,
    node_id: Uuid,
    chunk_index: i32,
    total_chunks: i32,
    content: String,
    content_hash: String,
    embedding_model_id: Uuid,
    heading_path: Vec<String>,
    symbol_path: sqlx::types::Json<Vec<mnm_core::types::SymbolSegment>>,
    start_byte: i32,
    end_byte: i32,
    token_count: i32,
    status: String,
    created_at: time::OffsetDateTime,
    // 6 document columns:
    d_source_path: String,
    d_published_url: Option<String>,
    d_source_url: Option<String>,
    d_language: Option<String>,
    d_kind: String,
    d_provenance: serde_json::Value,
    // 2 source columns:
    s_slug: String,
    s_display_name: String,
}

impl TryFrom<ChunkWithContextRow> for ChunkWithContext {
    type Error = StoreError;
    fn try_from(r: ChunkWithContextRow) -> Result<Self> {
        // Mirror the existing ChunkRow → Chunk pattern: round-trip the
        // status string through serde_json so the enum's existing serde
        // impl handles the decode.
        let status: ChunkStatus = serde_json::from_value(serde_json::Value::String(r.status))
            .map_err(|e| StoreError::Json(e.to_string()))?;
        let doc_kind: mnm_core::types::DocumentKind =
            serde_json::from_value(serde_json::Value::String(r.d_kind))
                .map_err(|e| StoreError::Json(e.to_string()))?;
        let chunk = Chunk {
            id: r.id,
            source_version_id: r.source_version_id,
            document_id: r.document_id,
            node_id: r.node_id,
            chunk_index: r.chunk_index,
            total_chunks: r.total_chunks,
            content: r.content,
            content_hash: r.content_hash,
            embedding_model_id: r.embedding_model_id,
            heading_path: r.heading_path,
            symbol_path: r.symbol_path.0,
            start_byte: r.start_byte,
            end_byte: r.end_byte,
            token_count: r.token_count,
            status,
            created_at: r.created_at,
        };
        Ok(Self {
            chunk,
            document: DocumentSummary {
                id: r.document_id,
                source_path: r.d_source_path,
                published_url: r.d_published_url,
                source_url: r.d_source_url,
                language: r.d_language,
                kind: doc_kind,
                provenance: r.d_provenance,
            },
            source: SourceSummary {
                slug: r.s_slug,
                display_name: r.s_display_name,
            },
        })
    }
}

#[derive(sqlx::FromRow)]
struct CarryForwardRow {
    content: String,
    content_hash: String,
    embedding: Option<Vector>,
    embedding_model_id: Uuid,
    code_embedding: Option<Vector>,
    heading_path: Vec<String>,
    symbol_path: sqlx::types::Json<Vec<mnm_core::types::SymbolSegment>>,
    chunk_index: i32,
    total_chunks: i32,
    start_byte: i32,
    end_byte: i32,
    token_count: i32,
    status: String,
}

impl TryFrom<CarryForwardRow> for CarryForwardChunk {
    type Error = crate::error::StoreError;

    fn try_from(r: CarryForwardRow) -> std::result::Result<Self, Self::Error> {
        let status: ChunkStatus = serde_json::from_value(serde_json::Value::String(r.status))
            .map_err(|e| crate::error::StoreError::Json(e.to_string()))?;
        Ok(Self {
            content: r.content,
            content_hash: r.content_hash,
            embedding: r.embedding.map(|v| v.to_vec()),
            embedding_model_id: r.embedding_model_id,
            code_embedding: r.code_embedding.map(|v| v.to_vec()),
            heading_path: r.heading_path,
            symbol_path: r.symbol_path.0,
            chunk_index: r.chunk_index,
            total_chunks: r.total_chunks,
            start_byte: r.start_byte,
            end_byte: r.end_byte,
            token_count: r.token_count,
            status,
        })
    }
}

#[derive(sqlx::FromRow)]
struct ChunkRow {
    id: Uuid,
    source_version_id: Uuid,
    document_id: Uuid,
    node_id: Uuid,
    chunk_index: i32,
    total_chunks: i32,
    content: String,
    content_hash: String,
    embedding_model_id: Uuid,
    heading_path: Vec<String>,
    symbol_path: sqlx::types::Json<Vec<mnm_core::types::SymbolSegment>>,
    start_byte: i32,
    end_byte: i32,
    token_count: i32,
    status: String,
    created_at: OffsetDateTime,
}

impl TryFrom<ChunkRow> for Chunk {
    type Error = crate::error::StoreError;

    fn try_from(r: ChunkRow) -> std::result::Result<Self, Self::Error> {
        let status: ChunkStatus = serde_json::from_value(serde_json::Value::String(r.status))
            .map_err(|e| crate::error::StoreError::Json(e.to_string()))?;
        Ok(Self {
            id: r.id,
            source_version_id: r.source_version_id,
            document_id: r.document_id,
            node_id: r.node_id,
            chunk_index: r.chunk_index,
            total_chunks: r.total_chunks,
            content: r.content,
            content_hash: r.content_hash,
            embedding_model_id: r.embedding_model_id,
            heading_path: r.heading_path,
            symbol_path: r.symbol_path.0,
            start_byte: r.start_byte,
            end_byte: r.end_byte,
            token_count: r.token_count,
            status,
            created_at: r.created_at,
        })
    }
}

/// Read a chunk's structured symbol path. Test/diagnostic helper.
///
/// # Errors
/// Propagates query errors.
pub async fn symbol_path_of(
    pool: &PgPool,
    id: Uuid,
) -> Result<Vec<mnm_core::types::SymbolSegment>> {
    let row: (sqlx::types::Json<Vec<mnm_core::types::SymbolSegment>>,) =
        sqlx::query_as("SELECT symbol_path FROM chunk WHERE id = $1")
            .bind(id)
            .fetch_one(pool)
            .await?;
    Ok(row.0 .0)
}