wyrm-mcp 4.0.0 → 5.0.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/dist/agent-loop.d.ts +86 -0
- package/dist/agent-loop.d.ts.map +1 -0
- package/dist/agent-loop.js +321 -0
- package/dist/agent-loop.js.map +1 -0
- package/dist/goals.d.ts +95 -0
- package/dist/goals.d.ts.map +1 -0
- package/dist/goals.js +146 -0
- package/dist/goals.js.map +1 -0
- package/dist/index.js +424 -0
- package/dist/index.js.map +1 -1
- package/dist/mcp-client.d.ts +76 -0
- package/dist/mcp-client.d.ts.map +1 -0
- package/dist/mcp-client.js +259 -0
- package/dist/mcp-client.js.map +1 -0
- package/dist/migrations.d.ts.map +1 -1
- package/dist/migrations.js +101 -0
- package/dist/migrations.js.map +1 -1
- package/dist/wyrm-loop.d.ts +27 -0
- package/dist/wyrm-loop.d.ts.map +1 -0
- package/dist/wyrm-loop.js +162 -0
- package/dist/wyrm-loop.js.map +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent loop — OODA + ReAct (Observe → Orient → Decide → Act).
|
|
3
|
+
*
|
|
4
|
+
* This is the thing that turns Wyrm from "memory tool" into "agent".
|
|
5
|
+
* Given a goal (or ad-hoc query), we run a multi-turn loop where:
|
|
6
|
+
*
|
|
7
|
+
* 1. Observe — assemble relevant context from Wyrm's own data
|
|
8
|
+
* (failures, truths, sessions, symbols, prior iterations)
|
|
9
|
+
* 2. Orient — LLM synthesises: "given this context, what's the state?"
|
|
10
|
+
* 3. Decide — LLM proposes ONE action: either a Wyrm tool call,
|
|
11
|
+
* an external MCP server call, or 'done' / 'block' / 'escalate'
|
|
12
|
+
* 4. Act — execute the proposed action, capture result
|
|
13
|
+
* 5. Loop — feed result back into the next Observe step
|
|
14
|
+
*
|
|
15
|
+
* Each iteration logs to `goal_iterations` if attached to a goal.
|
|
16
|
+
* Hard caps: `max_iterations` per goal, ~60s wall clock per iteration,
|
|
17
|
+
* actions matched against a whitelist (no `wyrm_audit_export` or other
|
|
18
|
+
* data-exfiltration tools from inside the loop).
|
|
19
|
+
*
|
|
20
|
+
* LLM protocol — JSON envelope works with both:
|
|
21
|
+
* - Ollama JSON mode (most models 7B+)
|
|
22
|
+
* - OpenAI native function calling (gpt-4o, gpt-4o-mini, ...)
|
|
23
|
+
*
|
|
24
|
+
* The response shape we ask for:
|
|
25
|
+
* {"thought":"...","action":"<tool_name>","args":{...}} or
|
|
26
|
+
* {"thought":"...","action":"done","summary":"..."} or
|
|
27
|
+
* {"thought":"...","action":"block","reason":"..."}
|
|
28
|
+
*
|
|
29
|
+
* @copyright 2026 Ghost Protocol (Pvt) Ltd. All Rights Reserved.
|
|
30
|
+
* @license Proprietary
|
|
31
|
+
*/
|
|
32
|
+
import type Database from 'better-sqlite3';
|
|
33
|
+
import { OutboundMcpClient } from './mcp-client.js';
|
|
34
|
+
export interface AgentAction {
|
|
35
|
+
thought?: string;
|
|
36
|
+
action: string;
|
|
37
|
+
args?: Record<string, unknown>;
|
|
38
|
+
summary?: string;
|
|
39
|
+
reason?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface IterationResult {
|
|
42
|
+
iteration_num: number;
|
|
43
|
+
observe_summary: string;
|
|
44
|
+
orient_summary: string;
|
|
45
|
+
decided: AgentAction;
|
|
46
|
+
action_result: string;
|
|
47
|
+
outcome: 'progressed' | 'blocked' | 'done' | 'error';
|
|
48
|
+
latency_ms: number;
|
|
49
|
+
tokens_in?: number;
|
|
50
|
+
tokens_out?: number;
|
|
51
|
+
model: string;
|
|
52
|
+
degraded: boolean;
|
|
53
|
+
}
|
|
54
|
+
export interface ToolDispatcher {
|
|
55
|
+
(toolName: string, args: Record<string, unknown>): Promise<{
|
|
56
|
+
ok: boolean;
|
|
57
|
+
result?: unknown;
|
|
58
|
+
error?: string;
|
|
59
|
+
}>;
|
|
60
|
+
}
|
|
61
|
+
export declare class AgentLoop {
|
|
62
|
+
private db;
|
|
63
|
+
private externalClient;
|
|
64
|
+
private internalToolDispatch;
|
|
65
|
+
private subAgent;
|
|
66
|
+
private goals;
|
|
67
|
+
private failures;
|
|
68
|
+
constructor(db: Database.Database, externalClient: OutboundMcpClient, internalToolDispatch: ToolDispatcher);
|
|
69
|
+
/** Run ONE OODA iteration on a goal. Returns the iteration result. */
|
|
70
|
+
iterate(goal_id: number, opts?: {
|
|
71
|
+
ollama_url?: string;
|
|
72
|
+
openai_api_key?: string;
|
|
73
|
+
model_override?: string;
|
|
74
|
+
}): Promise<IterationResult | null>;
|
|
75
|
+
/** Iterate until done / blocked / cap hit. Returns the array of iterations. */
|
|
76
|
+
pursue(goal_id: number, opts?: {
|
|
77
|
+
max_steps?: number;
|
|
78
|
+
ollama_url?: string;
|
|
79
|
+
openai_api_key?: string;
|
|
80
|
+
model_override?: string;
|
|
81
|
+
}): Promise<IterationResult[]>;
|
|
82
|
+
private observe;
|
|
83
|
+
private orientAndDecide;
|
|
84
|
+
private act;
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=agent-loop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-loop.d.ts","sourceRoot":"","sources":["../src/agent-loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAI3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAwBpD,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,eAAe;IAC9B,aAAa,EAAE,MAAM,CAAC;IAC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CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAgB/J,OAAO,CAAC,OAAO;YAwED,eAAe;YA0Ef,GAAG;CA4BlB"}
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent loop — OODA + ReAct (Observe → Orient → Decide → Act).
|
|
3
|
+
*
|
|
4
|
+
* This is the thing that turns Wyrm from "memory tool" into "agent".
|
|
5
|
+
* Given a goal (or ad-hoc query), we run a multi-turn loop where:
|
|
6
|
+
*
|
|
7
|
+
* 1. Observe — assemble relevant context from Wyrm's own data
|
|
8
|
+
* (failures, truths, sessions, symbols, prior iterations)
|
|
9
|
+
* 2. Orient — LLM synthesises: "given this context, what's the state?"
|
|
10
|
+
* 3. Decide — LLM proposes ONE action: either a Wyrm tool call,
|
|
11
|
+
* an external MCP server call, or 'done' / 'block' / 'escalate'
|
|
12
|
+
* 4. Act — execute the proposed action, capture result
|
|
13
|
+
* 5. Loop — feed result back into the next Observe step
|
|
14
|
+
*
|
|
15
|
+
* Each iteration logs to `goal_iterations` if attached to a goal.
|
|
16
|
+
* Hard caps: `max_iterations` per goal, ~60s wall clock per iteration,
|
|
17
|
+
* actions matched against a whitelist (no `wyrm_audit_export` or other
|
|
18
|
+
* data-exfiltration tools from inside the loop).
|
|
19
|
+
*
|
|
20
|
+
* LLM protocol — JSON envelope works with both:
|
|
21
|
+
* - Ollama JSON mode (most models 7B+)
|
|
22
|
+
* - OpenAI native function calling (gpt-4o, gpt-4o-mini, ...)
|
|
23
|
+
*
|
|
24
|
+
* The response shape we ask for:
|
|
25
|
+
* {"thought":"...","action":"<tool_name>","args":{...}} or
|
|
26
|
+
* {"thought":"...","action":"done","summary":"..."} or
|
|
27
|
+
* {"thought":"...","action":"block","reason":"..."}
|
|
28
|
+
*
|
|
29
|
+
* @copyright 2026 Ghost Protocol (Pvt) Ltd. All Rights Reserved.
|
|
30
|
+
* @license Proprietary
|
|
31
|
+
*/
|
|
32
|
+
import { Goals } from './goals.js';
|
|
33
|
+
import { SubAgent } from './sub-agent.js';
|
|
34
|
+
import { FailurePatterns } from './failure-patterns.js';
|
|
35
|
+
import { sanitizeFtsQuery } from './security.js';
|
|
36
|
+
const DEFAULT_MAX_ITERATIONS = 20;
|
|
37
|
+
const ITER_HARD_TIMEOUT_MS = 90_000;
|
|
38
|
+
// Tools the agent is allowed to call from inside the loop. Whitelist —
|
|
39
|
+
// excludes audit export, sync push, prune, encrypt setup, etc.
|
|
40
|
+
const SAFE_INTERNAL_TOOLS = new Set([
|
|
41
|
+
// Read-mostly
|
|
42
|
+
'wyrm_search', 'wyrm_recall', 'wyrm_project_context', 'wyrm_global_context',
|
|
43
|
+
'wyrm_truth_get', 'wyrm_all_quests', 'wyrm_failure_check', 'wyrm_failure_list',
|
|
44
|
+
'wyrm_symbol_search', 'wyrm_symbol_callers', 'wyrm_symbol_stats',
|
|
45
|
+
'wyrm_session_rehydrate', 'wyrm_decision_upstream', 'wyrm_decision_downstream',
|
|
46
|
+
'wyrm_presence_list', 'wyrm_sync_conflicts',
|
|
47
|
+
// Bounded writes (operator can audit each via goal_iterations)
|
|
48
|
+
'wyrm_quest_add', 'wyrm_failure_record', 'wyrm_decided_because',
|
|
49
|
+
'wyrm_capture', 'wyrm_remember', 'wyrm_distill',
|
|
50
|
+
// Status updates
|
|
51
|
+
'wyrm_quest_complete',
|
|
52
|
+
// External call gateway
|
|
53
|
+
'wyrm_call_external',
|
|
54
|
+
]);
|
|
55
|
+
export class AgentLoop {
|
|
56
|
+
db;
|
|
57
|
+
externalClient;
|
|
58
|
+
internalToolDispatch;
|
|
59
|
+
subAgent;
|
|
60
|
+
goals;
|
|
61
|
+
failures;
|
|
62
|
+
constructor(db, externalClient, internalToolDispatch) {
|
|
63
|
+
this.db = db;
|
|
64
|
+
this.externalClient = externalClient;
|
|
65
|
+
this.internalToolDispatch = internalToolDispatch;
|
|
66
|
+
this.subAgent = new SubAgent(db);
|
|
67
|
+
this.goals = new Goals(db);
|
|
68
|
+
this.failures = new FailurePatterns(db);
|
|
69
|
+
}
|
|
70
|
+
/** Run ONE OODA iteration on a goal. Returns the iteration result. */
|
|
71
|
+
async iterate(goal_id, opts) {
|
|
72
|
+
const goal = this.goals.get(goal_id);
|
|
73
|
+
if (!goal)
|
|
74
|
+
return null;
|
|
75
|
+
if (goal.status !== 'active')
|
|
76
|
+
return null;
|
|
77
|
+
if (goal.iterations_count >= goal.max_iterations) {
|
|
78
|
+
// Hit the cap — record a final 'blocked' iteration and abandon.
|
|
79
|
+
this.goals.recordIteration(goal_id, {
|
|
80
|
+
outcome: 'blocked',
|
|
81
|
+
notes: `Hit max_iterations=${goal.max_iterations}`,
|
|
82
|
+
latency_ms: 0,
|
|
83
|
+
});
|
|
84
|
+
this.goals.abandon(goal_id, `Exceeded max_iterations=${goal.max_iterations}`);
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const start = Date.now();
|
|
88
|
+
// ---- 1. OBSERVE ----
|
|
89
|
+
const observation = this.observe(goal);
|
|
90
|
+
// ---- 2. ORIENT + DECIDE (one LLM call) ----
|
|
91
|
+
const { decided, orient_summary, model, tokens_in, tokens_out, degraded } = await this.orientAndDecide(goal, observation, opts);
|
|
92
|
+
// ---- 3. ACT ----
|
|
93
|
+
const actionResult = await this.act(decided);
|
|
94
|
+
const latency = Date.now() - start;
|
|
95
|
+
const outcome = decided.action === 'done' ? 'done'
|
|
96
|
+
: decided.action === 'block' || decided.action === 'escalate' ? 'blocked'
|
|
97
|
+
: actionResult.startsWith('ERROR:') ? 'error'
|
|
98
|
+
: 'progressed';
|
|
99
|
+
// ---- 4. RECORD ----
|
|
100
|
+
this.goals.recordIteration(goal_id, {
|
|
101
|
+
observe_summary: observation.summary,
|
|
102
|
+
orient_summary,
|
|
103
|
+
decided_action: JSON.stringify(decided),
|
|
104
|
+
action_result: actionResult.slice(0, 4000),
|
|
105
|
+
outcome,
|
|
106
|
+
latency_ms: latency,
|
|
107
|
+
tokens_in,
|
|
108
|
+
tokens_out,
|
|
109
|
+
model,
|
|
110
|
+
});
|
|
111
|
+
// Append an agent_actions row too — the higher-level audit
|
|
112
|
+
try {
|
|
113
|
+
this.db.prepare(`
|
|
114
|
+
INSERT INTO agent_actions
|
|
115
|
+
(actor, goal_id, action_kind, summary, result_status)
|
|
116
|
+
VALUES ('wyrm-loop', ?, ?, ?, ?)
|
|
117
|
+
`).run(goal_id, decided.action === 'done' || decided.action === 'block' ? decided.action : 'act', `${decided.action}: ${(decided.thought ?? decided.summary ?? '').slice(0, 200)}`, outcome === 'done' ? 'success'
|
|
118
|
+
: outcome === 'progressed' ? 'partial'
|
|
119
|
+
: outcome === 'blocked' ? 'noop'
|
|
120
|
+
: 'failure');
|
|
121
|
+
}
|
|
122
|
+
catch { /* best-effort */ }
|
|
123
|
+
return {
|
|
124
|
+
iteration_num: goal.iterations_count + 1,
|
|
125
|
+
observe_summary: observation.summary,
|
|
126
|
+
orient_summary,
|
|
127
|
+
decided,
|
|
128
|
+
action_result: actionResult,
|
|
129
|
+
outcome,
|
|
130
|
+
latency_ms: latency,
|
|
131
|
+
tokens_in,
|
|
132
|
+
tokens_out,
|
|
133
|
+
model,
|
|
134
|
+
degraded,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/** Iterate until done / blocked / cap hit. Returns the array of iterations. */
|
|
138
|
+
async pursue(goal_id, opts) {
|
|
139
|
+
const cap = Math.min(50, Math.max(1, opts?.max_steps ?? 10));
|
|
140
|
+
const out = [];
|
|
141
|
+
for (let i = 0; i < cap; i++) {
|
|
142
|
+
const r = await this.iterate(goal_id, opts);
|
|
143
|
+
if (!r)
|
|
144
|
+
break;
|
|
145
|
+
out.push(r);
|
|
146
|
+
if (r.outcome === 'done' || r.outcome === 'blocked' || r.outcome === 'error')
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
// ============================================================
|
|
152
|
+
// OODA stages
|
|
153
|
+
// ============================================================
|
|
154
|
+
observe(goal) {
|
|
155
|
+
const parts = [];
|
|
156
|
+
const tags = [];
|
|
157
|
+
// Last 3 iterations — what's been tried already
|
|
158
|
+
const prior = this.goals.iterationsFor(goal.id, 3);
|
|
159
|
+
if (prior.length > 0) {
|
|
160
|
+
parts.push('## Recent iterations');
|
|
161
|
+
for (const it of prior.reverse()) {
|
|
162
|
+
parts.push(`- #${it.iteration_num} (${it.outcome}): ${it.notes ?? it.action_result?.slice(0, 200) ?? '(no notes)'}`);
|
|
163
|
+
}
|
|
164
|
+
tags.push(`iter:${prior.length}`);
|
|
165
|
+
}
|
|
166
|
+
// Project context (truths + open quests)
|
|
167
|
+
if (goal.project_id != null) {
|
|
168
|
+
try {
|
|
169
|
+
const truths = this.db.prepare(`
|
|
170
|
+
SELECT category, key, value, rationale FROM ground_truths
|
|
171
|
+
WHERE project_id = ? AND is_current = 1
|
|
172
|
+
ORDER BY confidence DESC LIMIT 10
|
|
173
|
+
`).all(goal.project_id);
|
|
174
|
+
if (truths.length > 0) {
|
|
175
|
+
parts.push('## Ground truths');
|
|
176
|
+
for (const t of truths) {
|
|
177
|
+
parts.push(`- ${t.category}.${t.key} = ${t.value}${t.rationale ? ` (${t.rationale})` : ''}`);
|
|
178
|
+
}
|
|
179
|
+
tags.push(`truths:${truths.length}`);
|
|
180
|
+
}
|
|
181
|
+
const quests = this.db.prepare(`
|
|
182
|
+
SELECT id, title, priority FROM quests
|
|
183
|
+
WHERE project_id = ? AND status IN ('pending','in_progress')
|
|
184
|
+
ORDER BY
|
|
185
|
+
CASE priority WHEN 'critical' THEN 0 WHEN 'high' THEN 1
|
|
186
|
+
WHEN 'medium' THEN 2 ELSE 3 END
|
|
187
|
+
LIMIT 8
|
|
188
|
+
`).all(goal.project_id);
|
|
189
|
+
if (quests.length > 0) {
|
|
190
|
+
parts.push('## Open quests');
|
|
191
|
+
for (const q of quests)
|
|
192
|
+
parts.push(`- #${q.id} [${q.priority}] ${q.title}`);
|
|
193
|
+
tags.push(`quests:${quests.length}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch { /* tables may not exist on very old DBs */ }
|
|
197
|
+
}
|
|
198
|
+
// Failures around the goal topic
|
|
199
|
+
try {
|
|
200
|
+
const failsQuery = sanitizeFtsQuery(goal.title.slice(0, 100));
|
|
201
|
+
if (failsQuery) {
|
|
202
|
+
const fails = this.db.prepare(`
|
|
203
|
+
SELECT fp.id, fp.scope, fp.target, fp.description, fp.why_failed
|
|
204
|
+
FROM failure_patterns fp
|
|
205
|
+
JOIN failure_patterns_fts fts ON fts.rowid = fp.id
|
|
206
|
+
WHERE failure_patterns_fts MATCH ? AND fp.resolved = 0
|
|
207
|
+
ORDER BY fp.occurrences DESC LIMIT 5
|
|
208
|
+
`).all(failsQuery);
|
|
209
|
+
if (fails.length > 0) {
|
|
210
|
+
parts.push('## Known failures — DO NOT repeat');
|
|
211
|
+
for (const f of fails) {
|
|
212
|
+
parts.push(`- #${f.id} ${f.scope}:${f.target} — ${f.description}${f.why_failed ? ` (why: ${f.why_failed})` : ''}`);
|
|
213
|
+
}
|
|
214
|
+
tags.push(`fails:${fails.length}`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
catch { /* failure_patterns may not exist */ }
|
|
219
|
+
const detail = parts.join('\n');
|
|
220
|
+
const summary = tags.join(' ') || '(no signal)';
|
|
221
|
+
return { summary, detail };
|
|
222
|
+
}
|
|
223
|
+
async orientAndDecide(goal, observation, opts) {
|
|
224
|
+
const toolNames = Array.from(SAFE_INTERNAL_TOOLS).sort();
|
|
225
|
+
const prompt = [
|
|
226
|
+
`You are Wyrm, an autonomous agent pursuing this goal across multiple sessions.`,
|
|
227
|
+
``,
|
|
228
|
+
`GOAL: ${goal.title}`,
|
|
229
|
+
goal.description ? `DESCRIPTION: ${goal.description}` : '',
|
|
230
|
+
goal.success_criteria ? `SUCCESS CRITERIA: ${goal.success_criteria}` : '',
|
|
231
|
+
goal.deadline ? `DEADLINE: ${goal.deadline}` : '',
|
|
232
|
+
``,
|
|
233
|
+
`=== OBSERVED CONTEXT ===`,
|
|
234
|
+
observation.detail || '(no context — fresh start)',
|
|
235
|
+
`=== END CONTEXT ===`,
|
|
236
|
+
``,
|
|
237
|
+
`Decide ONE next action. Respond with a single JSON object only. Schema:`,
|
|
238
|
+
` {"thought": "<reasoning>", "action": "<one of: done | block | escalate | ${toolNames.join(' | ')}>", "args": {...}, "summary": "<if done>", "reason": "<if block>"}`,
|
|
239
|
+
``,
|
|
240
|
+
`Rules:`,
|
|
241
|
+
`- If success_criteria is met based on context, respond with action="done" and a summary.`,
|
|
242
|
+
`- If you are stuck (need human input, missing capability), respond with action="block" and a reason.`,
|
|
243
|
+
`- If you've tried the same approach 3+ times unsuccessfully, switch tactics or block.`,
|
|
244
|
+
`- Otherwise pick exactly ONE tool from the list. Provide "args" matching that tool's schema.`,
|
|
245
|
+
`- Do NOT repeat actions listed in "Known failures".`,
|
|
246
|
+
``,
|
|
247
|
+
`Respond with ONLY the JSON object — no markdown, no preamble.`,
|
|
248
|
+
].filter(Boolean).join('\n');
|
|
249
|
+
// We use SubAgent.ask but bypass its context assembly — we supply the
|
|
250
|
+
// full prompt and just want the LLM response.
|
|
251
|
+
const r = await this.subAgent.ask({
|
|
252
|
+
query: prompt,
|
|
253
|
+
project_id: goal.project_id ?? null,
|
|
254
|
+
max_context_chars: 200, // minimal, we supply our own
|
|
255
|
+
ollama_url: opts?.ollama_url,
|
|
256
|
+
openai_api_key: opts?.openai_api_key,
|
|
257
|
+
model_override: opts?.model_override,
|
|
258
|
+
});
|
|
259
|
+
// Parse the JSON response (LLMs sometimes wrap with markdown — strip it)
|
|
260
|
+
const cleaned = r.answer
|
|
261
|
+
.replace(/^[\s\S]*?```(?:json)?/, '')
|
|
262
|
+
.replace(/```[\s\S]*$/, '')
|
|
263
|
+
.trim();
|
|
264
|
+
let decided;
|
|
265
|
+
try {
|
|
266
|
+
decided = JSON.parse(cleaned.length > 0 ? cleaned : r.answer);
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
// LLM gave non-JSON. Treat as 'block'.
|
|
270
|
+
decided = {
|
|
271
|
+
action: 'block',
|
|
272
|
+
reason: r.degraded
|
|
273
|
+
? 'No LLM available'
|
|
274
|
+
: 'LLM returned non-JSON response (could not parse decision)',
|
|
275
|
+
thought: r.answer.slice(0, 500),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
if (!decided.action)
|
|
279
|
+
decided.action = 'block';
|
|
280
|
+
return {
|
|
281
|
+
decided,
|
|
282
|
+
orient_summary: decided.thought ?? '(no thought)',
|
|
283
|
+
model: r.model,
|
|
284
|
+
tokens_in: r.tokens_in,
|
|
285
|
+
tokens_out: r.tokens_out,
|
|
286
|
+
degraded: r.degraded,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
async act(decided) {
|
|
290
|
+
if (decided.action === 'done')
|
|
291
|
+
return `DONE: ${decided.summary ?? '(no summary)'}`;
|
|
292
|
+
if (decided.action === 'block')
|
|
293
|
+
return `BLOCKED: ${decided.reason ?? '(no reason)'}`;
|
|
294
|
+
if (decided.action === 'escalate')
|
|
295
|
+
return `ESCALATED: ${decided.reason ?? decided.summary ?? '(no reason)'}`;
|
|
296
|
+
// Whitelist check — refuse anything not in SAFE_INTERNAL_TOOLS or a recognized external pattern
|
|
297
|
+
if (decided.action === 'wyrm_call_external') {
|
|
298
|
+
const args = decided.args;
|
|
299
|
+
if (!args?.server || !args?.tool)
|
|
300
|
+
return 'ERROR: wyrm_call_external requires server + tool';
|
|
301
|
+
const r = await this.externalClient.call(args.server, args.tool, args.args ?? {});
|
|
302
|
+
return r.ok
|
|
303
|
+
? `OK external ${args.server}.${args.tool}: ${JSON.stringify(r.result).slice(0, 500)}`
|
|
304
|
+
: `ERROR external ${args.server}.${args.tool}: ${r.error ?? 'unknown'}`;
|
|
305
|
+
}
|
|
306
|
+
if (!SAFE_INTERNAL_TOOLS.has(decided.action)) {
|
|
307
|
+
return `ERROR: tool '${decided.action}' is not in the agent-loop whitelist`;
|
|
308
|
+
}
|
|
309
|
+
// Internal tool dispatch via the harness function
|
|
310
|
+
try {
|
|
311
|
+
const r = await this.internalToolDispatch(decided.action, decided.args ?? {});
|
|
312
|
+
if (r.ok)
|
|
313
|
+
return `OK ${decided.action}: ${JSON.stringify(r.result).slice(0, 500)}`;
|
|
314
|
+
return `ERROR ${decided.action}: ${r.error ?? 'unknown'}`;
|
|
315
|
+
}
|
|
316
|
+
catch (err) {
|
|
317
|
+
return `ERROR ${decided.action}: ${err.message}`;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
//# sourceMappingURL=agent-loop.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-loop.js","sourceRoot":"","sources":["../src/agent-loop.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAGH,OAAO,EAAE,KAAK,EAAiC,MAAM,YAAY,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAqB,MAAM,uBAAuB,CAAC;AAE3E,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,MAAM,sBAAsB,GAAG,EAAE,CAAC;AAClC,MAAM,oBAAoB,GAAG,MAAM,CAAC;AAEpC,uEAAuE;AACvE,+DAA+D;AAC/D,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAS;IAC1C,cAAc;IACd,aAAa,EAAE,aAAa,EAAE,sBAAsB,EAAE,qBAAqB;IAC3E,gBAAgB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,mBAAmB;IAC9E,oBAAoB,EAAE,qBAAqB,EAAE,mBAAmB;IAChE,wBAAwB,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|
+
/**
|
|
2
|
+
* Goals — persistent objectives Wyrm pursues across sessions.
|
|
3
|
+
*
|
|
4
|
+
* A goal is a long-lived row. Each `pursue` invocation runs one OODA
|
|
5
|
+
* iteration (Observe → Orient → Decide → Act), appends a row to
|
|
6
|
+
* `goal_iterations`, and updates the goal's `last_pursued_at` +
|
|
7
|
+
* `iterations_count`. The agent loop (`wyrm-loop` daemon) picks the
|
|
8
|
+
* highest-priority active goal each tick.
|
|
9
|
+
*
|
|
10
|
+
* Goals carry `max_iterations` to prevent infinite spinning on
|
|
11
|
+
* unresolvable objectives. `success_criteria` is the operator-defined
|
|
12
|
+
* "done when X" — the LLM checks against it each iteration.
|
|
13
|
+
*
|
|
14
|
+
* @copyright 2026 Ghost Protocol (Pvt) Ltd. All Rights Reserved.
|
|
15
|
+
* @license Proprietary
|
|
16
|
+
*/
|
|
17
|
+
import type Database from 'better-sqlite3';
|
|
18
|
+
export type GoalStatus = 'active' | 'paused' | 'completed' | 'abandoned';
|
|
19
|
+
export type GoalPriority = 'critical' | 'high' | 'medium' | 'low';
|
|
20
|
+
export interface Goal {
|
|
21
|
+
id: number;
|
|
22
|
+
project_id: number | null;
|
|
23
|
+
title: string;
|
|
24
|
+
description: string | null;
|
|
25
|
+
success_criteria: string | null;
|
|
26
|
+
deadline: string | null;
|
|
27
|
+
status: GoalStatus;
|
|
28
|
+
priority: GoalPriority;
|
|
29
|
+
max_iterations: number;
|
|
30
|
+
iterations_count: number;
|
|
31
|
+
last_pursued_at: string | null;
|
|
32
|
+
last_outcome: string | null;
|
|
33
|
+
created_at: string;
|
|
34
|
+
updated_at: string;
|
|
35
|
+
completed_at: string | null;
|
|
36
|
+
}
|
|
37
|
+
export interface GoalIteration {
|
|
38
|
+
id: number;
|
|
39
|
+
goal_id: number;
|
|
40
|
+
iteration_num: number;
|
|
41
|
+
observe_summary: string | null;
|
|
42
|
+
orient_summary: string | null;
|
|
43
|
+
decided_action: string | null;
|
|
44
|
+
action_result: string | null;
|
|
45
|
+
outcome: 'progressed' | 'blocked' | 'done' | 'error';
|
|
46
|
+
notes: string | null;
|
|
47
|
+
latency_ms: number;
|
|
48
|
+
tokens_in: number | null;
|
|
49
|
+
tokens_out: number | null;
|
|
50
|
+
model: string | null;
|
|
51
|
+
ran_at: string;
|
|
52
|
+
}
|
|
53
|
+
export interface SetGoalInput {
|
|
54
|
+
project_id?: number | null;
|
|
55
|
+
title: string;
|
|
56
|
+
description?: string;
|
|
57
|
+
success_criteria?: string;
|
|
58
|
+
deadline?: string;
|
|
59
|
+
priority?: GoalPriority;
|
|
60
|
+
max_iterations?: number;
|
|
61
|
+
}
|
|
62
|
+
export declare class Goals {
|
|
63
|
+
private db;
|
|
64
|
+
constructor(db: Database.Database);
|
|
65
|
+
set(input: SetGoalInput): Goal;
|
|
66
|
+
get(id: number): Goal | null;
|
|
67
|
+
list(opts?: {
|
|
68
|
+
status?: GoalStatus;
|
|
69
|
+
project_id?: number;
|
|
70
|
+
limit?: number;
|
|
71
|
+
}): Goal[];
|
|
72
|
+
/** Pick the next goal to pursue: highest priority, least-recently
|
|
73
|
+
* pursued, only active goals that haven't hit iteration cap. Returns
|
|
74
|
+
* null when there's nothing to do. */
|
|
75
|
+
nextToPursue(projectId?: number): Goal | null;
|
|
76
|
+
/** Record one OODA iteration result and bump the goal's counters. */
|
|
77
|
+
recordIteration(goal_id: number, input: {
|
|
78
|
+
observe_summary?: string;
|
|
79
|
+
orient_summary?: string;
|
|
80
|
+
decided_action?: string;
|
|
81
|
+
action_result?: string;
|
|
82
|
+
outcome: 'progressed' | 'blocked' | 'done' | 'error';
|
|
83
|
+
notes?: string;
|
|
84
|
+
latency_ms: number;
|
|
85
|
+
tokens_in?: number;
|
|
86
|
+
tokens_out?: number;
|
|
87
|
+
model?: string;
|
|
88
|
+
}): GoalIteration;
|
|
89
|
+
iterationsFor(goal_id: number, limit?: number): GoalIteration[];
|
|
90
|
+
complete(id: number, note?: string): Goal | null;
|
|
91
|
+
pause(id: number): Goal | null;
|
|
92
|
+
resume(id: number): Goal | null;
|
|
93
|
+
abandon(id: number, reason?: string): Goal | null;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=goals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"goals.d.ts","sourceRoot":"","sources":["../src/goals.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,QAAQ,MAAM,gBAAgB,CAAC;AAE3C,MAAM,MAAM,UAAU,GAAG,QAAQ,GAAG,QAAQ,GAAG,WAAW,GAAG,WAAW,CAAC;AACzE,MAAM,MAAM,YAAY,GAAG,UAAU,GAAG,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAC;AAElE,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,UAAU,CAAC;IACnB,QAAQ,EAAE,YAAY,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;IACvB,gBAAgB,EAAE,MAAM,CAAC;IACzB,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AAED,MAAM,WAAW,aAAa;IAC5B,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;IACtB,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B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