Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions loopx/control_plane/effect_program.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {assertActionSelectionConsistency} from "./work_items/action_selection_admission.ts";
import { EffectRuntimeRequestError } from "./effect_runtime_errors.ts";
import { isStringLiteral, type JsonObject } from "./runtime_decode.ts";
import { isStringLiteral, pythonString, type JsonObject } from "./runtime_decode.ts";
import { decodeInteractionContract } from "./work_items/interaction_contract.ts";
import { EffectiveAction } from "./quota/effective_action.generated.ts";
import {BARE_SHA256_PATTERN} from "./content_digest.ts";
Expand Down Expand Up @@ -258,13 +258,6 @@ function asObject(value: unknown): JsonObject {
: {};
}

function pythonString(value: unknown): string {
if (value === null || value === undefined) return "None";
if (value === true) return "True";
if (value === false) return "False";
return String(value);
}

function truthyString(value: unknown): string {
return value ? pythonString(value) : "";
}
Expand Down
33 changes: 10 additions & 23 deletions loopx/control_plane/quota/turn_envelope.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,23 @@ def quota_capability_automation_source(payload: Mapping[str, Any]) -> dict[str,
return _turn_envelope_result("capability_automation_source", payload=dict(payload))


def turn_envelope_replan_settlement_identity(
envelope: Mapping[str, Any], *, turn_instance_id: str | None,
def _turn_settlement_identity(
operation: str, envelope: Mapping[str, Any], *, turn_instance_id: str | None,
) -> dict[str, Any] | None:
result = _turn_envelope_result("replan_settlement_identity", envelope=dict(envelope),
turn_instance_id=turn_instance_id)
identity = result.get("identity")
identity = _turn_envelope_result(operation, envelope=dict(envelope), turn_instance_id=turn_instance_id).get("identity")
if identity is None:
return None
if not isinstance(identity, Mapping):
raise RuntimeError("TypeScript Turn settlement identity must be an object")
return dict(identity)


def turn_envelope_replan_settlement_identity(
envelope: Mapping[str, Any], *, turn_instance_id: str | None,
) -> dict[str, Any] | None:
return _turn_settlement_identity("replan_settlement_identity", envelope, turn_instance_id=turn_instance_id)


def compose_turn_decision(
payload: Mapping[str, Any], *, scheduler_execution_context: (
Mapping[str, Any] | SchedulerExecutionContextResolution | None
Expand Down Expand Up @@ -347,11 +351,7 @@ def assert_complete_turn_decision(payload: Mapping[str, Any]) -> None:
def turn_envelope_scoped_settlement_identity(
envelope: Mapping[str, Any], *, turn_instance_id: str | None,
) -> dict[str, Any] | None:
identity = _turn_envelope_result("scoped_settlement_identity", envelope=dict(envelope),
turn_instance_id=turn_instance_id).get("identity")
if identity is not None and not isinstance(identity, Mapping):
raise RuntimeError("TypeScript Turn settlement identity must be an object")
return dict(identity) if identity is not None else None
return _turn_settlement_identity("scoped_settlement_identity", envelope, turn_instance_id=turn_instance_id)


def build_turn_envelope(
Expand All @@ -363,19 +363,6 @@ def build_turn_envelope(
captured_decision_path: str | None = None,
) -> dict[str, Any]:
"""Project one full quota decision through the canonical TS transaction."""

return _build_turn_projection(payload,
scheduler_execution_context=scheduler_execution_context,
captured_decision_path=captured_decision_path)


def _build_turn_projection(
payload: Mapping[str, Any], *, scheduler_execution_context: (
Mapping[str, Any] | SchedulerExecutionContextResolution | None
) = None,
captured_decision_path: str | None = None,
) -> dict[str, Any]:

resolution = resolve_scheduler_execution_context(scheduler_execution_context)
scheduler_execution_args: object = ""
if resolution.ok and resolution.context is not None:
Expand Down
1,466 changes: 122 additions & 1,344 deletions loopx/control_plane/quota/turn_envelope.ts

Large diffs are not rendered by default.

197 changes: 197 additions & 0 deletions loopx/control_plane/quota/turn_envelope_codec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import { createHash } from "node:crypto";
import { inflateSync } from "node:zlib";

import { authorityUnicodeCompare } from "../coordination/authority_store_codec.ts";
import { contextSourceIdentity } from "./diagnostic_projection.ts";
import { EffectRuntimeRequestError } from "../effect_runtime_errors.ts";
import { pythonString, requireJsonObject, type JsonObject } from "../runtime_decode.ts";

export const COMPLETE_TURN_DECISION_VERSION = "loopx_turn_envelope_v1";
// Sparse producer metadata is a different identity from the earlier
// experimental all-float binding; its saved contracts need their pinned reader.
export const COMPLETE_TURN_HASH_CODEC = "context_source_numbers_v2";

const EXECUTABLE_CLI_ARGS_MAX_ITEMS = 64;
const EXECUTABLE_CLI_ARGS_MAX_ITEM_CHARS = 512;
const EXECUTABLE_CLI_ARGS_MAX_TOTAL_CHARS = 2_048;

export function object(value: unknown): JsonObject {
return typeof value === "object" && value !== null && !Array.isArray(value)
? { ...(value as JsonObject) }
: {};
}

export function pickNonNull(source: JsonObject, fields: readonly string[]): JsonObject {
const picked: JsonObject = {};
for (const field of fields) {
if (source[field] !== null && source[field] !== undefined) picked[field] = source[field];
}
return picked;
}

export function pickOwn(source: JsonObject, fields: readonly string[]): JsonObject {
const picked: JsonObject = {};
for (const field of fields) {
if (Object.hasOwn(source, field)) picked[field] = source[field];
}
return picked;
}

export function isNonEmptyFilePath(value: unknown): value is string {
return typeof value === "string" && value.trim().length > 0 && !value.includes("\0");
}

export function scalarString(value: unknown, label: string, fallback = ""): string {
if (value === null || value === undefined || value === "") return fallback;
if (typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean") {
throw new EffectRuntimeRequestError(`${label} must be scalar-compatible`);
}
return pythonString(value);
}

export function integer(value: unknown, label: string): number {
if (value === null || value === undefined || value === "") return 0;
if (typeof value === "boolean") return Number(value);
if (typeof value === "number" && Number.isFinite(value)) return Math.trunc(value);
if (typeof value === "string" && /^[+-]?\d+$/u.test(value.trim())) return Number.parseInt(value, 10);
throw new EffectRuntimeRequestError(`${label} must be integer-compatible`);
}

export function text(value: unknown, limit: number): string | null {
if (value === null || value === undefined) return null;
const compact = scalarString(value, "text value").trim().split(/\s+/u).join(" ");
if (!compact) return null;
return compact.length <= limit ? compact : `${compact.slice(0, Math.max(0, limit - 3)).trimEnd()}...`;
}

export function textList(value: unknown, limit: number, itemLimit = 240): string[] {
if (!Array.isArray(value)) return [];
const result: string[] = [];
for (const item of value) {
const rendered = text(item, itemLimit);
if (!rendered || result.includes(rendered)) continue;
result.push(rendered);
if (result.length >= limit) break;
}
return result;
}

export function compactFields(
value: unknown, fields: readonly string[], limits: Readonly<Record<string, number>> = {},
): JsonObject {
const source = object(value);
const compact: JsonObject = {};
for (const field of fields) {
const raw = source[field];
if (raw === null || raw === undefined) continue;
if (limits[field] !== undefined) {
const rendered = text(raw, limits[field]);
if (rendered) compact[field] = rendered;
} else if (Array.isArray(raw)) {
const values = textList(raw, 6, 140);
if (values.length > 0) compact[field] = values;
} else compact[field] = raw;
}
return compact;
}

export function executableCliArgs(value: unknown): string[] {
if (!Array.isArray(value) || value.length === 0 || value.length > EXECUTABLE_CLI_ARGS_MAX_ITEMS) return [];
const result: string[] = [];
let totalChars = 0;
for (const item of value) {
if (typeof item !== "string" || !item || item.length > EXECUTABLE_CLI_ARGS_MAX_ITEM_CHARS) return [];
totalChars += item.length + 1;
if (totalChars > EXECUTABLE_CLI_ARGS_MAX_TOTAL_CHARS) return [];
result.push(item);
}
return result;
}

export function canonicalValue(value: unknown): unknown {
if (Array.isArray(value)) return value.map(canonicalValue);
if (typeof value !== "object" || value === null) return value;
const source = value as JsonObject;
return Object.fromEntries(
Object.keys(source).sort(authorityUnicodeCompare).map((key) => [key, canonicalValue(source[key])]),
);
}

export function canonicalHash(value: unknown): string {
return `sha256:${createHash("sha256").update(JSON.stringify(canonicalValue(value)), "utf8").digest("hex")}`;
}

export function completeSourceHash(value: JsonObject, identity = contextSourceIdentity(value, [])): string {
return `sha256:${createHash("sha256").update(identity(value, []), "utf8").digest("hex")}`;
}

/** Complete references reuse the checked content binding. Historical captures
* retain their old JSON hash; never rewrite a stored action or Turn key. */
export function decisionSourceHash(payload: JsonObject): string {
if (payload.schema_version !== COMPLETE_TURN_DECISION_VERSION) return canonicalHash(payload);
const digest = object(payload.contract_binding).content_hash;
if (typeof digest !== "string") throw new EffectRuntimeRequestError("complete Turn decision content binding is missing");
return digest;
}

export function shellQuote(value: string): string {
return /^[A-Za-z0-9_@%+=:,./-]+$/u.test(value) ? value : `'${value.replaceAll("'", `'"'"'`)}'`;
}

export function commandPrefix(runtimeRoot: unknown, registry?: unknown): string {
const runtimeRootText = scalarString(runtimeRoot, "quota payload runtime_root").trim();
const registryText = scalarString(registry, "quota payload registry").trim();
return "loopx" + (registryText ? ` --registry ${shellQuote(registryText)}` : "") +
(runtimeRootText ? ` --runtime-root ${shellQuote(runtimeRootText)}` : "");
}

export function coldPath(
payload: JsonObject, agentId: string | null, schedulerExecutionArgs: string, capturedDecisionPath?: string,
): JsonObject {
const goalId = scalarString(payload.goal_id, "quota payload goal_id", "<goal-id>");
const agentArg = agentId ? ` --agent-id ${agentId}` : "";
const prefix = commandPrefix(payload.runtime_root, payload.registry);
return {
full_decision: capturedDecisionPath ? `cat -- ${shellQuote(capturedDecisionPath)}` : schedulerExecutionArgs
? `${prefix} --format json quota should-run --goal-id ${goalId}${agentArg}${schedulerExecutionArgs}`
: "rerun the typed quota_guard from the current host packet",
...(capturedDecisionPath ? {
captured_decision: {
path: capturedDecisionPath,
goal_id: payload.goal_id ?? null,
agent_id: agentId,
turn_instance_id: object(payload.heartbeat_receipt).turn_instance_id ?? null,
source_hash_ref: "$.action_signature.source_decision_hash",
instruction: "Read this saved observation for omitted context. Check ok, Goal/Agent/Turn and source hash before use. " +
"It grants no fresh authority: selection, lease/workspace changes, cancellation or quota revalidation require " +
"the current host guard and a new capture directory. Missing or invalid capture requires recovery, not blind guard replay.",
},
} : {}),
todo_detail: `${prefix} --format json todo list --goal-id ${goalId}`,
status_detail: `${prefix} --format json status --goal-id ${goalId}`,
};
}

export function decodeTurnSource(request: JsonObject, inlineField: "payload" | "source"): JsonObject {
const transport = requireJsonObject(request.source_transport, "Turn composition source transport");
const { encoding, data } = transport;
const maxSourceBytes = 1024 * 1024;
if (Object.hasOwn(request, inlineField) || encoding !== "deflate-base64-json-v0" ||
typeof data !== "string" || data.length > maxSourceBytes ||
data.length % 4 !== 0 || !/^[A-Za-z0-9+/]+={0,2}$/.test(data)) {
throw new EffectRuntimeRequestError("invalid Turn composition source transport");
}
let source: unknown;
try {
const compressed = Buffer.from(data, "base64");
// Inflate may stop at the first stream and silently leave input unread.
// The synchronous info result reports consumed bytes on supported Node versions.
const { buffer, engine } = inflateSync(compressed,
{ info: true, maxOutputLength: maxSourceBytes }) as unknown as { buffer: Buffer; engine: { bytesWritten: number } };
if (engine.bytesWritten !== compressed.length) throw new Error("trailing compressed source data");
source = JSON.parse(new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(buffer));
} catch {
throw new EffectRuntimeRequestError("Turn composition source must be valid compressed JSON within 1 MiB");
}
return requireJsonObject(source, "complete Turn decision source");
}
Loading
Loading