openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

feat(learning): explicit user-preference tool + always-on pinned-facet prompt injection

Open sanil-23 opened this issue 6 months ago • 2 comments

Summary

  • Adds a first-class remember_preference agent tool so the model can persist user-stated preferences (e.g. "always be terse", "I use pnpm") as deterministic, pinned profile facets.
  • Adds an always-on prompt-injection path for pinned preferences via a new learning.explicit_preferences_enabled flag (default true), independent of the inference-based learning.enabled master switch (default false).
  • Three-path fetch_learned_context (both-off / explicit-only / full-learning) so explicit preferences surface even when the probabilistic learning subsystem is disabled.
  • Pure Rust core change: 9 files, ~905 insertions, 1 deletion. No frontend, no new RPC controllers, inference stack (reflection/heuristics/stability detector) untouched.

Problem

  • The learning subsystem ranks CueFamily::Explicit (a user directly stating a preference) as the highest-trust signal, but there was no deterministic path to capture it — explicit preferences were only picked up probabilistically by the LLM summariser / heuristic detectors, all gated behind learning.enabled (default off).
  • Net effect: out of the box, a user explicitly telling the agent a preference was never reliably persisted or surfaced into the prompt.

Solution

  • RememberPreferenceTool maps a user-stated preference to a (FacetClass, key, value) and upserts it as a pinned facet through the existing facet store — deterministic, idempotent (re-setting the same (class,key) overwrites; no duplicates), with no dependence on the inference engine.
  • New config flag learning.explicit_preferences_enabled (default true) + OPENHUMAN_LEARNING_EXPLICIT_PREFERENCES_ENABLED env override gates an always-on UserProfileSection injection path, separate from learning.enabled.
  • Design decision/tradeoff: explicit preferences are deliberately decoupled from the stability/decay/reflection machinery — user-authoritative pinned facts should not have to "earn" stability or be subject to inference noise.

Submission Checklist

If a section does not apply to this change, mark the item as N/A with a one-line reason. Do not delete items.

  • [x] Tests added or updated (happy path + at least one failure / edge case) — 16 remember_preference tests (arg/class validation, idempotent overwrite / no-duplicate edge) + 3 fetch_learned_context path tests.
  • [x] N/A: Diff coverage ≥ 80% — diff-cover/coverage tooling not runnable in this ephemeral worktree (no node_modules); new code is unit-tested and CI coverage.yml is the authoritative gate.
  • [x] N/A: Coverage matrix updated — not updated in this PR; flagged as a follow-up TODO (new remember_preference tool row).
  • [x] All affected feature IDs from the matrix are listed in ## Related — N/A: no existing matrix rows touched; none to list.
  • [x] No new external network dependencies introduced — confirmed: tool uses the existing in-process memory store; no network.
  • [x] N/A: Manual smoke checklist updated — does not touch release-cut UI/release surfaces (core agent tool only).
  • [x] N/A: Linked issue closed via Closes #NNN — no tracked issue; feature originated from a design working session.

Impact

  • Desktop/CLI core only. No data migration. One additive config flag (default-on, but only acts on explicitly-pinned facets, which do not exist until the tool is invoked → no behavioural change for existing users until used).
  • Security: tool writes route through the existing memory store + security wrapper; no new external surface or network deps.

Related

  • Closes: N/A — design-session origin, no tracked issue.
  • Follow-up PR(s)/TODOs: coverage-matrix row for remember_preference; broader STM / memory-pipeline work (separate WIP).

AI Authored PR Metadata (required for Codex/Linear PRs)

Linear Issue

  • Key: N/A
  • URL: N/A

Commit & Branch

  • Branch: feat/explicit-user-preferences
  • Commit SHA: 858f5bc6fd3ddef5604d183766019be476679ed3

Validation Run

  • [x] N/A: pnpm --filter openhuman-app format:check — no frontend files changed.
  • [x] N/A: pnpm typecheck — no frontend/TS changed.
  • [x] Focused tests cargo test --lib -- remember_preference fetch_learned_context: 19 passed, 0 failed (7624 filtered out) — independent re-run on PR head 858f5bc6. (The literal two-positional form cargo test remember_preference fetch_learned_context is invalid cargo CLI syntax; both filters must be passed after --.)
  • [x] Rust fmt/check (if changed): cargo check --manifest-path Cargo.toml → exit 0 (compiles on current main after rebase).
  • [x] N/A: Tauri fmt/check — src-tauri unchanged.

Validation Blocked

  • command: git push (pre-push hook runs pnpm --filter openhuman-app format)
  • error: prettier: command not found — ephemeral worktree has no node_modules
  • impact: Frontend format hook cannot run in this worktree; change is Rust-only (0 frontend files). Pushed with --no-verify per repo policy for unrelated hook failures. CI runs full validation.

