@juanbenjumea/pi-agy 0.7.7 → 0.7.9

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.
@@ -3,27 +3,47 @@
3
3
  *
4
4
  * Each Pi turn runs on a persistent agy driver process (see
5
5
  * provider-driver.ts) with the transcript rendered as text. Response text
6
- * streams back into Pi's TUI; agy's own tool activity surfaces as thinking
7
- * events (`[agy tool: …]`) for visibility.
8
- *
9
- * Architectural wall (Phase 1): agy runs its OWN closed tool loop against
10
- * the working directory. Pi's read/write/edit/bash tools never fire for
11
- * these turns, so Pi's inline diff review does not engage — edits already
12
- * landed on disk. Full Pi-tool bridging (MCP bridge + approval gates) is
13
- * Phase 2; until then, prefer `agy_execute` delegation from a trusted Pi
14
- * model when you need Pi-owned tool execution and diff review.
6
+ * streams into Pi's TUI, and agy's tool activity appears as thinking events
7
+ * (`[agy tool: …]`). By default, a loopback MCP bridge parks agy's Pi-tool
8
+ * calls for execution by Pi, so its normal permissions, approvals, and diff
9
+ * review apply. The bridge is separate from agy's own tools and can be disabled
10
+ * with `providerBridge: false`.
15
11
  */
16
12
 
17
13
  import { randomBytes } from "node:crypto";
