feat(learn): sync learned lessons with the workspace for owner review

Draft
SA
1mo ago
+8312-137

Issue for this PR

Follow-up to #1405 (learn v1) and #1418 (auto-promote). Server side: AltimateAI/altimate-backend#7108 (API) and AltimateAI/altimate-frontend#3504 (review screen).

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Lessons from altimate-code learn live on one machine today. This shares them through the linked workspace, with the workspace owner reviewing them first:

  • Lessons a session learns go to the workspace as proposals.
  • The owner approves, rejects, pins or retires them in the Altimate app.
  • Approved lessons reach every teammate's sessions in that repository.

On by default for linked projects. Sync does anything only for a project linked to a workspace through a git remote; path-only projects are skipped. It needs no step of its own, like workspace skills. To turn it off, use learn disable --sync, learn.sync: false, ALTIMATE_LEARN_SYNC=0, or /learn → Turn sync off; the environment variable overrides config either way. Learning from sessions (capture and reflection) is still enabled with learn enable.

How it works

  • Pull: approved team lessons go into a private remote.json. Sync never writes approved.json, which stays the Git-shared local set.
    • A session pulls at its start. It waits at most 2 s, and only when there is no cache and the project is linked here, so unlinked projects never wait.
    • It pulls again in the background on a later turn every 5 minutes (SESSION_REFRESH_MS); delivery picks up the refreshed cache on the next turn.
    • learn sync and /learn → Sync now pull on demand.
  • Effective set: lessons are keyed by (repo identity, store, key). The team version wins, and a workspace-wide team lesson stands in for a local lesson with its key. A lesson the team retired is hidden locally, including from running sessions' frozen section, request notes, file hook and compaction replay.
  • Push:
    • Staged candidates, promotions, auto-promotions and reflections go through a durable outbox.json.
    • A ledger decides whether each one is an add, a revision of my own pending proposal, or an edit/remove of an approved team lesson with versioned targets. A reflection that changes a team lesson becomes an edit proposal, never a local change.
    • The first sync backfills existing lessons (at most 200 per run, resumable).
    • A push that sent proposals pulls once, so the pending-review count includes them.
  • Rules the agent is asked to remember: with sync on in a linked project, a project-scope rule saved with training_save is not written to project training (which applied at once and reached the team through git without review). It is staged as a lesson and sent to the owner's review queue, going through reflectCore with the rule as its only change, so it is deduplicated against local and team lessons. A rule that cannot be a lesson (not one line, over 140 characters, commands or secrets) is refused with the reason, so the agent restates it. Global training and other kinds (patterns, glossary, playbooks) are unchanged.
  • Safety:
    • Auto-promote never edits or removes a team lesson (gate 9).
    • A version conflict is held, never re-sent over an owner's edit, until learn push --resubmit <key> or /learn → Held proposals → Resubmit.
    • Lessons the server would refuse are listed as not syncable instead of retried.
    • A refused /batch is split into single items only on request-validation answers (400/413/422). A 401 or anything else stops the push and keeps the queue intact.
    • Usage counts for team lessons go up in retry-safe batches.
    • promote --publish is refused while sync is on.
    • No network I/O happens under the learn lock, and sync never fails a run or a promotion.
  • CLI:
    • New learn sync [--json] and learn push [--resubmit <key>].
    • learn status and --json get a sync block: workspace, revision, team lessons, retired here, pending review, outbox (queued and held with reasons), last pull and push, first-sync progress, not syncable.
    • learn show marks lessons (team), (local), (pending review), (held …), (retired by team — not delivered) and (team version delivered instead), and lists # Team.
    • learn enable --sync and learn disable --sync turn only sync on or off; disabling clears the pulled cache.
  • TUI:
    • /learn menu:
      • Status (approved lessons, staged changes, the sync line)
      • View lessons: searchable, with the status of each
      • Review staged changes: added, changed and removed, then Promote or Discard with a confirmation. Promote takes only the reviewed candidate
      • Held proposals: with Resubmit
      • Sync now
      • Turn sync off / on: hidden when an environment override decides
      • Open review queue: where the workspace web address is known
    • Sidebar: the workspace tile gets a lessons line, 3 team lessons · 1 pending review · 2 queued · synced 4m ago, from local files. A stale count is refreshed in the background every 5 minutes.
  • Privacy: lesson text and redacted provenance are uploaded for linked projects. Signals are still never uploaded; the docs and signals.ts wording say so.

Decisions a reviewer should know (confirmed with the product owner):

  • Sync is on by default for linked projects, like workspace skills. Linking a project to a workspace is the opt-in.
  • With sync on, a team rule saved by the agent goes to review instead of project training, so there is one reviewed path for team rules and no duplicate delivery.
  • learn disable --sync turns off only sync; capture stays on.
  • Withdrawing your own open proposal isn't supported by the API yet. Discarding a local candidate leaves proposals already sent in the review queue, and the UI says so.

How did you verify your code works?

  • From packages/opencode:
    • bun run typecheck: clean.

    • test/altimate/learn, test/altimate/workspace, test/altimate/plugin, test/training, test/altimate/training-import.test.ts, test/altimate/carry-forward, test/memory/overlay-merge.test.ts, test/cli/tui/learn-nudge.test.ts, test/cli/tui/plugin-lifecycle.test.ts, test/upstream/fork-feature-guards.test.ts: 3454 pass.

    • New tests cover:

      • the default, the 5-minute refresh, and unlinked projects not waiting
      • the sidebar line and its refresh
      • the /learn logic (summary, view, staged review, promote with the changed-candidate guard, discard, held and resubmit, sync toggle and environment override)
      • rule routing (proposed, not routed, invalid, existing)
      • submission hashes pinned to backend-generated cases

      Each test fails without its change.

  • Known failures: test/session/prompt.test.ts has 2 failures ("a turn's background workspace sync holds the exit flush…", "concurrent loop callers all receive same error result"). Both fail without this branch's changes too.
  • End to end: run against a local backend and app built from the server PRs, using the Altimate Base model. The CLI and TUI ran from this branch with an isolated home.
    • The agent learned from a correction in a session; the reflection staged it, sent it for review automatically, and the owner approved it in the app.
    • Owner reject, retire and approve-edit were done in the app, then synced with /learn → Sync now and checked in View lessons.
    • An agent asked to "remember this as a team rule" called training_save, which returned Team rule: sent for review; no training file was written, and the lesson reached the review queue with origin manual.
    • Promote, Discard and the sync toggle were run in /learn.
$ altimate-code link --workspace 13 --yes
$ altimate-code run "Rename models/staging/orders.sql to a better staging model name."
$ altimate-code run --continue "That's wrong — we always prefix staging models with stg_<source>__<entity>, so this one must be stg_shop__orders."
learn: 1 signal -> +1 added; staged .altimate-code/learn/team-playbook/candidate.json
# pending review in the app: auto_reflect | add | Name staging models `stg_<source>__<entity>`; use `stg_shop__orders` …
# owner approves in the app
$ altimate-code learn sync
Pull team-playbook: ok (1 team lessons, 0 retired here)
Learn                                                  /learn in the TUI
Lessons    4 approved lessons · Nothing staged.
Sync       2 team lessons · synced 1m ago
Actions  ● View lessons · Sync now · Turn sync off · Done

Staged changes · team-playbook · 2 changes             Review staged changes
Added      + Prefix intermediate models with `int_`
Changed    ~ Every model needs `unique` and `not_null` tests on its primary key, declared in its schema file
Actions  ● Promote · Discard · Back

Screenshots / recordings

The owner's review screen in the app (server PRs), showing lessons pushed by this branch:

Pending review: CLI backfill + editHistory
pendinghistory

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

🤖 Generated with Claude Code


Summary by cubic

Syncs learned lessons with the linked Altimate workspace: staged, promoted, and auto-reflected lessons go to the workspace owner's review queue, and owner-approved lessons are delivered to every teammate's sessions. Sync is on by default for linked projects and needs a workspace link bound through a git remote; path-only projects are skipped.

New Features

  • Turn off with learn disable --sync, learn.sync: false, or ALTIMATE_LEARN_SYNC=0; with sync unset nothing changes and no network calls happen.
  • Pulls approved team lessons into a private remote.json at session start (blocking at most 2s only when the project is linked and has no cache) and in the background every 5 minutes; approved.json is never written by sync.
  • New learn sync, learn push [--resubmit <key>], a sync block in learn status --json, and team/local/pending/held/retired markers in learn show; learn sync --json exits non-zero on failure and disable --sync checks the override before dropping the cache.
  • /learn TUI menu adds status, View lessons, Review staged changes, Held proposals with Resubmit, Sync now, and a sync toggle; the workspace sidebar shows a lessons line refreshed in the background.
  • A project-scope rule saved with training_save while sync is on is staged as a lesson and sent to review instead of project training; invalid rules are refused with the reason.
  • First sync backfills existing lessons through a durable outbox; it reruns when the pull cache is missing and proposes only lessons the server hasn't seen yet, while a staged candidate counts as the full set so removals are not re-uploaded.
  • Lessons the server would refuse (for example, old lessons over 140 characters) are listed as not syncable instead of retried.

Safety and behavior

  • The team version wins for a lesson keyed by (repo identity, store, key), and a lesson retired by the team is hidden locally, including from running sessions' frozen section, request notes, file hook, and compaction replay; delivery also works from a fresh clone.
  • Version conflicts are held—never re-sent over an owner's edit—until learn push --resubmit <key>; a conflicted remove clears only when a pulled cache shows it gone, and auto-promote never edits or removes a team lesson (gate 9). promote --publish is refused while sync is on.
  • Push is resilient: batch results match one exact (lesson_key, submission_hash), a refused /batch is fanned to single items only on request-validation errors (400/413/422—anything else keeps the queue intact), usage over 500 items is split, and a corrupt outbox.json keeps its valid entries.
  • Promotion proposals are derived before publishing and queued after it, so a failed promote never touches the queue; a failed enqueue only reopens the backfill and never fails the publication. Session views track the account and binding epoch with an LRU memo.
  • No network I/O happens under the learn lock, sync failures never fail a run, and the server-unsupported backoff is scoped per account with each pull failure recorded under its scope.
  • Lesson text and redacted provenance (capped at 4000 characters) are uploaded with sync on; signals are never uploaded.
  • GitGuardian ignores the shared lint_cases.json fixture, which deliberately carries the guardrail corpus' synthetic credentials as texts the lint must refuse.