nexo-brain 2.7.0 → 3.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/.claude-plugin/plugin.json +1 -1
- package/README.md +66 -12
- package/hooks/hooks.json +79 -0
- package/package.json +1 -1
- package/src/agent_runner.py +290 -6
- package/src/cli.py +111 -0
- package/src/client_preferences.py +94 -0
- package/src/client_sync.py +202 -2
- package/src/cognitive/__init__.py +1 -1
- package/src/cognitive/_search.py +39 -19
- package/src/dashboard/app.py +140 -0
- package/src/dashboard/templates/base.html +4 -0
- package/src/dashboard/templates/protocol.html +199 -0
- package/src/db/__init__.py +23 -1
- package/src/db/_learnings.py +31 -4
- package/src/db/_personal_scripts.py +12 -0
- package/src/db/_protocol.py +303 -0
- package/src/db/_schema.py +248 -0
- package/src/db/_watchers.py +173 -0
- package/src/db/_workflow.py +952 -0
- package/src/doctor/providers/runtime.py +918 -7
- package/src/evolution_cycle.py +62 -0
- package/src/hook_guardrails.py +308 -0
- package/src/hooks/protocol-guardrail.sh +10 -0
- package/src/nexo_sdk.py +103 -0
- package/src/plugins/cognitive_memory.py +18 -0
- package/src/plugins/cortex.py +55 -35
- package/src/plugins/guard.py +132 -16
- package/src/plugins/protocol.py +911 -0
- package/src/plugins/schedule.py +40 -6
- package/src/plugins/simple_api.py +103 -0
- package/src/plugins/skills.py +67 -0
- package/src/plugins/state_watchers.py +79 -0
- package/src/plugins/workflow.py +588 -0
- package/src/public_contribution.py +86 -12
- package/src/script_registry.py +142 -0
- package/src/scripts/deep-sleep/apply_findings.py +204 -0
- package/src/scripts/deep-sleep/collect.py +49 -4
- package/src/scripts/nexo-agent-run.py +2 -0
- package/src/scripts/nexo-daily-self-audit.py +843 -5
- package/src/scripts/nexo-evolution-run.py +343 -1
- package/src/server.py +92 -6
- package/src/skills_runtime.py +151 -0
- package/src/state_watchers_runtime.py +334 -0
- package/src/tools_learnings.py +345 -7
- package/src/tools_sessions.py +183 -0
- package/templates/CLAUDE.md.template +9 -1
- package/templates/CODEX.AGENTS.md.template +10 -2
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
"""Workflow plugin — durable execution runtime for long multi-step tasks."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
|
|
7
|
+
from db import (
|
|
8
|
+
create_workflow_goal,
|
|
9
|
+
create_workflow_run,
|
|
10
|
+
get_workflow_goal,
|
|
11
|
+
get_protocol_task,
|
|
12
|
+
get_workflow_run,
|
|
13
|
+
get_workflow_replay,
|
|
14
|
+
get_workflow_resume_state,
|
|
15
|
+
list_workflow_goals,
|
|
16
|
+
list_workflow_runs,
|
|
17
|
+
record_workflow_transition,
|
|
18
|
+
update_workflow_goal,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _parse_json_list(value: str) -> list:
|
|
23
|
+
if not value:
|
|
24
|
+
return []
|
|
25
|
+
try:
|
|
26
|
+
parsed = json.loads(value)
|
|
27
|
+
except json.JSONDecodeError:
|
|
28
|
+
return []
|
|
29
|
+
return parsed if isinstance(parsed, list) else []
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def _parse_json_object(value: str) -> dict | None:
|
|
33
|
+
if value is None:
|
|
34
|
+
return None
|
|
35
|
+
stripped = str(value).strip()
|
|
36
|
+
if not stripped:
|
|
37
|
+
return None
|
|
38
|
+
try:
|
|
39
|
+
parsed = json.loads(stripped)
|
|
40
|
+
except json.JSONDecodeError:
|
|
41
|
+
return None
|
|
42
|
+
return parsed if isinstance(parsed, dict) else None
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def handle_workflow_open(
|
|
46
|
+
sid: str,
|
|
47
|
+
goal: str,
|
|
48
|
+
goal_id: str = "",
|
|
49
|
+
workflow_kind: str = "general",
|
|
50
|
+
protocol_task_id: str = "",
|
|
51
|
+
idempotency_key: str = "",
|
|
52
|
+
priority: str = "normal",
|
|
53
|
+
steps: str = "[]",
|
|
54
|
+
shared_state: str = "{}",
|
|
55
|
+
next_action: str = "",
|
|
56
|
+
owner: str = "",
|
|
57
|
+
) -> str:
|
|
58
|
+
"""Open a durable workflow run for a long multi-step task."""
|
|
59
|
+
clean_sid = (sid or "").strip()
|
|
60
|
+
clean_goal = (goal or "").strip()
|
|
61
|
+
if not clean_sid:
|
|
62
|
+
return json.dumps({"ok": False, "error": "sid is required"}, ensure_ascii=False, indent=2)
|
|
63
|
+
if not clean_goal:
|
|
64
|
+
return json.dumps({"ok": False, "error": "goal is required"}, ensure_ascii=False, indent=2)
|
|
65
|
+
|
|
66
|
+
clean_protocol_task_id = (protocol_task_id or "").strip()
|
|
67
|
+
if clean_protocol_task_id:
|
|
68
|
+
task = get_protocol_task(clean_protocol_task_id)
|
|
69
|
+
if not task:
|
|
70
|
+
return json.dumps(
|
|
71
|
+
{"ok": False, "error": f"Unknown protocol_task_id: {clean_protocol_task_id}"},
|
|
72
|
+
ensure_ascii=False,
|
|
73
|
+
indent=2,
|
|
74
|
+
)
|
|
75
|
+
if task.get("session_id") and task["session_id"] != clean_sid:
|
|
76
|
+
return json.dumps(
|
|
77
|
+
{
|
|
78
|
+
"ok": False,
|
|
79
|
+
"error": f"Protocol task {clean_protocol_task_id} belongs to {task['session_id']}, not {clean_sid}",
|
|
80
|
+
},
|
|
81
|
+
ensure_ascii=False,
|
|
82
|
+
indent=2,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
try:
|
|
86
|
+
run = create_workflow_run(
|
|
87
|
+
clean_sid,
|
|
88
|
+
clean_goal,
|
|
89
|
+
goal_id=(goal_id or "").strip(),
|
|
90
|
+
workflow_kind=(workflow_kind or "general").strip() or "general",
|
|
91
|
+
protocol_task_id=clean_protocol_task_id,
|
|
92
|
+
idempotency_key=(idempotency_key or "").strip(),
|
|
93
|
+
priority=(priority or "normal").strip().lower(),
|
|
94
|
+
steps=_parse_json_list(steps),
|
|
95
|
+
shared_state=_parse_json_object(shared_state) if str(shared_state).strip() else {},
|
|
96
|
+
next_action=(next_action or "").strip(),
|
|
97
|
+
owner=(owner or "").strip(),
|
|
98
|
+
)
|
|
99
|
+
except ValueError as exc:
|
|
100
|
+
return json.dumps({"ok": False, "error": str(exc)}, ensure_ascii=False, indent=2)
|
|
101
|
+
if not run:
|
|
102
|
+
return json.dumps({"ok": False, "error": "workflow run could not be created"}, ensure_ascii=False, indent=2)
|
|
103
|
+
|
|
104
|
+
step_titles = [step["title"] for step in (run.get("steps") or [])[:8]]
|
|
105
|
+
response = {
|
|
106
|
+
"ok": True,
|
|
107
|
+
"run_id": run["run_id"],
|
|
108
|
+
"status": run["status"],
|
|
109
|
+
"reused_existing": bool(run.get("reused_existing")),
|
|
110
|
+
"goal_id": run.get("goal_id", ""),
|
|
111
|
+
"workflow_kind": run.get("workflow_kind"),
|
|
112
|
+
"protocol_task_id": run.get("protocol_task_id", ""),
|
|
113
|
+
"current_step_key": run.get("current_step_key", ""),
|
|
114
|
+
"next_action": run.get("next_action", ""),
|
|
115
|
+
"step_count": len(run.get("steps") or []),
|
|
116
|
+
"steps": step_titles,
|
|
117
|
+
}
|
|
118
|
+
return json.dumps(response, ensure_ascii=False, indent=2)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def handle_goal_open(
|
|
122
|
+
sid: str,
|
|
123
|
+
title: str,
|
|
124
|
+
objective: str = "",
|
|
125
|
+
parent_goal_id: str = "",
|
|
126
|
+
priority: str = "normal",
|
|
127
|
+
next_action: str = "",
|
|
128
|
+
success_signal: str = "",
|
|
129
|
+
owner: str = "",
|
|
130
|
+
shared_state: str = "{}",
|
|
131
|
+
) -> str:
|
|
132
|
+
"""Open a durable goal so objectives survive sessions and can own workflows."""
|
|
133
|
+
clean_sid = (sid or "").strip()
|
|
134
|
+
clean_title = (title or "").strip()
|
|
135
|
+
if not clean_sid:
|
|
136
|
+
return json.dumps({"ok": False, "error": "sid is required"}, ensure_ascii=False, indent=2)
|
|
137
|
+
if not clean_title:
|
|
138
|
+
return json.dumps({"ok": False, "error": "title is required"}, ensure_ascii=False, indent=2)
|
|
139
|
+
try:
|
|
140
|
+
goal = create_workflow_goal(
|
|
141
|
+
clean_sid,
|
|
142
|
+
clean_title,
|
|
143
|
+
objective=(objective or "").strip(),
|
|
144
|
+
parent_goal_id=(parent_goal_id or "").strip(),
|
|
145
|
+
priority=(priority or "normal").strip().lower(),
|
|
146
|
+
next_action=(next_action or "").strip(),
|
|
147
|
+
success_signal=(success_signal or "").strip(),
|
|
148
|
+
owner=(owner or "").strip(),
|
|
149
|
+
shared_state=_parse_json_object(shared_state) if str(shared_state).strip() else {},
|
|
150
|
+
)
|
|
151
|
+
except ValueError as exc:
|
|
152
|
+
return json.dumps({"ok": False, "error": str(exc)}, ensure_ascii=False, indent=2)
|
|
153
|
+
|
|
154
|
+
return json.dumps(
|
|
155
|
+
{
|
|
156
|
+
"ok": True,
|
|
157
|
+
"goal_id": goal["goal_id"],
|
|
158
|
+
"status": goal["status"],
|
|
159
|
+
"title": goal["title"],
|
|
160
|
+
"priority": goal.get("priority", "normal"),
|
|
161
|
+
"parent_goal_id": goal.get("parent_goal_id", ""),
|
|
162
|
+
"next_action": goal.get("next_action", ""),
|
|
163
|
+
},
|
|
164
|
+
ensure_ascii=False,
|
|
165
|
+
indent=2,
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def handle_goal_update(
|
|
170
|
+
goal_id: str,
|
|
171
|
+
status: str = "",
|
|
172
|
+
title: str = "",
|
|
173
|
+
objective: str = "",
|
|
174
|
+
parent_goal_id: str = "",
|
|
175
|
+
next_action: str = "",
|
|
176
|
+
success_signal: str = "",
|
|
177
|
+
blocker_reason: str = "",
|
|
178
|
+
owner: str = "",
|
|
179
|
+
shared_state: str = "",
|
|
180
|
+
) -> str:
|
|
181
|
+
"""Update a durable goal with blocked/abandoned/completed state."""
|
|
182
|
+
clean_goal_id = (goal_id or "").strip()
|
|
183
|
+
if not clean_goal_id:
|
|
184
|
+
return json.dumps({"ok": False, "error": "goal_id is required"}, ensure_ascii=False, indent=2)
|
|
185
|
+
try:
|
|
186
|
+
goal = update_workflow_goal(
|
|
187
|
+
clean_goal_id,
|
|
188
|
+
status=(status or "").strip().lower(),
|
|
189
|
+
title=(title or "").strip(),
|
|
190
|
+
objective=(objective or "").strip(),
|
|
191
|
+
parent_goal_id=(parent_goal_id or "").strip(),
|
|
192
|
+
next_action=(next_action or "").strip(),
|
|
193
|
+
success_signal=(success_signal or "").strip(),
|
|
194
|
+
blocker_reason=(blocker_reason or "").strip(),
|
|
195
|
+
owner=(owner or "").strip(),
|
|
196
|
+
shared_state=_parse_json_object(shared_state) if str(shared_state).strip() else None,
|
|
197
|
+
)
|
|
198
|
+
except ValueError as exc:
|
|
199
|
+
return json.dumps({"ok": False, "error": str(exc)}, ensure_ascii=False, indent=2)
|
|
200
|
+
if not goal:
|
|
201
|
+
return json.dumps({"ok": False, "error": f"Unknown goal_id: {clean_goal_id}"}, ensure_ascii=False, indent=2)
|
|
202
|
+
|
|
203
|
+
return json.dumps(
|
|
204
|
+
{
|
|
205
|
+
"ok": True,
|
|
206
|
+
"goal_id": goal["goal_id"],
|
|
207
|
+
"status": goal["status"],
|
|
208
|
+
"title": goal["title"],
|
|
209
|
+
"next_action": goal.get("next_action", ""),
|
|
210
|
+
"blocker_reason": goal.get("blocker_reason", ""),
|
|
211
|
+
"run_count": goal.get("run_count", 0),
|
|
212
|
+
"open_run_count": goal.get("open_run_count", 0),
|
|
213
|
+
"child_count": goal.get("child_count", 0),
|
|
214
|
+
},
|
|
215
|
+
ensure_ascii=False,
|
|
216
|
+
indent=2,
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def handle_goal_get(goal_id: str, include_runs: bool = False) -> str:
|
|
221
|
+
"""Get one durable goal and optionally include linked workflow runs."""
|
|
222
|
+
clean_goal_id = (goal_id or "").strip()
|
|
223
|
+
if not clean_goal_id:
|
|
224
|
+
return json.dumps({"ok": False, "error": "goal_id is required"}, ensure_ascii=False, indent=2)
|
|
225
|
+
goal = get_workflow_goal(clean_goal_id, include_runs=bool(include_runs))
|
|
226
|
+
if not goal:
|
|
227
|
+
return json.dumps({"ok": False, "error": f"Unknown goal_id: {clean_goal_id}"}, ensure_ascii=False, indent=2)
|
|
228
|
+
return json.dumps({"ok": True, "goal": goal}, ensure_ascii=False, indent=2)
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def handle_goal_list(status: str = "", include_closed: bool = False, limit: int = 20) -> str:
|
|
232
|
+
"""List durable goals so objectives do not collapse into loose followups."""
|
|
233
|
+
goals = list_workflow_goals(
|
|
234
|
+
status=(status or "").strip().lower(),
|
|
235
|
+
include_closed=bool(include_closed),
|
|
236
|
+
limit=max(1, int(limit or 20)),
|
|
237
|
+
)
|
|
238
|
+
return json.dumps(
|
|
239
|
+
{
|
|
240
|
+
"ok": True,
|
|
241
|
+
"count": len(goals),
|
|
242
|
+
"goals": [
|
|
243
|
+
{
|
|
244
|
+
"goal_id": goal["goal_id"],
|
|
245
|
+
"session_id": goal.get("session_id", ""),
|
|
246
|
+
"title": goal["title"],
|
|
247
|
+
"status": goal["status"],
|
|
248
|
+
"priority": goal.get("priority", "normal"),
|
|
249
|
+
"parent_goal_id": goal.get("parent_goal_id", ""),
|
|
250
|
+
"next_action": goal.get("next_action", ""),
|
|
251
|
+
"blocker_reason": goal.get("blocker_reason", ""),
|
|
252
|
+
"run_count": goal.get("run_count", 0),
|
|
253
|
+
"open_run_count": goal.get("open_run_count", 0),
|
|
254
|
+
"child_count": goal.get("child_count", 0),
|
|
255
|
+
"updated_at": goal["updated_at"],
|
|
256
|
+
}
|
|
257
|
+
for goal in goals
|
|
258
|
+
],
|
|
259
|
+
},
|
|
260
|
+
ensure_ascii=False,
|
|
261
|
+
indent=2,
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def handle_workflow_update(
|
|
266
|
+
run_id: str,
|
|
267
|
+
step_key: str = "",
|
|
268
|
+
step_title: str = "",
|
|
269
|
+
step_status: str = "",
|
|
270
|
+
run_status: str = "",
|
|
271
|
+
checkpoint_label: str = "",
|
|
272
|
+
summary: str = "",
|
|
273
|
+
shared_state: str = "",
|
|
274
|
+
state_patch: str = "",
|
|
275
|
+
evidence: str = "",
|
|
276
|
+
next_action: str = "",
|
|
277
|
+
retry_after: str = "",
|
|
278
|
+
max_retries: int = 0,
|
|
279
|
+
retry_policy: str = "",
|
|
280
|
+
requires_approval: bool = False,
|
|
281
|
+
compensation: str = "",
|
|
282
|
+
actor: str = "",
|
|
283
|
+
owner: str = "",
|
|
284
|
+
) -> str:
|
|
285
|
+
"""Update a workflow run, recording a replayable checkpoint and step state."""
|
|
286
|
+
clean_run_id = (run_id or "").strip()
|
|
287
|
+
if not clean_run_id:
|
|
288
|
+
return json.dumps({"ok": False, "error": "run_id is required"}, ensure_ascii=False, indent=2)
|
|
289
|
+
|
|
290
|
+
run = record_workflow_transition(
|
|
291
|
+
clean_run_id,
|
|
292
|
+
step_key=(step_key or "").strip(),
|
|
293
|
+
step_title=(step_title or "").strip(),
|
|
294
|
+
step_status=(step_status or "").strip().lower(),
|
|
295
|
+
run_status=(run_status or "").strip().lower(),
|
|
296
|
+
checkpoint_label=(checkpoint_label or "").strip(),
|
|
297
|
+
summary=(summary or "").strip(),
|
|
298
|
+
shared_state=_parse_json_object(shared_state) if str(shared_state).strip() else None,
|
|
299
|
+
state_patch=_parse_json_object(state_patch) if str(state_patch).strip() else {},
|
|
300
|
+
evidence=(evidence or "").strip(),
|
|
301
|
+
next_action=(next_action or "").strip(),
|
|
302
|
+
retry_after=(retry_after or "").strip(),
|
|
303
|
+
max_retries=max(0, int(max_retries or 0)),
|
|
304
|
+
retry_policy=(retry_policy or "").strip(),
|
|
305
|
+
requires_approval=requires_approval,
|
|
306
|
+
compensation=(compensation or "").strip(),
|
|
307
|
+
actor=(actor or "").strip(),
|
|
308
|
+
owner=(owner or "").strip(),
|
|
309
|
+
)
|
|
310
|
+
if not run:
|
|
311
|
+
return json.dumps({"ok": False, "error": f"Unknown run_id: {clean_run_id}"}, ensure_ascii=False, indent=2)
|
|
312
|
+
|
|
313
|
+
resume = get_workflow_resume_state(clean_run_id) or {}
|
|
314
|
+
response = {
|
|
315
|
+
"ok": True,
|
|
316
|
+
"run_id": clean_run_id,
|
|
317
|
+
"status": run["status"],
|
|
318
|
+
"current_step_key": run.get("current_step_key", ""),
|
|
319
|
+
"last_checkpoint_label": run.get("last_checkpoint_label", ""),
|
|
320
|
+
"next_action": run.get("next_action", ""),
|
|
321
|
+
"resume_state": resume.get("resume_state", ""),
|
|
322
|
+
"resume_message": resume.get("message", ""),
|
|
323
|
+
}
|
|
324
|
+
if resume.get("next_step"):
|
|
325
|
+
response["next_step"] = {
|
|
326
|
+
"step_key": resume["next_step"]["step_key"],
|
|
327
|
+
"title": resume["next_step"]["title"],
|
|
328
|
+
"status": resume["next_step"]["status"],
|
|
329
|
+
"attempt_count": resume["next_step"].get("attempt_count", 0),
|
|
330
|
+
"max_retries": resume["next_step"].get("max_retries", 0),
|
|
331
|
+
}
|
|
332
|
+
return json.dumps(response, ensure_ascii=False, indent=2)
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def handle_workflow_get(run_id: str, include_steps: bool = True, checkpoint_limit: int = 8) -> str:
|
|
336
|
+
"""Read the full durable workflow state, including shared state and recent actors."""
|
|
337
|
+
clean_run_id = (run_id or "").strip()
|
|
338
|
+
if not clean_run_id:
|
|
339
|
+
return json.dumps({"ok": False, "error": "run_id is required"}, ensure_ascii=False, indent=2)
|
|
340
|
+
run = get_workflow_run(clean_run_id, include_steps=bool(include_steps))
|
|
341
|
+
if not run:
|
|
342
|
+
return json.dumps({"ok": False, "error": f"Unknown run_id: {clean_run_id}"}, ensure_ascii=False, indent=2)
|
|
343
|
+
|
|
344
|
+
replay = get_workflow_replay(clean_run_id, limit=max(1, int(checkpoint_limit or 8)))
|
|
345
|
+
resume = get_workflow_resume_state(clean_run_id) or {}
|
|
346
|
+
recent_actors: list[str] = []
|
|
347
|
+
for item in replay:
|
|
348
|
+
actor = (item.get("actor") or "").strip()
|
|
349
|
+
if actor and actor not in recent_actors:
|
|
350
|
+
recent_actors.append(actor)
|
|
351
|
+
|
|
352
|
+
return json.dumps(
|
|
353
|
+
{
|
|
354
|
+
"ok": True,
|
|
355
|
+
"run": run,
|
|
356
|
+
"resume_state": resume.get("resume_state", ""),
|
|
357
|
+
"can_resume": resume.get("can_resume", False),
|
|
358
|
+
"requires_approval": resume.get("requires_approval", False),
|
|
359
|
+
"recent_actors": recent_actors,
|
|
360
|
+
"checkpoints": [
|
|
361
|
+
{
|
|
362
|
+
"created_at": item["created_at"],
|
|
363
|
+
"checkpoint_label": item["checkpoint_label"],
|
|
364
|
+
"step_key": item["step_key"],
|
|
365
|
+
"run_status": item["run_status"],
|
|
366
|
+
"step_status": item["step_status"],
|
|
367
|
+
"summary": item["summary"],
|
|
368
|
+
"next_action": item["next_action"],
|
|
369
|
+
"actor": item.get("actor", ""),
|
|
370
|
+
}
|
|
371
|
+
for item in replay
|
|
372
|
+
],
|
|
373
|
+
},
|
|
374
|
+
ensure_ascii=False,
|
|
375
|
+
indent=2,
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
def handle_workflow_handoff(
|
|
380
|
+
run_id: str,
|
|
381
|
+
actor: str,
|
|
382
|
+
next_action: str = "",
|
|
383
|
+
handoff_note: str = "",
|
|
384
|
+
shared_state: str = "",
|
|
385
|
+
new_owner: str = "",
|
|
386
|
+
) -> str:
|
|
387
|
+
"""Record a durable handoff so another agent/client can resume honestly."""
|
|
388
|
+
clean_run_id = (run_id or "").strip()
|
|
389
|
+
clean_actor = (actor or "").strip()
|
|
390
|
+
if not clean_run_id:
|
|
391
|
+
return json.dumps({"ok": False, "error": "run_id is required"}, ensure_ascii=False, indent=2)
|
|
392
|
+
if not clean_actor:
|
|
393
|
+
return json.dumps({"ok": False, "error": "actor is required"}, ensure_ascii=False, indent=2)
|
|
394
|
+
|
|
395
|
+
handoff_summary = (handoff_note or f"Handoff recorded by {clean_actor}.").strip()
|
|
396
|
+
run = record_workflow_transition(
|
|
397
|
+
clean_run_id,
|
|
398
|
+
checkpoint_label="handoff",
|
|
399
|
+
summary=handoff_summary,
|
|
400
|
+
shared_state=_parse_json_object(shared_state) if str(shared_state).strip() else None,
|
|
401
|
+
next_action=(next_action or "").strip(),
|
|
402
|
+
actor=clean_actor,
|
|
403
|
+
owner=(new_owner or "").strip(),
|
|
404
|
+
)
|
|
405
|
+
if not run:
|
|
406
|
+
return json.dumps({"ok": False, "error": f"Unknown run_id: {clean_run_id}"}, ensure_ascii=False, indent=2)
|
|
407
|
+
|
|
408
|
+
return json.dumps(
|
|
409
|
+
{
|
|
410
|
+
"ok": True,
|
|
411
|
+
"run_id": clean_run_id,
|
|
412
|
+
"status": run["status"],
|
|
413
|
+
"owner": run.get("owner", ""),
|
|
414
|
+
"next_action": run.get("next_action", ""),
|
|
415
|
+
"shared_state": run.get("shared_state", {}),
|
|
416
|
+
"message": f"Handoff checkpoint recorded by {clean_actor}.",
|
|
417
|
+
},
|
|
418
|
+
ensure_ascii=False,
|
|
419
|
+
indent=2,
|
|
420
|
+
)
|
|
421
|
+
|
|
422
|
+
|
|
423
|
+
def handle_workflow_compensation(run_id: str, checkpoint_limit: int = 10) -> str:
|
|
424
|
+
"""Return the compensation / rollback plan for a partially completed workflow run."""
|
|
425
|
+
clean_run_id = (run_id or "").strip()
|
|
426
|
+
if not clean_run_id:
|
|
427
|
+
return json.dumps({"ok": False, "error": "run_id is required"}, ensure_ascii=False, indent=2)
|
|
428
|
+
run = get_workflow_run(clean_run_id, include_steps=True)
|
|
429
|
+
if not run:
|
|
430
|
+
return json.dumps({"ok": False, "error": f"Unknown run_id: {clean_run_id}"}, ensure_ascii=False, indent=2)
|
|
431
|
+
|
|
432
|
+
steps = [
|
|
433
|
+
{
|
|
434
|
+
"step_key": step["step_key"],
|
|
435
|
+
"title": step["title"],
|
|
436
|
+
"status": step["status"],
|
|
437
|
+
"compensation": step.get("compensation", ""),
|
|
438
|
+
"attempt_count": step.get("attempt_count", 0),
|
|
439
|
+
"last_summary": step.get("last_summary", ""),
|
|
440
|
+
}
|
|
441
|
+
for step in reversed(run.get("steps") or [])
|
|
442
|
+
if step.get("compensation")
|
|
443
|
+
and step.get("status") in {"completed", "running", "retrying", "failed", "blocked", "waiting_approval"}
|
|
444
|
+
]
|
|
445
|
+
replay = get_workflow_replay(clean_run_id, limit=max(1, int(checkpoint_limit or 10)))
|
|
446
|
+
recent_compensations = [
|
|
447
|
+
{
|
|
448
|
+
"created_at": item["created_at"],
|
|
449
|
+
"checkpoint_label": item["checkpoint_label"],
|
|
450
|
+
"step_key": item["step_key"],
|
|
451
|
+
"summary": item["summary"],
|
|
452
|
+
"compensation_note": item.get("compensation_note", ""),
|
|
453
|
+
"actor": item.get("actor", ""),
|
|
454
|
+
}
|
|
455
|
+
for item in replay
|
|
456
|
+
if item.get("compensation_note")
|
|
457
|
+
]
|
|
458
|
+
|
|
459
|
+
return json.dumps(
|
|
460
|
+
{
|
|
461
|
+
"ok": True,
|
|
462
|
+
"run_id": clean_run_id,
|
|
463
|
+
"status": run["status"],
|
|
464
|
+
"owner": run.get("owner", ""),
|
|
465
|
+
"current_step_key": run.get("current_step_key", ""),
|
|
466
|
+
"compensation_steps": steps,
|
|
467
|
+
"recent_compensation_notes": recent_compensations,
|
|
468
|
+
"recommended_action": (
|
|
469
|
+
"Execute compensation steps in listed order before cancelling or reopening the run."
|
|
470
|
+
if steps
|
|
471
|
+
else "No compensation steps registered for this workflow."
|
|
472
|
+
),
|
|
473
|
+
},
|
|
474
|
+
ensure_ascii=False,
|
|
475
|
+
indent=2,
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
def handle_workflow_resume(run_id: str) -> str:
|
|
480
|
+
"""Summarize what to do next for an active workflow run."""
|
|
481
|
+
clean_run_id = (run_id or "").strip()
|
|
482
|
+
if not clean_run_id:
|
|
483
|
+
return json.dumps({"ok": False, "error": "run_id is required"}, ensure_ascii=False, indent=2)
|
|
484
|
+
resume = get_workflow_resume_state(clean_run_id)
|
|
485
|
+
if not resume:
|
|
486
|
+
return json.dumps({"ok": False, "error": f"Unknown run_id: {clean_run_id}"}, ensure_ascii=False, indent=2)
|
|
487
|
+
|
|
488
|
+
run = resume["run"]
|
|
489
|
+
response = {
|
|
490
|
+
"ok": True,
|
|
491
|
+
"run_id": run["run_id"],
|
|
492
|
+
"status": run["status"],
|
|
493
|
+
"resume_state": resume["resume_state"],
|
|
494
|
+
"can_resume": resume["can_resume"],
|
|
495
|
+
"requires_approval": resume["requires_approval"],
|
|
496
|
+
"message": resume["message"],
|
|
497
|
+
"current_step_key": run.get("current_step_key", ""),
|
|
498
|
+
"next_action": run.get("next_action", ""),
|
|
499
|
+
}
|
|
500
|
+
if resume.get("next_step"):
|
|
501
|
+
response["next_step"] = {
|
|
502
|
+
"step_key": resume["next_step"]["step_key"],
|
|
503
|
+
"title": resume["next_step"]["title"],
|
|
504
|
+
"status": resume["next_step"]["status"],
|
|
505
|
+
"attempt_count": resume["next_step"].get("attempt_count", 0),
|
|
506
|
+
"max_retries": resume["next_step"].get("max_retries", 0),
|
|
507
|
+
"retry_after": resume["next_step"].get("retry_after", ""),
|
|
508
|
+
}
|
|
509
|
+
return json.dumps(response, ensure_ascii=False, indent=2)
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
def handle_workflow_replay(run_id: str, limit: int = 20) -> str:
|
|
513
|
+
"""Show the latest replayable checkpoints for a workflow run."""
|
|
514
|
+
clean_run_id = (run_id or "").strip()
|
|
515
|
+
if not clean_run_id:
|
|
516
|
+
return json.dumps({"ok": False, "error": "run_id is required"}, ensure_ascii=False, indent=2)
|
|
517
|
+
replay = get_workflow_replay(clean_run_id, limit=max(1, int(limit or 20)))
|
|
518
|
+
if not replay:
|
|
519
|
+
return json.dumps({"ok": False, "error": f"No replay data for {clean_run_id}"}, ensure_ascii=False, indent=2)
|
|
520
|
+
|
|
521
|
+
items = []
|
|
522
|
+
for checkpoint in replay:
|
|
523
|
+
items.append(
|
|
524
|
+
{
|
|
525
|
+
"created_at": checkpoint["created_at"],
|
|
526
|
+
"checkpoint_label": checkpoint["checkpoint_label"],
|
|
527
|
+
"step_key": checkpoint["step_key"],
|
|
528
|
+
"run_status": checkpoint["run_status"],
|
|
529
|
+
"step_status": checkpoint["step_status"],
|
|
530
|
+
"attempt": checkpoint["attempt"],
|
|
531
|
+
"summary": checkpoint["summary"],
|
|
532
|
+
"next_action": checkpoint["next_action"],
|
|
533
|
+
"requires_approval": checkpoint["requires_approval"],
|
|
534
|
+
}
|
|
535
|
+
)
|
|
536
|
+
return json.dumps(
|
|
537
|
+
{"ok": True, "run_id": clean_run_id, "count": len(items), "checkpoints": items},
|
|
538
|
+
ensure_ascii=False,
|
|
539
|
+
indent=2,
|
|
540
|
+
)
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
def handle_workflow_list(status: str = "", include_closed: bool = False, limit: int = 20) -> str:
|
|
544
|
+
"""List durable workflow runs so blocked/active work does not disappear into notes."""
|
|
545
|
+
runs = list_workflow_runs(
|
|
546
|
+
status=(status or "").strip().lower(),
|
|
547
|
+
include_closed=bool(include_closed),
|
|
548
|
+
limit=max(1, int(limit or 20)),
|
|
549
|
+
)
|
|
550
|
+
return json.dumps(
|
|
551
|
+
{
|
|
552
|
+
"ok": True,
|
|
553
|
+
"count": len(runs),
|
|
554
|
+
"runs": [
|
|
555
|
+
{
|
|
556
|
+
"run_id": run["run_id"],
|
|
557
|
+
"session_id": run.get("session_id", ""),
|
|
558
|
+
"goal_id": run.get("goal_id", ""),
|
|
559
|
+
"goal": run["goal"],
|
|
560
|
+
"workflow_kind": run.get("workflow_kind", ""),
|
|
561
|
+
"status": run["status"],
|
|
562
|
+
"priority": run.get("priority", "normal"),
|
|
563
|
+
"current_step_key": run.get("current_step_key", ""),
|
|
564
|
+
"next_action": run.get("next_action", ""),
|
|
565
|
+
"updated_at": run["updated_at"],
|
|
566
|
+
}
|
|
567
|
+
for run in runs
|
|
568
|
+
],
|
|
569
|
+
},
|
|
570
|
+
ensure_ascii=False,
|
|
571
|
+
indent=2,
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
TOOLS = [
|
|
576
|
+
(handle_goal_open, "nexo_goal_open", "Open a durable goal so multi-session objectives stay explicit instead of dissolving into notes."),
|
|
577
|
+
(handle_goal_update, "nexo_goal_update", "Update a durable goal with active/blocked/abandoned/completed state and next action."),
|
|
578
|
+
(handle_goal_get, "nexo_goal_get", "Read a durable goal and optionally include linked workflow runs."),
|
|
579
|
+
(handle_goal_list, "nexo_goal_list", "List durable goals so active, blocked, and abandoned objectives stay visible."),
|
|
580
|
+
(handle_workflow_open, "nexo_workflow_open", "Open a durable workflow run for long multi-step or cross-session work."),
|
|
581
|
+
(handle_workflow_update, "nexo_workflow_update", "Update a workflow run with replayable checkpoints, step status, retry metadata, and shared state."),
|
|
582
|
+
(handle_workflow_get, "nexo_workflow_get", "Read the full durable workflow state, including shared_state, recent actors, and replayable checkpoints."),
|
|
583
|
+
(handle_workflow_handoff, "nexo_workflow_handoff", "Record a durable handoff so another agent or client can resume the workflow honestly."),
|
|
584
|
+
(handle_workflow_compensation, "nexo_workflow_compensation", "Show the rollback / compensation plan for a partially completed workflow run."),
|
|
585
|
+
(handle_workflow_resume, "nexo_workflow_resume", "Summarize the next actionable step for a workflow run, including retry or approval gates."),
|
|
586
|
+
(handle_workflow_replay, "nexo_workflow_replay", "Replay the latest checkpoints of a workflow run so interrupted execution can resume honestly."),
|
|
587
|
+
(handle_workflow_list, "nexo_workflow_list", "List durable workflow runs so active, blocked, and resumable work stays visible."),
|
|
588
|
+
]
|