T-131: M2.1b — MCP copilot analyze_signal tool: vault-fenced short-interest signal study (bars + shortsAsOf → forward-return panel → analyzeSignal + trial ledger) in libs/research/src/mcp/tools/
This commit is contained in:
parent
e7bd019bb6
commit
ff9a180f12
3 changed files with 434 additions and 98 deletions
|
|
@ -14,6 +14,7 @@ import {
|
|||
type TableRow,
|
||||
type VaultFenceContext,
|
||||
} from '../../market-data-store';
|
||||
import { UNIVERSE_REFS } from '../../market-data-store-duckdb';
|
||||
import {
|
||||
SHORTS_SIGNAL_FIELDS,
|
||||
type ShortsSignalField,
|
||||
|
|
@ -73,6 +74,11 @@ const AGENT_PRINCIPAL_RE =
|
|||
/^agent:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
||||
const UTC_BOUND_ERROR_MESSAGE =
|
||||
'Expected YYYY-MM-DD calendar date or UTC ISO-8601 timestamp with timezone offset.';
|
||||
const SUPPORTED_UNIVERSE_REF_SET: ReadonlySet<string> = new Set(UNIVERSE_REFS);
|
||||
|
||||
function canonicalDailyHorizonBucket(horizonSessions: number): HorizonBucket {
|
||||
return horizonSessions <= 10 ? 'swing_1_10d' : 'position_10d_plus';
|
||||
}
|
||||
|
||||
const utcBoundStringSchema = z.string().min(1).refine(isValidUtcBoundString, UTC_BOUND_ERROR_MESSAGE);
|
||||
|
||||
|
|
@ -154,7 +160,40 @@ export const analyzeSignalInputSchema = z
|
|||
.describe('Bar price adjustment mode (default raw).'),
|
||||
})
|
||||
.strict()
|
||||
.refine(hasExclusiveListingSelector, EXCLUSIVE_LISTING_SELECTOR_MESSAGE);
|
||||
.superRefine((data, ctx) => {
|
||||
if (!hasExclusiveListingSelector(data)) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: EXCLUSIVE_LISTING_SELECTOR_MESSAGE,
|
||||
});
|
||||
}
|
||||
|
||||
const expectedBucket = canonicalDailyHorizonBucket(data.horizonSessions);
|
||||
if (data.horizonBucket !== expectedBucket) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ['horizonBucket'],
|
||||
message: `Daily horizonSessions=${data.horizonSessions} requires horizonBucket "${expectedBucket}".`,
|
||||
});
|
||||
}
|
||||
|
||||
if (data.universe !== undefined && SUPPORTED_UNIVERSE_REF_SET.has(data.universe)) {
|
||||
if (data.universeMarket !== data.universe) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ['universeMarket'],
|
||||
message: `Named universe "${data.universe}" requires universeMarket "${data.universe}".`,
|
||||
});
|
||||
}
|
||||
} else if (data.signal.source === 'shorts' && data.universeMarket === 'FX') {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ['universeMarket'],
|
||||
message:
|
||||
'Short-interest signal studies require an equity universeMarket (US_EQ, CA_EQ, or US_CA_EQ).',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export type AnalyzeSignalInput = z.infer<typeof analyzeSignalInputSchema>;
|
||||
|
||||
|
|
@ -222,6 +261,9 @@ interface AnalyzeSignalResultBase {
|
|||
readonly fromMs: number;
|
||||
readonly toMs: number;
|
||||
readonly asOfMs: number;
|
||||
readonly effectiveFrom: string;
|
||||
readonly effectiveTo: string;
|
||||
readonly effectiveAsOf: string;
|
||||
readonly clampedTo: boolean;
|
||||
readonly clampedAsOf: boolean;
|
||||
readonly listingCount: number;
|
||||
|
|
@ -351,6 +393,7 @@ function filterEligibleBars(
|
|||
if (
|
||||
row.ts < fromMs ||
|
||||
row.ts > toMs ||
|
||||
row.ts > asOfMs ||
|
||||
row.ts >= vaultCutoffTs ||
|
||||
utcDateOf(row.ts) >= vaultCutoffDate
|
||||
) {
|
||||
|
|
@ -424,11 +467,12 @@ function buildCanonicalStudyExpression(params: {
|
|||
readonly input: AnalyzeSignalInput;
|
||||
readonly listingIds: readonly number[];
|
||||
readonly universeSnapshotDate: string | null;
|
||||
readonly fromMs: number;
|
||||
readonly toMs: number;
|
||||
readonly asOfMs: number;
|
||||
readonly effectiveFrom: string;
|
||||
readonly effectiveTo: string;
|
||||
readonly effectiveAsOf: string;
|
||||
}): SignalStudyExpression {
|
||||
const { input, listingIds, universeSnapshotDate, fromMs, toMs, asOfMs } = params;
|
||||
const { input, listingIds, universeSnapshotDate, effectiveFrom, effectiveTo, effectiveAsOf } =
|
||||
params;
|
||||
return {
|
||||
expression: `mcp:analyze_signal:v1:${input.signal.source}:${input.signal.field}`,
|
||||
params: {
|
||||
|
|
@ -439,9 +483,9 @@ function buildCanonicalStudyExpression(params: {
|
|||
maxSignalAgeSessions: input.maxSignalAgeSessions ?? null,
|
||||
adjustment: input.adjustment,
|
||||
ledgerSeries: input.ledgerSeries,
|
||||
fromMs,
|
||||
toMs,
|
||||
asOfMs,
|
||||
effectiveFrom,
|
||||
effectiveTo,
|
||||
effectiveAsOf,
|
||||
},
|
||||
universe:
|
||||
input.universe !== undefined
|
||||
|
|
@ -604,6 +648,9 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
|
|||
fromMs: fenced.fromMs,
|
||||
toMs: fenced.toMs,
|
||||
asOfMs: fenced.asOfMs,
|
||||
effectiveFrom: fenced.canonicalFrom,
|
||||
effectiveTo: fenced.canonicalTo,
|
||||
effectiveAsOf: fenced.canonicalAsOf,
|
||||
clampedTo: fenced.clampedTo,
|
||||
clampedAsOf: fenced.clampedAsOf,
|
||||
listingCount: listingIds.length,
|
||||
|
|
@ -629,19 +676,19 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
|
|||
store.bars({
|
||||
listingIds,
|
||||
timeframe: '1d',
|
||||
from: input.from,
|
||||
to: fenced.effectiveFencedTo,
|
||||
from: fenced.canonicalFrom,
|
||||
to: fenced.canonicalTo,
|
||||
adjustment: input.adjustment,
|
||||
asOf: fenced.effectiveFencedAsOf,
|
||||
asOf: fenced.canonicalAsOf,
|
||||
scope: 'vault_fenced',
|
||||
})
|
||||
);
|
||||
|
||||
const rawShortsTable = await store.shortsAsOf({
|
||||
listingIds,
|
||||
from: input.from,
|
||||
to: fenced.effectiveFencedTo,
|
||||
asOf: fenced.effectiveFencedAsOf,
|
||||
from: fenced.canonicalFrom,
|
||||
to: fenced.canonicalTo,
|
||||
asOf: fenced.canonicalAsOf,
|
||||
});
|
||||
|
||||
const listingIdSet = new Set<number>(listingIds);
|
||||
|
|
@ -696,9 +743,9 @@ export function createAnalyzeSignalTool(options: AnalyzeSignalToolOptions): McpT
|
|||
input,
|
||||
listingIds,
|
||||
universeSnapshotDate,
|
||||
fromMs: fenced.fromMs,
|
||||
toMs: fenced.toMs,
|
||||
asOfMs: fenced.asOfMs,
|
||||
effectiveFrom: fenced.canonicalFrom,
|
||||
effectiveTo: fenced.canonicalTo,
|
||||
effectiveAsOf: fenced.canonicalAsOf,
|
||||
});
|
||||
|
||||
const studyResult = await analyzeSignal(ledger, {
|
||||
|
|
|
|||
|
|
@ -100,6 +100,13 @@ export function extractExplicitSubMsMicros(raw: string): number {
|
|||
return Number(microDigits);
|
||||
}
|
||||
|
||||
export function resolveFromSubMsMicros(rawFrom: string): number {
|
||||
if (DATE_ONLY_RE.test(rawFrom)) {
|
||||
return 0;
|
||||
}
|
||||
return extractExplicitSubMsMicros(rawFrom);
|
||||
}
|
||||
|
||||
export function resolveToSubMsMicros(rawTo: string, clampedTo: boolean): number {
|
||||
if (clampedTo || DATE_ONLY_RE.test(rawTo)) {
|
||||
return 999;
|
||||
|
|
@ -171,8 +178,12 @@ export function resolvePreStartUniverseDate(
|
|||
/** Result of fencing MCP query bounds against the server-injected Vault cutoff. */
|
||||
export interface VaultFencedBounds extends FencedTimeRange {
|
||||
readonly vaultCutoffDate: string;
|
||||
readonly fromSubMs: number;
|
||||
readonly toSubMs: number;
|
||||
readonly asOfSubMs: number;
|
||||
readonly canonicalFrom: string;
|
||||
readonly canonicalTo: string;
|
||||
readonly canonicalAsOf: string;
|
||||
readonly effectiveFencedTo: string;
|
||||
readonly effectiveFencedAsOf: string;
|
||||
}
|
||||
|
|
@ -199,8 +210,12 @@ export function fenceVaultQueryBounds(
|
|||
}
|
||||
);
|
||||
|
||||
const fromSubMs = resolveFromSubMsMicros(input.from);
|
||||
const toSubMs = resolveToSubMsMicros(input.to, fenced.clampedTo);
|
||||
const asOfSubMs = resolveAsOfSubMsMicros(input.asOf, fenced.clampedAsOf);
|
||||
const canonicalFrom = formatUtcIsoWithMicros(fenced.fromMs, fromSubMs);
|
||||
const canonicalTo = formatUtcIsoWithMicros(fenced.toMs, toSubMs);
|
||||
const canonicalAsOf = formatUtcIsoWithMicros(fenced.asOfMs, asOfSubMs);
|
||||
const effectiveFencedTo = fenced.clampedTo
|
||||
? formatUtcIsoWithMicros(fenced.toMs, 999)
|
||||
: input.to;
|
||||
|
|
@ -212,8 +227,12 @@ export function fenceVaultQueryBounds(
|
|||
return {
|
||||
...fenced,
|
||||
vaultCutoffDate: utcDateOf(vaultCutoffTs),
|
||||
fromSubMs,
|
||||
toSubMs,
|
||||
asOfSubMs,
|
||||
canonicalFrom,
|
||||
canonicalTo,
|
||||
canonicalAsOf,
|
||||
effectiveFencedTo,
|
||||
effectiveFencedAsOf,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue