Standalone per-card browser model/security/view modules for HUX-01..10 plus node+pytest suites that read the hux.v1 contract schemas directly. Reconciled drift found on integration: the activity model now accepts all 32 hux.event.v1 kinds (delegation.*, memory.suppressed, memory.retrieval_removed, budget.exhausted, side_effect.*), the autonomy model carries the external_side_effect capability, and the foundation boundary test now asserts the shipped static HUX surface exists on disk and that images never bake activated HUX_FLAGS. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BvMSXH8VH2tMWXanb8SJdf
81 lines
3.0 KiB
TypeScript
81 lines
3.0 KiB
TypeScript
/** Identity, scope, URL and primitive guards for HUX-10. */
|
|
|
|
import type {
|
|
HuxSurface,
|
|
PrivacyScope,
|
|
RawIdentity,
|
|
ScopedIdentity,
|
|
} from "./types.ts";
|
|
|
|
const ID = /^[a-z]{2,6}_[A-Za-z0-9._-]{4,80}$/;
|
|
const TENANT = /^tnt_[0-9a-f]{16,64}$/;
|
|
const USER = /^usr_[0-9a-f]{16,64}$/;
|
|
const UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z$/;
|
|
const CONTROL = /[\u0000-\u001f\u007f]/;
|
|
const SURFACES = new Set<HuxSurface>(["chat", "worker", "telegram", "voice", "api"]);
|
|
|
|
export function asRecord(value: unknown): Record<string, unknown> | null {
|
|
return value && typeof value === "object" && !Array.isArray(value) ?
|
|
value as Record<string, unknown> : null;
|
|
}
|
|
|
|
export function exactKeys(
|
|
value: Record<string, unknown>,
|
|
required: readonly string[],
|
|
optional: readonly string[] = [],
|
|
): boolean {
|
|
const allowed = new Set([...required, ...optional]);
|
|
return required.every((key) => Object.prototype.hasOwnProperty.call(value, key)) &&
|
|
Object.keys(value).every((key) => allowed.has(key));
|
|
}
|
|
|
|
export function isId(value: unknown, prefix?: string): value is string {
|
|
return typeof value === "string" && ID.test(value) &&
|
|
(!prefix || value.startsWith(`${prefix}_`));
|
|
}
|
|
|
|
export function isUtc(value: unknown): value is string {
|
|
return typeof value === "string" && UTC.test(value) &&
|
|
!Number.isNaN(Date.parse(value));
|
|
}
|
|
|
|
export function isSafeText(value: unknown, min: number, max: number): value is string {
|
|
return typeof value === "string" && value.length >= min && value.length <= max &&
|
|
!CONTROL.test(value);
|
|
}
|
|
|
|
export function normalizeIdentity(raw: unknown): ScopedIdentity | null {
|
|
const value = raw as RawIdentity | null;
|
|
if (!value || typeof value !== "object" || Array.isArray(value) ||
|
|
!exactKeys(value as Record<string, unknown>, ["tenant_ref", "user_ref", "surface"]))
|
|
return null;
|
|
if (typeof value.tenant_ref !== "string" || !TENANT.test(value.tenant_ref) ||
|
|
typeof value.user_ref !== "string" || !USER.test(value.user_ref) ||
|
|
typeof value.surface !== "string" || !SURFACES.has(value.surface as HuxSurface))
|
|
return null;
|
|
return {tenantRef: value.tenant_ref, userRef: value.user_ref,
|
|
surface: value.surface as HuxSurface};
|
|
}
|
|
|
|
export function sameIdentity(left: ScopedIdentity, right: ScopedIdentity): boolean {
|
|
return left.tenantRef === right.tenantRef && left.userRef === right.userRef &&
|
|
left.surface === right.surface;
|
|
}
|
|
|
|
export function normalizeScope(raw: PrivacyScope): PrivacyScope | null {
|
|
const value = asRecord(raw);
|
|
if (!value || !exactKeys(value, ["sessionId", "conversationId"]) ||
|
|
!isId(value.sessionId, "ses") || !isId(value.conversationId, "conv")) return null;
|
|
return {sessionId: value.sessionId, conversationId: value.conversationId};
|
|
}
|
|
|
|
export function privacyQuery(scope: PrivacyScope): string {
|
|
const safe = normalizeScope(scope);
|
|
if (!safe) throw new TypeError("Privacy scope is invalid");
|
|
const query = new URLSearchParams({
|
|
session_id: safe.sessionId,
|
|
conversation_id: safe.conversationId,
|
|
});
|
|
return `/privacy/snapshot?${query}`;
|
|
}
|