nomoreide-daemon 0.20.1

The NoMoreIDE daemon: the local HTTP server, its route registry, and the embedded web dashboard.
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
//! The context library over HTTP: what the vault holds, what links what, and
//! the notes a person writes into it.
//!
//! Every refusal here is a flat sentence rather than a zod report, which is the
//! whole reason this domain is cheap: the schemas are strict and the wording is
//! fixed, so a body either parses into what the operation wants or gets one of
//! four sentences back.
//!
//! **A revision is a precondition, not a field.** An update or a delete carries
//! the revision the caller believes the note is at, and a mismatch is a 409
//! carrying the note as it *actually* is — the only refusal on this surface
//! that hands back state. That is what makes two editors safe without locking
//! anything: the loser is told what it missed rather than being told "no".

use crate::server::app::AppState;
use crate::server::body::decode_uri_component as percent_decode;
use crate::server::errors::{error, method_not_allowed};
use crate::server::routes::query::query_value;
use axum::body::Bytes;
use axum::extract::State;
use axum::http::{StatusCode, Uri};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post, put};
use axum::{Json, Router};
use nomoreide_core::agent_transcripts::{
    default_transcript_homes, list_agent_transcripts, AgentTranscript, DEFAULT_TRANSCRIPT_LIMIT,
};
use nomoreide_core::context_library::{
    ContextAttachment, ContextLibrary, ContextNote, ContextRef, CreateContextNote,
    UpdateContextNote, CONTEXT_KINDS,
};
use nomoreide_core::context_snapshot::{
    context_graph, context_snapshot, ContextListing, ContextQuery,
};
use serde::Serialize;
use serde_json::Value;

pub(crate) fn routes() -> Router<AppState> {
    Router::new()
        .route("/api/context", get(list))
        .route("/api/context/graph", get(graph))
        .route("/api/context/notes", post(create_note))
        .route(
            "/api/context/notes/:id",
            get(read_note)
                .put(update_note)
                .delete(delete_note)
                .fallback(method_not_allowed),
        )
        .route("/api/context/pins", put(set_pins))
        .route("/api/context/preview", post(preview))
        .route("/api/context/content", post(content))
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ListEnvelope {
    ok: bool,
    #[serde(flatten)]
    listing: ContextListing,
}

async fn list(State(state): State<AppState>, uri: Uri) -> Response {
    match snapshot_for(&state, &query_from(&uri)).await {
        Ok((listing, _)) => Json(ListEnvelope { ok: true, listing }).into_response(),
        Err(message) => error(StatusCode::INTERNAL_SERVER_ERROR, &message),
    }
}

async fn graph(State(state): State<AppState>, uri: Uri) -> Response {
    let query = query_from(&uri);
    let (listing, notes) = match snapshot_for(&state, &query).await {
        Ok(pair) => pair,
        Err(message) => return error(StatusCode::INTERNAL_SERVER_ERROR, &message),
    };
    let config = match state.config_store.load().await {
        Ok(config) => config,
        Err(failure) => {
            return error(StatusCode::INTERNAL_SERVER_ERROR, &failure.to_string());
        }
    };
    Json(serde_json::json!({
        "ok": true,
        "graph": context_graph(&listing, &notes, &config),
    }))
    .into_response()
}

/// The listing, and the notes it was built from.
///
/// The graph needs both: the snapshot decides what is *visible* (it is
/// filtered, sorted and capped), while the edges are read off the notes' own
/// links, which the items no longer carry.
async fn snapshot_for(
    state: &AppState,
    query: &ContextQuery,
) -> Result<(ContextListing, Vec<ContextNote>), String> {
    let config = state
        .config_store
        .load()
        .await
        .map_err(|failure| failure.to_string())?;
    let incidents = state.errors.list(100);
    // Reading every candidate transcript is filesystem work, and so is walking
    // each repository for its Markdown. Neither belongs on the runtime thread.
    let transcripts: Vec<AgentTranscript> = if query.includes_session() {
        let (home, codex_home) = default_transcript_homes();
        tokio::task::spawn_blocking(move || {
            list_agent_transcripts(&home, &codex_home, None, DEFAULT_TRANSCRIPT_LIMIT)
        })
        .await
        .map_err(|join| join.to_string())?
    } else {
        Vec::new()
    };
    let query = query.clone();
    tokio::task::spawn_blocking(
        move || -> Result<(ContextListing, Vec<ContextNote>), String> {
            let library = ContextLibrary::default();
            let notes = library.notes()?;
            let snapshot = context_snapshot(&library, &config, &incidents, &transcripts, &query)?;
            Ok((snapshot, notes))
        },
    )
    .await
    .map_err(|join| join.to_string())?
}

/// `kinds` is split on commas and **silently drops** what it does not
/// recognise. So `kinds=note,widget` is `kinds=note`, and `kinds=widget` is an
/// *empty* set rather than an absent one — which matches nothing at all rather
/// than everything. An absent parameter and a blank one are different things
/// here, and only the absent one means "every kind".
fn query_from(uri: &Uri) -> ContextQuery {
    ContextQuery {
        q: query_value(uri, "q"),
        project_path: query_value(uri, "projectPath"),
        kinds: query_value(uri, "kinds").map(|raw| {
            raw.split(',')
                .filter(|kind| CONTEXT_KINDS.contains(kind))
                .map(str::to_string)
                .collect()
        }),
    }
}

async fn create_note(body: Bytes) -> Response {
    let Some(input) = create_input(&parsed_body(&body)) else {
        return error(StatusCode::BAD_REQUEST, "Invalid context note.");
    };
    match tokio::task::spawn_blocking(move || ContextLibrary::default().create_note(input)).await {
        Ok(Ok(note)) => (StatusCode::CREATED, Json(note_envelope(note))).into_response(),
        Ok(Err(message)) => context_failure(message),
        Err(join) => error(StatusCode::INTERNAL_SERVER_ERROR, &join.to_string()),
    }
}

async fn read_note(uri: Uri) -> Response {
    let id = match note_id(&uri) {
        Ok(id) => id,
        Err((status, message)) => return error(status, message),
    };
    match tokio::task::spawn_blocking(move || ContextLibrary::default().get_note(&id)).await {
        Ok(Ok(note)) => Json(note_envelope(note)).into_response(),
        // **A note that is not there is a 404 here and a 400 everywhere else.**
        // The reference's `getNote` answers `undefined` and this route turns
        // that into a 404, while an update or a delete raises the same words as
        // a validation failure and gets a 400. Same sentence, two statuses,
        // decided by which route is asking — so the split lives here rather
        // than in the shared refusal.
        Ok(Err(message)) if message == "Context note not found." => {
            error(StatusCode::NOT_FOUND, &message)
        }
        Ok(Err(message)) => context_failure(message),
        Err(join) => error(StatusCode::INTERNAL_SERVER_ERROR, &join.to_string()),
    }
}

async fn update_note(uri: Uri, body: Bytes) -> Response {
    let id = match note_id(&uri) {
        Ok(id) => id,
        Err((status, message)) => return error(status, message),
    };
    let Some(input) = update_input(&parsed_body(&body)) else {
        return error(StatusCode::BAD_REQUEST, "Invalid context note update.");
    };
    let target = id.clone();
    match tokio::task::spawn_blocking(move || ContextLibrary::default().update_note(&id, input))
        .await
    {
        Ok(Ok(note)) => Json(note_envelope(note)).into_response(),
        Ok(Err(message)) => refusal(message, &target).await,
        Err(join) => error(StatusCode::INTERNAL_SERVER_ERROR, &join.to_string()),
    }
}

async fn delete_note(uri: Uri, body: Bytes) -> Response {
    let id = match note_id(&uri) {
        Ok(id) => id,
        Err((status, message)) => return error(status, message),
    };
    let Some(revision) = revision_only(&parsed_body(&body)) else {
        return error(
            StatusCode::BAD_REQUEST,
            "A valid note revision is required.",
        );
    };
    let target = id.clone();
    match tokio::task::spawn_blocking(move || ContextLibrary::default().delete_note(&id, &revision))
        .await
    {
        Ok(Ok(())) => Json(serde_json::json!({ "ok": true })).into_response(),
        Ok(Err(message)) => refusal(message, &target).await,
        Err(join) => error(StatusCode::INTERNAL_SERVER_ERROR, &join.to_string()),
    }
}

async fn set_pins(body: Bytes) -> Response {
    let Some(refs) = pins(&parsed_body(&body)) else {
        return error(StatusCode::BAD_REQUEST, "Invalid pinned context.");
    };
    match tokio::task::spawn_blocking(move || ContextLibrary::default().set_pinned(refs)).await {
        Ok(Ok(pinned)) => Json(serde_json::json!({ "ok": true, "pinned": pinned })).into_response(),
        Ok(Err(message)) => context_failure(message),
        Err(join) => error(StatusCode::INTERNAL_SERVER_ERROR, &join.to_string()),
    }
}

/// One item's body, for a reader rather than for an agent's prompt.
///
/// **Deliberately not part of `preview`.** A preview is the `<nomoreide-context>`
/// block that gets handed to an agent, and it renders a derived row as the few
/// facts that place it. Inlining file bodies there would grow every attachment
/// the MCP surface builds and start evicting items against `MAX_CONTEXT_CHARS`.
/// This route answers the different question the dashboard's panel is asking —
/// "show me this file" — and leaves that contract alone.
async fn content(State(state): State<AppState>, body: Bytes) -> Response {
    let payload = parsed_body(&body);
    let Some(context_ref) = content_input(&payload) else {
        return error(StatusCode::BAD_REQUEST, "Invalid context content request.");
    };
    // Unfiltered, like the preview: a ref names one thing directly, and whether
    // it happens to sit outside the page's current filter is not this route's
    // business. It is also what scopes the read — only an indexed item resolves.
    let (listing, _) = match snapshot_for(&state, &ContextQuery::default()).await {
        Ok(pair) => pair,
        Err(message) => return error(StatusCode::INTERNAL_SERVER_ERROR, &message),
    };
    let items = listing.items();
    match tokio::task::spawn_blocking(move || {
        ContextLibrary::default().content(&context_ref, &items)
    })
    .await
    {
        Ok(Ok(content)) => {
            Json(serde_json::json!({ "ok": true, "content": content })).into_response()
        }
        Ok(Err(message)) => error(StatusCode::NOT_FOUND, &message),
        Err(failure) => error(StatusCode::INTERNAL_SERVER_ERROR, &failure.to_string()),
    }
}

async fn preview(State(state): State<AppState>, body: Bytes) -> Response {
    let payload = parsed_body(&body);
    // **`projectPath` is validated and then dropped.** The reference uses it to
    // mark a resolved item as belonging to another project, and this core's
    // `preview` takes no such argument — so a preview scoped to a project comes
    // back unscoped. Left as a gap rather than papered over: the shape of the
    // refusal is right today, and widening `ContextLibrary::preview` is a
    // change to a function the MCP surface also calls.
    let Some((attachment, _project_path)) = preview_input(&payload) else {
        return error(StatusCode::BAD_REQUEST, "Invalid context preview request.");
    };
    // A preview resolves against the **whole** library, unfiltered: an
    // attachment names refs directly, and a ref that happens to be outside the
    // page's current filter is still the thing the caller asked for.
    let (listing, _) = match snapshot_for(&state, &ContextQuery::default()).await {
        Ok(pair) => pair,
        Err(message) => return error(StatusCode::INTERNAL_SERVER_ERROR, &message),
    };
    let items = listing.items();
    match tokio::task::spawn_blocking(move || {
        ContextLibrary::default().preview(&attachment, &items)
    })
    .await
    {
        Ok(Ok(preview)) => {
            // A preview resolves against plain items, so what it hands back has
            // a note's body, revision and links stripped off it. The listing
            // still holds the whole row and the client renders a resolved note
            // from one, so each resolved ref is put back the way the listing
            // has it.
            let resolved: Vec<Value> = preview
                .resolved
                .iter()
                .map(|item| {
                    listing
                        .items
                        .iter()
                        .find(|entry| entry.item().context_ref == item.context_ref)
                        .and_then(|entry| serde_json::to_value(entry).ok())
                        .unwrap_or_else(|| serde_json::to_value(item).unwrap_or(Value::Null))
                })
                .collect();
            Json(serde_json::json!({
                "ok": true,
                "preview": {
                    "context": preview.context,
                    "estimatedTokens": preview.estimated_tokens,
                    "resolved": resolved,
                    "missing": preview.missing,
                    "warnings": preview.warnings,
                },
            }))
            .into_response()
        }
        Ok(Err(message)) => context_failure(message),
        Err(join) => error(StatusCode::INTERNAL_SERVER_ERROR, &join.to_string()),
    }
}

fn note_envelope(note: ContextNote) -> Value {
    serde_json::json!({ "ok": true, "note": note })
}

/// How the library's failures reach the wire.
///
/// There are only two kinds. A **conflict** carries the note as it stands, so
/// the caller can see what it missed; everything else is a flat 400. The split
/// is on the wording because that is what the library gives us — it has one
/// error type — and the conflict is the one message that means "your copy is
/// stale" rather than "your request was wrong".
fn context_failure(message: String) -> Response {
    error(StatusCode::BAD_REQUEST, &message)
}

/// A refusal from an operation that named a note, which is the only place a
/// conflict can arise.
///
/// A conflict answers **409 with the note as it actually is**, so the caller can
/// see what it missed rather than being told to go and look. That is what makes
/// two editors safe here without locking anything, and it is why the note is
/// read *again* after the failure: the whole point is that the caller's copy is
/// the stale one.
async fn refusal(message: String, id: &str) -> Response {
    if !message.starts_with("This note changed outside NoMoreIDE") {
        return context_failure(message);
    }
    let target = id.to_string();
    let current = tokio::task::spawn_blocking(move || ContextLibrary::default().get_note(&target))
        .await
        .ok()
        .and_then(Result::ok);
    (
        StatusCode::CONFLICT,
        Json(serde_json::json!({ "ok": false, "error": message, "current": current })),
    )
        .into_response()
}

/// `[a-zA-Z0-9-]{8,100}`, checked **before** the note is looked for.
///
/// The id names a file in the vault, so a segment that could climb out of it
/// must never reach the filesystem — which is why this refuses rather than
/// answering 404 for something that is not a plausible id at all.
fn note_id(uri: &Uri) -> Result<String, Refusal> {
    let raw = uri.path().split('/').nth(4).unwrap_or_default();
    // **A malformed escape is a 500, not a 400.** `decodeURIComponent` throws a
    // `URIError` rather than returning undefined, and the route's catch only
    // knows the library's two error types — so it rethrows and the dispatcher
    // renders it as an unhandled failure. That is a rough edge in the
    // reference, not a decision, but a client that branches on the status would
    // see a port answering 400 as a different endpoint.
    let id = percent_decode(raw).ok_or((StatusCode::INTERNAL_SERVER_ERROR, "URI malformed"))?;
    let length = id.chars().count();
    if !(8..=100).contains(&length)
        || !id
            .chars()
            .all(|character| character.is_ascii_alphanumeric() || character == '-')
    {
        return Err((StatusCode::BAD_REQUEST, "Invalid context note id."));
    }
    Ok(id)
}

/// A status and the wording that goes with it, small enough not to trip
/// clippy's `result_large_err` in an error position.
type Refusal = (StatusCode, &'static str);

fn parsed_body(body: &[u8]) -> Value {
    serde_json::from_slice::<Value>(body).unwrap_or(Value::Null)
}

/// `noteCreateSchema`. Strict, so an unknown key is a refusal; every array is
/// optional here and required on an update.
fn create_input(payload: &Value) -> Option<CreateContextNote> {
    const KEYS: &[&str] = &[
        "title",
        "body",
        "projectPaths",
        "tags",
        "aliases",
        "sourceKey",
    ];
    let object = payload.as_object()?;
    if object.keys().any(|key| !KEYS.contains(&key.as_str())) {
        return None;
    }
    Some(CreateContextNote {
        title: bounded(object.get("title")?, 1, 120)?,
        body: match object.get("body") {
            None => String::new(),
            Some(value) => sized(value, 1024 * 1024)?,
        },
        project_paths: string_list(object.get("projectPaths"), 50, 2_000)?,
        tags: string_list(object.get("tags"), 100, 100)?,
        aliases: string_list(object.get("aliases"), 100, 120)?,
        source_key: match object.get("sourceKey") {
            None => None,
            Some(value) => Some(bounded(value, 1, 1_000)?),
        },
    })
}

/// `noteUpdateSchema`: the create shape with every array **required**, a
/// required body, a revision, and `sourceKey` omitted — so sending one is a
/// refusal even though creating with one is fine.
fn update_input(payload: &Value) -> Option<UpdateContextNote> {
    const KEYS: &[&str] = &[
        "title",
        "body",
        "projectPaths",
        "tags",
        "aliases",
        "revision",
    ];
    let object = payload.as_object()?;
    if object.keys().any(|key| !KEYS.contains(&key.as_str())) {
        return None;
    }
    Some(UpdateContextNote {
        title: bounded(object.get("title")?, 1, 120)?,
        body: sized(object.get("body")?, 1024 * 1024)?,
        project_paths: string_list(Some(object.get("projectPaths")?), 50, 2_000)?,
        tags: string_list(Some(object.get("tags")?), 100, 100)?,
        aliases: string_list(Some(object.get("aliases")?), 100, 120)?,
        revision: revision(object.get("revision")?)?,
    })
}

fn revision_only(payload: &Value) -> Option<String> {
    let object = payload.as_object()?;
    if object.keys().any(|key| key != "revision") {
        return None;
    }
    revision(object.get("revision")?)
}

/// `z.string().regex(/^[a-f0-9]{64}$/)` — lowercase only, so an uppercase
/// sha256 of exactly the right bytes is still refused.
fn revision(value: &Value) -> Option<String> {
    let text = value.as_str()?;
    (text.len() == 64
        && text
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte)))
    .then(|| text.to_string())
}

fn pins(payload: &Value) -> Option<Vec<ContextRef>> {
    let object = payload.as_object()?;
    if object.keys().any(|key| key != "refs") {
        return None;
    }
    context_refs(object.get("refs")?)
}

/// `{ "ref": { "kind", "id" } }` and nothing else.
fn content_input(payload: &Value) -> Option<ContextRef> {
    let object = payload.as_object()?;
    if object.keys().any(|key| key != "ref") {
        return None;
    }
    let mut refs = context_refs(&Value::Array(vec![object.get("ref")?.clone()]))?;
    refs.pop()
}

fn preview_input(payload: &Value) -> Option<(ContextAttachment, Option<String>)> {
    let object = payload.as_object()?;
    if object
        .keys()
        .any(|key| key != "attachment" && key != "projectPath")
    {
        return None;
    }
    let attachment = object.get("attachment")?.as_object()?;
    if attachment
        .keys()
        .any(|key| key != "refs" && key != "includePinned")
    {
        return None;
    }
    Some((
        ContextAttachment {
            refs: context_refs(attachment.get("refs")?)?,
            include_pinned: attachment.get("includePinned")?.as_bool()?,
        },
        match object.get("projectPath") {
            None => None,
            Some(value) => Some(bounded(value, 1, 2_000)?),
        },
    ))
}

