Bound Hot-Loop Work

Default to explicit resource budgets and ensure they’re actually enforced. Apply these rules in code review: 1) **Bound inputs and remote pagination**

copy reviewer prompt

Prompt

Reviewer Prompt

Default to explicit resource budgets and ensure they’re actually enforced.

Apply these rules in code review: 1) Bound inputs and remote pagination

  • Any pagination/feed scan must specify server-side bounds (e.g., perPage/maxPages) and must not re-walk a forever-growing queue.
  • Any regex/string processing must operate on bounded inputs; do not rely on timers if the work blocks the event loop.

2) Eliminate repeated expensive work in hot loops

  • Hoist/compute once per request/session (e.g., pre-resolve paths; memoize stable directory real paths).
  • Remove redundant structuredClone/materialization and dead-code flushes.
  • Update state in-loop but call expensive persistence once.

3) Make enforcement observable/tested

  • Add tests that exercise the pathological/performance shape (not just “large but uniform” inputs).
  • Add invariants or unit tests for cursor/list trimming and budget-consistency.

Example (bounded pagination + draining):

const todos = await this.api.TodoLists.all({
  state: 'pending',
  perPage: 100,
  maxPages: 5,
});

for (const todo of todos) {
  if (unsupported(todo)) {
    // must drain/skipped items to avoid re-fetch forever
    await this.api.TodoLists.done({ todoId: todo.id });
    continue;
  }
  await this.process(todo);
  await this.api.TodoLists.done({ todoId: todo.id });
}

Example (memoize stable resolved roots):

// once per config/session
const resolvedPinnedRoots = memoryRoots.map((root) =>
  realpathExistingOrNew(path.resolve(root, AUTO_MEMORY_PINNED_DIRNAME)),
);

function isProtectedPinnedMemoryPath(resolvedCandidate: string|undefined, literalCandidate: string) {
  return memoryRoots.some((_, i) =>
    isWithinRootCaseInsensitive(literalCandidate, literalPinnedRoots[i]) ||
    (resolvedCandidate && resolvedPinnedRoots[i] &&
      isWithinRootCaseInsensitive(resolvedCandidate, resolvedPinnedRoots[i]!))
  );
}

4) Don’t endorse unverifiable perf claims

  • If a benchmark can’t be re-run in review, require an actionable artifact: script, env, raw results, and what measurement proves the mechanism—not just the number.

Source discussions