Skip to content

fix(sync): 为会话启动刷新增加冷却和并发复用 - #1683

Merged
SaulMoro merged 6 commits into
Tencent:mainfrom
YogeLiu:fix/1681-background-refresh-cooldown
Nov 3, 2026
Merged

SaulMoro merged 6 commits into
Tencent:mainfrom
YogeLiu:fix/1681-background-refresh-cooldown

Conversation

@YogeLiu

@YogeLiu YogeLiu commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

修复会话启动时重复刷新团队仓库的问题。预热后,连续三次 SessionStart 原来各执行一次 git pull;现在在 60 秒内复用最近成功的刷新。

  • 为 SessionStart 增加内部标记。并发启动等待已有 scope 锁,取得锁后复用成功刷新,各 checkout 仍执行本地下发和核对。
  • 冷却读取不延长有效期。拉取或子模块更新失败时不写入刷新记录,保留上次成功的时间,冷却结束后的第一次启动会重试;inline TTL 仍读取上次成功的刷新。
  • 手动 pull、强制 pull 和显式 team ref 操作仍会刷新。HTTP/self 路径和现有 inline TTL 保持原有语义。
  • 更新中英文成员指南及数据目录设计说明。

Closes #1681

验证基于最新 origin/main(38e13613),在独立 worktree 中完成。先增加回归测试,确认原代码在连续启动、冷却复用和锁等待场景失败,再实现修复。

已通过:

  • npm run build
  • npm run typecheck
  • npm run lint(含文档检查)
  • npm test -- src/__tests__/pull-skip-sync.test.ts src/__tests__/hook-handlers.test.ts src/__tests__/pull-prepare-checkout.test.ts src/__tests__/pull-submodule-skip-sync.test.ts src/__tests__/pull-sync-truth.test.ts:232 项通过。
  • npm run test:e2e -- src/__tests__/e2e/session-start-refresh-1681.test.ts:5 项通过,使用构建后的真实 CLI、本地 bare remote、隔离配置和 Git Trace2,未修改构建产物。

真实 CLI 记录:

场景 结果
手动预热后连续启动三次 三次 git pull 次数均为 0,刷新时间不变
冷却期间启用 Cursor 不拉取远端,skill 正常下发
新建未运行 Git hook 的 checkout 不拉取远端,Claude skill 正常下发
冷却到期 / 手动 pull / 强制 pull / 显式 team ref 均重新刷新
拉取失败后恢复 remote 刷新记录保持上次成功的时间,下次启动重试成功
两个并发启动 第一个刷新 1 次,第二个等待后刷新 0 次;紧接的第三次启动也为 0
另一个团队仓库 独立执行首次刷新

环境:macOS、Node v22.22.3、Git 2.50.1。验证覆盖 git provider 的 Claude 启动入口和 Cursor 下发。其余 provider/agent 组合留给 CI。现有 new-checkout-trigger.test.ts 的 9 项用例因要求 Git >=2.54 而跳过;本 PR 新增的 hookless checkout 场景已独立通过。

行为取舍:团队远端的新提交可能最多延迟 60 秒被自动刷新发现;需要立即同步时执行 teamai pull。

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

未发现当前 diff 中可确认的问题,无需提出审查 finding。

PR 描述的测试记录充分:包含构建、类型检查、lint、232 项单元测试,以及使用构建后真实 CLI 的 5 项端到端测试;覆盖冷却复用、并发锁等待、新 checkout 下发、显式刷新和失败重试。其余 provider/agent 组合已明确留给 CI,不构成阻断项。

本次仅只读审查指定 diff,未运行、构建或安装 PR 中的任何内容。

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

I built main (38e1361) and this head (b28426b), and ran the session-start hook against a local bare remote, counting git pull calls. The cooldown does what the body says:

Case main this head
Three session starts after a manual pull 1 / 1 / 1 pulls 0 / 0 / 0, stamp unchanged
A push to the remote inside the 60 s window seen at once seen after the window, as the member guide says
Remote failing, then restored retried every start retried after the failure
Two overlapping starts, then a third 1 / 0 / 1 1 / 0 / 0, the second waits and delivers

[P1 阻断] A failed fetch now deletes the 24-hour inline record, so a new worktree gets no team skills while the remote is down

pullTeamRepo now removes last-fetch.json when the fetch fails (src/pull.ts, the transitionTeamRepo(...).catch branch). That file is not only the 60-second cooldown: the post-checkout inline pull reads it with the 24-hour TTL (teamFetchedWithinTtl(localConfig) in the options.inline branch) to deliver from the cached clone without refetching.

To reproduce in a workspace install:

  1. Run teamai pull while the remote is up, then age the stamp to 5 minutes. That is inside the 24 hours and outside the 60 seconds.
  2. Make the remote unreachable and run one session start. Its fetch fails.
  3. Run git worktree add.

Result:

  • main: the stamp survives step 2, the hook reads the cached clone (fetched within the TTL, not refetched), and the new worktree has the team's skills.
  • this head: the stamp is gone after step 2, so the hook refetches, the fetch fails again, and the new worktree has no team skill.

This contradicts the body's "现有 inline TTL 保持原有语义" and the new line in data-directory-layout.md. The stamp is only written after a successful fetch, so the cooldown is already correct without the delete. Dropping that rm fixes it. The rm after a failed submodule update changes the inline TTL the same way: keep it or drop it, but make the design note say which.

[P3 可选] A session start can now spend most of the pull handler's budget waiting

A start can wait up to 60 s for the scope lock and then fetch, inside the 120 s pull handler budget, so a slow holder plus a slow fetch could time out. I did not reproduce it.

CI was still running at this head when I checked. The CI review found nothing; the case above is why I disagree.

A failed session-start fetch deleted last-fetch.json, which is also the
24-hour inline TTL record, so a new worktree created while the remote was
down got no team resources. The stamp is only written after a complete
refresh, so the cooldown stays correct without the delete. A failed
submodule update now also leaves the previous stamp in place.
@SaulMoro

SaulMoro commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

I pushed b6c1c2c on top of your branch to close the blocking item, so this can land:

  • A failed fetch or a failed submodule update no longer deletes last-fetch.json. The stamp is still written only after a complete refresh, so the next session start after the 60-second window retries, and a worktree created while the remote is down still gets the team skills through the 24-hour inline check, as on main.
  • New regression test in pull-skip-sync.test.ts for that worktree case. It fails on b28426b (the inline pull fetches a second time) and passes now.
  • The test "retries after a failed forced fetch even if an earlier fetch was recent" relied on the delete. It now checks the [perf] 会话启动后台 pull 缺少短刷新冷却,团队内容未变化时仍重复刷新 #1681 rule directly: a failed start is not recorded as a refresh, so the next start fetches again.
  • docs/designs/data-directory-layout.md says the same.

Locally: npm run typecheck, npm run lint and npm run build pass, and pull-skip-sync, hook-handlers, pull-prepare-checkout and pull-sync-truth pass (231 tests).

Real CLI, isolated HOME, local bare team origin:

Case Pulls
Three session starts after a manual pull 0 / 0 / 0, stamp unchanged
Push inside the window, then after it 0, then 1
Remote down, then restored 1 (fails, stamp kept), then 1 / 0
Two overlapping starts, then a third 1 / 0 / 0
Remote down, then git worktree add no pull, "fetched within the TTL, not refetched", worktree gets the skill

I will approve and squash-merge once CI is green on this head. Thanks for the cooldown.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
  • [P1 阻断] src/__tests__/e2e/session-start-refresh-1681.test.ts:141:此断言必然失败。m.age() 已写入过期记录,随后拉取失败的路径直接返回,并不删除记录;当前实现和新增单元测试也明确保留上次成功时间。因此文件仍存在,不能断言为 false。请改为验证时间未变且恢复 remote 后重试成功。

  • [P2 非阻断] src/pull.ts:341:最近一次成功刷新后的 60 秒内,若手动 pull 拉到新提交但子模块更新失败,旧记录仍有效;紧接着的 SessionStart 会跳过刷新,并返回 submodulesFailed: false,把未完整更新的仓库当作成功刷新使用,违背“失败后下次启动重试”的承诺。建议单独记录失败状态以禁用会话冷却,同时保留供 inline TTL 使用的上次成功时间。

PR 描述包含充分的构建、单元测试及真实 CLI 验证记录,不存在缺少端到端记录的问题;但上述断言与当前实现矛盾,需要修正并重新核验。其余 provider/agent 组合留给 CI 不构成阻断。

此前审查未提出具体 finding,无需标记已解决项。本次仅只读指定 diff,未运行、构建或安装 PR 内容。

@SaulMoro

SaulMoro commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

On the two points in the latest CI review:

  • [P1 blocking], session-start-refresh-1681.test.ts:141: correct. That assertion still expected the stamp to be deleted. 49be912 now checks that a failed refresh leaves the aged stamp unchanged, and the test still checks that the next start retries once the remote is back. The old assertion fails ("expected true to be false"), and with the change npm run test:e2e -- src/__tests__/e2e/session-start-refresh-1681.test.ts passes 5/5. npm run lint passes.
  • [P2 non-blocking], pull.ts:341: I am leaving this out of this PR. It needs a manual pull whose submodule update fails within 60 seconds of a successful refresh. That pull already keeps the old revision and reports the sync as failed, so the only cost is that the next retry waits up to 60 seconds. A separate failure marker would add state to fix that delay. It can be a follow-up if it shows up in practice.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
  • [P2 非阻断] src/pull.ts:341:此前的失败冷却问题仍未修复。成功刷新后 60 秒内,若手动 pull 拉到新提交但子模块更新失败,旧成功记录仍有效;紧接的 SessionStart 会跳过刷新并返回 submodulesFailed: false,将未完整更新的仓库当作成功刷新使用,也不会按指南承诺在下次启动重试。建议保留上次成功时间供 inline TTL 使用,同时记录失败状态以禁用会话冷却。

