Skip to content

Add ImportService gRPC: proto definition, nodespaced handler, and Tauri proxy (closes #1136) - #1154

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1136-import-service-grpc
May 18, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1136-import-service-grpc

Conversation

@malibio

@malibio malibio commented May 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Defines ImportService in a new import_service.proto with server-streaming ImportMarkdown and ImportMarkdownFiles RPCs
  • Implements ImportServiceImpl in nodespaced, preserving the two-phase pipeline (parse → background DB write) and smart collection routing from the original Tauri commands
  • Migrates commands/import.rs in the Tauri app to a thin gRPC proxy; all import logic now runs in the daemon (survives window close)
  • Wires nodespace import file and nodespace import dir CLI subcommands
  • Fixes pre-existing CreateNodeRequest struct literal errors in three test/integration files

Test plan

  • cargo build succeeds with no errors
  • bun run test:all passes with no new failures
  • bun run quality:fix passes clean
  • nodespace import file <path> streams progress and reports success
  • nodespace import dir <path> imports all .md files recursively
  • Tauri import_markdown_file and import_markdown_files commands work via gRPC proxy

🤖 Generated with Claude Code

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Summary

This PR successfully migrates bulk markdown import from the Tauri process into nodespaced over gRPC, which is the correct architectural move under ADR-031. The proto definition is well-structured, the two-phase pipeline is correctly preserved, and the Tauri proxy is properly thinned out. The change is a definitive net improvement to the codebase.

All acceptance criteria are met except for one — there are no new tests for ImportService itself (the integration test suite covers NodeService round-trips but has no import-specific test). This is the primary concern to address before merge.


Findings

Critical Issues

None.


Suggested Improvements

[Improvement] packages/daemon/src/services/import_service.rs — No integration test for ImportService

The issue requires "bun run test:all passes" but there is no new test exercising the import gRPC path. grpc_round_trip.rs only covers NodeService. The import handler touches non-trivial logic: the two-phase pipeline, collection routing, the CHANNEL_BUFFER=64 back-pressure, and the final-event-carries-results contract. A minimal test (spin up in-process server, call ImportMarkdown with a temp .md file, drain the stream, assert step==9 and success==true) would close the gap between the existing coverage and what this PR adds.

[Improvement] packages/daemon/src/services/import_service.rs lines 1070–1228 — Silent bulk-create failure in Phase 2 background task does not surface to the caller

match node_service_clone
    .bulk_create_hierarchy_trusted(all_nodes)
    .await
{
    Ok(ids) => tracing::info!("Bulk created {} nodes", ids.len()),
    Err(e) => tracing::error!("Failed to bulk create nodes: {:?}", e),
}

If bulk_create_hierarchy_trusted fails, the error is logged but the stream still sends a step=9 / complete event with success=true for all prepared files. The client (CLI or Tauri) therefore receives a successful result for files that were never written to the database. The Phase 2 task should either mark results as failed and send those in the final event, or send an error-typed event. At minimum, the final summary count should reflect actual DB outcomes, not Phase 1 parse outcomes.

[Improvement] packages/cli/src/commands/import.rs lines 162–163 — Magic constant 9 for final step

if event.step == 9 {

This appears in four places across the CLI and Tauri proxy. The proto comment documents 9 = complete but there is no shared constant or enum. A const STEP_COMPLETE: u32 = 9; in the daemon crate (or a proto enum) would make intent explicit and eliminate the risk of one site being updated while others are not.

[Improvement] packages/daemon/src/services/import_service.rs lines 830–851 — base_dir fallback uses first file's parent, not the common ancestor

let base_dir = if !opts.base_directory.is_empty() {
    PathBuf::from(&opts.base_directory)
} else {
    file_paths
        .first()
        .map(|p| {
            PathBuf::from(p)
                .parent()
                ...
        })
        ...
};

When base_directory is empty and files come from multiple directories, the routing falls back to the first file's parent. This can produce inconsistent collection assignments depending on file ordering. This is a pre-existing behavior inherited from the original commands/import.rs, but now that the daemon owns this logic, documenting the known limitation (or asserting that base_directory must be set for multi-directory batches) would prevent silent mis-routing.

[Improvement] packages/desktop-app/src-tauri/src/commands/import.rs line 2284 — import_markdown_file returns an error if the stream closes without a result event

last_result.ok_or_else(|| "Import stream ended without a result".to_string())

This is the right approach, but the error string surfaced to the Tauri frontend is opaque. If the daemon crashes mid-import the user sees "Import stream ended without a result" with no context. Adding the file path to the message (format!("Import stream ended without a result for {}", file_path)) makes the frontend error actionable.

[Improvement] packages/daemon/src/services/import_service.rs — import_markdown_content (run_single_file_import path) duplicates node creation logic from the batch path

The single-file path calls create_node_with_parent + bulk_create_hierarchy_root_notify, while the batch path calls bulk_create_hierarchy_trusted. This means single-file imports trigger per-node notification events while batch imports do not, which is an intentional difference inherited from the original code — but the divergence now lives inside the daemon where it is less visible. A comment at the top of import_markdown_content explaining why it uses root_notify vs trusted would prevent accidental unification that breaks notification semantics.


Nitpicks

  • Nit: packages/daemon/proto/import_service.proto line 433 — the comment "file_result populated for single-file RPC" on ImportProgressEvent.results is slightly misleading; both RPCs populate results on step 9. Worth updating to "Populated on the final event (step=9) with per-file import results for both RPCs".

  • Nit: packages/cli/src/commands/import.rs lines 148–149 — exclude_patterns: vec![] and base_directory: String::new() are set unconditionally for the single-file path even though ImportFileArgs has no exclude_patterns field. These are proto defaults and are harmless, but shadowing them explicitly while ImportFileArgs has no corresponding fields adds slight confusion. A comment noting these are unused for single-file RPCs would help.

  • Nit: packages/daemon/src/services/import_service.rs — FileReadResult.relative_path is constructed but only used for display in filename fallback. The field could be removed in favour of computing the display name inline at the single call site, reducing the struct footprint.

  • Nit: packages/daemon/src/services/import_service.rs lines 1094–1101 — the large anonymous tuple type Vec<(String, String, String, Option<String>, f64, serde_json::Value)> appears twice (single-file path and batch path). Extracting this as a type alias type BulkNodeTuple = (String, String, String, Option<String>, f64, serde_json::Value) would reduce noise.


Acceptance Criteria Status

Criterion Status
ImportService proto defined and compiled via tonic-build Done
nodespaced handler implements two-phase import with smart collection routing Done
Progress streamed back via gRPC server-streaming RPC Done
commands/import.rs proxies via gRPC; no direct packages/core imports remain Done
Import completes if Tauri window is closed mid-import Done (daemon-side background task)
nodespace import <file> CLI subcommand works Done
cargo build succeeds Cannot verify in review — author must confirm
bun run test:all passes No new import-specific tests added (see Improvement above)
bun run quality:fix passes Cannot verify in review — author must confirm

The silent-failure-on-bulk-create-error issue and the missing integration test are the two items most worth addressing before merge. Everything else is polish.

@malibio

malibio commented May 18, 2026

Copy link
Copy Markdown
Collaborator Author

Addressed the two review findings from the initial review:

1. Silent bulk-insert failure (🔴 Critical) — fixed

Added a bulk_insert_failed bool in Phase 2. When bulk_create_hierarchy_trusted returns Err, all phase1_results are marked success=false with a descriptive error, and the collection assignment, mention creation, and lifecycle update steps are skipped entirely. Previously the error was only logged; callers saw success=true.

2. No integration test (🟡 Important) — added

New packages/daemon/tests/import_round_trip.rs with 3 in-process gRPC tests:

  • import_markdown_single_file_streams_progress_and_succeeds — streams progress events, asserts step-9 with success=true and nodes_created > 0
  • import_markdown_files_batch_reports_all_results — two-file batch, both results successful
  • import_markdown_missing_file_surfaces_failure — non-existent file yields success=false with non-empty error

All 1182 Rust tests pass; cargo clippy clean.

Michael Libio and others added 2 commits May 18, 2026 08:36
…ri proxy (closes #1136)

- `packages/daemon/proto/import_service.proto`: ImportService with
  ImportMarkdown and ImportMarkdownFiles server-streaming RPCs
- `packages/daemon/src/services/import_service.rs`: handler preserving
  two-phase pipeline (parse → background DB write) and smart collection
  routing from commands/import.rs
- `packages/daemon/src/main.rs`: registers ImportServiceServer alongside
  NodeServiceServer in both headless and tray server loops
- `packages/desktop-app/src-tauri/src/commands/import.rs`: replaced
  direct nodespace-core calls with thin gRPC proxy; imports now survive
  Tauri window close
- `packages/desktop-app/src-tauri/src/services/grpc_client.rs`: added
  ImportServiceClient to the in-process server; exposes import_client()
- `packages/cli/src/commands/import.rs`: `nodespace import file` and
  `nodespace import dir` subcommands via ImportServiceClient
- Fixed pre-existing CreateNodeRequest struct literal errors in
  grpc_round_trip.rs, cli/commands/node.rs, and cli_integration.rs
  (id and insert_after_node_id fields added in #1147 but not propagated)

Co-Authored-By: Claude <[email protected]>
- Surface bulk DB write failures to gRPC callers (bulk_insert_failed flag
  marks all phase1 results as failed rather than silently reporting success)
- Add import_round_trip.rs: 3 in-process gRPC tests covering single-file
  import, batch import, and missing-file error surfacing

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio
malibio force-pushed the worktree-issue-1136-import-service-grpc branch from ca62981 to 32a9944 Compare May 18, 2026 06:37
@malibio
malibio merged commit d39511f into main May 18, 2026
@malibio
malibio deleted the worktree-issue-1136-import-service-grpc branch May 18, 2026 06:38
malibio added a commit that referenced this pull request Jun 22, 2026
…ri proxy (closes #1136) (#1154)

* Add ImportService gRPC: proto definition, nodespaced handler, and Tauri proxy (closes #1136)

- `packages/daemon/proto/import_service.proto`: ImportService with
  ImportMarkdown and ImportMarkdownFiles server-streaming RPCs
- `packages/daemon/src/services/import_service.rs`: handler preserving
  two-phase pipeline (parse → background DB write) and smart collection
  routing from commands/import.rs
- `packages/daemon/src/main.rs`: registers ImportServiceServer alongside
  NodeServiceServer in both headless and tray server loops
- `packages/desktop-app/src-tauri/src/commands/import.rs`: replaced
  direct nodespace-core calls with thin gRPC proxy; imports now survive
  Tauri window close
- `packages/desktop-app/src-tauri/src/services/grpc_client.rs`: added
  ImportServiceClient to the in-process server; exposes import_client()
- `packages/cli/src/commands/import.rs`: `nodespace import file` and
  `nodespace import dir` subcommands via ImportServiceClient
- Fixed pre-existing CreateNodeRequest struct literal errors in
  grpc_round_trip.rs, cli/commands/node.rs, and cli_integration.rs
  (id and insert_after_node_id fields added in #1147 but not propagated)

Co-Authored-By: Claude <[email protected]>

* Fix silent bulk-insert failure and add ImportService integration tests

- Surface bulk DB write failures to gRPC callers (bulk_insert_failed flag
  marks all phase1 results as failed rather than silently reporting success)
- Add import_round_trip.rs: 3 in-process gRPC tests covering single-file
  import, batch import, and missing-file error surfacing

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant