lardva 2.0.1__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.
- lardva/__init__.py +2 -0
- lardva/__main__.py +3 -0
- lardva/cli.py +314 -0
- lardva/doctor.py +68 -0
- lardva/engine.py +931 -0
- lardva/history.json +349 -0
- lardva/payload/.agents/POLICY.md +9 -0
- lardva/payload/.agents/PROJECT.md +16 -0
- lardva/payload/.agents/providers.md +23 -0
- lardva/payload/.agents/schemas/findings.schema.json +93 -0
- lardva/payload/.agents/schemas/rulings.schema.json +54 -0
- lardva/payload/.agents/scripts/context.py +1115 -0
- lardva/payload/.agents/scripts/evidence.py +183 -0
- lardva/payload/.agents/scripts/test_context.py +1279 -0
- lardva/payload/.agents/scripts/test_evidence.py +73 -0
- lardva/payload/.agents/skills/agent-orchestrator/SKILL.md +26 -0
- lardva/payload/.agents/skills/agent-system-design/SKILL.md +8 -0
- lardva/payload/.agents/skills/context-engineering/SKILL.md +8 -0
- lardva/payload/.agents/skills/context-refresh/SKILL.md +19 -0
- lardva/payload/.agents/skills/context-sync/SKILL.md +24 -0
- lardva/payload/.agents/skills/docs-traceability/SKILL.md +8 -0
- lardva/payload/.agents/skills/epic-delivery/SKILL.md +8 -0
- lardva/payload/.agents/skills/evidence-review/SKILL.md +8 -0
- lardva/payload/.agents/skills/investigate-first/SKILL.md +8 -0
- lardva/payload/.agents/skills/lean-build/SKILL.md +8 -0
- lardva/payload/.agents/skills/migration/SKILL.md +8 -0
- lardva/payload/.agents/skills/performance-baseline/SKILL.md +8 -0
- lardva/payload/.agents/skills/research-experiment/SKILL.md +8 -0
- lardva/payload/.agents/skills/retrieval-evaluation/SKILL.md +8 -0
- lardva/payload/.agents/skills/safe-refactor/SKILL.md +8 -0
- lardva/payload/.agents/skills/security-review/SKILL.md +8 -0
- lardva/payload/.agents/skills/session-handoff/SKILL.md +22 -0
- lardva/payload/.agents/skills/spec-driven-design/SKILL.md +8 -0
- lardva/payload/.agents/skills/spec-reconciliation/SKILL.md +33 -0
- lardva/payload/.agents/skills/surgical-patch/SKILL.md +8 -0
- lardva/payload/.agents/skills/verify-and-stop/SKILL.md +8 -0
- lardva/payload/.agents/templates/ADR.md +6 -0
- lardva/payload/.agents/templates/SAD.md +7 -0
- lardva/payload/.agents/templates/SDD.md +7 -0
- lardva/payload/.agents/templates/SRS.md +10 -0
- lardva/payload/.agents/templates/TASKS.md +4 -0
- lardva/payload/.agents/templates/brief.md +10 -0
- lardva/payload/.agents/templates/handoff.json +21 -0
- lardva/payload/.agents/templates/handoff.md +7 -0
- lardva/payload/.agents/templates/prompts.md +19 -0
- lardva/payload/.agents/templates/telemetry.json +26 -0
- lardva/payload/.claude/agents/builder.md +8 -0
- lardva/payload/.claude/agents/reviewer.md +8 -0
- lardva/payload/.claude/agents/scout.md +8 -0
- lardva/payload/.claude/skills/agent-orchestrator/SKILL.md +6 -0
- lardva/payload/.claude/skills/agent-system-design/SKILL.md +6 -0
- lardva/payload/.claude/skills/context-engineering/SKILL.md +6 -0
- lardva/payload/.claude/skills/context-refresh/SKILL.md +6 -0
- lardva/payload/.claude/skills/context-sync/SKILL.md +6 -0
- lardva/payload/.claude/skills/docs-traceability/SKILL.md +6 -0
- lardva/payload/.claude/skills/epic-delivery/SKILL.md +6 -0
- lardva/payload/.claude/skills/evidence-review/SKILL.md +6 -0
- lardva/payload/.claude/skills/investigate-first/SKILL.md +6 -0
- lardva/payload/.claude/skills/lean-build/SKILL.md +6 -0
- lardva/payload/.claude/skills/migration/SKILL.md +6 -0
- lardva/payload/.claude/skills/performance-baseline/SKILL.md +6 -0
- lardva/payload/.claude/skills/research-experiment/SKILL.md +6 -0
- lardva/payload/.claude/skills/retrieval-evaluation/SKILL.md +6 -0
- lardva/payload/.claude/skills/safe-refactor/SKILL.md +6 -0
- lardva/payload/.claude/skills/security-review/SKILL.md +6 -0
- lardva/payload/.claude/skills/session-handoff/SKILL.md +6 -0
- lardva/payload/.claude/skills/spec-driven-design/SKILL.md +6 -0
- lardva/payload/.claude/skills/spec-reconciliation/SKILL.md +6 -0
- lardva/payload/.claude/skills/surgical-patch/SKILL.md +6 -0
- lardva/payload/.claude/skills/verify-and-stop/SKILL.md +6 -0
- lardva/payload/.codex/agents/builder.toml +6 -0
- lardva/payload/.codex/agents/reviewer.toml +7 -0
- lardva/payload/.codex/agents/scout.toml +7 -0
- lardva/payload/AGENTS.md +30 -0
- lardva/payload/CLAUDE.md +2 -0
- lardva-2.0.1.dist-info/METADATA +304 -0
- lardva-2.0.1.dist-info/RECORD +80 -0
- lardva-2.0.1.dist-info/WHEEL +4 -0
- lardva-2.0.1.dist-info/entry_points.txt +2 -0
- lardva-2.0.1.dist-info/licenses/LICENSE +21 -0
lardva/__init__.py
ADDED
lardva/__main__.py
ADDED
lardva/cli.py
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
"""Command-line interface: argparse, output and exit codes (SDD §6)."""
|
|
2
|
+
import argparse
|
|
3
|
+
import json
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from lardva import __version__, engine
|
|
7
|
+
from lardva.engine import LardvaError, RECORD
|
|
8
|
+
|
|
9
|
+
ORDER = ('add', 'replace', 'delete', 'adopt', 'keep', 'conflict', 'skip', 'blocked', 'preserve')
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class UsageError(Exception):
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Parser(argparse.ArgumentParser):
|
|
17
|
+
def error(self, message):
|
|
18
|
+
raise UsageError(message)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _parser():
|
|
22
|
+
p = Parser(prog='lardva', description='Install and manage the Lardva kit in a project.', allow_abbrev=False)
|
|
23
|
+
p.add_argument('--version', action='version', version=f'lardva {__version__}')
|
|
24
|
+
sub = p.add_subparsers(dest='command', required=True)
|
|
25
|
+
|
|
26
|
+
def cmd(name, help_text):
|
|
27
|
+
s = sub.add_parser(name, help=help_text, allow_abbrev=False)
|
|
28
|
+
s.add_argument('path', nargs='?', default='.', help='project directory (default: .)')
|
|
29
|
+
s.add_argument('--json', action='store_true', help='print one JSON object')
|
|
30
|
+
return s
|
|
31
|
+
|
|
32
|
+
s = cmd('init', 'install Lardva into a project')
|
|
33
|
+
s.add_argument('--dry-run', action='store_true')
|
|
34
|
+
s.add_argument('--adopt', action='store_true', help='take ownership of existing files with shipped Lardva content')
|
|
35
|
+
s.add_argument('--keep', action='append', default=[], metavar='PATH', help='keep this path as a user file')
|
|
36
|
+
cmd('status', 'show installed files and their state')
|
|
37
|
+
s = cmd('update', 'update managed files to this version')
|
|
38
|
+
s.add_argument('--dry-run', action='store_true')
|
|
39
|
+
s.add_argument('--keep', action='append', default=[], metavar='PATH', help='release this path to the user')
|
|
40
|
+
s.add_argument('--trust-record', metavar='CODE', help='confirm the install record shown with this code')
|
|
41
|
+
s = cmd('uninstall', 'remove unmodified managed files')
|
|
42
|
+
s.add_argument('--dry-run', action='store_true')
|
|
43
|
+
s.add_argument('--trust-record', metavar='CODE', help='confirm the install record shown with this code')
|
|
44
|
+
s = cmd('recover', 'recover an interrupted transaction')
|
|
45
|
+
s.add_argument('--dry-run', action='store_true')
|
|
46
|
+
cmd('doctor', 'read-only diagnostics')
|
|
47
|
+
return p
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _ascii(s):
|
|
51
|
+
return str(s).encode('ascii', 'replace').decode('ascii')
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def emit(args, exit_code, error, result):
|
|
55
|
+
if args.json:
|
|
56
|
+
if result is not None: # SDD §6.3 result shapes: presentation-only keys stay out of JSON
|
|
57
|
+
result = {k: v for k, v in result.items()
|
|
58
|
+
if k != 'summary' and not (args.command == 'doctor' and k == 'notes')}
|
|
59
|
+
print(json.dumps({'schema': 1, 'command': args.command, 'exit_code': exit_code,
|
|
60
|
+
'dry_run': bool(getattr(args, 'dry_run', False)), 'error': error, 'result': result},
|
|
61
|
+
sort_keys=True))
|
|
62
|
+
return exit_code
|
|
63
|
+
out = sys.stdout if exit_code == 0 else sys.stderr
|
|
64
|
+
if error: # summary line first, then grouped paths, then notes (SDD §6.3)
|
|
65
|
+
print(_ascii(f'lardva {args.command}: {error["message"]}'), file=out)
|
|
66
|
+
elif (result or {}).get('summary'):
|
|
67
|
+
print(_ascii(result['summary']), file=out)
|
|
68
|
+
if result and 'actions' in result:
|
|
69
|
+
groups = {}
|
|
70
|
+
for a in result['actions']:
|
|
71
|
+
groups.setdefault(a['action'], []).append(a)
|
|
72
|
+
for action in sorted(groups, key=lambda x: ORDER.index(x) if x in ORDER else 99):
|
|
73
|
+
print(f'{action}:', file=out)
|
|
74
|
+
for a in groups[action]:
|
|
75
|
+
print(_ascii(f' {a["path"]} ({a["reason"]})'), file=out)
|
|
76
|
+
if result.get('confirmation_code') and error and error['code'] == 'confirmation_required':
|
|
77
|
+
print(f'confirmation code: {result["confirmation_code"]}', file=out)
|
|
78
|
+
if result and 'files' in result:
|
|
79
|
+
for f in result['files']:
|
|
80
|
+
print(_ascii(f' {f["state"]:<9} {f["category"]:<12} {f["path"]}'), file=out)
|
|
81
|
+
if result and 'checks' in result:
|
|
82
|
+
for c in result['checks']:
|
|
83
|
+
print(_ascii(f' [{c["status"]}] {c["id"]}: {c["detail"]}'), file=out)
|
|
84
|
+
for n in (result or {}).get('notes', []):
|
|
85
|
+
print(_ascii(f'note: {n}'), file=out)
|
|
86
|
+
return exit_code
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _result(ctx, plan, from_version, code=None, extra=None):
|
|
90
|
+
actions = list(plan.actions) + list(extra or [])
|
|
91
|
+
return {'from_version': from_version, 'to_version': ctx.version, 'confirmation_code': code,
|
|
92
|
+
'actions': actions, 'notes': plan.notes}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _decisions_actions(decisions):
|
|
96
|
+
out = []
|
|
97
|
+
for d in decisions:
|
|
98
|
+
a = {'path': d['path'], 'action': d['action'], 'reason': d['reason']}
|
|
99
|
+
if 'expected' in d:
|
|
100
|
+
a['expected'] = d['expected']
|
|
101
|
+
out.append(a)
|
|
102
|
+
return out
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def run_mutating(args, ctx):
|
|
106
|
+
engine.journal_block(ctx)
|
|
107
|
+
rec, rec_bytes = engine.load_record(ctx, mutating=True)
|
|
108
|
+
cmd = args.command
|
|
109
|
+
if cmd == 'init':
|
|
110
|
+
if rec is not None and (args.adopt or args.keep):
|
|
111
|
+
raise LardvaError(2, 'usage', 'already installed; use lardva update --keep PATH')
|
|
112
|
+
replan = (lambda: engine.plan_init_existing(ctx)) if rec is not None else \
|
|
113
|
+
(lambda: engine.plan_init(ctx, adopt=args.adopt, keep=args.keep))
|
|
114
|
+
elif rec is None:
|
|
115
|
+
if getattr(args, 'trust_record', None) is not None:
|
|
116
|
+
raise LardvaError(2, 'usage', '--trust-record: there is no install record to confirm')
|
|
117
|
+
raise LardvaError(1, 'attention', 'not installed; run lardva init', None)
|
|
118
|
+
elif cmd == 'update':
|
|
119
|
+
replan = lambda: engine.plan_update(ctx, rec, rec_bytes, keep=args.keep)
|
|
120
|
+
else:
|
|
121
|
+
replan = lambda: engine.plan_uninstall(ctx, rec, rec_bytes)
|
|
122
|
+
plan = replan()
|
|
123
|
+
from_version = rec['lardva_version'] if rec else None
|
|
124
|
+
code = engine.confirmation_code(ctx, rec_bytes) if rec_bytes else None
|
|
125
|
+
trust = getattr(args, 'trust_record', None)
|
|
126
|
+
if trust is not None and trust != code:
|
|
127
|
+
raise LardvaError(2, 'usage', '--trust-record code does not match this project\'s current record; '
|
|
128
|
+
're-run without it to see the current code')
|
|
129
|
+
if plan.conflicts:
|
|
130
|
+
raise LardvaError(1, 'conflicts', f'{len(plan.conflicts)} conflict(s); nothing written', _result(ctx, plan, from_version, code))
|
|
131
|
+
if not plan.empty: # runtime blockers are reported before any write, dry run included (SDD §3.3)
|
|
132
|
+
try:
|
|
133
|
+
engine.check_runtime(ctx, *((f'{engine.BACKUP}/{i}', 'file') for i in range(len(plan.entries()))))
|
|
134
|
+
except LardvaError as e:
|
|
135
|
+
e.result = _result(ctx, plan, from_version, code)
|
|
136
|
+
raise
|
|
137
|
+
st = engine.load_state(ctx)
|
|
138
|
+
confirmed = rec_bytes is not None and st.get('record') == engine.text_hash(rec_bytes)
|
|
139
|
+
if plan.destructive and not confirmed and trust is None:
|
|
140
|
+
raise LardvaError(1, 'confirmation_required',
|
|
141
|
+
f'ownership not confirmed on this machine; review the actions above, then re-run with --trust-record {code}',
|
|
142
|
+
_result(ctx, plan, from_version, code))
|
|
143
|
+
result = _result(ctx, plan, from_version, code)
|
|
144
|
+
attention = plan.attention
|
|
145
|
+
if args.dry_run or plan.empty:
|
|
146
|
+
result['summary'] = ('dry run; nothing written' if args.dry_run else 'nothing to do')
|
|
147
|
+
if attention:
|
|
148
|
+
raise LardvaError(1, 'attention', 'some files need attention', result)
|
|
149
|
+
return result
|
|
150
|
+
if cmd == 'init' and rec is None:
|
|
151
|
+
set_record = engine.text_hash(engine.serialize_record(plan.new_record))
|
|
152
|
+
elif cmd == 'uninstall':
|
|
153
|
+
set_record = False
|
|
154
|
+
elif confirmed or trust is not None:
|
|
155
|
+
set_record = engine.text_hash(engine.serialize_record(plan.new_record))
|
|
156
|
+
else:
|
|
157
|
+
set_record = None
|
|
158
|
+
try:
|
|
159
|
+
engine.execute(ctx, plan, replan, set_record)
|
|
160
|
+
except LardvaError as e:
|
|
161
|
+
if isinstance(e.result, list):
|
|
162
|
+
e.result = {**result, 'actions': _decisions_actions(e.result)}
|
|
163
|
+
raise
|
|
164
|
+
result['summary'] = f'{cmd} complete'
|
|
165
|
+
if attention:
|
|
166
|
+
raise LardvaError(1, 'attention', 'complete; some files need attention', result)
|
|
167
|
+
return result
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def run_recover(args, ctx):
|
|
171
|
+
# SDD §6.2: a held lock is exit 4 before any journal check; probing never creates a lock file (AC-007f)
|
|
172
|
+
lock = None if args.dry_run else engine.Lock(ctx, create=False)
|
|
173
|
+
try:
|
|
174
|
+
js = engine.journal_status(ctx)
|
|
175
|
+
if js is not None and js[2] and lock is not None and lock.f is None:
|
|
176
|
+
lock = engine.Lock(ctx) # verified journal: take the real lock before writing
|
|
177
|
+
js = engine.journal_status(ctx)
|
|
178
|
+
if js is None:
|
|
179
|
+
raise LardvaError(1, 'nothing_to_recover', 'nothing to recover', _empty_result(ctx))
|
|
180
|
+
j, data, verified = js
|
|
181
|
+
st = engine.load_state(ctx)
|
|
182
|
+
if not verified:
|
|
183
|
+
actions = []
|
|
184
|
+
for e in j['entries']:
|
|
185
|
+
cur = engine.read_file(ctx.root, e['path']) if engine.disk_check(ctx.root, e['path']) is None else None
|
|
186
|
+
h = engine.raw_hash(cur) if cur is not None else None
|
|
187
|
+
state = 'at before' if h == e['before'] else 'at after' if h == e['after'] else 'differs'
|
|
188
|
+
actions.append({'path': e['path'], 'action': 'preserve', 'reason': f'{e["op"]}; {state}'})
|
|
189
|
+
raise LardvaError(1, 'unverified_journal',
|
|
190
|
+
'journal not verified by this machine\'s state; nothing written. Inspect the listed files, '
|
|
191
|
+
f'restore any by hand if needed, then delete {engine.JOURNAL}',
|
|
192
|
+
{**_empty_result(ctx), 'actions': actions})
|
|
193
|
+
try:
|
|
194
|
+
decisions = engine.undo(ctx, j, st, dry=args.dry_run)
|
|
195
|
+
except LardvaError as e:
|
|
196
|
+
if isinstance(e.result, list):
|
|
197
|
+
e.result = {**_empty_result(ctx), 'actions': _decisions_actions(e.result)}
|
|
198
|
+
raise
|
|
199
|
+
result = {**_empty_result(ctx), 'actions': _decisions_actions(decisions),
|
|
200
|
+
'summary': 'dry run; nothing written' if args.dry_run else 'recovery complete'}
|
|
201
|
+
if any(d['action'] == 'preserve' for d in decisions):
|
|
202
|
+
raise LardvaError(1, 'attention', 'recovery finished; preserved files need a manual step', result)
|
|
203
|
+
return result
|
|
204
|
+
finally:
|
|
205
|
+
if lock:
|
|
206
|
+
lock.release()
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _empty_result(ctx):
|
|
210
|
+
return {'from_version': None, 'to_version': ctx.version, 'confirmation_code': None, 'actions': [], 'notes': []}
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def load_record_after_journal(ctx, js, notes):
|
|
214
|
+
"""SDD §6.2 rule 3: a pending journal outranks record errors for status and doctor (exit 1, reported)."""
|
|
215
|
+
try:
|
|
216
|
+
return engine.load_record(ctx, mutating=False)
|
|
217
|
+
except LardvaError as e:
|
|
218
|
+
if js is None:
|
|
219
|
+
raise
|
|
220
|
+
notes.append(e.message)
|
|
221
|
+
return None, None
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def run_status(args, ctx):
|
|
225
|
+
js = engine.journal_status(ctx)
|
|
226
|
+
notes, attention = [], False
|
|
227
|
+
rec, rec_bytes = load_record_after_journal(ctx, js, notes)
|
|
228
|
+
if js is not None:
|
|
229
|
+
attention = True
|
|
230
|
+
notes.append('interrupted transaction; run lardva recover' if js[2] else
|
|
231
|
+
f'unverified journal; inspect files, then delete {engine.JOURNAL}')
|
|
232
|
+
if rec is None:
|
|
233
|
+
message = 'not installed; run lardva init' if js is None else 'pending transaction; see notes'
|
|
234
|
+
raise LardvaError(1, 'attention', message,
|
|
235
|
+
{'installed_version': None, 'package_version': ctx.version, 'confirmed': False,
|
|
236
|
+
'transaction': None if js is None else 'interrupted' if js[2] else 'unverified',
|
|
237
|
+
'gitignore_line': not engine.gitignore_missing(ctx.root), 'files': [], 'notes': notes})
|
|
238
|
+
files = []
|
|
239
|
+
for p, e in sorted(rec['files'].items()):
|
|
240
|
+
if p not in ctx.P:
|
|
241
|
+
state = 'unknown'
|
|
242
|
+
elif e['category'] != 'managed':
|
|
243
|
+
state = 'kept'
|
|
244
|
+
else:
|
|
245
|
+
why = engine.disk_check(ctx.root, p)
|
|
246
|
+
data = engine.read_file(ctx.root, p) if why is None else None
|
|
247
|
+
state = 'missing' if why is None and data is None else \
|
|
248
|
+
'clean' if data is not None and engine.text_hash(data) == e['sha256'] else 'modified'
|
|
249
|
+
attention = attention or state in ('unknown', 'missing', 'modified')
|
|
250
|
+
files.append({'path': p, 'category': e['category'], 'state': state})
|
|
251
|
+
confirmed = engine.load_state(ctx).get('record') == engine.text_hash(rec_bytes)
|
|
252
|
+
if not confirmed:
|
|
253
|
+
notes.append(f'record not confirmed on this machine (code {engine.confirmation_code(ctx, rec_bytes)})')
|
|
254
|
+
if engine.version_tuple(rec['lardva_version']) > engine.version_tuple(ctx.version):
|
|
255
|
+
attention = True
|
|
256
|
+
notes.append(f'record was written by lardva {rec["lardva_version"]}; upgrade lardva')
|
|
257
|
+
line = not engine.gitignore_missing(ctx.root)
|
|
258
|
+
if not line:
|
|
259
|
+
notes.append(f'.gitignore does not contain {engine.IGNORE_RULE}; run lardva init to add it')
|
|
260
|
+
for t in engine.tmp_leftovers(ctx):
|
|
261
|
+
notes.append(f'leftover temp file {t}; delete it')
|
|
262
|
+
result = {'installed_version': rec['lardva_version'], 'package_version': ctx.version, 'confirmed': confirmed,
|
|
263
|
+
'transaction': None if js is None else 'interrupted' if js[2] else 'unverified',
|
|
264
|
+
'gitignore_line': line, 'files': files, 'notes': notes,
|
|
265
|
+
'summary': f'{len(files)} recorded paths'}
|
|
266
|
+
if attention:
|
|
267
|
+
raise LardvaError(1, 'attention', 'some files need attention', result)
|
|
268
|
+
return result
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def main(argv=None):
|
|
272
|
+
argv = sys.argv[1:] if argv is None else argv
|
|
273
|
+
try:
|
|
274
|
+
args = _parser().parse_args(argv)
|
|
275
|
+
except UsageError as e:
|
|
276
|
+
args = argparse.Namespace(command=next((a for a in argv if not a.startswith('-')), None),
|
|
277
|
+
json='--json' in argv, dry_run='--dry-run' in argv)
|
|
278
|
+
return emit(args, 2, {'code': 'usage', 'message': str(e)}, None)
|
|
279
|
+
ctx = None
|
|
280
|
+
try:
|
|
281
|
+
ctx = engine.Ctx(args.path)
|
|
282
|
+
ctx.check_state_dir()
|
|
283
|
+
if args.command == 'doctor':
|
|
284
|
+
from lardva import doctor
|
|
285
|
+
result = doctor.run(ctx)
|
|
286
|
+
elif args.command == 'status':
|
|
287
|
+
result = run_status(args, ctx)
|
|
288
|
+
elif args.command == 'recover':
|
|
289
|
+
result = run_recover(args, ctx)
|
|
290
|
+
else:
|
|
291
|
+
result = run_mutating(args, ctx)
|
|
292
|
+
return emit(args, 0, None, result)
|
|
293
|
+
except OSError as e: # an unexpected I/O failure; a journal, if any, is left for lardva recover
|
|
294
|
+
err = LardvaError(4 if args.command == 'recover' else 1, 'interrupted' if args.command == 'recover' else 'blocked',
|
|
295
|
+
f'I/O error: {e}; nothing further written' + ('; fix it and re-run lardva recover'
|
|
296
|
+
if args.command == 'recover' else ''))
|
|
297
|
+
return _emit_error(args, ctx, err)
|
|
298
|
+
except LardvaError as e:
|
|
299
|
+
return _emit_error(args, ctx, e)
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def _emit_error(args, ctx, e):
|
|
303
|
+
result = None
|
|
304
|
+
if e.exit_code in (1, 4) and e.code != 'locked':
|
|
305
|
+
result = e.result
|
|
306
|
+
if result is None and ctx is not None:
|
|
307
|
+
if args.command == 'doctor':
|
|
308
|
+
result = {'checks': [{'id': 'io', 'status': 'error', 'detail': e.message}]}
|
|
309
|
+
elif args.command == 'status':
|
|
310
|
+
result = {'installed_version': None, 'package_version': ctx.version, 'confirmed': False,
|
|
311
|
+
'transaction': None, 'gitignore_line': False, 'files': [], 'notes': [e.message]}
|
|
312
|
+
else:
|
|
313
|
+
result = _empty_result(ctx)
|
|
314
|
+
return emit(args, e.exit_code, {'code': e.code, 'message': e.message}, result)
|
lardva/doctor.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Read-only diagnostics (SDD §9). Executes nothing; uses shutil.which only."""
|
|
2
|
+
import os
|
|
3
|
+
import shutil
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from lardva import engine
|
|
7
|
+
from lardva.engine import LardvaError
|
|
8
|
+
|
|
9
|
+
POINTER = 'Add a line such as `Follow .agents/POLICY.md and the skills in .agents/skills/ (Lardva).` to {}. Lardva will not edit this file.'
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def run(ctx):
|
|
13
|
+
checks = []
|
|
14
|
+
|
|
15
|
+
def check(cid, ok, detail, failing='warning'):
|
|
16
|
+
checks.append({'id': cid, 'status': 'ok' if ok is True else (ok or failing), 'detail': detail})
|
|
17
|
+
|
|
18
|
+
check('python', sys.version_info >= (3, 11), f'Python {sys.version_info[0]}.{sys.version_info[1]}', 'error')
|
|
19
|
+
check('git', bool(shutil.which('git')), 'git on PATH' if shutil.which('git') else 'git not found on PATH')
|
|
20
|
+
root, cur = ctx.root, ctx.root
|
|
21
|
+
while not os.path.exists(os.path.join(cur, '.git')) and os.path.dirname(cur) != cur:
|
|
22
|
+
cur = os.path.dirname(cur)
|
|
23
|
+
check('git-worktree', True if os.path.exists(os.path.join(cur, '.git')) else 'info',
|
|
24
|
+
'inside a git worktree' if os.path.exists(os.path.join(cur, '.git')) else 'not inside a git worktree')
|
|
25
|
+
js = engine.journal_status(ctx) # raises invalid_journal (exit 2)
|
|
26
|
+
from lardva.cli import load_record_after_journal
|
|
27
|
+
notes = []
|
|
28
|
+
rec, rec_bytes = load_record_after_journal(ctx, js, notes) # newer_schema (3) / invalid (2) unless a journal exists
|
|
29
|
+
if notes:
|
|
30
|
+
check('record', 'error', notes[0])
|
|
31
|
+
elif rec is None:
|
|
32
|
+
check('record', 'warning', 'not installed; run lardva init')
|
|
33
|
+
else:
|
|
34
|
+
bad = [p for p, e in rec['files'].items() if p in ctx.P and e['category'] == 'managed'
|
|
35
|
+
and (engine.disk_check(ctx.root, p) or engine.read_file(ctx.root, p) is None
|
|
36
|
+
or engine.text_hash(engine.read_file(ctx.root, p)) != e['sha256'])]
|
|
37
|
+
unknown = [p for p in rec['files'] if p not in ctx.P]
|
|
38
|
+
check('record', not bad and not unknown,
|
|
39
|
+
f'{len(rec["files"])} recorded paths; {len(bad)} modified or missing; {len(unknown)} unknown to this version')
|
|
40
|
+
newer = engine.version_tuple(rec['lardva_version']) > engine.version_tuple(ctx.version)
|
|
41
|
+
check('version', not newer, f'record {rec["lardva_version"]}, package {ctx.version}'
|
|
42
|
+
+ ('; upgrade lardva' if newer else ''))
|
|
43
|
+
confirmed = engine.load_state(ctx).get('record') == engine.text_hash(rec_bytes)
|
|
44
|
+
check('confirmation', True if confirmed else 'info', 'record confirmed on this machine' if confirmed else
|
|
45
|
+
f'record not confirmed on this machine (code {engine.confirmation_code(ctx, rec_bytes)})')
|
|
46
|
+
for name in ('AGENTS.md', 'CLAUDE.md'):
|
|
47
|
+
e = rec['files'].get(name)
|
|
48
|
+
data = engine.read_file(root, name) if engine.disk_check(root, name) is None else None
|
|
49
|
+
if e and e['category'] == 'external' and data is not None and b'.agents/' not in data:
|
|
50
|
+
check(f'pointer-{name}', 'warning', f'{name} is kept and does not mention .agents/. ' + POINTER.format(name))
|
|
51
|
+
check('journal', js is None, 'no pending transaction' if js is None else
|
|
52
|
+
'interrupted transaction; run lardva recover' if js[2] else
|
|
53
|
+
f'unverified journal; inspect files, then delete {engine.JOURNAL}')
|
|
54
|
+
line = not engine.gitignore_missing(root)
|
|
55
|
+
check('gitignore', line, f'.gitignore covers {engine.IGNORE_RULE}' if line else
|
|
56
|
+
f'.gitignore lacks {engine.IGNORE_RULE}; runtime files and backups could be committed; run lardva init')
|
|
57
|
+
leftovers = engine.tmp_leftovers(ctx)
|
|
58
|
+
check('temp-files', True if not leftovers else 'info',
|
|
59
|
+
'no leftover temp files' if not leftovers else f'{len(leftovers)} leftover .lardva-*.tmp file(s); delete them')
|
|
60
|
+
check('context-manifest', True if os.path.isfile(os.path.join(root, '.agents', 'context', 'manifest.json')) else 'info',
|
|
61
|
+
'context manifest present' if os.path.isfile(os.path.join(root, '.agents', 'context', 'manifest.json'))
|
|
62
|
+
else 'no .agents/context/manifest.json yet; run context-sync')
|
|
63
|
+
found = [n for n in ('claude', 'codex') if shutil.which(n)]
|
|
64
|
+
check('providers', bool(found), f'on PATH: {", ".join(found)}' if found else 'neither claude nor codex found on PATH')
|
|
65
|
+
result = {'checks': checks, 'notes': [], 'summary': f'{len(checks)} checks'}
|
|
66
|
+
if any(c['status'] in ('warning', 'error') for c in checks):
|
|
67
|
+
raise LardvaError(1, 'attention', 'doctor found warnings', result)
|
|
68
|
+
return result
|