openhermes 4.11.2 → 4.13.0
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/CONTEXT.md +1 -1
- package/ETHOS.md +1 -1
- package/README.md +12 -18
- package/bootstrap.ts +73 -148
- package/docs/HOW-IT-WORKS.md +162 -0
- package/docs/adr/ADR-0001-rebuild-vs-increment.md +30 -0
- package/docs/adr/ADR-0002-routing-graph-vs-linear-chain.md +36 -0
- package/docs/adr/ADR-0003-per-directory-plan-storage.md +34 -0
- package/docs/adr/ADR-0004-composer-fragment-architecture.md +42 -0
- package/docs/adr/ADR-0005-hook-system-design.md +42 -0
- package/docs/adr/README.md +9 -0
- package/harness/codex/AUTOPILOT.md +30 -23
- package/harness/codex/CHARTER.md +3 -3
- package/harness/lib/composer/compose.test.ts +11 -0
- package/harness/lib/composer/fragments/02-delegation.md +2 -1
- package/harness/lib/composer/fragments/04-task-flow.md +42 -2
- package/harness/lib/composer/fragments/08-routing.md +1 -1
- package/harness/lib/composer/fragments/09-guardrails.md +17 -4
- package/harness/lib/composer/index.ts +1 -1
- package/harness/lib/guards/guard-config.ts +72 -0
- package/harness/lib/hooks/builtins/confidence-gate-hook.ts +2 -4
- package/harness/lib/hooks/builtins/delegation-depth-hook.ts +23 -4
- package/harness/lib/hooks/builtins/dynamic-route-hook.ts +99 -0
- package/harness/lib/hooks/builtins/next-route-hook.ts +24 -0
- package/harness/lib/hooks/builtins/plan-check-hook.ts +2 -2
- package/harness/lib/hooks/builtins/route-tracking-hook.ts +79 -25
- package/harness/lib/hooks/hooks.test.ts +117 -205
- package/harness/lib/hooks/index.ts +38 -30
- package/harness/lib/hooks/registry.ts +309 -416
- package/harness/lib/hooks/types.ts +116 -71
- package/harness/lib/plans/plan-location.ts +134 -0
- package/harness/lib/routing/index.ts +21 -0
- package/harness/lib/routing/route-guidance.ts +147 -0
- package/harness/lib/routing/route-resolver.ts +58 -0
- package/harness/lib/routing/routing.test.ts +195 -0
- package/harness/lib/routing/skill-frontmatter.ts +125 -0
- package/harness/lib/routing/types.ts +52 -0
- package/harness/skills/oh-ascii/SKILL.md +1 -1
- package/harness/skills/oh-fusion/DEEP.md +56 -33
- package/harness/skills/oh-fusion/SKILL.md +30 -16
- package/harness/skills/oh-init/DEEP.md +2 -2
- package/harness/skills/oh-manifest/SKILL.md +1 -0
- package/harness/skills/oh-plan-review/DEEP.md +1 -1
- package/harness/skills/oh-planner/DEEP.md +3 -3
- package/harness/skills/oh-review/DEEP.md +2 -0
- package/harness/skills/oh-review/SKILL.md +1 -0
- package/package.json +56 -55
- package/harness/lib/background/background.test.ts +0 -197
- package/harness/lib/background/index.ts +0 -7
- package/harness/lib/background/interfaces.ts +0 -31
- package/harness/lib/background/manager.ts +0 -320
- package/harness/lib/hooks/builtins/error-recovery-hook.ts +0 -107
- package/harness/lib/hooks/builtins/memory-sync-hook.ts +0 -73
- package/harness/lib/hooks/builtins/sanity-check-hook.ts +0 -52
- package/harness/lib/memory/index.ts +0 -18
- package/harness/lib/memory/interfaces.ts +0 -53
- package/harness/lib/memory/memory-manager.ts +0 -205
- package/harness/lib/memory/memory.test.ts +0 -491
- package/harness/lib/memory/plan-store.ts +0 -366
- package/harness/lib/recovery/handler.ts +0 -243
- package/harness/lib/recovery/index.ts +0 -14
- package/harness/lib/recovery/interfaces.ts +0 -48
- package/harness/lib/recovery/patterns.ts +0 -149
- package/harness/lib/recovery/recovery.test.ts +0 -312
- package/harness/lib/sanity/anomaly-tracker.ts +0 -127
- package/harness/lib/sanity/checker.ts +0 -178
- package/harness/lib/sanity/index.ts +0 -13
- package/harness/lib/sanity/interfaces.ts +0 -24
- package/harness/lib/sanity/sanity.test.ts +0 -472
- package/harness/lib/sync/file-watcher.ts +0 -174
- package/harness/lib/sync/index.ts +0 -11
- package/harness/lib/sync/interfaces.ts +0 -27
- package/harness/lib/sync/plan-sync.ts +0 -536
- package/harness/lib/sync/sync.test.ts +0 -832
|
@@ -1,71 +1,116 @@
|
|
|
1
|
-
// ---------------------------------------------------------------------------
|
|
2
|
-
// Hook System — type definitions
|
|
3
|
-
// ---------------------------------------------------------------------------
|
|
4
|
-
|
|
5
|
-
export enum HookPhase {
|
|
6
|
-
EARLY = 0,
|
|
7
|
-
NORMAL = 1,
|
|
8
|
-
LATE = 2,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface
|
|
12
|
-
sessionId: string;
|
|
13
|
-
agent: string;
|
|
14
|
-
directory: string;
|
|
15
|
-
sessions: Map<string, unknown>;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
|
71
|
-
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// Hook System — type definitions
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
export enum HookPhase {
|
|
6
|
+
EARLY = 0,
|
|
7
|
+
NORMAL = 1,
|
|
8
|
+
LATE = 2,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface HookContextBase {
|
|
12
|
+
sessionId: string;
|
|
13
|
+
agent: string;
|
|
14
|
+
directory: string;
|
|
15
|
+
sessions: Map<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface HookContextExtras {
|
|
19
|
+
_planCheck?: "missing" | "found";
|
|
20
|
+
_planFilePath?: string;
|
|
21
|
+
_planCheckInstruction?: string;
|
|
22
|
+
|
|
23
|
+
_shellPlatform?: string;
|
|
24
|
+
_shellType?: string;
|
|
25
|
+
_shellPreamble?: string;
|
|
26
|
+
|
|
27
|
+
_delegationDepth?: number;
|
|
28
|
+
_depthExceeded?: boolean;
|
|
29
|
+
_depthError?: string;
|
|
30
|
+
|
|
31
|
+
_confidenceLevel?: "HIGH" | "MEDIUM" | "LOW" | string;
|
|
32
|
+
_confidenceExchanges?: number;
|
|
33
|
+
|
|
34
|
+
// Guard configuration (centralized — replaces _routeTrackingConfig and _maxDelegationDepth)
|
|
35
|
+
_guardConfig?: import("../guards/guard-config.ts").GuardConfig;
|
|
36
|
+
_guardProgression?: import("../guards/guard-config.ts").GuardProgression;
|
|
37
|
+
|
|
38
|
+
// Subagent failure tracking
|
|
39
|
+
_subagentFailures?: number;
|
|
40
|
+
_subagentFailureThreshold?: number;
|
|
41
|
+
_optiRoute?: {
|
|
42
|
+
reason: string;
|
|
43
|
+
chain: Array<{
|
|
44
|
+
skill: string;
|
|
45
|
+
timestamp: number;
|
|
46
|
+
producedArtifact: boolean;
|
|
47
|
+
}>;
|
|
48
|
+
skillCounts: Record<string, number>;
|
|
49
|
+
unproductiveCount: number;
|
|
50
|
+
maxSkillRepeats: number;
|
|
51
|
+
maxUnproductiveHops: number;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
_routingSkillsDir?: string;
|
|
55
|
+
_nextRoute?: import("../routing/index.ts").RuntimeRouteDecision;
|
|
56
|
+
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type HookContext = HookContextBase & HookContextExtras;
|
|
61
|
+
|
|
62
|
+
export type HookContextPatch = Partial<HookContextBase> &
|
|
63
|
+
Partial<HookContextExtras>;
|
|
64
|
+
|
|
65
|
+
export interface HookMetadata {
|
|
66
|
+
name: string;
|
|
67
|
+
priority: number; // 0-100, higher = earlier within phase
|
|
68
|
+
phase: HookPhase;
|
|
69
|
+
dependencies: string[]; // hook names this depends on
|
|
70
|
+
errorHandling: "propagate" | "isolate" | "retry";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export enum HookResult {
|
|
74
|
+
CONTINUE = "continue",
|
|
75
|
+
STOP = "stop",
|
|
76
|
+
INJECT = "inject",
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface PreToolUseHook {
|
|
80
|
+
metadata: HookMetadata;
|
|
81
|
+
execute(
|
|
82
|
+
context: HookContext,
|
|
83
|
+
): Promise<{ result: HookResult; modifiedContext?: HookContextPatch }>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface PostToolUseHook {
|
|
87
|
+
metadata: HookMetadata;
|
|
88
|
+
execute(
|
|
89
|
+
context: HookContext,
|
|
90
|
+
output: string,
|
|
91
|
+
): Promise<{
|
|
92
|
+
result: HookResult;
|
|
93
|
+
modifiedOutput?: string;
|
|
94
|
+
}>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export interface RouteHook {
|
|
98
|
+
metadata: HookMetadata;
|
|
99
|
+
execute(
|
|
100
|
+
context: HookContext,
|
|
101
|
+
route: string,
|
|
102
|
+
): Promise<{ result: HookResult; modifiedRoute?: string }>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface SessionHook {
|
|
106
|
+
metadata: HookMetadata;
|
|
107
|
+
onSessionStart(context: HookContext): Promise<void>;
|
|
108
|
+
onSessionEnd(context: HookContext): Promise<void>;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Union type for any hook in the registry
|
|
112
|
+
export type AnyHook =
|
|
113
|
+
| PreToolUseHook
|
|
114
|
+
| PostToolUseHook
|
|
115
|
+
| RouteHook
|
|
116
|
+
| SessionHook;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import fs from "node:fs"
|
|
2
|
+
import os from "node:os"
|
|
3
|
+
import path from "node:path"
|
|
4
|
+
|
|
5
|
+
let _planStorageOverride: string | undefined
|
|
6
|
+
|
|
7
|
+
export interface PlanAccess {
|
|
8
|
+
path: string
|
|
9
|
+
status: string | null
|
|
10
|
+
objective: string | null
|
|
11
|
+
summary: string | null
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function setPlanStorageDirForTest(dir: string | undefined): void {
|
|
15
|
+
_planStorageOverride = dir
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function planStorageDir(): string {
|
|
19
|
+
return _planStorageOverride ?? path.join(os.homedir(), ".local", "share", "openhermes", "plans")
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function getProjectName(projectDir: string): string {
|
|
23
|
+
return path.basename(projectDir)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function ensureDir(dir: string): void {
|
|
27
|
+
try {
|
|
28
|
+
if (!fs.existsSync(dir)) {
|
|
29
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
30
|
+
}
|
|
31
|
+
} catch (err) {
|
|
32
|
+
const msg = err instanceof Error ? err.message : String(err)
|
|
33
|
+
console.error(`[openhermes] Failed to create directory ${dir}: ${msg}`)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function readPlanAccess(filePath: string): PlanAccess | null {
|
|
38
|
+
if (!fs.existsSync(filePath)) return null
|
|
39
|
+
const source = fs.readFileSync(filePath, "utf8")
|
|
40
|
+
const status = source.match(/^Status:\s*(.+)$/m)?.[1]?.trim() ?? null
|
|
41
|
+
const objective = source.match(/^Objective:\s*(.+)$/m)?.[1]?.trim() ?? null
|
|
42
|
+
if (!status && !objective) return null
|
|
43
|
+
const parts = [status ? `status=${status}` : null, objective ? `objective=${objective}` : null].filter(Boolean)
|
|
44
|
+
return {
|
|
45
|
+
path: filePath,
|
|
46
|
+
status,
|
|
47
|
+
objective,
|
|
48
|
+
summary: `Active plan: ${parts.join(" | ")}`,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function resolvePlanAccess(projectDir: string): PlanAccess | null {
|
|
53
|
+
const latest = findLatestPlanFile(projectDir)
|
|
54
|
+
if (!latest) return null
|
|
55
|
+
return readPlanAccess(latest)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function findLatestPlanFile(projectDir: string): string | null {
|
|
59
|
+
const projectName = getProjectName(projectDir)
|
|
60
|
+
const storage = planStorageDir()
|
|
61
|
+
const projectDirPath = path.join(storage, projectName)
|
|
62
|
+
if (!fs.existsSync(projectDirPath)) return null
|
|
63
|
+
let latest: string | null = null
|
|
64
|
+
let highest = -1
|
|
65
|
+
try {
|
|
66
|
+
for (const entry of fs.readdirSync(projectDirPath)) {
|
|
67
|
+
const m = entry.match(/^plan-(\d{3})\.md$/)
|
|
68
|
+
if (m) {
|
|
69
|
+
const n = parseInt(m[1], 10)
|
|
70
|
+
if (n > highest) {
|
|
71
|
+
highest = n
|
|
72
|
+
latest = path.join(projectDirPath, entry)
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
} catch {
|
|
77
|
+
return null
|
|
78
|
+
}
|
|
79
|
+
return latest
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function ensurePlanFile(projectDir: string): string {
|
|
83
|
+
const access = resolvePlanAccess(projectDir)
|
|
84
|
+
if (access?.status === "active" || access?.status === "in-progress") {
|
|
85
|
+
return access.path
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const projectName = getProjectName(projectDir)
|
|
89
|
+
const storage = planStorageDir()
|
|
90
|
+
const projectDirPath = path.join(storage, projectName)
|
|
91
|
+
ensureDir(projectDirPath)
|
|
92
|
+
|
|
93
|
+
const latest = access?.path ?? findLatestPlanFile(projectDir)
|
|
94
|
+
let nextSeq = 1
|
|
95
|
+
if (latest) {
|
|
96
|
+
const m = path.basename(latest).match(/^plan-(\d{3})\.md$/)
|
|
97
|
+
if (m) nextSeq = parseInt(m[1], 10) + 1
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const seq = String(nextSeq).padStart(3, "0")
|
|
101
|
+
const planId = `${projectName}/plan-${seq}.md`
|
|
102
|
+
const planPath = path.join(projectDirPath, `plan-${seq}.md`)
|
|
103
|
+
const now = new Date().toISOString().replace("T", " ").slice(0, 16)
|
|
104
|
+
|
|
105
|
+
const content = [
|
|
106
|
+
`# PLAN: ${projectName}`,
|
|
107
|
+
"",
|
|
108
|
+
`Plan ID: ${planId}`,
|
|
109
|
+
`Project: ${projectName}`,
|
|
110
|
+
`Status: active`,
|
|
111
|
+
`Created: ${now}`,
|
|
112
|
+
`Updated: ${now}`,
|
|
113
|
+
`Project Path: ${projectDir}`,
|
|
114
|
+
`Plan Path: ${planPath}`,
|
|
115
|
+
`Objective: (pending classification)`,
|
|
116
|
+
"",
|
|
117
|
+
"## Tasks",
|
|
118
|
+
"",
|
|
119
|
+
"- [ ] (discoverable — pending classification)",
|
|
120
|
+
"",
|
|
121
|
+
].join("\n")
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
fs.writeFileSync(planPath, content, "utf8")
|
|
125
|
+
} catch (err) {
|
|
126
|
+
const msg = err instanceof Error ? err.message : String(err)
|
|
127
|
+
console.error(`[openhermes] Failed to write plan file ${planPath}: ${msg}`)
|
|
128
|
+
}
|
|
129
|
+
return planPath
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function readPlanSummary(projectDir: string): string | null {
|
|
133
|
+
return resolvePlanAccess(projectDir)?.summary ?? null
|
|
134
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export { extractFrontmatter, parseSkillFrontmatter, readSkillFrontmatter, emptySkillRoutes } from "./skill-frontmatter.ts";
|
|
2
|
+
export { resolveRoute } from "./route-resolver.ts";
|
|
3
|
+
export {
|
|
4
|
+
clearRuntimeRouteDecision,
|
|
5
|
+
consumeRouteGuidance,
|
|
6
|
+
extractRouteGuidance,
|
|
7
|
+
extractRuntimeRouteDecision,
|
|
8
|
+
getRuntimeRouteDecision,
|
|
9
|
+
NEXT_ROUTE_PREFIX,
|
|
10
|
+
rememberRuntimeRouteDecision,
|
|
11
|
+
ROUTE_GUIDANCE_PREFIX,
|
|
12
|
+
} from "./route-guidance.ts";
|
|
13
|
+
export { ROUTE_OUTCOMES } from "./types.ts";
|
|
14
|
+
export type {
|
|
15
|
+
RouteEvidence,
|
|
16
|
+
RouteOutcome,
|
|
17
|
+
RouteResolution,
|
|
18
|
+
RuntimeRouteDecision,
|
|
19
|
+
SkillRouteMap,
|
|
20
|
+
SkillRoutingFrontmatter,
|
|
21
|
+
} from "./types.ts";
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ROUTE_ACTIONS,
|
|
3
|
+
ROUTE_OUTCOMES,
|
|
4
|
+
ROUTE_VERIFICATIONS,
|
|
5
|
+
ROUTE_WORK_TYPES,
|
|
6
|
+
} from "./types.ts";
|
|
7
|
+
import type {
|
|
8
|
+
RouteAction,
|
|
9
|
+
RouteOutcome,
|
|
10
|
+
RouteResolution,
|
|
11
|
+
RouteVerification,
|
|
12
|
+
RouteWork,
|
|
13
|
+
RuntimeRouteDecision,
|
|
14
|
+
} from "./types.ts";
|
|
15
|
+
|
|
16
|
+
export const ROUTE_GUIDANCE_PREFIX = "ROUTE_GUIDANCE:";
|
|
17
|
+
export const NEXT_ROUTE_PREFIX = "NEXT_ROUTE:";
|
|
18
|
+
|
|
19
|
+
interface ConsumedRouteGuidance {
|
|
20
|
+
output: string;
|
|
21
|
+
selected: string | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const runtimeRouteState = new Map<string, RuntimeRouteDecision>();
|
|
25
|
+
|
|
26
|
+
function isRouteOutcome(value: unknown): value is RouteOutcome {
|
|
27
|
+
return typeof value === "string" && ROUTE_OUTCOMES.includes(value as RouteOutcome);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isRouteVerification(value: unknown): value is RouteVerification {
|
|
31
|
+
return typeof value === "string" && ROUTE_VERIFICATIONS.includes(value as RouteVerification);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isRouteAction(value: unknown): value is RouteAction {
|
|
35
|
+
return typeof value === "string" && ROUTE_ACTIONS.includes(value as RouteAction);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isRouteWork(value: unknown): value is RouteWork {
|
|
39
|
+
return typeof value === "string" && ROUTE_WORK_TYPES.includes(value as RouteWork);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function extractRouteGuidance(output: string): RouteResolution | null {
|
|
43
|
+
const guidanceLine = output
|
|
44
|
+
.split(/\r?\n/)
|
|
45
|
+
.map((line) => line.trim())
|
|
46
|
+
.find((line) => line.startsWith(ROUTE_GUIDANCE_PREFIX));
|
|
47
|
+
|
|
48
|
+
if (!guidanceLine) return null;
|
|
49
|
+
|
|
50
|
+
const raw = guidanceLine.slice(ROUTE_GUIDANCE_PREFIX.length).trim();
|
|
51
|
+
if (!raw) return null;
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const parsed = JSON.parse(raw) as Partial<RouteResolution>;
|
|
55
|
+
if (!isRouteOutcome(parsed.outcome)) return null;
|
|
56
|
+
if (parsed.verification !== undefined && !isRouteVerification(parsed.verification)) return null;
|
|
57
|
+
if (parsed.action !== undefined && !isRouteAction(parsed.action)) return null;
|
|
58
|
+
if (parsed.work !== undefined && !isRouteWork(parsed.work)) return null;
|
|
59
|
+
if (!Array.isArray(parsed.candidates) || !parsed.candidates.every((candidate) => typeof candidate === "string")) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
if (parsed.selected !== null && parsed.selected !== undefined && typeof parsed.selected !== "string") {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
if (typeof parsed.reason !== "string") return null;
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
outcome: parsed.outcome,
|
|
69
|
+
...(parsed.verification ? { verification: parsed.verification } : {}),
|
|
70
|
+
...(parsed.action ? { action: parsed.action } : {}),
|
|
71
|
+
...(parsed.work ? { work: parsed.work } : {}),
|
|
72
|
+
candidates: parsed.candidates,
|
|
73
|
+
selected: parsed.selected ?? null,
|
|
74
|
+
reason: parsed.reason,
|
|
75
|
+
};
|
|
76
|
+
} catch {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function consumeRouteGuidance(output: string): ConsumedRouteGuidance {
|
|
82
|
+
const guidance = extractRouteGuidance(output);
|
|
83
|
+
if (!guidance?.selected || output.includes(NEXT_ROUTE_PREFIX)) {
|
|
84
|
+
return { output, selected: guidance?.selected ?? null };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
output: `${output.trimEnd()}\n${NEXT_ROUTE_PREFIX} ${guidance.selected}`,
|
|
89
|
+
selected: guidance.selected,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function extractExplicitNextRoute(output: string): string | null {
|
|
94
|
+
const routeLine = output
|
|
95
|
+
.split(/\r?\n/)
|
|
96
|
+
.map((line) => line.trim())
|
|
97
|
+
.find((line) => line.startsWith(NEXT_ROUTE_PREFIX));
|
|
98
|
+
|
|
99
|
+
if (!routeLine) return null;
|
|
100
|
+
|
|
101
|
+
const raw = routeLine.slice(NEXT_ROUTE_PREFIX.length).trim();
|
|
102
|
+
if (!raw || /\s/.test(raw)) return null;
|
|
103
|
+
return raw;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function extractRuntimeRouteDecision(output: string): RuntimeRouteDecision | null {
|
|
107
|
+
const explicitNextRoute = extractExplicitNextRoute(output);
|
|
108
|
+
if (explicitNextRoute) {
|
|
109
|
+
return {
|
|
110
|
+
selected: explicitNextRoute,
|
|
111
|
+
source: "next_route",
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const guidance = extractRouteGuidance(output);
|
|
116
|
+
if (!guidance?.selected) return null;
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
selected: guidance.selected,
|
|
120
|
+
source: "route_guidance",
|
|
121
|
+
outcome: guidance.outcome,
|
|
122
|
+
verification: guidance.verification,
|
|
123
|
+
action: guidance.action,
|
|
124
|
+
work: guidance.work,
|
|
125
|
+
candidates: guidance.candidates,
|
|
126
|
+
reason: guidance.reason,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function rememberRuntimeRouteDecision(sessionId: string, output: string): RuntimeRouteDecision | null {
|
|
131
|
+
const decision = extractRuntimeRouteDecision(output);
|
|
132
|
+
if (!decision) {
|
|
133
|
+
runtimeRouteState.delete(sessionId);
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
runtimeRouteState.set(sessionId, decision);
|
|
138
|
+
return decision;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function getRuntimeRouteDecision(sessionId: string): RuntimeRouteDecision | null {
|
|
142
|
+
return runtimeRouteState.get(sessionId) ?? null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function clearRuntimeRouteDecision(sessionId: string): void {
|
|
146
|
+
runtimeRouteState.delete(sessionId);
|
|
147
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { RouteEvidence, RouteResolution, SkillRouteMap } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
function findCandidate(candidates: string[], fragment: string): string | null {
|
|
4
|
+
return candidates.find((candidate) => candidate.includes(fragment)) ?? null;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function buildResolution(evidence: RouteEvidence, candidates: string[], selected: string | null, reason: string): RouteResolution {
|
|
8
|
+
return {
|
|
9
|
+
outcome: evidence.outcome,
|
|
10
|
+
...(evidence.verification ? { verification: evidence.verification } : {}),
|
|
11
|
+
...(evidence.action ? { action: evidence.action } : {}),
|
|
12
|
+
...(evidence.work ? { work: evidence.work } : {}),
|
|
13
|
+
candidates,
|
|
14
|
+
selected,
|
|
15
|
+
reason,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function resolveRoute(routeMap: SkillRouteMap, evidence: RouteEvidence): RouteResolution {
|
|
20
|
+
const candidates = [...routeMap[evidence.outcome]];
|
|
21
|
+
if (candidates.length === 0) {
|
|
22
|
+
return buildResolution(evidence, candidates, null, `No route candidates declared for outcome \"${evidence.outcome}\".`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (evidence.target && candidates.includes(evidence.target)) {
|
|
26
|
+
return buildResolution(evidence, candidates, evidence.target, `Selected \"${evidence.target}\" from output evidence.`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (evidence.action === "fixable" || evidence.work === "implement") {
|
|
30
|
+
const builderCandidate = findCandidate(candidates, "builder");
|
|
31
|
+
if (builderCandidate) {
|
|
32
|
+
return buildResolution(evidence, candidates, builderCandidate, `Selected \"${builderCandidate}\" for fixable implementation work.`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (evidence.verification === "unverified") {
|
|
37
|
+
const gauntletCandidate = findCandidate(candidates, "gauntlet");
|
|
38
|
+
if (gauntletCandidate) {
|
|
39
|
+
return buildResolution(evidence, candidates, gauntletCandidate, `Selected \"${gauntletCandidate}\" because work is still unverified.`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (evidence.work === "verify") {
|
|
44
|
+
const gauntletCandidate = findCandidate(candidates, "gauntlet");
|
|
45
|
+
if (gauntletCandidate) {
|
|
46
|
+
return buildResolution(evidence, candidates, gauntletCandidate, `Selected \"${gauntletCandidate}\" for verification work.`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (evidence.work === "ship" && evidence.verification === "verified" && evidence.action === "done") {
|
|
51
|
+
const shipCandidate = findCandidate(candidates, "ship");
|
|
52
|
+
if (shipCandidate) {
|
|
53
|
+
return buildResolution(evidence, candidates, shipCandidate, `Selected \"${shipCandidate}\" for verified ship-ready work.`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return buildResolution(evidence, candidates, candidates[0], `Selected first declared route for outcome \"${evidence.outcome}\".`);
|
|
58
|
+
}
|