perf(skill-loader): add blocking discovery API with worker threads

Implement synchronous skill discovery using Node.js Worker Threads and Atomics.wait for blocking operations. Allows synchronous API access while leveraging async operations internally via dedicated worker thread.

Changes:
- blocking.ts: Main blocking discovery function using SharedArrayBuffer and MessagePort
- discover-worker.ts: Worker thread implementation for async skill discovery
- blocking.test.ts: Comprehensive test suite with BDD comments

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
YeonGyu-Kim
2026-01-04 21:48:58 +09:00
parent b1f36d61a8
commit ce69007fde
3 changed files with 331 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
import { Worker, MessageChannel, receiveMessageOnPort } from "worker_threads"
import type { LoadedSkill, SkillScope } from "./types"
interface WorkerInput {
dirs: string[]
scopes: SkillScope[]
}
interface WorkerOutputSuccess {
ok: true
skills: LoadedSkill[]
}
interface WorkerOutputError {
ok: false
error: { message: string; stack?: string }
}
type WorkerOutput = WorkerOutputSuccess | WorkerOutputError
const TIMEOUT_MS = 30000
export function discoverAllSkillsBlocking(dirs: string[], scopes: SkillScope[]): LoadedSkill[] {
const signal = new Int32Array(new SharedArrayBuffer(4))
const { port1, port2 } = new MessageChannel()
const worker = new Worker(new URL("./discover-worker.ts", import.meta.url), {
workerData: { signal }
})
worker.postMessage({ port: port2 }, [port2])
const input: WorkerInput = { dirs, scopes }
port1.postMessage(input)
const waitResult = Atomics.wait(signal, 0, 0, TIMEOUT_MS)
if (waitResult === "timed-out") {
worker.terminate()
port1.close()
throw new Error(`Worker timeout after ${TIMEOUT_MS}ms`)
}
const message = receiveMessageOnPort(port1)
worker.terminate()
port1.close()
if (!message) {
throw new Error("Worker did not return result")
}
const output = message.message as WorkerOutput
if (output.ok === false) {
const error = new Error(output.error.message)
error.stack = output.error.stack
throw error
}
return output.skills
}