Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
8.1 KB
Raw Blame

Key files: Core Services (2/3, more)

Subsystem index · Previous entries. Read only the entries relevant to your change. Current behavior and load-bearing invariants; history belongs in Git and CHANGELOG.

  • src/core/ops/search.ts — the search-domain op module (search, query, search_stats, search_modes, search_tune, cache_stats). Both resolve their source scope through ops/search-source-scope.ts and return no rows when that scope is empty. query is the hybrid entry (hybridSearchCached) plus two non-hybrid legs that share ONE effective-row-contract helper, resolveEffectiveLimit(ctx, p): an explicit limit wins, otherwise the mode-derived searchLimit resolved through the same trust-gated chain hybridSearch uses (resolvePerCallMode ignores a remote caller's mode, so a remote client cannot select the tokenmax row count); the config reads run lazily on those paths only. The legs: the image-similarity branch (embedding_image vector search, test/query-image-mode-limit.serial.test.ts) and the CRAG escalation slice — when search.crag_escalation is on and shouldEscalateRetrieval (src/core/search/crag.ts) says the first pass graded weak, was not already escalated, AND did not already run with the caller's expansion on (callerExpanded), the op re-runs once at limit: max(effectiveLimit, 50) with expansion + relational on and autocut off, keeping the better-graded run. buildRetrievalResponseMeta(ctx, scope, …) is the ONE producer of the retrieval response-meta channel (keyword-only path included): when a remote caller's scope holds pages of any kind below the safe-chunk index version (hasUnsealedPagesInScope: sargable chunker_version < SAFE_FENCE_CHUNKER_VERSION LIMIT 1 probe served by the unsealed-only partial pages_safe_chunk_pending_idx — NOT the COALESCE safeChunksFilter form; pinned by test/search-safe-index-probe.test.ts; fail-open; retains source, private, quarantine and live-page policy), it appends the safe_index_pending degraded stage to empty and partial results alike, so neither reads as a clean or complete answer; the drain is gbrain repair safe-chunks. The withholding itself is unchanged. Empty and nonempty replies also carry projection_readiness, with projection_pending/projection_status_unknown degradation when appropriate (projection_by_design when the only unsealed pages in scope are protected-OCR images, #6803). Vector candidate incompleteness and scoped pool details survive the public CLI/MCP serialization boundary; public type filters constrain the current-projection probe. Retrieval output copies and sanitizes result/metadata after grading and capture using the canonical scanner, before snippet truncation; internal pipeline/capture remains unredacted. searchOutput then projects the returned rows to the caller's row shape (src/core/search/lean-rows.ts: lean for remote callers unless fields: "full", the transport chose full rows, or the caller is trusted-local; a lean row keeps id only when ctx.isCallable('fetch'), the MCP dispatch predicate buildOperationContext threads in, says the caller can call fetch (unset keeps it), and prints score at 4 significant digits) and reports it as retrieval.rows for remote callers and whenever fields is passed. The CLI formatter repeats this on JSON, explain and human output. System One: the query op asks S4 answerability (decide.answerability, diagnostic only) and surfaces meta.answerability; the crag escalation re-run keeps S2-S5 off (rerankOnly). Each handler times its request (SearchTimings, search/stage-timings.ts) and searchOutput emits retrieval.timings last. The typed-search type check lives in ops/search-type-filter.ts (reconcileTypeFilter): one statement of per-type EXISTS probes over each type's schema-expanded set, with the DISTINCT type list for the type_filter_notice evaluated only when a probe misses, run jitOff and unprepared (a generic plan scans every page per missing type); pinned by test/search-type-filter-reconcile.test.ts and test/e2e/typed-search-jit-postgres.test.ts. buildRetrievalResponseMeta also stamps stale on hits whose page's newer file is held, adds held_files per source in scope and emits the held_files degraded notice (persistence/held-reads.ts; Test: test/held-read-signals.test.ts); coverage counts pending, held and embedding-held items with could_affect (search/retrieval-coverage.ts). Its safe-index probe, projection readiness, entity saved facts, saved-fact match and held-file coverage run concurrently, each with its own catch; the answer-feedback write runs after them (test/search-response-meta-concurrency.test.ts). The saved-fact match is one statement: an EXISTS probe skips its scan (idx_facts_fts; LIKE ANY for CJK) when no fact qualifies. The handlers start the alias fan-out's resolveQueryEntity alongside the hybrid search, and evidenceOutput reads page trust (loadPageTrust over the rows every delivered row is copied from) alongside evidence delivery (test/search-post-rerank-concurrency.test.ts). The starter list leaves out (fullSurfaceOnly, still accepted) mode, snippet_chars, return_window, token_budget, salience, recency, fields, and on query image/image_mime/embedding_column, the code params, detail, adaptive_return, autocut and relational.

  • src/core/ops/search-source-scope.ts — per-request scope of search/query: source_id through federatedSearchScope, narrowed by source_ids (parseSourceIdsParam: trimmed, deduped, validated; __all__ refused; [] or a blank string is absent, #5390). Each listed id is authorized like an explicit source_id (out-of-grant: permission_denied; non-live: unknown_source); with both params the scope is their intersection, and an empty one (sourceIds: []) emits empty_retrieval and nothing is searched. Tests: test/search/search-source-ids.test.ts (+ its test/e2e/ Postgres twin), test/e2e/serve-http-member-tokens.test.ts.

  • src/core/ops/sources.ts — sources_remove is confined to the caller's WRITE authority via assertSourceInCallerWriteScope(ctx, id) (context.ts): an untrusted caller may hard-remove only its own write source; a federated read grant naming a source does not make it removable; any other id answers OperationError('not_found', 'Unknown source: <id>'), byte-identical to a nonexistent source so the error is not an existence oracle. sources_status keeps the read-scope helper (assertSourceInCallerScope), so a federated grant can still diagnose a source it cannot remove. Trusted local CLI keeps the full operator view. Over HTTP every client carries a write source (registration default default) and its own oauth_clients.source_id row FK-RESTRICTs removing that source, so hard removal is effectively an operator action (gbrain sources remove); a source a remote sources_admin caller just created via sources_add stays outside its authority until the operator rescopes the client. There is no sources_archive op — archive lives in the CLI (gbrain sources archive). Pinned by test/sources-mcp.test.ts (federated-read-only id → not_found while status still answers; write-source id → removed; unbound → any; local → any; remote: undefined fail-closed) and test/e2e/sources-remote-mcp.test.ts (real server: not_found before AND after a read-grant widen; operator CLI completes; rescoping --source moves the authority).

  • src/core/page-summary.ts — generatePerChunkSynopsis: the per-chunk synopsis call (utility-tier default, thinking: 'off', SYNOPSIS_DOC_MAX_CHARS surrogate-safe head truncation, D27 failure envelope). One user message of TWO text blocks (PR #6341): the stable title + document block flagged cache: true (the only prefix every chunk of a page shares) and the per-chunk block; joined with \n they equal the former single string, so SYNOPSIS_PROMPT_VERSION stays 1. Pinned by test/page-summary-prompt-cache.test.ts.

  • src/core/notifier/ + src/core/commit-notifier.ts — the commit notifier, wanted markers, residents and search freshness: Commit notifier.