Behavior Changes

  • Intended behavior change: model can persist user-stated preferences; pinned preferences are injected into the system prompt regardless of learning.enabled.
  • User-visible effect: none until remember_preference is invoked (no pre-existing pinned facets).

Parity Contract

  • Legacy behavior preserved: inference stack (reflection/heuristics/stability) untouched; learning.enabled semantics unchanged; new path is additive and inert-by-default until the tool is used.
  • Guard/fallback/dispatch parity checks: three-path fetch_learned_context preserves prior behaviour for both-flags-off and full-learning-on.

Duplicate / Superseded PR Handling

  • Duplicate PR(s): None
  • Canonical PR: This
  • Resolution (closed/superseded/updated): N/A

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced "remember preference" tool to explicitly pin user preferences across six categories (style, identity, tooling, veto, goal, channel).
    • Pinned preferences are now injected into the system on every turn, even when learning is disabled.
    • Added configuration option for explicit preferences control via environment variable.
  • Tests

    • Added comprehensive tests for preference pinning and context retrieval.

Review Change Stack

sanil-23 avatar May 18 '26 20:05 sanil-23

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1ea1457f-5088-4b04-903e-d7060237684a

📥 Commits

Reviewing files that changed from the base of the PR and between a4f8d934dc3cffd3143a3662212f6fcd2bcaf3a5 and 37695944e9255a66f8779aafcb46d05d2af68b50.

📒 Files selected for processing (9)
  • src/openhuman/agent/harness/session/builder.rs
  • src/openhuman/agent/harness/session/turn.rs
  • src/openhuman/agent/harness/session/turn_tests.rs
  • src/openhuman/agent/harness/session/types.rs
  • src/openhuman/config/schema/learning.rs
  • src/openhuman/config/schema/load.rs
  • src/openhuman/tools/impl/agent/mod.rs
  • src/openhuman/tools/impl/agent/remember_preference.rs
  • src/openhuman/tools/ops.rs
🚧 Files skipped from review as they are similar to previous changes (9)
  • src/openhuman/tools/impl/agent/mod.rs
  • src/openhuman/tools/ops.rs
  • src/openhuman/config/schema/learning.rs
  • src/openhuman/agent/harness/session/turn.rs
  • src/openhuman/agent/harness/session/types.rs
  • src/openhuman/config/schema/load.rs
  • src/openhuman/agent/harness/session/builder.rs
  • src/openhuman/agent/harness/session/turn_tests.rs
  • src/openhuman/tools/impl/agent/remember_preference.rs

📝 Walkthrough

Walkthrough

Adds an explicit-preferences mode: config and agent wiring to enable pinned user preferences independent of learning, conditional fetch logic to return only pinned preferences when configured, a RememberPreferenceTool to persist pinned prefs, tests, and registry integration.

Changes

Explicit Preferences Feature

Layer / File(s) Summary
Configuration Schema and Environment Support
src/openhuman/config/schema/learning.rs, src/openhuman/config/schema/load.rs
New explicit_preferences_enabled: bool field added to LearningConfig with default true, and exposed via OPENHUMAN_LEARNING_EXPLICIT_PREFERENCES_ENABLED environment variable for runtime override.
Agent and Builder Type Definitions
src/openhuman/agent/harness/session/types.rs
Agent and AgentBuilder structs gain explicit_preferences_enabled: bool field to carry the configuration through session construction.
Agent Builder Fluent API and Initialization
src/openhuman/agent/harness/session/builder.rs
AgentBuilder::new() defaults flag to true, new explicit_preferences_enabled(bool) fluent setter, and build() wires flag into constructed Agent.
Session Prompt Wiring and Conditional Injection
src/openhuman/agent/harness/session/builder.rs
UserProfileSection is conditionally injected into session prompt when explicit_preferences_enabled=true and learning_enabled=false; builder instantiation from config applies the configuration flag.
Learned Context Conditional Fetching
src/openhuman/agent/harness/session/turn.rs
fetch_learned_context implements three paths: empty return when both flags off, explicit-preferences-only fetch from user_profile namespace (sanitized, capped to 50) when learning disabled but explicit enabled, and full learning fetch otherwise; adds debug logs.
Learned Context Integration Tests
src/openhuman/agent/harness/session/turn_tests.rs
Integration tests verify fetch_learned_context behavior under three flag combinations with helper constructors for test agents and UnifiedMemory instances.
Remember Preference Tool with Full Test Coverage
src/openhuman/tools/impl/agent/remember_preference.rs, src/openhuman/tools/impl/agent/mod.rs
New RememberPreferenceTool with FacetClass taxonomy, deterministic pinned memory keys/content, JSON schema validation, security policy enforcement, persistence to user_profile namespace, and comprehensive tests covering validation, happy paths, idempotent overwrite, all facet classes, and security gate.
Tool Registry Integration
src/openhuman/tools/ops.rs
RememberPreferenceTool unconditionally registered in all_tools_with_runtime with shared memory and security handles.

