Garry's Opinionated OpenClaw/Hermes Agent Brain
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 forpinned_questions(one row per pin: question, scope, state, publish mode, the answer as JSON sentences, watermark, durable lease, spend) andquestion_evidence(per-sentence pointers: kindpage|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 ofsrc/schema.sqlviascripts/build-schema.ts; applied by thepinned_questionsschema 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_questionfrontmatter key) andsentenceId. Pure; imported by the migration handler.src/core/questions/store.ts— Row reads, idempotentinsertPin(ON CONFLICT DO NOTHING),updatePinState(bumpsrevisionso an in-flight refresh fails revalidation), and the durable lease (acquireLeasetakes a free or expired lease;releaseLeaserecords the attempt's error and spend). PlainexecuteRawon side tables the canonical writer guard does not cover.src/core/questions/freshness.ts— Read-time staleness.evaluateAnswerjoins the current answer's evidence pointers againstpages,sources,facts,timeline_entriesandtakeson every read: missing row, soft delete, archived source, generation or knowledge-revision change, fact expiry byvalid_untilwith 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.needsFullflags reasons an incremental edit cannot repair. Hash expressions (FACT_HASH_SQLetc.) are shared with capture and commit.src/core/questions/refresh.ts—refreshPin: no-spend pre-checks (inactive,no_model_keyviaprobeChatModel,budget_exhaustedvia the caller'sBudgetMeter), 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 orfull), 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 recordsrefresh_failed:<stage>. The output cap iscycle.standing_questions.max_output_tokens(default 2,000); a reply cut off at it (alengthstop or claude-cli's output-token-maximum error) recordsrefresh_failed:answer_too_long:<cap>with no JSON retry (#7306).RefreshHooksare test-only injection points.src/core/questions/pages.ts— The question page: question, scope and the owner's notes, never the answer.syncQuestionPagecreates, archives or restores it through coordinatedput_page(trusted local callers only).ownerClaimsturns 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: raisecycle.standing_questions.max_output_tokens),refresh_failed,no_worker),fix(an agent-operatorActionwith a read-onlyverify) and the answer for owner-capable readers.scheduledRefreshAvailable(astanding_questionsrun in the last two days, or installed unpaused autopilot) decidesno_worker.src/core/questions/service.ts— The operations' behavior.requireOwnerCapableadmits the trusted local CLI and remote grants that read private pages and are neither slug-fenced nor delegated; everyone else getsquestion_owner_onlywith the CLI equivalent. Consent: CLI pins are active; MCP pins are inactive (awaiting_consent) unless a paid preapproval exists;pin --idon the host activates. First answer is a bounded wait (wait_ms, abort releases the lease).pinnedAnswersForPackfeeds context_pack's optionalpinned_questions/withheldfields (published, active, entity-scoped pins whose(source_id, scope_entity)is one of the pack's accepted cards; fresh sentences only). Emits thepinned_answer_stalenotice.src/core/questions/phase.ts— Cycle phasestanding_questions(global, writes): syncs question pages, refreshes due active pins (never answered, then stale, then new evidence since the watermark; cooldown per pin orcycle.standing_questions.cooldown_days) under oneBudgetMeter(cycle.standing_questions.budget_usd) andmax_per_cycle, stops onbudget_exhaustedorno_model_key, skips (reportedblocked:answer_too_long) a pin whose last refresh did not fit the current output cap until the cap is raised, and stampscycle.standing_questions.last_run_at. Kill switchcycle.standing_questions.enabled=false.src/core/questions/auto-think-migration.ts— Maps the sevendream.auto_thinkkeys onto pins without creating consent (every pin imports inactive:migrated_disabled,migrated_zero_budgetormigrated_enabled;auto_commit=false→ draft; cooldown and model per pin; budget, max and allow_unpriced seedcycle.standing_questions.*only when unset). Config reads and inserts only; idempotent.autoThinkReplacementis whatgbrain config get|setprints for an old key.src/core/ops/questions.ts—questions_pin,questions_refresh,questions_unpin(write,writeInferenceexplicit_llm/none) andquestions_list,questions_status(read). On the full surface registrations pin (notSTARTER_OPS). CLI equivalents insrc/core/ops/cli-equivalent.ts.src/commands/questions.ts—gbrain questions pin|list|status|refresh|unpinthroughhandleToolCall(trusted local); exit 1 when the answer is blocked or a refresh did not publish.Privacy and retrieval seams:
src/core/search/private-visibility.tstreatstype: questionwithout a visibility as private and any page with thepinned_questionmarker as private whatever itsvisibility;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); thestanding_questionsrow oftest/managed-phase-matrix.test.ts.