dsh-ai-collab 0.4.3 → 0.5.0

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.
@@ -82,6 +82,40 @@ $ErrorActionPreference = 'Stop'
82
82
 
83
83
  $Utf8NoBom = New-Object System.Text.UTF8Encoding($false)
84
84
 
85
+ # 并发保护:写临时文件再替换,避免读者看到半截 JSON/Markdown。
86
+ function Write-TextAtomic([string]$Path, [string]$Text) {
87
+ $dir = Split-Path -Parent $Path
88
+ if ($dir -and -not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Force -Path $dir | Out-Null }
89
+ $tmp = Join-Path $dir ('.' + [System.IO.Path]::GetFileName($Path) + '.' + [guid]::NewGuid().ToString('N') + '.tmp')
90
+ [System.IO.File]::WriteAllText($tmp, $Text, $Utf8NoBom)
91
+ if (-not (Test-Path -LiteralPath $Path)) { Move-Item -LiteralPath $tmp -Destination $Path -Force; return }
92
+ for ($i = 0; $i -lt 8; $i++) {
93
+ try { [System.IO.File]::Replace($tmp, $Path, [NullString]::Value); return }
94
+ catch [System.IO.IOException] { Start-Sleep -Milliseconds (30 * ($i + 1)) }
95
+ }
96
+ Move-Item -LiteralPath $tmp -Destination $Path -Force
97
+ }
98
+
99
+ # 跨进程互斥:CreateNew 是原子的;超时抛错,由调用方决定是否继续。
100
+ function Enter-ProjectLock([string]$Path, [int]$TimeoutMs = 15000) {
101
+ $sw = [Diagnostics.Stopwatch]::StartNew()
102
+ while ($true) {
103
+ try {
104
+ $fs = [System.IO.File]::Open($Path, [System.IO.FileMode]::CreateNew, [System.IO.FileAccess]::Write, [System.IO.FileShare]::None)
105
+ try {
106
+ $bytes = [Text.Encoding]::UTF8.GetBytes(("pid={0} ts={1}" -f $PID, (Get-Date).ToString('o')))
107
+ $fs.Write($bytes, 0, $bytes.Length); $fs.Flush()
108
+ } finally { $fs.Dispose() }
109
+ return
110
+ }
111
+ catch [System.IO.IOException] {
112
+ if ($sw.ElapsedMilliseconds -gt $TimeoutMs) { throw "获取锁超时: $Path" }
113
+ Start-Sleep -Milliseconds 50
114
+ }
115
+ }
116
+ }
117
+ function Exit-ProjectLock([string]$Path) { Remove-Item -LiteralPath $Path -Force -ErrorAction SilentlyContinue }
118
+
85
119
  $ActionEnum = @('task_start', 'task_done', 'fix', 'diagnosis', 'build', 'commit',
86
120
  'doc', 'review', 'rollback', 'correction', 'maintenance', 'handoff')
87
121
  $StatusEnum = @('ok', 'partial', 'blocked', 'todo', 'failed')
@@ -289,7 +323,7 @@ function Save-StatusObject([string]$ProjectRoot, $Object) {
289
323
  $pretty = ConvertTo-Json -InputObject $Object -Depth 8
290
324
  $pretty = ConvertFrom-JsonEscapes $pretty
291
325
  $pretty = Format-JsonPretty $pretty
292
- [System.IO.File]::WriteAllText($path, ($pretty -replace "`r`n", "`n") + "`n", $Utf8NoBom)
326
+ Write-TextAtomic $path (($pretty -replace "`r`n", "`n") + "`n")
293
327
  return $path
294
328
  }
295
329
 
@@ -487,9 +521,13 @@ function Invoke-Add {
487
521
  $logPath = Get-LogPath $ProjectRoot
488
522
  $dir = Split-Path -Parent $logPath
489
523
  if (-not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Force -Path $dir | Out-Null }
490
- [System.IO.File]::AppendAllText($logPath, $line + "`n", $Utf8NoBom)
491
-
492
- Update-StatusFromLog $ProjectRoot $ordered
524
+ $lockPath = Join-Path $dir '.log.lock'
525
+ [void](Enter-ProjectLock $lockPath)
526
+ try {
527
+ [System.IO.File]::AppendAllText($logPath, $line + "`n", $Utf8NoBom)
528
+ Update-StatusFromLog $ProjectRoot $ordered
529
+ }
530
+ finally { Exit-ProjectLock $lockPath }
493
531
 
494
532
  # 自动刷新跨项目看板(约定:项目父目录存在 .ai\board.md)。-NoBoard 可关闭。
495
533
  if (-not $NoBoard) {
@@ -675,7 +713,7 @@ Record work through `.ai\llm.cmd` (status / tail / add / check). Never rewrite t
675
713
  $content = $template.Replace('{{TITLE}}', $t.Title).Replace('{{TOOL}}', $t.Tool).Replace('{{ROOT}}', $ProjectRoot)
676
714
  $dest = Join-Path $entriesDir $t.Name
677
715
  if ((Test-Path -LiteralPath $dest) -and -not $Force) { continue }
678
- [System.IO.File]::WriteAllText($dest, ($content -replace "`r`n", "`n") + "`n", $Utf8NoBom)
716
+ Write-TextAtomic $dest (($content -replace "`r`n", "`n") + "`n")
679
717
  }
680
718
  Write-Info ("母版已更新: .ai\entries\({0} 个)" -f $targets.Count)
681
719
 
@@ -726,7 +764,10 @@ function Invoke-Tasks([string]$ProjectRoot) {
726
764
  if ($rows.Count -eq 0) { [void]$sb.AppendLine('| (暂无记录) | | | | |') }
727
765
 
728
766
  $out = Join-Path $ProjectRoot 'TASKS.md'
729
- [System.IO.File]::WriteAllText($out, $sb.ToString().Replace("`r`n", "`n"), $Utf8NoBom)
767
+ $lock = Join-Path $ProjectRoot '.ai\.tasks.lock'
768
+ [void](Enter-ProjectLock $lock)
769
+ try { Write-TextAtomic $out ($sb.ToString().Replace("`r`n", "`n")) }
770
+ finally { Exit-ProjectLock $lock }
730
771
  if (-not $Quiet) { Write-Info ("已生成任务清单: {0}({1} 条)" -f $out, $rows.Count) }
731
772
  }
732
773
  function Invoke-Archive([string]$ProjectRoot) {
@@ -769,12 +810,21 @@ function Invoke-Archive([string]$ProjectRoot) {
769
810
  $archiveFile = Join-Path $archiveDir ("log-{0}-{1}.jsonl" -f (Get-Date).Year, $quarter)
770
811
  $backupFile = Join-Path $archiveDir ("log-{0}-backup-{1}.jsonl" -f (Get-Date).ToString('yyyyMMdd-HHmmss'), $quarter)
771
812
 
772
- Copy-Item -LiteralPath $logPath -Destination $backupFile -Force
773
- foreach ($line in $archived) { [System.IO.File]::AppendAllText($archiveFile, $line + "`n", $Utf8NoBom) }
813
+ $lockPath = Join-Path (Join-Path $ProjectRoot '.ai') '.log.lock'
814
+ [void](Enter-ProjectLock $lockPath)
815
+ try {
816
+ # 归档基于之前读取的行;若期间有并发追加,宁可中止也不能丢记录。
817
+ $currentCount = @([System.IO.File]::ReadAllLines($logPath, [Text.Encoding]::UTF8) | Where-Object { $_.Trim() }).Count
818
+ if ($currentCount -ne $lines.Count) { throw '归档期间日志有新增记录,已中止;请重试' }
774
819
 
775
- $keepText = ''
776
- if ($keep.Count -gt 0) { $keepText = ($keep -join "`n") + "`n" }
777
- [System.IO.File]::WriteAllText($logPath, $keepText, $Utf8NoBom)
820
+ Copy-Item -LiteralPath $logPath -Destination $backupFile -Force
821
+ foreach ($line in $archived) { [System.IO.File]::AppendAllText($archiveFile, $line + "`n", $Utf8NoBom) }
822
+
823
+ $keepText = ''
824
+ if ($keep.Count -gt 0) { $keepText = ($keep -join "`n") + "`n" }
825
+ Write-TextAtomic $logPath $keepText
826
+ }
827
+ finally { Exit-ProjectLock $lockPath }
778
828
 
779
829
  Write-Info ("已归档 {0} 条 → {1}" -f $archived.Count, $archiveFile)
780
830
  Write-Info ("整文件备份 → {0}" -f $backupFile)
@@ -869,7 +919,7 @@ function Invoke-Board([string]$ProjectRoot) {
869
919
 
870
920
  $outDir = Split-Path -Parent $Out
871
921
  if (-not (Test-Path -LiteralPath $outDir)) { New-Item -ItemType Directory -Force -Path $outDir | Out-Null }
872
- [System.IO.File]::WriteAllText($Out, $sb.ToString().Replace("`r`n", "`n"), $Utf8NoBom)
922
+ Write-TextAtomic $Out ($sb.ToString().Replace("`r`n", "`n"))
873
923
  Write-Info ("已生成看板: {0}({1} 个项目,{2} 个未接入)" -f $Out, $rows.Count, $unadopted.Count)
874
924
  }
875
925