rohitg00/ai-engineering-from-scratch · error · Error
capacity must be > 0
Error message
capacity must be > 0
What it means
Constructor precondition on RingBuffer: capacity sizes the fixed slot array and drives the write-index wrap. Zero or negative capacity would allocate no slots and make every push meaningless, so the guard rejects it before the buffer is built.
Solutions
- Pass a positive integer capacity (e.g. 1000 retained spans)
- Clamp caller-supplied sizes to a minimum like Math.max(1, n) when the value comes from config
- Validate capacity in one place at startup and fail fast with a clear message
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at phases/19-capstone-projects/11-llm-observability-dashboard/code/ts/src/spans.ts:44 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/cc802dec37af6fba.
Report an issue: GitHub.
Appendix: source
Thrown at phases/19-capstone-projects/11-llm-observability-dashboard/code/ts/src/spans.ts:44
return Number.isFinite(n) && Number.isInteger(n) && n >= 0;
}
const TOKEN_KEYS = [
"gen_ai.request.prompt_tokens",
"gen_ai.usage.input_tokens",
"gen_ai.request.completion_tokens",
"gen_ai.usage.output_tokens",
"gen_ai.usage.total_tokens",
];
export class RingBuffer<T> {
private readonly capacity: number;
private readonly slots: (T | undefined)[];
private writeIdx = 0;
private filled = false;
constructor(capacity: number) {
if (capacity <= 0) throw new Error("capacity must be > 0");
this.capacity = capacity;
this.slots = new Array<T | undefined>(capacity);
}
push(item: T): void {
this.slots[this.writeIdx] = item;
this.writeIdx = (this.writeIdx + 1) % this.capacity;
if (this.writeIdx === 0) this.filled = true;
}
size(): number {
return this.filled ? this.capacity : this.writeIdx;
}
isFull(): boolean {
return this.filled;
}
View on GitHub (pinned to 39ea8a1c6d)