Documentation
¶
Overview ¶
Package vfsindex bridges a vfs.MountSession into brain knowledge objects.
Canonical architecture: docs/knowledge.md. This package is only the artifact ingest path (IndexPath / policy / schedulers).
VFS and brain work alone or together. This package is the optional composition layer: it imports both, while vfs and brain never import each other.
Model ¶
Each text-like virtual file becomes a brain Document (parent) with Chunk parts. The row contract (ids, property types, content_hash, and replace rules) is "File mirror rows" in docs/knowledge.md. In-process writers should call IndexPath or DocumentID rather than insert rows themselves.
Document id = UUID v5, namespace a1b2c3d4-e5f6-7890-abcd-ef1234567890,
name = Namespace.String() + 0x00 + virtual path
Document.properties.vfs_path, content_hash, size, mtime, media_type
Chunk id = UUID v5 under the Document id, name "chunk:<position>"
or "block:<block id>"
Chunk.properties.start_line, end_line, byte_start, byte_end
Chunk.properties.block_id, heading_path = the block id, when structured
Chunk.Content = chunk body
Replacing a file puts the parent, soft-deletes the previous chunks, then puts the new chunks. content_hash is lowercase hex SHA-256. The same hash skips the write. Live file bytes stay the source of truth.
Index policy (MountSpec.IndexPolicy) ¶
none — index_file errors selective — index_file / host IndexPath prefix — host may walk the mount with IndexPrefix watch — same host hint as prefix
Empty policy normalizes to selective (NormalizePolicy / AutoIndex helpers). AutoIndex is for a pipeline the host builds. The turn does not walk mounts, subscribe to remote changes, or re-index after WriteFile.
Single pipeline ¶
File bytes become brain chunks only through IndexPath (or UnindexPath). The turn calls that from index_file and unindex. A host calls IndexPath or IndexPrefix from its own system. content_hash skip returns PathSkipped without re-chunking.
Session-visible body ¶
IndexPath uses MountSession.ReadText (markdown) and MountSession.Open (other text). Writes are write-through, so a later index_file sees the last persist.
Schedulers ¶
SyncScheduler runs IndexPath inline. AsyncScheduler enqueues with coalesce (last reason wins), a bounded pending set, and a background worker. Notify never blocks on re-chunk. Start creates an AsyncScheduler and does not call Notify.
A turn with a brain, a workspace, and a search namespace starts a Bridge and registers index_file / unindex. Mounts with IndexPolicy=none are skipped by index_file. Keeping those chunks current after the sandbox exits is the host's storage pipeline, not this package.
Kinds ¶
Hosts that use a non-empty kind catalog should register MountIndexKinds() (or equivalent fields) before indexing. Open-catalog engines accept any props.
Content search over mounts is brain search/find_exact on Chunks with vfs_path. Live grep is run_command → rg on the FUSE tree. This package does not implement grep.
Index ¶
- Constants
- Variables
- func AutoIndex(policy string) bool
- func MountIndexKinds() []brain.KindSpec
- func NormalizePolicy(raw string) string
- type AsyncScheduler
- type Bridge
- type IndexOpts
- type IndexReason
- type IndexScheduler
- type MountIndexer
- func (x *MountIndexer) DocumentID(virtualPath string) uuid.UUID
- func (x *MountIndexer) IndexFileResult(ctx context.Context, virtualPath string, st vfs.FileInfo) (PathIndexResult, error)
- func (x *MountIndexer) IndexPath(ctx context.Context, virtualPath string) error
- func (x *MountIndexer) IndexPathResult(ctx context.Context, virtualPath string) (PathIndexResult, error)
- func (x *MountIndexer) IndexPrefix(ctx context.Context, prefix string, opts IndexOpts) (Stats, error)
- func (x *MountIndexer) UnindexPath(ctx context.Context, virtualPath string) (bool, error)
- type PathIndexResult
- type SchedulerEvent
- type Stats
- type SyncScheduler
Constants ¶
const ( DefaultLinesPerChunk = 40 DefaultMaxIndexBytes = 64 << 20 // 64 MiB per file )
Defaults for streaming index.
const ( PropVFSPath = "vfs_path" PropSize = "size" PropMTime = "mtime" PropContentHash = "content_hash" PropMediaType = "media_type" PropStartLine = "start_line" PropEndLine = "end_line" PropByteStart = "byte_start" PropByteEnd = "byte_end" PropBlockID = "block_id" PropHeadingPath = "heading_path" )
Property keys written on mount-indexed objects.
const ( DefaultDocumentKind = "Document" DefaultChunkKind = "Chunk" )
Default kind names (host may override on MountIndexer).
const ( PolicyNone = "none" PolicySelective = "selective" PolicyPrefix = "prefix" PolicyWatch = "watch" )
Index policy values stored on vfs.MountSpec.IndexPolicy. Empty policy normalizes to PolicySelective.
const ( DefaultAsyncQueueCap = 64 DefaultAsyncTimeout = 2 * time.Minute )
Defaults for AsyncScheduler.
Variables ¶
var ( // ErrSchedulerClosed means Notify was called after shutdown. ErrSchedulerClosed = errors.New("vfsindex: scheduler closed") // ErrQueueFull means a distinct path could not be queued. ErrQueueFull = errors.New("vfsindex: scheduler queue full") )
Functions ¶
func AutoIndex ¶
AutoIndex reports whether a host-built pipeline would treat the policy as automatic. prefix and watch return true. The turn does not run that pipeline.
func MountIndexKinds ¶
MountIndexKinds returns KindSpecs for catalog mode when indexing mounts. Fields are optional so pure knowledge Documents/Chunks without vfs_path remain valid.
func NormalizePolicy ¶
NormalizePolicy returns a canonical policy string. Unknown or empty values become PolicySelective.
Types ¶
type AsyncScheduler ¶
type AsyncScheduler struct {
Indexer *MountIndexer
QueueCap int // max distinct pending paths; default DefaultAsyncQueueCap
Timeout time.Duration // per-path IndexPath timeout; default DefaultAsyncTimeout
// contains filtered or unexported fields
}
AsyncScheduler re-indexes paths on background worker(s). Notify is non-blocking: it enqueues the path (duplicates coalesce) and returns immediately. Under pressure (queue full and path not already pending), the notify is dropped. Close cancels in-flight work and stops the worker.
func NewAsyncScheduler ¶
func NewAsyncScheduler(idx *MountIndexer) *AsyncScheduler
NewAsyncScheduler starts a single background worker. Call Close on teardown.
func (*AsyncScheduler) Close ¶
func (s *AsyncScheduler) Close() error
Close stops the worker and cancels any in-flight IndexPath. Safe to call once or more; subsequent calls are no-ops.
func (*AsyncScheduler) Notify ¶
func (s *AsyncScheduler) Notify(ctx context.Context, virtualPath string, reason IndexReason) error
Notify implements IndexScheduler. Never blocks on IndexPath. The caller's ctx is not used for enqueue: AfterPersist and similar short-lived contexts must not cancel pending work. IndexPath runs under the scheduler's own cancel + Timeout context.
func (*AsyncScheduler) SetObserver ¶
func (s *AsyncScheduler) SetObserver(observer func(SchedulerEvent))
SetObserver installs a non-blocking host failure observer.
type Bridge ¶
type Bridge struct {
Indexer *MountIndexer
// contains filtered or unexported fields
}
Bridge is the explicit file indexer for one mount session: IndexPath and the selective track set. It does not watch the mount, walk it at start, or re-index after a write. A host that keeps the brain current does that in its own system.
func Start ¶
Start builds an indexer and an idle async scheduler. It does not register AfterPersist and it does not read the mount.
func (*Bridge) PolicyAt ¶
PolicyAt is the normalized IndexPolicy for a virtual path (selective if unknown).
func (*Bridge) SetObserver ¶
func (b *Bridge) SetObserver(observer func(SchedulerEvent))
SetObserver replaces the asynchronous indexing failure observer.
func (*Bridge) ShouldIndex ¶
ShouldIndex reports whether a host pipeline should enqueue path. The turn does not call this after writes.
type IndexOpts ¶
type IndexOpts struct {
// MaxFiles stops after this many files opened (0 = unlimited).
MaxFiles int
}
IndexOpts configures a tree walk.
type IndexReason ¶
type IndexReason int
IndexReason explains why a path was scheduled for re-index. Notify accepts a reason for host/API clarity; IndexPath does not branch on it.
const ( // ReasonSync is a successful VFS WriteFile or Sync (AfterPersist bridge). ReasonSync IndexReason = iota // ReasonExplicit is a host or tool request (index_file / IndexPath). ReasonExplicit )
type IndexScheduler ¶
type IndexScheduler interface {
Notify(ctx context.Context, virtualPath string, reason IndexReason) error
}
IndexScheduler receives path invalidations. SyncScheduler runs inline; AsyncScheduler enqueues work with the same interface.
type MountIndexer ¶
type MountIndexer struct {
VFS *vfs.MountSession
Brain *brain.Engine
Scope brain.Scope
DocumentKind string // default Document
ChunkKind string // default Chunk
LinesPerChunk int // default DefaultLinesPerChunk
MaxIndexBytes int64 // default DefaultMaxIndexBytes
// contains filtered or unexported fields
}
MountIndexer streams text-like mount files into brain Document + Chunk objects.
func NewMountIndexer ¶
func NewMountIndexer(ms *vfs.MountSession, eng *brain.Engine, scope brain.Scope) (*MountIndexer, error)
NewMountIndexer validates required fields. Scope.Namespace must be set (brain Put requires namespace attrs).
func (*MountIndexer) DocumentID ¶
func (x *MountIndexer) DocumentID(virtualPath string) uuid.UUID
DocumentID returns the stable brain id for a virtual path under this scope.
func (*MountIndexer) IndexFileResult ¶
func (x *MountIndexer) IndexFileResult(ctx context.Context, virtualPath string, st vfs.FileInfo) (PathIndexResult, error)
IndexFileResult indexes a path already known to exist (caller Stat'd). It does not Stat again, so batch tools can validate once then index.
func (*MountIndexer) IndexPath ¶
func (x *MountIndexer) IndexPath(ctx context.Context, virtualPath string) error
IndexPath indexes one virtual file (or removes the brain mirror if missing).
func (*MountIndexer) IndexPathResult ¶
func (x *MountIndexer) IndexPathResult(ctx context.Context, virtualPath string) (PathIndexResult, error)
IndexPathResult indexes one path and returns a compact outcome for tools/hosts.
func (*MountIndexer) IndexPrefix ¶
func (x *MountIndexer) IndexPrefix(ctx context.Context, prefix string, opts IndexOpts) (Stats, error)
IndexPrefix walks a directory (or single file) and indexes text-like files.
func (*MountIndexer) UnindexPath ¶
UnindexPath soft-deletes the brain Document/Chunks for virtualPath without touching the VFS file. Returns true when a mirror was present and removed, false when nothing was indexed (idempotent noop).
type PathIndexResult ¶
type PathIndexResult string
PathIndexResult is a compact outcome of indexing one path (indexed|skipped|removed|directory). Used by agent tools and hosts.
const ( // PathIndexed means Document/Chunks were written or re-chunked. PathIndexed PathIndexResult = "indexed" // PathSkipped means hash match, binary, non-text, or PolicyNone. PathSkipped PathIndexResult = "skipped" // PathRemoved means the path was missing and any brain mirror was soft-deleted. PathRemoved PathIndexResult = "removed" // PathDirectory means the path is a directory (IndexPath is a no-op for dirs). PathDirectory PathIndexResult = "directory" )
type SchedulerEvent ¶
type SchedulerEvent struct {
Path string
Reason IndexReason
Err error
}
SchedulerEvent reports an asynchronous indexing outcome to the host.
type Stats ¶
type Stats struct {
Indexed int // files written or re-chunked
Skipped int // hash match, binary, non-text, or PolicyNone
Removed int // missing paths soft-deleted from brain
}
Stats summarizes IndexPrefix work.
type SyncScheduler ¶
type SyncScheduler struct {
Indexer *MountIndexer
}
SyncScheduler re-indexes immediately on Notify (v1).
func NewSyncScheduler ¶
func NewSyncScheduler(idx *MountIndexer) *SyncScheduler
NewSyncScheduler returns an IndexScheduler that calls IndexPath inline.
func (*SyncScheduler) Notify ¶
func (s *SyncScheduler) Notify(ctx context.Context, virtualPath string, reason IndexReason) error
Notify implements IndexScheduler.