Skip to content

feat(learnings): write learnings to the teamai-learnings branch - #616

Merged
jeff-r2026 merged 5 commits into
Tencent:mainfrom
SaulMoro:feat/learnings-branch-485
Sep 17, 2026
Merged

jeff-r2026 merged 5 commits into
Tencent:mainfrom
SaulMoro:feat/learnings-branch-485

Conversation

@SaulMoro

@SaulMoro SaulMoro commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

learnings/ moves to a teamai-learnings orphan branch, written by direct push, so a member with no write access to a protected default branch can contribute. Everything the team wrote before the switch stays exactly where it is and keeps coming back from teamai recall.

Shaped by the decisions in #485: orphan branch holding only learnings/, the old tree kept as the last read root, and the read seam folded into this one PR rather than landed separately. The offline-first queue rework is out of scope here and now lives in #615.

  • The orphan-branch machinery from Route report data (members/sessions/votes/stats) to teamai-reports in independent git clones #484 is extracted into branch-worktree.ts, parameterised by branch, worktree directory and lock. Reports become one instance of it and keep every exported name; learnings are the other.
  • learningsRoots(localConfig) gives a write root and an ordered read list. buildIndex takes the whole list and, for one relative path, the first root wins — that path is also the id votes are counted by.
  • teamai contribute publishes to the branch in both independent-clone and single-repo mode. Single-repo mode stops opening a pull request per contribution.
  • Pruning, promotion and confidence write-backs write to the write root and publish what they changed, instead of mutating a checkout nothing pushes.
  • EN/ZH docs describe the branch layout and the minimum Git permissions (Document branch layout and minimum Git permissions after reports/learnings leave main #486).

Type of Change

  • Bug fix (non-breaking change that fixes an existing issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature causing existing behavior to change)
  • Documentation only
  • Refactor / internal cleanup

Bugs found and fixed along the way

Each has a test.

What was wrong Where
A publish reported success as soon as git push resolved, and returned the same false for "busy lock", "nothing staged" and "rejected". A caller holding the only durable copy could not tell them apart branch-worktree.ts
"Nothing to commit" was read as "already on origin", so a learning committed by a failed attempt was dropped from the queue with its content only in a local commit. Caught by the real-CLI run, not by the unit tests branch-worktree.ts
Single-repo contribute persisted nothing when the push failed: the note was lost contribute.ts
Single-repo contribute copied every contribution into ~/.teamai/learnings, which every project on the machine shares, so one project's learnings surfaced in another's recall contribute.ts
A recall that had to rebuild the index passed no project namespaces, so project-private learnings vanished from the rebuilt index recall.ts
Recall printed the absolute path an entry had when indexed; a rebuilt worktree left the agent reading the output a dead pointer recall.ts
Promotion wrote its promoted_to mark into a checkout nothing commits, so a realign discarded it and the same learning was promoted again on the next run, paying for another model call promote.ts
Pruning archived next to the learning it archived; for a learning in a root nothing pushes, that archived copy was the only one left prune.ts
The learnings mirror deletes whatever its source lacks, so a single source would have stopped propagating upstream deletions (#458) pull.ts
Recall swallowed the reason a scope was unavailable: an invalid projects manifest reported "No learnings available" and nothing else recall.ts
ensureReportsDir was dead code duplicating ensureReportsWorktree reports-branch.ts
docs/providers.md said the push target is hardcoded to master; getDefaultBranch() resolves it docs, git.ts
The auto-migration copied a disposable worktree it did not know about. It now skips every worktree from one shared list, while pending-learnings/ still travels because it is work the member has already done migrate.ts

Test Plan

  • npx tsc --noEmit — clean
  • npx vitest run — 3348 tests, 244 files, green
  • New real-git coverage in src/__tests__/git-kind-learnings.test.ts (bare origin whose update hook refuses main/master): the learning lands on origin/teamai-learnings with main untouched; a refused branch leaves the note recoverable; a commit whose push failed is delivered on the next attempt; a namespaced learning keeps its subdirectory; one member reads another's learning after a refresh; two members publishing at once both land; the inherited corpus is still readable and still in the clone
  • src/__tests__/maintenance-two-roots.test.ts: an inherited learning is updated in the write root, the clone is untouched, and pruning an inherited learning is refused with an actionable message
  • src/__tests__/learnings-path-guard.test.ts: fails on a new hand-built learnings path
  • src/__tests__/contribute-unpublishable.test.ts: a contribution stays findable on the member's own machine even when the learnings worktree cannot be created at all — the guarantee the old write-into-the-clone path gave for free
  • src/__tests__/contribute-edge-modes.test.ts: a single-repo install with no commits yet keeps the note; an HTTP team repo is refused by the read-only guard, as before
  • src/__tests__/pull-publishes-queue.test.ts: pull publishes the queue in single-repo mode too, not only in an independent clone
  • A --single-branch clone, the shape CI checkouts have: a push of a side branch leaves no remote-tracking ref, so delivery is judged by the push itself (git-kind-learnings.test.ts)
  • npm run build, then the built CLI against a local bare remote whose update hook rejects the default branch:
1.  teamai pull                     → Synced 1 learnings
2.  teamai contribute --file …      → Contributed: learnings/side-branch-note-….md
3.  git ls-tree origin/teamai-learnings
                                    → learnings/side-branch-note-….md
4.  git ls-tree origin/main         → unchanged (learnings/inherited.md, skills/.gitkeep, teamai.yaml)
5.  teamai recall "side branch note"
                                    → File: …/learnings-wt/learnings/side-branch-note-….md
    teamai recall "written before the switch"
                                    → File: …/.teamai/learnings/inherited.md   (the pre-switch corpus)
7.  origin moved away, teamai contribute
                                    → Saved locally (…). Will retry on the next pull.
8.  teamai recall "offline"         → File: …/learnings-wt/learnings/offline-note-….md
10. origin back, teamai pull        → Published 1 learning(s) that could not be pushed earlier
11. git ls-tree origin/teamai-learnings
                                    → both notes
12. git ls-tree origin/main         → still unchanged
13. ls <clone>/learnings            → inherited.md, untouched
  • Agents: Claude (hooks injected and verified in the run above). Codex, CodeBuddy and OpenCode share the same hook-injection path, untouched by this change
  • Provider git end to end, as above. gitlab and github differ only in pull-request creation, which contribute no longer uses; teamai push is untouched

Related Issues

Closes #485
Closes #486

Follow-up: #615 (make the pending queue the write path for contribute, and surface it) — split out at your request.

Notes for Reviewers

  • viz --repo <path> and ci/extract-mr have no LocalConfig to resolve roots from, so they opt out of the path guard in place, with the reason on the line.
  • ci extract-mr still writes its learning to the default branch. CI holds a token that may write there, the clone stays a read root so the learning is still found, and splitting its single commit across two branches is not something this change can verify end to end. Write learnings/ to a teamai-learnings branch (direct push, no PR) #485 lists it as a follow-up.
  • src/ci/extract-mr.ts logs in Chinese, against the repo's own rule. It predates this change; translating it would widen the diff, so it is left alone.

`reports-branch.ts` managed the `teamai-reports` orphan branch: cold-start
creation across two git versions, stale-worktree repair, a non-blocking lock,
commit, and push with fetch+rebase retry. Learnings need the same machinery on
their own branch (Tencent#485), and must not share the branch, the worktree or the
lock.

The engine moves to `branch-worktree.ts`, parameterised by a spec of three
names plus an init commit message. Reports become one instance of it and keep
every exported name, so no caller changes. `reports-branch.ts` keeps what is
not a side branch: `EmptyRepoError` and `withKnowledgeWorktree`.

- A publish returns `PublishResult` instead of a boolean. A caller holding the
  only durable copy of its data must be able to tell "landed on origin" from
  "another writer holds the lock", "nothing to commit" and "rejected". Reports
  map it back to today's boolean, so their behaviour is unchanged.
- A publish confirms the ref actually moved before reporting success, and
  separates "nothing to commit" from "nothing to deliver": an earlier attempt
  may have committed the content and failed to push it. A successful push
  updates the remote-tracking ref locally, so the check is a local rev-list.
  Anything unreadable counts as not landed, which costs one extra push instead
  of losing data.
- `getWorktreeDir` and `getBusinessRoot` come out of the reports-specific
  helpers; `getReportsDir` is now one call to the first.
- `usesReportsBranch` was a one-line forward; its eleven call sites use
  `usesBranchWorktree`, which says what it means now that two branches use it.
- A side branch's `.gitignore` lists every worktree directory, so no worktree
  can nest-track another.
- `ensureReportsDir` had no callers and duplicated `ensureReportsWorktree`.

Refs Tencent#485
…every root

Contributing no longer touches the default branch, so a member whose team
protects `main` can contribute. Everything the team wrote before the switch
stays readable exactly where it is: nothing is copied, deleted or migrated.

**One accessor.** Eleven call sites built the learnings path by hand, from a
different base each time, so a forgotten one did not fail — it read an empty
directory and recall quietly returned less. `learningsRoots(localConfig)` now
answers with a write root and an ordered read list, and a test fails on a new
hand-built path. Two call sites have no config to resolve (the `viz --repo`
flag and CI) and opt out in place, with the reason on the line.

**Every root is read.** `buildIndex` takes the whole list. For one relative
path the first root wins, deduplicated while collecting: that path is also the
id votes are counted by, so two entries would double-count votes and then have
one silently dropped by recall's dedup. The clone's `learnings/` is always the
last root, which is what keeps the pre-switch corpus searchable.

**One publish path.** `teamai contribute` writes into the `teamai-learnings`
worktree and pushes, for independent clones and single-repo installs alike.
Single-repo mode stops opening a pull request per contribution, so the
disposable knowledge worktree and the machine-wide cache copy that stood in for
it are gone — and with them the cross-project leak of writing one project's
learnings into a cache every project shares. What cannot be published stays in
the durable queue outside the clone and is retried by the next `teamai pull`.

**Maintenance.** Pruning, promotion and confidence write-backs used to mutate a
checkout nothing pushes: the result reached no teammate, and a realign could
undo it. Promotion was worse — the `promoted_to` mark it wrote was discarded,
so the same learning was promoted again on the next run and paid for another
model call. They now write to the write root, publish what they changed, and
refuse to prune an inherited learning instead of deleting a file that comes
back.

Fixes that fall out of reading the same code:

- A recall that had to rebuild the index passed no project namespaces, so every
  project-private learning vanished from the rebuilt index while a pull-built
  one had them.
- Recall printed the absolute path an entry had when it was indexed; a worktree
  that is removed and rebuilt leaves that dangling, and the agent reading the
  output got a dead pointer.
- `pull`'s mirror deletes whatever its source does not have, so it now takes
  every published root except the mirror itself — pointed at one root it would
  have stopped propagating upstream deletions (Tencent#458).
- The learnings count in `pull` deduplicates across roots, the way the index
  does.
- Contributing self-heals the single-repo `.gitignore` first, the way `pull`
  and `push` already do, so the new worktree never shows up in the user's own
  `git status`.
- Recall says why a scope was skipped instead of swallowing the reason: an
  invalid projects manifest reported "No learnings available" and nothing else.

Real-git coverage against a bare origin whose `update` hook refuses the default
branch: the learning lands on the side branch with `main` untouched, a refused
branch leaves the note recoverable, a commit whose push failed is delivered on
the next attempt, two members publishing at once both land, one member reads
the other's learning after a refresh, and the inherited corpus is still there.

Refs Tencent#485
…cent#486)

A team that turns on branch protection needs to know what still works and what
access its members actually need. The docs said learnings live on the default
branch, and the providers guide said the push target is hardcoded to `master`.

- The data-layout table covers knowledge, learnings, reports and machine-local
  data, and says for each how it is written and whether it needs write access to
  the default branch. EN and ZH match row for row.
- A minimum-permissions section in the usage guide and in the providers guide:
  what a member needs, what they do not, and that `provider: git` still cannot
  open a pull request for you while `contribute` never needs one.
- The directory trees show `learnings-wt/` and `pending-learnings/`.
- The admin checklist says `init` commits an empty `learnings/` and that
  contributions do not go there.
- `docs/providers.md` no longer says the push target is hardcoded: it is
  resolved by `getDefaultBranch()`. The same stale claim in the docstring of
  `pushRepoDirectly` goes with it.
- The share-learnings skill names the branch instead of "directly to master".
- All five READMEs name the branch in the `teamai contribute` row.
- Both design documents describe the new split.

Closes Tencent#486
…lished

Splitting this change in two left a gap the full version did not have. Before
learnings moved to their own branch the note was written into the clone, so it
was indexed no matter what git did. Now a contribution is written into the
branch worktree — and when that worktree cannot be created at all, the durable
copy was kept but never indexed, so the member could not recall what they had
just written.

- The queue is a learnings root for the index, in `contribute` and in `pull`.
- The durable copy is written before the index is rebuilt, not after, or there
  would be nothing to index.

The queue stays the fallback here; making it the write path is Tencent#615.

Refs Tencent#485
@SaulMoro

Copy link
Copy Markdown
Collaborator Author

The follow-up is ready to go whenever this merges: #615 has the branch SaulMoro:feat/learnings-queue-615 prepared and stacked on this one, so its PR will show only the queue change.

It carries what you moved out of #485: the queue as the single write path, single-repo mode routed through it, and the queue state surfaced during pull plus a doctor check. Already verified on that branch — npx tsc --noEmit clean, npx vitest run green at 3343 tests, and the built CLI against a bare remote whose update hook refuses the default branch.

No rush from my side: it is out of this PR's way, exactly as you asked.

…long it can take

A review pass against edge cases found three ways this change could report or
behave worse than what it replaced.

**A successful push read as a failure.** The publish confirmed delivery by
checking that HEAD was no longer ahead of `origin/<branch>`. That ref is only
updated through the remote's FETCH refspec, so in a clone made with
`--single-branch` — what CI checkouts and many business repos are — pushing a
side branch leaves no tracking ref behind and the check said "not landed".
The result was five pushes of the same content and a reported failure although
the data was on origin, and for reports a `save-session` that printed a push
timeout for a write that had worked. A push that resolves is a push the remote
accepted; git exits non-zero when it refuses one. The tracking ref is now only
consulted to answer "does this worktree still owe origin a commit?", where an
unreadable answer means push and find out.

**No cap on how long publishing can take.** `contribute` used to give its push
ten seconds. That cap was lost, so an unreachable origin or a credential prompt
could block the CLI through five push and rebase rounds. Both publish paths
carry it again; timing out is safe because the durable copy stays.

**A warning aimed at a backend with no branch.** Maintenance on an HTTP team
repo has nothing to publish, and said so as a failure. It stays quiet.

Also from the same pass:

- `teamai pull` publishes the queue for every repo kind. It sat inside the
  team-repo refresh, which returns early for single-repo and HTTP, so the
  promise contribute makes — "will retry on the next pull" — was only true for
  independent clones.
- The auto-migration skips `learnings-wt/` along with the other worktrees,
  driven by the shared list so the next worktree is covered without anyone
  remembering this file. `pending-learnings/` deliberately still travels: it is
  work the member has already done.
- `recall`'s own index rebuild reads the queue, so a contribution that could not
  be published does not vanish when anything invalidates the index.
- The learnings mirror carries every visible file in an active namespace again,
  not only Markdown.

Refs Tencent#485
@jeff-r2026 jeff-r2026 self-assigned this Sep 17, 2026
@jeff-r2026
jeff-r2026 merged commit d4c57a0 into Tencent:main Sep 17, 2026
8 checks passed
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
…ent#616)

* refactor(branch): extract the orphan-branch worktree engine

`reports-branch.ts` managed the `teamai-reports` orphan branch: cold-start
creation across two git versions, stale-worktree repair, a non-blocking lock,
commit, and push with fetch+rebase retry. Learnings need the same machinery on
their own branch (Tencent#485), and must not share the branch, the worktree or the
lock.

The engine moves to `branch-worktree.ts`, parameterised by a spec of three
names plus an init commit message. Reports become one instance of it and keep
every exported name, so no caller changes. `reports-branch.ts` keeps what is
not a side branch: `EmptyRepoError` and `withKnowledgeWorktree`.

- A publish returns `PublishResult` instead of a boolean. A caller holding the
  only durable copy of its data must be able to tell "landed on origin" from
  "another writer holds the lock", "nothing to commit" and "rejected". Reports
  map it back to today's boolean, so their behaviour is unchanged.
- A publish confirms the ref actually moved before reporting success, and
  separates "nothing to commit" from "nothing to deliver": an earlier attempt
  may have committed the content and failed to push it. A successful push
  updates the remote-tracking ref locally, so the check is a local rev-list.
  Anything unreadable counts as not landed, which costs one extra push instead
  of losing data.
- `getWorktreeDir` and `getBusinessRoot` come out of the reports-specific
  helpers; `getReportsDir` is now one call to the first.
- `usesReportsBranch` was a one-line forward; its eleven call sites use
  `usesBranchWorktree`, which says what it means now that two branches use it.
- A side branch's `.gitignore` lists every worktree directory, so no worktree
  can nest-track another.
- `ensureReportsDir` had no callers and duplicated `ensureReportsWorktree`.

Refs Tencent#485

* feat(learnings): write learnings to teamai-learnings, read them from every root

Contributing no longer touches the default branch, so a member whose team
protects `main` can contribute. Everything the team wrote before the switch
stays readable exactly where it is: nothing is copied, deleted or migrated.

**One accessor.** Eleven call sites built the learnings path by hand, from a
different base each time, so a forgotten one did not fail — it read an empty
directory and recall quietly returned less. `learningsRoots(localConfig)` now
answers with a write root and an ordered read list, and a test fails on a new
hand-built path. Two call sites have no config to resolve (the `viz --repo`
flag and CI) and opt out in place, with the reason on the line.

**Every root is read.** `buildIndex` takes the whole list. For one relative
path the first root wins, deduplicated while collecting: that path is also the
id votes are counted by, so two entries would double-count votes and then have
one silently dropped by recall's dedup. The clone's `learnings/` is always the
last root, which is what keeps the pre-switch corpus searchable.

**One publish path.** `teamai contribute` writes into the `teamai-learnings`
worktree and pushes, for independent clones and single-repo installs alike.
Single-repo mode stops opening a pull request per contribution, so the
disposable knowledge worktree and the machine-wide cache copy that stood in for
it are gone — and with them the cross-project leak of writing one project's
learnings into a cache every project shares. What cannot be published stays in
the durable queue outside the clone and is retried by the next `teamai pull`.

**Maintenance.** Pruning, promotion and confidence write-backs used to mutate a
checkout nothing pushes: the result reached no teammate, and a realign could
undo it. Promotion was worse — the `promoted_to` mark it wrote was discarded,
so the same learning was promoted again on the next run and paid for another
model call. They now write to the write root, publish what they changed, and
refuse to prune an inherited learning instead of deleting a file that comes
back.

Fixes that fall out of reading the same code:

- A recall that had to rebuild the index passed no project namespaces, so every
  project-private learning vanished from the rebuilt index while a pull-built
  one had them.
- Recall printed the absolute path an entry had when it was indexed; a worktree
  that is removed and rebuilt leaves that dangling, and the agent reading the
  output got a dead pointer.
- `pull`'s mirror deletes whatever its source does not have, so it now takes
  every published root except the mirror itself — pointed at one root it would
  have stopped propagating upstream deletions (Tencent#458).
- The learnings count in `pull` deduplicates across roots, the way the index
  does.
- Contributing self-heals the single-repo `.gitignore` first, the way `pull`
  and `push` already do, so the new worktree never shows up in the user's own
  `git status`.
- Recall says why a scope was skipped instead of swallowing the reason: an
  invalid projects manifest reported "No learnings available" and nothing else.

Real-git coverage against a bare origin whose `update` hook refuses the default
branch: the learning lands on the side branch with `main` untouched, a refused
branch leaves the note recoverable, a commit whose push failed is delivered on
the next attempt, two members publishing at once both land, one member reads
the other's learning after a refresh, and the inherited corpus is still there.

Refs Tencent#485

* docs: document the branch layout and the minimum Git permissions (Tencent#486)

A team that turns on branch protection needs to know what still works and what
access its members actually need. The docs said learnings live on the default
branch, and the providers guide said the push target is hardcoded to `master`.

- The data-layout table covers knowledge, learnings, reports and machine-local
  data, and says for each how it is written and whether it needs write access to
  the default branch. EN and ZH match row for row.
- A minimum-permissions section in the usage guide and in the providers guide:
  what a member needs, what they do not, and that `provider: git` still cannot
  open a pull request for you while `contribute` never needs one.
- The directory trees show `learnings-wt/` and `pending-learnings/`.
- The admin checklist says `init` commits an empty `learnings/` and that
  contributions do not go there.
- `docs/providers.md` no longer says the push target is hardcoded: it is
  resolved by `getDefaultBranch()`. The same stale claim in the docstring of
  `pushRepoDirectly` goes with it.
- The share-learnings skill names the branch instead of "directly to master".
- All five READMEs name the branch in the `teamai contribute` row.
- Both design documents describe the new split.

Closes Tencent#486

* fix(contribute): keep a contribution findable when nothing can be published

Splitting this change in two left a gap the full version did not have. Before
learnings moved to their own branch the note was written into the clone, so it
was indexed no matter what git did. Now a contribution is written into the
branch worktree — and when that worktree cannot be created at all, the durable
copy was kept but never indexed, so the member could not recall what they had
just written.

- The queue is a learnings root for the index, in `contribute` and in `pull`.
- The durable copy is written before the index is rebuilt, not after, or there
  would be nothing to index.

The queue stays the fallback here; making it the write path is Tencent#615.

Refs Tencent#485

* fix(learnings): do not call a successful push a failure, and cap how long it can take

A review pass against edge cases found three ways this change could report or
behave worse than what it replaced.

**A successful push read as a failure.** The publish confirmed delivery by
checking that HEAD was no longer ahead of `origin/<branch>`. That ref is only
updated through the remote's FETCH refspec, so in a clone made with
`--single-branch` — what CI checkouts and many business repos are — pushing a
side branch leaves no tracking ref behind and the check said "not landed".
The result was five pushes of the same content and a reported failure although
the data was on origin, and for reports a `save-session` that printed a push
timeout for a write that had worked. A push that resolves is a push the remote
accepted; git exits non-zero when it refuses one. The tracking ref is now only
consulted to answer "does this worktree still owe origin a commit?", where an
unreadable answer means push and find out.

**No cap on how long publishing can take.** `contribute` used to give its push
ten seconds. That cap was lost, so an unreachable origin or a credential prompt
could block the CLI through five push and rebase rounds. Both publish paths
carry it again; timing out is safe because the durable copy stays.

**A warning aimed at a backend with no branch.** Maintenance on an HTTP team
repo has nothing to publish, and said so as a failure. It stays quiet.

Also from the same pass:

- `teamai pull` publishes the queue for every repo kind. It sat inside the
  team-repo refresh, which returns early for single-repo and HTTP, so the
  promise contribute makes — "will retry on the next pull" — was only true for
  independent clones.
- The auto-migration skips `learnings-wt/` along with the other worktrees,
  driven by the shared list so the next worktree is covered without anyone
  remembering this file. `pending-learnings/` deliberately still travels: it is
  work the member has already done.
- `recall`'s own index rebuild reads the queue, so a contribution that could not
  be published does not vanish when anything invalidates the index.
- The learnings mirror carries every visible file in an active namespace again,
  not only Markdown.

Refs Tencent#485
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants