can1357/oh-my-pi · error

Invalid query: missing ] in ${query}

Error message

Invalid query: missing ] in ${query}

What it means

parseQuery tokenizes a jq-like query string (.foo.bar[0]) used to extract values from JSON payloads referenced by agent:// URLs. When it sees an opening '[' it searches for the matching ']' and throws if none exists anywhere later in the string. This guards the tokenizer against truncated or malformed bracket segments.

Source

Thrown at packages/coding-agent/src/internal-urls/json-query.ts:35

	if (!input) return [];
	if (input.startsWith(".")) input = input.slice(1);
	if (!input) return [];

	const tokens: Array<string | number> = [];
	let i = 0;

	const isIdentChar = (ch: string) => /[A-Za-z0-9_-]/.test(ch);

	while (i < input.length) {
		const ch = input[i];
		if (ch === ".") {
			i++;
			continue;
		}
		if (ch === "[") {
			const closeIndex = input.indexOf("]", i + 1);
			if (closeIndex === -1) {
				throw new Error(`Invalid query: missing ] in ${query}`);
			}
			const raw = input.slice(i + 1, closeIndex).trim();
			if (!raw) {
				throw new Error(`Invalid query: empty [] in ${query}`);
			}
			const quote = raw[0];
			if ((quote === '"' || quote === "'") && raw.endsWith(quote)) {
				let inner = raw.slice(1, -1);
				inner = inner.replace(/\\(["'\\])/g, "$1");
				tokens.push(inner);
			} else if (/^\d+$/.test(raw)) {
				tokens.push(Number(raw));
			} else {
				tokens.push(raw);
			}
			i = closeIndex + 1;
			continue;
		}

View on GitHub (pinned to 9690622007)

Solutions

  1. Add the missing closing ']' to the bracket segment in the query string.
  2. If the key itself contains ']' (e.g. ["a]b"]), note this parser does not support it — restructure the data or use pathToQuery-safe keys.
  3. Validate query strings with a regex like /^\.?[A-Za-z0-9_-]+(\[[^\]]*\]|\.[A-Za-z0-9_-]+)*$/ before passing them to applyQuery.

Example fix

// before
applyQuery(data, ".foo[0");
// after
applyQuery(data, ".foo[0]");
Defensive patterns

Strategy: validation

Validate before calling

function hasBalancedBrackets(q) { let d = 0; for (const c of q) { if (c === '[') d++; else if (c === ']') d--; if (d < 0) return false; } return d === 0; }
if (!hasBalancedBrackets(query)) throw new Error(`Unclosed [ in query: ${query}`);

Try / catch

try { tokens = parseQuery(query); } catch (e) { if (String(e.message).includes('Invalid query')) { /* surface query to user / fall back to raw path */ } else throw e; }

Prevention

When it happens

Trigger: Calling parseQuery or applyQuery with a string like '.foo[0' or '.items["key' where a '[' is never closed. Because the parser scans forward for the first ']', any '[' without a subsequent ']' triggers it, even if a later unrelated ']' would look balanced.

Common situations: Hand-written query strings in tool configs or model-emitted JSON extraction paths; truncated queries from URL parsing where brackets were stripped or percent-decoding mangled them; typos like '.foo[0].' missing the closing bracket.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/c7acfb61ab066423. Report an issue: GitHub.