fix: stage file esterni in /tmp con nome univoco (niente --add-dir)

Su richiesta: invece di copiare i file esterni in ~/.agy-scratch con --add-dir,
ora vengono copiati in /tmp (cartella nativamente accessibile da agy, verificato)
con nome univoco casuale (agy_stage_<timestamp>_<random><ext>) per evitare
sovrascritture. Rimosso --add-dir in executeAgy: /tmp è già accessibile.
Testato: file /tmp analizzato da agy senza --add-dir.
This commit is contained in:
2026-08-11 01:00:01 +02:00
parent 7fa1f028d9
commit f9a8648c86
+9 -11
View File
@@ -26,8 +26,6 @@ const execFileAsync = promisify(execFile);
const AGY_CHAT_DIR = path.join(os.homedir(), ".agy-chat"); const AGY_CHAT_DIR = path.join(os.homedir(), ".agy-chat");
const STATE_FILE = path.join(AGY_CHAT_DIR, "conversation_id"); const STATE_FILE = path.join(AGY_CHAT_DIR, "conversation_id");
const CONV_DIR = path.join(os.homedir(), ".gemini", "antigravity-cli", "conversations"); const CONV_DIR = path.join(os.homedir(), ".gemini", "antigravity-cli", "conversations");
// Cartella scratch per i file esterni da analizzare (workaround accesso agy)
const AGY_SCRATCH = path.join(os.homedir(), ".agy-scratch");
const DEFAULT_TIMEOUT_MS = 180_000; // 3 min const DEFAULT_TIMEOUT_MS = 180_000; // 3 min
const IMAGE_TIMEOUT_MS = 300_000; // 5 min const IMAGE_TIMEOUT_MS = 300_000; // 5 min
@@ -681,14 +679,14 @@ ${blocks.join("\n\n")}`;
} }
// Workaround: se un file da analizzare è FUORI dalla cartella di lavoro corrente, // Workaround: se un file da analizzare è FUORI dalla cartella di lavoro corrente,
// lo copia in una cartella scratch accessibile e riscrive i riferimenti nel prompt. // lo copia in /tmp (cartella nativamente accessibile da agy) con un nome univoco
// Previene errori di accesso/competenza quando agy legge file esterni al workspace. // casuale e riscrive i riferimenti nel prompt. Previene errori di accesso/competenza
// quando agy legge file esterni al workspace, senza sovrascrivere file esistenti.
function stageExternalFiles( function stageExternalFiles(
prompt: string, prompt: string,
filePaths: string[], filePaths: string[],
): { prompt: string; filePaths: string[]; dirs: string[] } { ): { prompt: string; filePaths: string[]; dirs: string[] } {
const cwdBase = path.resolve(process.cwd()) + path.sep; const cwdBase = path.resolve(process.cwd()) + path.sep;
const dirs = new Set<string>();
const stagedPaths: string[] = []; const stagedPaths: string[] = [];
let newPrompt = prompt; let newPrompt = prompt;
@@ -701,19 +699,20 @@ function stageExternalFiles(
continue; continue;
} }
try { try {
fs.mkdirSync(AGY_SCRATCH, { recursive: true }); const ext = path.extname(abs);
const dest = path.join(AGY_SCRATCH, path.basename(abs)); const unique = `agy_stage_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}${ext}`;
const dest = path.join(os.tmpdir(), unique);
fs.copyFileSync(abs, dest); fs.copyFileSync(abs, dest);
// riscrivi i riferimenti nel prompt (path originale e assoluto) // riscrivi i riferimenti nel prompt (path originale e assoluto)
newPrompt = newPrompt.split(f).join(dest).split(abs).join(dest); newPrompt = newPrompt.split(f).join(dest).split(abs).join(dest);
stagedPaths.push(dest); stagedPaths.push(dest);
dirs.add(AGY_SCRATCH);
} catch { } catch {
// se la copia fallisce, lascia il path originale (potrebbe comunque funzionare) // se la copia fallisce, lascia il path originale (potrebbe comunque funzionare)
stagedPaths.push(f); stagedPaths.push(f);
} }
} }
return { prompt: newPrompt, filePaths: stagedPaths, dirs: [...dirs] }; // /tmp è accessibile nativamente da agy → nessun --add-dir necessario
return { prompt: newPrompt, filePaths: stagedPaths, dirs: [] };
} }
async function executeAgy(opts: AgyExecOptions) { async function executeAgy(opts: AgyExecOptions) {
@@ -722,11 +721,10 @@ async function executeAgy(opts: AgyExecOptions) {
const finalPrompt = opts.contextBlock ? `${opts.contextBlock}\n\n${staged.prompt}` : staged.prompt; const finalPrompt = opts.contextBlock ? `${opts.contextBlock}\n\n${staged.prompt}` : staged.prompt;
const args: string[] = ["-p", finalPrompt]; const args: string[] = ["-p", finalPrompt];
// add-dir per i file // add-dir per i file (i file esterni sono stagizzati in /tmp, accessibile nativamente)
const dirs = new Set<string>(); const dirs = new Set<string>();
for (const d of opts.addDirs ?? []) if (d) dirs.add(d); for (const d of opts.addDirs ?? []) if (d) dirs.add(d);
for (const f of staged.filePaths) if (f) dirs.add(path.dirname(f)); for (const f of staged.filePaths) if (f) dirs.add(path.dirname(f));
for (const d of staged.dirs) dirs.add(d);
for (const d of dirs) args.push("--add-dir", d); for (const d of dirs) args.push("--add-dir", d);
// conversazione // conversazione