feat(backtests): separate saving from retry-safe run submission

This commit is contained in:
Boki 2026-10-22 17:01:46 -04:00
parent 5eb0985057
commit 9b9403e36b
18 changed files with 1200 additions and 226 deletions

View file

@ -2,6 +2,12 @@ import { createHash, createHmac, randomBytes, timingSafeEqual } from 'node:crypt
import type { Hono, MiddlewareHandler } from 'hono';
type Role = 'read' | 'admin' | 'service';
const verifiedPrincipals = new WeakMap<Request, 'operator' | 'service'>();
/** Server-owned identity established only after successful HTTP authorization. */
export function getVerifiedHttpPrincipal(request: Request): 'operator' | 'service' | undefined {
return verifiedPrincipals.get(request);
}
export interface HttpSecurityOptions {
readToken?: string;
adminToken?: string;
@ -142,6 +148,7 @@ export function createHttpSecurity(options: HttpSecurityOptions = httpSecurityOp
return { role, cookie: true };
}
function authorize(request: Request, admin = false): Response | undefined {
verifiedPrincipals.delete(request);
const identity = authenticate(request);
if (!identity) {
return Response.json(
@ -161,6 +168,9 @@ export function createHttpSecurity(options: HttpSecurityOptions = httpSecurityOp
) {
return Response.json({ error: 'Untrusted request origin' }, { status: 403 });
}
if (identity.role !== 'read') {
verifiedPrincipals.set(request, identity.role === 'service' ? 'service' : 'operator');
}
return undefined;
}
const middleware: MiddlewareHandler = async (context, next) => {

View file

@ -6,6 +6,7 @@ export {
createHttpSecurity,
httpSecurityOptions,
authenticatedServiceFetch,
getVerifiedHttpPrincipal,
} from './http/security';
export type { HttpSecurity, HttpSecurityOptions } from './http/security';
export { calculatePoolSize, getServicePoolSize, getHandlerPoolSize } from './pool-size-calculator';