Skip to content

feat(openviking): continuation after plugin hardening#150

Open
mczabca-boop wants to merge 28 commits intoTinyAGI:mainfrom
mczabca-boop:steven-pr-2-upstream
Open

feat(openviking): continuation after plugin hardening#150
mczabca-boop wants to merge 28 commits intoTinyAGI:mainfrom
mczabca-boop:steven-pr-2-upstream

Conversation

@mczabca-boop
Copy link
Collaborator

Follow-up to #149.\n\nThis PR contains the OpenViking continuation stack rebased onto TinyAGI/main (without the PR1 hardening commit).

@mczabca-boop
Copy link
Collaborator Author

Summary

This PR continues the OpenViking integration/pluginization line (TinyAGI#127 -> #1 -> #2) and adds the next stabilization layer focused on latency control, gate behavior, and runtime reliability.

In short: OpenViking remains fully pluginized, but prefetch is no longer “always-on”; it is now gated rule-first with optional LLM decisioning for ambiguous cases.

What This PR Adds

1) Rule-first prefetch gate inside openviking-context plugin

  • Adds prefetch gate modes:
    • always
    • never
    • rule (default)
    • rule_then_llm
  • Adds configurable gate controls:
    • prefetch_force_patterns
    • prefetch_skip_patterns
    • prefetch_rule_threshold
    • prefetch_llm_ambiguity_low
    • prefetch_llm_ambiguity_high
    • prefetch_llm_timeout_ms
  • Force/skip/rule/llm decisions are all plugin-internal (no OpenViking-specific branching in core queue flow).

2) Expanded bilingual trigger vocabulary (CN/EN)

  • Extended force/skip phrase coverage significantly in:
    • plugin rule evaluator
    • setup defaults
    • runtime defaults
  • Improves practical recall/precision for real user phrasing in Chinese + English.

3) Optional LLM gate for ambiguous cases only

  • In rule_then_llm, LLM is called only when rule verdict is ambiguous.
  • LLM output is parsed as structured JSON (need_memory, reason).
  • Timeout/error is fail-open to no-prefetch (llm_no) to avoid blocking the main response path.

4) Better hook-budget awareness in beforeModel

  • Gate logs now include:
    • hook_budget_ms
    • hook_remaining_ms
    • prefetch_timeout_effective_ms
  • If remaining hook budget is insufficient, plugin cleanly skips prefetch with explicit reason (instead of hidden timeout behavior).

5) Stability hardening around OpenViking runtime

  • Adds vectordb dimension consistency guard in daemon startup:
    • compares expected dim (config) vs actual dim (runtime data metadata)
    • on mismatch, auto backup + rebuild runtime data dir
  • Reduces recurring “expected X, got Y” instability loops.

6) Queue processor env loading fix

  • queue-processor now loads .env (dotenv/config), ensuring plugin hook timeout/env knobs reliably take effect after normal restart.
  • Fixes cases where .env had correct values but runtime still used defaults.

Observability Improvements

Per-turn logs now clearly expose gate behavior:

  • prefetch_decision=force|rule_yes|rule_no|llm_yes|llm_no|disabled
  • reason=...
  • LLM gate timing + result (or timeout reason)
  • Prefetch hit distribution remains visible (memory/resource/skill)

Key Files Touched

  • src/plugins/openviking-context/index.ts
  • src/plugins/openviking-context/prefetch-gate.ts
  • src/queue-processor.ts
  • lib/common.sh
  • lib/setup-wizard.sh
  • lib/daemon.sh
  • README.md

Compatibility / Rollback

No change to core requirement:

  • OpenViking remains optional and pluginized.
  • Disabling OpenViking/plugin keeps TinyClaw normal behavior intact.

Feature flags remain compatible:

  • TINYCLAW_OPENVIKING_CONTEXT_PLUGIN=0
  • TINYCLAW_OPENVIKING_SESSION_NATIVE=1
  • TINYCLAW_OPENVIKING_SEARCH_NATIVE=1
  • TINYCLAW_OPENVIKING_PREFETCH=1
  • TINYCLAW_OPENVIKING_AUTOSYNC=1

Gate defaults for new installs remain conservative:

  • prefetch_gate_mode=rule (no LLM gate by default)

Validation Notes

Passed locally:

  • npm run build:main
  • npm run test:prefetch-parser
  • npm run test:openviking-plugin

Manual runtime validation completed for:

  • force path (prefetch_decision=force)
  • rule-no path (prefetch_decision=rule_no)
  • rule-then-llm path (prefetch_decision=llm_yes/llm_no)
  • commit-required memory ingestion behavior (memory appears after successful session commit)

Risk / Follow-up

  • LLM gate latency remains environment-dependent; timeout tuning still matters in production.
  • If teams prefer lower latency by default, keep rule mode and enable rule_then_llm selectively.
  • OpenViking upstream config behavior still requires explicit api_key in current version (pure env-substitution in ov.conf is not yet reliable).

@mczabca-boop mczabca-boop force-pushed the steven-pr-2-upstream branch from fc86072 to 2a5bc26 Compare March 2, 2026 20:46
mczabca-boop and others added 4 commits March 4, 2026 20:38
…ncurrency

- Remove --dangerously-bypass-approvals-and-sandbox from codex LLM gate
  invocation and --dangerously-skip-permissions from claude invocation
- Replace global process.env API key pollution (applyOpenVikingToolEnv)
  with buildOpenVikingToolEnv() that scopes credentials to each subprocess
  env only; openviking service spawn and runOpenVikingToolJson both receive
  env locally via runCommand extraEnv parameter
- Rewrite session-map with in-memory cache + serialised async disk flush:
  mutations apply synchronously to the cache (preserving read-after-write
  consistency) while disk writes are queued and atomic (tmp + rename)
- Add drainSessionMapWrites() for clean shutdown
- Add TODO(perf) comment on runOpenVikingToolJson subprocess spawn hot path

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…messages

Internal messages are system coordination traffic, not user conversation
records. Previously they would fall through to the legacy writeback path
and emit a spurious INFO log on every turn. Now afterModel exits early for
internal messages before the writeback decision, eliminating the noise.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…otate path

When rotateSessionMappingAndCommitAsync runs, it deletes the session from
the map and then schedules an async commit. A crash between these two steps
left the session unrecoverable because startup recovery only scans the
session map.

Fix: write a pending-commit marker file (runtime/openviking/pending-commits/)
before deleting the map entry, clear it on successful commit, and extend
scheduleStartupRecoveryCommit to scan and replay any leftover markers on
next startup.

Also adds a 'pending-commit-recovery' integration test scenario that
simulates the post-crash state (marker present, session map empty) and
verifies the recovery commits the session and cleans up the marker.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ation

- Add step 6 to Quick Setup with two-tier flag guidance:
  minimum (SEARCH_NATIVE=1, retrieval only) and recommended
  (SESSION_NATIVE=1 + SEARCH_NATIVE=1, full session persistence)
- Clarify that SESSION_NATIVE defaults to 0 and must be set explicitly
- Replace flat Common flags list with a full Prefetch flags table
  including defaults and descriptions for all env vars
- Add new Session Configuration section covering SESSION_NATIVE,
  SESSION_IDLE_TIMEOUT_MS, SESSION_SWITCH_MARKERS, COMMIT_ON_SHUTDOWN,
  COMMIT_TIMEOUT_MS, AUTOSYNC, and CLOSED_SESSION_RETENTION_DAYS
- Note that internal messages are never written to OpenViking sessions

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants