pi-agy-acp 0.2.3 → 0.2.5

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/session-db.ts CHANGED
@@ -43,10 +43,17 @@ export type HiddenTurnErrorKind =
43
43
  | 'rate_limit'
44
44
  | 'transient_not_found'
45
45
  | 'model_not_found'
46
+ | 'invalid_request'
46
47
 
47
48
  export interface HiddenTurnError {
48
49
  kind: HiddenTurnErrorKind
49
- code: 'QUOTA' | 'PROVIDER_ERROR' | 'RATE_LIMIT' | 'SERVER' | 'UNKNOWN_MODEL'
50
+ code:
51
+ | 'QUOTA'
52
+ | 'PROVIDER_ERROR'
53
+ | 'RATE_LIMIT'
54
+ | 'SERVER'
55
+ | 'UNKNOWN_MODEL'
56
+ | 'INVALID_REQUEST'
50
57
  message: string
51
58
  dirtySession?: boolean
52
59
  resetTimestamp?: string
@@ -396,6 +403,8 @@ const RATE_LIMIT_RE =
396
403
  /(?:HTTP\s+429|\(code\s+429\)|"code"\s*:\s*429|\bRESOURCE_EXHAUSTED\b)/
397
404
  const NOT_FOUND_RE =
398
405
  /(?:HTTP\s+404|\(code\s+404\)|"code"\s*:\s*404|\bNOT_FOUND\b)/
406
+ const INVALID_ARGUMENT_RE =
407
+ /(?:HTTP\s+400|\(code\s+400\)|"code"\s*:\s*400|\bINVALID_ARGUMENT\b|Unable to process input image)/i
399
408
 
400
409
  /**
401
410
  * Classify a terminal `steps` error payload (`step_type = 17`) into a structured
@@ -475,6 +484,17 @@ export function classifyStepErrorPayload(
475
484
  }
476
485
  }
477
486
 
487
+ if (INVALID_ARGUMENT_RE.test(payload)) {
488
+ const detail = /Unable to process input image/i.test(payload)
489
+ ? ': Unable to process input image'
490
+ : ''
491
+ return {
492
+ kind: 'invalid_request',
493
+ code: 'INVALID_REQUEST',
494
+ message: `Antigravity rejected the request (400 INVALID_ARGUMENT)${detail}`,
495
+ }
496
+ }
497
+
478
498
  return undefined
479
499
  }
480
500
 
package/src/util.ts CHANGED
@@ -4,7 +4,19 @@
4
4
  * @module pi-agy-acp/util
5
5
  */
6
6
 
7
- import { createHash } from 'node:crypto'
7
+ import { createHash, randomBytes } from 'node:crypto'
8
+ import {
9
+ chmodSync,
10
+ closeSync,
11
+ constants as fsConstants,
12
+ lstatSync,
13
+ openSync,
14
+ renameSync,
15
+ unlinkSync,
16
+ writeFileSync,
17
+ type Stats,
18
+ } from 'node:fs'
19
+ import { basename, dirname, join } from 'node:path'
8
20
 
9
21
  /** A promise with externally exposed settlement. */
10
22
  export interface Deferred<T> {
@@ -155,12 +167,70 @@ export function digest(value: unknown): string {
155
167
  }
156
168
 
157
169
  /**
158
- * Sleep for a duration.
159
- * @param ms - delay.
160
- * @returns a promise resolved after the delay.
170
+ * Atomically write a regular file in the target directory while rejecting
171
+ * symlinked or non-regular destination paths.
161
172
  */
162
- export function delay(ms: number): Promise<void> {
163
- return new Promise((resolve) => setTimeout(resolve, ms))
173
+ export function atomicWriteFile(
174
+ path: string,
175
+ content: string | Uint8Array,
176
+ options?: { mode?: number },
177
+ ): void {
178
+ let existing: Stats | undefined
179
+ try {
180
+ existing = lstatSync(path)
181
+ } catch (error) {
182
+ if ((error as NodeJS.ErrnoException)?.code !== 'ENOENT') throw error
183
+ }
184
+ if (
185
+ existing !== undefined &&
186
+ (existing.isSymbolicLink() || !existing.isFile())
187
+ ) {
188
+ throw new Error(
189
+ `Refusing to write to symlink or non-regular file: ${path}`,
190
+ )
191
+ }
192
+ const tmpPath = join(
193
+ dirname(path),
194
+ `.${basename(path)}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`,
195
+ )
196
+ const mode = options?.mode
197
+ let fd: number | undefined
198
+ let created = false
199
+ try {
200
+ fd = openSync(
201
+ tmpPath,
202
+ fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL,
203
+ mode ?? 0o666,
204
+ )
205
+ created = true
206
+ writeFileSync(fd, content)
207
+ closeSync(fd)
208
+ fd = undefined
209
+ if (mode !== undefined) {
210
+ try {
211
+ chmodSync(tmpPath, mode)
212
+ } catch {
213
+ // Mode bits are advisory on Windows.
214
+ }
215
+ }
216
+ renameSync(tmpPath, path)
217
+ } catch (error) {
218
+ if (fd !== undefined) {
219
+ try {
220
+ closeSync(fd)
221
+ } catch {
222
+ // Ignore close errors during cleanup.
223
+ }
224
+ }
225
+ if (created) {
226
+ try {
227
+ unlinkSync(tmpPath)
228
+ } catch {
229
+ // Best-effort temp file cleanup.
230
+ }
231
+ }
232
+ throw error
233
+ }
164
234
  }
165
235
 
166
236
  /** Cancel one caller's wait without cancelling work shared with other callers. */