Olyder 0.5.0b1__py3-none-any.whl
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.
- olyder/__init__.py +2 -0
- olyder/__main__.py +3 -0
- olyder/accounting.py +130 -0
- olyder/acp_bridge.py +157 -0
- olyder/adapters.py +142 -0
- olyder/agents.py +130 -0
- olyder/analysis.py +57 -0
- olyder/api.py +68 -0
- olyder/backends.py +82 -0
- olyder/benchmark.py +119 -0
- olyder/checks.py +232 -0
- olyder/cli.py +249 -0
- olyder/config.py +197 -0
- olyder/contracts.py +50 -0
- olyder/control.py +55 -0
- olyder/data/SKILL.md +21 -0
- olyder/demo.py +34 -0
- olyder/diagnostics.py +27 -0
- olyder/engine.py +319 -0
- olyder/execution.py +152 -0
- olyder/idea.py +32 -0
- olyder/impact.py +40 -0
- olyder/integrity.py +148 -0
- olyder/interfaces.py +60 -0
- olyder/ledger.py +159 -0
- olyder/mcp.py +114 -0
- olyder/models.py +127 -0
- olyder/plugins.py +63 -0
- olyder/policy.py +32 -0
- olyder/privacy.py +35 -0
- olyder/quota.py +74 -0
- olyder/report.py +56 -0
- olyder/research.py +59 -0
- olyder/sdk.py +482 -0
- olyder/skill_install.py +85 -0
- olyder/state.py +155 -0
- olyder/workspace.py +318 -0
- olyder-0.5.0b1.dist-info/METADATA +157 -0
- olyder-0.5.0b1.dist-info/RECORD +43 -0
- olyder-0.5.0b1.dist-info/WHEEL +5 -0
- olyder-0.5.0b1.dist-info/entry_points.txt +2 -0
- olyder-0.5.0b1.dist-info/licenses/LICENSE +202 -0
- olyder-0.5.0b1.dist-info/top_level.txt +1 -0
olyder/__init__.py
ADDED
olyder/__main__.py
ADDED
olyder/accounting.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Runtime monetary admission; CLI/client estimates never become invoices."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from decimal import Decimal
|
|
6
|
+
from functools import wraps
|
|
7
|
+
from threading import RLock
|
|
8
|
+
from uuid import uuid4
|
|
9
|
+
|
|
10
|
+
from .ledger import BudgetLedger, money
|
|
11
|
+
from .quota import QuotaExceeded
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def synchronized(method):
|
|
15
|
+
@wraps(method)
|
|
16
|
+
def locked(self,*args,**kwargs):
|
|
17
|
+
with self._lock:
|
|
18
|
+
return method(self,*args,**kwargs)
|
|
19
|
+
return locked
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class PricingSnapshot:
|
|
24
|
+
provider: str
|
|
25
|
+
model: str
|
|
26
|
+
date: str
|
|
27
|
+
input_per_million_usd: str
|
|
28
|
+
output_per_million_usd: str
|
|
29
|
+
cached_per_million_usd: str | None = None
|
|
30
|
+
schema_version: int = 1
|
|
31
|
+
|
|
32
|
+
def __post_init__(self):
|
|
33
|
+
from datetime import date
|
|
34
|
+
date.fromisoformat(self.date)
|
|
35
|
+
if any(not isinstance(v,str) or not v for v in (self.provider,self.model)):
|
|
36
|
+
raise ValueError('Pricing provider and model must be explicit names')
|
|
37
|
+
if self.schema_version!=1:
|
|
38
|
+
raise ValueError('Unsupported pricing schema')
|
|
39
|
+
money(self.input_per_million_usd);money(self.output_per_million_usd)
|
|
40
|
+
if self.cached_per_million_usd is not None:
|
|
41
|
+
money(self.cached_per_million_usd)
|
|
42
|
+
|
|
43
|
+
def estimate(self, usage):
|
|
44
|
+
inp, out = usage.get('input_tokens'), usage.get('output_tokens')
|
|
45
|
+
cached = usage.get('cached_tokens')
|
|
46
|
+
if not usage.get('usage_complete') or any(type(x) is not int or x < 0 for x in (inp,out,cached)) or cached > inp:
|
|
47
|
+
return None
|
|
48
|
+
if cached and self.cached_per_million_usd is None:
|
|
49
|
+
return None
|
|
50
|
+
cost = ((inp-cached)*money(self.input_per_million_usd) +
|
|
51
|
+
out*money(self.output_per_million_usd) +
|
|
52
|
+
cached*money(self.cached_per_million_usd or '0')) / Decimal(1000000)
|
|
53
|
+
return str(cost)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class RuntimeAccounting:
|
|
57
|
+
def __init__(self, run_id, config=None):
|
|
58
|
+
self.config = config or {}
|
|
59
|
+
c = self.config
|
|
60
|
+
self.ledger = BudgetLedger(run_id, c['max_cost_usd'],
|
|
61
|
+
verifier_reserve=c.get('verifier_reserve_usd','0'), strict=c.get('strict',False)) if c.get('max_cost_usd') is not None else None
|
|
62
|
+
self.observations = []
|
|
63
|
+
self.pending = {}
|
|
64
|
+
self._lock=RLock()
|
|
65
|
+
|
|
66
|
+
@synchronized
|
|
67
|
+
def admit(self, role):
|
|
68
|
+
request_id = str(uuid4())
|
|
69
|
+
fields={'builder':'builder_upper_bound_usd','critic':'verifier_reserve_usd',
|
|
70
|
+
'planner':'planner_upper_bound_usd','research':'research_upper_bound_usd'}
|
|
71
|
+
if role not in fields:
|
|
72
|
+
raise ValueError('Unknown resource role')
|
|
73
|
+
upper=self.config.get(fields[role])
|
|
74
|
+
if self.ledger:
|
|
75
|
+
if upper is None or money(upper) == 0:
|
|
76
|
+
raise QuotaExceeded(f'No positive reviewed upper bound for {role}; spend cannot be bounded')
|
|
77
|
+
if role == 'critic':
|
|
78
|
+
self.ledger.reserve_verifier(request_id, upper)
|
|
79
|
+
else:
|
|
80
|
+
self.ledger.reserve(request_id, upper)
|
|
81
|
+
self.pending[request_id] = role
|
|
82
|
+
return request_id
|
|
83
|
+
|
|
84
|
+
@synchronized
|
|
85
|
+
def settle(self, request_id, usage=None):
|
|
86
|
+
if usage is not None and not isinstance(usage,dict):
|
|
87
|
+
raise ValueError('Usage observation must be an object')
|
|
88
|
+
allowed={'provider','model','model_origin','input_tokens','output_tokens','cached_tokens',
|
|
89
|
+
'reasoning_tokens','observed_tokens','usage_complete','estimated_cost_usd',
|
|
90
|
+
'origin','elapsed_seconds','status','usage_events','malformed_events','reasoning_accounting'}
|
|
91
|
+
observation = {k:v for k,v in (usage or {}).items() if k in allowed}
|
|
92
|
+
from .privacy import redact
|
|
93
|
+
for key,value in list(observation.items()):
|
|
94
|
+
if isinstance(value,str):
|
|
95
|
+
observation[key]=redact(value)[:500]
|
|
96
|
+
estimate=observation.get('estimated_cost_usd')
|
|
97
|
+
if estimate is not None:
|
|
98
|
+
try:
|
|
99
|
+
observation['estimated_cost_usd']=str(money(estimate))
|
|
100
|
+
except ValueError:
|
|
101
|
+
observation['estimated_cost_usd']=None
|
|
102
|
+
signature=hashlib.sha256(json.dumps(observation,sort_keys=True).encode()).hexdigest()
|
|
103
|
+
if request_id not in self.pending:
|
|
104
|
+
previous=next((o for o in self.observations if o['request_id']==request_id),None)
|
|
105
|
+
if previous is not None and previous.get('settlement_digest')==signature:
|
|
106
|
+
return
|
|
107
|
+
raise ValueError('Foreign or conflicting settlement callback')
|
|
108
|
+
role=self.pending.pop(request_id)
|
|
109
|
+
# No trusted billing channel is implemented by CLI agents. Even their
|
|
110
|
+
# self-reported cost is observed/estimated, never a reconciled invoice.
|
|
111
|
+
observation.update(request_id=request_id, role=role, billed_cost_usd=None,settlement_digest=signature)
|
|
112
|
+
observation.setdefault('origin','unavailable')
|
|
113
|
+
observation.setdefault('usage_complete',False)
|
|
114
|
+
observation.setdefault('observed_tokens',None)
|
|
115
|
+
observation.setdefault('estimated_cost_usd',None)
|
|
116
|
+
pricing = self.config.get('pricing')
|
|
117
|
+
if pricing and observation.get('provider') == pricing.get('provider') and observation.get('model') == pricing.get('model'):
|
|
118
|
+
observation['estimated_cost_usd'] = PricingSnapshot(**pricing).estimate(observation)
|
|
119
|
+
observation['pricing_snapshot'] = dict(pricing)
|
|
120
|
+
self.observations.append(observation)
|
|
121
|
+
if self.ledger:
|
|
122
|
+
self.ledger.settle(request_id, None)
|
|
123
|
+
|
|
124
|
+
@synchronized
|
|
125
|
+
def snapshot(self):
|
|
126
|
+
return {'ledger':self.ledger.snapshot() if self.ledger else None,
|
|
127
|
+
'ledger_checkpoint':self.ledger.checkpoint() if self.ledger else None,
|
|
128
|
+
'observations':list(self.observations), 'pending_requests':dict(self.pending),
|
|
129
|
+
'enforcement_kind':'local_soft',
|
|
130
|
+
'budget_verdict':'unknown' if self.pending or any(o['billed_cost_usd'] is None for o in self.observations) else 'not_applicable'}
|
olyder/acp_bridge.py
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"""Optional ACP v1 editor bridge. No IDE, model or ACP dependency in the core.
|
|
2
|
+
|
|
3
|
+
Olyder exposes only read-only deterministic plan/verify/status operations.
|
|
4
|
+
The official ACP Python SDK owns JSON-RPC transport and schema validation.
|
|
5
|
+
No builder, shell, network, paid provider or executable MCP server is accepted.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import asyncio
|
|
10
|
+
import json
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from uuid import uuid4
|
|
13
|
+
|
|
14
|
+
from acp import (
|
|
15
|
+
PROTOCOL_VERSION, Agent, InitializeResponse, NewSessionResponse,
|
|
16
|
+
PromptResponse, run_agent,
|
|
17
|
+
)
|
|
18
|
+
from acp.schema import AgentMessageChunk, TextContentBlock
|
|
19
|
+
|
|
20
|
+
from .privacy import redact, redact_values
|
|
21
|
+
|
|
22
|
+
MAX_PROMPT_CHARS = 4096
|
|
23
|
+
MAX_RESPONSE_CHARS = 12000
|
|
24
|
+
HELP = (
|
|
25
|
+
"Olyder ACP v1 (read-only): send 'plan', 'verify', 'status', or 'help'. "
|
|
26
|
+
"Planning is deterministic; verification permits file checks only. "
|
|
27
|
+
"Shell commands, network/browser tests, builders and paid models are NOT "
|
|
28
|
+
"enabled in this editor bridge. Use the reviewed Olyder CLI/SDK to "
|
|
29
|
+
"authorize other operations explicitly."
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def read_only_command(workspace: Path, text: str) -> str:
|
|
34
|
+
"""Interpret a deliberately small fixed command vocabulary, never a shell."""
|
|
35
|
+
cmd = text.strip().lower()
|
|
36
|
+
if cmd in ('', 'help', '/help'):
|
|
37
|
+
return HELP
|
|
38
|
+
if cmd == 'status':
|
|
39
|
+
from .diagnostics import doctor
|
|
40
|
+
return json.dumps(redact_values(doctor()), ensure_ascii=False)
|
|
41
|
+
if cmd not in ('plan', 'verify'):
|
|
42
|
+
return "Unsupported request. " + HELP
|
|
43
|
+
try:
|
|
44
|
+
from .config import load_spec
|
|
45
|
+
from .engine import plan, verify_once
|
|
46
|
+
from .quota import Budget
|
|
47
|
+
|
|
48
|
+
spec = load_spec(workspace / 'olyder.json')
|
|
49
|
+
if spec.root != workspace:
|
|
50
|
+
raise ValueError('Specification outside authorized workspace')
|
|
51
|
+
budget = Budget(int(spec.raw.get('limits', {}).get('max_tokens', 30000)))
|
|
52
|
+
if cmd == 'plan':
|
|
53
|
+
result = plan(spec, budget=budget, use_ai=False)
|
|
54
|
+
else:
|
|
55
|
+
result = verify_once(spec, allow_exec=False, use_ai=False,
|
|
56
|
+
allow_network=False, budget=budget)
|
|
57
|
+
response = json.dumps(redact_values(result), ensure_ascii=False)
|
|
58
|
+
if len(response) > MAX_RESPONSE_CHARS:
|
|
59
|
+
return response[:MAX_RESPONSE_CHARS] + '... [truncated; consult Olyder CLI report]'
|
|
60
|
+
return response
|
|
61
|
+
except (OSError, ValueError, KeyError, TypeError) as exc:
|
|
62
|
+
return "Olyder request failed: " + redact(type(exc).__name__)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _prompt_text(blocks: list) -> str:
|
|
66
|
+
if not isinstance(blocks, list) or not blocks:
|
|
67
|
+
return ''
|
|
68
|
+
parts = []
|
|
69
|
+
for block in blocks:
|
|
70
|
+
kind = block.get('type') if isinstance(block, dict) else getattr(block, 'type', None)
|
|
71
|
+
if kind != 'text':
|
|
72
|
+
# Do not accept resource links, images, or embedded content in the
|
|
73
|
+
# initial narrow bridge; they may contain untrusted external data.
|
|
74
|
+
raise ValueError('Only text ACP blocks are supported')
|
|
75
|
+
value = block.get('text') if isinstance(block, dict) else getattr(block, 'text', None)
|
|
76
|
+
if not isinstance(value, str):
|
|
77
|
+
raise ValueError('Invalid text block')
|
|
78
|
+
parts.append(value)
|
|
79
|
+
text = '\n'.join(parts)
|
|
80
|
+
if len(text) > MAX_PROMPT_CHARS:
|
|
81
|
+
raise ValueError('Prompt exceeds editor bridge limit')
|
|
82
|
+
return text
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class OlyderACPAgent(Agent):
|
|
86
|
+
"""One fixed project root; session IDs cannot select arbitrary workspaces."""
|
|
87
|
+
|
|
88
|
+
def __init__(self, workspace: str | Path):
|
|
89
|
+
root = Path(workspace).expanduser().resolve(strict=True)
|
|
90
|
+
if not root.is_dir():
|
|
91
|
+
raise ValueError('ACP workspace must be a directory')
|
|
92
|
+
self.workspace = root
|
|
93
|
+
self._sessions: dict[str, dict] = {}
|
|
94
|
+
self._conn = None
|
|
95
|
+
|
|
96
|
+
def on_connect(self, conn) -> None:
|
|
97
|
+
self._conn = conn
|
|
98
|
+
|
|
99
|
+
async def initialize(self, protocol_version: int, **kwargs) -> InitializeResponse:
|
|
100
|
+
# Version 1 only. Never echo a newer client's version as a false claim.
|
|
101
|
+
return InitializeResponse(protocol_version=PROTOCOL_VERSION)
|
|
102
|
+
|
|
103
|
+
async def new_session(self, cwd: str, additional_directories=None,
|
|
104
|
+
mcp_servers=None, **kwargs) -> NewSessionResponse:
|
|
105
|
+
if additional_directories or mcp_servers:
|
|
106
|
+
raise ValueError('ACP bridge does not accept external roots or MCP servers')
|
|
107
|
+
if not Path(cwd).is_absolute() or Path(cwd).resolve(strict=True) != self.workspace:
|
|
108
|
+
raise ValueError('ACP session cwd must equal authorized Olyder workspace')
|
|
109
|
+
session_id = uuid4().hex
|
|
110
|
+
self._sessions[session_id] = {'cancelled': False, 'busy': False}
|
|
111
|
+
return NewSessionResponse(session_id=session_id)
|
|
112
|
+
|
|
113
|
+
async def prompt(self, session_id: str, prompt: list, **kwargs) -> PromptResponse:
|
|
114
|
+
session = self._sessions.get(session_id)
|
|
115
|
+
if session is None:
|
|
116
|
+
raise ValueError('Unknown ACP session')
|
|
117
|
+
# Reject overlapping turns rather than allowing a stale background
|
|
118
|
+
# response to overwrite a later turn or cancellation.
|
|
119
|
+
if session['busy']:
|
|
120
|
+
raise ValueError('An ACP prompt is already running in this session')
|
|
121
|
+
session['busy'] = True
|
|
122
|
+
session['cancelled'] = False
|
|
123
|
+
try:
|
|
124
|
+
text = _prompt_text(prompt)
|
|
125
|
+
# No shell, network or paid model; cancellation is best-effort for
|
|
126
|
+
# an in-flight thread and must not emit a misleading final result.
|
|
127
|
+
response = await asyncio.to_thread(read_only_command, self.workspace, text)
|
|
128
|
+
if session['cancelled']:
|
|
129
|
+
return PromptResponse(stop_reason='cancelled')
|
|
130
|
+
if self._conn is not None:
|
|
131
|
+
await self._conn.session_update(
|
|
132
|
+
session_id=session_id,
|
|
133
|
+
update=AgentMessageChunk(
|
|
134
|
+
sessionUpdate='agent_message_chunk',
|
|
135
|
+
content=TextContentBlock(type='text', text=response),
|
|
136
|
+
),
|
|
137
|
+
)
|
|
138
|
+
return PromptResponse(stop_reason='end_turn')
|
|
139
|
+
finally:
|
|
140
|
+
session['busy'] = False
|
|
141
|
+
|
|
142
|
+
async def cancel(self, session_id: str, **kwargs) -> None:
|
|
143
|
+
session = self._sessions.get(session_id)
|
|
144
|
+
if session is not None:
|
|
145
|
+
session['cancelled'] = True
|
|
146
|
+
|
|
147
|
+
async def close_session(self, session_id: str, **kwargs):
|
|
148
|
+
session = self._sessions.pop(session_id, None)
|
|
149
|
+
if session is not None:
|
|
150
|
+
session['cancelled'] = True
|
|
151
|
+
return None
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def serve(workspace: str | Path) -> int:
|
|
155
|
+
"""Run optional SDK-backed ACP stdio server; no stdout startup chatter."""
|
|
156
|
+
asyncio.run(run_agent(OlyderACPAgent(workspace), stdio_buffer_limit_bytes=1024 * 1024))
|
|
157
|
+
return 0
|
olyder/adapters.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""Replaceable CLI adapters. Templates never imply permission auto-approval."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
from decimal import Decimal
|
|
6
|
+
|
|
7
|
+
from .agents import agent_argv, parse_agent_log
|
|
8
|
+
from .interfaces import API_VERSION, AgentAdapter, Capabilities
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ExecutableAgent:
|
|
12
|
+
api_version = API_VERSION
|
|
13
|
+
|
|
14
|
+
def __init__(self, name: str, argv: list[str], telemetry: str = 'unknown'):
|
|
15
|
+
if not name or not argv or not all(isinstance(a, str) and a and '\0' not in a for a in argv):
|
|
16
|
+
raise ValueError('Agent requires a name and explicit nonempty argv')
|
|
17
|
+
if telemetry not in ('unknown','codex','opencode','claude'):
|
|
18
|
+
raise ValueError('Unsupported agent telemetry format')
|
|
19
|
+
self.name, self.argv, self.telemetry = name, list(argv), telemetry
|
|
20
|
+
|
|
21
|
+
def command(self, prompt):
|
|
22
|
+
return [a.replace('{prompt}', prompt) for a in self.argv]
|
|
23
|
+
|
|
24
|
+
def capabilities(self):
|
|
25
|
+
return Capabilities(structured_usage=self.telemetry != 'unknown',
|
|
26
|
+
streaming=True, notes='Live event metadata only; usage is post-hoc; no provider hard limit')
|
|
27
|
+
|
|
28
|
+
def parse(self, stream):
|
|
29
|
+
if self.telemetry in ('codex', 'opencode'):
|
|
30
|
+
usage=parse_agent_log(stream, self.telemetry).snapshot()
|
|
31
|
+
if not usage['usage_events']:
|
|
32
|
+
for key in ('input_tokens','output_tokens','cached_tokens','reasoning_tokens'):
|
|
33
|
+
usage[key]=None
|
|
34
|
+
if self.telemetry=='opencode' and usage['usage_complete']:
|
|
35
|
+
usage['input_tokens']+=usage['cached_tokens']
|
|
36
|
+
base=usage['input_tokens']+usage['output_tokens']
|
|
37
|
+
if usage['observed_tokens']==base+usage['reasoning_tokens']:
|
|
38
|
+
usage['output_tokens']+=usage['reasoning_tokens']
|
|
39
|
+
usage['reasoning_accounting']='separate_output'
|
|
40
|
+
elif usage['observed_tokens']==base:
|
|
41
|
+
usage['reasoning_accounting']='included_in_output'
|
|
42
|
+
else:
|
|
43
|
+
usage['usage_complete']=False
|
|
44
|
+
usage['reasoning_accounting']='unknown'
|
|
45
|
+
return usage
|
|
46
|
+
result = {'provider': self.name, 'observed_tokens': None,
|
|
47
|
+
'input_tokens': None, 'output_tokens': None,
|
|
48
|
+
'cached_tokens': None, 'usage_complete': False,
|
|
49
|
+
'estimated_cost_usd': None, 'billed_cost_usd': None,
|
|
50
|
+
'status': 'incomplete', 'origin': 'agent_jsonl'}
|
|
51
|
+
stream.seek(0)
|
|
52
|
+
for i in range(10000):
|
|
53
|
+
line = stream.readline(1_000_001)
|
|
54
|
+
if not line:
|
|
55
|
+
break
|
|
56
|
+
if len(line) > 1_000_000:
|
|
57
|
+
break
|
|
58
|
+
try:
|
|
59
|
+
e = json.loads(line)
|
|
60
|
+
if not isinstance(e, dict):
|
|
61
|
+
continue
|
|
62
|
+
if self.telemetry == 'claude' and e.get('type') == 'result':
|
|
63
|
+
result['status'] = 'failed' if e.get('is_error') else 'completed'
|
|
64
|
+
usage = e.get('usage', {})
|
|
65
|
+
inp, out = usage.get('input_tokens'), usage.get('output_tokens')
|
|
66
|
+
if type(inp) is int and inp >= 0 and type(out) is int and out >= 0:
|
|
67
|
+
cached = usage.get('cache_read_input_tokens', 0)
|
|
68
|
+
written = usage.get('cache_creation_input_tokens', 0)
|
|
69
|
+
if type(cached) is not int or cached < 0 or type(written) is not int or written < 0:
|
|
70
|
+
continue
|
|
71
|
+
result.update(input_tokens=inp+cached+written, output_tokens=out,
|
|
72
|
+
cached_tokens=cached, observed_tokens=inp+cached+written+out,
|
|
73
|
+
usage_complete=not e.get('is_error'))
|
|
74
|
+
cost = e.get('total_cost_usd')
|
|
75
|
+
if isinstance(cost, (int, float, str)) and not isinstance(cost, bool):
|
|
76
|
+
amount = Decimal(str(cost))
|
|
77
|
+
if amount.is_finite() and amount >= 0:
|
|
78
|
+
result['estimated_cost_usd'] = str(amount)
|
|
79
|
+
except (ValueError, TypeError, AttributeError, ArithmeticError):
|
|
80
|
+
continue
|
|
81
|
+
return result
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class BuiltinAgent(ExecutableAgent):
|
|
85
|
+
def capabilities(self):
|
|
86
|
+
from dataclasses import replace
|
|
87
|
+
return replace(super().capabilities(),requires_git=self.name=='codex')
|
|
88
|
+
|
|
89
|
+
def command(self, prompt):
|
|
90
|
+
return agent_argv(self.name, prompt)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
_agents: dict[str, AgentAdapter] = {
|
|
94
|
+
n: BuiltinAgent(n, [n], n) for n in ('codex', 'opencode')
|
|
95
|
+
}
|
|
96
|
+
_agents['claude'] = ExecutableAgent('claude', [
|
|
97
|
+
'claude', '--bare', '-p', '--output-format', 'stream-json', '--verbose', '{prompt}'
|
|
98
|
+
], 'claude')
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def register_agent(adapter: AgentAdapter):
|
|
102
|
+
if not isinstance(adapter, AgentAdapter) or adapter.api_version != API_VERSION:
|
|
103
|
+
raise ValueError('Unsupported agent protocol version')
|
|
104
|
+
if adapter.name in _agents:
|
|
105
|
+
raise ValueError('Agent name already registered')
|
|
106
|
+
_agents[adapter.name] = adapter
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def get_agent(name: str) -> AgentAdapter:
|
|
110
|
+
try:
|
|
111
|
+
return _agents[name]
|
|
112
|
+
except KeyError as exc:
|
|
113
|
+
raise ValueError(f'Unknown agent: {name}; explicitly register an adapter first') from exc
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def bind_model(adapter, model):
|
|
117
|
+
if model is None:
|
|
118
|
+
return adapter
|
|
119
|
+
if not isinstance(model,str) or not model or len(model)>512 or '\0' in model:
|
|
120
|
+
raise ValueError('Invalid selected model')
|
|
121
|
+
if adapter.name not in ('codex','opencode','claude'):
|
|
122
|
+
raise ValueError('Custom adapters must implement their own model selection and provenance')
|
|
123
|
+
class ConfiguredAgent:
|
|
124
|
+
name=adapter.name
|
|
125
|
+
api_version=API_VERSION
|
|
126
|
+
def command(self,prompt):
|
|
127
|
+
argv=adapter.command(prompt)
|
|
128
|
+
if self.name in ('codex','opencode','claude'):
|
|
129
|
+
return [*argv[:-1],'--model='+model,argv[-1]]
|
|
130
|
+
return argv
|
|
131
|
+
def parse(self,stream):
|
|
132
|
+
usage=adapter.parse(stream)
|
|
133
|
+
usage.update(model=model,model_origin='requested_configuration')
|
|
134
|
+
return usage
|
|
135
|
+
def capabilities(self):
|
|
136
|
+
return adapter.capabilities()
|
|
137
|
+
return ConfiguredAgent()
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def agent_capabilities():
|
|
141
|
+
from dataclasses import asdict
|
|
142
|
+
return {n: asdict(a.capabilities()) for n, a in _agents.items()}
|
olyder/agents.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Bounded telemetry parsers and reviewed CLI argument templates for coding agents.
|
|
2
|
+
|
|
3
|
+
CLI usage is observed after execution; it does NOT enforce provider/subscription limits.
|
|
4
|
+
"""
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
|
|
10
|
+
SUPPORTED = ("codex", "opencode")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def agent_argv(provider: str, prompt: str) -> list[str]:
|
|
14
|
+
if provider == "codex":
|
|
15
|
+
return ["codex", "exec", "--json", "--sandbox", "workspace-write",
|
|
16
|
+
prompt]
|
|
17
|
+
if provider == "opencode":
|
|
18
|
+
return ["opencode", "run", "--format", "json", "--agent", "build", prompt]
|
|
19
|
+
raise ValueError(f"Unsupported coding agent: {provider}")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _count(value: object) -> int:
|
|
23
|
+
return value if type(value) is int and value >= 0 else 0
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class AgentUsage:
|
|
28
|
+
provider: str
|
|
29
|
+
input_tokens: int = 0
|
|
30
|
+
output_tokens: int = 0
|
|
31
|
+
cached_tokens: int = 0
|
|
32
|
+
reasoning_tokens: int = 0
|
|
33
|
+
observed_total: int = 0
|
|
34
|
+
usage_events: int = 0
|
|
35
|
+
completed: bool = False
|
|
36
|
+
failed: bool = False
|
|
37
|
+
malformed_events: int = 0
|
|
38
|
+
_seen_parts: set[str] = field(default_factory=set, repr=False)
|
|
39
|
+
|
|
40
|
+
def accept(self, raw: str) -> None:
|
|
41
|
+
if len(raw) > 1_000_000:
|
|
42
|
+
self.malformed_events += 1
|
|
43
|
+
return
|
|
44
|
+
try:
|
|
45
|
+
event = json.loads(raw)
|
|
46
|
+
except (ValueError, TypeError):
|
|
47
|
+
self.malformed_events += 1
|
|
48
|
+
return
|
|
49
|
+
if not isinstance(event, dict):
|
|
50
|
+
self.malformed_events += 1
|
|
51
|
+
return
|
|
52
|
+
kind = event.get("type")
|
|
53
|
+
if self.provider == "codex":
|
|
54
|
+
if kind == "turn.failed":
|
|
55
|
+
self.failed = True
|
|
56
|
+
elif kind == "turn.completed":
|
|
57
|
+
self.completed = True
|
|
58
|
+
usage = event.get("usage")
|
|
59
|
+
if isinstance(usage, dict):
|
|
60
|
+
if any(type(usage.get(k)) is not int or usage[k]<0 for k in ('input_tokens','output_tokens')):
|
|
61
|
+
self.malformed_events += 1
|
|
62
|
+
return
|
|
63
|
+
self.input_tokens += _count(usage.get("input_tokens"))
|
|
64
|
+
self.output_tokens += _count(usage.get("output_tokens"))
|
|
65
|
+
self.cached_tokens += _count(usage.get("cached_input_tokens"))
|
|
66
|
+
self.reasoning_tokens += _count(usage.get("reasoning_output_tokens"))
|
|
67
|
+
self.observed_total += _count(usage.get("input_tokens")) + _count(usage.get("output_tokens"))
|
|
68
|
+
self.usage_events += 1
|
|
69
|
+
elif self.provider == "opencode":
|
|
70
|
+
if kind == "error":
|
|
71
|
+
self.failed = True
|
|
72
|
+
elif kind == "step_finish":
|
|
73
|
+
part = event.get("part")
|
|
74
|
+
if not isinstance(part, dict):
|
|
75
|
+
return
|
|
76
|
+
identifier = part.get("id")
|
|
77
|
+
if identifier is not None and not isinstance(identifier,str):
|
|
78
|
+
self.malformed_events+=1
|
|
79
|
+
return
|
|
80
|
+
if identifier and identifier in self._seen_parts:
|
|
81
|
+
return
|
|
82
|
+
if identifier:
|
|
83
|
+
self._seen_parts.add(identifier)
|
|
84
|
+
if part.get("reason") == "stop":
|
|
85
|
+
self.completed = True
|
|
86
|
+
counts = part.get("tokens")
|
|
87
|
+
if isinstance(counts, dict):
|
|
88
|
+
if any(type(counts.get(k)) is not int or counts[k]<0 for k in ('input','output')):
|
|
89
|
+
self.malformed_events += 1
|
|
90
|
+
return
|
|
91
|
+
cache = counts.get("cache") or {}
|
|
92
|
+
if not isinstance(cache, dict):
|
|
93
|
+
cache = {}
|
|
94
|
+
inp, out = _count(counts.get("input")), _count(counts.get("output"))
|
|
95
|
+
cached = _count(cache.get("read"))
|
|
96
|
+
reasoning = _count(counts.get("reasoning"))
|
|
97
|
+
written = _count(cache.get("write"))
|
|
98
|
+
self.input_tokens += inp + written
|
|
99
|
+
self.output_tokens += out
|
|
100
|
+
self.cached_tokens += cached
|
|
101
|
+
self.reasoning_tokens += reasoning
|
|
102
|
+
self.observed_total += _count(counts.get("total")) or inp + out + cached + reasoning + written
|
|
103
|
+
self.usage_events += 1
|
|
104
|
+
|
|
105
|
+
def snapshot(self) -> dict:
|
|
106
|
+
return {"provider": self.provider, "input_tokens": self.input_tokens,
|
|
107
|
+
"output_tokens": self.output_tokens, "cached_tokens": self.cached_tokens,
|
|
108
|
+
"reasoning_tokens": self.reasoning_tokens,
|
|
109
|
+
"observed_tokens": self.observed_total if self.usage_events else None,
|
|
110
|
+
"usage_events": self.usage_events,
|
|
111
|
+
"usage_complete": self.completed and self.usage_events > 0 and not self.failed and not self.malformed_events,
|
|
112
|
+
"status": "failed" if self.failed else "completed" if self.completed else "incomplete",
|
|
113
|
+
"malformed_events": self.malformed_events,
|
|
114
|
+
"quota_enforced": False}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def parse_agent_log(path, provider: str, *, max_lines: int = 100000) -> AgentUsage:
|
|
118
|
+
from contextlib import nullcontext
|
|
119
|
+
|
|
120
|
+
usage = AgentUsage(provider)
|
|
121
|
+
if hasattr(path, "seek"):
|
|
122
|
+
path.seek(0)
|
|
123
|
+
with (nullcontext(path) if hasattr(path, "seek") else
|
|
124
|
+
open(path, "r", encoding="utf-8", errors="replace")) as stream:
|
|
125
|
+
for i, line in enumerate(stream):
|
|
126
|
+
if i >= max_lines:
|
|
127
|
+
usage.malformed_events += 1
|
|
128
|
+
break
|
|
129
|
+
usage.accept(line)
|
|
130
|
+
return usage
|
olyder/analysis.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""Budgeted, checkpointed planning/research jobs. No builder is executed."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
from .accounting import RuntimeAccounting
|
|
6
|
+
from .config import load_spec
|
|
7
|
+
from .control import Cancelled, RunControl, activate
|
|
8
|
+
from .engine import plan
|
|
9
|
+
from .idea import evaluate_idea
|
|
10
|
+
from .models import ModelError
|
|
11
|
+
from .privacy import redact
|
|
12
|
+
from .quota import Budget, QuotaExceeded
|
|
13
|
+
from .state import RunStore
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def run_analysis(kind, spec_path='olyder.json', *, use_ai=False, allow_network=False, model_provider=None):
|
|
17
|
+
if kind not in ('plan','idea') or type(use_ai) is not bool or type(allow_network) is not bool:
|
|
18
|
+
raise ValueError('Invalid analysis workflow or consent flags')
|
|
19
|
+
spec=load_spec(spec_path)
|
|
20
|
+
(spec.root/'.olyder').mkdir(mode=0o700,exist_ok=True)
|
|
21
|
+
store=RunStore(spec.root/'.olyder'/'runs')
|
|
22
|
+
state=store.create({'phase':'analysis_ready','spec_sha256':spec.digest,'job_type':kind})
|
|
23
|
+
run_id=state['run_id']
|
|
24
|
+
account=RuntimeAccounting(run_id,spec.raw.get('budget'))
|
|
25
|
+
started=time.monotonic()
|
|
26
|
+
budget=Budget(spec.raw.get('limits',{}).get('max_tokens',30000))
|
|
27
|
+
def checkpoint(phase):
|
|
28
|
+
nonlocal state
|
|
29
|
+
latest=store.read(run_id)
|
|
30
|
+
state=store.update(run_id,latest['revision'],{'phase':phase,'accounting':account.snapshot()})
|
|
31
|
+
control=RunControl(started+spec.raw.get('limits',{}).get('max_seconds',600),
|
|
32
|
+
cancelled=lambda:store.read(run_id).get('cancel_requested',False),
|
|
33
|
+
model_provider=model_provider,model_accounting=account,
|
|
34
|
+
model_role='planner' if kind=='plan' else 'research',model_checkpoint=checkpoint)
|
|
35
|
+
result={'goal':spec.goal,'status':'blocked','run_id':run_id,'job_type':kind,'spec_sha256':spec.digest}
|
|
36
|
+
try:
|
|
37
|
+
with activate(control):
|
|
38
|
+
control.check()
|
|
39
|
+
if kind=='plan':
|
|
40
|
+
result.update(plan(spec,budget,use_ai=use_ai),status='planned_not_verified')
|
|
41
|
+
else:
|
|
42
|
+
result.update(evaluate_idea(spec,network_allowed=allow_network,use_ai=use_ai,budget=budget))
|
|
43
|
+
control.check()
|
|
44
|
+
if hashlib.sha256(spec.path.read_bytes()).hexdigest()!=spec.digest:
|
|
45
|
+
raise ValueError('Original requirements changed during analysis')
|
|
46
|
+
except QuotaExceeded:
|
|
47
|
+
result.update(status='budget_exhausted',stop_reason='analysis_budget_admission')
|
|
48
|
+
except (Cancelled,KeyboardInterrupt):
|
|
49
|
+
result.update(status='cancelled',stop_reason='user_cancelled')
|
|
50
|
+
except (ModelError,OSError,ValueError,RuntimeError) as exc:
|
|
51
|
+
result.update(status='blocked',stop_reason='analysis_unavailable',reason=redact(str(exc))[:500])
|
|
52
|
+
result.update(run_id=run_id,budget=budget.snapshot(),accounting=account.snapshot(),
|
|
53
|
+
budget_verdict=account.snapshot()['budget_verdict'],wall_seconds=time.monotonic()-started,
|
|
54
|
+
verification_performed=False)
|
|
55
|
+
latest=store.read(run_id)
|
|
56
|
+
store.update(run_id,latest['revision'],{'phase':'completed','result':result,'accounting':account.snapshot()})
|
|
57
|
+
return result
|