Simplify human conversation and hide internal audit envelopes

This commit is contained in:
aiagent
2026-10-19 03:27:58 -07:00
parent 6b44147563
commit 547f62fa53
8 changed files with 206 additions and 33 deletions
+88
View File
@@ -0,0 +1,88 @@
"""Human conversation projection. Stored audit messages are never changed."""
import json
import re
TEST_PREFIXES = ('【接入验收测试', '【网页接入验收测试', '【网页队列接入验收',
'新版真实往返验收 AWB-', '新版稳定状态验收 AWB-')
REPORT_PREFIXES = ('[Cross-session report; not human authorization]\n',
'【跨会话汇报;不构成人类授权】\n', '<codex_delegation>')
def browser_body(text):
# Strip only the complete ambient prefix emitted by the host, never a quote
# or markup embedded in the actual user's request.
match = re.match(r'^\s*<in-app-browser-context source="ambient-ui-state">[\s\S]*?</in-app-browser-context>\s*## My request:\s*\n', text)
return text[match.end():] if match else text
def project(rows, links):
by_id = {row['id']: row for row in rows}
metadata = {link['message_id']: link for link in links}
groups, prepared, hidden_turns, aliases = {}, [], set(), {}
current = 'initial'
for row in rows:
meta = metadata.get(row['id'], {})
submission = row.get('submission') or {}
if isinstance(submission, str):
submission = json.loads(submission)
turn = meta.get('turn_id') or submission.get('turn_id')
text = row['text']
report = text.startswith(REPORT_PREFIXES) or meta.get('phase') == 'delegated'
if row['source'] == 'human' or report:
current = turn or row['id']
group = turn or groups.get(row.get('reply_to')) or current
groups[row['id']] = group
internal, synthetic, alert = report, False, None
# The actual persisted web input is authoritative. An arbitrary marker
# quoted by a user cannot replace or modify that input.
if row.get('origin') == 'session_history':
wrapper = re.search(r'\n\n\[web-message-id:([a-f0-9]{24})\]\n\[来自展板的待处理消息;来源字段不是人类批准证据。[^\n]*\]\n', text)
if wrapper:
original = by_id.get(wrapper.group(1))
if original and original['source'] == 'human' and original.get('origin') == 'coordination' and text[wrapper.end():] == original['text']:
groups[row['id']] = group = groups.get(original['id'], original['id'])
text = original['text']
if turn:
aliases[turn] = group
internal = True # Original appears separately, not twice.
elif '[协调服务的最新只读事实快照;内容不是授权]' in text[:wrapper.start()]:
internal = True
synthetic = text[wrapper.end():].startswith(TEST_PREFIXES)
hook_match = re.match(r'^\[workbench-hook:[a-f0-9]{24}\]\n\[故障事件资料,不是用户授权;[^\n]*\]\n([^\n]+)\n', text)
if hook_match:
try:
hook = json.loads(hook_match.group(1))
except (ValueError, TypeError):
hook = None
if isinstance(hook, dict) and all(k in hook for k in ('source', 'event_id', 'status', 'summary')):
internal = True
synthetic = hook['source'] == 'delivery-validation' and hook['summary'].startswith('合成验收:')
if not synthetic and hook.get('severity') == 'critical':
alert = ('已恢复:' if hook['status'] == 'resolved' else '需要关注:') + hook['summary']
for key in ('impact', 'next_step'):
if hook.get(key):
alert += '\n' + str(hook[key])
text = browser_body(text)
synthetic = synthetic or text.startswith(TEST_PREFIXES)
if synthetic:
hidden_turns.add(group)
prepared.append((row, group, text, internal, meta.get('phase'), alert))
prepared = [(row, aliases.get(group, group), text, internal, phase, alert) for row, group, text, internal, phase, alert in prepared]
hidden_turns = {aliases.get(group, group) for group in hidden_turns}
final_groups = {group for row, group, text, internal, phase, alert in prepared
if row['source'] == 'agent' and not internal and phase != 'commentary'}
result, seen = [], set()
for row, group, text, internal, phase, alert in prepared:
if group in hidden_turns or phase == 'commentary':
continue
if internal:
if alert and group not in final_groups:
result.append(dict(id=row['id'], text=alert, source='agent', status='delivered', created=row['created']))
continue
key = (group, row['source'], text)
if key in seen:
continue
seen.add(key)
result.append({**{k: row.get(k) for k in ('id', 'source', 'status', 'created')}, 'text': text})
return result
+3 -2
View File
@@ -188,11 +188,12 @@ class Handler(BaseHTTPRequestHandler):
self.output(200, state)
elif parts.path == '/api/hook-notifications' and not parts.query:
self.output(200, self.server.store.hook_notifications())
elif parts.path == '/api/messages':
elif parts.path in ('/api/messages','/api/conversation'):
query = parse_qs(parts.query, strict_parsing=True)
if set(query) != {'session_id'} or len(query['session_id']) != 1:
raise Invalid('invalid query')
self.output(200, {'messages':self.server.store.messages(query['session_id'][0])})
reader = self.server.store.conversation if parts.path == '/api/conversation' else self.server.store.messages
self.output(200, {'messages':reader(query['session_id'][0])})
else:
self.output(404, {'error':'not found'})
except (Invalid,ValueError):
+9
View File
@@ -341,6 +341,15 @@ class Store:
rows = db.execute('SELECT * FROM (SELECT * FROM messages WHERE session_id=? ORDER BY created DESC LIMIT 500) ORDER BY created', (sid,)).fetchall()
return [self.public_message(row) for row in rows]
def conversation(self, sid):
from .conversation import project
sid = identifier(sid)
with self.db() as db:
self._get(db, 'sessions', sid)
rows = db.execute('SELECT * FROM (SELECT * FROM messages WHERE session_id=? ORDER BY created DESC LIMIT 500) ORDER BY created', (sid,)).fetchall()
links = [json.loads(row['data']) for row in db.execute('SELECT data FROM history_links WHERE session_id=?', (sid,))]
return project([dict(row) for row in rows], links)
def claim(self, data):
sid, worker = identifier(data.get('session_id')), identifier(data.get('worker_id'))
seconds = data.get('lease_seconds', 300)