fn context_refs(value: &Value) -> Option<Vec<ContextRef>> {
    let refs = value.as_array()?;
    if refs.len() > 200 {
        return None;
    }
    refs.iter()
        .map(|entry| {
            let object = entry.as_object()?;
            if object.keys().any(|key| key != "kind" && key != "id") {
                return None;
            }
            let kind = object.get("kind")?.as_str()?;
            if !CONTEXT_KINDS.contains(&kind) {
                return None;
            }
            Some(ContextRef {
                kind: kind.to_string(),
                id: bounded(object.get("id")?, 1, 1_000)?,
            })
        })
        .collect()
}

/// `z.string().trim().min(a).max(b)`: the trim is a transform, so it runs first
/// and the bounds apply to what survives it.
fn bounded(value: &Value, min: usize, max: usize) -> Option<String> {
    let text = value.as_str()?.trim();
    let length = text.chars().map(char::len_utf16).sum::<usize>();
    (length >= min && length <= max).then(|| text.to_string())
}

/// A bound with no trim and no minimum — a body may be empty and its
/// whitespace is content.
fn sized(value: &Value, max: usize) -> Option<String> {
    let text = value.as_str()?;
    (text.chars().map(char::len_utf16).sum::<usize>() <= max).then(|| text.to_string())
}

fn string_list(value: Option<&Value>, max_items: usize, max_length: usize) -> Option<Vec<String>> {
    let Some(value) = value else {
        return Some(Vec::new());
    };
    let entries = value.as_array()?;
    if entries.len() > max_items {
        return None;
    }
    entries
        .iter()
        .map(|entry| bounded(entry, 1, max_length))
        .collect()
}