feat(research): reopen artifacts and immutable history (RP25)
This commit is contained in:
parent
f9b97c3004
commit
e39989c4d5
4 changed files with 385 additions and 16 deletions
|
|
@ -57,7 +57,7 @@ import {
|
|||
type WalkForwardInput,
|
||||
type WalkForwardSubmission,
|
||||
} from '@stock-bot/stock-config/walk-forward';
|
||||
import type { StrategyVersionPackage } from '@stock-bot/types';
|
||||
import type { ResearchEvidence, StrategyVersionPackage } from '@stock-bot/types';
|
||||
|
||||
const uuid = z
|
||||
.string()
|
||||
|
|
@ -107,6 +107,14 @@ const hypothesisRecord = z.object({
|
|||
validationState: z.literal('unvalidated'),
|
||||
}),
|
||||
});
|
||||
const availabilityMatches = (
|
||||
metric: Pick<ResearchEvidence, 'availability' | 'value' | 'unavailableReason'>
|
||||
) =>
|
||||
metric.availability === 'available'
|
||||
? metric.value !== null && metric.unavailableReason === null
|
||||
: metric.value === null &&
|
||||
typeof metric.unavailableReason === 'string' &&
|
||||
metric.unavailableReason.trim().length > 0;
|
||||
const findingRecord = z.object({
|
||||
id: uuid,
|
||||
projectId: uuid,
|
||||
|
|
@ -136,19 +144,86 @@ const findingRecord = z.object({
|
|||
financialEngine: z.literal('rust'),
|
||||
limitations: z.array(z.object({ code: z.string(), message: z.string() })),
|
||||
})
|
||||
.refine(
|
||||
metric =>
|
||||
metric.availability === 'available'
|
||||
? metric.value !== null && metric.unavailableReason === null
|
||||
: metric.value === null &&
|
||||
typeof metric.unavailableReason === 'string' &&
|
||||
metric.unavailableReason.trim().length > 0,
|
||||
'Metric availability must match its value and reason'
|
||||
)
|
||||
.refine(availabilityMatches, 'Metric availability must match its value and reason')
|
||||
),
|
||||
}),
|
||||
});
|
||||
|
||||
// Reads include human revisions, notes and qualified historical measurements.
|
||||
// Creation keeps the narrower AI/canonical findingRecord above.
|
||||
const retainedEvidenceRecord = z
|
||||
.object({
|
||||
runId: uuid,
|
||||
backtestId: uuid,
|
||||
strategyVersionId: uuid.nullable().optional(),
|
||||
metricsVersion: z.enum(['canonical-analytics/v1', 'legacy-display/v1']),
|
||||
metricPath: z.string(),
|
||||
value: z.number().finite().nullable(),
|
||||
unit: z.enum(['fraction', 'ratio', 'count', 'currency']),
|
||||
availability: z.enum(['available', 'unavailable']),
|
||||
unavailableReason: z.string().nullable(),
|
||||
datasetHash: hash.nullable(),
|
||||
resultKind: z.enum(['canonical', 'legacy']),
|
||||
financialEngine: z.enum(['rust', 'native', 'typescript', 'legacy']),
|
||||
limitations: z.array(z.object({ code: z.string(), message: z.string() })),
|
||||
})
|
||||
.refine(availabilityMatches, 'Metric availability must match its value and reason')
|
||||
.refine(
|
||||
metric =>
|
||||
metric.metricsVersion === 'canonical-analytics/v1'
|
||||
? metric.resultKind === 'canonical' &&
|
||||
metric.financialEngine === 'rust' &&
|
||||
metric.datasetHash !== null
|
||||
: metric.resultKind === 'legacy' &&
|
||||
metric.financialEngine !== 'rust' &&
|
||||
!metric.strategyVersionId,
|
||||
'Metrics version must match the retained result and engine'
|
||||
);
|
||||
const referenceOf = ({
|
||||
runId,
|
||||
metricsVersion,
|
||||
metricPath,
|
||||
strategyVersionId,
|
||||
}: z.output<typeof retainedEvidenceRecord>) => ({
|
||||
runId,
|
||||
metricsVersion,
|
||||
metricPath,
|
||||
strategyVersionId,
|
||||
});
|
||||
const artifactRevisionRecord = z
|
||||
.object({
|
||||
id: uuid,
|
||||
artifactId: uuid,
|
||||
ordinal: z.number().int().positive(),
|
||||
title: z.string(),
|
||||
body: z.string(),
|
||||
submittedBy: researchActorSchema,
|
||||
contentOrigin: z.enum(['human', 'ai', 'unknown']),
|
||||
validationState: z.literal('unvalidated'),
|
||||
evidenceState: z.enum(['none', 'referenced']),
|
||||
source: researchFindingSourceSchema.nullable().default(null),
|
||||
evidence: z.array(retainedEvidenceRecord),
|
||||
createdAt: z.string().min(1),
|
||||
})
|
||||
.refine(
|
||||
revision =>
|
||||
revision.evidenceState === (revision.evidence.length ? 'referenced' : 'none') &&
|
||||
createResearchArtifactSchema.shape.evidence.safeParse(revision.evidence.map(referenceOf))
|
||||
.success &&
|
||||
(!revision.source ||
|
||||
(revision.evidence.length > 0 &&
|
||||
revision.evidence.every(metric => metric.metricsVersion === 'canonical-analytics/v1'))),
|
||||
'Retained evidence must match its state, references and source'
|
||||
);
|
||||
const artifactRecord = findingRecord.extend({
|
||||
ownerPrincipal: researchActorSchema,
|
||||
updatedBy: researchActorSchema,
|
||||
archived: z.boolean(),
|
||||
createdAt: z.string().min(1),
|
||||
updatedAt: z.string().min(1),
|
||||
latestRevision: artifactRevisionRecord,
|
||||
});
|
||||
|
||||
export class ResearchRequestError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
|
|
@ -687,6 +762,49 @@ export class ResearchClient {
|
|||
key
|
||||
);
|
||||
}
|
||||
artifact(artifactId: string, signal?: AbortSignal) {
|
||||
artifactId = uuid.parse(artifactId);
|
||||
return this.#request(
|
||||
`analysis-artifacts/${id(artifactId)}`,
|
||||
'GET',
|
||||
200,
|
||||
value => {
|
||||
const saved = artifactRecord.parse(envelope(value, 'artifact'));
|
||||
expect(
|
||||
saved.id === artifactId &&
|
||||
saved.latestRevision.artifactId === artifactId &&
|
||||
(saved.kind !== 'finding' || saved.latestRevision.evidence.length > 0)
|
||||
);
|
||||
return saved;
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
signal
|
||||
);
|
||||
}
|
||||
artifactRevisions(artifactId: string, signal?: AbortSignal) {
|
||||
artifactId = uuid.parse(artifactId);
|
||||
return this.#request(
|
||||
`analysis-artifacts/${id(artifactId)}/revisions`,
|
||||
'GET',
|
||||
200,
|
||||
value => {
|
||||
const revisions = z
|
||||
.array(artifactRevisionRecord)
|
||||
.min(1)
|
||||
.parse(envelope(value, 'revisions'));
|
||||
expect(
|
||||
revisions.every(revision => revision.artifactId === artifactId) &&
|
||||
new Set(revisions.map(revision => revision.id)).size === revisions.length &&
|
||||
new Set(revisions.map(revision => revision.ordinal)).size === revisions.length
|
||||
);
|
||||
return revisions;
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
signal
|
||||
);
|
||||
}
|
||||
saveFinding(projectId: string, input: FindingInput) {
|
||||
projectId = uuid.parse(projectId);
|
||||
const body = createResearchArtifactSchema.parse({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue