omp-antigravity-acp-provider 0.2.4 → 0.3.2

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.
@@ -103,6 +103,182 @@ function plainEntries(value: object, budget: ProjectionBudget): Array<[string, u
103
103
  return entries;
104
104
  }
105
105
 
106
+ // agy compiles every projected `pattern` with Go's RE2 engine, while JSON
107
+ // Schema patterns are ECMA-262: Unicode escapes and lookarounds make agy fail
108
+ // "building toolbox" and abort the whole turn before any model call. Translate
109
+ // the escapes RE2 can express, keep a pattern only when nothing RE2 cannot
110
+ // express (or would reinterpret) remains, and otherwise drop the field while
111
+ // preserving the original as a description hint for the model.
112
+ const PATTERN_HINT_PREFIX = "pattern: ";
113
+ const HIGH_SURROGATE_START = 0xd800;
114
+ const HIGH_SURROGATE_END = 0xdbff;
115
+ const LOW_SURROGATE_START = 0xdc00;
116
+ const LOW_SURROGATE_END = 0xdfff;
117
+
118
+ function isHighSurrogate(cp: number): boolean {
119
+ return cp >= HIGH_SURROGATE_START && cp <= HIGH_SURROGATE_END;
120
+ }
121
+
122
+ function isLowSurrogate(cp: number): boolean {
123
+ return cp >= LOW_SURROGATE_START && cp <= LOW_SURROGATE_END;
124
+ }
125
+
126
+ function joinSurrogates(high: number, low: number): number {
127
+ return 0x10000 + ((high - HIGH_SURROGATE_START) << 10) + (low - LOW_SURROGATE_START);
128
+ }
129
+
130
+ // Recognizes `\uXXXX` and `\u{...}` starting at `index`.
131
+ function parseUnicodeEscape(source: string, index: number): { end: number; cp: number } | undefined {
132
+ if (source[index] !== "\\") return undefined;
133
+ const rest = source.slice(index + 1);
134
+ let match = /^u\{([0-9a-fA-F]+)\}/.exec(rest);
135
+ if (match) return { end: index + 1 + match[0].length, cp: Number.parseInt(match[1]!, 16) };
136
+ match = /^u([0-9a-fA-F]{4})/.exec(rest);
137
+ if (match) return { end: index + 1 + match[0].length, cp: Number.parseInt(match[1]!, 16) };
138
+ return undefined;
139
+ }
140
+
141
+ function emitCodePoint(cp: number): string {
142
+ const digits = cp.toString(16).toUpperCase();
143
+ return `\\x{${cp <= 0xffff ? digits.padStart(4, "0") : digits}}`;
144
+ }
145
+
146
+ // Escape-aware translation of ECMA-262 Unicode escapes into RE2 `\x{...}`
147
+ // escapes, joining adjacent UTF-16 surrogate pairs (escaped or raw) into one
148
+ // code point so an astral pattern character stays a single RE2 atom.
149
+ // `\\u00e9` (a literal backslash before the letters u00e9) is left alone, and
150
+ // the `\x{...}` output is never re-translated, so the rewrite is idempotent.
151
+ function translateUnicodeEscapes(source: string): string {
152
+ let out = "";
153
+ let index = 0;
154
+ while (index < source.length) {
155
+ const parsed = parseUnicodeEscape(source, index);
156
+ if (parsed) {
157
+ const next = parseUnicodeEscape(source, parsed.end);
158
+ if (isHighSurrogate(parsed.cp) && next && isLowSurrogate(next.cp)) {
159
+ out += emitCodePoint(joinSurrogates(parsed.cp, next.cp));
160
+ index = next.end;
161
+ continue;
162
+ }
163
+ // An escaped high surrogate followed by a raw low surrogate.
164
+ if (isHighSurrogate(parsed.cp) && isLowSurrogate(source.charCodeAt(parsed.end))) {
165
+ out += emitCodePoint(joinSurrogates(parsed.cp, source.charCodeAt(parsed.end)));
166
+ index = parsed.end + 1;
167
+ continue;
168
+ }
169
+ out += emitCodePoint(parsed.cp);
170
+ index = parsed.end;
171
+ continue;
172
+ }
173
+ if (source[index] === "\\") {
174
+ out += source[index++]!;
175
+ if (index < source.length) out += source[index++]!;
176
+ continue;
177
+ }
178
+ const code = source.charCodeAt(index);
179
+ if (isHighSurrogate(code)) {
180
+ const next = parseUnicodeEscape(source, index + 1);
181
+ if (next && isLowSurrogate(next.cp)) {
182
+ out += emitCodePoint(joinSurrogates(code, next.cp));
183
+ index = next.end;
184
+ continue;
185
+ }
186
+ if (isLowSurrogate(source.charCodeAt(index + 1))) {
187
+ out += emitCodePoint(joinSurrogates(code, source.charCodeAt(index + 1)));
188
+ index += 2;
189
+ continue;
190
+ }
191
+ }
192
+ out += source[index++]!;
193
+ }
194
+ return out;
195
+ }
196
+
197
+ // Only these escape letters keep their JavaScript meaning in RE2. Everything
198
+ // else after a backslash is rejected: `\u`/`\U`/`\c` are invalid there,
199
+ // backreferences `\1`-`\9` and `\k<name>` do not exist, and letters like `\a`
200
+ // (BEL), `\A`, `\z`, `\K`, `\Q…\E`, `\e` change or lose their meaning. A
201
+ // digit is never safe (RE2 reads octal, JavaScript reads a backreference), and
202
+ // a lone trailing backslash is invalid in both engines.
203
+ const RE2_SAFE_ESCAPE_LETTERS = new Set(["f", "n", "r", "t", "v", "d", "D", "s", "S", "w", "W", "b", "B", "x", "0"]);
204
+
205
+ // Lookaheads `(?=` / `(?!` and lookbehinds `(?<=` / `(?<!` have no RE2 form.
206
+ // `(?<name>` groups are fine and stay. Go also rejects `\b`/`\B` inside a
207
+ // character class and an `\x` not followed by two hex digits or `{hex}`.
208
+ // Property escapes (`\p{Emoji}`, `\P{L}`) depend on a Go property table this
209
+ // projection cannot verify, so they are dropped instead of aborting agy's
210
+ // toolbox build.
211
+ function isRE2Safe(source: string): boolean {
212
+ let inClass = false;
213
+ let classStart = -1;
214
+ for (let index = 0; index < source.length; index++) {
215
+ const char = source[index];
216
+ if (char === "\\") {
217
+ const next = source[++index];
218
+ if (next === undefined) return false;
219
+ if (inClass && (next === "b" || next === "B")) return false;
220
+ if (next === "p" || next === "P") return false;
221
+ if (next === "x") {
222
+ const braced = /^\{([0-9a-fA-F]+)\}/.exec(source.slice(index + 1));
223
+ if (braced) {
224
+ if (Number.parseInt(braced[1]!, 16) > 0x10ffff) return false;
225
+ index += braced[0].length;
226
+ continue;
227
+ }
228
+ if (!/^[0-9a-fA-F]{2}/.test(source.slice(index + 1))) return false;
229
+ index += 2;
230
+ continue;
231
+ }
232
+ if (/[a-zA-Z0-9]/u.test(next) && !RE2_SAFE_ESCAPE_LETTERS.has(next)) return false;
233
+ continue;
234
+ }
235
+ if (inClass) {
236
+ // `[:alpha:]` is a POSIX name, not a nested class: its `]` must not
237
+ // end the enclosing class before the real closer.
238
+ if (char === "[" && source[index + 1] === ":") {
239
+ const end = source.indexOf(":]", index + 2);
240
+ if (end >= 0) {
241
+ index = end + 1;
242
+ continue;
243
+ }
244
+ }
245
+ if (char === "]" && index > classStart) inClass = false;
246
+ continue;
247
+ }
248
+ if (char === "[") {
249
+ inClass = true;
250
+ classStart = source[index + 1] === "^" ? index + 2 : index + 1;
251
+ continue;
252
+ }
253
+ if (char === "(" && source[index + 1] === "?") {
254
+ const ahead = source[index + 2];
255
+ if (ahead === "=" || ahead === "!") return false;
256
+ if (ahead === "<" && (source[index + 3] === "=" || source[index + 3] === "!")) return false;
257
+ }
258
+ }
259
+ if (inClass) return false;
260
+ return true;
261
+ }
262
+
263
+ // The RE2-safe rewrite of a pattern, or undefined when it must be dropped.
264
+ function toRE2Pattern(original: string): string | undefined {
265
+ const translated = translateUnicodeEscapes(original);
266
+ return isRE2Safe(translated) ? translated : undefined;
267
+ }
268
+
269
+ // Bridge-style hint: appended in parentheses after an existing description,
270
+ // standing alone when there was none. Re-projecting a hinted node is a no-op.
271
+ function appendPatternHint(node: Record<string, unknown>, hint: string, budget: ProjectionBudget): void {
272
+ const description = node.description;
273
+ if (typeof description === "string" && description.trim() && !description.includes(hint)) {
274
+ node.description = `${description} (${hint})`;
275
+ spend(budget, 0, hint.length + 3);
276
+ } else if (typeof description !== "string" || !description.trim()) {
277
+ node.description = hint;
278
+ spend(budget, 0, hint.length + 2);
279
+ }
280
+ }
281
+
106
282
  function sanitizeSchema(value: unknown, depth: number, budget: ProjectionBudget): Projection {
107
283
  if (depth > MAX_SCHEMA_DEPTH) return { reason: "too_deep" };
108
284
  if (!value || typeof value !== "object") return { reason: "invalid_keyword_value" };
@@ -123,6 +299,7 @@ function sanitizeNode(value: object, depth: number, budget: ProjectionBudget): P
123
299
  if (!entries) return { reason: "unsupported_keyword" };
124
300
  const output: Record<string, unknown> = {};
125
301
  let typeUnion: string[] | undefined;
302
+ let patternHint: string | undefined;
126
303
  for (const [key, candidate] of entries) {
127
304
  if (key.startsWith("~")) {
128
305
  if (!HIDDEN_KEYS.has(key)) return { reason: "unsupported_keyword" };
@@ -142,6 +319,17 @@ function sanitizeNode(value: object, depth: number, budget: ProjectionBudget): P
142
319
  }
143
320
  continue;
144
321
  }
322
+ if (key === "pattern") {
323
+ if (typeof candidate !== "string") return { reason: "invalid_keyword_value" };
324
+ const rewritten = toRE2Pattern(candidate);
325
+ if (rewritten === undefined) {
326
+ patternHint = `${PATTERN_HINT_PREFIX}${candidate}`;
327
+ } else {
328
+ spend(budget, 0, key.length + 3 + scalarChars(rewritten));
329
+ output.pattern = rewritten;
330
+ }
331
+ continue;
332
+ }
145
333
  if (key === "properties") {
146
334
  if (types.isProxy(candidate)) return { reason: "unsupported_keyword" };
147
335
  if (!candidate || typeof candidate !== "object" || Array.isArray(candidate)) {
@@ -245,6 +433,7 @@ function sanitizeNode(value: object, depth: number, budget: ProjectionBudget): P
245
433
  spend(budget, 0, 9 + scalarChars(output.const as null | string | number | boolean));
246
434
  output.enum = [output.const];
247
435
  }
436
+ if (patternHint !== undefined) appendPatternHint(output, patternHint, budget);
248
437
  // Never infer a type: beside const/enum/combinators that could contradict
249
438
  // a valid typeless schema. Empty nested schemas legitimately allow anything.
250
439
  return { schema: output };