Repository navigation
Expand file tree
/
Copy pathruntime.py
More file actions
278 lines (227 loc) · 8.09 KB
/
Copy pathruntime.py
File metadata and controls
278 lines (227 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""Workflow runtime contract (Phase 6.4 of pi-runner change).
Defines the base Workflow class, WorkflowContext and WorkflowResult dataclasses,
and the JSON reliability policy shared by all bridge-owned workflows.
Per design doc `command-runtime-plan.md` §JSON Reliability Policy:
1. Strict JSON request.
2. Retry with validation error feedback.
3. Fenced ```json block extraction.
4. Fail closed — no file mutation.
The runtime is Workflow-agnostic: PlanWorkflow (Phase 6.4), MemoryGcWorkflow
(6.5), DoneWorkflow (6.7) all plug in via the same base class.
"""
from __future__ import annotations
import json
import logging
import re
import time
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Callable, Optional, Protocol
log = logging.getLogger("feishu-bridge")
# ---- state enum -------------------------------------------------------
STATE_DRAFT = "draft"
STATE_WAITING_CONFIRMATION = "waiting_confirmation"
STATE_COMPLETED = "completed"
STATE_FAILED = "failed"
STATE_EXPIRED = "expired"
STATE_CANCELLED = "cancelled"
ALL_STATES = frozenset({
STATE_DRAFT,
STATE_WAITING_CONFIRMATION,
STATE_COMPLETED,
STATE_FAILED,
STATE_EXPIRED,
STATE_CANCELLED,
})
TERMINAL_STATES = frozenset({
STATE_COMPLETED,
STATE_FAILED,
STATE_EXPIRED,
STATE_CANCELLED,
})
# ---- TTL parsing ------------------------------------------------------
_TTL_RE = re.compile(r"^\s*(\d+)\s*([smhdw]?)\s*$", re.IGNORECASE)
_TTL_UNITS = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800, "": 1}
def parse_ttl_seconds(ttl: str | None, default_seconds: int) -> int:
"""Parse a ttl string like `7d`, `24h`, `300s`. Falls back on parse error."""
if not ttl:
return default_seconds
match = _TTL_RE.match(str(ttl))
if not match:
return default_seconds
qty, unit = match.group(1), (match.group(2) or "").lower()
try:
return int(qty) * _TTL_UNITS[unit]
except (KeyError, ValueError):
return default_seconds
# ---- dataclasses ------------------------------------------------------
@dataclass
class WorkflowContext:
"""Per-invocation runtime context supplied by the bridge command handler.
Fields are populated from the incoming message item + bot state. Workflows
should treat this as read-only and pass it through to nested helpers.
"""
bot_id: str
chat_id: str
thread_id: str | None
sender_id: str
chat_type: str
message_id: str | None
workspace: Path
runner: Any # BaseRunner; typed Any to avoid cyclic import at module load
runner_type: str
handle: Any # ResponseHandle
journal: Any # SessionJournal | None
session_id: str | None
agents_home: Path
skill_dir: Path # ~/.agents/skills/<name>/
now: float = field(default_factory=time.time)
@property
def scope_key(self) -> str:
return f"{self.bot_id}|{self.chat_id}|{self.thread_id or ''}"
@dataclass
class WorkflowResult:
"""Outcome of a single Workflow.start() or Workflow.resume() call."""
state: str
user_message: str = ""
next_expected_input: str | None = None
expires_at: float | None = None
artifacts: list[str] = field(default_factory=list)
error: str | None = None
payload: dict = field(default_factory=dict)
def __post_init__(self) -> None:
if self.state not in ALL_STATES:
raise ValueError(
f"invalid workflow state {self.state!r}; expected one of {sorted(ALL_STATES)}"
)
class Workflow(Protocol):
"""Structural contract; PlanWorkflow implements via duck typing."""
name: str
version: int
def start(self, ctx: WorkflowContext, arg: str) -> WorkflowResult: ...
def resume(
self, ctx: WorkflowContext, user_text: str, payload: dict,
) -> WorkflowResult: ...
# ---- JSON reliability policy -----------------------------------------
_FENCED_JSON_RE = re.compile(
r"```(?:json)?\s*\n(.*?)\n```", re.DOTALL | re.IGNORECASE,
)
class JsonPolicyError(Exception):
"""Raised when all 3 strikes of the JSON policy have been exhausted."""
def __init__(self, message: str, last_output: str = "") -> None:
super().__init__(message)
self.last_output = last_output
def _extract_json_object(text: str) -> dict | None:
"""Try to parse `text` as JSON, or extract first ```json block."""
if not text:
return None
stripped = text.strip()
# Direct parse
try:
data = json.loads(stripped)
return data if isinstance(data, dict) else None
except json.JSONDecodeError:
pass
# Fenced-block fallback
match = _FENCED_JSON_RE.search(text)
if match:
try:
data = json.loads(match.group(1))
return data if isinstance(data, dict) else None
except json.JSONDecodeError:
return None
return None
def _validate_shape(
obj: dict, schema_required: list[str],
) -> list[str]:
"""Return list of missing/invalid field names; empty list = valid."""
missing: list[str] = []
for key in schema_required:
value = obj.get(key)
if value is None:
missing.append(key)
continue
if isinstance(value, str) and not value.strip():
missing.append(key)
return missing
def request_json_with_policy(
*,
runner_call: Callable[[str], dict],
base_prompt: str,
schema_required: list[str],
max_attempts: int = 3,
log_prefix: str = "workflow",
) -> dict:
"""Run the 3-strike JSON policy against a callable that wraps runner.run().
`runner_call(prompt_text)` must return a dict with keys `result` (str) and
`is_error` (bool). It is re-invoked on each retry with a different prompt.
Returns the validated JSON dict on success. Raises JsonPolicyError on
exhaustion — caller must fail closed without mutation.
"""
last_output = ""
last_error = "no attempt made"
for attempt in range(1, max_attempts + 1):
if attempt == 1:
prompt = base_prompt
elif attempt == 2:
prompt = (
f"{base_prompt}\n\n"
f"Your previous reply did not parse as valid JSON matching the "
f"schema. Error: {last_error}\n"
f"Reply ONLY with the corrected JSON object — no prose, no "
f"markdown fences."
)
else:
prompt = (
f"{base_prompt}\n\n"
f"Your previous replies were invalid. Last error: {last_error}\n"
f"You MAY wrap the JSON in a ```json ... ``` fenced block. "
f"Reply with nothing else."
)
result = runner_call(prompt)
text = str(result.get("result", "") or "")
last_output = text
if result.get("is_error"):
last_error = (text[:500] or "runner returned is_error=True").strip()
log.warning(
"%s: JSON attempt %d runner error: %s",
log_prefix, attempt, last_error[:200],
)
continue
obj = _extract_json_object(text)
if obj is None:
last_error = "response could not be parsed as JSON object"
log.warning(
"%s: JSON attempt %d parse failed; first 200 chars: %r",
log_prefix, attempt, text[:200],
)
continue
missing = _validate_shape(obj, schema_required)
if missing:
last_error = f"missing/empty required fields: {', '.join(missing)}"
log.warning(
"%s: JSON attempt %d shape invalid: %s",
log_prefix, attempt, last_error,
)
continue
return obj
raise JsonPolicyError(
f"3-strike JSON policy exhausted: {last_error}",
last_output=last_output,
)
__all__ = [
"STATE_DRAFT",
"STATE_WAITING_CONFIRMATION",
"STATE_COMPLETED",
"STATE_FAILED",
"STATE_EXPIRED",
"STATE_CANCELLED",
"ALL_STATES",
"TERMINAL_STATES",
"WorkflowContext",
"WorkflowResult",
"Workflow",
"JsonPolicyError",
"parse_ttl_seconds",
"request_json_with_policy",
]