can1357/oh-my-pi · error · ToolError

SQLite order direction must be 'asc' or 'desc'; got '${direc

Error message

SQLite order direction must be 'asc' or 'desc'; got '${direction}'

What it means

Thrown by resolveOrderClause when the direction suffix of the 'order' parameter is not exactly 'asc' or 'desc' (after trimming and lowercasing). The tool whitelists directions so only validated, safe ORDER BY clauses are built.

Source

Thrown at packages/coding-agent/src/tools/sqlite-reader.ts:390

function resolveOrderClause(order: string | undefined, columns: string[]): string {
	if (!order) return "";
	const trimmed = order.trim();
	if (!trimmed) return "";

	const separatorIndex = trimmed.lastIndexOf(":");
	const column = separatorIndex === -1 ? trimmed : trimmed.slice(0, separatorIndex);
	const direction =
		separatorIndex === -1
			? "asc"
			: trimmed
					.slice(separatorIndex + 1)
					.trim()
					.toLowerCase();
	if (!columns.includes(column)) {
		throw new ToolError(`SQLite order column '${column}' not found in table schema`);
	}
	if (direction !== "asc" && direction !== "desc") {
		throw new ToolError(`SQLite order direction must be 'asc' or 'desc'; got '${direction}'`);
	}
	return ` ORDER BY ${quoteSqliteIdentifier(column)} ${direction.toUpperCase()}`;
}

const FORBIDDEN_WHERE_KEYWORDS = new Set([
	"limit",
	"offset",
	"union",
	"intersect",
	"except",
	"attach",
	"detach",
	"pragma",
]);

const COMMENT_OR_TERMINATOR_ERROR =
	"SQLite 'where' clause must not contain comments or statement terminators; use '?q=SELECT ...' for raw SQL";
const FORBIDDEN_KEYWORD_ERROR =

View on GitHub (pinned to 9690622007)

Solutions

  1. Use 'asc' or 'desc' as the direction suffix
  2. Omit the direction entirely (defaults to 'asc')
  3. Check the separator format: order is 'column[:direction]'
  4. Strip whitespace — trailing text after asc/desc still fails

Example fix

// before
?order=created_at:ascending
// after
?order=created_at:desc
Defensive patterns

Strategy: validation

Validate before calling

const dir = (orderDirection ?? 'asc').trim().toLowerCase();
if (dir !== 'asc' && dir !== 'desc') {
  throw new Error(`direction must be asc|desc, got '${dir}'`);
}

Type guard

function isSortDirection(v: string): v is 'asc' | 'desc' {
  return v === 'asc' || v === 'desc';
}

Try / catch

try {
  await reader.read(`db.sqlite/users?order=name:${dir}`);
} catch (err) {
  if (err instanceof ToolError && err.message.includes("must be 'asc' or 'desc'")) {
    // retry with default direction
  } else throw err;
}

Prevention

When it happens

Trigger: Passing order like 'name:ASCENDING', 'name:1', 'name:up', or 'name:' with a junk/empty direction suffix after the ':' separator.

Common situations: Copy-pasting SQL-style direction keywords ('ASCENDING', 'ascending', 'ascending order'); using numeric sort direction codes; locale issues where a direction word got translated.

Understand the failure class

Background: "must be positive", "Invalid value": how libraries reject invalid parameter values (ValueError, ArgumentError, INVALID_PARAMETER_VALUE) — this error's family across 28 libraries.

Related errors


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