Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
7.5 KB
Raw Blame

Key files: Core Questions (pinned questions)

Subsystem index. Read only the entries relevant to your change. Current behavior and load-bearing invariants; history belongs in Git and CHANGELOG. User guide: docs/guides/pinned-questions.md.

  • src/core/questions/schema.ts — DDL for pinned_questions (one row per pin: question, scope, state, publish mode, the answer as JSON sentences, watermark, durable lease, spend) and question_evidence (per-sentence pointers: kind page|fact|timeline|take|owner, page id, page generation and knowledge revision, item id, content hash). No foreign keys, so a hard delete or cascade leaves a dangling pointer that reads as stale. Fragment of src/schema.sql via scripts/build-schema.ts; applied by the pinned_questions schema migration.

  • src/core/questions/identity.ts — Source-qualified ids (<source_id>:questions/<slug>) and deterministic slugs (kebab of the question plus a hash of the case-folded question, slug prefix and entity), so pinning the same question with the same scope is idempotent. PINNED_QUESTION_MARKER (pinned_question frontmatter key) and sentenceId. Pure; imported by the migration handler.

  • src/core/questions/store.ts — Row reads, idempotent insertPin (ON CONFLICT DO NOTHING), updatePinState (bumps revision so an in-flight refresh fails revalidation), and the durable lease (acquireLease takes a free or expired lease; releaseLease records the attempt's error and spend). Plain executeRaw on side tables the canonical writer guard does not cover.

  • src/core/questions/freshness.ts — Read-time staleness. evaluateAnswer joins the current answer's evidence pointers against pages, sources, facts, timeline_entries and takes on every read: missing row, soft delete, archived source, generation or knowledge-revision change, fact expiry by valid_until with no write, withdrawal (expired_at), supersession, content-hash change, inactive take and owner edits each mark the sentence stale; a sentence with no pointer or an unknown kind is stale (fail closed); a pointer outside the reader's sources hides the sentence. needsFull flags reasons an incremental edit cannot repair. Hash expressions (FACT_HASH_SQL etc.) are shared with capture and commit.

  • src/core/questions/refresh.ts — refreshPin: no-spend pre-checks (inactive, no_model_key via probeChatModel, budget_exhausted via the caller's BudgetMeter), lease, capture (pin revision, question page, global page-generation clock and newest fact id as the watermark), retrieveEvidence (hybrid retrieval with keyword fallback, plus facts, timeline rows and takes anchored to the retrieved pages; question and synthesis pages, marker pages and pages that repeat a published answer sentence after it was published are never evidence), one model call (incremental edit of the previous answer, or full recompute after deletions, withdrawals, conflicting corrections, non-monotonic questions or full), then one transaction that locks the pin and every cited row (FOR UPDATE/FOR SHARE), revalidates lease, revision and dependency revisions, writes the answer and replaces the evidence rows. A mismatch publishes nothing (conflict); any failure keeps the previous answer and records refresh_failed:<stage>. The output cap is cycle.standing_questions.max_output_tokens (default 2,000); a reply cut off at it (a length stop or claude-cli's output-token-maximum error) records refresh_failed:answer_too_long:<cap> with no JSON retry (#7306). RefreshHooks are test-only injection points.

  • src/core/questions/pages.ts — The question page: question, scope and the owner's notes, never the answer. syncQuestionPage creates, archives or restores it through coordinated put_page (trusted local callers only). ownerClaims turns the body (minus heading and comments) into owner-attributed sentences, pinned to the body's hash.

  • src/core/questions/receipt.ts — buildReceipt: freshness (fresh|stale|awaiting_refresh|refreshing|archived), sentence counts, evidence watermark, last refresh, blocked reason (awaiting_consent, no_model_key, budget_exhausted, answer_too_long (fix: raise cycle.standing_questions.max_output_tokens), refresh_failed, no_worker), fix (an agent-operator Action with a read-only verify) and the answer for owner-capable readers. scheduledRefreshAvailable (a standing_questions run in the last two days, or installed unpaused autopilot) decides no_worker.

  • src/core/questions/service.ts — The operations' behavior. requireOwnerCapable admits the trusted local CLI and remote grants that read private pages and are neither slug-fenced nor delegated; everyone else gets question_owner_only with the CLI equivalent. Consent: CLI pins are active; MCP pins are inactive (awaiting_consent) unless a paid preapproval exists; pin --id on the host activates. First answer is a bounded wait (wait_ms, abort releases the lease). pinnedAnswersForPack feeds context_pack's optional pinned_questions / withheld fields (published, active, entity-scoped pins whose (source_id, scope_entity) is one of the pack's accepted cards; fresh sentences only). Emits the pinned_answer_stale notice.

  • src/core/questions/phase.ts — Cycle phase standing_questions (global, writes): syncs question pages, refreshes due active pins (never answered, then stale, then new evidence since the watermark; cooldown per pin or cycle.standing_questions.cooldown_days) under one BudgetMeter (cycle.standing_questions.budget_usd) and max_per_cycle, stops on budget_exhausted or no_model_key, skips (reported blocked:answer_too_long) a pin whose last refresh did not fit the current output cap until the cap is raised, and stamps cycle.standing_questions.last_run_at. Kill switch cycle.standing_questions.enabled=false.

  • src/core/questions/auto-think-migration.ts — Maps the seven dream.auto_think keys onto pins without creating consent (every pin imports inactive: migrated_disabled, migrated_zero_budget or migrated_enabled; auto_commit=false → draft; cooldown and model per pin; budget, max and allow_unpriced seed cycle.standing_questions.* only when unset). Config reads and inserts only; idempotent. autoThinkReplacement is what gbrain config get|set prints for an old key.

  • src/core/ops/questions.ts — questions_pin, questions_refresh, questions_unpin (write, writeInference explicit_llm / none) and questions_list, questions_status (read). On the full surface registrations pin (not STARTER_OPS). CLI equivalents in src/core/ops/cli-equivalent.ts.

  • src/commands/questions.ts — gbrain questions pin|list|status|refresh|unpin through handleToolCall (trusted local); exit 1 when the answer is blocked or a refresh did not publish.

  • Privacy and retrieval seams: src/core/search/private-visibility.ts treats type: question without a visibility as private and any page with the pinned_question marker as private whatever its visibility; buildVisibilityClause (src/core/search/sql-ranking.ts) keeps marker pages out of every search arm for every caller.

  • Tests: test/helpers/pinned-questions-scenarios.ts (B5 safety gate, operator journeys, migration) runs on PGLite (test/pinned-questions-safety.test.ts) and Postgres (test/e2e/pinned-questions-postgres.test.ts); test/questions-cli.test.ts; test/pinned-questions-answer-too-long.test.ts (output cap, #7306); test/pinned-questions-benefit-gate.test.ts (offline harness plumbing); the standing_questions row of test/managed-phase-matrix.test.ts.