pi-agy-acp 0.2.2 → 0.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/src/preflight.ts CHANGED
@@ -8,42 +8,33 @@
8
8
  * @module pi-agy-acp/preflight
9
9
  */
10
10
 
11
- import { randomBytes } from 'node:crypto'
12
11
  import {
13
12
  accessSync,
14
- chmodSync,
15
13
  closeSync,
16
14
  constants as fsConstants,
17
15
  fstatSync,
18
16
  mkdirSync,
19
17
  openSync,
20
- writeFileSync,
21
18
  readFileSync,
22
19
  realpathSync,
23
- renameSync,
24
20
  statSync,
25
21
  lstatSync,
26
22
  readdirSync,
27
- unlinkSync,
28
23
  type BigIntStats,
29
24
  type Stats,
30
25
  } from 'node:fs'
31
26
  import { homedir } from 'node:os'
32
- import {
33
- basename,
34
- delimiter,
35
- dirname,
36
- isAbsolute,
37
- join,
38
- resolve,
39
- } from 'node:path'
27
+ import { delimiter, isAbsolute, join, resolve } from 'node:path'
40
28
  import { readJson } from './config.ts'
41
29
  import { removeArtifacts } from './session-db.ts'
30
+ import { atomicWriteFile } from './util.ts'
31
+
32
+ export { atomicWriteFile }
42
33
 
43
34
  /** Agent id installed in the isolated home. */
44
35
  export const BRIDGE_AGENT = 'pi-bridge'
45
- /** Minimum official CLI version supported (`agy 1.5.10+`). */
46
- export const MIN_AGY_VERSION: readonly [number, number, number] = [1, 5, 10]
36
+ /** Minimum official CLI version supported (`agy 1.5.18+`). */
37
+ export const MIN_AGY_VERSION: readonly [number, number, number] = [1, 5, 18]
47
38
 
48
39
  /** Output of one finished helper command. */
