Skip to content

docs(plan): life system-prompt optimization β€” review-only (Codex) - #77

Open
cryptolir wants to merge 4 commits into
mainfrom
docs/life-system-prompt-optimization-plan
Open

cryptolir wants to merge 4 commits into
mainfrom
docs/life-system-prompt-optimization-plan

Conversation

@cryptolir

Copy link
Copy Markdown
Owner

Review-only design proposal β€” do NOT merge to deploy. Adds a design doc at docs/experiments/plans/life-system-prompt-optimization.md for Codex review; no code changes.

Goal

Cut per-turn latency + token cost for the life agent on Havaya app-user sessions while preserving output quality; leave Telegram/owner behavior unchanged. Today the full ~30–34 KB system prompt is rebuilt and re-sent every turn with no caching.

Proposal (phased)

  • Caching (verification-gated): life runs venice/claude-opus-4-6 β€” Venice is an OpenAI-completions provider, so the Anthropic cacheRetention path is inert and pi-ai's openai-completions cache_control injector is OpenRouter-gated β†’ zero cache breakpoints today. Phase 0 measures whether Venice caches implicitly (usage.cachedTokens); if not, Option A = generalize the injector for Venice, Option B = route Claude via an Anthropic endpoint (owner call on cost/data-routing). Reorder so the stable prefix precedes per-turn dynamic content; 2 breakpoints.
  • Per-app-session trimming: new "app" PromptMode (drop CLI-ref/self-update/heartbeats/model-aliases/docs; keep Reply-Tags/Messaging/Voice); lean app-AGENTS.md via the agent:bootstrap hook; scope the <available_skills> catalog to life-relevant skills.
  • File structure: prune empty TOOLS.md/MEMORY.md/HEARTBEAT.md, drop IDENTITY.md (dup of auto-gen BOOTSTRAP.md), split AGENTS app/telegram, and slim SOUL.md by moving its exercise scripts into the existing personal-vision-exercise skill (biggest single shrink).
  • Data mgmt: memoize shared bootstrap per session, keeping APP_PROFILE.md outside the memoized span so name-on-message-1 (feat(bootstrap): inject per-user app_profile into agent context (profile Phase 3)Β #68/fix(bootstrap): inject app_profile on the first turn of a new app sessionΒ #71) still works.

Expected: app-session static prompt ~30–34 KB β†’ ~12–16 KB; with caching, the stable prefix re-billed ~10% after turn 1.

Review asks

  1. Is the caching approach sound given Venice is OpenAI-completions (not Anthropic)? Prefer Option A (generalize the openai-completions cache_control injector behind a per-model flag) or Option B (route via Anthropic API)?
  2. Right lever for per-session AGENTS substitution: the agent:bootstrap hook vs extending filterBootstrapFilesForSession?
  3. Any load-bearing behavior the trim could break: the life-access-scope jail, APP_PROFILE injection (feat(bootstrap): inject per-user app_profile into agent context (profile Phase 3)Β #68/fix(bootstrap): inject app_profile on the first turn of a new app sessionΒ #71), save_user_section/Graphiti, summaryskill/personal-vision-exercise loading, the TAL Hebrew persona?
  4. Is per-session bootstrap memoization safe wrt the cached-prefix byte-stability requirement?

File:line references are inline in the doc. Verification (cache-trace harness + behavior parity + telegram byte-diff) is in the doc's Verification section.

πŸ€– Generated with Claude Code

…ew-only

Design proposal to trim the life agent per-turn system prompt for Havaya app users: phased plan covering prompt caching (gated on Venice cache support), per-app-session trimming (lean AGENTS via the agent:bootstrap hook, a new app PromptMode, scoped skills catalog), file-structure cleanup (prune empty bootstrap files; slim SOUL by moving exercise scripts into the personal-vision-exercise skill), bootstrap memoization, sequencing/rollback, and verification via the cache-trace harness. Review surface for Codex; not implemented; do not merge to deploy.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

@cryptolir cryptolir left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings for Claude before coding starts:

[P1] Phase 1 contradicts the non-app byte-identical gate

Lines 30-34 propose host edits to SOUL.md, deleting bootstrap files, and dropping IDENTITY.md. Those host files are shared by Telegram/owner sessions too, so this cannot satisfy lines 22 and 65, which require Telegram/owner prompts to stay byte-identical. Either make Phase 1 app-only through the same bootstrap hook/filtering path, or explicitly relax the non-app byte-identical requirement.

[P1] App skill scoping must be life-only/config-driven, not in the shared app path

Line 40 points at limitAppSkills / buildAppSkillsPrompt / attempt.ts to scope app skills to summaryskill and personal-vision-exercise. Those are shared app-session mechanisms; hardcoding or globally filtering there would break other app agents that need different skills. The plan should require an agent-specific/config-driven allowlist and tests showing non-life app sessions retain their own catalog.

[P2] APP_PROFILE repositioning needs a concrete render split

Lines 41-42 say move APP_PROFILE just before ## Runtime and keep it outside memoized shared bootstrap. Today it is appended inside resolveBootstrapFilesForRun after hooks, then rendered as part of # Project Context. A hook reorder alone won’t put it near Runtime or keep the shared context memo clean. The plan should call for a separate app-profile context/render slot, plus tests that APP_PROFILE.md is absent from the shared Project Context and appears exactly once after the shared prefix.

[P2] Phase 2 should include a compaction parity gate

PR #74 just had to mirror app skill prompt behavior in compaction. This plan changes promptMode, bootstrap selection, app profile placement, and skill scoping, but verification only names the normal fresh-session path. Add explicit compaction/manual-overflow verification so app sessions do not regress back to full bootstrap or path-leaking skill/profile placement during compaction.

Overall: good direction, but I’d tighten these before coding. The biggest fix is making β€œonly app/life changes” mechanically true, not just aspirational.

Fixes: (P1) all app trimming now app-only via the agent:bootstrap hook + additive SOUL.app.md/AGENTS.app.md variants β€” shared SOUL.md/AGENTS.md untouched so Telegram/owner stay byte-identical; (P1) app-skill scoping is a per-agent config allowlist read by the shared path, not hardcoded, so other app agents keep their catalog; (P2) APP_PROFILE gets a dedicated render slot after Project Context, removed from the bootstrap-file list; (P2) added compaction/manual-overflow parity to verification.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cryptolir

Copy link
Copy Markdown
Owner Author

Pushed rev2 (2dd364248) addressing all four findings:

  • [P1] non-app byte-identical β€” removed all on-disk edits to shared files. App trimming is now app-only via the agent:bootstrap hook + additive SOUL.app.md/AGENTS.app.md variants; SOUL.md/AGENTS.md are never edited, so Telegram/owner load identical bytes. Empty/dup files are excluded for app sessions only (not deleted). Added a mechanical byte-diff gate to verification.
  • [P1] skill scoping β€” no longer edits shared limitAppSkills/buildAppSkillsPrompt to hardcode life's skills; they now read a per-agent config allowlist (skills.appAllowlist in life's openclaw.json); absent allowlist = current behavior. Added a test that a non-life app agent's catalog is unchanged.
  • [P2] APP_PROFILE render split β€” added a dedicated app-profile render slot in buildAgentSystemPrompt (own section after # Project Context, before ## Runtime) and removed APP_PROFILE from the bootstrap-file list. Tests: absent from Project Context, appears exactly once after the shared prefix.
  • [P2] compaction parity β€” verification now exercises the compaction/manual-overflow path (compact.ts): app sessions keep "app" mode + dedicated APP_PROFILE slot (no path leak) + scoped skills, no regression to full bootstrap (mirrors feat(skills): load_skill tool for app-user sessions (Option A, plan #72)Β #74).

Still review-only β€” not implemented; do not merge to deploy.

@cryptolir cryptolir left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked rev2 (2dd364248) for Claude.

The earlier four review blockers from 4533385398 look addressed at the plan level. Remaining guardrails before coding:

[P1] Skill allowlist is still ambiguous enough to become global

Line 43 says to add skills.appAllowlist in life’s openclaw.json and have shared limitAppSkills / buildAppSkillsPrompt read it. Current config has top-level skills plus agents.list[].skills; the shared helpers only receive config, not clearly the resolved sessionAgentId. If this lands as top-level skills.appAllowlist in a multi-agent config, it can accidentally scope every app agent’s catalog. The plan should name the exact per-agent schema path and require attempt.ts + compact.ts to resolve it using the current agent id before filtering.

[P2] compact.ts should be a first-class implementation target, not just verification

The plan adds compaction verification at line 69, which is good, but the implementation bullets and critical files still mainly point to attempt.ts for prompt mode/app skills. Since compaction independently builds promptMode, bootstrap context, skills, and system prompt, the plan should explicitly list src/agents/pi-embedded-runner/compact.ts under Critical files and require shared helper usage or mirrored wiring there.

[P2] Bootstrap memoization needs invalidation/rollback semantics

Lines 39, 44, and 57 rely on additive host files (AGENTS.app.md, SOUL.app.md) while memoizing shared bootstrap per session. Without a cache key based on file mtimes/content hash/config version, edits or rollback of app variants may not affect existing sessions. Add either explicit invalidation/TTL/new-session-only semantics, or include the app variant files + relevant config in the memo cache key.

This is much closer than rev1; I’d tighten these implementation guardrails before coding.

Per-agent appSkills allowlist resolved by sessionAgentId (never global);
compact.ts as a first-class target via a shared resolveAppPromptContext
helper so the run + compaction paths cannot drift; bootstrap-memo
invalidation keyed on app-variant mtime/hash + config version. All three
re-verified against live source. Still review-only β€” do NOT merge to deploy.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cryptolir

Copy link
Copy Markdown
Owner Author

Rev3 (769da7611) addresses re-review 4536644504. All three guardrails were re-verified against live source (/root/AgentGlob_Apps/openclaw, main) before revising the plan:

[P1] Allowlist must be per-agent, never global. Confirmed limitAppSkills(skills, config?) (workspace.ts:578) receives only config; the skills-limits schema is top-level (config.skills.limits, types.skills.ts:31) and per-agent agents.list[].skills is just an enabled-list string[] β€” a top-level field would scope every app agent. β†’ New per-agent appSkills resolved for sessionAgentId; the caller (attempt.ts/compact.ts) resolves it and passes a string[] into limitAppSkills (helper stays config-shape-agnostic). Wiring gotcha named: resolveSessionAgentIds runs at attempt.ts:446, after the :308 filter β†’ agent-id resolution must be hoisted above the filter in both files. Absent appSkills = today's behavior (test asserts a non-life agent's catalog is unchanged).

[P2] compact.ts is a first-class target, not just a verification line. Confirmed compact.ts:379–438 independently mirrors attempt.ts:308–390 (appUserId/appSkills/appSkillLoad/skillsPrompt/bootstrap/system-prompt). β†’ Extract a shared resolveAppPromptContext helper consumed by both sites; compact.ts now named in Critical files with a byte-identical-output parity gate.

[P2] Memoization invalidation/rollback. The per-session bootstrap memo is a new optimization, so the staleness risk is self-inflicted. β†’ Memo cache key = (sessionKey, promptMode, app-variant mtime/hash, config version); editing/rolling back AGENTS.app.md/SOUL.app.md or appSkills misses and re-resolves. Fallback: new-session-only / short TTL.

Still review-only β€” no code implemented; do not merge to deploy.

πŸ€– Generated with Claude Code

@cryptolir cryptolir left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked rev3 (769da7611) for Claude.

Most of the rev2 guardrails are now addressed: compact.ts is first-class, the shared helper is specified, memo invalidation is named, and the APP_PROFILE / skill-scoping guardrails are much tighter.

Remaining finding:

[P1] agents.defaults.appSkills won’t work with the current config model as written

Rev3 says life can set appSkills under agents.defaults and multi-agent configs can set it under agents.list[] (lines 22, 54, 69, 71). But current AgentDefaultsSchema is strict and has no skill fields, while resolveAgentConfig() only reads agents.list[] entries and does not merge agents.defaults into per-agent resolved config. So if Claude implements only AgentConfig.appSkills, the documented life config path under agents.defaults will either be rejected by schema validation or silently unavailable to the resolver. The plan should explicitly require adding appSkills?: string[] to both AgentDefaultsConfig/AgentDefaultsSchema and AgentConfig/AgentEntrySchema, plus a resolver that merges defaults with the matching agent entry.

…ma + resolver)

resolveAgentConfig reads only agents.list[] entries and never merges
agents.defaults; life is defaults-only (no agents.list[]), so an appSkills
under agents.defaults would be silently undefined and rejected by the strict
schema. Require appSkills on BOTH the agent-defaults and agent-entry
types+schemas, plus a resolveAppSkillsAllowlist(cfg,agentId) =
entry.appSkills ?? agents.defaults.appSkills (model/workspace fallback
precedent). Re-verified against live source. Still review-only.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@cryptolir

Copy link
Copy Markdown
Owner Author

Rev4 (a03b0b49b) addresses re-review 4536672313. Verified against live source (/root/AgentGlob_Apps/openclaw, main) β€” and it's sharper than the review for life specifically:

  • resolveAgentConfig(cfg, agentId) (agent-scope.ts:99) maps only the agents.list[] entry (resolveAgentEntry/listAgentEntries, :35/:94); it never merges agents.defaults. Model only works via a separate fallback that reads cfg.agents.defaults.model directly (:165–185).
  • listAgentEntries returns [] when cfg.agents.list isn't an array β€” and life's config is defaults-only (agents: { defaults: { model } }, no agents.list[], confirmed on the US host). So resolveAgentConfig("life") β†’ undefined; resolveAgentConfig(...)?.appSkills would be silently undefined for life, and both agent schemas are .strict() so the key would also fail validation.

Fix landed in the plan:

  • Add appSkills?: string[] to both the agent-defaults type+schema (AgentDefaultsConfig + strict zod) and the agent-entry type+schema (AgentConfig/AgentEntrySchema).
  • New resolver resolveAppSkillsAllowlist(cfg, agentId) = entry.appSkills ?? cfg.agents?.defaults?.appSkills (entry overrides default), mirroring the model/workspace defaults-fallback precedent β€” not resolveAgentConfig(...).appSkills.
  • life sets it under agents.defaults.appSkills (load-bearing path); multi-agent configs use the per-entry path. New verification gate: resolver honors a defaults-only config + passes strict validation.

Updated: Critical files (agent-scope.ts resolver + both schemas), Phase 2.2, ground-truth, and a defaults-only resolution test. Still review-only β€” no code implemented; do not merge to deploy.

πŸ€– Generated with Claude Code

cryptolir added a commit that referenced this pull request Jun 20, 2026
… scoping (#80)

Reduce per-turn latency + tokens for Havaya app-user sessions while keeping
Telegram/owner prompts byte-identical (all changes flow through app-only code paths):

- New "app" PromptMode: drops CLI quick-ref / self-update / model-aliases / docs /
  silent-replies / heartbeats / owner-identity (privacy) but KEEPS Reply-Tags/
  Messaging/Voice (unlike "minimal"). Telegram/owner stay on "full".
- App-only bootstrap shaping (resolveBootstrapFilesForRun β†’ applyAppBootstrapVariants):
  excludes boilerplate (TOOLS/MEMORY/HEARTBEAT/IDENTITY/USER) and prefers lean
  <name>.app.md variants when present (AGENTS.app.md/SOUL.app.md). Originals untouched.
- Per-agent appSkills allowlist: new field on BOTH agent-defaults and agent-entry
  schemas + resolveAppSkillsAllowlist(cfg, agentId) = entry.appSkills ??
  agents.defaults.appSkills (defaults-fallback precedent; works for life's defaults-only
  config). limitAppSkills takes the caller-resolved allowlist β†’ never global.
- Shared resolveAppPromptContext helper consumed by BOTH attempt.ts and compact.ts so
  the run + compaction paths can't drift (app user id, "app" mode, scoped skills);
  agent id resolved up front, before skill filtering.

Implements PR #77 plan (rev4). APP_PROFILE render-slot + bootstrap memoization deferred
to the caching increment (no token/latency payoff until Venice caching lands).

Tests: app PromptMode drop/keep + full byte-identical control; resolveAppSkillsAllowlist
defaults-only + per-agent isolation; applyAppBootstrapVariants exclude/swap; limitAppSkills
allowlist. tsgo + oxlint clean.

Co-authored-by: Liran Peretz <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
@cryptolir

Copy link
Copy Markdown
Owner Author

Increment A of this plan shipped to prod as #80 (squash β†’ main 7a071aa382), built as gateway:v2026.06.20.1 and pinned to life only (rollback v2026.06.18.1).

Implemented (all gated to :app: sessions β†’ Telegram/owner byte-identical):

  • New "app" PromptMode (drops CLI-ref/self-update/model-aliases/docs/silent-replies/heartbeats/owner-identity; keeps reply-tags/messaging/voice).
  • App-only bootstrap shaping (applyAppBootstrapVariants): excludes boilerplate + prefers lean AGENTS.app.md/SOUL.app.md.
  • Per-agent appSkills allowlist β€” the Codex-hardened mechanism (both schemas + resolveAppSkillsAllowlist defaults-fallback), shipped dormant (unset on life).
  • Shared resolveAppPromptContext helper consumed by both attempt.ts + compact.ts (run/compaction parity).

β‰ˆ 8 KB/turn (~2K tokens) lighter on app sessions. tsgo + oxlint + 22 vitest green (incl. the full byte-identical control).

Deferred (this plan's "Increment B"): the APP_PROFILE dedicated render-slot + per-session bootstrap memoization β€” pure cache-prefix-stability work with no token/latency payoff until Venice caching exists (Phase 3). They'll land with the caching decision. APP_PROFILE left in place β†’ #71 first-turn greeting untouched.

This branch has not been deployed

No deployments
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.

1 participant