此前的 E2E 断言问题已解决:src/__tests__/e2e/session-start-refresh-1681.test.ts:142 现在验证失败后时间戳不变,并验证恢复 remote 后重试成功,不再重复提出。

PR 描述包含构建、单元测试及构建后真实 CLI 的端到端验证记录,满足测试记录要求;其余 provider/agent 组合明确留给 CI,不构成阻断项。

本次仅只读检查指定 diff,未运行、构建或安装 PR 中的任何内容。

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
  • [P2 非阻断] src/pull.ts:341:此前的失败冷却问题仍未修复。成功刷新后 60 秒内,若手动 pull 拉到新提交但子模块更新失败,旧成功记录仍有效;紧接的 SessionStart 会跳过刷新并返回 submodulesFailed: false,不会按指南承诺在下次启动重试。建议保留上次成功时间供 inline TTL 使用,同时记录失败状态以禁用会话冷却。PR 描述所称“失败时清除刷新记录”也与实现不符。

此前的 E2E 断言问题已解决:src/__tests__/e2e/session-start-refresh-1681.test.ts:142 验证失败后时间戳不变,并验证恢复 remote 后重试成功,不再重复提出。

PR 描述包含构建、类型检查、lint、单元测试及构建后真实 CLI 的端到端验证记录,满足测试记录要求;其余 provider/agent 组合明确留给 CI,不构成阻断项。

本次仅只读检查指定 diff,未运行、构建或安装 PR 中的任何内容。

@SaulMoro

SaulMoro commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

CI at 49be912 failed on two tests that predate my commits (they failed at b28426b too). I pushed fixes for both; neither changes runtime code:

  • 088141a, launch-context.test.ts: the session-start pull of a workflow-launched tool now passes sessionStart: true, as hook-handlers.ts does since this PR. The test expected { silent: true } only.
  • 862fbfe, e2e/git-exclude-doctor-915.test.ts: its fixture pulls and then starts a session within a second, so the new 60-second cooldown skipped the fetch and the team change the test commits never arrived. The sessionStart helper now ages the fetch stamp first, so each case starts a session after the cooldown, which is what it meant to test.

Locally: launch-context.test.ts passes 29/29 and npm run test:e2e -- src/__tests__/e2e/git-exclude-doctor-915.test.ts passes 15/15 (it failed 2 before); npm run lint passes. I will approve and squash-merge once CI is green on 862fbfe.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
  • [P2 非阻断] src/pull.ts:341:此前的失败冷却问题仍未修复。成功刷新后 60 秒内,若手动 pull 拉到新提交但子模块更新失败,旧成功记录仍有效;紧接的 SessionStart 会跳过刷新并返回 submodulesFailed: false,不会按成员指南承诺在下次启动重试。建议保留成功时间供 inline TTL 使用,同时记录失败状态以禁用会话冷却。PR 描述所称“失败时清除刷新记录”也与实现不符。

此前的 E2E 断言问题已解决:src/__tests__/e2e/session-start-refresh-1681.test.ts:142 验证失败后时间戳不变,并验证恢复 remote 后重试成功,不再重复提出。

PR 描述包含构建、类型检查、lint、单元测试及构建后真实 CLI 的端到端验证记录,满足测试记录要求;其余 provider/agent 组合明确留给 CI,不构成阻断项。

本次仅只读检查指定 diff,未运行、构建或安装 PR 中的任何内容。

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

未发现当前指定 diff 中可确认、需要提出的 finding。

此前问题已处理:

  • E2E 断言已修正:验证失败后时间戳不变,并在恢复 remote 后验证重试成功。
  • 失败冷却问题的文案矛盾已消除:PR 描述及中英文指南明确保留上次成功时间,在冷却结束后的第一次启动重试;实现仍可能在剩余冷却期内复用缓存,这是当前明确的行为取舍,不再重复提出原 finding。

PR 描述的测试记录充分,包含构建、类型检查、lint、232 项单元测试及构建后真实 CLI 的 5 项端到端测试。其余 provider/agent 组合明确留给 CI,不构成阻断项。

本次仅只读审查指定 diff,未运行、构建或安装 PR 中的任何内容。

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

Approving at d50d363. All 18 checks are green and the CI review has no findings: session starts reuse a successful refresh for 60 seconds, overlapping starts wait for the same lock, and a failed refresh keeps the last successful stamp, so new worktrees still get the team resources while the remote is down. Thanks for the cooldown.

@SaulMoro
SaulMoro merged commit 164fac9 into Tencent:main Nov 3, 2026
18 checks passed
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.

[perf] 会话启动后台 pull 缺少短刷新冷却,团队内容未变化时仍重复刷新

3 participants