File size: 17,465 Bytes
5ca3c16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
279
280
281
282
283
284
285
286
"""Read models for the admin canvas. Existing Skill documents remain authoritative.



This adapter never prepares, publishes or advances a run. Layout is browser-local.

Historical traces always resolve their own immutable release, not today's draft.

"""
import json
import re
import sqlite3
import time
from collections import Counter
from contextlib import contextmanager

from fastapi import APIRouter, Depends, Query
from pydantic import ValidationError

from src.modules.skill.domain.models import Draft, Framework, Operation, SkillError
from src.modules.skill.graph.compiler import compile_graph


MANAGERS = {'admin', 'skill_manager', 'reviewer'}
DOMAIN = {'erp-java-development', 'wms-java-development', 'equipment-ops-java-development'}
REFERENCES = {'requirement-java-business', 'webapp-java-development', 'pdf-java-development'}
SECRET = re.compile(r'password|passwd|secret|api[_-]?key|authorization|access[_-]?token|private[_-]?key|^token$|^credentials?$', re.I)
SAFE_REF = re.compile(r'(credential|secret).*(_ref|_id)$', re.I)


def safe(value, key=''):
    """Keep shape/unknown fields, redact credential values; never emit run payloads."""
    if SECRET.search(key) and not SAFE_REF.search(key):
        return '[已隐藏]'
    if isinstance(value, dict):
        return {k: safe(v, k) for k, v in value.items()}
    if isinstance(value, list):
        return [safe(v) for v in value]
    if isinstance(value, str):
        return re.sub(r'(?i)(bearer\s+\S+|sk-[A-Za-z0-9_-]{12,}|hf_[A-Za-z0-9]{12,})', '[已隐藏]', value)
    return value


def summary(value):
    if isinstance(value, dict):
        return {'type': 'object', 'fields': {k: summary(v) for k, v in value.items()}}
    if isinstance(value, list):
        return {'type': 'array', 'length': len(value)}
    if isinstance(value, str):
        return {'type': 'string', 'length': len(value)}
    return {'type': type(value).__name__}


def system_atom(node):
    kind, scope = node.get('kind'), node.get('approval_scope')
    names = {'start': '需求与检索输入', 'end': '交付结束', 'design': '设计确认',
             'execution': '执行确认', 'acceptance': '用户验收'}
    return {'operation_id': 'system:' + str(kind) + (':' + str(scope) if scope else ''),
            'version': None, 'name': names.get(scope or kind, '不支持的系统节点'),
            'handler_ref': 'interpreter.' + str(kind), 'state': 'system',
            'inputs': [{'name': 'manifest', 'type': 'object', 'required': True}] if kind == 'end' else [],
            'outputs': [{'name': k, 'type': 'string', 'required': True} for k in ('requirement', 'query')] if kind == 'start' else [],
            'note': '对应现有解释器 kind;不是另行注册的 AO,也没有虚构版本。'}


