askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
resolve_run.py · 5.8 KB · 144 lines
| 1 | """action_resolve_run -- a person decides a run the machine refused to. |
| 2 | |
| 3 | POST {run_id, decision: "charge" | "refund", note?} |
| 4 | |
| 5 | `needs_review` is the one state this system creates deliberately and |
| 6 | cannot leave on its own. A run submitted to a provider whose outcome was |
| 7 | never learned has money genuinely undecided: releasing the hold gives |
| 8 | away what we may already have paid, charging bills a customer for work |
| 9 | that may never have arrived. The sweeper stops there on purpose. |
| 10 | |
| 11 | Stopping without a way to continue is not a design, it is an abandoned |
| 12 | queue -- so this is the other half. It is the ONLY hand that moves a run |
| 13 | out of needs_review, and it is a person's. |
| 14 | |
| 15 | ## Why the two decisions are not symmetric |
| 16 | |
| 17 | **refund** is the ordinary failure settlement, unchanged: release the |
| 18 | hold, charge nothing. The operator has decided the customer should not |
| 19 | pay, whatever it cost us. That cost is already recorded on the run |
| 20 | (provider_cost_cents), so eating it is a visible number rather than a |
| 21 | silent one. |
| 22 | |
| 23 | **charge** is the release-plus-debit that a successful run gets. The |
| 24 | operator has confirmed the work arrived. Note what this does NOT do: it |
| 25 | never re-runs anything and never contacts the provider. This verb settles |
| 26 | money against a job whose fate somebody has established by other means -- |
| 27 | looking in the provider's dashboard, or at the file that landed. |
| 28 | |
| 29 | ## What it refuses |
| 30 | |
| 31 | Only a run actually in `needs_review`. A terminal run is already settled |
| 32 | and re-settling it is the double-charge the provenance markers exist to |
| 33 | prevent (they would no-op it, but a verb that silently does nothing is |
| 34 | worse than one that says why). And a run still `running` is not yours to |
| 35 | decide -- the provider may still answer, and polling is cheaper and more |
| 36 | truthful than a guess. |
| 37 | |
| 38 | Settlement stays idempotent by the same release/charge markers everything |
| 39 | else uses, so a double-submitted decision writes money once. |
| 40 | """ |
| 41 | |
| 42 | import os |
| 43 | |
| 44 | import object_records |
| 45 | import object_template_runs |
| 46 | |
| 47 | ACTOR = "action_resolve_run" |
| 48 | RUNS = "template_runs" |
| 49 | DECISIONS = ("charge", "refund") |
| 50 | |
| 51 | |
| 52 | def _base_dir(): |
| 53 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 54 | |
| 55 | |
| 56 | def _text(value): |
| 57 | return str(value if value is not None else "").strip() |
| 58 | |
| 59 | |
| 60 | def _now(): |
| 61 | from datetime import datetime, timezone |
| 62 | return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") |
| 63 | |
| 64 | |
| 65 | def POST(request): |
| 66 | request = request or {} |
| 67 | base = _base_dir() |
| 68 | |
| 69 | identity = request.get("_identity") or {} |
| 70 | user_id = _text(identity.get("user_id")) |
| 71 | if not user_id: |
| 72 | return {"status": 401, |
| 73 | "error": "Resolving a run decides who pays for it, so it " |
| 74 | "requires an account."} |
| 75 | |
| 76 | run_id = _text(request.get("run_id")) |
| 77 | if not run_id: |
| 78 | return {"status": 400, "error": "run_id is required."} |
| 79 | |
| 80 | decision = _text(request.get("decision")).lower() |
| 81 | if decision not in DECISIONS: |
| 82 | return {"status": 400, |
| 83 | "error": (f"decision must be one of {', '.join(DECISIONS)}. " |
| 84 | f"'charge' settles the run as delivered (release " |
| 85 | f"the hold, then debit the stamped price); " |
| 86 | f"'refund' settles it as not delivered (release " |
| 87 | f"the hold, charge nothing).")} |
| 88 | |
| 89 | try: |
| 90 | run = object_records.get_collection_record(RUNS, run_id, base_dir=base) |
| 91 | except Exception: |
| 92 | return {"status": 404, "error": f"There is no run {run_id!r}."} |
| 93 | if run is None: |
| 94 | return {"status": 404, "error": f"There is no run {run_id!r}."} |
| 95 | |
| 96 | # Owner or admin. A run's money is its owner's money. |
| 97 | is_admin = bool(identity.get("is_admin")) or "admin" in (identity.get("roles") or []) |
| 98 | if not is_admin and _text(run.get("owner_id")) != user_id: |
| 99 | return {"status": 403, |
| 100 | "error": "Only the run's owner or an admin can resolve it."} |
| 101 | |
| 102 | status = _text(run.get("status")) |
| 103 | if status != object_template_runs.NEEDS_REVIEW_STATUS: |
| 104 | return {"status": 409, |
| 105 | "error": (f"Run {run_id} is {status!r}, not " |
| 106 | f"{object_template_runs.NEEDS_REVIEW_STATUS!r}. Only " |
| 107 | f"a run the machine refused to decide is yours to " |
| 108 | f"decide: a terminal run is already settled, and a " |
| 109 | f"running one may still be answered by the provider " |
| 110 | f"-- polling is cheaper and more truthful than a " |
| 111 | f"guess.")} |
| 112 | |
| 113 | succeeded = decision == "charge" |
| 114 | |
| 115 | # Money first, then status -- the same ordering the runner uses, and |
| 116 | # for the same reason: a crash between them leaves money correct and |
| 117 | # a status the next pass can see is behind, rather than the reverse. |
| 118 | try: |
| 119 | entries = object_records.read_collection_records("wallet_entries", |
| 120 | base_dir=base) |
| 121 | except Exception: |
| 122 | entries = [] |
| 123 | if object_template_runs.already_settled(run_id, entries): |
| 124 | settled = "already settled" |
| 125 | else: |
| 126 | written = object_template_runs.settlement(run, succeeded=succeeded) |
| 127 | for entry in written: |
| 128 | object_records.create_collection_record( |
| 129 | "wallet_entries", entry, base_dir=base, actor=user_id) |
| 130 | settled = "settled" if written else "nothing to settle (free run)" |
| 131 | |
| 132 | note = _text(request.get("note")) |
| 133 | resolution = (f"Resolved by {user_id} as {decision} on {_now()}." |
| 134 | + (f" {note}" if note else "")) |
| 135 | object_records.update_collection_record( |
| 136 | RUNS, run_id, |
| 137 | {"status": "succeeded" if succeeded else "failed", |
| 138 | "error": (_text(run.get("error")) + "\n\n" + resolution)[:2000], |
| 139 | "finished_at": _now()}, |
| 140 | base_dir=base, actor=user_id) |
| 141 | |
| 142 | return {"status": 200, "ok": True, "run_id": run_id, |
| 143 | "decision": decision, "settlement": settled, |
| 144 | "resolved_by": user_id} |