File size: 2,135 Bytes
04e89f2 | 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 | """Recover received context and complete open requests from the delivered ledger.
This part reads records. It never invokes a work part or replays an effect.
"""
import hashlib
import time
def entries(sid, limit=12):
import reflect
return list(reflect.load(sid).get('ledger') or [])[-limit:]
def open_requests(sid):
import reflect
return [item['original'] for item in reflect.load(sid).get('open_requests', [])
if item.get('state') == 'open']
def record(text, sid, reply, reading=None):
"""Keep unfinished instructions whole; retire only exact checked obligations."""
import reflect
state = reflect.load(sid)
obligations = state.setdefault('open_requests', [])
execution = reply.get('execution') or {}
performed = execution.get('state') in ('checked', 'changed-checked', 'sent', 'done') and not execution.get('holes')
completed = {r.get('text') for r in execution.get('requirements', [])
if r.get('status') in ('passed', 'met')}
if performed:
completed.add(text)
for item in obligations:
if item['original'] in completed:
item.update(state='done', completed=time.time(), receipt=reply.get('path'))
asks_work = bool(reply.get('_open_request') or (reading or {}).get('outcomes'))
# A checked conversational answer or a writer's own durable obligation
# belongs to its owner, not to this record of otherwise unperformed work.
pending = asks_work and not performed and not reply.get('writing') and reply.get('status') != 'answered'
if pending:
identity = hashlib.sha256((sid + '\0' + text).encode('utf-8')).hexdigest()[:24]
item = next((x for x in obligations if x['id'] == identity), None)
if item is None:
item = {'id': identity, 'original': text, 'created': time.time(), 'state': 'open'}
obligations.append(item)
item.update(last_seen=time.time(), path=reply.get('path'), missing=execution.get('holes') or [],
reading=reading, state='open')
reflect.save(sid, state)
reply.pop('_open_request', None)
return reply
|