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
57 changes: 57 additions & 0 deletions loopx/control_plane/coordination/authority_store_codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,63 @@ export function requireAuthorityStoreId(value: unknown, name: string): string {
return value;
}

export function optionalAuthorityString(value: unknown, label: string): string | null {
if (value === null || value === undefined || value === "") return null;
if (typeof value !== "string") {
throw new AuthorityStoreProtocolError(`${label} must be a string or null`);
}
return value;
}

export function optionalAuthoritySafeInteger(value: unknown, label: string): number | null {
if (value === null || value === undefined) return null;
if (!Number.isSafeInteger(value) || Number(value) < 0) {
throw new AuthorityStoreProtocolError(`${label} must be a non-negative safe integer or null`);
}
return Number(value);
}

export function requireAuthorityBoolean(value: unknown, label: string): boolean {
if (typeof value !== "boolean") {
throw new AuthorityStoreProtocolError(`${label} must be a boolean`);
}
return value;
}

export function optionalAuthorityBoolean(value: unknown, label: string): boolean | null {
if (value === null || value === undefined) return null;
return requireAuthorityBoolean(value, label);
}

export function requireAuthorityDate(value: unknown, label: string): Date {
if (!(value instanceof Date) || Number.isNaN(value.valueOf())) {
throw new AuthorityStoreProtocolError(`${label} must be a valid Date`);
}
return value;
}

export function requireAuthorityLiteral<T extends string>(
value: unknown,
values: readonly T[],
label: string,
): T {
if (typeof value !== "string" || !values.includes(value as T)) {
throw new AuthorityStoreProtocolError(`${label} must be one of: ${values.join(", ")}`);
}
return value as T;
}

export function uniqueAuthorityStoreIds(value: readonly string[], label: string): string[] {
if (!Array.isArray(value)) {
throw new AuthorityStoreProtocolError(`${label} must be an array`);
}
const values = value.map((item, index) => requireAuthorityStoreId(item, `${label}[${index}]`));
if (new Set(values).size !== values.length) {
throw new AuthorityStoreProtocolError(`${label} must contain unique ids`);
}
return values;
}

export function canonicalAuthorityJson(
value: unknown,
stack = new Set<object>(),
Expand Down
19 changes: 18 additions & 1 deletion loopx/control_plane/coordination/todo_agents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import {AuthorityStoreProtocolError} from "./authority_store_codec.ts";
import {AuthorityStoreProtocolError, optionalAuthorityString} from "./authority_store_codec.ts";
import {compactPythonWhitespace} from "./python_whitespace.ts";
export {compactPythonWhitespace, hasPythonNonWhitespaceText, stripPythonWhitespace} from "./python_whitespace.ts";

export function optionalTodoAgent(value: unknown, label: string): string | null {
const candidate = optionalAuthorityString(value, label);
return candidate === null ? null : normalizeTodoAgent(candidate, label);
}

export function optionalRegisteredTodoAgent(
value: unknown,
label: string,
registeredAgents: readonly string[],
): string | null {
const agent = optionalTodoAgent(value, label);
if (agent !== null && !registeredAgents.includes(agent)) {
throw new AuthorityStoreProtocolError(`${label} is not a registered agent`);
}
return agent;
}

export function normalizeTodoAgent(value: unknown, label: string): string {
if (typeof value !== "string") {
throw new AuthorityStoreProtocolError(`${label} must be a public-safe agent id`);
Expand Down
14 changes: 5 additions & 9 deletions loopx/control_plane/coordination/todo_claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
AuthorityStoreProtocolError,
canonicalAuthorityObject,
canonicalAuthoritySha256,
requireAuthorityBoolean,
requireAuthorityDate,
requireAuthorityStoreId,
} from "./authority_store_codec.ts";
import {validateContinuationNote, computeContinuationTodoFacts} from "./continuation_note.ts";
Expand Down Expand Up @@ -376,21 +378,15 @@ async function executeClaimAttempt(
registered_agents: normalizeRegisteredTodoAgents(rawInput.registered_agents),
lease_request: normalizeLeaseRequest(rawInput.lease_request),
};
if (input.manage_execution !== undefined && typeof input.manage_execution !== "boolean") {
throw new AuthorityStoreProtocolError("manage_execution must be a boolean");
}
if (input.manage_execution !== undefined) requireAuthorityBoolean(input.manage_execution, "manage_execution");
if (input.required_handoff_mode !== undefined && !EXECUTION_HANDOFF_MODES.includes(input.required_handoff_mode)) {
throw new AuthorityStoreProtocolError("required_handoff_mode must be a current execution mode");
}
if (input.manage_execution && (input.lease_request != null || input.actor_agent_id !== input.claimed_by)) {
throw new AuthorityStoreProtocolError("Managed execution requires the claiming actor and no explicit lease request");
}
if (typeof input.dry_run !== "boolean") {
throw new AuthorityStoreProtocolError("dry_run must be a boolean");
}
if (!(input.now instanceof Date) || Number.isNaN(input.now.valueOf())) {
throw new AuthorityStoreProtocolError("now must be a valid Date");
}
requireAuthorityBoolean(input.dry_run, "dry_run");
requireAuthorityDate(input.now, "now");
} catch (error) {
return failure(
"invalid_coordination_todo_claim",
Expand Down
Loading
Loading