WayfinderKnowledgeStore class
Saved local indexes of OKF bundles, and dense search over them.
Each bundle's index lives in dataDirectory under
indexes/bundles/<bundle key>/<configuration key>/. The bundle key hashes
the bundle's canonical root, so worktrees and copies get indexes of their
own. The configuration key hashes the store's chunking, snapshot and
storage configuration with its encoder's identity, so a different
model, chunker or format never reads or replaces another's index. Indexes
earlier releases wrote elsewhere under indexes/, by Wayfinder 0.2
(indexes/<sha256 of the root>/) or by an ObjectBox-backed store
(indexes/<bundle key>/<configuration key>/ with a .lock beside it),
are never read, changed or deleted; the first index after an upgrade
builds anew.
An index is a sequence of immutable generations,
generation-<id>/{snapshot.json, vectors.f32}, and a current pointer
that names the published one. index embeds only passages whose inputs
changed, writes a complete generation and then replaces the pointer
atomically, so a failed or cancelled run leaves the previous generation
current. A search reads the pointer and the generation it names, checks
the vectors' checksum, and scores every eligible passage exactly in a
MemoryStore. search refuses an index that is missing, stale or
incompatible, and never indexes implicitly.
Only writers lock. index, forget and forgetProjectRoot hold the
bundle's writer.lock (WayfinderFileLock), across isolates and
processes; another writer meanwhile fails with index-busy instead of
waiting. status, search and searchBundle hold no lock, so they keep
working while an index runs: they read the last published generation,
which nothing changes. To tell whether a writer runs, they probe the
writer lock with WayfinderFileLock.isHeld, a shared lock taken for an
instant, which a writer's WayfinderFileLock.tryExclusive waits out
rather than report as busy. A superseded generation stays for
gracePeriod after the publication that replaced it, so a reader that
read the pointer just before still finds it; garbage collection runs
whenever a writer holds the lock, at the start of every index, after
each publication and in prune, and a reader that loses the race anyway
reads the pointer again. With StaleIndexPolicy.serveWhileRefreshing, a search
during a refresh serves the last published generation even when the
bundle has changed since, with notices. forget and forgetProjectRoot
delete an index with its lock file and the directories that leaves empty;
prune deletes a lock file only once it has gone unused for its period.
The store never locates a model, downloads anything or loads a native
library itself: encoder supplies encoders (ADR-0018). okf reads each
bundle under its .okf.lock, which does not exclude isolates, so a
process keeps all okf bundle I/O, this store's included, on one isolate.
Failures are WayfinderExceptions with a stable code:
bundle-missingwhen a bundle root is not a directory;data-directory-unavailable, ordata-directory-invalidwhen the data directory is inside the bundle;index-busywhen another operation is indexing or forgetting the bundle's index, for index, forget and forgetProjectRoot, and for a search with StaleIndexPolicy.serveWhileRefreshing while the first index of a bundle is being built or an unusable one replaced; on Windows also when readers keep thecurrentpointer open for seconds while index publishes or forget deletes it;index-missing,index-staleorindex-incompatiblewhen search finds no usable index; status reports the same as a value;bundle-changedwhen the bundle changed while an operation read it;model-missingormodel-invalidfrom the encoder, andmodel-invalidwhen an opened encoder reports another identity;cancelledwhen an index run or a search was cancelled;closedafter dispose.
A project search prefixes each message but cancelled's with the
failing bundle's root. Messages are sentences for a reader and never name
a command; a host says how to index.
A host's tests may extend this class to replace operations; tests that
index for real use package:wayfinder_embeddings/testing.dart.
Constructors
- WayfinderKnowledgeStore({required Directory dataDirectory, required WayfinderEncoderProvider encoder, bool retainEncoder = false, int cacheBytes = 64 << 20, Duration gracePeriod = const Duration(minutes: 10), Duration? encoderIdleTimeout})
-
A store over
dataDirectory, usuallyWayfinderDataDirectory.resolve().
Properties
- cacheBytes → int
-
final
- dataDirectory → Directory
-
final
- encoder → WayfinderEncoderProvider
-
final
- encoderIdleTimeout → Duration?
-
How long a retained encoder stays open after the last operation that
used it; null keeps it until dispose or releaseEncoder.
final
- gracePeriod → Duration
-
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- retainEncoder → bool
-
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
dispose(
) → Future< void> - Rejects new operations, waits for accepted ones, then disposes a retained encoder. Later calls return the same future.
-
forget(
ProjectBundle bundle) → Future< void> -
Deletes every configuration's index of
bundle, whose root may no longer exist, such as a removed worktree, with itswriter.lockand the directories that leaves empty. Throwsindex-busy, and deletes nothing, while an operation indexes any of them or acurrentpointer cannot be removed; a search reading one meanwhile then reports it missing. -
forgetProjectRoot(
String root) → Future< List< String> > -
Deletes every index of every bundle at or inside the project
root, whose directory may no longer exist, such as a removed worktree, with theirwriter.locks and the directories that leaves empty, as forget does for one bundle. Returns the canonical roots of the bundles whose indexes it deleted, sorted. -
index(
ProjectBundle bundle, {bool force = false, void onProgress(IndexProgress progress)?, WayfinderCancellation? cancel}) → Future< WayfinderIndexResult> -
Creates or refreshes
bundle's index. A current index returns without opening an encoder;forcediscards it and re-embeds every passage. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
prune(
{Duration unusedFor = const Duration(days: 30)}) → Future< KnowledgePruneResult> -
Deletes what no bundle will use again: every index of a root that no
longer exists, and indexes of other configurations or encoders that no
operation used for
unusedFor. Indexes an operation is writing are left for a later prune. Indexes earlier releases wrote elsewhere underindexes/are never touched. -
releaseEncoder(
) → Future< void> - Releases the retained encoder now and keeps the parsed snapshots, so a host can free the model's memory without losing them. Operations that use the encoder finish with it; it is disposed once they have, and the next operation that needs an encoder opens one again. Completes once the encoder is disposed, with the error its disposal threw, if any. Does nothing when no encoder is retained, such as without retainEncoder; an encoder still opening is kept.
-
search(
List< ProjectBundle> bundles, String query, {int limit = 5, KnowledgeMetadataFilter? filters, StaleIndexPolicy stale = StaleIndexPolicy.refuse, WayfinderCancellation? cancel}) → Future<KnowledgeProjectSearch> -
Searches the saved indexes of
bundleswith one query vector and ranks their passages together under onelimit, from 1 to 100. -
searchBundle(
ProjectBundle bundle, String query, {int limit = 5, KnowledgeMetadataFilter? filters, StaleIndexPolicy stale = StaleIndexPolicy.refuse, WayfinderCancellation? cancel}) → Future< KnowledgeBundleSearch> -
Searches the saved index of one
bundle, with its own context policy rather than a project's shared ranking.staleandcancelare as for search. -
status(
ProjectBundle bundle) → Future< KnowledgeIndexStatus> -
The saved index of
bundlecompared with the bundle as it is now: KnowledgeIndexRefreshing while another operation indexes or forgets it. Holds no lock, probing the writer's, and opens no encoder. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited