task-ledger 1.2.3 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -118,7 +118,9 @@
118
118
  - 临时文件名唯一(`mkstemp`),写完 `fsync` 再 `os.replace`,无论成败都清理临时文件
119
119
  - key 按字母序落盘,减少两人同时新增时的文本冲突
120
120
  - 冲突时**报错要求重跑**,不静默覆盖
121
- - 脚本里**没有任何** `git pull` / `rebase` / `commit` —— 冲突不要靠拉取去"解决"
121
+ - 写入成功后脚本会**自动同步**:把本次改动的台账文件 `git add <精确文件>` + `commit`(消息 `ledger(<命令>): <提交者>`);当前分支有上游跟踪分支时再 `push`。全程确定性脚本,不依赖大模型
122
+ - 无 git 仓库 / 无上游跟踪 / 被 `.gitignore` 排除 / 无实际变更 都会静默跳过;`--no-sync` 或环境变量 `TASK_LEDGER_NO_SYNC=1` 可关闭
123
+ - 冲突**仍不靠 `git pull` / `rebase` 解决**:并发靠指纹复查 + 原子写防覆盖,冲突时**报错要求重跑**,不静默合并
122
124
 
123
125
  ### 结案是原子操作
124
126
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "task-ledger",
3
- "version": "1.2.3",
3
+ "version": "1.2.4",
4
4
  "description": "Task-ledger skill for coding agents: per-workspace progress tracking (ledger/progress.json, history, worklog) driven by a CLI script. Standalone install for CodeBuddy and other agents; also bundled inside the dsh-task-ledger DSH plugin.",
5
5
  "keywords": [
6
6
  "skill",
@@ -245,6 +245,10 @@ def read_json(path: Path) -> dict[str, Any]:
245
245
  return data
246
246
 
247
247
 
248
+ # 本次进程内被台账写入的真实文件路径,供写成功后一次性自动同步(git commit/push)。
249
+ CHANGED_FILES: list[Path] = []
250
+
251
+
248
252
  def write_text_atomic(path: Path, text: str) -> None:
249
253
  """原子写文本:同目录唯一临时名 + fsync + os.replace,写完清理临时文件。
250
254
 
@@ -265,6 +269,7 @@ def write_text_atomic(path: Path, text: str) -> None:
265
269
  fh.flush()
266
270
  os.fsync(fh.fileno())
267
271
  os.replace(tmp, path)
272
+ CHANGED_FILES.append(path) # 记下本次改动,供写后自动同步
268
273
  tmp = None
269
274
  except OSError as exc:
270
275
  raise Fail(f"写 {path} 失败:{exc}")
@@ -349,6 +354,68 @@ def commit_guarded(
349
354
  write_json(path, data)
350
355
 
351
356
 
357
+ def _git(*args: str, timeout: int | None = None) -> subprocess.CompletedProcess:
358
+ """跑一条 git 命令,吞掉输出(同步用,不污染 stdout)。"""
359
+ return subprocess.run(list(args), capture_output=True, text=True, timeout=timeout)
360
+
361
+
362
+ def _git_actor(repo_root: str) -> str:
363
+ """提交者:复用 git 身份;取不到用占位,不阻断提交。"""
364
+ out = _git("git", "-C", repo_root, "config", "user.name")
365
+ return out.stdout.strip() or "ledger-script"
366
+
367
+
368
+ def git_sync(action: str) -> None:
369
+ """写成功后调用一次:把本次改动的台账文件提交;有上游跟踪分支则推送。
370
+
371
+ 纯脚本、确定性,不依赖大模型。无 git / 无上游 / 全被 ignore / 无实际变更 都静默跳过。
372
+ 同步是尽力而为:任何异常都不影响已完成的台账写入(只打印提示)。
373
+ """
374
+ if os.environ.get("TASK_LEDGER_NO_SYNC"):
375
+ return
376
+ try:
377
+ files = [p for p in CHANGED_FILES if p.exists()]
378
+ if not files:
379
+ return
380
+ # 从任一文件反查仓库根,不依赖调用方透传 --repo
381
+ top = _git("git", "-C", str(files[0].parent), "rev-parse", "--show-toplevel")
382
+ if top.returncode != 0:
383
+ return # 不是 git 项目 → 跳过(规则1:有才提交)
384
+ root = top.stdout.strip()
385
+ # 只 add 本次真实改动、且未被 ignore 的文件
386
+ to_add = [
387
+ str(f)
388
+ for f in files
389
+ if _git("git", "-C", root, "check-ignore", "-q", str(f)).returncode != 0
390
+ ]
391
+ if not to_add:
392
+ return # 全被 ignore → 尊重用户选择,不强行提交
393
+ _git("git", "-C", root, "add", *to_add)
394
+ if _git("git", "-C", root, "diff", "--cached", "--quiet").returncode == 0:
395
+ return # 无实际变更
396
+ actor = _git_actor(root)
397
+ _git("git", "-C", root, "commit", "-m", f"ledger({action}): {actor}")
398
+ # 规则2:有上游跟踪分支才推
399
+ if _git("git", "-C", root, "rev-parse", "--abbrev-ref", "@{upstream}").returncode != 0:
400
+ return
401
+ try:
402
+ _git("git", "-C", root, "push", timeout=30)
403
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
404
+ # 非快进:自动 rebase 一次再推;冲突 / 超时则放弃,绝不 --force
405
+ try:
406
+ _git("git", "-C", root, "pull", "--rebase", "--autostash", timeout=30)
407
+ _git("git", "-C", root, "push", timeout=30)
408
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
409
+ print(
410
+ "提示:本地已提交,但推送被拒(他人已推 / 需认证 / 超时)。"
411
+ "请手动 `git pull --rebase` 解决后重试;本次改动已安全留在本地。",
412
+ file=sys.stderr,
413
+ )
414
+ except Exception as exc: # noqa: BLE001 —— 同步失败不应污染台账主流程
415
+ print(f"提示:写后自动同步未执行({type(exc).__name__}: {exc})。数据已写入本地。",
416
+ file=sys.stderr)
417
+
418
+
352
419
  def require_task_id(tid: str) -> str:
353
420
  """任务号会参与拼路径,任何命令拿到它都要先验一遍。"""
354
421
  if not ID_RE.fullmatch(tid):
@@ -2070,6 +2137,11 @@ def build_parser() -> argparse.ArgumentParser:
2070
2137
  prog="progress.py",
2071
2138
  description="管理项目任务进度(ledger/progress.json + worklog + 历史)",
2072
2139
  )
2140
+ parser.add_argument(
2141
+ "--no-sync",
2142
+ action="store_true",
2143
+ help="跳过写后自动 git 提交 / 推送(等价于 TASK_LEDGER_NO_SYNC=1)",
2144
+ )
2073
2145
  sub = parser.add_subparsers(dest="command", required=True)
2074
2146
 
2075
2147
  def add_repo(p: argparse.ArgumentParser) -> None:
@@ -2202,6 +2274,8 @@ def build_parser() -> argparse.ArgumentParser:
2202
2274
 
2203
2275
  def main(argv: list[str] | None = None) -> int:
2204
2276
  args = build_parser().parse_args(argv)
2277
+ if getattr(args, "no_sync", False):
2278
+ os.environ["TASK_LEDGER_NO_SYNC"] = "1"
2205
2279
  try:
2206
2280
  args.func(args)
2207
2281
  except Fail as exc:
@@ -2221,6 +2295,7 @@ def main(argv: list[str] | None = None) -> int:
2221
2295
  except Exception as exc: # noqa: BLE001 —— 兜底:未预期的失败用独立退出码,与业务错误区分
2222
2296
  print(f"错误:未预期的失败 {type(exc).__name__}: {exc}", file=sys.stderr)
2223
2297
  return 3
2298
+ git_sync(args.command) # 写成功后一次性自动同步(commit / push)
2224
2299
  return 0
2225
2300
 
2226
2301