18
- import { chmod, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
19
- import * as os from "node:os";
20
14
  import * as path from "node:path";
21
15
 
22
16
  import { loadAgyConfig } from "./config.js";
17
+ import { projectBridgeTools, bridgeToolsFingerprint, type ProviderToolSource } from "./provider-tools.js";
18
+ export { projectBridgeTools, bridgeToolsFingerprint, type ProviderToolSource } from "./provider-tools.js";
19
+
20
+ import {
21
+ clearImageStaging,
22
+ collectImageBlocks,
23
+ ensureImageStagingRoot,
24
+ getLiveImageStagingCount,
25
+ renderProviderPrompt,
26
+ stageImages,
27
+ textOfContent,
28
+ type ProviderTranscriptMessage,
29
+ } from "./provider-prompt.js";
30
+ export {
31
+ clearImageStaging,
32
+ collectImageBlocks,
33
+ ensureImageStagingRoot,
34
+ imageExtension,
35
+ renderProviderPrompt,
36
+ stageImages,
37
+ MAX_IMAGE_BYTES,
38
+ MAX_STAGED_IMAGES,
39
+ type PromptImageBlock,
40
+ type ProviderTranscriptMessage,
41
+ type StagedImages,
42
+ } from "./provider-prompt.js";
43
+
23
44
  import { withDirLock } from "./lock.js";
24
45
  import {
25
46
  startMcpBridge,
26
- toSafeBridgeName,
27
47
  type BridgeToolDef,
28
48
  type McpBridgeHandle,
29
49
  } from "./provider-mcp.js";
@@ -42,19 +62,6 @@ export const AGY_PROVIDER_ID = "antigravity";
42
62
  /** Custom API sentinel; no built-in provider claims it. */
43
63
  export const AGY_PROVIDER_API = "agy-provider";
44
64
 
45
- /** Bounds for transcript rendering into a single turn prompt. */
46
- const MAX_HISTORY_CHARS = 24_000;
47
- const MAX_SYSTEM_CHARS = 8_000;
48
- const MAX_MESSAGE_CHARS = 6_000;
49
-
50
- /** Minimal structural transcript message (subset of Pi's Message). */
51
- export interface ProviderTranscriptMessage {
52
- role: string;
53
- content?: unknown;
54
- toolName?: string;
55
- toolCallId?: string;
56
- isError?: boolean;
57
- }
58
65
 
59
66
  export interface ProviderTurnCallbacks {
60
67
  onText?: (delta: string) => void;
@@ -125,325 +132,6 @@ export interface ProviderTurnResult {
125
132
  deniedActions?: Array<{ action?: string; display_name?: string }>;
126
133
  }
127
134
 
128
- function textOfContent(content: unknown, imagePath?: (block: object) => string | undefined): string {
129
- if (typeof content === "string") return content;
130
- if (Array.isArray(content)) {
131
- const parts: string[] = [];
132
- for (const block of content) {
133
- if (typeof block === "string") {
134
- parts.push(block);
135
- } else if (block && typeof block === "object") {
136
- const record = block as Record<string, unknown>;
137
- if (record.type === "text" && typeof record.text === "string") {
138
- parts.push(record.text);
139
- } else if (record.type === "image") {
140
- const staged = imagePath?.(block);
141
- parts.push(
142
- staged ?? "[image omitted: agy provider turns are text-only]",
143
- );
144
- } else if (record.type === "toolCall") {
145
- parts.push(`[tool call: ${String(record.name ?? "unknown")}]`);
146
- } else if (record.type === "thinking" && typeof record.thinking === "string") {
147
- parts.push(record.thinking);
148
- }
149
- }
150
- }
151
- return parts.join("\n");
152
- }
153
- return "";
154
- }
155
-
156
- function clip(text: string, max: number): string {
157
- if (text.length <= max) return text;
158
- return text.slice(0, max - 1) + "…";
159
- }
160
-
161
- export interface PromptImageBlock {
162
- /** Identity of the transcript block (lookup key, never serialized). */
163
- block: object;
164
- data: string;
165
- mimeType: string;
166
- }
167
-
168
- export interface StagedImages {
169
- /** Per-turn subdir, undefined when nothing stageable. */
170
- dir: string | undefined;
171
- /** Block identity → absolute path (order-independent lookup). */
172
- paths: Map<object, string>;
173
- /** Remove this staging (idempotent, never throws). */
174
- cleanup: () => Promise<void>;
175
- }
176
-
177
- /** Cap staged images per invocation (count and per-file size). */
178
- export const MAX_STAGED_IMAGES = 8;
179
- export const MAX_IMAGE_BYTES = 8 * 1024 * 1024;
180
-
181
- const IMAGE_EXTENSIONS: Record<string, string> = {
182
- "image/png": "png",
183
- "image/jpeg": "jpg",
184
- "image/webp": "webp",
185
- "image/gif": "gif",
186
- "image/bmp": "bmp",
187
- "image/svg+xml": "svg",
188
- };
189
-
190
- export function imageExtension(mimeType: string): string {
191
- return IMAGE_EXTENSIONS[mimeType.toLowerCase()] ?? "bin";
192
- }
193
-
194
- /** Collect image blocks in transcript order, optionally only one tool result. */
195
- export function collectImageBlocks(
196
- messages: ProviderTranscriptMessage[],
197
- onlyToolCallId?: string,
198
- ): PromptImageBlock[] {
199
- const blocks: PromptImageBlock[] = [];
200
- for (const message of messages) {
201
- if (onlyToolCallId !== undefined) {
202
- if (message.role !== "toolResult" || message.toolCallId !== onlyToolCallId) continue;
203
- }
204
- const content = message.content;
205
- if (!Array.isArray(content)) continue;
206
- for (const block of content) {
207
- if (typeof block !== "object" || block === null) continue;
208
- const record = block as Record<string, unknown>;
209
- if (record.type !== "image") continue;
210
- if (typeof record.data !== "string" || typeof record.mimeType !== "string") continue;
211
- blocks.push({ block, data: record.data, mimeType: record.mimeType });
212
- }
213
- }
214
- return blocks;
215
- }
216
-
217
- /** Strict base64 (Node's decoder silently strips non-alphabet chars). */
218
- const STRICT_BASE64_RE = /^[A-Za-z0-9+/]*={0,2}$/;
219
-
220
- function decodeImageData(data: string): Buffer | undefined {
221
- if (typeof data !== "string" || data.length === 0 || data.length % 4 !== 0) return undefined;
222
- if (!STRICT_BASE64_RE.test(data)) return undefined;
223
- try {
224
- const bytes = Buffer.from(data, "base64");
225
- if (bytes.length === 0 || bytes.length > MAX_IMAGE_BYTES) return undefined;
226
- return bytes;
227
- } catch {
228
- return undefined;
229
- }
230
- }
231
-
232
- /** Process-stable staging root (0700): per-turn subdirs, one --add-dir. */
233
- let imageStagingRoot: string | undefined;
234
-
235
- /** Live staging dirs (teardown sweep; per-turn cleanup disposes handles). */
236
- const liveStagingDirs = new Set<string>();
237
-
238
- /** Resolve (creating once) the process staging root for agy --add-dir. */
239
- export async function ensureImageStagingRoot(): Promise<string> {
240
- if (!imageStagingRoot) {
241
- // mkdtemp (not join+mkdir): atomic, unique, 0700 — no pre-creation or
242
- // symlink race between check and creation.
243
- imageStagingRoot = await mkdtemp(path.join(os.tmpdir(), "pi-agy-images-"));
244
- try {
245
- await chmod(imageStagingRoot, 0o700);
246
- } catch {
247
- // mkdtemp already guarantees 0700; best effort.
248
- }
249
- }
250
- return imageStagingRoot;
251
- }
252
-
253
- /**
254
- * Materialize image blocks as files agy's tools can open. Newest transcript
255
- * images win (oldest starve first) so a fresh attachment is never dropped
256
- * behind history. Failures degrade to omitted notes, never to turn
257
- * failures. Each call owns its subdir; dispose via the returned cleanup.
258
- */
259
- export async function stageImages(blocks: PromptImageBlock[]): Promise<StagedImages> {
260
- const paths = new Map<object, string>();
261
- const noop = async (): Promise<void> => undefined;
262
- const bounded = blocks.slice(-MAX_STAGED_IMAGES);
263
- if (bounded.length === 0) return { dir: undefined, paths, cleanup: noop };
264
- let root: string;
265
- try {
266
- root = await ensureImageStagingRoot();
267
- } catch {
268
- return { dir: undefined, paths, cleanup: noop };
269
- }
270
- const dir = path.join(root, randomBytes(8).toString("hex"));
271
- try {
272
- await mkdir(dir, { mode: 0o700 });
273
- } catch {
274
- return { dir: undefined, paths, cleanup: noop };
275
- }
276
- liveStagingDirs.add(dir);
277
- let cleaned = false;
278
- const cleanup = async (): Promise<void> => {
279
- if (cleaned) return;
280
- cleaned = true;
281
- liveStagingDirs.delete(dir);
282
- await rm(dir, { recursive: true, force: true }).catch(() => undefined);
283
- };
284
- for (let index = 0; index < bounded.length; index++) {
285
- const block = bounded[index];
286
- const bytes = decodeImageData(block.data);
287
- if (!bytes) continue;
288
- const file = path.join(dir, `image-${index}.${imageExtension(block.mimeType)}`);
289
- try {
290
- await writeFile(file, bytes, { mode: 0o600 });
291
- await chmod(file, 0o600);
292
- } catch {
293
- continue;
294
- }
295
- paths.set(block.block, file);
296
- }
297
- if (paths.size === 0) {
298
- await cleanup();
299
- return { dir: undefined, paths, cleanup: noop };
300
- }
301
- return { dir, paths, cleanup };
302
- }
303
-
304
- /** Remove every staging dir and the root (teardown, tests). */
305
- export async function clearImageStaging(): Promise<void> {
306
- const dirs = [...liveStagingDirs];
307
- liveStagingDirs.clear();
308
- await Promise.all(dirs.map((dir) => rm(dir, { recursive: true, force: true }).catch(() => undefined)));
309
- if (imageStagingRoot) {
310
- const root = imageStagingRoot;
311
- imageStagingRoot = undefined;
312
- await rm(root, { recursive: true, force: true }).catch(() => undefined);
313
- }
314
- }
315
-
316
- /**
317
- * Render Pi transcript messages into a single agy `-p` prompt. The latest
318
- * user message is authoritative; earlier history is bounded reference. Pi
319
- * tool definitions are intentionally NOT translated — agy runs its own tool
320
- * loop (see the module wall note).
321
- */
322
- export function renderProviderPrompt(
323
- messages: ProviderTranscriptMessage[],
324
- systemPrompt?: string,
325
- imagePaths?: Map<object, string>,
326
- ): string {
327
- const imageRef = (block: object): string | undefined => {
328
- const file = imagePaths?.get(block);
329
- return file
330
- ? `[attached image saved at ${file} — open it with your file tools to view it]`
331
- : undefined;
332
- };
333
- const lines: string[] = [];
334
- if (systemPrompt?.trim()) {
335
- lines.push("System instructions:", clip(systemPrompt.trim(), MAX_SYSTEM_CHARS), "");
336
- }
337
- const history = messages.slice(0, -1);
338
- const rendered: string[] = [];
339
- let budget = MAX_HISTORY_CHARS;
340
- for (let i = history.length - 1; i >= 0 && budget > 0; i--) {
341
- const message = history[i];
342
- const text = clip(textOfContent(message.content, imageRef).trim(), MAX_MESSAGE_CHARS);
343
- if (!text) continue;
344
- let label: string;
345
- if (message.role === "user") label = "User";
346
- else if (message.role === "assistant") label = "Assistant (previous turn)";
347
- else if (message.role === "toolResult") {
348
- label = `Tool result (${message.toolName ?? "tool"}${message.isError ? ", error" : ""})`;
349
- } else if (message.role === "system") label = "System";
350
- else label = message.role;
351
- const entry = `${label}: ${text}`;
352
- if (entry.length > budget) break;
353
- budget -= entry.length;
354
- rendered.unshift(entry);
355
- }
356
- if (rendered.length > 0) {
357
- lines.push("Conversation history (reference only; the current message is authoritative):", ...rendered, "");
358
- }
359
- const last = messages[messages.length - 1];
360
- const current = last ? textOfContent(last.content, imageRef).trim() : "";
361
- const currentLabel = last && last.role !== "user" ? `Current message (${last.role}):` : "Current message:";
362
- lines.push(currentLabel, current || "(empty — respond with a brief acknowledgement)");
363
- return lines.join("\n");
364
- }
365
-
366
- /** Pi tool source shape (structural subset of Pi's ToolInfo). */
367
- export interface ProviderToolSource {
368
- name: string;
369
- description?: string;
370
- parameters?: unknown;
371
- }
372
-
373
- /**
374
- * Tools that must never be bridged back into agy: self-delegation loops
375
- * burn quota and nest conversations, they are not capabilities agy lacks.
376
- */
377
- const BRIDGE_EXCLUDED_TOOL_NAMES = new Set(["askantigravity"]);
378
- const MAX_BRIDGED_TOOLS = 100;
379
-
380
- function isBridgableToolName(name: string): boolean {
381
- const lowered = name.toLowerCase();
382
- if (lowered === "agy" || lowered.startsWith("agy_") || lowered.startsWith("agy-")) return false;
383
- return !BRIDGE_EXCLUDED_TOOL_NAMES.has(lowered);
384
- }
385
-
386
- function jsonClone(value: unknown): Record<string, unknown> | undefined {
387
- try {
388
- const cloned: unknown = JSON.parse(JSON.stringify(value));
389
- if (cloned && typeof cloned === "object" && !Array.isArray(cloned)) {
390
- return cloned as Record<string, unknown>;
391
- }
392
- return undefined;
393
- } catch {
394
- return undefined;
395
- }
396
- }
397
-
398
- /**
399
- * Project Pi tools into bridge definitions: exclude self-delegation,
400
- * sanitize MCP names, drop unserializable schemas. First-seen wins on
401
- * collisions so output is deterministic.
402
- */
403
- export function projectBridgeTools(sources: ProviderToolSource[]): BridgeToolDef[] {
404
- const defs: BridgeToolDef[] = [];
405
- const seenNames = new Set<string>();
406
- const seenPiNames = new Set<string>();
407
- for (const source of sources) {
408
- if (defs.length >= MAX_BRIDGED_TOOLS) break;
409
- if (typeof source.name !== "string" || !isBridgableToolName(source.name)) continue;
410
- if (seenPiNames.has(source.name)) continue;
411
- const name = toSafeBridgeName(source.name);
412
- if (seenNames.has(name)) continue;
413
- // MCP requires an object root; anything else degrades to no-arg form
414
- // instead of shipping a schema agy cannot validate against.
415
- const cloned = jsonClone(source.parameters);
416
- const inputSchema =
417
- cloned && (cloned as { type?: unknown }).type === "object" && typeof (cloned as { properties?: unknown }).properties === "object"
418
- ? (cloned as Record<string, unknown>)
419
- : { type: "object", properties: {} };
420
- seenPiNames.add(source.name);
421
- seenNames.add(name);
422
- defs.push({
423
- name,
424
- piName: source.name,
425
- description:
426
- typeof source.description === "string" && source.description.trim()
427
- ? source.description.trim().slice(0, 500)
428
- : `Pi tool ${source.name}`,
429
- inputSchema,
430
- });
431
- }
432
- return defs;
433
- }
434
-
435
- /** Fingerprint the bridged catalog: changes recycle the driver (fresh ListTools). */
436
- export function bridgeToolsFingerprint(tools: BridgeToolDef[]): string {
437
- const canonical = JSON.stringify(
438
- tools.map((tool) => [tool.name, tool.piName, tool.description, tool.inputSchema]),
439
- );
440
- let hash = 5381;
441
- for (let i = 0; i < canonical.length; i++) {
442
- hash = ((hash << 5) + hash + canonical.charCodeAt(i)) | 0;
443
- }
444
- return `n${tools.length}-h${(hash >>> 0).toString(16)}`;
445
- }
446
-
447
135
  /**
448
136
  * Default runner: driver turns serialized on the per-directory lock so a
449
137
  * provider turn can never race a delegation run (or another provider turn)
@@ -678,7 +366,7 @@ export function getProviderStatus(dir: string): ProviderStatus {
678
366
  : undefined,
679
367
  bridge: bridge ? { toolCount: bridge.describe().toolCount } : undefined,
680
368
  suspended,
681
- stagedImages: liveStagingDirs.size,
369
+ stagedImages: getLiveImageStagingCount(),
682
370
  };
683
371
  } catch {
684
372
  return { dir: key, suspended, stagedImages: 0 };
@@ -15,6 +15,19 @@ export interface VerifyDetectionDeps {
15
15
  justAvailable?: () => Promise<boolean>;
16
16
  }
17
17
 
18
+ // Detection walks up to the repo boundary reading several marker files per
19
+ // level on every accept-edits run. Cache per starting directory with a short
20
+ // TTL: project config rarely changes mid-session, and a stale hit only
21
+ // reuses an equivalent verify loop for under a minute. Bypassed when the
22
+ // test hook overrides the `just` probe so tests never poison each other.
23
+ const VERIFY_CACHE_TTL_MS = 60_000;
24
+ const verifyCache = new Map<string, { at: number; command: string | null }>();
25
+
26
+ /** Test helper — drop cached verify-command detections. */
27
+ export function resetVerifyDetectionCache(): void {
28
+ verifyCache.clear();
29
+ }
30
+
18
31
  /**
19
32
  * Detect a local verification command for accept-edits verify-loop injection.
20
33
  * The upward walk stops at the repository boundary (a directory containing
@@ -25,7 +38,23 @@ export async function detectVerifyCommand(
25
38
  cwd: string,
26
39
  deps: VerifyDetectionDeps = {},
27
40
  ): Promise<string | null> {
28
- let current = path.resolve(cwd);
41
+ const key = path.resolve(cwd);
42
+ if (deps.justAvailable === undefined) {
43
+ const hit = verifyCache.get(key);
44
+ if (hit && Date.now() - hit.at < VERIFY_CACHE_TTL_MS) return hit.command;
45
+ }
46
+ const command = await detectVerifyCommandUncached(key, deps);
47
+ if (deps.justAvailable === undefined) {
48
+ verifyCache.set(key, { at: Date.now(), command });
49
+ }
50
+ return command;
51
+ }
52
+
53
+ async function detectVerifyCommandUncached(
54
+ startDir: string,
55
+ deps: VerifyDetectionDeps,
56
+ ): Promise<string | null> {
57
+ let current = startDir;
29
58
  while (true) {
30
59
  const command = await detectVerifyCommandAt(current, deps);
31
60
  if (command) return command;