Skip to content

feat(bootstrap): inject per-user app_profile into agent context (profile Phase 3) - #68

Merged
cryptolir merged 1 commit into
mainfrom
feat/app-profile-context-injection
Jun 17, 2026
Merged

cryptolir merged 1 commit into
mainfrom
feat/app-profile-context-injection

Conversation

@cryptolir

Copy link
Copy Markdown
Owner

Per-user-profile plan — Phase 3: inject app_profile into the agent's context every turn. The capstone — the agent now always knows the user's name and running summary without being reminded. Built from the design investigation against the actual bootstrap-assembly code (plan #64).

What

For an app-user session, the chatting user's app_profile section (seeded by the Havaya app, maintained by the agent via save_user_section) is surfaced as a synthetic APP_PROFILE.md bootstrap context file injected into the system prompt every turn.

  • New src/agents/app-profile-context.ts (pure + one IO wrapper):
    • extractAppProfileSection — fail-closed marker extractor (null on absent / malformed / duplicate). Cross-repo contract with the writer's upsertSection and the dashboard's extractSection.
    • clampAppProfile — UTF-8 byte-safe 2 KB cap (mirrors SECTION_CONTENT_BYTE_CAPS.app_profile and the dashboard APP_PROFILE_MAX_BYTES). Defense-in-depth: admin/raw-edited files bypass the writer cap, so clamp again before injecting.
    • appendAppProfileBootstrapFile — app-session-only (isAppUserSession + resolveAppUserId), reads the canonical users/<appUserId>.md, no-op otherwise.
  • src/agents/bootstrap-files.ts (3-line wrap): append the synthetic file after hook overrides (so user hooks can't strip it) and before buildBootstrapContextFiles (so the existing per-file / total budget clamps apply). The model sees it as ## APP_PROFILE.md — a bare filename, so the on-disk users/<id>.md path never leaks.

Why first-class (not a host hook)

The agent:bootstrap bundled-hook path is gated behind hooks.internal.enabled + a per-hook config entry — incompatible with "always-on, app-session-only, zero-config." A direct step in bootstrap assembly is simpler and matches codex's guidance on plan #64.

Safety

  • No cross-user leak (verified): bootstrap context is resolved per-run inside runAttempt — loadWorkspaceBootstrapFiles re-reads disk and resolveAppUserId re-reads the session entry every call; no per-agent memoization. Different sessions resolve different appUserIds independently.
  • Bounded: hard 2 KB byte clamp on top of the existing context-file budget.
  • Compaction-safe: compact.ts uses the same resolver, so the profile stays injected after compaction.
  • App-only: Telegram / no-appUserId / missing-file / empty-section → no-op.

Gate

  • vitest src/agents/app-profile-context.test.ts → 14/14 (extractor incl. fail-closed dup, byte-safe clamp incl. multi-byte straddle, synthetic-file shape, non-app no-op).
  • tsgo --noEmit → clean on the touched files. ⚠️ Repo-wide check is red on pre-existing tsgo errors (chat.ts:417, two test files — none mine), tracked separately; this PR adds none.

Not in this PR

Agent SOUL.md/AGENTS.md prose so the agent maintains the running summary (host-side, 2ndclaw), and the gateway image roll that ships this + the Phase-1/2 writer to the live life agent.

🤖 Generated with Claude Code

…e 3)

For app-user sessions, surface the chatting user's app_profile section as a
synthetic APP_PROFILE.md bootstrap context file injected into the system prompt
every turn, so the agent always knows who it is talking to without being
reminded (the per-user-profile plan, Phase 3).

- New src/agents/app-profile-context.ts: extractAppProfileSection (fail-closed
  marker extractor), clampAppProfile (UTF-8 byte-safe 2KB cap, mirrors the
  writer + dashboard caps), buildAppProfileContextFile (pure), and
  appendAppProfileBootstrapFile (app-session-only via isAppUserSession +
  resolveAppUserId; reads the canonical users/<appUserId>.md).
- bootstrap-files.ts: append the synthetic file after hook overrides and before
  the buildBootstrapContextFiles clamp, so it is budgeted and survives
  compaction (compact.ts uses the same resolver).

First-class in bootstrap assembly (not a config-gated host hook): zero-config,
app-session-only, byte-bounded, resolved per-turn (no cross-session caching, so
no cross-user leak). Telegram / no-appUserId sessions are no-ops. Reaches the
live agent on the next gateway image roll.

Gate: vitest app-profile-context 14/14; tsgo clean on the touched files; oxfmt.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@cryptolir

Copy link
Copy Markdown
Owner Author

@codex review

Phase 3 of the per-user-profile plan — first-class gateway injection of the per-user app_profile into the agent's system prompt every turn, per your guidance on plan #64 (gateway bootstrap assembly, not a life-only host hook). Please verify:

  1. Injection point / ordering — appendAppProfileBootstrapFile runs in resolveBootstrapFilesForRun AFTER applyBootstrapHookOverrides and BEFORE buildBootstrapContextFiles. Confirm user hooks can't strip it and the existing per-file/total clamps still apply.
  2. No cross-user leak — I claim bootstrap context is resolved per-run (no per-agent memoization) so different sessions resolve different appUserIds. Try to find a caller that caches resolveBootstrapContextForRun/resolveBootstrapFilesForRun output across sessions.
  3. App-session gate — isAppUserSession(sessionKey) + resolveAppUserId(sessionKey); Telegram / no-appUserId / missing-file / empty-section must all no-op. Note I pass params.sessionKey (not sessionId) into the appender — confirm that's the key the :app: marker + persisted appUserId live on.
  4. Path correctness — reads canonicalUserFileDir(workspaceDir)/<appUserId>.md where workspaceDir is the shared agent home (not the jailed per-user cwd). Confirm the caller passes the shared workspace.
  5. Extractor/clamp parity — extractAppProfileSection marker strings are byte-identical to the writer + dashboard; clampAppProfile is UTF-8-byte-safe (2KB). Any way a malformed/oversize on-disk profile injects something unbounded or path-leaking?
  6. Compaction — compact.ts shares the resolver, so the profile persists post-compaction. Confirm no double-injection.

Gate: vitest app-profile-context 14/14; tsgo clean on the touched files (repo-wide check is red on pre-existing tsgo errors, not mine).

@cryptolir
cryptolir merged commit 50f6c2d into main Jun 17, 2026
16 of 20 checks passed
@cryptolir
cryptolir deleted the feat/app-profile-context-injection branch June 17, 2026 08:10
cryptolir added a commit that referenced this pull request Jun 17, 2026
Record the per-user-profile Phase 3 deploy: PR #68 (app_profile context
injection) + #69 (CI gate fix) merged; gateway image v2026.06.17.1
(sourceSha 50f6c2d) built and pinned to the life agent only on 2ndClaw;
life workspace AGENTS.md gained the app_profile writable section + rules.
Note the intentional life-ahead image drift on the US host.

