Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

test(install.ps1): install while the unpacked binary is held open

Vinícius DS Andrade8ec93d2a18b1
diff --git a/tests/install_ps1.ps1 b/tests/install_ps1.ps1
index 489fe7a9..2ea130f6 100644
--- a/tests/install_ps1.ps1
+++ b/tests/install_ps1.ps1
@@ -1,7 +1,8 @@
# install.ps1 against crafted releases served over local HTTP: a good release
-# installs (under PowerShell 7 and Windows PowerShell 5.1), and a checksum
-# mismatch, an archive entry escaping the install dir, and an existing install
-# are each refused with nothing written. The user PATH is never touched
+# installs (under PowerShell 7 and Windows PowerShell 5.1), also while another
+# process holds the unpacked binary open, and a checksum mismatch, an archive
+# entry escaping the install dir, and an existing install are each refused
+# with nothing written. The user PATH is never touched
# (AI_MEMORY_NO_MODIFY_PATH=1). Needs python on PATH for the HTTP server.
#
# Usage: pwsh -NoProfile -File tests/install_ps1.ps1 -Binary target\debug\ai-memory.exe
@@ -82,6 +83,90 @@ try {
if ((Get-Content $again -Raw) -notmatch 'ai-memory upgrade') { Fail "$shell did not point to upgrade: $(Get-Content $again -Raw)" }
}
+ # An antivirus scan of the unpacked binary holds it open, sharing read and
+ # write but not delete access, so Windows refuses to rename or delete it
+ # (#1480). The install must still go through. TMP points at a directory of
+ # its own, so the watcher sees only the staged release.
+ Add-Type -TypeDefinition @'
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Threading;
+
+public static class StagedBinaryScan
+{
+ static readonly List<FileStream> held = new List<FileStream>();
+ static FileSystemWatcher watcher;
+ static volatile bool stopped;
+
+ public static void Start(string root)
+ {
+ watcher = new FileSystemWatcher(root, "ai-memory.exe");
+ watcher.IncludeSubdirectories = true;
+ watcher.Created += (sender, e) => Hold(e.FullPath);
+ watcher.EnableRaisingEvents = true;
+ }
+
+ // Retried: the unpacker may still be writing the file without sharing it.
+ static void Hold(string path)
+ {
+ DateTime deadline = DateTime.UtcNow.AddSeconds(60);
+ while (!stopped && DateTime.UtcNow < deadline)
+ {
+ try
+ {
+ FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+ lock (held)
+ {
+ if (stopped) { stream.Dispose(); } else { held.Add(stream); }
+ }
+ return;
+ }
+ catch (Exception)
+ {
+ Thread.Sleep(1);
+ }
+ }
+ }
+
+ public static int Count
+ {
+ get { lock (held) { return held.Count; } }
+ }
+
+ public static void Stop()
+ {
+ stopped = true;
+ watcher.Dispose();
+ lock (held)
+ {
+ foreach (FileStream stream in held) { stream.Dispose(); }
+ held.Clear();
+ }
+ }
+}
+'@
+ $scanTemp = Join-Path $tmp 'scan-temp'
+ New-Item -ItemType Directory -Path $scanTemp | Out-Null
+ $savedTmp, $savedTemp = $env:TMP, $env:TEMP
+ [StagedBinaryScan]::Start($scanTemp)
+ try {
+ $env:TMP = $scanTemp
+ $env:TEMP = $scanTemp
+ foreach ($shell in @('pwsh', 'powershell')) {
+ $dest = Join-Path $tmp "inst-scan-$shell\ai-memory"
+ $log = Join-Path $tmp "scan-$shell.log"
+ if ((Install $shell 'good' $dest $log) -ne 0) { Fail "$shell failed while the unpacked binary was held open: $(Get-Content $log -Raw)" }
+ if (-not (Test-Path (Join-Path $dest 'ai-memory.exe'))) { Fail "$shell did not install ai-memory.exe while it was held open" }
+ if (-not (Test-Path (Join-Path $dest 'hooks\_lib.sh'))) { Fail "$shell did not install hooks while ai-memory.exe was held open" }
+ }
+ if ([StagedBinaryScan]::Count -lt 2) { Fail "the unpacked binary was held open in $([StagedBinaryScan]::Count) of 2 installs" }
+ } finally {
+ $env:TMP = $savedTmp
+ $env:TEMP = $savedTemp
+ [StagedBinaryScan]::Stop()
+ }
+
Publish badsum -BadSum | Out-Null
$dest = Join-Path $tmp 'inst-badsum\ai-memory'
$log = Join-Path $tmp 'badsum.log'