triflux 10.0.0 → 10.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +171 -0
- package/README.md +32 -15
- package/bin/triflux.mjs +62 -5
- package/hooks/agent-route-guard.mjs +109 -0
- package/hooks/cross-review-tracker.mjs +122 -0
- package/hooks/error-context.mjs +148 -0
- package/hooks/hook-adaptive-collector.mjs +86 -0
- package/hooks/hook-manager.mjs +365 -0
- package/hooks/hook-orchestrator.mjs +312 -0
- package/hooks/hook-registry.json +246 -0
- package/hooks/hooks.json +89 -0
- package/hooks/keyword-rules.json +574 -0
- package/hooks/lib/resolve-root.mjs +59 -0
- package/hooks/mcp-config-watcher.mjs +80 -0
- package/hooks/pipeline-stop.mjs +76 -0
- package/hooks/safety-guard.mjs +169 -0
- package/hooks/subagent-verifier.mjs +80 -0
- package/hub/account-broker.mjs +251 -0
- package/hub/adaptive-diagnostic.mjs +323 -0
- package/hub/adaptive-inject.mjs +186 -0
- package/hub/adaptive-memory.mjs +163 -0
- package/hub/adaptive.mjs +143 -0
- package/hub/assign-callbacks.mjs +133 -0
- package/hub/bridge.mjs +799 -0
- package/hub/cli-adapter-base.mjs +280 -0
- package/hub/codex-adapter.mjs +199 -0
- package/hub/codex-compat.mjs +11 -0
- package/hub/codex-preflight.mjs +166 -0
- package/hub/delegator/contracts.mjs +37 -0
- package/hub/delegator/index.mjs +14 -0
- package/hub/delegator/schema/delegator-tools.schema.json +250 -0
- package/hub/delegator/service.mjs +307 -0
- package/hub/delegator/tool-definitions.mjs +35 -0
- package/hub/fullcycle.mjs +96 -0
- package/hub/gemini-adapter.mjs +180 -0
- package/hub/hitl.mjs +143 -0
- package/hub/intent.mjs +193 -0
- package/hub/lib/cache-guard.mjs +114 -0
- package/hub/lib/known-errors.json +72 -0
- package/hub/lib/memory-store.mjs +748 -0
- package/hub/lib/process-utils.mjs +361 -0
- package/hub/lib/ssh-command.mjs +211 -0
- package/hub/lib/ssh-retry.mjs +59 -0
- package/hub/lib/uuidv7.mjs +44 -0
- package/hub/memory-doctor.mjs +480 -0
- package/hub/middleware/request-logger.mjs +161 -0
- package/hub/paths.mjs +30 -0
- package/hub/pipe.mjs +664 -0
- package/hub/pipeline/gates/confidence.mjs +56 -0
- package/hub/pipeline/gates/consensus.mjs +94 -0
- package/hub/pipeline/gates/index.mjs +5 -0
- package/hub/pipeline/gates/selfcheck.mjs +82 -0
- package/hub/pipeline/index.mjs +318 -0
- package/hub/pipeline/state.mjs +191 -0
- package/hub/pipeline/transitions.mjs +124 -0
- package/hub/platform.mjs +225 -0
- package/hub/public/dashboard.html +355 -0
- package/hub/public/tray-icon.ico +0 -0
- package/hub/public/tray-icon.png +0 -0
- package/hub/quality/deslop.mjs +253 -0
- package/hub/reflexion.mjs +372 -0
- package/hub/research.mjs +146 -0
- package/hub/router.mjs +791 -0
- package/hub/routing/complexity.mjs +166 -0
- package/hub/routing/index.mjs +117 -0
- package/hub/routing/q-learning.mjs +336 -0
- package/hub/schema.sql +148 -0
- package/hub/server.mjs +1264 -0
- package/hub/session-fingerprint.mjs +352 -0
- package/hub/state.mjs +258 -0
- package/hub/store-adapter.mjs +118 -0
- package/hub/store.mjs +857 -0
- package/hub/team/agent-map.json +11 -0
- package/hub/team/ansi.mjs +379 -0
- package/hub/team/backend.mjs +90 -0
- package/hub/team/cli/commands/attach.mjs +37 -0
- package/hub/team/cli/commands/control.mjs +43 -0
- package/hub/team/cli/commands/debug.mjs +74 -0
- package/hub/team/cli/commands/focus.mjs +53 -0
- package/hub/team/cli/commands/interrupt.mjs +36 -0
- package/hub/team/cli/commands/kill.mjs +37 -0
- package/hub/team/cli/commands/list.mjs +24 -0
- package/hub/team/cli/commands/send.mjs +37 -0
- package/hub/team/cli/commands/start/index.mjs +106 -0
- package/hub/team/cli/commands/start/parse-args.mjs +130 -0
- package/hub/team/cli/commands/start/start-headless.mjs +109 -0
- package/hub/team/cli/commands/start/start-in-process.mjs +40 -0
- package/hub/team/cli/commands/start/start-mux.mjs +73 -0
- package/hub/team/cli/commands/start/start-wt.mjs +69 -0
- package/hub/team/cli/commands/status.mjs +87 -0
- package/hub/team/cli/commands/stop.mjs +31 -0
- package/hub/team/cli/commands/task.mjs +30 -0
- package/hub/team/cli/commands/tasks.mjs +13 -0
- package/hub/team/cli/help.mjs +42 -0
- package/hub/team/cli/index.mjs +41 -0
- package/hub/team/cli/manifest.mjs +29 -0
- package/hub/team/cli/render.mjs +30 -0
- package/hub/team/cli/services/attach-fallback.mjs +54 -0
- package/hub/team/cli/services/hub-client.mjs +227 -0
- package/hub/team/cli/services/member-selector.mjs +30 -0
- package/hub/team/cli/services/native-control.mjs +117 -0
- package/hub/team/cli/services/runtime-mode.mjs +62 -0
- package/hub/team/cli/services/state-store.mjs +48 -0
- package/hub/team/cli/services/task-model.mjs +30 -0
- package/hub/team/conductor-mesh-bridge.mjs +121 -0
- package/hub/team/conductor.mjs +671 -0
- package/hub/team/dashboard-anchor.mjs +14 -0
- package/hub/team/dashboard-layout.mjs +33 -0
- package/hub/team/dashboard-open.mjs +153 -0
- package/hub/team/dashboard.mjs +274 -0
- package/hub/team/event-log.mjs +76 -0
- package/hub/team/handoff.mjs +303 -0
- package/hub/team/headless.mjs +1156 -0
- package/hub/team/health-probe.mjs +272 -0
- package/hub/team/launcher-template.mjs +95 -0
- package/hub/team/lead-control.mjs +104 -0
- package/hub/team/native-supervisor.mjs +392 -0
- package/hub/team/native.mjs +649 -0
- package/hub/team/nativeProxy.mjs +688 -0
- package/hub/team/notify.mjs +293 -0
- package/hub/team/orchestrator.mjs +161 -0
- package/hub/team/pane.mjs +153 -0
- package/hub/team/process-cleanup.mjs +342 -0
- package/hub/team/psmux.mjs +1354 -0
- package/hub/team/remote-probe.mjs +276 -0
- package/hub/team/remote-session.mjs +299 -0
- package/hub/team/remote-watcher.mjs +478 -0
- package/hub/team/routing.mjs +223 -0
- package/hub/team/session-sync.mjs +169 -0
- package/hub/team/session.mjs +611 -0
- package/hub/team/shared.mjs +13 -0
- package/hub/team/staleState.mjs +361 -0
- package/hub/team/swarm-hypervisor.mjs +589 -0
- package/hub/team/swarm-locks.mjs +204 -0
- package/hub/team/swarm-planner.mjs +260 -0
- package/hub/team/swarm-reconciler.mjs +137 -0
- package/hub/team/tui-lite.mjs +380 -0
- package/hub/team/tui-remote-adapter.mjs +393 -0
- package/hub/team/tui-viewer.mjs +463 -0
- package/hub/team/tui.mjs +1449 -0
- package/hub/team/worktree-lifecycle.mjs +193 -0
- package/hub/team/wt-manager.mjs +407 -0
- package/hub/team/wt-templates.json +43 -0
- package/hub/team-bridge.mjs +27 -0
- package/hub/token-mode.mjs +224 -0
- package/hub/tools.mjs +636 -0
- package/hub/tray.mjs +376 -0
- package/hub/workers/claude-worker.mjs +475 -0
- package/hub/workers/codex-mcp.mjs +507 -0
- package/hub/workers/delegator-mcp.mjs +1076 -0
- package/hub/workers/factory.mjs +21 -0
- package/hub/workers/gemini-worker.mjs +374 -0
- package/hub/workers/interface.mjs +52 -0
- package/hub/workers/worker-utils.mjs +104 -0
- package/hud/colors.mjs +88 -0
- package/hud/constants.mjs +88 -0
- package/hud/context-monitor.mjs +403 -0
- package/hud/hud-qos-status.mjs +210 -0
- package/hud/providers/claude.mjs +314 -0
- package/hud/providers/codex.mjs +151 -0
- package/hud/providers/gemini.mjs +320 -0
- package/hud/renderers.mjs +442 -0
- package/hud/terminal.mjs +140 -0
- package/hud/utils.mjs +313 -0
- package/mesh/index.mjs +63 -0
- package/mesh/mesh-budget.mjs +128 -0
- package/mesh/mesh-heartbeat.mjs +100 -0
- package/mesh/mesh-protocol.mjs +96 -0
- package/mesh/mesh-queue.mjs +165 -0
- package/mesh/mesh-registry.mjs +78 -0
- package/mesh/mesh-router.mjs +76 -0
- package/package.json +8 -1
- package/references/hosts.json +33 -0
- package/scripts/__tests__/gen-skill-docs.test.mjs +87 -0
- package/scripts/__tests__/keyword-detector.test.mjs +234 -0
- package/scripts/__tests__/mcp-guard-engine.test.mjs +118 -0
- package/scripts/__tests__/remote-spawn-transfer.test.mjs +117 -0
- package/scripts/__tests__/remote-spawn.test.mjs +92 -0
- package/scripts/__tests__/skill-template.test.mjs +193 -0
- package/scripts/__tests__/smoke.test.mjs +34 -0
- package/scripts/cache-buildup.mjs +30 -0
- package/scripts/cache-doctor.mjs +149 -0
- package/scripts/cache-warmup.mjs +557 -0
- package/scripts/claudemd-sync.mjs +148 -0
- package/scripts/cli-route.sh +3 -0
- package/scripts/completions/tfx.bash +47 -0
- package/scripts/completions/tfx.fish +44 -0
- package/scripts/completions/tfx.zsh +83 -0
- package/scripts/cross-review-gate.mjs +126 -0
- package/scripts/cross-review-tracker.mjs +238 -0
- package/scripts/gen-skill-docs.mjs +111 -0
- package/scripts/headless-guard-fast.sh +21 -0
- package/scripts/headless-guard.mjs +360 -0
- package/scripts/hub-ensure.mjs +120 -0
- package/scripts/keyword-detector.mjs +272 -0
- package/scripts/keyword-rules-expander.mjs +521 -0
- package/scripts/lib/claudemd-scanner.mjs +218 -0
- package/scripts/lib/context.mjs +67 -0
- package/scripts/lib/cross-review-utils.mjs +51 -0
- package/scripts/lib/env-probe.mjs +241 -0
- package/scripts/lib/gemini-profiles.mjs +85 -0
- package/scripts/lib/handoff.mjs +171 -0
- package/scripts/lib/hook-utils.mjs +14 -0
- package/scripts/lib/keyword-rules.mjs +166 -0
- package/scripts/lib/logger.mjs +105 -0
- package/scripts/lib/mcp-filter.mjs +739 -0
- package/scripts/lib/mcp-guard-engine.mjs +954 -0
- package/scripts/lib/mcp-manifest.mjs +79 -0
- package/scripts/lib/mcp-server-catalog.mjs +118 -0
- package/scripts/lib/psmux-info.mjs +119 -0
- package/scripts/lib/remote-spawn-transfer.mjs +196 -0
- package/scripts/lib/skill-template.mjs +326 -0
- package/scripts/mcp-check.mjs +237 -0
- package/scripts/mcp-cleanup.ps1 +17 -0
- package/scripts/mcp-gateway-config.mjs +207 -0
- package/scripts/mcp-gateway-ensure.mjs +85 -0
- package/scripts/mcp-gateway-integration-test.mjs +228 -0
- package/scripts/mcp-gateway-start.mjs +226 -0
- package/scripts/mcp-gateway-start.ps1 +141 -0
- package/scripts/mcp-gateway-verify.mjs +77 -0
- package/scripts/mcp-safety-guard.mjs +44 -0
- package/scripts/notion-read.mjs +556 -0
- package/scripts/pack.mjs +295 -0
- package/scripts/preflight-cache.mjs +69 -0
- package/scripts/preinstall.mjs +96 -0
- package/scripts/remote-spawn.mjs +1376 -0
- package/scripts/run.cjs +79 -0
- package/scripts/session-spawn-helper.mjs +185 -0
- package/scripts/setup.mjs +1178 -0
- package/scripts/test-lock.mjs +71 -0
- package/scripts/test-tfx-route-no-claude-native.mjs +57 -0
- package/scripts/tfx-batch-stats.mjs +96 -0
- package/scripts/tfx-gate-activate.mjs +89 -0
- package/scripts/tfx-route-post.mjs +505 -0
- package/scripts/tfx-route-worker.mjs +223 -0
- package/scripts/tfx-route.sh +2014 -0
- package/scripts/tmp-cleanup.mjs +103 -0
- package/scripts/token-snapshot.mjs +575 -0
- package/skills/tfx-auto/SKILL.md.tmpl +2 -3
- package/skills/tfx-autoresearch/SKILL.md +6 -5
- package/skills/tfx-codex/SKILL.md.tmpl +2 -3
- package/skills/tfx-codex-swarm-workspace/iteration-1/benchmark.json +33 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/eval_metadata.json +42 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/with_skill/grading.json +11 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/with_skill/outputs/analysis.md +87 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/with_skill/outputs/classification.md +35 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/with_skill/outputs/commands.sh +275 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/with_skill/outputs/routing.md +56 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/with_skill/timing.json +5 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/without_skill/grading.json +11 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/without_skill/outputs/analysis.md +92 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/without_skill/outputs/classification.md +71 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/without_skill/outputs/commands.sh +264 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/without_skill/outputs/routing.md +113 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/full-swarm-all-prds/without_skill/timing.json +5 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/eval_metadata.json +32 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/with_skill/grading.json +9 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/with_skill/outputs/analysis.md +96 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/with_skill/outputs/classification.md +38 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/with_skill/outputs/commands.sh +151 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/with_skill/outputs/routing.md +51 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/with_skill/timing.json +5 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/without_skill/grading.json +9 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/without_skill/outputs/analysis.md +127 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/without_skill/outputs/classification.md +57 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/without_skill/outputs/commands.sh +129 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/without_skill/outputs/routing.md +84 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/implicit-swarm-no-keywords/without_skill/timing.json +5 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/eval_metadata.json +27 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/with_skill/grading.json +8 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/with_skill/outputs/analysis.md +98 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/with_skill/outputs/classification.md +65 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/with_skill/outputs/commands.sh +123 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/with_skill/outputs/routing.md +66 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/with_skill/timing.json +5 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/without_skill/grading.json +8 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/without_skill/outputs/analysis.md +88 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/without_skill/outputs/classification.md +40 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/without_skill/outputs/commands.sh +130 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/without_skill/outputs/routing.md +61 -0
- package/skills/tfx-codex-swarm-workspace/iteration-1/selective-spawn-with-override/without_skill/timing.json +5 -0
- package/skills/tfx-deep-interview/SKILL.md +1 -2
- package/skills/tfx-plan/SKILL.md.tmpl +2 -3
- package/skills/tfx-psmux-rules/SKILL.md +11 -2
- package/skills/tfx-qa/SKILL.md.tmpl +2 -3
- package/skills/tfx-remote-spawn/SKILL.md +8 -11
- package/skills/tfx-research/SKILL.md.tmpl +2 -3
- package/skills/tfx-review/SKILL.md.tmpl +2 -3
- package/skills/tfx-workspace/async-tests/run-tests.sh +203 -0
- package/skills/tfx-workspace/evals/evals.json +79 -0
- package/skills/tfx-workspace/iteration-1/benchmark.json +162 -0
- package/skills/tfx-workspace/iteration-1/codex-gemini-remap/eval_metadata.json +11 -0
- package/skills/tfx-workspace/iteration-1/codex-gemini-remap/old_skill/grading.json +9 -0
- package/skills/tfx-workspace/iteration-1/codex-gemini-remap/old_skill/outputs/analysis.md +154 -0
- package/skills/tfx-workspace/iteration-1/codex-gemini-remap/old_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/codex-gemini-remap/with_skill/grading.json +9 -0
- package/skills/tfx-workspace/iteration-1/codex-gemini-remap/with_skill/outputs/analysis.md +126 -0
- package/skills/tfx-workspace/iteration-1/codex-gemini-remap/with_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/doctor-diagnosis/eval_metadata.json +11 -0
- package/skills/tfx-workspace/iteration-1/doctor-diagnosis/old_skill/grading.json +9 -0
- package/skills/tfx-workspace/iteration-1/doctor-diagnosis/old_skill/outputs/analysis.md +119 -0
- package/skills/tfx-workspace/iteration-1/doctor-diagnosis/old_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/doctor-diagnosis/with_skill/grading.json +9 -0
- package/skills/tfx-workspace/iteration-1/doctor-diagnosis/with_skill/outputs/analysis.md +115 -0
- package/skills/tfx-workspace/iteration-1/doctor-diagnosis/with_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/hub-start-sequence/eval_metadata.json +10 -0
- package/skills/tfx-workspace/iteration-1/hub-start-sequence/old_skill/grading.json +8 -0
- package/skills/tfx-workspace/iteration-1/hub-start-sequence/old_skill/outputs/analysis.md +86 -0
- package/skills/tfx-workspace/iteration-1/hub-start-sequence/old_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/hub-start-sequence/with_skill/grading.json +8 -0
- package/skills/tfx-workspace/iteration-1/hub-start-sequence/with_skill/outputs/analysis.md +81 -0
- package/skills/tfx-workspace/iteration-1/hub-start-sequence/with_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/multi-team-creation/eval_metadata.json +12 -0
- package/skills/tfx-workspace/iteration-1/multi-team-creation/old_skill/grading.json +10 -0
- package/skills/tfx-workspace/iteration-1/multi-team-creation/old_skill/outputs/analysis.md +316 -0
- package/skills/tfx-workspace/iteration-1/multi-team-creation/old_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/multi-team-creation/with_skill/grading.json +10 -0
- package/skills/tfx-workspace/iteration-1/multi-team-creation/with_skill/outputs/analysis.md +352 -0
- package/skills/tfx-workspace/iteration-1/multi-team-creation/with_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/review.html +1325 -0
- package/skills/tfx-workspace/iteration-1/routing-implement-shortcut/eval_metadata.json +12 -0
- package/skills/tfx-workspace/iteration-1/routing-implement-shortcut/old_skill/grading.json +10 -0
- package/skills/tfx-workspace/iteration-1/routing-implement-shortcut/old_skill/outputs/analysis.md +97 -0
- package/skills/tfx-workspace/iteration-1/routing-implement-shortcut/old_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/routing-implement-shortcut/with_skill/grading.json +10 -0
- package/skills/tfx-workspace/iteration-1/routing-implement-shortcut/with_skill/outputs/analysis.md +94 -0
- package/skills/tfx-workspace/iteration-1/routing-implement-shortcut/with_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/routing-multi-task-triage/eval_metadata.json +12 -0
- package/skills/tfx-workspace/iteration-1/routing-multi-task-triage/old_skill/grading.json +10 -0
- package/skills/tfx-workspace/iteration-1/routing-multi-task-triage/old_skill/outputs/analysis.md +209 -0
- package/skills/tfx-workspace/iteration-1/routing-multi-task-triage/old_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-1/routing-multi-task-triage/with_skill/grading.json +10 -0
- package/skills/tfx-workspace/iteration-1/routing-multi-task-triage/with_skill/outputs/analysis.md +193 -0
- package/skills/tfx-workspace/iteration-1/routing-multi-task-triage/with_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-2/benchmark.json +62 -0
- package/skills/tfx-workspace/iteration-2/multi-team-creation-refactored/eval_metadata.json +13 -0
- package/skills/tfx-workspace/iteration-2/multi-team-creation-refactored/old_skill/grading.json +11 -0
- package/skills/tfx-workspace/iteration-2/multi-team-creation-refactored/old_skill/outputs/analysis.md +382 -0
- package/skills/tfx-workspace/iteration-2/multi-team-creation-refactored/old_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-2/multi-team-creation-refactored/with_skill/grading.json +11 -0
- package/skills/tfx-workspace/iteration-2/multi-team-creation-refactored/with_skill/outputs/analysis.md +333 -0
- package/skills/tfx-workspace/iteration-2/multi-team-creation-refactored/with_skill/timing.json +5 -0
- package/skills/tfx-workspace/iteration-2/review.html +1325 -0
- package/skills/tfx-workspace/skill-snapshot/tfx-auto/SKILL.md +217 -0
- package/skills/{tfx-auto-codex/SKILL.md.tmpl → tfx-workspace/skill-snapshot/tfx-auto-codex/SKILL.md} +3 -31
- package/skills/tfx-workspace/skill-snapshot/tfx-codex/SKILL.md +65 -0
- package/skills/tfx-workspace/skill-snapshot/tfx-doctor/SKILL.md +94 -0
- package/skills/{tfx-gemini/SKILL.md.tmpl → tfx-workspace/skill-snapshot/tfx-gemini/SKILL.md} +6 -14
- package/skills/tfx-workspace/skill-snapshot/tfx-hub/SKILL.md +133 -0
- package/skills/tfx-workspace/skill-snapshot/tfx-multi/SKILL.md +426 -0
- package/skills/tfx-workspace/skill-snapshot/tfx-setup/SKILL.md +101 -0
- package/skills/merge-worktree/SKILL.md.tmpl +0 -144
- package/skills/shared/arguments-processing.md +0 -2
- package/skills/shared/mandatory-rules.md +0 -6
- package/skills/shared/telemetry-segment.md +0 -6
- package/skills/star-prompt/SKILL.md.tmpl +0 -122
- package/skills/tfx-analysis/SKILL.md.tmpl +0 -106
- package/skills/tfx-analysis/skill.json +0 -11
- package/skills/tfx-auto/skill.json +0 -26
- package/skills/tfx-auto-codex/skill.json +0 -8
- package/skills/tfx-autopilot/SKILL.md.tmpl +0 -115
- package/skills/tfx-autopilot/skill.json +0 -10
- package/skills/tfx-autoresearch/SKILL.md.tmpl +0 -135
- package/skills/tfx-autoresearch/skill.json +0 -14
- package/skills/tfx-autoroute/SKILL.md.tmpl +0 -188
- package/skills/tfx-autoroute/skill.json +0 -12
- package/skills/tfx-codex/skill.json +0 -8
- package/skills/tfx-codex-swarm/SKILL.md.tmpl +0 -16
- package/skills/tfx-codex-swarm/skill.json +0 -5
- package/skills/tfx-consensus/SKILL.md.tmpl +0 -145
- package/skills/tfx-consensus/skill.json +0 -8
- package/skills/tfx-debate/SKILL.md.tmpl +0 -191
- package/skills/tfx-debate/skill.json +0 -12
- package/skills/tfx-deep-analysis/SKILL.md.tmpl +0 -227
- package/skills/tfx-deep-analysis/skill.json +0 -10
- package/skills/tfx-deep-interview/SKILL.md.tmpl +0 -203
- package/skills/tfx-deep-interview/skill.json +0 -12
- package/skills/tfx-deep-plan/SKILL.md.tmpl +0 -281
- package/skills/tfx-deep-plan/skill.json +0 -13
- package/skills/tfx-deep-qa/SKILL.md.tmpl +0 -164
- package/skills/tfx-deep-qa/skill.json +0 -11
- package/skills/tfx-deep-research/SKILL.md.tmpl +0 -216
- package/skills/tfx-deep-research/skill.json +0 -14
- package/skills/tfx-deep-review/SKILL.md.tmpl +0 -178
- package/skills/tfx-deep-review/skill.json +0 -12
- package/skills/tfx-doctor/SKILL.md.tmpl +0 -172
- package/skills/tfx-doctor/skill.json +0 -8
- package/skills/tfx-find/skill.json +0 -12
- package/skills/tfx-forge/SKILL.md.tmpl +0 -187
- package/skills/tfx-forge/skill.json +0 -12
- package/skills/tfx-fullcycle/SKILL.md.tmpl +0 -285
- package/skills/tfx-fullcycle/skill.json +0 -11
- package/skills/tfx-gemini/skill.json +0 -8
- package/skills/tfx-hooks/SKILL.md.tmpl +0 -216
- package/skills/tfx-hooks/skill.json +0 -8
- package/skills/tfx-hub/SKILL.md.tmpl +0 -212
- package/skills/tfx-hub/skill.json +0 -8
- package/skills/tfx-index/skill.json +0 -11
- package/skills/tfx-interview/SKILL.md.tmpl +0 -284
- package/skills/tfx-interview/skill.json +0 -12
- package/skills/tfx-multi/SKILL.md.tmpl +0 -183
- package/skills/tfx-multi/skill.json +0 -8
- package/skills/tfx-panel/SKILL.md.tmpl +0 -188
- package/skills/tfx-panel/skill.json +0 -12
- package/skills/tfx-persist/SKILL.md.tmpl +0 -269
- package/skills/tfx-persist/skill.json +0 -12
- package/skills/tfx-plan/skill.json +0 -11
- package/skills/tfx-profile/SKILL.md.tmpl +0 -239
- package/skills/tfx-profile/skill.json +0 -8
- package/skills/tfx-prune/SKILL.md.tmpl +0 -199
- package/skills/tfx-prune/skill.json +0 -12
- package/skills/tfx-psmux-rules/SKILL.md.tmpl +0 -317
- package/skills/tfx-psmux-rules/skill.json +0 -8
- package/skills/tfx-qa/skill.json +0 -11
- package/skills/tfx-ralph/SKILL.md.tmpl +0 -27
- package/skills/tfx-ralph/skill.json +0 -8
- package/skills/tfx-remote-setup/SKILL.md.tmpl +0 -576
- package/skills/tfx-remote-setup/skill.json +0 -8
- package/skills/tfx-remote-spawn/SKILL.md.tmpl +0 -263
- package/skills/tfx-remote-spawn/skill.json +0 -9
- package/skills/tfx-research/skill.json +0 -13
- package/skills/tfx-review/skill.json +0 -11
- package/skills/tfx-setup/SKILL.md.tmpl +0 -380
- package/skills/tfx-setup/skill.json +0 -8
- package/skills/tfx-swarm/SKILL.md.tmpl +0 -154
- package/skills/tfx-swarm/skill.json +0 -5
package/hub/store.mjs
ADDED
|
@@ -0,0 +1,857 @@
|
|
|
1
|
+
// hub/store.mjs — SQLite 감사 로그/메타데이터 저장소
|
|
2
|
+
// 실시간 배달 큐는 router/pipe가 담당하고, SQLite는 재생/감사 용도로만 유지한다.
|
|
3
|
+
import { recalcConfidence } from './reflexion.mjs';
|
|
4
|
+
import { readFileSync, mkdirSync } from 'node:fs';
|
|
5
|
+
import { join, dirname } from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
import { createRequire } from 'node:module';
|
|
8
|
+
import { uuidv7 } from './lib/uuidv7.mjs';
|
|
9
|
+
|
|
10
|
+
export { uuidv7 };
|
|
11
|
+
|
|
12
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
14
|
+
|
|
15
|
+
function parseJson(str, fallback = null) {
|
|
16
|
+
if (str == null) return fallback;
|
|
17
|
+
try { return JSON.parse(str); } catch { return fallback; }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function parseAgentRow(row) {
|
|
21
|
+
if (!row) return null;
|
|
22
|
+
const { capabilities_json, topics_json, metadata_json, ...rest } = row;
|
|
23
|
+
return {
|
|
24
|
+
...rest,
|
|
25
|
+
capabilities: parseJson(capabilities_json, []),
|
|
26
|
+
topics: parseJson(topics_json, []),
|
|
27
|
+
metadata: parseJson(metadata_json, {}),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function parseMessageRow(row) {
|
|
32
|
+
if (!row) return null;
|
|
33
|
+
const { payload_json, ...rest } = row;
|
|
34
|
+
return { ...rest, payload: parseJson(payload_json, {}) };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function parseHumanRequestRow(row) {
|
|
38
|
+
if (!row) return null;
|
|
39
|
+
const { schema_json, response_json, ...rest } = row;
|
|
40
|
+
return {
|
|
41
|
+
...rest,
|
|
42
|
+
schema: parseJson(schema_json, {}),
|
|
43
|
+
response: parseJson(response_json, null),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function parseAssignRow(row) {
|
|
48
|
+
if (!row) return null;
|
|
49
|
+
const { payload_json, result_json, error_json, ...rest } = row;
|
|
50
|
+
return {
|
|
51
|
+
...rest,
|
|
52
|
+
payload: parseJson(payload_json, {}),
|
|
53
|
+
result: parseJson(result_json, null),
|
|
54
|
+
error: parseJson(error_json, null),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function parseReflexionRow(row) {
|
|
59
|
+
if (!row) return null;
|
|
60
|
+
const { context_json, adaptive_state_json, ...rest } = row;
|
|
61
|
+
return {
|
|
62
|
+
...rest,
|
|
63
|
+
type: rest.type || 'reflexion',
|
|
64
|
+
context: parseJson(context_json, {}),
|
|
65
|
+
adaptive_state: parseJson(adaptive_state_json, {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function hasColumn(db, tableName, columnName) {
|
|
70
|
+
const rows = db.prepare(`PRAGMA table_info(${tableName})`).all();
|
|
71
|
+
return rows.some((row) => row.name === columnName);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function ensureColumn(db, tableName, columnName, definition) {
|
|
75
|
+
if (hasColumn(db, tableName, columnName)) return;
|
|
76
|
+
db.exec(`ALTER TABLE ${tableName} ADD COLUMN ${columnName} ${definition}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 저장소 생성
|
|
81
|
+
* @param {string} dbPath
|
|
82
|
+
*/
|
|
83
|
+
export async function importBetterSqlite3() {
|
|
84
|
+
const mod = await import('better-sqlite3');
|
|
85
|
+
return mod.default ?? mod;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function resolveBetterSqlite3(options = {}) {
|
|
89
|
+
if (options.DatabaseCtor) return options.DatabaseCtor;
|
|
90
|
+
const mod = require('better-sqlite3');
|
|
91
|
+
return mod.default ?? mod;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function createStore(dbPath, options = {}) {
|
|
95
|
+
mkdirSync(dirname(dbPath), { recursive: true });
|
|
96
|
+
const Database = resolveBetterSqlite3(options);
|
|
97
|
+
const db = new Database(dbPath);
|
|
98
|
+
|
|
99
|
+
db.pragma('journal_mode = WAL');
|
|
100
|
+
db.pragma('synchronous = NORMAL');
|
|
101
|
+
db.pragma('foreign_keys = ON');
|
|
102
|
+
db.pragma('busy_timeout = 5000');
|
|
103
|
+
db.pragma('wal_autocheckpoint = 1000');
|
|
104
|
+
|
|
105
|
+
const schemaSQL = readFileSync(join(__dirname, 'schema.sql'), 'utf8');
|
|
106
|
+
db.exec("CREATE TABLE IF NOT EXISTS _meta (key TEXT PRIMARY KEY, value TEXT)");
|
|
107
|
+
const SCHEMA_VERSION = '4';
|
|
108
|
+
const curVer = (() => {
|
|
109
|
+
try { return db.prepare("SELECT value FROM _meta WHERE key='schema_version'").pluck().get(); }
|
|
110
|
+
catch { return null; }
|
|
111
|
+
})();
|
|
112
|
+
// 마이그레이션 전략: 스키마 버전이 다르면 schema.sql을 재실행한다.
|
|
113
|
+
// schema.sql은 CREATE TABLE IF NOT EXISTS 패턴을 사용하므로 멱등하게 적용된다.
|
|
114
|
+
// 비파괴적 컬럼 추가는 자동으로 처리되지만, 컬럼 제거/이름 변경은 수동 마이그레이션이 필요하다.
|
|
115
|
+
if (curVer !== SCHEMA_VERSION) {
|
|
116
|
+
if (curVer != null) {
|
|
117
|
+
// 이미 버전이 기록된 DB에서 버전 불일치가 발생한 경우 경고한다.
|
|
118
|
+
console.warn(`[store] schema version mismatch: found=${curVer} expected=${SCHEMA_VERSION}. Applying schema.sql (idempotent).`);
|
|
119
|
+
}
|
|
120
|
+
db.exec(schemaSQL);
|
|
121
|
+
db.prepare("INSERT OR REPLACE INTO _meta (key, value) VALUES ('schema_version', ?)").run(SCHEMA_VERSION);
|
|
122
|
+
}
|
|
123
|
+
ensureColumn(db, 'reflexion_entries', 'type', "TEXT NOT NULL DEFAULT 'reflexion'");
|
|
124
|
+
ensureColumn(db, 'reflexion_entries', 'adaptive_state_json', "TEXT NOT NULL DEFAULT '{}'");
|
|
125
|
+
|
|
126
|
+
const S = {
|
|
127
|
+
upsertAgent: db.prepare(`
|
|
128
|
+
INSERT INTO agents (agent_id, cli, pid, capabilities_json, topics_json, last_seen_ms, lease_expires_ms, status, metadata_json)
|
|
129
|
+
VALUES (@agent_id, @cli, @pid, @capabilities_json, @topics_json, @last_seen_ms, @lease_expires_ms, @status, @metadata_json)
|
|
130
|
+
ON CONFLICT(agent_id) DO UPDATE SET
|
|
131
|
+
cli=excluded.cli,
|
|
132
|
+
pid=excluded.pid,
|
|
133
|
+
capabilities_json=excluded.capabilities_json,
|
|
134
|
+
topics_json=excluded.topics_json,
|
|
135
|
+
last_seen_ms=excluded.last_seen_ms,
|
|
136
|
+
lease_expires_ms=excluded.lease_expires_ms,
|
|
137
|
+
status=excluded.status,
|
|
138
|
+
metadata_json=excluded.metadata_json`),
|
|
139
|
+
getAgent: db.prepare('SELECT * FROM agents WHERE agent_id = ?'),
|
|
140
|
+
setAgentTopics: db.prepare('UPDATE agents SET topics_json=?, last_seen_ms=? WHERE agent_id=?'),
|
|
141
|
+
heartbeat: db.prepare("UPDATE agents SET last_seen_ms=?, lease_expires_ms=?, status='online' WHERE agent_id=?"),
|
|
142
|
+
setAgentStatus: db.prepare('UPDATE agents SET status=? WHERE agent_id=?'),
|
|
143
|
+
onlineAgents: db.prepare("SELECT * FROM agents WHERE status != 'offline'"),
|
|
144
|
+
allAgents: db.prepare('SELECT * FROM agents'),
|
|
145
|
+
agentsByTopic: db.prepare("SELECT a.* FROM agents a, json_each(a.topics_json) t WHERE t.value=? AND a.status != 'offline'"),
|
|
146
|
+
markStale: db.prepare("UPDATE agents SET status='stale' WHERE status='online' AND lease_expires_ms < ?"),
|
|
147
|
+
markOffline: db.prepare("UPDATE agents SET status='offline' WHERE status='stale' AND lease_expires_ms < ? - 300000"),
|
|
148
|
+
|
|
149
|
+
insertAuditMessage: db.prepare(`
|
|
150
|
+
INSERT INTO messages (id, type, from_agent, to_agent, topic, priority, ttl_ms, created_at_ms, expires_at_ms, correlation_id, trace_id, payload_json, status)
|
|
151
|
+
VALUES (@id, @type, @from_agent, @to_agent, @topic, @priority, @ttl_ms, @created_at_ms, @expires_at_ms, @correlation_id, @trace_id, @payload_json, @status)`),
|
|
152
|
+
getMsg: db.prepare('SELECT * FROM messages WHERE id=?'),
|
|
153
|
+
getResponse: db.prepare("SELECT * FROM messages WHERE correlation_id=? AND type='response' ORDER BY created_at_ms DESC LIMIT 1"),
|
|
154
|
+
getMsgsByTrace: db.prepare('SELECT * FROM messages WHERE trace_id=? ORDER BY created_at_ms'),
|
|
155
|
+
setMsgStatus: db.prepare('UPDATE messages SET status=? WHERE id=?'),
|
|
156
|
+
recentAgentMessages: db.prepare(`
|
|
157
|
+
SELECT * FROM messages
|
|
158
|
+
WHERE to_agent=?
|
|
159
|
+
ORDER BY created_at_ms DESC
|
|
160
|
+
LIMIT ?`),
|
|
161
|
+
recentAgentMessagesWithTopics: db.prepare(`
|
|
162
|
+
SELECT * FROM messages
|
|
163
|
+
WHERE to_agent=?
|
|
164
|
+
OR (
|
|
165
|
+
substr(to_agent, 1, 6)='topic:'
|
|
166
|
+
AND topic IN (SELECT value FROM json_each(?))
|
|
167
|
+
)
|
|
168
|
+
ORDER BY created_at_ms DESC
|
|
169
|
+
LIMIT ?`),
|
|
170
|
+
|
|
171
|
+
insertHR: db.prepare(`
|
|
172
|
+
INSERT INTO human_requests (request_id, requester_agent, kind, prompt, schema_json, state, deadline_ms, default_action, correlation_id, trace_id, response_json)
|
|
173
|
+
VALUES (@request_id, @requester_agent, @kind, @prompt, @schema_json, @state, @deadline_ms, @default_action, @correlation_id, @trace_id, @response_json)`),
|
|
174
|
+
getHR: db.prepare('SELECT * FROM human_requests WHERE request_id=?'),
|
|
175
|
+
updateHR: db.prepare('UPDATE human_requests SET state=?, response_json=? WHERE request_id=?'),
|
|
176
|
+
pendingHR: db.prepare("SELECT * FROM human_requests WHERE state='pending'"),
|
|
177
|
+
expireHR: db.prepare("UPDATE human_requests SET state='timed_out' WHERE state='pending' AND deadline_ms < ?"),
|
|
178
|
+
|
|
179
|
+
insertDL: db.prepare('INSERT OR REPLACE INTO dead_letters (message_id, reason, failed_at_ms, last_error) VALUES (?,?,?,?)'),
|
|
180
|
+
getDL: db.prepare('SELECT * FROM dead_letters ORDER BY failed_at_ms DESC LIMIT ?'),
|
|
181
|
+
|
|
182
|
+
insertAssign: db.prepare(`
|
|
183
|
+
INSERT INTO assign_jobs (
|
|
184
|
+
job_id, supervisor_agent, worker_agent, topic, task, payload_json,
|
|
185
|
+
status, attempt, retry_count, max_retries, priority, ttl_ms, timeout_ms, deadline_ms,
|
|
186
|
+
trace_id, correlation_id, last_message_id, result_json, error_json,
|
|
187
|
+
created_at_ms, updated_at_ms, started_at_ms, completed_at_ms, last_retry_at_ms
|
|
188
|
+
) VALUES (
|
|
189
|
+
@job_id, @supervisor_agent, @worker_agent, @topic, @task, @payload_json,
|
|
190
|
+
@status, @attempt, @retry_count, @max_retries, @priority, @ttl_ms, @timeout_ms, @deadline_ms,
|
|
191
|
+
@trace_id, @correlation_id, @last_message_id, @result_json, @error_json,
|
|
192
|
+
@created_at_ms, @updated_at_ms, @started_at_ms, @completed_at_ms, @last_retry_at_ms
|
|
193
|
+
)`),
|
|
194
|
+
getAssign: db.prepare('SELECT * FROM assign_jobs WHERE job_id = ?'),
|
|
195
|
+
updateAssign: db.prepare(`
|
|
196
|
+
UPDATE assign_jobs SET
|
|
197
|
+
supervisor_agent=@supervisor_agent,
|
|
198
|
+
worker_agent=@worker_agent,
|
|
199
|
+
topic=@topic,
|
|
200
|
+
task=@task,
|
|
201
|
+
payload_json=@payload_json,
|
|
202
|
+
status=@status,
|
|
203
|
+
attempt=@attempt,
|
|
204
|
+
retry_count=@retry_count,
|
|
205
|
+
max_retries=@max_retries,
|
|
206
|
+
priority=@priority,
|
|
207
|
+
ttl_ms=@ttl_ms,
|
|
208
|
+
timeout_ms=@timeout_ms,
|
|
209
|
+
deadline_ms=@deadline_ms,
|
|
210
|
+
trace_id=@trace_id,
|
|
211
|
+
correlation_id=@correlation_id,
|
|
212
|
+
last_message_id=@last_message_id,
|
|
213
|
+
result_json=@result_json,
|
|
214
|
+
error_json=@error_json,
|
|
215
|
+
updated_at_ms=@updated_at_ms,
|
|
216
|
+
started_at_ms=@started_at_ms,
|
|
217
|
+
completed_at_ms=@completed_at_ms,
|
|
218
|
+
last_retry_at_ms=@last_retry_at_ms
|
|
219
|
+
WHERE job_id=@job_id`),
|
|
220
|
+
|
|
221
|
+
findExpired: db.prepare("SELECT id FROM messages WHERE status='queued' AND expires_at_ms < ?"),
|
|
222
|
+
urgentDepth: db.prepare("SELECT COUNT(*) as cnt FROM messages WHERE status='queued' AND priority >= 7"),
|
|
223
|
+
normalDepth: db.prepare("SELECT COUNT(*) as cnt FROM messages WHERE status='queued' AND priority < 7"),
|
|
224
|
+
onlineCount: db.prepare("SELECT COUNT(*) as cnt FROM agents WHERE status='online'"),
|
|
225
|
+
msgCount: db.prepare('SELECT COUNT(*) as cnt FROM messages'),
|
|
226
|
+
dlqDepth: db.prepare('SELECT COUNT(*) as cnt FROM dead_letters'),
|
|
227
|
+
ackedRecent: db.prepare("SELECT COUNT(*) as cnt FROM messages WHERE status='acked' AND created_at_ms > ? - 300000"),
|
|
228
|
+
assignCountByStatus: db.prepare('SELECT COUNT(*) as cnt FROM assign_jobs WHERE status = ?'),
|
|
229
|
+
activeAssignCount: db.prepare("SELECT COUNT(*) as cnt FROM assign_jobs WHERE status IN ('queued','running')"),
|
|
230
|
+
|
|
231
|
+
// reflexion
|
|
232
|
+
insertReflexion: db.prepare(`
|
|
233
|
+
INSERT INTO reflexion_entries (id, type, error_pattern, error_message, context_json, solution, solution_code, adaptive_state_json, confidence, hit_count, success_count, last_hit_ms, created_at_ms, updated_at_ms)
|
|
234
|
+
VALUES (@id, @type, @error_pattern, @error_message, @context_json, @solution, @solution_code, @adaptive_state_json, @confidence, @hit_count, @success_count, @last_hit_ms, @created_at_ms, @updated_at_ms)`),
|
|
235
|
+
getReflexionById: db.prepare('SELECT * FROM reflexion_entries WHERE id = ?'),
|
|
236
|
+
findReflexionExact: db.prepare('SELECT * FROM reflexion_entries WHERE error_pattern = ? ORDER BY confidence DESC'),
|
|
237
|
+
findReflexionLike: db.prepare("SELECT * FROM reflexion_entries WHERE error_pattern LIKE ? ESCAPE '\\' ORDER BY confidence DESC LIMIT 10"),
|
|
238
|
+
updateReflexionHitSuccess: db.prepare('UPDATE reflexion_entries SET hit_count = hit_count + 1, success_count = success_count + 1, last_hit_ms = ?, updated_at_ms = ? WHERE id = ?'),
|
|
239
|
+
updateReflexionHitOnly: db.prepare('UPDATE reflexion_entries SET hit_count = hit_count + 1, last_hit_ms = ?, updated_at_ms = ? WHERE id = ?'),
|
|
240
|
+
updateReflexionConfidence: db.prepare('UPDATE reflexion_entries SET confidence = ?, updated_at_ms = ? WHERE id = ?'),
|
|
241
|
+
pruneReflexionEntries: db.prepare('DELETE FROM reflexion_entries WHERE updated_at_ms < ? AND confidence < ?'),
|
|
242
|
+
listReflexionEntries: db.prepare('SELECT * FROM reflexion_entries ORDER BY confidence DESC, updated_at_ms DESC'),
|
|
243
|
+
deleteReflexionEntry: db.prepare('DELETE FROM reflexion_entries WHERE id = ?'),
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const assignStatusListeners = new Set();
|
|
247
|
+
|
|
248
|
+
function buildAssignCallbackEvent(row) {
|
|
249
|
+
return {
|
|
250
|
+
job_id: row.job_id,
|
|
251
|
+
status: row.status,
|
|
252
|
+
result: row.result ?? row.error ?? null,
|
|
253
|
+
timestamp: new Date(row.updated_at_ms || Date.now()).toISOString(),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function notifyAssignStatusListeners(row) {
|
|
258
|
+
const event = buildAssignCallbackEvent(row);
|
|
259
|
+
for (const listener of Array.from(assignStatusListeners)) {
|
|
260
|
+
try { listener(event, row); } catch {}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function clampMaxMessages(value, fallback = 20) {
|
|
265
|
+
const num = Number(value);
|
|
266
|
+
if (!Number.isFinite(num)) return fallback;
|
|
267
|
+
return Math.max(1, Math.min(Math.trunc(num), 100));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function clampPriority(value, fallback = 5) {
|
|
271
|
+
const num = Number(value);
|
|
272
|
+
if (!Number.isFinite(num)) return fallback;
|
|
273
|
+
return Math.max(1, Math.min(Math.trunc(num), 9));
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function clampDuration(value, fallback = 600000, min = 1000, max = 86400000) {
|
|
277
|
+
const num = Number(value);
|
|
278
|
+
if (!Number.isFinite(num)) return fallback;
|
|
279
|
+
return Math.max(min, Math.min(Math.trunc(num), max));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const store = {
|
|
283
|
+
db,
|
|
284
|
+
uuidv7,
|
|
285
|
+
|
|
286
|
+
close() {
|
|
287
|
+
db.close();
|
|
288
|
+
},
|
|
289
|
+
|
|
290
|
+
registerAgent({ agent_id, cli, pid, capabilities = [], topics = [], heartbeat_ttl_ms = 30000, metadata = {} }) {
|
|
291
|
+
const now = Date.now();
|
|
292
|
+
const leaseExpires = now + heartbeat_ttl_ms;
|
|
293
|
+
S.upsertAgent.run({
|
|
294
|
+
agent_id,
|
|
295
|
+
cli,
|
|
296
|
+
pid: pid ?? null,
|
|
297
|
+
capabilities_json: JSON.stringify(capabilities),
|
|
298
|
+
topics_json: JSON.stringify(topics),
|
|
299
|
+
last_seen_ms: now,
|
|
300
|
+
lease_expires_ms: leaseExpires,
|
|
301
|
+
status: 'online',
|
|
302
|
+
metadata_json: JSON.stringify(metadata),
|
|
303
|
+
});
|
|
304
|
+
return { agent_id, lease_id: uuidv7(), lease_expires_ms: leaseExpires, server_time_ms: now };
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
getAgent(id) {
|
|
308
|
+
return parseAgentRow(S.getAgent.get(id));
|
|
309
|
+
},
|
|
310
|
+
|
|
311
|
+
refreshLease(agentId, ttlMs = 30000) {
|
|
312
|
+
const now = Date.now();
|
|
313
|
+
S.heartbeat.run(now, now + ttlMs, agentId);
|
|
314
|
+
return { agent_id: agentId, lease_expires_ms: now + ttlMs, server_time_ms: now };
|
|
315
|
+
},
|
|
316
|
+
|
|
317
|
+
updateAgentTopics(agentId, topics = []) {
|
|
318
|
+
const now = Date.now();
|
|
319
|
+
return S.setAgentTopics.run(JSON.stringify(topics), now, agentId).changes > 0;
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
listOnlineAgents() {
|
|
323
|
+
return S.onlineAgents.all().map(parseAgentRow);
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
listAllAgents() {
|
|
327
|
+
return S.allAgents.all().map(parseAgentRow);
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
getAgentsByTopic(topic) {
|
|
331
|
+
return S.agentsByTopic.all(topic).map(parseAgentRow);
|
|
332
|
+
},
|
|
333
|
+
|
|
334
|
+
sweepStaleAgents() {
|
|
335
|
+
const now = Date.now();
|
|
336
|
+
return {
|
|
337
|
+
stale: S.markStale.run(now).changes,
|
|
338
|
+
offline: S.markOffline.run(now).changes,
|
|
339
|
+
};
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
updateAgentStatus(agentId, status) {
|
|
343
|
+
return S.setAgentStatus.run(status, agentId).changes > 0;
|
|
344
|
+
},
|
|
345
|
+
|
|
346
|
+
auditLog({ type, from, to, topic, priority = 5, ttl_ms = 300000, payload = {}, trace_id, correlation_id, status = 'queued' }) {
|
|
347
|
+
const now = Date.now();
|
|
348
|
+
const row = {
|
|
349
|
+
id: uuidv7(),
|
|
350
|
+
type,
|
|
351
|
+
from_agent: from,
|
|
352
|
+
to_agent: to,
|
|
353
|
+
topic,
|
|
354
|
+
priority,
|
|
355
|
+
ttl_ms,
|
|
356
|
+
created_at_ms: now,
|
|
357
|
+
expires_at_ms: now + ttl_ms,
|
|
358
|
+
correlation_id: correlation_id || uuidv7(),
|
|
359
|
+
trace_id: trace_id || uuidv7(),
|
|
360
|
+
payload_json: JSON.stringify(payload),
|
|
361
|
+
status,
|
|
362
|
+
};
|
|
363
|
+
S.insertAuditMessage.run(row);
|
|
364
|
+
return { ...row, payload };
|
|
365
|
+
},
|
|
366
|
+
|
|
367
|
+
// 하위 호환: 기존 enqueueMessage 호출은 auditLog로 위임한다.
|
|
368
|
+
enqueueMessage(args) {
|
|
369
|
+
return store.auditLog(args);
|
|
370
|
+
},
|
|
371
|
+
|
|
372
|
+
getMessage(id) {
|
|
373
|
+
return parseMessageRow(S.getMsg.get(id));
|
|
374
|
+
},
|
|
375
|
+
|
|
376
|
+
getResponseByCorrelation(cid) {
|
|
377
|
+
return parseMessageRow(S.getResponse.get(cid));
|
|
378
|
+
},
|
|
379
|
+
|
|
380
|
+
getMessagesByTrace(tid) {
|
|
381
|
+
return S.getMsgsByTrace.all(tid).map(parseMessageRow);
|
|
382
|
+
},
|
|
383
|
+
|
|
384
|
+
updateMessageStatus(id, status) {
|
|
385
|
+
return S.setMsgStatus.run(status, id).changes > 0;
|
|
386
|
+
},
|
|
387
|
+
|
|
388
|
+
getAuditMessagesForAgent(agentId, { max_messages = 20, include_topics = null } = {}) {
|
|
389
|
+
const limit = clampMaxMessages(max_messages);
|
|
390
|
+
const topics = Array.isArray(include_topics) && include_topics.length
|
|
391
|
+
? include_topics
|
|
392
|
+
: (store.getAgent(agentId)?.topics || []);
|
|
393
|
+
|
|
394
|
+
const rows = topics.length
|
|
395
|
+
? S.recentAgentMessagesWithTopics.all(agentId, JSON.stringify(topics), limit)
|
|
396
|
+
: S.recentAgentMessages.all(agentId, limit);
|
|
397
|
+
|
|
398
|
+
return rows.map(parseMessageRow);
|
|
399
|
+
},
|
|
400
|
+
|
|
401
|
+
// 하위 호환: 실시간 수신함 대신 감사 로그 재생 결과를 반환한다.
|
|
402
|
+
deliverToAgent(messageId, agentId) {
|
|
403
|
+
return !!store.getMessage(messageId) && !!agentId;
|
|
404
|
+
},
|
|
405
|
+
|
|
406
|
+
deliverToTopic(messageId, topic) {
|
|
407
|
+
void messageId;
|
|
408
|
+
return store.getAgentsByTopic(topic).length;
|
|
409
|
+
},
|
|
410
|
+
|
|
411
|
+
pollForAgent(agentId, { max_messages = 20, include_topics = null } = {}) {
|
|
412
|
+
return store.getAuditMessagesForAgent(agentId, {
|
|
413
|
+
max_messages,
|
|
414
|
+
include_topics,
|
|
415
|
+
});
|
|
416
|
+
},
|
|
417
|
+
|
|
418
|
+
ackMessages() {
|
|
419
|
+
return 0;
|
|
420
|
+
},
|
|
421
|
+
|
|
422
|
+
insertHumanRequest({ requester_agent, kind, prompt, requested_schema = {}, deadline_ms, default_action, correlation_id, trace_id }) {
|
|
423
|
+
const requestId = uuidv7();
|
|
424
|
+
const now = Date.now();
|
|
425
|
+
const deadlineAt = now + deadline_ms;
|
|
426
|
+
S.insertHR.run({
|
|
427
|
+
request_id: requestId,
|
|
428
|
+
requester_agent,
|
|
429
|
+
kind,
|
|
430
|
+
prompt,
|
|
431
|
+
schema_json: JSON.stringify(requested_schema),
|
|
432
|
+
state: 'pending',
|
|
433
|
+
deadline_ms: deadlineAt,
|
|
434
|
+
default_action,
|
|
435
|
+
correlation_id: correlation_id || uuidv7(),
|
|
436
|
+
trace_id: trace_id || uuidv7(),
|
|
437
|
+
response_json: null,
|
|
438
|
+
});
|
|
439
|
+
return { request_id: requestId, state: 'pending', deadline_ms: deadlineAt };
|
|
440
|
+
},
|
|
441
|
+
|
|
442
|
+
getHumanRequest(id) {
|
|
443
|
+
return parseHumanRequestRow(S.getHR.get(id));
|
|
444
|
+
},
|
|
445
|
+
|
|
446
|
+
updateHumanRequest(id, state, resp = null) {
|
|
447
|
+
return S.updateHR.run(state, resp ? JSON.stringify(resp) : null, id).changes > 0;
|
|
448
|
+
},
|
|
449
|
+
|
|
450
|
+
getPendingHumanRequests() {
|
|
451
|
+
return S.pendingHR.all().map(parseHumanRequestRow);
|
|
452
|
+
},
|
|
453
|
+
|
|
454
|
+
expireHumanRequests() {
|
|
455
|
+
return S.expireHR.run(Date.now()).changes;
|
|
456
|
+
},
|
|
457
|
+
|
|
458
|
+
moveToDeadLetter(messageId, reason, lastError = null) {
|
|
459
|
+
db.transaction(() => {
|
|
460
|
+
S.setMsgStatus.run('dead_letter', messageId);
|
|
461
|
+
S.insertDL.run(messageId, reason, Date.now(), lastError);
|
|
462
|
+
})();
|
|
463
|
+
return true;
|
|
464
|
+
},
|
|
465
|
+
|
|
466
|
+
getDeadLetters(limit = 50) {
|
|
467
|
+
return S.getDL.all(limit);
|
|
468
|
+
},
|
|
469
|
+
|
|
470
|
+
createAssign({
|
|
471
|
+
job_id,
|
|
472
|
+
supervisor_agent,
|
|
473
|
+
worker_agent,
|
|
474
|
+
topic = 'assign.job',
|
|
475
|
+
task = '',
|
|
476
|
+
payload = {},
|
|
477
|
+
status = 'queued',
|
|
478
|
+
attempt = 1,
|
|
479
|
+
retry_count = 0,
|
|
480
|
+
max_retries = 0,
|
|
481
|
+
priority = 5,
|
|
482
|
+
ttl_ms = 600000,
|
|
483
|
+
timeout_ms = 600000,
|
|
484
|
+
deadline_ms,
|
|
485
|
+
trace_id,
|
|
486
|
+
correlation_id,
|
|
487
|
+
last_message_id = null,
|
|
488
|
+
result = null,
|
|
489
|
+
error = null,
|
|
490
|
+
}) {
|
|
491
|
+
const now = Date.now();
|
|
492
|
+
const normalizedTimeout = clampDuration(timeout_ms, 600000);
|
|
493
|
+
const row = {
|
|
494
|
+
job_id: job_id || uuidv7(),
|
|
495
|
+
supervisor_agent,
|
|
496
|
+
worker_agent,
|
|
497
|
+
topic: String(topic || 'assign.job'),
|
|
498
|
+
task: String(task || ''),
|
|
499
|
+
payload_json: JSON.stringify(payload || {}),
|
|
500
|
+
status,
|
|
501
|
+
attempt: Math.max(1, Number(attempt) || 1),
|
|
502
|
+
retry_count: Math.max(0, Number(retry_count) || 0),
|
|
503
|
+
max_retries: Math.max(0, Number(max_retries) || 0),
|
|
504
|
+
priority: clampPriority(priority, 5),
|
|
505
|
+
ttl_ms: clampDuration(ttl_ms, normalizedTimeout),
|
|
506
|
+
timeout_ms: normalizedTimeout,
|
|
507
|
+
deadline_ms: Number.isFinite(Number(deadline_ms))
|
|
508
|
+
? Math.trunc(Number(deadline_ms))
|
|
509
|
+
: now + normalizedTimeout,
|
|
510
|
+
trace_id: trace_id || uuidv7(),
|
|
511
|
+
correlation_id: correlation_id || uuidv7(),
|
|
512
|
+
last_message_id,
|
|
513
|
+
result_json: result == null ? null : JSON.stringify(result),
|
|
514
|
+
error_json: error == null ? null : JSON.stringify(error),
|
|
515
|
+
created_at_ms: now,
|
|
516
|
+
updated_at_ms: now,
|
|
517
|
+
started_at_ms: status === 'running' ? now : null,
|
|
518
|
+
completed_at_ms: ['succeeded', 'failed', 'timed_out'].includes(status) ? now : null,
|
|
519
|
+
last_retry_at_ms: retry_count > 0 ? now : null,
|
|
520
|
+
};
|
|
521
|
+
S.insertAssign.run(row);
|
|
522
|
+
const inserted = store.getAssign(row.job_id);
|
|
523
|
+
notifyAssignStatusListeners(inserted);
|
|
524
|
+
return inserted;
|
|
525
|
+
},
|
|
526
|
+
|
|
527
|
+
getAssign(jobId) {
|
|
528
|
+
return parseAssignRow(S.getAssign.get(jobId));
|
|
529
|
+
},
|
|
530
|
+
|
|
531
|
+
updateAssignStatus(jobId, status, patch = {}) {
|
|
532
|
+
const current = store.getAssign(jobId);
|
|
533
|
+
if (!current) return null;
|
|
534
|
+
|
|
535
|
+
const now = Date.now();
|
|
536
|
+
const nextStatus = status || current.status;
|
|
537
|
+
const isTerminal = ['succeeded', 'failed', 'timed_out'].includes(nextStatus);
|
|
538
|
+
const nextTimeout = clampDuration(patch.timeout_ms ?? current.timeout_ms, current.timeout_ms);
|
|
539
|
+
const nextRow = {
|
|
540
|
+
job_id: current.job_id,
|
|
541
|
+
supervisor_agent: patch.supervisor_agent ?? current.supervisor_agent,
|
|
542
|
+
worker_agent: patch.worker_agent ?? current.worker_agent,
|
|
543
|
+
topic: patch.topic ?? current.topic,
|
|
544
|
+
task: patch.task ?? current.task,
|
|
545
|
+
payload_json: JSON.stringify(patch.payload ?? current.payload ?? {}),
|
|
546
|
+
status: nextStatus,
|
|
547
|
+
attempt: Math.max(1, Number(patch.attempt ?? current.attempt) || current.attempt || 1),
|
|
548
|
+
retry_count: Math.max(0, Number(patch.retry_count ?? current.retry_count) || 0),
|
|
549
|
+
max_retries: Math.max(0, Number(patch.max_retries ?? current.max_retries) || 0),
|
|
550
|
+
priority: clampPriority(patch.priority ?? current.priority, current.priority || 5),
|
|
551
|
+
ttl_ms: clampDuration(patch.ttl_ms ?? current.ttl_ms, current.ttl_ms || nextTimeout),
|
|
552
|
+
timeout_ms: nextTimeout,
|
|
553
|
+
deadline_ms: (() => {
|
|
554
|
+
if (Object.prototype.hasOwnProperty.call(patch, 'deadline_ms')) {
|
|
555
|
+
return patch.deadline_ms == null ? null : Math.trunc(Number(patch.deadline_ms));
|
|
556
|
+
}
|
|
557
|
+
if (isTerminal) return null;
|
|
558
|
+
if (nextStatus === 'running' && !current.deadline_ms) return now + nextTimeout;
|
|
559
|
+
return current.deadline_ms;
|
|
560
|
+
})(),
|
|
561
|
+
trace_id: patch.trace_id ?? current.trace_id,
|
|
562
|
+
correlation_id: patch.correlation_id ?? current.correlation_id,
|
|
563
|
+
last_message_id: Object.prototype.hasOwnProperty.call(patch, 'last_message_id')
|
|
564
|
+
? patch.last_message_id
|
|
565
|
+
: current.last_message_id,
|
|
566
|
+
result_json: Object.prototype.hasOwnProperty.call(patch, 'result')
|
|
567
|
+
? (patch.result == null ? null : JSON.stringify(patch.result))
|
|
568
|
+
: (current.result == null ? null : JSON.stringify(current.result)),
|
|
569
|
+
error_json: Object.prototype.hasOwnProperty.call(patch, 'error')
|
|
570
|
+
? (patch.error == null ? null : JSON.stringify(patch.error))
|
|
571
|
+
: (current.error == null ? null : JSON.stringify(current.error)),
|
|
572
|
+
updated_at_ms: now,
|
|
573
|
+
started_at_ms: Object.prototype.hasOwnProperty.call(patch, 'started_at_ms')
|
|
574
|
+
? patch.started_at_ms
|
|
575
|
+
: (nextStatus === 'running' ? (current.started_at_ms || now) : current.started_at_ms),
|
|
576
|
+
completed_at_ms: Object.prototype.hasOwnProperty.call(patch, 'completed_at_ms')
|
|
577
|
+
? patch.completed_at_ms
|
|
578
|
+
: (isTerminal ? (current.completed_at_ms || now) : current.completed_at_ms),
|
|
579
|
+
last_retry_at_ms: Object.prototype.hasOwnProperty.call(patch, 'last_retry_at_ms')
|
|
580
|
+
? patch.last_retry_at_ms
|
|
581
|
+
: current.last_retry_at_ms,
|
|
582
|
+
};
|
|
583
|
+
S.updateAssign.run(nextRow);
|
|
584
|
+
const updated = store.getAssign(jobId);
|
|
585
|
+
if (updated && current.status !== updated.status) {
|
|
586
|
+
notifyAssignStatusListeners(updated);
|
|
587
|
+
}
|
|
588
|
+
return updated;
|
|
589
|
+
},
|
|
590
|
+
|
|
591
|
+
listAssigns({
|
|
592
|
+
supervisor_agent,
|
|
593
|
+
worker_agent,
|
|
594
|
+
status,
|
|
595
|
+
statuses,
|
|
596
|
+
trace_id,
|
|
597
|
+
correlation_id,
|
|
598
|
+
active_before_ms,
|
|
599
|
+
limit = 50,
|
|
600
|
+
} = {}) {
|
|
601
|
+
const clauses = [];
|
|
602
|
+
const values = [];
|
|
603
|
+
|
|
604
|
+
if (supervisor_agent) {
|
|
605
|
+
clauses.push('supervisor_agent = ?');
|
|
606
|
+
values.push(supervisor_agent);
|
|
607
|
+
}
|
|
608
|
+
if (worker_agent) {
|
|
609
|
+
clauses.push('worker_agent = ?');
|
|
610
|
+
values.push(worker_agent);
|
|
611
|
+
}
|
|
612
|
+
if (trace_id) {
|
|
613
|
+
clauses.push('trace_id = ?');
|
|
614
|
+
values.push(trace_id);
|
|
615
|
+
}
|
|
616
|
+
if (correlation_id) {
|
|
617
|
+
clauses.push('correlation_id = ?');
|
|
618
|
+
values.push(correlation_id);
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
const statusList = Array.isArray(statuses) && statuses.length
|
|
622
|
+
? statuses
|
|
623
|
+
: (status ? [status] : []);
|
|
624
|
+
if (statusList.length) {
|
|
625
|
+
clauses.push(`status IN (${statusList.map(() => '?').join(',')})`);
|
|
626
|
+
values.push(...statusList);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
if (Number.isFinite(Number(active_before_ms))) {
|
|
630
|
+
clauses.push('deadline_ms IS NOT NULL AND deadline_ms <= ?');
|
|
631
|
+
values.push(Math.trunc(Number(active_before_ms)));
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// WHERE 절은 호출마다 달라지므로 prepared statement를 미리 캐시할 수 없다.
|
|
635
|
+
// db.prepare()는 호출당 한 번 실행되며, better-sqlite3 내부에서 SQLite 구문 파싱을
|
|
636
|
+
// 수행한다. 필터 조합이 2^6 = 64가지이므로 정적 캐시 대신 동적 생성을 선택했다.
|
|
637
|
+
// 이 함수는 hot path(heartbeat/poll)가 아닌 관리/조회 경로에서만 호출되므로 허용한다.
|
|
638
|
+
const sql = `
|
|
639
|
+
SELECT * FROM assign_jobs
|
|
640
|
+
${clauses.length ? `WHERE ${clauses.join(' AND ')}` : ''}
|
|
641
|
+
ORDER BY updated_at_ms DESC
|
|
642
|
+
LIMIT ?`;
|
|
643
|
+
values.push(clampMaxMessages(limit, 50));
|
|
644
|
+
return db.prepare(sql).all(...values).map(parseAssignRow);
|
|
645
|
+
},
|
|
646
|
+
|
|
647
|
+
retryAssign(jobId, patch = {}) {
|
|
648
|
+
const current = store.getAssign(jobId);
|
|
649
|
+
if (!current) return null;
|
|
650
|
+
|
|
651
|
+
const nextRetryCount = Math.max(0, Number(patch.retry_count ?? current.retry_count + 1) || 0);
|
|
652
|
+
const nextAttempt = Math.max(current.attempt + 1, Number(patch.attempt ?? current.attempt + 1) || 1);
|
|
653
|
+
const nextTimeout = clampDuration(patch.timeout_ms ?? current.timeout_ms, current.timeout_ms);
|
|
654
|
+
return store.updateAssignStatus(jobId, 'queued', {
|
|
655
|
+
retry_count: nextRetryCount,
|
|
656
|
+
attempt: nextAttempt,
|
|
657
|
+
timeout_ms: nextTimeout,
|
|
658
|
+
ttl_ms: patch.ttl_ms ?? current.ttl_ms,
|
|
659
|
+
deadline_ms: Date.now() + nextTimeout,
|
|
660
|
+
completed_at_ms: null,
|
|
661
|
+
started_at_ms: null,
|
|
662
|
+
last_retry_at_ms: Date.now(),
|
|
663
|
+
result: patch.result ?? null,
|
|
664
|
+
error: Object.prototype.hasOwnProperty.call(patch, 'error') ? patch.error : current.error,
|
|
665
|
+
last_message_id: null,
|
|
666
|
+
});
|
|
667
|
+
},
|
|
668
|
+
|
|
669
|
+
sweepExpired() {
|
|
670
|
+
const now = Date.now();
|
|
671
|
+
return db.transaction(() => {
|
|
672
|
+
const expired = S.findExpired.all(now);
|
|
673
|
+
for (const { id } of expired) {
|
|
674
|
+
S.setMsgStatus.run('dead_letter', id);
|
|
675
|
+
S.insertDL.run(id, 'ttl_expired', now, null);
|
|
676
|
+
}
|
|
677
|
+
const humanRequests = S.expireHR.run(now).changes;
|
|
678
|
+
return { messages: expired.length, human_requests: humanRequests };
|
|
679
|
+
})();
|
|
680
|
+
},
|
|
681
|
+
|
|
682
|
+
getQueueDepths() {
|
|
683
|
+
return {
|
|
684
|
+
urgent: S.urgentDepth.get().cnt,
|
|
685
|
+
normal: S.normalDepth.get().cnt,
|
|
686
|
+
dlq: S.dlqDepth.get().cnt,
|
|
687
|
+
};
|
|
688
|
+
},
|
|
689
|
+
|
|
690
|
+
onAssignStatusChange(listener) {
|
|
691
|
+
if (typeof listener !== 'function') {
|
|
692
|
+
return () => {};
|
|
693
|
+
}
|
|
694
|
+
assignStatusListeners.add(listener);
|
|
695
|
+
return () => {
|
|
696
|
+
assignStatusListeners.delete(listener);
|
|
697
|
+
};
|
|
698
|
+
},
|
|
699
|
+
|
|
700
|
+
getDeliveryStats() {
|
|
701
|
+
return {
|
|
702
|
+
total_deliveries: S.ackedRecent.get(Date.now()).cnt,
|
|
703
|
+
avg_delivery_ms: 0,
|
|
704
|
+
};
|
|
705
|
+
},
|
|
706
|
+
|
|
707
|
+
getHubStats() {
|
|
708
|
+
return {
|
|
709
|
+
online_agents: S.onlineCount.get().cnt,
|
|
710
|
+
total_messages: S.msgCount.get().cnt,
|
|
711
|
+
active_assign_jobs: S.activeAssignCount.get().cnt,
|
|
712
|
+
...store.getQueueDepths(),
|
|
713
|
+
};
|
|
714
|
+
},
|
|
715
|
+
|
|
716
|
+
getAuditStats() {
|
|
717
|
+
return {
|
|
718
|
+
online_agents: S.onlineCount.get().cnt,
|
|
719
|
+
total_messages: S.msgCount.get().cnt,
|
|
720
|
+
dlq: S.dlqDepth.get().cnt,
|
|
721
|
+
assign_queued: S.assignCountByStatus.get('queued').cnt,
|
|
722
|
+
assign_running: S.assignCountByStatus.get('running').cnt,
|
|
723
|
+
assign_failed: S.assignCountByStatus.get('failed').cnt,
|
|
724
|
+
assign_timed_out: S.assignCountByStatus.get('timed_out').cnt,
|
|
725
|
+
};
|
|
726
|
+
},
|
|
727
|
+
|
|
728
|
+
// --- Reflexion CRUD ---
|
|
729
|
+
|
|
730
|
+
addReflexion({
|
|
731
|
+
type = 'reflexion',
|
|
732
|
+
error_pattern,
|
|
733
|
+
error_message,
|
|
734
|
+
context = {},
|
|
735
|
+
solution,
|
|
736
|
+
solution_code = null,
|
|
737
|
+
adaptive_state = {},
|
|
738
|
+
confidence = 0.5,
|
|
739
|
+
hit_count = 1,
|
|
740
|
+
success_count = 0,
|
|
741
|
+
last_hit_ms,
|
|
742
|
+
created_at_ms,
|
|
743
|
+
updated_at_ms,
|
|
744
|
+
}) {
|
|
745
|
+
const now = Date.now();
|
|
746
|
+
const id = uuidv7();
|
|
747
|
+
S.insertReflexion.run({
|
|
748
|
+
id,
|
|
749
|
+
type,
|
|
750
|
+
error_pattern,
|
|
751
|
+
error_message,
|
|
752
|
+
context_json: JSON.stringify(context),
|
|
753
|
+
solution,
|
|
754
|
+
solution_code,
|
|
755
|
+
adaptive_state_json: JSON.stringify(adaptive_state),
|
|
756
|
+
confidence,
|
|
757
|
+
hit_count,
|
|
758
|
+
success_count,
|
|
759
|
+
last_hit_ms: last_hit_ms ?? now,
|
|
760
|
+
created_at_ms: created_at_ms ?? now,
|
|
761
|
+
updated_at_ms: updated_at_ms ?? now,
|
|
762
|
+
});
|
|
763
|
+
return store.getReflexion(id);
|
|
764
|
+
},
|
|
765
|
+
|
|
766
|
+
getReflexion(id) {
|
|
767
|
+
return parseReflexionRow(S.getReflexionById.get(id));
|
|
768
|
+
},
|
|
769
|
+
|
|
770
|
+
findReflexion(errorPattern, context = {}) {
|
|
771
|
+
const ctxKeys = Object.keys(context).filter(k => context[k] != null);
|
|
772
|
+
const ctxWhere = ctxKeys.map(k => ` AND json_extract(context_json, '$.${k}') = ?`).join('');
|
|
773
|
+
const ctxVals = ctxKeys.map(k => context[k]);
|
|
774
|
+
|
|
775
|
+
if (ctxKeys.length === 0) {
|
|
776
|
+
let rows = S.findReflexionExact.all(errorPattern);
|
|
777
|
+
if (rows.length) return rows.map(parseReflexionRow);
|
|
778
|
+
const escaped = errorPattern.replace(/[%_\\]/g, '\\$&');
|
|
779
|
+
rows = S.findReflexionLike.all(`%${escaped.slice(0, 100)}%`);
|
|
780
|
+
return rows.map(parseReflexionRow);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
const exactSql = `SELECT * FROM reflexion_entries WHERE error_pattern = ?${ctxWhere} ORDER BY confidence DESC`;
|
|
784
|
+
let rows = db.prepare(exactSql).all(errorPattern, ...ctxVals);
|
|
785
|
+
if (rows.length) return rows.map(parseReflexionRow);
|
|
786
|
+
|
|
787
|
+
const escaped = errorPattern.replace(/[%_\\]/g, '\\$&');
|
|
788
|
+
const likeSql = `SELECT * FROM reflexion_entries WHERE error_pattern LIKE ? ESCAPE '\\'${ctxWhere} ORDER BY confidence DESC LIMIT 10`;
|
|
789
|
+
rows = db.prepare(likeSql).all(`%${escaped.slice(0, 100)}%`, ...ctxVals);
|
|
790
|
+
return rows.map(parseReflexionRow);
|
|
791
|
+
},
|
|
792
|
+
|
|
793
|
+
updateReflexionHit(id, success = false) {
|
|
794
|
+
const now = Date.now();
|
|
795
|
+
if (success) {
|
|
796
|
+
S.updateReflexionHitSuccess.run(now, now, id);
|
|
797
|
+
} else {
|
|
798
|
+
S.updateReflexionHitOnly.run(now, now, id);
|
|
799
|
+
}
|
|
800
|
+
const entry = store.getReflexion(id);
|
|
801
|
+
if (entry && entry.hit_count > 0) {
|
|
802
|
+
const conf = recalcConfidence(entry);
|
|
803
|
+
S.updateReflexionConfidence.run(Math.max(0, Math.min(1, conf)), now, id);
|
|
804
|
+
}
|
|
805
|
+
return store.getReflexion(id);
|
|
806
|
+
},
|
|
807
|
+
|
|
808
|
+
listReflexion(filters = {}) {
|
|
809
|
+
const { type, minConfidence = Number.NEGATIVE_INFINITY, projectSlug } = filters;
|
|
810
|
+
return S.listReflexionEntries.all()
|
|
811
|
+
.map(parseReflexionRow)
|
|
812
|
+
.filter((entry) => !type || entry.type === type)
|
|
813
|
+
.filter((entry) => entry.confidence >= minConfidence)
|
|
814
|
+
.filter((entry) => !projectSlug || entry.adaptive_state?.project_slug === projectSlug);
|
|
815
|
+
},
|
|
816
|
+
|
|
817
|
+
patchReflexion(id, patch = {}) {
|
|
818
|
+
const current = store.getReflexion(id);
|
|
819
|
+
if (!current) return null;
|
|
820
|
+
const next = {
|
|
821
|
+
...current,
|
|
822
|
+
...patch,
|
|
823
|
+
context: patch.context ?? current.context,
|
|
824
|
+
adaptive_state: patch.adaptive_state ?? current.adaptive_state,
|
|
825
|
+
updated_at_ms: patch.updated_at_ms ?? Date.now(),
|
|
826
|
+
};
|
|
827
|
+
const sets = [
|
|
828
|
+
['type', next.type],
|
|
829
|
+
['error_pattern', next.error_pattern],
|
|
830
|
+
['error_message', next.error_message],
|
|
831
|
+
['context_json', JSON.stringify(next.context ?? {})],
|
|
832
|
+
['solution', next.solution],
|
|
833
|
+
['solution_code', next.solution_code ?? null],
|
|
834
|
+
['adaptive_state_json', JSON.stringify(next.adaptive_state ?? {})],
|
|
835
|
+
['confidence', next.confidence],
|
|
836
|
+
['hit_count', next.hit_count],
|
|
837
|
+
['success_count', next.success_count],
|
|
838
|
+
['last_hit_ms', next.last_hit_ms],
|
|
839
|
+
['updated_at_ms', next.updated_at_ms],
|
|
840
|
+
];
|
|
841
|
+
const sql = `UPDATE reflexion_entries SET ${sets.map(([key]) => `${key} = ?`).join(', ')} WHERE id = ?`;
|
|
842
|
+
db.prepare(sql).run(...sets.map(([, value]) => value), id);
|
|
843
|
+
return store.getReflexion(id);
|
|
844
|
+
},
|
|
845
|
+
|
|
846
|
+
deleteReflexion(id) {
|
|
847
|
+
return S.deleteReflexionEntry.run(id).changes > 0;
|
|
848
|
+
},
|
|
849
|
+
|
|
850
|
+
pruneReflexion(maxAge_ms = 30 * 24 * 3600 * 1000, minConfidence = 0.2) {
|
|
851
|
+
const cutoff = Date.now() - maxAge_ms;
|
|
852
|
+
return S.pruneReflexionEntries.run(cutoff, minConfidence).changes;
|
|
853
|
+
},
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
return store;
|
|
857
|
+
}
|