class WorkflowRead:
    def __init__(self, engine):
        self.engine, self.skills, self.store = engine, engine.skills, engine.store
        self.runtime = next(iter(engine.runtimes.values()))

    def guard(self, actor):
        self.skills.require(actor, MANAGERS)

    @contextmanager
    def db(self):
        db = sqlite3.connect(self.store.path.resolve().as_uri() + '?mode=ro', uri=True)
        db.row_factory = sqlite3.Row
        try:
            yield db
        finally:
            db.close()

    def workflows(self, actor):
        self.guard(actor)
        items = []
        for doc in self.skills.drafts(actor):
            b, graph = doc['body'], doc['body'].get('graph', {})
            items.append({'id': doc['key'], 'name': b['name'], 'description': b.get('description', ''),
                          'revision': doc['revision'], 'digest': doc['digest'], 'project_id': b.get('project_id'),
                          'framework_ref': b.get('framework_ref'), 'nodes': len(graph.get('nodes', [])),
                          'control_edges': len(graph.get('control_edges', [])),
                          'group': 'domain' if doc['key'] in DOMAIN else 'reference' if doc['key'] in REFERENCES else 'other'})
        return safe(sorted(items, key=lambda x: (x['group'], x['name'])))

    def releases(self, actor, skill_id):
        result = []
        with self.skills.repository.transaction() as tx:
            for doc in tx.listing(actor.tenant_id, 'release'):
                if doc['body'].get('skill_id') != skill_id:
                    continue
                try:
                    self.skills.scope(actor, doc['body'])
                except SkillError as exc:
                    if exc.status in {403, 404}: continue
                    raise
                result.append({'ref': doc['key'], 'digest': doc['digest'],
                               'scope': doc['body'].get('publication_scope', 'capability-definition')})
        return result

    def definition(self, actor, skill_id, *, run_id=None, release_ref=None):
        self.guard(actor)
        if run_id and release_ref:
            raise SkillError('SOURCE_CONFLICT', '只能选择一个定义来源。')
        if run_id:
            run = self.runtime.get(actor, run_id)
            release = run['release']
            if release['skill_id'] != skill_id:
                raise SkillError('RUN_NOT_FOUND', '任务不属于此工作流。', 404)
            body = release['draft']
            ops = release['operations']
            framework = release['framework']
            source = {'kind': 'run', 'ref': run_id, 'digest': run['snapshot']['release_digest'],
                      'revision': release.get('draft_revision'), 'scope': release.get('publication_scope'),
                      'release_ref': run['request']['release_ref'], 'operations_lock': release['operations_lock']}
        elif release_ref:
            with self.skills.repository.transaction() as tx:
                doc = self.skills.need(tx, actor, 'release', release_ref)
                self.skills.scope(actor, doc['body'])
            release = doc['body']
            if release['skill_id'] != skill_id:
                raise SkillError('NOT_FOUND', '版本不属于此工作流。', 404)
            body, ops, framework = release['draft'], release['operations'], release['framework']
            source = {'kind': 'release', 'ref': release_ref, 'digest': doc['digest'],
                      'scope': release.get('publication_scope'), 'operations_lock': release['operations_lock']}
        else:
            doc = self.skills.read_draft(actor, skill_id)
            body = {k: v for k, v in doc['body'].items() if k not in {'owner_id', 'last_editor_id'}}
            ops = {d['key']: d['body'] for d in self.skills.catalog(actor, 'operation')}
            framework = next((d['body'] for d in self.skills.catalog(actor, 'framework') if d['key'] == body['framework_ref']), None)
            source = {'kind': 'draft', 'ref': skill_id, 'revision': doc['revision'], 'digest': doc['digest']}
        issues, order = [], [n.get('id') for n in body.get('graph', {}).get('nodes', [])]
        try:
            report = compile_graph(Draft.model_validate(body), {k: Operation.model_validate(v) for k, v in ops.items()},
                                   Framework.model_validate(framework) if framework else None)
            issues = [i.model_dump() for i in report.issues]
            order = report.execution_order or order
        except ValidationError as exc:
            issues = [{'code': 'UNSUPPORTED_DEFINITION', 'location': '.'.join(map(str, e['loc'])),
                       'message': '字段或节点不被当前引擎支持;原始定义保留,未丢弃。'} for e in exc.errors()]
        except (ValueError, TypeError, KeyError):
            issues = [{'code': 'UNSUPPORTED_DEFINITION', 'location': 'graph', 'message': '定义无法由当前编译器表达;原始字段保留。'}]
        from src.modules.orchestrator.profile import supported_graph
        if not supported_graph(body.get('graph', {'nodes': []})):
            issues.append({'code': 'EXECUTOR_PROFILE_GAP', 'location': 'graph', 'message': '当前可重复需求/检索/上下文/策略原子,模型与制品执行阶段须保持唯一权威实例。'})
        nodes = []
        for n in body.get('graph', {}).get('nodes', []):
            op = ops.get(n.get('operation_ref')) if n.get('kind') == 'operation' else system_atom(n)
            nodes.append({'id': n.get('id'), 'definition': n, 'atom': op,
                          'missing': op is None})
        with self.skills.repository.transaction() as tx:
            retirements = [d['key'] for d in tx.listing(actor.tenant_id, 'retirement')]
        return safe({'definition': body, 'source': source, 'nodes': nodes, 'execution_order': order,
                     'issues': issues, 'retirements': retirements,
                     'releases': self.releases(actor, skill_id),
                     'boundary': '图来自真实 Skill 定义;草稿可保存并在现有画像内试运行。条件/循环未实现;修复及沙箱内部步骤保留在运行日志。'})

    def atoms(self, actor):
        self.guard(actor)
        refs = {}
        drafts = self.skills.drafts(actor)
        with self.skills.repository.transaction() as tx:
            releases = tx.listing(actor.tenant_id, 'release')
            retired = {d['key'] for d in tx.listing(actor.tenant_id, 'retirement')}
        for doc in [*drafts, *releases]:
            body = doc['body']
            try: self.skills.scope(actor, body)
            except SkillError as exc:
                if exc.status in {403, 404}: continue
                raise
            draft = body.get('draft', body)
            for node in draft.get('graph', {}).get('nodes', []):
                ref = node.get('operation_ref') or system_atom(node)['operation_id']
                refs.setdefault(ref, []).append({'workflow': draft['skill_id'], 'name': draft['name'],
                                                'node': node['id'], 'source': doc['key'],
                                                'kind': 'release' if 'draft' in body else 'draft'})
        items = []
        for doc in self.skills.catalog(actor, 'operation'):
            items.append({**doc['body'], 'ref': doc['key'], 'references': refs.get(doc['key'], []),
                          'retired': 'operation:' + doc['key'] in retired,
                          'note': '注册定义与节点固定输入、映射分别存储。错误恢复由既有编排器统一处理;当前无原子级自定义错误策略字段。'})
        for kind, scope in [('start', None), ('approval', 'design'), ('approval', 'execution'), ('approval', 'acceptance'), ('end', None)]:
            atom = system_atom({'kind': kind, 'approval_scope': scope})
            items.append({**atom, 'ref': atom['operation_id'], 'references': refs.get(atom['operation_id'], [])})
        return safe(items)

    def runs(self, actor, *, before=2147483647, limit=30, workflow=None):
        self.guard(actor)
        result, cursor, more = [], before, False
        # Close the read cursor before the existing ACL adapter opens its own
        # RuntimeStore transaction. A live cursor here would block its commit.
        with self.db() as db:
            rows = [dict(r) for r in db.execute('SELECT rowid,id,body FROM runs WHERE tenant=? AND rowid<? ORDER BY rowid DESC', (actor.tenant_id, before))]
            web = {r['id']: r['state'] for r in db.execute('SELECT id,state FROM web_jobs')} if db.execute("SELECT 1 FROM sqlite_master WHERE name='web_jobs'").fetchone() else {}
        for row in rows:
            run = json.loads(row['body'])
            if workflow and run['release']['skill_id'] != workflow: continue
            try: self.runtime._access(actor, row['id'])
            except SkillError as exc:
                if exc.status in {403, 404}: continue
                raise
            if len(result) == limit:
                more = True
                break
            cursor = row['rowid']
            result.append({'id': row['id'], 'workflow': run['release']['skill_id'], 'name': run['release']['draft']['name'],
                           'status': run['status'], 'web_status': web.get(row['id']),
                           'lease_expired': run['status'] == 'running' and run.get('lease_until', 0) < time.time(),
                           'created_at': run['created_at'], 'error': run.get('error'),
                           'release_ref': run['request']['release_ref'], 'project_id': run['project_id'],
                           'pending_scope': (run.get('pending_approval') or {}).get('scope')})
        return safe({'items': result, 'before': cursor if more else None})

    def trace(self, actor, run_id, *, after=0, limit=100):
        self.guard(actor)
        run = self.runtime.get(actor, run_id)
        with self.db() as db:
            all_events = [dict(r) for r in db.execute('SELECT id,timestamp,stage,status,detail FROM runtime_stage_events WHERE run_id=? ORDER BY id', (run_id,))]
            web = db.execute('SELECT state FROM web_jobs WHERE id=?', (run_id,)).fetchone() if db.execute("SELECT 1 FROM sqlite_master WHERE name='web_jobs'").fetchone() else None
        states = {}
        for n in run['release']['draft']['graph']['nodes']:
            events = [e for e in all_events if e['stage'] == n['id']]
            attempts, started = [], None
            for e in events:
                if e['status'] == 'started': started = e['timestamp']
                elif e['status'] in {'completed', 'failed', 'unknown'} and started is not None:
                    attempts.append({'status': e['status'], 'seconds': round(max(0, e['timestamp'] - started), 3)})
                    started = None
            status = events[-1]['status'] if events else 'not_started'
            approved = [a for a in run.get('approvals', []) if a.get('node') == n['id']]
            if approved: status = 'approved'
            if (run.get('pending_approval') or {}).get('node') == n['id']: status = 'waiting'
            if not events and n['id'] in run.get('outputs', {}): status = 'recorded'
            states[n['id']] = {'status': status, 'attempts': attempts, 'approvals': safe(approved),
                               'output': summary(run['outputs'][n['id']]) if n['id'] in run.get('outputs', {}) else None,
                               'error': next((e['detail'] for e in reversed(events) if e['status'] in {'failed', 'unknown'} and re.fullmatch('[A-Z0-9_]{1,100}', e['detail'])), None)}
        # Log text may contain generated content/credentials. Show structural event metadata;
        # failure codes are allowlisted, full protected records remain on their existing route.
        page = [e for e in all_events if e['id'] > after][:limit + 1]
        has_more, page = len(page) > limit, page[:limit]
        events = [{k: v for k, v in e.items() if k != 'detail'} | {
            'code': e['detail'] if re.fullmatch('[A-Z0-9_]{1,100}', e['detail']) else None,
            'summary': '日志包含内容,详情见受权限保护的原始运行记录。' if e['detail'] else ''} for e in page]
        calls = [{k: c.get(k) for k in ('id', 'state', 'charged', 'reservation', 'provider')} | {
            'metadata': {k: v for k, v in c.get('metadata', {}).items() if k in {'request_id', 'model', 'stage', 'input_tokens', 'output_tokens', 'started_at', 'ended_at', 'failure_category', 'reason_code'}}} for c in run.get('model_calls', [])]
        return safe({'id': run_id, 'workflow': run['release']['skill_id'], 'status': run['status'],
                     'error': run.get('error'), 'nodes': states, 'events': events, 'calls': calls,
                     'web_status': web['state'] if web else None,
                     'lease_expired': run['status'] == 'running' and run.get('lease_until', 0) < time.time(),
                     'after': page[-1]['id'] if page and has_more else None,
                     'total_events': len(all_events), 'pending': run.get('pending_approval'),
                     'provenance': {k: run.get(k) for k in ('repair_origin',)},
                     'record_url': '/api/jobs/' + run_id + '/record'})


def build_router(engine, principal):
    read = WorkflowRead(engine)
    router = APIRouter(prefix='/api/v1/admin')

    @router.get('/workflows')
    def workflows(actor=Depends(principal)):
        return read.workflows(actor)

    @router.get('/workflows/{skill_id}')
    def definition(skill_id: str, run_id: str | None = None, release_ref: str | None = None, actor=Depends(principal)):
        return read.definition(actor, skill_id, run_id=run_id, release_ref=release_ref)

    @router.get('/workflow-atoms')
    def atoms(actor=Depends(principal)):
        return read.atoms(actor)

    @router.get('/workflow-runs')
    def runs(before: int = Query(2147483647, ge=1), limit: int = Query(30, ge=1, le=100), workflow: str | None = None, actor=Depends(principal)):
        return read.runs(actor, before=before, limit=limit, workflow=workflow)

    @router.get('/workflow-runs/{run_id}')
    def trace(run_id: str, after: int = Query(0, ge=0), limit: int = Query(100, ge=1, le=500), actor=Depends(principal)):
        return read.trace(actor, run_id, after=after, limit=limit)

    return router