123module cli45import json26import src.fmt7import src.history8import src.snapshot9101112pub fn cmd_save(raw []string, is_checkpoint bool) int {13 verb := if is_checkpoint { 'checkpoint' } else { 'save' }14 args := parse(verb, raw, Options{15 values: ['agent', 'session']16 flags: ['json']17 }) or { return usage_error(err.msg()) }18 if args.positional.len > 1 {19 example := if is_checkpoint { 'before x' } else { 'fix the bug' }20 return usage_error('${verb} takes one message — quote it: koh ${verb} "${example}"')21 }22 message := args.positional_at(0)23 if message == '' {24 example := if is_checkpoint {25 'before combine replay engine'26 } else {27 'fix off-by-one in chunk boundary'28 }29 return usage_error('${verb} needs a message, e.g. koh ${verb} "${example}"')30 }31 store := open_store() or { return refuse(args.json(), err.msg()) }32 defer {33 store.close()34 }35 mut hashing := reading()36 walked := snapshot.walk_report(store, store.root, snapshot.load(store.root), mut hashing) or {37 return refuse(args.json(), err.msg())38 }39 save := history.save_worktree(store, store.root, snapshot.load(store.root), history.SaveInput{40 message: message41 agent: args.value('agent')42 session: args.value('session')43 is_checkpoint: is_checkpoint44 storing: fn (done int, total int) {45 if done % 100 == 1 || done == total {46 working('storing ${done} / ${total} files')47 }48 }49 }) or { return refuse(args.json(), err.msg()) }5051 files := history.files_of(store, save.id) or { []snapshot.FileEntry{} }52 if args.json() {53 emit_json(json2.encode(SaveOut{54 id: save.id55 parent: save.parent56 message: save.message57 created_at: save.created_at58 machine: save.machine59 is_checkpoint: save.is_checkpoint60 agent: save.agent61 session: save.session62 files: files.len63 size_bytes: save.size_bytes64 unsafe_paths: walked.unsafe_paths65 }))66 auto_offer(store, true)67 return 068 }69 for path in walked.unsafe_paths {70 warn('not saved: unsafe name ${path}')71 }72 ok('saved [${save.id}] ${save.message}')73 mut detail := []string{}74 if save.is_checkpoint {75 detail << 'checkpoint'76 }77 detail << fmt.stamp(save.created_at)78 detail << fmt.plural(files.len, 'file')79 detail << fmt.human_bytes(save.size_bytes)80 note(detail.join(' · '))81 mut attribution := []string{}82 if save.agent != '' {83 attribution << 'agent: ${save.agent}'84 }85 if save.session != '' {86 attribution << 'session: ${save.session}'87 }88 if attribution.len > 0 {89 note(attribution.join(' · '))90 }91 auto_offer(store, false)92 return 093}94959697struct SaveOut {98 id string99 parent string100 message string101 created_at i64102 machine string103 is_checkpoint bool104 agent string105 session string106 files int107 size_bytes i64108 unsafe_paths []string109}