Skip to content

Commit fb85ebb

Browse files
committed
Restore main settlement closeout after the merge
The first merge tree took this branch's settlement readback whole, which dropped current main's pending-closeout parameter, its PowerShell route rendering, and the settlement recovery markdown. Rebuild those two files with a three-way merge so the scheduler fence keeps its tolerant history read while refresh-state recovery keeps working. Signed-off-by: ChengLong GU <88359731+gcl-coder@users.noreply.github.com>
1 parent dedc271 commit fb85ebb

2 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎loopx/control_plane/quota/settlement.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,22 @@ def attach_settlement_progress(
233233
}
234234
# PowerShell quoting applies to every argument, even with an unquoted launcher.
235235
# Keep the argv for the composed CLI so it does not have to parse shell text.
236-
if os.name == "nt":
236+
if os.name == "nt" or route[0].lower().endswith(".ps1"):
237237
argv = shlex.split(command)
238238
owed.update(command=render_owed_host_command(argv), command_argv=argv)
239239
payload["settlement_owed"] = owed
240240

241241

242242
def render_settlement_progress_markdown(payload: dict[str, Any]) -> list[str]:
243+
recovery = payload.get("settlement_recovery")
244+
if isinstance(recovery, dict):
245+
identity = recovery["settlement_identity"]
246+
lines = [f"- Pending settlement: `{identity['turn_instance_id']}`", recovery["instruction"]]
247+
for command in recovery["next_cli_actions"]:
248+
lines.extend(["", "```sh", command, "```"])
249+
return lines
250+
if payload.get("settlement_recovery_error"):
251+
return [f"- Settlement recovery could not be read: {payload['settlement_recovery_error']}"]
243252
progress = payload.get("settlement_progress")
244253
if not isinstance(progress, dict):
245254
return []
@@ -457,6 +466,7 @@ def read_heartbeat_settlement(
457466
infer_turn_instance_id: bool = False,
458467
allow_unbound_binding: bool = False,
459468
resolve_original_binding: bool = False,
469+
pending_closeout: bool = False,
460470
refresh_retry: dict[str, Any] | None = None,
461471
registry_path: Path | None = None,
462472
goal_ref: Mapping[str, Any] | None = None,
@@ -489,6 +499,7 @@ def read_heartbeat_settlement(
489499
else {}
490500
),
491501
**({"resolve_original_binding": True} if resolve_original_binding else {}),
502+
**({"pending_closeout": True} if pending_closeout else {}),
492503
**({"refresh_retry": refresh_retry} if refresh_retry is not None else {}),
493504
**({"borrow_source_admission": True} if borrow_source_admission else {}),
494505
**(

‎loopx/control_plane/quota/settlement_readback.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ interface ReadbackRequest {
108108
infer_turn_instance_id: boolean;
109109
allow_unbound_binding: boolean;
110110
resolve_original_binding: boolean;
111+
pending_closeout: boolean;
111112
borrow_source_admission: boolean;
112113
include_record_bodies: boolean;
113114
tolerate_malformed_history: boolean;
@@ -236,6 +237,13 @@ function decodeRequest(
236237
if (request.resolve_original_binding === true && request.infer_turn_instance_id) {
237238
throw new EffectRuntimeRequestError("original binding requires an explicit Turn identity");
238239
}
240+
if (request.pending_closeout !== undefined && typeof request.pending_closeout !== "boolean") {
241+
throw new EffectRuntimeRequestError("pending_closeout must be a boolean");
242+
}
243+
if (request.pending_closeout === true && (request.infer_turn_instance_id || request.allow_unbound_binding
244+
|| request.refresh_retry != null || request.heartbeat_reentry_guard != null || request.spend_precheck === true)) {
245+
throw new EffectRuntimeRequestError("pending closeout is a read-only receipt recovery, not inference or settlement admission");
246+
}
239247
if (
240248
request.borrow_source_admission !== undefined
241249
&& typeof request.borrow_source_admission !== "boolean"
@@ -290,6 +298,7 @@ function decodeRequest(
290298
infer_turn_instance_id: request.infer_turn_instance_id,
291299
allow_unbound_binding: request.allow_unbound_binding,
292300
resolve_original_binding: request.resolve_original_binding === true,
301+
pending_closeout: request.pending_closeout === true,
293302
borrow_source_admission: request.borrow_source_admission === true,
294303
include_record_bodies: request.include_record_bodies !== false,
295304
tolerate_malformed_history: request.tolerate_malformed_history === true,
@@ -1119,7 +1128,8 @@ function owedPriorTurnFromSnapshot(
11191128
request: ReadbackRequest,
11201129
snapshot: QuotaSettlementReadbackSnapshot,
11211130
agentId: string,
1122-
turnInstanceId: string,
1131+
turnInstanceId: string | null,
1132+
options?: { includeAdmittedReplans: boolean },
11231133
): OwedPriorTurn | null {
11241134
const receipts = heartbeatReceiptsFromEvents(snapshot.events, request.goal_id, agentId)
11251135
.filter((event) => quotaOwnerOwnsProjection(request.owner, event.goal_ref));
@@ -1128,6 +1138,7 @@ function owedPriorTurnFromSnapshot(
11281138
request.goal_id,
11291139
agentId,
11301140
turnInstanceId,
1141+
options,
11311142
);
11321143
for (const selected of candidates) {
11331144
// A committed monitor poll is its own exact no-spend closeout. This is the
@@ -1151,6 +1162,7 @@ function owedPriorTurnFromSnapshot(
11511162
infer_turn_instance_id: false,
11521163
allow_unbound_binding: false,
11531164
resolve_original_binding: false,
1165+
pending_closeout: false,
11541166
refresh_retry: null,
11551167
heartbeat_reentry_guard: null,
11561168
},
@@ -1165,6 +1177,48 @@ function owedPriorTurnFromSnapshot(
11651177
return null;
11661178
}
11671179

1180+
/** Resolve pending recovery without another provider read or a latest-Run heuristic. */
1181+
function readPendingCloseoutFromRequest(
1182+
request: ReadbackRequest,
1183+
snapshot: QuotaSettlementReadbackSnapshot,
1184+
): JsonObject {
1185+
const explicitAgentId = normalizeAgentId(request.agent_id);
1186+
if (explicitAgentId === null) {
1187+
throw new EffectRuntimeRequestError("pending closeout requires an explicit Agent");
1188+
}
1189+
const notFound = {schema_version: QUOTA_SETTLEMENT_READBACK_RESULT_SCHEMA, found: false};
1190+
const turn = request.turn_instance_id ?? owedPriorTurnFromSnapshot(
1191+
request, snapshot, explicitAgentId, null, { includeAdmittedReplans: true },
1192+
)?.prior_turn_instance_id;
1193+
if (!turn) return notFound;
1194+
// Selection, original binding validation and receipt progress share this
1195+
// immutable snapshot and admitted owner. Maintenance Run classifications
1196+
// cannot conceal the guard's debt or select another Agent's work.
1197+
const result = readQuotaSettlementFromRequest({...request, turn_instance_id: turn,
1198+
pending_closeout: false, resolve_original_binding: true}, snapshot, false);
1199+
const identity = jsonObject(jsonObject(jsonObject(result.identity)?.result)?.value);
1200+
const admission = jsonObject(result.native_child_admission);
1201+
const originalReceipt = jsonObject(result.heartbeat_receipt);
1202+
const workAdmitted = originalReceipt !== null &&
1203+
heartbeatReceiptFactFromEvent(originalReceipt).settlement_effect_id !== null &&
1204+
(admission?.report_permission === "new_operation" ||
1205+
admission?.report_permission === "existing_operation_only") &&
1206+
(!identity?.replan_obligation_id ||
1207+
jsonObject(result.semantic_replan_guard)?.selected_obligation_id === identity.replan_obligation_id);
1208+
const partialSettlement = [result.writeback, result.spend].some(step =>
1209+
jsonObject(jsonObject(step)?.payload)?.ok === true);
1210+
// Binding a candidate is not work admission. Reuse the receipt owner's
1211+
// qualified work facts, while retaining verified partial commits even if
1212+
// the original guard did not admit new work. Identity failures stay visible.
1213+
if (identity !== null && !workAdmitted && !partialSettlement) return notFound;
1214+
if (jsonObject(jsonObject(result.settlement)?.payload)?.ok === true ||
1215+
(identity?.todo_id && committedMonitorPollFromSnapshot(snapshot, {
1216+
goal_id: request.goal_id, agent_id: explicitAgentId, turn_instance_id: turn,
1217+
todo_id: String(identity.todo_id),
1218+
}, request.owner) !== null)) return notFound;
1219+
return result;
1220+
}
1221+
11681222
function readQuotaSettlementFromRequest(
11691223
request: ReadbackRequest,
11701224
snapshot: QuotaSettlementReadbackSnapshot,
@@ -1180,6 +1234,7 @@ function readQuotaSettlementFromRequest(
11801234
);
11811235
}
11821236
const explicitAgentId = normalizeAgentId(request.agent_id);
1237+
if (request.pending_closeout) return readPendingCloseoutFromRequest(request, snapshot);
11831238
// Only latest-Turn inference needs the full run history. Explicit readback
11841239
// must use the existing Turn indexes before applying the Goal owner fence;
11851240
// otherwise a batch over N Turns repeatedly scans all N receipts.

0 commit comments

Comments
 (0)