Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4b9ec5d
feat(db): add workspace badcase assignments (#582)
HaleyBG Nov 24, 2026
31af0b5
feat(api): configure workspace badcase labels (#582)
HaleyBG Nov 24, 2026
7e6d410
feat(sync): route public badcases by label (#582)
HaleyBG Nov 24, 2026
58b6286
fix(ingest): preserve workspace-owned badcases (#582)
HaleyBG Nov 24, 2026
dce73dd
refactor(api): unify workspace badcase visibility (#582)
HaleyBG Nov 24, 2026
05f7cc4
feat(web): manage workspace badcase labels (#582)
HaleyBG Nov 25, 2026
2d0a46c
docs: record workspace badcase label routing (#582)
HaleyBG Nov 25, 2026
3d10075
test(api): repair association-boundary fixtures and assertions (#582)
HaleyBG Nov 25, 2026
8e2f0a0
Merge remote-tracking branch 'origin/main' into codex/issue-582-badca…
HaleyBG Nov 25, 2026
418c711
fix(prototype): transparent badcase label chip remove button (#582)
HaleyBG Nov 25, 2026
adddd47
fix(test): backfill legacy associations in shared fixture seeds (#582)
HaleyBG Nov 25, 2026
164c32e
Merge remote-tracking branch 'origin/main' into codex/issue-582-badca…
HaleyBG Nov 25, 2026
913dd44
fix(web): hydrate label draft on entry and reset it on leave (#582)
HaleyBG Nov 25, 2026
94c018b
fix(ci): repair label routing gates and association fixtures (#582)
ZTGolem Nov 25, 2026
767296c
fix(api): close label routing review findings (#582)
ZTGolem Nov 25, 2026
82c40b1
Merge remote-tracking branch 'origin/main' into codex/issue-582-badca…
ZTGolem Nov 25, 2026
bf7f091
fix(ci): serialize catalog savepoints and renumber flow issues (#582)
ZTGolem Nov 25, 2026
13ad2b6
Merge remote-tracking branch 'origin/main' into codex/issue-582-badca…
ZTGolem Nov 25, 2026
95d6eb1
Merge remote-tracking branch 'origin/main' into codex/issue-582-badca…
ZTGolem Nov 25, 2026
6811e32
chore(flow): close #599 after PR #607 merge (#582)
ZTGolem Nov 25, 2026
d57fca8
Merge remote-tracking branch 'origin/main' into codex/issue-582-badca…
ZTGolem Nov 25, 2026
aa9fdcd
Merge remote-tracking branch 'origin/main' into codex/issue-582-badca…
ZTGolem Nov 25, 2026
feb88d4
chore(flow): close #600 after PR #606 merge (#582)
ZTGolem Nov 25, 2026
3462dfc
Merge remote-tracking branch 'origin/main' into codex/issue-582-badca…
ZTGolem Nov 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ English | [中文](bugfix-bad-case-sync-type-title.md)

```yaml flow
kind: issue
id: bugfix-56
id: bugfix-61
milestone: bugfix
depends:
- m6-16
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 双语配对一致性记录(docs/i18n/README.md):两侧在最后一次确认一致状态下的
# git blob hash。两种语言同等权威;编辑任一侧后带上另一侧,然后重录:
# pnpm run record:i18n -- .agents/flow/issues/bugfix-bad-case-sync-type-title.md
bugfix-bad-case-sync-type-title.md: f4e45ba4ca7827736c83279fb505f6e523731b84
bugfix-bad-case-sync-type-title.en.md: e35fa3db53045f615616bf0a5ec38490924e444d
bugfix-bad-case-sync-type-title.md: f2d6e372e117f8f52e8a50178a2421d6afb348fe
bugfix-bad-case-sync-type-title.en.md: c4945429a0c3b7edc4f5f34838b8f656278345f3
2 changes: 1 addition & 1 deletion .agents/flow/issues/bugfix-bad-case-sync-type-title.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

```yaml flow
kind: issue
id: bugfix-56
id: bugfix-61
milestone: bugfix
depends:
- m6-16
Expand Down
90 changes: 90 additions & 0 deletions .agents/flow/issues/bugfix-evolution-label-allowlist.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Assign public bad cases to workspaces by label

English | [中文](bugfix-evolution-label-allowlist.md)

```yaml flow
kind: issue
id: bugfix-60
milestone: bugfix
depends: []
priority: P2
status: in-progress
scope:
- apps/api/**
- apps/web/**
- packages/contracts/**
- tests/**
- plans/**
- .agents/**
- docs/**
adr: []
github:
number: 582
url: https://github.com/Shadow-Azure/harness-evolution/issues/582
```

## Background

Production bad cases are reported to one public repository. The platform periodically pulls mixed categories from that source, and `items[].meta_data.label` identifies each category. Workspaces configure accepted labels; the same label may be configured by multiple workspaces, so one matching bad case must be available to all of them.

## Goals

Add a Badcase types tab to workspace management and use its rules to build a many-to-many assignment between public bad cases and workspaces. Browsing, Sessions, statistics and evolution then share that assignment boundary.

## Design authority

Follow the [evolution domain](../../../plans/fullstack-design/02-features.md#22-自进化域) and public catalog sync design. Update the relevant design chapter and an Agent Note during implementation.

## Problem, investigation and root cause

1. apps/api/app/third_party_api/bad_case_query/http.py calls GET {HE_BAD_CASE_QUERY_BASE_URL}/bad-cases with bad_case_id, offset and limit, without label parameters.
2. models.py in that directory declares BadCaseItem.meta_data as Any | None without a label contract. This supports carrying labels but does not prove their presence or type in live responses.
3. apps/api/app/service/acquisition/bad_case_catalog_sync.py preserves meta_data but upserts public records directly into bad_cases without parsing labels or assigning workspaces.
4. bad_cases.bad_case_id is a global primary key and each row has one workspace_id. It cannot represent one public bad case assigned to several workspaces, while copying rows conflicts with the global key.
5. Browsing, Sessions, statistics and PotentialMatcher.associated_sessions use bad_cases.workspace_id. Evolution-only filtering would create inconsistent boundaries and still cannot expose one case to several workspaces.
6. apps/web/src/pages/workspaces/Admin.vue has no label-rule UI, while the backend lacks rule persistence, reconciliation and unassigned-data observability.

The root cause is coupling the public bad-case fact to a single workspace_id, plus the absence of a many-to-many label assignment rule and post-sync allocation process. Findings are static code analysis; production data was not queried. Confirm label shape and completeness with sanitized samples.

## Implementation

The sole recommended solution is to keep bad_cases as the public fact table, add workspace label rules and a badcase-workspace relation, and have sync plus reconciliation maintain the relation. Every workspace read path uses that relation.

- Add workspace_bad_case_labels(workspace_id, label), unique by the pair. One label may map to many workspaces. Add a manually managed Badcase types tab; global and workspace administrators may write, with server-side authorization.
- Add workspace_bad_cases(workspace_id, bad_case_id, assignment_source, label, assigned_at), unique by workspace, case and source, where assignment_source is legacy_owner / direct_ingest / catalog_label and label only appears on catalog_label rows. Keep one public fact in bad_cases and stop using its single workspace_id as ownership. Migrate existing workspace_id values into legacy_owner relations and remove legacy-field dependencies, for SQLite and MySQL.
- Sync validates meta_data.label, upserts the public fact, finds every workspace accepting that label, and idempotently writes all relations. One bad case can belong to many workspaces while its source record remains singular.
- Saving rules starts a controlled reconciliation over local public facts for affected labels. Adding a rule backfills history; removing it deletes only that workspace's live relations. No cursor rewind or upstream refetch is required, and persisted inputs of existing runs remain intact.
- Browsing, Sessions, statistics, quota counts, attribution candidates and evolution all constrain through workspace_bad_cases. Existing target, version, time and evidence filters still intersect with this boundary.
- Recommend nonempty, case-sensitive exact string labels. Reject blank and surrounding whitespace. Preserve records with missing, null, empty or non-string labels but leave them unassigned, with invalid/unassigned sync counters; never put them in a default workspace.
- When upstream changes a historical label, remove relations produced by the old label and rebuild from the new label within a transaction or recoverable job boundary.

## Implementation steps

1. Verify sanitized label samples and fix the label contract, migration design and Agent Note.
2. Implement both relation tables, migrate current workspace_id data, add the authorized rule API and generate shared contracts.
3. Update public sync and reconciliation, including invalid/unassigned observability.
4. Move every workspace read path to the relation and complete UI and cross-stack verification.

## Dependencies and boundary

No upstream filtering API is required; one public source and one global cursor remain. Actual label type verification is an implementation prerequisite. This issue owns workspace assignment and all consuming read paths, not upstream data production.

## Verification

Use public fixtures with A/B, missing, null and numeric labels. Workspace one accepts A; workspace two accepts A/B. Run pnpm test:api:serial, pnpm test:db, pnpm test:db:mysql, pnpm test and pnpm test:e2e. Verify that A is stored once and related to both spaces, B only to space two, and invalid/unconfigured labels remain unassigned. Adding and removing rules reconciles history; browsing, statistics, Sessions and full/cluster_only evolution see identical sets; retries create no duplicates and existing runs still resume.

## Acceptance

- One label can serve several workspaces; each source bad case is stored once and related to every matching workspace.
- Rules persist with authorization; additions and removals deterministically reconcile history and unauthorized writes are rejected.
- Missing, invalid and unconfigured labels enter no workspace and have observable counts.
- Browsing, Sessions, statistics, quotas and evolution share the same set; sync and reconciliation are idempotent and existing runs remain recoverable.

## Gates and CI

Implementation runs pnpm contract:generate, pnpm contract:check, pnpm typecheck, pnpm typecheck:web, pnpm lint:api, pnpm lint:web, pnpm test:db, pnpm test:db:mysql and the behavior tests above. CI includes new cases and maintains at least 80% coverage. Documentation runs pnpm record:i18n -- .agents/flow/issues/bugfix-evolution-label-allowlist.md, pnpm verify:flow and pnpm lint:docs. Issue creation does not claim product acceptance tests passed.

## Non-goals

No upstream changes, public fact deletion, label inference or completion, label dictionary, or retrospective modification of existing run inputs.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 双语配对一致性记录(docs/i18n/README.md):两侧在最后一次确认一致状态下的
# git blob hash。两种语言同等权威;编辑任一侧后带上另一侧,然后重录:
# pnpm run record:i18n -- .agents/flow/issues/bugfix-evolution-label-allowlist.md
bugfix-evolution-label-allowlist.md: 69f962120196489a9e4a56abed64d144d0aae320
bugfix-evolution-label-allowlist.en.md: 8aefb25a78e31712f737997a1088a0af53eb5efb
90 changes: 90 additions & 0 deletions .agents/flow/issues/bugfix-evolution-label-allowlist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# 按 label 将公共 badcase 划分到空间

[English](bugfix-evolution-label-allowlist.en.md) | 中文

```yaml flow
kind: issue
id: bugfix-60
milestone: bugfix
depends: []
priority: P2
status: in-progress
scope:
- apps/api/**
- apps/web/**
- packages/contracts/**
- tests/**
- plans/**
- .agents/**
- docs/**
adr: []
github:
number: 582
url: https://github.com/Shadow-Azure/harness-evolution/issues/582
```

## 背景

生产环境中的 badcase 统一上报到公共仓库,平台定期从同一个仓库拉取多种类型的数据,`items[].meta_data.label` 表示 badcase 大类型。空间需要配置自己接收的 label;同一个 label 可以同时配置给多个空间,匹配的数据应同时对这些空间可见并参与各自后续自进化。

## 目标

在空间管理增加「Badcase 类型」页签维护当前空间接受的 label。公共仓库同步后,平台按 label 建立 badcase 与空间的多对多分配关系,使列表、Session、统计和自进化统一使用同一空间数据边界。

## 设计依据

遵循 [自进化域](../../../plans/fullstack-design/02-features.md#22-自进化域) 与公共 badcase 同步设计。本 issue 补充公共目录到空间数据面的划分规则,实施时同步对应设计章节与 Agent Note。

## 问题描述、分析过程与根因

1. `apps/api/app/third_party_api/bad_case_query/http.py` 调用 GET `{HE_BAD_CASE_QUERY_BASE_URL}/bad-cases`,参数是 bad_case_id、offset、limit,没有 label 筛选参数。
2. 同目录 `models.py` 中 `BadCaseItem.meta_data` 是 `Any | None`,没有显式 label 契约。代码可承载 label,但尚未核实真实响应中的存在性与类型。
3. `apps/api/app/service/acquisition/bad_case_catalog_sync.py` 的 `_row` 保留完整 meta_data,但目录同步按递增 ID 将公共记录直接 upsert 到 `bad_cases`,没有解析 label 或执行空间分配。
4. `bad_cases.bad_case_id` 是全局主键且每行只有一个 `workspace_id`。该模型无法表达“同一公共 badcase 因同一 label 同时属于多个空间”;把记录复制到多行也会与全局主键冲突。
5. 当前列表、Session、统计和 `PotentialMatcher.associated_sessions` 都通过 `bad_cases.workspace_id` 限定空间。若只在自进化入口过滤,其他空间数据面会与自进化口径不一致,也无法让同一 badcase进入多个空间。
6. `apps/web/src/pages/workspaces/Admin.vue` 缺少 label 分配规则入口,后端也没有规则存储、重算和未分配观测能力。

结论:根因是公共 badcase 事实与空间归属耦合在 `bad_cases.workspace_id` 单值字段中,同时缺少 label 到空间的多对多规则及同步后的统一分配过程。真实 label 类型与完整性仍需脱敏样本确认;上述结论来自静态代码分析,未查询生产数据。

## 实现内容

唯一推荐方案:把 `bad_cases` 作为公共仓库事实表,新增空间 label 规则表和 badcase—空间关联表,由目录同步与规则重算共同维护关联;所有空间数据查询统一通过关联表限定范围。

- 新增 `workspace_bad_case_labels(workspace_id, label)`,联合唯一;同一 label 可对应多个 workspace。空间管理新增「Badcase 类型」页签,V1 手动添加、删除和保存。全局管理员与空间管理员可写,后端强制权限检查。
- 新增 `workspace_bad_cases(workspace_id, bad_case_id, assignment_source, label, assigned_at)`,以 `(workspace_id, bad_case_id, assignment_source)` 唯一;`assignment_source` 三态 `legacy_owner` / `direct_ingest` / `catalog_label`,`label` 只落在 `catalog_label` 行。`bad_cases` 保存一份公共原始事实,不再以单值 `workspace_id` 表达归属;迁移把现有记录的 workspace_id 回填为 `legacy_owner` 关联,再逐步移除旧字段依赖。SQLite/MySQL 同步迁移。
- 公共同步验证 `meta_data.label` 后先幂等 upsert 公共事实,再查询接受该 label 的全部空间,为每个空间幂等写入关联。同一 badcase 可因此属于多个空间,但原始记录只保存一次。
- 保存 label 配置后,在同一受控任务中按本地公共事实重算受影响 label 的关联:新增规则补齐历史数据,删除规则移除该空间的实时关联;不需要回退同步游标或重新请求上游。已创建运行持久化的候选和报告不被删除。
- 列表、Session、统计、额度计数、归因候选和自进化取数全部由 `workspace_bad_cases` 关联限定;因此自进化无需再实现一套独立 label 过滤,原有对象、版本、时间和证据条件继续取交集。
- 建议 V1 label 为非空字符串,区分大小写精确匹配;配置去重并拒绝空白和首尾空格。缺失、null、空串和非字符串 label 的公共记录保留但不分配,纳入同步状态中的 invalid/unassigned 计数,不进入默认空间。
- 上游更新历史 badcase 的 label 时,先删除旧 label 产生的关联,再按新 label 重建;整个重分配具备事务或可恢复任务边界,避免数据同时消失或重复。

## 实施步骤

1. 核实脱敏 label 样本,固定 label 契约、迁移方案和 Agent Note。
2. 实现两张关系表、现有 workspace_id 数据迁移、规则 API 与共享协议。
3. 改造公共目录同步和配置重算任务,补未分配/非法 label 观测。
4. 将所有空间读路径切到关联表,完成管理页签与跨栈验证。

## 依赖与交付边界

不依赖上游新增筛选接口,公共仓库仍只配置一个来源且同步游标保持全局。真实 label 类型确认是实施前置条件。本 issue 负责空间划分及其所有读路径;不修改上游仓库的数据生产方式。

## 独立验证

合成公共数据含 A/B、无 label、null、数字 label;空间一接受 A,空间二接受 A/B。执行 `pnpm test:api:serial`、`pnpm test:db`、`pnpm test:db:mysql`、`pnpm test`、`pnpm test:e2e`。验证 A 记录只存一份但关联两个空间,B 只关联空间二,非法/未配置 label 保留且不分配;新增规则能补齐历史关联,删除规则只移除目标空间关联;列表、统计、Session 和 full/cluster_only 自进化看到一致集合,重试不会产生重复关联,既有运行可继续恢复。

## 验收标准

- 同一 label 可配置给多个空间,同一 badcase 原始事实只存一份并正确关联所有匹配空间。
- 配置持久化且权限隔离;新增、删除规则会确定性重算历史关联,非管理员写入被拒绝。
- 缺失、非法和未配置 label 不进入任何空间,并有可观测计数。
- 列表、Session、统计、额度和自进化使用相同空间集合;同步及重算重试保持幂等,既有运行不被配置变化破坏。

## 门禁与 CI

实现运行 `pnpm contract:generate`、`pnpm contract:check`、`pnpm typecheck`、`pnpm typecheck:web`、`pnpm lint:api`、`pnpm lint:web`、`pnpm test:db`、`pnpm test:db:mysql` 及上述行为测试,CI 纳入新增用例且覆盖率不少于 80%。文档运行 `pnpm record:i18n -- .agents/flow/issues/bugfix-evolution-label-allowlist.md`、`pnpm verify:flow`、`pnpm lint:docs`。本次创建 issue 不代表这些产品验收已通过。

## 非目标

不改上游接口、不删除公共原始 badcase、不推断或补全 label、不做标签字典、不追溯修改已经创建的运行输入。

Loading
Loading