feat(experiments): 独立注册实验类型并分离用户分组查询
- 实验类型:自定义标识、数据契约、默认值和真实调用后的接入验收 - 测试方案:按类型校验 A/B 值,保留历史分组与预设快照,未验收禁止发布 - 用户管理:真实账号分页搜索、只读预览与有效账号人工指定校验 - 文档与验证:同步迁移014及消费者接口说明,15项核心回归通过
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
/** 后台实验生命周期与账号稳定分配;前台只消费短时决策并回报曝光。 */
|
||||
import { scene } from "./digital.scenes";
|
||||
import {
|
||||
experimentType as scene,
|
||||
normalizePlanValues,
|
||||
recordTypeRead,
|
||||
} from "./digital.experiment-types";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import type { PoolConnection } from "mysql2/promise";
|
||||
import type { z } from "zod";
|
||||
@@ -18,7 +22,10 @@ export type Experiment = {
|
||||
id: string;
|
||||
name: string;
|
||||
scope: string;
|
||||
runtime: "ui" | "chat";
|
||||
runtime: string | null;
|
||||
consumerKey: string;
|
||||
integrationState: string;
|
||||
defaultValue: unknown;
|
||||
sceneEnabled: number;
|
||||
state: "draft" | "running" | "paused" | "finished";
|
||||
config: Config;
|
||||
@@ -48,7 +55,7 @@ export function eligible(config: Config, account: string, createdAt: string) {
|
||||
export async function experiments() {
|
||||
return (
|
||||
await rows<Experiment>(
|
||||
"SELECT e.*,s.runtime,s.name AS sceneName,s.enabled AS sceneEnabled FROM dh_experiments e JOIN dh_scenes s ON s.id=e.scope ORDER BY e.created_at DESC LIMIT 200",
|
||||
"SELECT e.*,s.runtime,s.consumer_key AS consumerKey,s.integration_state AS integrationState,s.default_value AS defaultValue,s.name AS sceneName,s.name AS typeName,s.enabled AS sceneEnabled FROM dh_experiments e JOIN dh_scenes s ON s.id=e.scope ORDER BY e.created_at DESC,e.id DESC",
|
||||
)
|
||||
).map((e) => ({ ...e, config: json<Config>(e.config) }));
|
||||
}
|
||||
@@ -82,7 +89,12 @@ export async function saveExperiment(
|
||||
c,
|
||||
);
|
||||
const registered = await scene(input.scope, c);
|
||||
if (!registered.enabled) throw badRequest("场景已停用,不能创建或编辑实验");
|
||||
if (!registered.enabled) throw badRequest("类型已停用,不能创建或编辑方案");
|
||||
const config = {
|
||||
...input.config,
|
||||
...normalizePlanValues(registered, input.config),
|
||||
typeRevision: registered.contractRevision,
|
||||
};
|
||||
const [old] = await rows<Experiment>(
|
||||
"SELECT * FROM dh_experiments WHERE id=? FOR UPDATE",
|
||||
[id],
|
||||
@@ -93,13 +105,13 @@ export async function saveExperiment(
|
||||
if (old)
|
||||
await write(
|
||||
"UPDATE dh_experiments SET name=?,scope=?,config=?,revision=revision+1 WHERE id=?",
|
||||
[input.name, input.scope, JSON.stringify(input.config), id],
|
||||
[input.name, input.scope, JSON.stringify(config), id],
|
||||
c,
|
||||
);
|
||||
else
|
||||
await write(
|
||||
"INSERT INTO dh_experiments(id,name,scope,config) VALUES (?,?,?,?)",
|
||||
[id, input.name, input.scope, JSON.stringify(input.config)],
|
||||
[id, input.name, input.scope, JSON.stringify(config)],
|
||||
c,
|
||||
);
|
||||
await audit(
|
||||
@@ -148,18 +160,25 @@ export async function transition(
|
||||
};
|
||||
const registered = await scene(e.scope, c);
|
||||
if (state === "running") {
|
||||
if (!registered.enabled) throw badRequest("场景已停用,不能启动实验");
|
||||
if (!registered.enabled) throw badRequest("类型已停用,不能启动方案");
|
||||
if (registered.integrationState !== "confirmed")
|
||||
throw conflict("该类型尚未完成应用接入验收,请先到实验类型页确认接入");
|
||||
if (
|
||||
config.typeRevision &&
|
||||
config.typeRevision !== registered.contractRevision
|
||||
)
|
||||
throw conflict("类型契约已变更,请重新保存草稿");
|
||||
if (config.audience === "specified" && !config.accounts.length)
|
||||
throw badRequest("指定名单不能为空");
|
||||
const others = await rows(
|
||||
"SELECT e.id FROM dh_experiments e JOIN dh_scenes s ON s.id=e.scope WHERE s.runtime=? AND e.id<>? AND e.state IN ('running','paused')",
|
||||
[registered.runtime, id],
|
||||
"SELECT e.id FROM dh_experiments e JOIN dh_scenes s ON s.id=e.scope WHERE s.consumer_key=? AND e.id<>? AND e.state IN ('running','paused')",
|
||||
[registered.consumerKey, id],
|
||||
c,
|
||||
);
|
||||
if (others.length)
|
||||
throw conflict(
|
||||
"该应用接入点已有未结束的实验,请先结束,避免多个场景同时控制同一功能",
|
||||
);
|
||||
throw conflict("该类型的消费标识已有未结束方案,请先结束现有方案");
|
||||
const cfg = config;
|
||||
if (registered.runtime === "chat" && !cfg.presetSnapshots) {
|
||||
if (registered.consumerKey === "chat" && !cfg.presetSnapshots) {
|
||||
if (!cfg.presetA || !cfg.presetB)
|
||||
throw badRequest("对话实验须分别选择两个已发布预设");
|
||||
const available = await rows<{ id: string; body: unknown }>(
|
||||
@@ -262,18 +281,41 @@ export async function resolveExperiment(
|
||||
};
|
||||
}
|
||||
/** 从已按创建时间排序的场景实验中选择当前接入点的有效配置。 */
|
||||
export function activeExperiment(list: Experiment[], runtime: "ui" | "chat") {
|
||||
export function activeExperiment(list: Experiment[], runtime: string) {
|
||||
const candidates = list.filter(
|
||||
(e) => e.runtime === runtime && !!e.sceneEnabled && e.state !== "draft",
|
||||
(e) =>
|
||||
(e.consumerKey ?? e.runtime) === runtime &&
|
||||
!!e.sceneEnabled &&
|
||||
e.state !== "draft",
|
||||
);
|
||||
return (
|
||||
candidates.find((e) => e.state === "running" || e.state === "paused") ??
|
||||
candidates[0]
|
||||
);
|
||||
}
|
||||
/** 历史方案没有typed values时保留原UI/预设含义,不重算账号分组。 */
|
||||
export function planValue(
|
||||
e: Experiment,
|
||||
variant: "A" | "B",
|
||||
fallback: unknown,
|
||||
) {
|
||||
if (e.config.values && Object.hasOwn(e.config.values, variant))
|
||||
return e.config.values[variant];
|
||||
const consumer = e.consumerKey ?? e.runtime;
|
||||
if (consumer === "ui") return variant === "A" ? "old" : "new";
|
||||
if (consumer === "chat")
|
||||
return variant === "A"
|
||||
? (e.config.presetA ?? fallback)
|
||||
: (e.config.presetB ?? fallback);
|
||||
return fallback;
|
||||
}
|
||||
/** 返回前台所需最小决策,规则、指定名单和提示词均不下发。 */
|
||||
export async function decide(account: string, scope: "ui" | "chat") {
|
||||
const e = activeExperiment(await experiments(), scope);
|
||||
export async function decide(account: string, scope: string) {
|
||||
const type = await scene(scope);
|
||||
await recordTypeRead(type.id, type.contractRevision);
|
||||
const e = type.enabled
|
||||
? activeExperiment(await experiments(), type.consumerKey)
|
||||
: undefined;
|
||||
if (!e)
|
||||
return {
|
||||
variant: "A" as const,
|
||||
@@ -283,6 +325,9 @@ export async function decide(account: string, scope: "ui" | "chat") {
|
||||
experimentId: null,
|
||||
presetId: undefined,
|
||||
presetSnapshot: undefined,
|
||||
value: type.defaultValue,
|
||||
typeKey: type.id,
|
||||
typeRevision: type.contractRevision,
|
||||
expiresAt: Date.now() + 60000,
|
||||
};
|
||||
const d = await resolveExperiment(e, account);
|
||||
@@ -297,6 +342,9 @@ export async function decide(account: string, scope: "ui" | "chat") {
|
||||
);
|
||||
return {
|
||||
...d,
|
||||
value: planValue(e, d.variant, type.defaultValue),
|
||||
typeKey: type.id,
|
||||
typeRevision: type.contractRevision,
|
||||
decisionId,
|
||||
fallbackVariant:
|
||||
e.state === "finished"
|
||||
@@ -329,7 +377,7 @@ export async function trackUiEvent(
|
||||
`INSERT IGNORE INTO dh_events(experiment_id,decision_id,account,variant,cohort,event_name,conversation_id,request_id)
|
||||
SELECT d.experiment_id,d.id,d.account,d.variant,IF(d.reason='override','manual','random'),?,?,?
|
||||
FROM dh_decisions d JOIN dh_experiments e ON e.id=d.experiment_id JOIN dh_scenes s ON s.id=e.scope
|
||||
WHERE d.id=? AND d.account=? AND s.runtime='ui' AND d.reason IN ('allocated','sticky','override') AND ${validity}`,
|
||||
WHERE d.id=? AND d.account=? AND s.consumer_key='ui' AND d.reason IN ('allocated','sticky','override') AND ${validity}`,
|
||||
[
|
||||
event,
|
||||
conversationId,
|
||||
|
||||
Reference in New Issue
Block a user