feat(research): cut over canonical runtime and enforce agent budgets
This commit is contained in:
parent
ab08db82fd
commit
bbf0dd4453
103 changed files with 6138 additions and 3112 deletions
|
|
@ -4,6 +4,15 @@ import type { Hono, MiddlewareHandler } from 'hono';
|
|||
type Role = 'read' | 'admin' | 'service';
|
||||
const verifiedPrincipals = new WeakMap<Request, 'operator' | 'service'>();
|
||||
const credentialRequests = new WeakSet<Request>();
|
||||
const researchPrincipals = new WeakMap<Request, `agent:${string}`>();
|
||||
const researchBearer = (request: Request) =>
|
||||
/^Bearer sb-research\./i.test(request.headers.get('authorization') ?? '');
|
||||
/** Research routes opt into this accessor; privileged service/operator accessors never impersonate agents. */
|
||||
export function getResearchHttpPrincipal(
|
||||
request: Request
|
||||
): 'operator' | 'service' | `agent:${string}` | undefined {
|
||||
return researchPrincipals.get(request) ?? getCredentialHttpPrincipal(request);
|
||||
}
|
||||
|
||||
/** Server-owned identity established only after successful HTTP authorization. */
|
||||
export function getVerifiedHttpPrincipal(request: Request): 'operator' | 'service' | undefined {
|
||||
|
|
@ -22,6 +31,10 @@ export interface HttpSecurityOptions {
|
|||
hostname?: string;
|
||||
origins?: string[];
|
||||
browserSessions?: boolean;
|
||||
/** Explicit web API HTTP capability; never used by synchronous authorize or WebSocket upgrades. */
|
||||
resolveResearchCredential?: (
|
||||
request: Request
|
||||
) => Promise<{ principal: `agent:${string}` } | Response | undefined>;
|
||||
}
|
||||
const cookieName = 'stock_bot_session';
|
||||
const sessionSeconds = 3600;
|
||||
|
|
@ -153,7 +166,14 @@ export function createHttpSecurity(options: HttpSecurityOptions = httpSecurityOp
|
|||
return { role, cookie: true };
|
||||
}
|
||||
function authorize(request: Request, admin = false): Response | undefined {
|
||||
researchPrincipals.delete(request);
|
||||
verifiedPrincipals.delete(request);
|
||||
if (researchBearer(request)) {
|
||||
return Response.json(
|
||||
{ error: 'Research credentials are unavailable on this interface' },
|
||||
{ status: 403, headers: { 'Cache-Control': 'no-store' } }
|
||||
);
|
||||
}
|
||||
credentialRequests.delete(request);
|
||||
const identity = authenticate(request);
|
||||
if (!identity) {
|
||||
|
|
@ -193,6 +213,28 @@ export function createHttpSecurity(options: HttpSecurityOptions = httpSecurityOp
|
|||
await next();
|
||||
return undefined;
|
||||
}
|
||||
if (researchBearer(context.req.raw)) {
|
||||
context.header('Cache-Control', 'no-store');
|
||||
if (disabled || !options.resolveResearchCredential || context.req.header('upgrade')) {
|
||||
return context.json(
|
||||
{ error: 'Research credentials are unavailable on this interface' },
|
||||
403
|
||||
);
|
||||
}
|
||||
if (context.req.header('origin') && !originAllowed(context.req.raw)) {
|
||||
return context.json({ error: 'Untrusted request origin' }, 403);
|
||||
}
|
||||
const resolved = await options.resolveResearchCredential(context.req.raw);
|
||||
if (resolved instanceof Response) {
|
||||
return resolved;
|
||||
}
|
||||
if (!resolved) {
|
||||
return context.json({ error: 'Invalid or disabled research credential' }, 401);
|
||||
}
|
||||
researchPrincipals.set(context.req.raw, resolved.principal);
|
||||
await next();
|
||||
return undefined;
|
||||
}
|
||||
const admin =
|
||||
!['GET', 'HEAD'].includes(context.req.method) ||
|
||||
/^\/api\/(queue|operations|handlers)(\/|$)/.test(path);
|
||||
|
|
@ -243,6 +285,12 @@ export function createHttpSecurity(options: HttpSecurityOptions = httpSecurityOp
|
|||
if (token.length > 4096) {
|
||||
return context.json({ error: 'Invalid access token' }, 401);
|
||||
}
|
||||
if (token.startsWith('sb-research.')) {
|
||||
return context.json(
|
||||
{ error: 'Research credentials cannot establish browser sessions' },
|
||||
403
|
||||
);
|
||||
}
|
||||
const role = disabled ? 'admin' : roleForToken(token);
|
||||
if (!role || role === 'service') {
|
||||
return context.json({ error: 'Invalid access token' }, 401);
|
||||
|
|
@ -266,7 +314,13 @@ export function createHttpSecurity(options: HttpSecurityOptions = httpSecurityOp
|
|||
return context.json({ authenticated: false });
|
||||
});
|
||||
}
|
||||
return { mount, authorize, hostname, allowedOrigins };
|
||||
return {
|
||||
mount,
|
||||
authorize,
|
||||
hostname,
|
||||
allowedOrigins,
|
||||
researchEnabled: !disabled && !!options.resolveResearchCredential,
|
||||
};
|
||||
}
|
||||
|
||||
export type HttpSecurity = ReturnType<typeof createHttpSecurity>;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ export {
|
|||
authenticatedServiceFetch,
|
||||
getVerifiedHttpPrincipal,
|
||||
getCredentialHttpPrincipal,
|
||||
getResearchHttpPrincipal,
|
||||
} from './http/security';
|
||||
export type { HttpSecurity, HttpSecurityOptions } from './http/security';
|
||||
export { calculatePoolSize, getServicePoolSize, getHandlerPoolSize } from './pool-size-calculator';
|
||||
|
|
|
|||
|
|
@ -160,3 +160,4 @@ export type {
|
|||
} from './historical-data';
|
||||
|
||||
export type * from './research';
|
||||
export { isResearchActor, isResearchAgent } from './research';
|
||||
|
|
|
|||
|
|
@ -1,5 +1,16 @@
|
|||
/** Research narrative and citations. Referencing a measurement never validates prose. */
|
||||
export type ResearchActor = 'operator' | 'service';
|
||||
export type ResearchActor = 'operator' | 'service' | `agent:${string}`;
|
||||
export function isResearchActor(value: unknown): value is ResearchActor {
|
||||
return (
|
||||
value === 'operator' ||
|
||||
value === 'service' ||
|
||||
(typeof value === 'string' &&
|
||||
/^agent:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(value))
|
||||
);
|
||||
}
|
||||
export function isResearchAgent(value: unknown): value is `agent:${string}` {
|
||||
return isResearchActor(value) && value.startsWith('agent:');
|
||||
}
|
||||
export type ResearchContentOrigin = 'human' | 'ai' | 'unknown';
|
||||
export type ResearchMetricsVersion = 'canonical-analytics/v1' | 'legacy-display/v1';
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import type { ResearchActor } from './research';
|
||||
import type { StrategyParameterValue, StrategyVersionPackage } from './strategy-version';
|
||||
|
||||
export type StrategyPackageMetadata = Omit<
|
||||
|
|
@ -11,8 +12,8 @@ export interface StrategyRecord {
|
|||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
ownerPrincipal: 'operator' | 'service';
|
||||
updatedBy: 'operator' | 'service';
|
||||
ownerPrincipal: ResearchActor;
|
||||
updatedBy: ResearchActor;
|
||||
archived: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
|
|
@ -22,7 +23,7 @@ export interface StrategyVersionSummary {
|
|||
strategyId: string;
|
||||
ordinal: number;
|
||||
parentVersionId: string | null;
|
||||
createdBy: 'operator' | 'service';
|
||||
createdBy: ResearchActor;
|
||||
origin: 'candidate' | 'builtin';
|
||||
createdAt: string;
|
||||
package: StrategyPackageMetadata;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue