Skip to content

Memory: hybrid FTS+vector backend, localvector brought in-tree (no vendoring) - #165

Merged
FMXExpress merged 1 commit into
mainfrom
claude/memory-vector-inline
Jun 7, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/memory-vector-inline

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Summary

Pivot from vendoring (PR #164's make get-localvector + vendor/localvector/) to fork-in-place. PasClaw owns the source for the hybrid memory backend under src/pkg/memory/localvector/, with the upstream MIT LICENSE preserved alongside it and a per-file header naming the source commit (0f00eec) so we can diff against upstream for change tracking. No external repo dependency at build time, no vendor/ step, no make get-localvector.

Why

Maintainer feedback on PR #164's vendored phase 2 was: "why wire the localvector directory in, instead of just using its ideas in the main pasclaw?" The vendoring approach had real costs — extra build step (make get-localvector required before first build), vendor tree shipped things we don't use (the standalone CLI app), and upstream bug fixes didn't reach us automatically. Fork-in-place addresses all three: code lives in our tree, only the units we actually use ship, and the per-file headers let us still cherry-pick upstream fixes manually when we want them.

Removed

  • Makefile: the get-localvector recipe, LOCALVECTOR_DIR / LOCALVECTOR_REPO / LOCALVECTOR_REF variables, the localvector vendoring comment block, the get-localvector .PHONY entry. All gone — fork-in-place doesn't need them.

Brought in-tree under src/pkg/memory/localvector/

File Role
LICENSE verbatim MIT from upstream
LocalVector.VectorStore.pas IVectorStore interface + RRF fusion + chunker + FTS query builder
LocalVector.VectorStore.Sqlite.pas FPC backend (dynamic sqlite3 + vec0 + FTS5)
LocalVector.VectorStore.FireDAC.pas Delphi backend (FireDAC + vec0 + FTS5)
LocalVector.Sqlite3.pas minimal dynamic sqlite3 binding (needed for load_extension)
LocalVector.Embedder.pas ONNX inference + mean/CLS/last pooling
LocalVector.Tokenizer.pas BERT WordPiece
LocalVector.Models.pas embedding model registry (MiniLM / bge / mxbai)
LocalVector.Runtime.pas runtime info
LocalVector.OrtProvision.pas ONNX Runtime DLL load / auto-download (win-x64 today)
LocalVector.VecProvision.pas sqlite-vec extension load / auto-download
LocalVector.Downloader.pas generic HTTP fetch helper
onnxruntime.pas ONNX Runtime C API wrapper
onnxruntime_pas_api.pas ONNX Runtime C API binding

Excluded: LocalVector.App.pas (the standalone CLI app — PasClaw is the consumer, not the CLI).

Each .pas carries a uniform attribution header pointing at the upstream URL + the exact source commit so we can diff against upstream for later bug-fix pulls without a submodule.

New adapter: src/pkg/memory/PasClaw.Memory.Vector.pas

Implements the IMemoryIndex contract over the in-tree IVectorStore + TEmbedder + TBertTokenizer. Open() locates the sqlite-vec extension, the chosen embedding model, the BERT vocab, and the ONNX Runtime under $PASCLAW_HOME/cache/localvector/. Each missing piece returns False with a single debug-level log line; no exception leaks to the caller — so PasClaw.Tools.Memory can fall through to the FTS-only NewMemoryIndex without an agent-visible stack trace.

Default model is MiniLM (DEFAULT_MODEL from LocalVector.Models — 384-d, ~90 MB). Search fuses FTS5 + vec0 ranks via Reciprocal Rank Fusion (k=60) inside the localvector store and returns chunk text as snippet. SyncDir is full-rebuild-per-call for now (embedding is fast enough for typical memory dirs; mtime-tracked incremental sync is a follow-up).

Updated: src/pkg/tools/PasClaw.Tools.Memory.pas

Tool_MemorySearch reads Cfg.VectorSearchEnabled (the flag PR #164 landed). When True, tries NewVectorMemoryIndex first; on Open() False, falls through to the existing NewMemoryIndex (FTS-only). Separate DB files (.index.db vs .index.db.vec) so flipping the flag doesn't cross-talk between the two schemas. Both backends live alongside each other on disk; only one is opened per call.

Makefile

src/pkg/memory/localvector added to UNIT_DIRS so LocalVector.* and onnxruntime* units resolve at compile time. No new build deps — ONNX Runtime + sqlite-vec are dynamically loaded at runtime, not linked. A stock build with no $PASCLAW_HOME/cache/localvector/ artifacts still runs; vector search just stays inert until the runtime is provisioned.

README

memory_search row updated: the hybrid mode is no longer "after the runtime hookup PR lands" — it's wired now; only the runtime artifacts under $PASCLAW_HOME/cache/localvector/ have to be present for the vector half to engage.

Test plan

  • make clean on Linux/FPC. The in-tree localvector units compile, link into the binary, no new build-time deps.
  • NewVectorMemoryIndex.Open returns False quietly on a system with no artifacts under <home>/cache/localvector — no exception, no error to the user. Tools.Memory falls through to FTS as designed.
  • NewMemoryIndex still works the same — the FTS5 fallback is unchanged.
  • Full test suite green: smoke + hashline + toolview + anthropic-server-tools + openai-server-tools + println-helper + utf8-codepage-tag + gemini-schema-strip + markdown-render + json-utf8-roundtrip.
  • Live exercise of the hybrid path against a memory dir + provisioned sqlite-vec + ONNX Runtime + MiniLM model weights — that's the provisioning PR's responsibility.

Supersedes

PR #164 stayed merged since it landed the config flag + onboarding prompt (both still useful). This PR removes the now-unused vendoring tooling PR #164 also added, and supersedes the closed phase-2 vendor-based adapter work.


Generated by Claude Code

PR #164 vendored localvector via `make get-localvector` cloning into
`vendor/localvector`. Maintainer feedback was that for a core feature
we shouldn't be coupled to an external repo at build time and the
vendor tree shipped things we don't use. Pivot to fork-in-place:
PasClaw owns the source under src/pkg/memory/localvector/ with the
upstream MIT LICENSE preserved alongside it and a per-file header
naming the source commit (0f00eec) for change tracking.

Removed
-------
  - Makefile: `make get-localvector`, `LOCALVECTOR_DIR`,
    `LOCALVECTOR_REPO`, `LOCALVECTOR_REF`, the localvector vendoring
    comment block, the `get-localvector` .PHONY entry.
  - The vendoring approach as a whole. `vendor/localvector` was
    transitional and never committed (it was always under the
    gitignored `vendor/` prefix); nothing to delete in tree.

Brought in-tree (src/pkg/memory/localvector/)
---------------------------------------------
  LICENSE                                — verbatim MIT from upstream
  LocalVector.VectorStore.pas            — IVectorStore + RRF + chunker
  LocalVector.VectorStore.Sqlite.pas     — FPC backend (dynamic sqlite3
                                           + vec0 + FTS5)
  LocalVector.VectorStore.FireDAC.pas    — Delphi backend (FireDAC +
                                           vec0 + FTS5)
  LocalVector.Sqlite3.pas                — minimal dynamic sqlite3
                                           binding (needed for
                                           load_extension)
  LocalVector.Embedder.pas               — ONNX inference + pooling
  LocalVector.Tokenizer.pas              — BERT WordPiece
  LocalVector.Models.pas                 — embedding model registry
  LocalVector.Runtime.pas                — runtime info
  LocalVector.OrtProvision.pas           — ONNX Runtime DLL load /
                                           auto-download (win-x64)
  LocalVector.VecProvision.pas           — sqlite-vec extension
                                           load / auto-download
  LocalVector.Downloader.pas             — generic HTTP fetch helper
  onnxruntime.pas                        — ONNX Runtime C API wrapper
  onnxruntime_pas_api.pas                — ONNX Runtime C API binding

Excluded: `LocalVector.App.pas` (the standalone CLI app — PasClaw is
the consumer here, not the CLI).

Each .pas file carries an attribution header pointing at the upstream
URL + the exact source commit so we can diff against upstream for
later bug-fix pulls without a submodule.

New unit: src/pkg/memory/PasClaw.Memory.Vector.pas

  Implements the IMemoryIndex contract over the in-tree IVectorStore
  + TEmbedder + TBertTokenizer. Open() locates the sqlite-vec
  extension, the chosen embedding model, the BERT vocab, and the ONNX
  Runtime under `$PASCLAW_HOME/cache/localvector/`. Each missing piece
  returns False with a single debug-level log line; no exception leaks
  to the caller — so PasClaw.Tools.Memory can fall through to the
  FTS-only NewMemoryIndex without an agent-visible stack trace.

  Default model is MiniLM (DEFAULT_MODEL from LocalVector.Models —
  384-d, ~90 MB). A config knob can expose bge / mxbai later. Search
  fuses FTS5 + vec0 ranks via Reciprocal Rank Fusion (k=60) inside
  the localvector store and returns chunk text as snippet.

  SyncDir is full-rebuild-per-call for now (embedding is fast enough
  for typical memory dirs; mtime-tracked incremental sync is a
  follow-up).

Updated: src/pkg/tools/PasClaw.Tools.Memory.pas

  Tool_MemorySearch reads `Cfg.VectorSearchEnabled` (the flag PR #164
  landed). When True, tries NewVectorMemoryIndex first; on Open()
  False, falls through to the existing NewMemoryIndex (FTS-only).
  Separate DB files (.index.db vs .index.db.vec) so flipping the flag
  doesn't cross-talk between the two schemas. Both backends live
  alongside each other on disk; only one is opened per call.

Makefile

  src/pkg/memory/localvector added to UNIT_DIRS so `LocalVector.*` and
  `onnxruntime*` units resolve at compile time. No new build deps —
  ONNX Runtime + sqlite-vec are dynamically loaded at runtime, not
  linked. A stock build with no `$PASCLAW_HOME/cache/localvector/`
  artifacts still runs; vector search just stays inert until the
  runtime is provisioned.

README

  memory_search row updated: the hybrid mode is no longer "after the
  runtime hookup PR lands" — it's wired now; only the runtime
  artifacts under $PASCLAW_HOME/cache/localvector/ have to be present
  for the vector half to engage.

Verified
--------
  - `make` clean on Linux/FPC. The in-tree localvector units compile,
    link into the binary, drag no new build-time deps in.
  - NewVectorMemoryIndex.Open returns False quietly on a system with
    no artifacts under <home>/cache/localvector — no exception, no
    error to the user. Tools.Memory falls through to FTS as designed.
  - NewMemoryIndex still works the same — the FTS5 fallback is
    unchanged.
  - Full test suite green: smoke + hashline + toolview +
    anthropic-server-tools + openai-server-tools + println-helper +
    utf8-codepage-tag + gemini-schema-strip + markdown-render +
    json-utf8-roundtrip.

Supersedes
----------
PR #164 stayed merged on main since it landed the config flag +
onboarding prompt (both still useful). This PR removes the now-unused
vendoring tooling that PR #164 also added.
@FMXExpress
FMXExpress merged commit fb01cb3 into main Jun 7, 2026
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
PR FMXExpress#165's PasClaw.Memory.Vector landed but its Open() needs three
artifacts under $PASCLAW_HOME/cache/localvector/ — sqlite-vec
extension, ONNX Runtime, and the MiniLM model + vocab — before the
hybrid backend will engage. Operators had to know to download those
themselves. This PR closes that loop with one new command and an
onboarding hook.

New: src/cmd/PasClaw.Cmd.Memory.pas

  Two subcommands:

    pasclaw memory provision
      Downloads sqlite-vec, ONNX Runtime, MiniLM model + vocab into
      $PASCLAW_HOME/cache/localvector/.

      Idempotent — re-running skips anything already present and
      redownloads anything suspiciously small (treated as partial
      fetch by TModelDownloader, mirrors the localvector convention).

      Calls the in-tree provisioning helpers wired by PR FMXExpress#165:
        LocalVector.VecProvision.EnsureVec0      — sqlite-vec from
                                                   asg017/sqlite-vec
                                                   GitHub releases
                                                   (cross-platform —
                                                   linux/macos/windows
                                                   × x86_64/aarch64).
        LocalVector.OrtProvision.EnsureOnnxRuntime — ONNX Runtime;
                                                   auto-download is
                                                   win-x64 only per
                                                   CanAutoProvisionRuntime.
                                                   On other platforms
                                                   prints install
                                                   instructions for
                                                   the system package
                                                   manager.
        LocalVector.Downloader.TModelDownloader — MiniLM model.onnx
                                                   (~90 MB) + vocab.txt
                                                   from HuggingFace.

      Per-step status: green ✓ on success, red ✗ on failure, dim ·
      for "platform doesn't support auto-download here". Final
      summary tells the operator whether the hybrid backend is
      ready or whether memory_search will still fall back to FTS.

      Exit code 0 on full success, 1 if any of the three steps
      failed. ONNX Runtime missing on Linux/macOS is the expected
      partial-success case — that exits 1 with clear "install via
      apt/brew, then re-run" guidance.

    pasclaw memory status
      Read-only view: shows the active backend (hybrid when
      vector_search_enabled and artifacts present, else FTS-only),
      the cache directory, and a per-artifact ✓/· checklist mirroring
      what PasClaw.Memory.Vector.Open looks for. Useful for "did the
      provision actually take" diagnostics without running a real
      memory_search.

Onboarding hook: src/cmd/PasClaw.Cmd.Onboard.pas

  After the existing PromptVectorSearch question, when the operator
  said yes, ask once more: "Download now? [y/N]". Default NO because
  the artifacts are ~91 MB and an Enter-through-everything onboarding
  shouldn't trigger a fat download the user didn't opt into. Yes
  invokes Cmd_Memory_Run(['provision']) inline; No prints "deferred,
  run pasclaw memory provision when ready".

  Dropped the now-stale "follow-up release downloads the model on
  first use" disclaimer from the prompt — that follow-up release is
  this PR.

Root.pas:
  - Register 'memory' in the subcommand list (slot 20 — between
    update and version to mirror the implementation-order Sub[]
    array).
  - Wire Cmd_Memory_Run into DispatchCommand.

Verified end-to-end on Linux/FPC:

  $ pasclaw memory status
    config setting: hybrid (when artifacts present) else FTS-only
    cache dir:      /root/.pasclaw/cache/localvector
    · sqlite-vec missing at .../vec0.so
    · all-MiniLM-L6-v2 model missing at .../model.onnx
    · vocab missing at .../vocab.txt

  $ pasclaw memory provision
    ✓ sqlite-vec extension installed at .../vec0.so
    · ONNX Runtime auto-download is win-x64 only on this build;
      install via your system package manager:
        Debian / Ubuntu:  apt install libonnxruntime-dev
        macOS (Homebrew): brew install onnxruntime
        Other Linux:      see https://onnxruntime.ai/docs/install/
    ✗ ONNX Runtime: Could not load onnxruntime. Put onnxruntime.so
      next to the executable or on the loader path.
      (install per the instructions above, then re-run
       `pasclaw memory provision`)
    ✓ embedding model all-MiniLM-L6-v2 (~90 MB) installed at .../models/...
    ! provisioning incomplete — memory_search will fall back to FTS-only

  $ pasclaw memory status
    ✓ sqlite-vec at .../vec0.so
    ✓ all-MiniLM-L6-v2 model at .../model.onnx
    ✓ vocab at .../vocab.txt
    (ONNX Runtime not shown because the status checklist mirrors
    the on-disk artifacts; the ONNX runtime is loadable-only, not
    a file we can stat.)

  $ ls -la $PASCLAW_HOME/cache/localvector/
    vec0.so                                160 KB
    models/all-MiniLM-L6-v2/model.onnx     90 MB
    models/all-MiniLM-L6-v2/vocab.txt      232 KB

  $ NewVectorMemoryIndex.Open() — returns False (ONNX missing →
    falls back to FTS as designed; with libonnxruntime-dev
    installed it'd return True and the hybrid path would activate
    on the next memory_search).

Full test suite green: smoke + hashline + toolview +
anthropic-server-tools + openai-server-tools + println-helper +
utf8-codepage-tag + gemini-schema-strip + markdown-render +
json-utf8-roundtrip.

Stacks on top of PR FMXExpress#165 (the in-tree localvector port). FMXExpress#165
must merge first.
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
…+ localvector + Cmd.Memory

dcc64 F2613 on PR FMXExpress#166:
  PasClaw.Cmd.Memory.pas(49): Unit 'LocalVector.Models' not found.

Same shape as PR FMXExpress#156's miss after PR FMXExpress#155 — the FPC Makefile got the
new unit dirs but the Delphi .dproj didn't. PR FMXExpress#165 added the in-tree
localvector port under src/pkg/memory/localvector/ + the new
PasClaw.Memory.Vector adapter; PR FMXExpress#166 added src/cmd/PasClaw.Cmd.Memory.pas.
Neither was wired into PasClaw.dproj, so the Delphi build can't resolve
LocalVector.Models / LocalVector.VectorStore / LocalVector.OrtProvision /
LocalVector.VecProvision / LocalVector.Downloader / etc.

Edits:

  DCC_UnitSearchPath
    Added `..\pkg\memory\localvector` (right after `..\pkg\memory` to
    keep the source-tree shape readable).

  DCCReference — pkg/memory
    Added `..\pkg\memory\PasClaw.Memory.Vector.pas` (PR FMXExpress#165 new file).
    Added all 13 in-tree localvector files under a fresh
    `pkg/memory/localvector` comment block:
      LocalVector.Models.pas
      LocalVector.Runtime.pas
      LocalVector.Sqlite3.pas
      LocalVector.Tokenizer.pas
      LocalVector.Embedder.pas
      LocalVector.VectorStore.pas
      LocalVector.VectorStore.Sqlite.pas
      LocalVector.VectorStore.FireDAC.pas
      LocalVector.Downloader.pas
      LocalVector.OrtProvision.pas
      LocalVector.VecProvision.pas
      onnxruntime.pas
      onnxruntime_pas_api.pas

  DCCReference — cmd
    Added `..\cmd\PasClaw.Cmd.Memory.pas` (PR FMXExpress#166 new file). Slotted
    next to PasClaw.Cmd.Membench.pas, matching the .pas filename
    ordering already present in this block.

Stacks on PR FMXExpress#166 (which stacks on PR FMXExpress#165). Lands after both;
merge order FMXExpress#165 → FMXExpress#166 → this.
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.

2 participants