andy-stack_vaultkeeper-ai/Helpers/Semaphore.ts
Andrew Beal cefc408b2e Add context passing between execution steps and enhance orchestration
- Add context_for_next_step parameter to CompleteStep for passing execution history
- Add context parameter to CompleteTask for preserving task completion state
- Update OrchestrationResult to handle context propagation between steps
- Add debug color differentiation for agent types (Main, Execution, Orchestration, Planning)
- Reorganize SearchTypes from Helpers to Types directory
- Add justification requirement for execution deviations
- Support reasonable deviations in orchestration plan validation
- Refactor dependency service with TryResolve utility
- Add whitespace cleanup to Semaphore class
2026-01-28 21:23:47 +00:00

43 lines
No EOL
949 B
TypeScript

export class Semaphore {
private max: number;
private count: number;
private readonly waitAsync: boolean;
private readonly queue: ((value: boolean) => void)[];
constructor(max: number, waitAsync: boolean) {
this.max = max;
this.count = max;
this.waitAsync = waitAsync;
this.queue = [];
}
async wait(): Promise<boolean> {
if (this.count > 0) {
this.count--;
return true;
}
if (!this.waitAsync) {
return false;
}
return new Promise<boolean>((resolve) => {
this.queue.push(resolve);
});
}
release() {
if (this.queue.length > 0) {
const resolve = this.queue.shift();
if (resolve) {
resolve(true);
}
} else {
if (this.count < this.max) {
this.count++;
}
}
}
}