Sequence Diagram

sequenceDiagram
  participant User
  participant RememberPreferenceTool
  participant SecurityPolicy
  participant Memory
  User->>RememberPreferenceTool: execute(class, key, value)
  RememberPreferenceTool->>SecurityPolicy: enforce_tool_operation(Act, remember_preference)
  alt Security denied
    SecurityPolicy-->>RememberPreferenceTool: error
    RememberPreferenceTool-->>User: ToolResult::error
  else Security allowed
    SecurityPolicy-->>RememberPreferenceTool: ok
    RememberPreferenceTool->>RememberPreferenceTool: validate class/key/value
    RememberPreferenceTool->>Memory: store(namespace="user_profile", key=pinned/{class}/{key}, content="[pinned] ...", MemoryCategory::Core)
    Memory-->>RememberPreferenceTool: stored
    RememberPreferenceTool-->>User: ToolResult::success
  end

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Suggested labels

working

Suggested reviewers

  • senamakel
  • graycyrus

Poem

🐰 I tuck your preferences safe and neat,
Pinned in memory, steady heartbeat,
Learning rests while tastes stay true,
Small notes that guide each thing you do—
A rabbit keeps them just for you.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and specifically describes the main changes: adding an explicit user-preference tool (RememberPreferenceTool) and enabling pinned-facet prompt injection independent of the learning system.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 18 '26 20:05 coderabbitai[bot]

Test-wiring investigation results

This comment reports the findings from investigating the two alleged test-wiring defects on feat/explicit-user-preferences (HEAD 858f5bc6).


Defect 1: remember_preference tests not running

Finding: already wired and passing. No code change needed.

Root cause of the false "0 tests" diagnosis: the investigation used cargo test --manifest-path Cargo.toml remember_preference (no --lib flag). Without --lib, cargo dispatches the filter to each compiled test binary individually — and the integration-test binaries (json_rpc_e2e, observability_smoke, etc.) all report "0 tests … N filtered out" for remember_preference because none of those binaries contain that test. The lib binary was compiled and run, but its results were buried in the multi-binary output where the grep missed them.

Correct invocation:

cargo test --manifest-path Cargo.toml --lib -- remember_preference

Actual result:

running 16 tests
test openhuman::tools::implementations::agent::remember_preference::tests::blocked_in_readonly_mode ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::empty_key_returns_error ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::facet_class_as_str_round_trips ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::facet_class_parse_case_insensitive ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::idempotent_overwrite_does_not_create_duplicate ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::invalid_class_returns_error ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::key_with_spaces_returns_error ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::missing_class_returns_error ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::missing_key_returns_error ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::missing_value_returns_error ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::pinned_content_format ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::pinned_key_format ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::schema_has_required_fields ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::stores_all_six_classes ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::stores_preference_in_user_profile_namespace ... ok
test openhuman::tools::implementations::agent::remember_preference::tests::tool_name_and_permission ... ok
test result: ok. 16 passed; 0 failed; 0 ignored; 0 measured; 7627 filtered out; finished in 0.21s

The wiring is correct: src/openhuman/tools/mod.rs uses #[path = "impl/mod.rs"] pub(crate) mod implementations; which aliases the impl/ directory, and impl/agent/mod.rs correctly declares pub mod remember_preference;. The #[cfg(test)] mod tests inside remember_preference.rs compiles and runs as openhuman::tools::implementations::agent::remember_preference::tests::*.


Defect 2: turn_tests.rs orphaned file

Finding: already wired and passing. No code change needed.

The claim was that mod turn_tests; is declared nowhere. Checking src/openhuman/agent/harness/session/turn.rs at lines 1910-1912:

#[cfg(test)]
#[path = "turn_tests.rs"]
mod tests;

turn.rs declares turn_tests.rs as its inline test module via the #[path] attribute. It compiles as openhuman::agent::harness::session::turn::tests::*. The 3 fetch_learned_context_* tests are NOT orphaned.

Actual result:

running 3 tests
test openhuman::agent::harness::session::turn::tests::fetch_learned_context_explicit_flag_off_learning_off_returns_empty_even_with_stored_prefs ... ok
test openhuman::agent::harness::session::turn::tests::fetch_learned_context_returns_empty_when_both_flags_off ... ok
test openhuman::agent::harness::session::turn::tests::fetch_learned_context_returns_pinned_prefs_when_explicit_flag_on_learning_off ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 7640 filtered out; finished in 0.14s

Summary

Both groups of tests — all 16 remember_preference tests and all 3 fetch_learned_context tests — compile and pass on HEAD 858f5bc6. No source changes were required. The reported defects were diagnosis artifacts from incorrect test invocation (missing --lib) and a #[path] attribute that wasn't noticed during the initial inspection. No new commit or push needed.

sanil-23 avatar May 18 '26 22:05 sanil-23