49
40
  export interface CommandOutput {
@@ -183,7 +174,7 @@ export function resolveAgyBinary(
183
174
  )
184
175
  if (found === undefined) {
185
176
  throw new Error(
186
- 'antigravity-acp: the Antigravity CLI (agy) was not found; install it from https://antigravity.google/docs/install?tab=cli and sign in once with `agy`',
177
+ 'antigravity-acp: the Antigravity CLI (agy) was not found; install it from https://antigravity.google/docs/install?tab=cli and sign in once with `agy` (or `agy acp --login` for headless/SSH setups)',
187
178
  )
188
179
  }
189
180
  return found
@@ -324,73 +315,6 @@ function ensureRealDirectory(
324
315
  return current
325
316
  }
326
317
 
327
- /**
328
- * Atomically write a regular file in the target directory while rejecting
329
- * symlinked or non-regular destination paths.
330
- */
331
- export function atomicWriteFile(
332
- path: string,
333
- content: string | Uint8Array,
334
- options?: { mode?: number },
335
- ): void {
336
- let existing: Stats | undefined
337
- try {
338
- existing = lstatSync(path)
339
- } catch (error) {
340
- if ((error as NodeJS.ErrnoException)?.code !== 'ENOENT') throw error
341
- }
342
- if (
343
- existing !== undefined &&
344
- (existing.isSymbolicLink() || !existing.isFile())
345
- ) {
346
- throw new Error(
347
- `Refusing to write to symlink or non-regular file: ${path}`,
348
- )
349
- }
350
- const tmpPath = join(
351
- dirname(path),
352
- `.${basename(path)}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`,
353
- )
354
- const mode = options?.mode
355
- let fd: number | undefined
356
- let created = false
357
- try {
358
- fd = openSync(
359
- tmpPath,
360
- fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL,
361
- mode ?? 0o666,
362
- )
363
- created = true
364
- writeFileSync(fd, content)
365
- closeSync(fd)
366
- fd = undefined
367
- if (mode !== undefined) {
368
- try {
369
- chmodSync(tmpPath, mode)
370
- } catch {
371
- // Mode bits are advisory on Windows.
372
- }
373
- }
374
- renameSync(tmpPath, path)
375
- } catch (error) {
376
- if (fd !== undefined) {
377
- try {
378
- closeSync(fd)
379
- } catch {
380
- // Ignore close errors during cleanup.
381
- }
382
- }
383
- if (created) {
384
- try {
385
- unlinkSync(tmpPath)
386
- } catch {
387
- // Best-effort temp file cleanup.
388
- }
389
- }
390
- throw error
391
- }
392
- }
393
-
394
318
  /**
395
319
  * Seed or refresh the isolated `antigravity-oauth-token` file from the user's
396
320
  * Antigravity CLI home (`ANTIGRAVITY_CLI_HOME`, `AGY_HOME`, or
@@ -499,10 +423,19 @@ export function syncOAuthToken(
499
423
  }
500
424
  }
501
425
 
426
+ /** Optional controls for isolated home preparation. */
427
+ export interface PrepareHomeOptions {
428
+ /** Remove stale ACP conversation/brain/annotation artifacts and rotated logs. Defaults to `true`. */
429
+ cleanArtifacts?: boolean
430
+ }
431
+
502
432
  /**
503
433
  * Write the isolated Antigravity home: bridge agent and ACP settings.
504
434
  * @param home - absolute home directory used as `--gemini-dir`.
505
435
  * @param userHome - optional user home override for testing.
436
+ * @param env - optional environment override for testing.
437
+ * @param onWarn - optional warning callback for non-fatal OAuth sync issues.
438
+ * @param options - optional preparation controls.
506
439
  * @returns the empty workspace directory used as every session's cwd.
507
440
  */
508
441
  export function prepareHome(
@@ -510,6 +443,7 @@ export function prepareHome(
510
443
  userHome: string = homedir(),
511
444
  env: NodeJS.ProcessEnv = process.env,
512
445
  onWarn?: (message: string) => void,
446
+ options: PrepareHomeOptions = {},
513
447
  ): string {
514
448
  const pluginDir = ensureRealDirectory(home, [
515
449
  'config',
@@ -536,25 +470,27 @@ export function prepareHome(
536
470
  ].join('\n'),
537
471
  )
538
472
  const appData = ensureRealDirectory(home, ['agy'])
539
- // Pi owns conversation history; no ACP session is restored across processes.
540
- // shortcut: cleanup requires an exclusive home; add locking before supporting shared homes.
541
- removeArtifacts(appData, [
542
- 'conversations',
543
- 'brain',
544
- 'annotations',
545
- 'conversation_summaries.db',
546
- 'conversation_summaries.db-wal',
547
- 'conversation_summaries.db-shm',
548
- 'conversation_summaries.db-journal',
549
- 'jetbox_summaries_proto.pb',
550
- ])
551
- try {
552
- removeArtifacts(
553
- home,
554
- readdirSync(home).filter((name) => /^acp\.log\.\d+$/.test(name)),
555
- )
556
- } catch {
557
- // Log cleanup is best-effort when the home cannot be listed.
473
+ if (options.cleanArtifacts !== false) {
474
+ // Pi owns conversation history; cold startup cleans stale ACP artifacts from prior processes.
475
+ // shortcut: cleanup requires an exclusive home; add locking before supporting shared homes.
476
+ removeArtifacts(appData, [
477
+ 'conversations',
478
+ 'brain',
479
+ 'annotations',
480
+ 'conversation_summaries.db',
481
+ 'conversation_summaries.db-wal',
482
+ 'conversation_summaries.db-shm',
483
+ 'conversation_summaries.db-journal',
484
+ 'jetbox_summaries_proto.pb',
485
+ ])
486
+ try {
487
+ removeArtifacts(
488
+ home,
489
+ readdirSync(home).filter((name) => /^acp\.log\.\d+$/.test(name)),
490
+ )
491
+ } catch {
492
+ // Log cleanup is best-effort when the home cannot be listed.
493
+ }
558
494
  }
559
495
  try {
560
496
  syncOAuthToken(home, userHome, env)
package/src/quota.ts CHANGED
@@ -221,26 +221,33 @@ function parseBucket(raw: unknown): QuotaBucket | undefined {
221
221
  * @returns the quota groups.
222
222
  */
223
223
  export function parseUsage(stdout: string): QuotaGroup[] {
224
- const parsed = JSON.parse(stdout) as {
225
- command?: { data?: { groups?: unknown[] } }
226
- }
227
- const groups = parsed.command?.data?.groups
224
+ const parsed: unknown = JSON.parse(stdout)
225
+ const command =
226
+ parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)
227
+ ? (parsed as { command?: unknown }).command
228
+ : undefined
229
+ const data =
230
+ command !== null && typeof command === 'object' && !Array.isArray(command)
231
+ ? (command as { data?: unknown }).data
232
+ : undefined
233
+ const groups =
234
+ data !== null && typeof data === 'object' && !Array.isArray(data)
235
+ ? (data as { groups?: unknown }).groups
236
+ : undefined
228
237
  if (!Array.isArray(groups))
229
238
  throw new Error('agy /usage returned no quota groups')
230
- return groups.map((raw) => {
231
- const group = raw as {
232
- name?: string
233
- description?: string
234
- buckets?: unknown[]
235
- }
236
- return {
239
+ return groups
240
+ .filter(
241
+ (raw): raw is Record<string, unknown> =>
242
+ raw !== null && typeof raw === 'object' && !Array.isArray(raw),
243
+ )
244
+ .map((group) => ({
237
245
  name: String(group.name ?? ''),
238
246
  description: String(group.description ?? ''),
239
247
  buckets: (Array.isArray(group.buckets) ? group.buckets : [])
240
248
  .map(parseBucket)
241
249
  .filter((bucket): bucket is QuotaBucket => bucket !== undefined),
242
- }
243
- })
250
+ }))
244
251
  }
245
252
 
246
253
  /** Cached quota reader with request coalescing. */
@@ -280,10 +287,6 @@ export class QuotaReader {
280
287
  : undefined
281
288
  }
282
289
 
283
- get pending(): boolean {
284
- return this.inflight !== undefined
285
- }
286
-
287
290
  /**
288
291
  * Read the quota snapshot.
289
292
  * @param force - bypass the cache.
@@ -318,7 +321,7 @@ export class QuotaReader {
318
321
  AUTH_REQUIRED_STDERR.test(result.stderr)
319
322
  ) {
320
323
  throw new Error(
321
- 'Antigravity requires sign-in: run `agy` once in a terminal (Authentication required)',
324
+ 'Antigravity requires sign-in: run `agy` once in a terminal or `agy acp --login` for headless/SSH setups (Authentication required)',
322
325
  )
323
326
  }
324
327
  if (result.abortedBy === 'maxStdout') {