feat(research): isolate candidates and add reviewed paper promotion

This commit is contained in:
Boki 2026-10-24 18:46:55 -04:00
parent 26b97265c6
commit 25b5cbf481
128 changed files with 13038 additions and 1388 deletions

View file

@ -0,0 +1,96 @@
import { Hono } from 'hono';
import { z } from 'zod';
import {
paperActivationSchema,
paperHaltSchema,
PaperRuntimeError,
} from '../../contracts/paper-runtime';
import { ExecutionConflictError } from '../../execution/repository';
import { PaperDeploymentError } from '../../paper/paper-deployment-custody';
import { paperAllowedOrigins, requirePaperOperator } from '../../paper/paper-operator';
import type { PaperDeploymentHost } from '../../paper/PaperDeploymentHost';
export function createPaperDeploymentHostRoutes(
host?: PaperDeploymentHost,
origins: readonly string[] = paperAllowedOrigins()
) {
const app = new Hono();
app.use('*', async (c, next) => {
c.header('Cache-Control', 'no-store');
requirePaperOperator(c.req.raw, origins);
await next();
});
app.onError((error, c) => {
if (error instanceof PaperDeploymentError) {
return c.json({ error: error.message }, error.status);
}
if (error instanceof z.ZodError || error instanceof SyntaxError) {
return c.json({ error: 'Invalid paper command' }, 400);
}
if (error instanceof PaperRuntimeError || error instanceof ExecutionConflictError) {
return c.json({ error: error.message }, 409);
}
return c.json({ error: 'Paper deployment host unavailable' }, 503);
});
const configured = () => {
if (!host) {
throw new PaperDeploymentError(
503,
'Paper deployment host is explicitly disabled or unconfigured'
);
}
return host;
};
app.get('/:id/readiness', async c =>
c.json(
await configured().status(
z
.string()
.uuid()
.transform(value => value.toLowerCase())
.parse(c.req.param('id'))
)
)
);
app.post('/:id/bind', async c => {
z.object({})
.strict()
.parse(await c.req.json());
return c.json(
await configured().bind(
z
.string()
.uuid()
.transform(value => value.toLowerCase())
.parse(c.req.param('id'))
)
);
});
app.post('/:id/activate', async c => {
const input = paperActivationSchema.parse(await c.req.json());
return c.json(
await configured().activate(
z
.string()
.uuid()
.transform(value => value.toLowerCase())
.parse(c.req.param('id')),
input.recoverPending
)
);
});
app.post('/:id/halt', async c => {
const input = paperHaltSchema.parse(await c.req.json());
return c.json(
await configured().halt(
z
.string()
.uuid()
.transform(value => value.toLowerCase())
.parse(c.req.param('id')),
input.reason
)
);
});
return app;
}

View file

@ -1,6 +1,6 @@
import { Hono } from 'hono';
import { z } from 'zod';
import { getVerifiedHttpPrincipal } from '@stock-bot/di';
import { getCredentialHttpPrincipal } from '@stock-bot/di';
import {
paperActivationSchema,
paperConnectionSchema,
@ -10,12 +10,20 @@ import {
type PaperReadiness,
} from '../../contracts/paper-runtime';
import { ExecutionConflictError } from '../../execution/repository';
import { PaperDeploymentError } from '../../paper/paper-deployment-custody';
import { paperAllowedOrigins, requirePaperOperator } from '../../paper/paper-operator';
import type { PaperDeploymentHost } from '../../paper/PaperDeploymentHost';
import type { OrchestratorServices } from '../../types';
export function createPaperRoutes(services: OrchestratorServices): Hono {
export function createPaperRoutes(
services: OrchestratorServices,
options: { deploymentHost?: PaperDeploymentHost; origins?: readonly string[] } = {}
): Hono {
const app = new Hono();
const runtime = () => {
const value = services.custom?.CanonicalPaperRuntime;
const value = options.deploymentHost
? options.deploymentHost.runtime()
: services.custom?.CanonicalPaperRuntime;
if (!value) {
throw new PaperRuntimeError('not-ready', 'Canonical paper execution is not configured');
}
@ -24,8 +32,11 @@ export function createPaperRoutes(services: OrchestratorServices): Hono {
app.use('*', async (c, next) => {
c.header('Cache-Control', 'no-store');
if (c.req.method !== 'GET') {
const principal = getVerifiedHttpPrincipal(c.req.raw);
const principal = getCredentialHttpPrincipal(c.req.raw);
const feed = c.req.path.includes('/feed/');
if (!feed) {
requirePaperOperator(c.req.raw, options.origins ?? paperAllowedOrigins());
}
if (principal !== 'operator' && !(feed && principal === 'service')) {
return c.json(
{ error: feed ? 'Verified feed producer required' : 'Human operator required' },
@ -37,6 +48,9 @@ export function createPaperRoutes(services: OrchestratorServices): Hono {
return undefined;
});
app.onError((error, c) => {
if (error instanceof PaperDeploymentError) {
return c.json({ error: error.message }, error.status);
}
if (error instanceof z.ZodError || error instanceof SyntaxError) {
return c.json({ error: 'Invalid paper request' }, 400);
}
@ -53,7 +67,10 @@ export function createPaperRoutes(services: OrchestratorServices): Hono {
return c.json({ error: 'Canonical paper runtime unavailable' }, 503);
});
app.get('/readiness', async c => {
if (!services.custom?.CanonicalPaperRuntime) {
if (options.deploymentHost) {
return c.json(await options.deploymentHost.readiness());
}
if (!services.custom?.CanonicalPaperRuntime && !options.deploymentHost) {
const unavailable: PaperReadiness = {
configured: false,
accountId: null,
@ -76,9 +93,12 @@ export function createPaperRoutes(services: OrchestratorServices): Hono {
}
return c.json(await runtime().status());
});
app.post('/activate', async c =>
c.json(await runtime().activate(paperActivationSchema.parse(await c.req.json())))
);
app.post('/activate', async c => {
if (options.deploymentHost) {
throw new PaperDeploymentError(409, 'Use the deployment-bound activation endpoint');
}
return c.json(await runtime().activate(paperActivationSchema.parse(await c.req.json())));
});
app.post('/halt', async c =>
c.json(await runtime().halt(paperHaltSchema.parse(await c.req.json()).reason))
);