Skip to content
Merged
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
22 changes: 18 additions & 4 deletions loopx/cli_commands/quota_context.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import argparse
from collections.abc import Callable, Mapping
from collections.abc import Callable, Iterable, Mapping
from dataclasses import dataclass
from pathlib import Path

Expand Down Expand Up @@ -118,6 +118,17 @@ def _scheduler_execution_context_from_args(
return None


def _format_runtime_profile_choices(
profiles: Iterable[SchedulerRuntimeProfile],
) -> str:
names = sorted(profile.value for profile in profiles)
if len(names) <= 1:
return "".join(names)
if len(names) == 2:
return f"{names[0]} or {names[1]}"
return f"{', '.join(names[:-1])}, or {names[-1]}"


def validate_quota_command_context_request(
args: argparse.Namespace,
) -> tuple[
Expand Down Expand Up @@ -260,10 +271,13 @@ def validate_quota_command_context_request(
if begin_turn:
profile = scheduler_runtime_profile_for_execution_context(scheduler_context)
if profile not in GUIDED_START_TURN_RUNTIME_PROFILES:
allowed_profiles = _format_runtime_profile_choices(
GUIDED_START_TURN_RUNTIME_PROFILES
)
raise QuotaCommandValidationError(
"--begin-turn requires runtime-profile codex_app_heartbeat, "
"trae_app, or codex_app_ssh_goal; every other host starts its turn by "
"passing its own --turn-instance-id"
f"--begin-turn requires runtime-profile {allowed_profiles}; "
"every other host starts its turn by passing its own "
"--turn-instance-id"
)
if (
(heartbeat_turn_id or begin_turn)
Expand Down
10 changes: 4 additions & 6 deletions loopx/control_plane/coordination/authority_state_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
canonicalAuthorityObject,
canonicalAuthoritySha256,
copyAuthorityJson,
hashCanonicalCommitEnvelope,
isAuthorityJsonObject,
} from "./authority_store_codec.ts";

Expand Down Expand Up @@ -238,12 +239,9 @@ export class AuthorityStateReplay {
// Canonicalize the ordinary envelope, inserting our owned projection's
// exact bytes at its key. This is the existing v0 digest, not a Merkle hash
// or a new persistent proof. Integer-like keys obey native JSON ordering.
const envelope = canonicalAuthorityObject({...fields, next_projection: null}, "commit proof");
const keys = Object.keys(envelope), split = keys.indexOf("next_projection");
const field = (key: string) => JSON.stringify(key) + ":" + JSON.stringify(envelope[key]);
const before = keys.slice(0, split).map(field), after = keys.slice(split + 1).map(field);
const hash = createHash("sha256").update("{" + (before.length ? before.join(",") + "," : "") + '"next_projection":');
return this.#updateProjection(hash).update((after.length ? "," + after.join(",") : "") + "}").digest("hex");
return hashCanonicalCommitEnvelope(fields, (hash) => {
this.#updateProjection(hash);
});
}

#updateProjection(hash: Hash): Hash {
Expand Down
41 changes: 27 additions & 14 deletions loopx/control_plane/coordination/authority_store_codec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHash } from "node:crypto";
import { createHash, type Hash } from "node:crypto";

import type { JsonObject } from "../effect_program.ts";
import type { AuthorityStoreCommit } from "./authority_store.ts";
Expand Down Expand Up @@ -147,20 +147,16 @@ function cloneAuthorityJson(value: unknown, stack: Set<object>, canonicalKeys: b
stack.add(value);
try {
const keys = Object.keys(value);
if (!canonicalKeys) {
// Fill without inherited setters (including __proto__), then normalize
// to the same ordinary object as Object.fromEntries. No per-field tuples.
const copy: JsonObject = Object.create(null);
for (const key of keys) copy[key] = cloneAuthorityJson(value[key], stack, false);
return Object.setPrototypeOf(copy, Object.prototype);
if (canonicalKeys && keys.length > 1) {
keys.sort(authorityUnicodeCompare);
}
keys.sort(authorityUnicodeCompare);
return Object.fromEntries(
keys.map((key) => [
key,
cloneAuthorityJson(value[key], stack, canonicalKeys),
]),
);
// Fill without inherited setters (including __proto__), then normalize
// to an ordinary object. Avoids intermediate per-field tuple arrays.
const copy: JsonObject = Object.create(null);
for (const key of keys) {
copy[key] = cloneAuthorityJson(value[key], stack, canonicalKeys);
}
return Object.setPrototypeOf(copy, Object.prototype);
} finally {
stack.delete(value);
}
Expand Down Expand Up @@ -196,6 +192,23 @@ export function canonicalAuthoritySha256(value: unknown): string {
return createHash("sha256").update(canonicalAuthorityBytes(value)).digest("hex");
}

/**
* Hash a commit envelope when the caller already owns the canonicalized
* `next_projection` bytes or stream, avoiding a duplicate full-tree clone.
*/
export function hashCanonicalCommitEnvelope(
fields: Omit<AuthorityStoreCommit, "next_projection">,
writeProjection: (hash: Hash) => void,
): string {
const envelope = canonicalAuthorityObject({...fields, next_projection: null}, "commit proof");
const keys = Object.keys(envelope), split = keys.indexOf("next_projection");
const field = (key: string) => JSON.stringify(key) + ":" + JSON.stringify(envelope[key]);
const before = keys.slice(0, split).map(field), after = keys.slice(split + 1).map(field);
const hash = createHash("sha256").update("{" + (before.length ? before.join(",") + "," : "") + '"next_projection":');
writeProjection(hash);
return hash.update((after.length ? "," + after.join(",") : "") + "}").digest("hex");
}

export function parseAuthorityCursor(value: string | null): bigint {
if (value === null) return 0n;
if (typeof value !== "string" || !/^[1-9]\d*$/.test(value)) {
Expand Down
67 changes: 46 additions & 21 deletions loopx/control_plane/coordination/sqlite_authority_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ import { createHash, randomUUID } from "node:crypto";
import { existsSync, mkdirSync, statSync } from "node:fs";
import {sqliteAuthorityRuntime} from "./sqlite_runtime.ts";
import { dirname, join } from "node:path";
import type { DatabaseSync } from "node:sqlite";
import type { DatabaseSync, StatementSync } from "node:sqlite";

import type { JsonObject } from "../effect_program.ts";
import type { AuthorityStore, AuthorityStoreCommit, AuthorityStoreCommitResult, AuthorityStoreCommittedTransaction,
AuthorityStoreIdentityResult, AuthorityStoreLoadResult, AuthorityStoreSnapshotResult, AuthorityStoreReadFailure, AuthorityStoreHead,
AuthorityStoreReceiptResult, AuthorityStoreReceiptBatchResult, AuthorityStoreScanResult } from "./authority_store.ts";
import { AuthorityStoreProtocolError, canonicalAuthorityBytes, canonicalAuthorityObject,
canonicalAuthorityObjectList, canonicalAuthoritySha256,
canonicalAuthorityObjectList, canonicalAuthoritySha256, hashCanonicalCommitEnvelope,
normalizeAuthorityStoreCommit, requireAuthorityStoreId } from "./authority_store_codec.ts";
import {
AUTHORITY_STATE_CHECKPOINT_INTERVAL,
AuthorityStateReplay,
authorityStateCheckpointCursor,
authorityStateDelta,
authorityStateDeltaReconstructs,
authorityStateDigest,
authorityStateReplayBudget,
decodeAuthorityStateDelta,
isAuthorityStateCheckpoint,
Expand Down Expand Up @@ -187,8 +186,10 @@ export class SqliteAuthorityStore implements AuthorityStore {
db.exec("PRAGMA busy_timeout = 5000; PRAGMA foreign_keys = ON;");
if (write) {
db.exec("PRAGMA journal_mode = WAL; PRAGMA synchronous = FULL;");
db.exec("BEGIN IMMEDIATE");
let inTransaction = false;
try {
db.exec("BEGIN IMMEDIATE");
inTransaction = true;
const version = Number(db.prepare("PRAGMA user_version").get()?.user_version ?? 0);
if (version === 0) {
// Never adopt an unrelated/unversioned database.
Expand All @@ -201,7 +202,13 @@ export class SqliteAuthorityStore implements AuthorityStore {
}
this.identity(db);
db.exec("COMMIT");
} catch (error) { db.exec("ROLLBACK"); throw error; }
inTransaction = false;
} catch (error) {
if (inTransaction) {
try { db.exec("ROLLBACK"); } catch { /* close abandons active transaction */ }
}
throw error;
}
} else this.identity(db);
return db;
} catch (error) { db.close(); throw error; }
Expand Down Expand Up @@ -283,11 +290,16 @@ export class SqliteAuthorityStore implements AuthorityStore {
return {cursor: row.cursor, digest: row.state_digest, replay};
}

private loadCheckpoint(db: DatabaseSync, cursor: bigint): SqliteReplayCursor {
private loadCheckpoint(
db: DatabaseSync,
cursor: bigint,
checkpointStmt?: StatementSync,
): SqliteReplayCursor {
const expected = authorityStateCheckpointCursor(cursor);
const row = db.prepare(
const stmt = checkpointStmt ?? db.prepare(
"SELECT cursor, projection, projection_digest FROM checkpoints WHERE cursor = ?",
).get(expected.toString());
);
const row = stmt.get(expected.toString());
if (!row) protocol("SQLite authority checkpoint is missing for its window");
const replay = new AuthorityStateReplay(this.parseJson(row.projection, "SQLite checkpoint projection"));
const digest = this.requireDigest(row.projection_digest, "SQLite checkpoint digest");
Expand Down Expand Up @@ -361,8 +373,11 @@ export class SqliteAuthorityStore implements AuthorityStore {
}
const projection = canonicalAuthorityObject(this.parseJson(row.projection, "SQLite head projection"),
"SQLite head projection");
const projectionJson = JSON.stringify(projection);
const digest = this.requireDigest(row.state_digest, "SQLite head state digest");
if (authorityStateDigest(projection) !== digest) protocol("SQLite authority head digest mismatch");
if (createHash("sha256").update(projectionJson, "utf8").digest("hex") !== digest) {
protocol("SQLite authority head digest mismatch");
}
const retained = db.prepare(`SELECT ${COMMIT_COLUMNS} FROM commits WHERE cursor = ?`).get(row.sequence);
if (!retained) protocol("SQLite authority head has no retained transaction");
const newest = this.decodeCommitRow(retained as unknown as Record<string, unknown>);
Expand All @@ -374,8 +389,10 @@ export class SqliteAuthorityStore implements AuthorityStore {
: newest.parent_state_digest !== parent?.state_digest) {
protocol("SQLite authority head parent lineage is invalid");
}
if (newest.commit_digest !==
commitDigest(identity, cursor, newest.operation_id, projection, newest.events, newest.receipts)) {
if (newest.commit_digest !== hashCanonicalCommitEnvelope(
commitFields(identity, cursor, newest.operation_id, newest.events, newest.receipts),
(hash) => { hash.update(projectionJson, "utf8"); },
)) {
protocol("SQLite committed row digest mismatch");
}
// Recovery of this head resumes from one retained checkpoint, so its
Expand Down Expand Up @@ -514,26 +531,31 @@ export class SqliteAuthorityStore implements AuthorityStore {
const next = (cursor ?? 0n) + 1n;
if (next > MAX_SEQUENCE) protocol("SQLite authority sequence exhausted");
const before = current?.state ?? {cursor: 0n, projection: {}, digest: ""};
const nextState = canonicalAuthorityObject(normalized.next_projection, "SQLite authority state");
const nextState = normalized.next_projection;
const delta = authorityStateDelta(before.projection, nextState);
// The encoder is proved before it is persisted: replaying the stored
// delta must reproduce the committed projection byte for byte.
if (!authorityStateDeltaReconstructs(before.projection, delta, nextState)) {
protocol("SQLite authority state delta does not reconstruct its commit");
}
const stateDigest = authorityStateDigest(nextState);
const nextStateJson = JSON.stringify(nextState);
const stateDigest = createHash("sha256").update(nextStateJson, "utf8").digest("hex");
const nextCommitDigest = hashCanonicalCommitEnvelope(
commitFields(identity, next, normalized.operation_id, normalized.events, normalized.receipts),
(hash) => { hash.update(nextStateJson, "utf8"); },
);
if (isAuthorityStateCheckpoint(next)) {
db.prepare("INSERT INTO checkpoints VALUES (?, ?, ?)")
.run(next.toString(), JSON.stringify(nextState), stateDigest);
.run(next.toString(), nextStateJson, stateDigest);
}
db.prepare("INSERT INTO commits VALUES (?, ?, ?, ?, ?, ?, ?, ?)").run(next.toString(),
normalized.operation_id,
commitDigest(identity, next, normalized.operation_id, nextState, normalized.events, normalized.receipts),
nextCommitDigest,
stateDigest, cursor === null ? null : before.digest,
JSON.stringify(delta), JSON.stringify(normalized.events), JSON.stringify(normalized.receipts));
db.prepare(`INSERT INTO head VALUES (1, ?, ?, ?)
ON CONFLICT(singleton) DO UPDATE SET cursor = excluded.cursor, projection = excluded.projection,
state_digest = excluded.state_digest`).run(next.toString(), JSON.stringify(nextState), stateDigest);
state_digest = excluded.state_digest`).run(next.toString(), nextStateJson, stateDigest);
committing = true;
db.exec("COMMIT"); transactionOpen = false;
return {status: "applied", provider_revision: `${identity}:${next}`, cursor: next.toString()};
Expand Down Expand Up @@ -570,9 +592,9 @@ export class SqliteAuthorityStore implements AuthorityStore {
const head = this.current(db);
if (head === null) return missing();
const ranges = new Map<bigint, {from: bigint; to: bigint}>();
const selectByOperation = db.prepare(`SELECT ${COMMIT_COLUMNS} FROM commits WHERE operation_id = ?`);
const selected = operationIds.map(id => {
const row = db!.prepare(`SELECT ${COMMIT_COLUMNS} FROM commits WHERE operation_id = ?`)
.get(id) as unknown as Record<string, unknown> | undefined;
const row = selectByOperation.get(id) as unknown as Record<string, unknown> | undefined;
if (!row) return null;
const cursor = this.decodeCommitRow(row).cursor;
const checkpoint = authorityStateCheckpointCursor(cursor);
Expand Down Expand Up @@ -655,20 +677,23 @@ export class SqliteAuthorityStore implements AuthorityStore {
return {schema_version: "loopx_sqlite_authority_history_audit_v0", status: "verified", commits: 0, checkpoints: 0};
}
const counted = db.prepare("SELECT COUNT(*) AS count FROM checkpoints").get();
const pageStmt = db.prepare(`SELECT ${COMMIT_COLUMNS} FROM commits WHERE cursor > ? ORDER BY cursor LIMIT ?`);
const checkpointStmt = db.prepare(
"SELECT cursor, projection, projection_digest FROM checkpoints WHERE cursor = ?",
);
let state: SqliteReplayCursor = {cursor: 0n, digest: "", replay: new AuthorityStateReplay({})};
let cursor = 0n;
let commits = 0;
for (;;) {
const rows = db.prepare(`SELECT ${COMMIT_COLUMNS} FROM commits WHERE cursor > ? ORDER BY cursor LIMIT ?`)
.all(cursor.toString(), BigInt(AUDIT_PAGE)) as unknown as Record<string, unknown>[];
const rows = pageStmt.all(cursor.toString(), BigInt(AUDIT_PAGE)) as unknown as Record<string, unknown>[];
if (rows.length === 0) break;
for (const raw of rows) {
const row = this.decodeCommitRow(raw);
// The exact delta chain is proved from the empty root, so a retained
// delta can never diverge from the history it claims to extend.
const replayed = this.verifyCommitRow(row, identity, {kind: "predecessor", state});
if (isAuthorityStateCheckpoint(row.cursor)) {
const checkpoint = this.loadCheckpoint(db, row.cursor);
const checkpoint = this.loadCheckpoint(db, row.cursor, checkpointStmt);
const sealed = this.verifyCommitRow(row, identity, {kind: "sealed", state: checkpoint});
if (sealed.digest !== replayed.digest ||
sealed.replay.canonicalJson() !== replayed.replay.canonicalJson()) {
Expand Down
Loading
Loading