Co-authored-by: Liran Peretz <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
cryptolir added a commit that referenced this pull request Jun 17, 2026
…sion (#71)

resolveAppUserId reads appUserId from the persisted session entry, but
chat.send only writes it onto an EXISTING entry (updateSessionStoreEntry no-ops
when the entry is missing). On the first turn of a brand-new session the entry
doesn't exist yet, so the id isn't available and the APP_PROFILE.md injection
no-ops — the agent asks the user their name even though we know it. Every new
app conversation (e.g. a page refresh / new chat) starts a fresh session and
hit this; from turn 2 on the entry exists and the profile injects correctly.

Fix: add appUserIdFromSessionKey() as a fallback. When the persisted entry has
no appUserId yet, derive it from the session key itself — the second-to-last
":"-segment after ":app:", valid for both the namespaced 4-part form
(app:havaya:<userId>:<conv>) and the legacy 3-part form (app:<userId>:<conv>),
since the conversationId is always the final colon-free segment. Read-only and
same-user (the id comes from this very session's key) — it cannot surface
another user's profile, and it leaves the shared resolveAppUserId (and thus the
workspace jail + save_user_section) untouched.

Also brace the guard-returns in this file to satisfy oxlint `curly` — it was
the only file with project lint errors (pre-existing from #68's --admin merge),
so this greens `pnpm lint` repo-wide.

Tests: appUserIdFromSessionKey unit cases (4-part / 3-part / bare / lowercasing
/ non-app / no-userId / undefined) + a first-turn fallback integration test
(temp users/<id>.md, no persisted entry). vitest 22/22, lint 0, format clean.

Co-authored-by: Liran Peretz <[email protected]>
Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
cryptolir added a commit that referenced this pull request Jun 18, 2026
…oad_skill #74) (#75)

Record the load_skill roll: #74 merged, gateway v2026.06.18.1 (sourceSha
09a99e4) pinned to life only — ships the full per-user stack to prod (writer
#65/#66 + injection #68 + first-turn #71 + load_skill #74). Bump the
INFRASTRUCTURE life-ahead tag to v2026.06.18.1 and note the US-host disk cleanup
(97% to 67%, removed 8 unused/unreferenced gateway tags).

Co-authored-by: Liran Peretz <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
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