fix(tasks): recover deleted captures without reviving tombstones
This commit is contained in:
1 parent
43a2d8c5a1
commit
4049acf76e
10 files changed
+594
-28
No files matched your search
+26
-4
@@ -32,7 +32,7 @@ import urllib.parse as url
|
||||
import urllib.request as http
|
||||
import webbrowser
|
||||
|
||||
VERSION = '0.4.0'
|
||||
VERSION = '0.4.2'
|
||||
|
||||
class Failure(Exception):
|
||||
def __init__(self, message, status=0, payload=None):
|
||||
@@ -569,7 +569,7 @@ def tasks(client, profile, words):
|
||||
for name in ('done','reopen','archive','start'):
|
||||
p=sub.add_parser(name);p.add_argument('uuid',type=task_uuid)
|
||||
sub.add_parser('stop');sub.add_parser('status')
|
||||
p=sub.add_parser('retry');p.add_argument('request_id',type=task_uuid)
|
||||
p=sub.add_parser('retry');p.add_argument('request_id',type=task_uuid);p.add_argument('--recreate-deleted',action='store_true',help='Explicitly recreate a deleted capture with a new UUID; preserve and relink any existing Wiki page')
|
||||
p=sub.add_parser('report');p.add_argument('--from',dest='start');p.add_argument('--to',dest='end')
|
||||
p=sub.add_parser('preferences');p.add_argument('--wiki-prefix');p.add_argument('--project');p.add_argument('--assignee');p.add_argument('--due-days',type=int);p.add_argument('--start-days',type=int);p.add_argument('--planned-days',type=int);p.add_argument('--review-days',type=int);p.add_argument('--estimate',type=int);p.add_argument('--complexity',type=int);p.add_argument('--digest',choices=['on','off']);p.add_argument('--at');p.add_argument('--timezone')
|
||||
for child in sub.choices.values():child.add_argument('--json',action='store_true')
|
||||
@@ -578,10 +578,19 @@ def tasks(client, profile, words):
|
||||
def capture(payload):
|
||||
pending=profile.data.setdefault('task_outbox',{})
|
||||
if len(pending)>=20 and payload['request_id'] not in pending:raise Failure('20 pending captures retained. Retry them before adding more.')
|
||||
if payload['request_id'] in pending and pending[payload['request_id']] != payload:raise Failure('A different draft with this request ID is retained. Use pages task retry '+payload['request_id'])
|
||||
replaced=payload['request_id'] in pending and pending[payload['request_id']] != payload
|
||||
pending[payload['request_id']]=payload;profile.save()
|
||||
if replaced:print('Replaced the retained draft with the current input.',file=sys.stderr)
|
||||
try: result=client.json(TASK_API+'capture',method='POST',data=payload)
|
||||
except Failure as e:
|
||||
if e.status==410 and e.payload.get('error')=='deleted':
|
||||
raise Failure('Draft retained. This task was deleted; normal retry will not restore it. To explicitly create a new task (preserving any existing Wiki body), use: pages task retry '+payload['request_id']+' --recreate-deleted',e.status,e.payload) from e
|
||||
if e.status==409 and e.payload.get('error')=='capture_missing':
|
||||
raise Failure('Draft retained. The server has a capture receipt but no task or deletion record. Server-side consistency repair is required; retry will not fix it.',e.status,e.payload) from e
|
||||
if e.status==412:
|
||||
raise Failure('Draft retained. This request ID conflicts with a server capture or task. An unchanged retry will not resolve it; inspect with: pages task show '+payload['request_id'],e.status,e.payload) from e
|
||||
if e.status==409 and e.payload.get('error')=='note_exists':
|
||||
raise Failure('Draft retained. A Wiki page already occupies the destination; it was not overwritten.',e.status,e.payload) from e
|
||||
print('Draft retained; retry with: pages task retry '+payload['request_id'],file=sys.stderr)
|
||||
if e.payload.get('error') == 'wiki_prefix_required': raise Failure('Set a private Wiki destination first: pages task preferences --wiki-prefix /YOUR/MAIL/PATH',e.status) from e
|
||||
raise
|
||||
@@ -606,6 +615,19 @@ def tasks(client, profile, words):
|
||||
if action=='retry':
|
||||
payload=profile.data.get('task_outbox',{}).get(a.request_id)
|
||||
if not payload:raise Failure('No retained task capture with this request ID.')
|
||||
if a.recreate_deleted:
|
||||
pending=profile.data['task_outbox']
|
||||
replacement=str(uuid.uuid5(uuid.NAMESPACE_URL,'pages:task-recreate:'+a.request_id))
|
||||
payload=dict(payload,request_id=replacement,replaces_deleted=a.request_id)
|
||||
if replacement in pending and pending[replacement]!=payload:
|
||||
raise Failure('A different recreation draft already exists; inspect or retry it first: '+replacement)
|
||||
previous=pending.copy()
|
||||
pending[replacement]=payload;del pending[a.request_id]
|
||||
try:profile.save()
|
||||
except Exception:
|
||||
profile.data['task_outbox']=previous
|
||||
raise
|
||||
print('Recreation draft saved. Existing Wiki content will be preserved. Retry ID: '+replacement,file=sys.stderr)
|
||||
emit(capture(payload));return
|
||||
if action in ('add','from-mail'):
|
||||
payload={'request_id':getattr(a,'request_id',None) or str(uuid.uuid4()),'title':a.title,'notes':a.notes or ''}
|
||||
@@ -622,7 +644,7 @@ def tasks(client, profile, words):
|
||||
value=getattr(a,attr)
|
||||
if value is not None:payload[key]=task_project(client,value) if attr=='project' else value
|
||||
# A retry of the same pending mail must not drift across a month boundary
|
||||
# when Date was missing. Other changed options still fail closed.
|
||||
# when Date was missing. Changed input replaces the pending draft below.
|
||||
old=profile.data.get('task_outbox',{}).get(payload['request_id'])
|
||||
if action=='from-mail' and old and {k:v for k,v in old.items() if k!='wiki_suffix'}=={k:v for k,v in payload.items() if k!='wiki_suffix'}:
|
||||
payload=old.copy()
|
||||
|
||||
@@ -9,6 +9,74 @@ import unittest
|
||||
m=runpy.run_path(str(Path(__file__).with_name('pages')))
|
||||
|
||||
class ProductivityTests(unittest.TestCase):
|
||||
def test_deleted_capture_message_retains_draft_without_retry_loop(self):
|
||||
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
||||
original={'request_id':ident,'title':'Private title','notes':'Private notes'}
|
||||
for status,code,message in [(410,'deleted','--recreate-deleted'),(409,'capture_missing','consistency repair'),(412,'conflict','unchanged retry'),(409,'note_exists','not overwritten')]:
|
||||
profile=SimpleNamespace(data={'task_outbox':{ident:original.copy()}},save=lambda:None)
|
||||
calls=[]
|
||||
def send(*args,**kw):
|
||||
calls.append(kw)
|
||||
raise m['Failure']('old generic message',status,{'error':code})
|
||||
output=io.StringIO()
|
||||
with contextlib.redirect_stderr(output),self.assertRaises(m['Failure']) as error:
|
||||
m['tasks'](SimpleNamespace(json=send),profile,['retry',ident])
|
||||
self.assertIn(message,str(error.exception))
|
||||
self.assertNotIn('Private',str(error.exception)+output.getvalue())
|
||||
self.assertNotIn('retry with:',output.getvalue())
|
||||
self.assertEqual(profile.data['task_outbox'][ident],original)
|
||||
self.assertEqual(len(calls),1)
|
||||
|
||||
def test_recreate_deleted_persists_successor_and_retries_same_id(self):
|
||||
import uuid
|
||||
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
||||
replacement=str(uuid.uuid5(uuid.NAMESPACE_URL,'pages:task-recreate:'+ident))
|
||||
payload={'request_id':ident,'title':'Private title','wiki_body':'Private body','wiki_suffix':'2030/01/'+ident}
|
||||
saved,calls=[],[]
|
||||
profile=SimpleNamespace(data={'task_outbox':{ident:payload.copy()}})
|
||||
profile.save=lambda:saved.append(json.loads(json.dumps(profile.data)))
|
||||
def send(route,**kw):
|
||||
calls.append(kw['data'].copy())
|
||||
self.assertEqual(saved[-1]['task_outbox'][replacement],kw['data'])
|
||||
self.assertNotIn(ident,saved[-1]['task_outbox'])
|
||||
if len(calls)==1:raise m['Failure']('Connection lost')
|
||||
return {'item':{'uuid':replacement}}
|
||||
output,error=io.StringIO(),io.StringIO()
|
||||
with contextlib.redirect_stdout(output),contextlib.redirect_stderr(error):
|
||||
with self.assertRaises(m['Failure']):m['tasks'](SimpleNamespace(json=send),profile,['retry',ident,'--recreate-deleted'])
|
||||
self.assertEqual(len(profile.data['task_outbox']),1)
|
||||
m['tasks'](SimpleNamespace(json=send),profile,['retry',replacement])
|
||||
self.assertEqual(calls[0],calls[1])
|
||||
self.assertEqual(calls[0]['request_id'],replacement)
|
||||
self.assertEqual(calls[0]['replaces_deleted'],ident)
|
||||
self.assertEqual(calls[0]['wiki_suffix'],payload['wiki_suffix'])
|
||||
self.assertEqual(calls[0]['wiki_body'],payload['wiki_body'])
|
||||
self.assertEqual(profile.data['task_outbox'],{})
|
||||
self.assertIn(replacement,error.getvalue())
|
||||
self.assertNotIn('Private',error.getvalue())
|
||||
|
||||
def test_recreate_deleted_save_failure_keeps_original_and_sends_nothing(self):
|
||||
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
||||
payload={'request_id':ident,'title':'Draft'}
|
||||
def fail():raise m['Failure']('disk full')
|
||||
profile=SimpleNamespace(data={'task_outbox':{ident:payload.copy()}},save=fail)
|
||||
calls=[]
|
||||
with self.assertRaises(m['Failure']):m['tasks'](SimpleNamespace(json=lambda *a,**kw:calls.append(kw)),profile,['retry',ident,'--recreate-deleted'])
|
||||
self.assertEqual(calls,[])
|
||||
self.assertEqual(profile.data['task_outbox'],{ident:payload})
|
||||
|
||||
def test_recreate_deleted_never_clobbers_different_successor_draft(self):
|
||||
import uuid
|
||||
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
||||
replacement=str(uuid.uuid5(uuid.NAMESPACE_URL,'pages:task-recreate:'+ident))
|
||||
pending={ident:{'request_id':ident,'title':'Original'},replacement:{'request_id':replacement,'title':'Different'}}
|
||||
before=json.loads(json.dumps(pending))
|
||||
calls=[]
|
||||
profile=SimpleNamespace(data={'task_outbox':pending},save=lambda:calls.append('save'))
|
||||
with self.assertRaises(m['Failure']):m['tasks'](SimpleNamespace(json=lambda *a,**kw:calls.append('send')),profile,['retry',ident,'--recreate-deleted'])
|
||||
self.assertEqual(calls,[])
|
||||
self.assertEqual(pending,before)
|
||||
|
||||
def test_mail_path_priority_and_dates(self):
|
||||
import datetime,hashlib
|
||||
ident='06cb3a24-61f8-4a9f-a0bf-89eea9632f25'
|
||||
@@ -24,7 +92,7 @@ class ProductivityTests(unittest.TestCase):
|
||||
raw=b'Subject: [../unsafe 123] Title\r\n'+date+b'\r\nbody'
|
||||
self.assertEqual(suffix(raw,ident,now),'2030/02/'+hashlib.sha256(raw).hexdigest())
|
||||
|
||||
def test_pending_mail_month_and_options_are_preserved(self):
|
||||
def test_pending_mail_keeps_month_but_replaces_changed_options(self):
|
||||
from unittest.mock import patch
|
||||
raw=b'Subject: Test\r\nMessage-ID: <same@example.invalid>\r\n\r\nbody'
|
||||
calls=[]
|
||||
@@ -37,10 +105,86 @@ class ProductivityTests(unittest.TestCase):
|
||||
with patch.dict(m['tasks'].__globals__,task_mail_suffix=lambda raw,ident:month+'/'+ident),patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw))),contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
|
||||
m['tasks'](client,profile,['from-mail'])
|
||||
self.assertEqual(calls[0],calls[1])
|
||||
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw))),self.assertRaises(m['Failure']):
|
||||
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(raw))),contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
|
||||
m['tasks'](client,profile,['from-mail','--wiki-prefix','/changed'])
|
||||
self.assertEqual(len(calls),2)
|
||||
self.assertEqual(next(iter(profile.data['task_outbox'].values())),calls[0])
|
||||
self.assertEqual(len(calls),3)
|
||||
self.assertEqual(calls[2]['request_id'],calls[0]['request_id'])
|
||||
self.assertEqual(calls[2]['wiki_prefix'],'/changed')
|
||||
self.assertEqual(next(iter(profile.data['task_outbox'].values())),calls[2])
|
||||
|
||||
def test_changed_capture_is_saved_before_send_and_retained_on_conflict(self):
|
||||
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
||||
saved,calls=[],[]
|
||||
old={'request_id':ident,'title':'Old draft','notes':'Old private notes'}
|
||||
profile=SimpleNamespace(data={'task_outbox':{ident:old}})
|
||||
profile.save=lambda:saved.append(json.loads(json.dumps(profile.data)))
|
||||
def send(route,**kw):
|
||||
payload=kw['data'].copy();calls.append(payload)
|
||||
self.assertEqual(saved[-1]['task_outbox'][ident],payload)
|
||||
if len(calls)==1:raise m['Failure']('conflict',412,{'error':'conflict'})
|
||||
return {'item':{'uuid':ident}}
|
||||
client=SimpleNamespace(json=send)
|
||||
out,err=io.StringIO(),io.StringIO()
|
||||
with contextlib.redirect_stdout(out),contextlib.redirect_stderr(err):
|
||||
with self.assertRaises(m['Failure']) as failure:
|
||||
m['tasks'](client,profile,['add','New draft','--request-id',ident,'--notes','New private notes'])
|
||||
self.assertEqual(failure.exception.status,412)
|
||||
self.assertEqual(profile.data['task_outbox'][ident],calls[0])
|
||||
self.assertEqual(len(calls),1) # No automatic server overwrite/retry.
|
||||
m['tasks'](client,profile,['retry',ident])
|
||||
self.assertEqual(calls[0],calls[1])
|
||||
self.assertEqual(calls[0]['title'],'New draft')
|
||||
self.assertEqual(profile.data['task_outbox'],{})
|
||||
self.assertIn('Replaced the retained draft',err.getvalue())
|
||||
self.assertNotIn('private notes',err.getvalue())
|
||||
self.assertEqual(json.loads(out.getvalue())['item']['uuid'],ident)
|
||||
|
||||
def test_replacing_draft_does_not_send_when_local_save_fails(self):
|
||||
ident='b5550cab-22d4-54eb-b06d-3b432d3f5101'
|
||||
def fail_save():raise m['Failure']('local write failed')
|
||||
profile=SimpleNamespace(data={'task_outbox':{ident:{'request_id':ident,'title':'Old','notes':''}}},save=fail_save)
|
||||
calls=[]
|
||||
client=SimpleNamespace(json=lambda *a,**kw:calls.append(kw))
|
||||
with self.assertRaises(m['Failure']):m['tasks'](client,profile,['add','New','--request-id',ident])
|
||||
self.assertEqual(calls,[])
|
||||
|
||||
def test_full_outbox_allows_replacement_but_not_new_id(self):
|
||||
import uuid
|
||||
pending={str(uuid.uuid4()):{'title':'Old'} for _ in range(20)}
|
||||
ident=next(iter(pending))
|
||||
profile=SimpleNamespace(data={'task_outbox':pending},save=lambda:None)
|
||||
calls=[]
|
||||
def send(route,**kw):
|
||||
calls.append(kw['data'].copy())
|
||||
raise m['Failure']('offline')
|
||||
client=SimpleNamespace(json=send)
|
||||
with contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
|
||||
m['tasks'](client,profile,['add','Replacement','--request-id',ident])
|
||||
self.assertEqual(len(calls),1)
|
||||
self.assertEqual(pending[ident]['title'],'Replacement')
|
||||
with self.assertRaises(m['Failure']):m['tasks'](client,profile,['add','Another'])
|
||||
self.assertEqual(len(calls),1)
|
||||
self.assertEqual(len(pending),20)
|
||||
|
||||
def test_reimported_mail_replaces_body_and_removes_obsolete_options(self):
|
||||
from unittest.mock import patch
|
||||
raw=b'Subject: Old\r\nMessage-ID: <same@example.invalid>\r\n\r\nOLD BODY'
|
||||
calls=[]
|
||||
def send(route,**kw):
|
||||
calls.append(kw['data'].copy())
|
||||
raise m['Failure']('offline')
|
||||
client=SimpleNamespace(base='https://example.invalid',json=send)
|
||||
profile=SimpleNamespace(data={'username':'test'},save=lambda:None)
|
||||
changed=raw.replace(b'Subject: Old',b'Subject: New').replace(b'OLD BODY',b'NEW BODY')
|
||||
for mail,args in [(raw,['--wiki-prefix','/old']),(changed,['--wiki-prefix','/new']),(changed,['--metadata-only'])]:
|
||||
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(mail))),contextlib.redirect_stderr(io.StringIO()),self.assertRaises(m['Failure']):
|
||||
m['tasks'](client,profile,['from-mail',*args])
|
||||
self.assertEqual(len({c['request_id'] for c in calls}),1)
|
||||
self.assertEqual(calls[1]['title'],'New')
|
||||
self.assertIn('NEW BODY',calls[1]['wiki_body'])
|
||||
self.assertNotIn('OLD BODY',calls[1]['wiki_body'])
|
||||
for key in ('wiki_body','wiki_prefix','wiki_suffix'):self.assertNotIn(key,calls[2])
|
||||
self.assertEqual(next(iter(profile.data['task_outbox'].values())),calls[2])
|
||||
|
||||
def test_mail_privacy_and_long_subject_override(self):
|
||||
raw=b'Subject: Test\r\nFrom: Person <person@example.invalid>\r\nMessage-ID: <id@example.invalid>\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nPRIVATE BODY'
|
||||
|
||||
@@ -5,6 +5,7 @@ import json
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import sys
|
||||
import uuid
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
from email.message import EmailMessage
|
||||
@@ -61,5 +62,50 @@ def run():
|
||||
output=io.StringIO()
|
||||
with contextlib.redirect_stdout(output):m['contacts'](client,['export',contact['uuid']])
|
||||
assert 'UID:urn:test:cli' in output.getvalue()
|
||||
# Failed captures can be replaced locally; committed receipts remain protected.
|
||||
request_id=str(uuid.uuid4())
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
try:task('add','Rejected draft','--request-id',request_id,'--estimate','-1')
|
||||
except m['Failure'] as error:assert error.status==422
|
||||
else:raise AssertionError('Invalid estimate accepted')
|
||||
assert profile.data['task_outbox'][request_id]['estimate_minutes']==-1
|
||||
corrected=task('add','Corrected draft','--request-id',request_id,'--estimate','15')['item']
|
||||
assert corrected['uuid']==request_id and corrected['title']=='Corrected draft'
|
||||
assert request_id not in profile.data['task_outbox']
|
||||
try:task('add','Changed after commit','--request-id',request_id,'--estimate','30')
|
||||
except m['Failure'] as error:assert error.status==412
|
||||
else:raise AssertionError('Committed capture silently overwritten')
|
||||
assert profile.data['task_outbox'][request_id]['title']=='Changed after commit'
|
||||
stored=task('show',request_id)['item']
|
||||
assert stored['title']=='Corrected draft' and stored['estimate_minutes']==15
|
||||
# Deleted receipts must not be mistaken for editable conflicts. An
|
||||
# explicit recreation preserves the old tombstone and changes identity.
|
||||
client.request(m['TASK_API']+'items/'+request_id,method='DELETE',etag='"'+str(stored['version'])+'"')
|
||||
with contextlib.redirect_stderr(io.StringIO()):
|
||||
try:task('retry',request_id)
|
||||
except m['Failure'] as error:
|
||||
assert error.status==410 and '--recreate-deleted' in str(error)
|
||||
else:raise AssertionError('Deleted task implicitly recreated')
|
||||
recreated=task('retry',request_id,'--recreate-deleted')['item']
|
||||
replacement=str(uuid.uuid5(uuid.NAMESPACE_URL,'pages:task-recreate:'+request_id))
|
||||
assert recreated['uuid']==replacement and recreated['title']=='Changed after commit'
|
||||
assert recreated['estimate_minutes']==30 and not profile.data.get('task_outbox')
|
||||
try:task('show',request_id)
|
||||
except m['Failure'] as error:assert error.status==404
|
||||
else:raise AssertionError('Deleted UUID resurrected')
|
||||
# Reimport a deleted mail with a changed body: reconnect the surviving
|
||||
# Wiki note, without overwriting its independent content or revision.
|
||||
mail_id=captured['item']['uuid']
|
||||
client.request(m['TASK_API']+'items/'+mail_id,method='DELETE',etag='"1"')
|
||||
changed_mail=mail.replace(b'Private mail body',b'Changed private body')
|
||||
with patch('sys.stdin',SimpleNamespace(buffer=io.BytesIO(changed_mail))),contextlib.redirect_stderr(io.StringIO()):
|
||||
try:task('from-mail')
|
||||
except m['Failure'] as error:assert error.status==410
|
||||
else:raise AssertionError('Deleted mail task recreated implicitly')
|
||||
with contextlib.redirect_stderr(io.StringIO()):recreated_mail=task('retry',mail_id,'--recreate-deleted')
|
||||
assert recreated_mail['item']['page_uuid']==captured['item']['page_uuid']
|
||||
assert recreated_mail['page_path']==captured['page_path']
|
||||
body,_=client.raw('path:'+recreated_mail['page_path'])
|
||||
assert 'Private mail body' in body.decode() and 'Changed private body' not in body.decode()
|
||||
|
||||
if __name__=='__main__':run()
|
||||
Reference in new issue
Block a user