From 8a9a3c79d31297723fb3f007c1e118f26d90c6de Mon Sep 17 00:00:00 2001 From: Jiaqiiii523 <293262669+Jiaqiiii523@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:31:50 +0800 Subject: [PATCH] fix(leases): fold Unicode normalization aliases in write-scope comparison NFC and NFD spellings of one host path now overlap in the comparison view; stored scope and request spelling stay unchanged. Signed-off-by: Jiaqiiii523 <293262669+Jiaqiiii523@users.noreply.github.com> --- docs/reference/canonical-lease-renew.md | 6 ++- .../work_items/task_lease_acquire_decision.ts | 7 ++- .../test_canonical_lease_scopes.py | 54 +++++++++++++++++++ .../task_lease_workspace.test.ts | 22 +++++++- 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/docs/reference/canonical-lease-renew.md b/docs/reference/canonical-lease-renew.md index f0cd49f27c..418548e5fd 100644 --- a/docs/reference/canonical-lease-renew.md +++ b/docs/reference/canonical-lease-renew.md @@ -44,7 +44,10 @@ New canonical acquisitions freeze the canonical Todo's normalized override and no inference from the CLI working directory. Within one Goal, overlapping relative paths conflict unless **both** execution grants have known, different repository identities, or the caller uses the verified cooperative -code-edit mode below. Host/path case aliases remain overlapping. +code-edit mode below. Host/path case aliases remain overlapping. Unicode +canonical equivalents (NFC/NFD) of one path are host aliases too: the +comparison folds them to NFC while the stored scope and request spelling stay +unchanged. The existing complete-head scan, owner eligibility, TTL, generations, CAS and receipt identities are unchanged; an empty scope set still does not conflict. @@ -57,6 +60,7 @@ Equivalent strings in an accepted Todo declaration do not require a declaration revision before atomic claim/acquire. 冲突比较会归并相对路径的词法别名,但保留 glob 字符类中的斜杠、点与字面量右括号。 +同一路径的 NFC/NFD 规范等价写法也按主机别名折叠为 NFC;存储范围与请求拼写不变。 新租约保存规范范围;原请求的拼写仍参与不可变请求身份,旧请求在升级或续租后按原参数 重试会恢复原回执与当前执行证明。已有 Todo 声明中的等价字符串不会要求用户先重录声明。 diff --git a/loopx/control_plane/work_items/task_lease_acquire_decision.ts b/loopx/control_plane/work_items/task_lease_acquire_decision.ts index fb381a7af8..642e9b36d5 100644 --- a/loopx/control_plane/work_items/task_lease_acquire_decision.ts +++ b/loopx/control_plane/work_items/task_lease_acquire_decision.ts @@ -115,11 +115,14 @@ function foldCharacter(character: string): string { /** * Folding one code point at a time keeps the fold composable across a path, so a * comparison-only view can fold substrings without changing the retained - * spelling or the immutable request identity. + * spelling or the immutable request identity. Canonical normalization runs on + * the full value first: NFC and NFD spellings of one host path must overlap even + * though the number of code points differs. Character class bodies stay as + * authored, so their membership semantics do not change. */ function caseFold(value: string): string { let folded = ""; - for (const character of value) folded += foldCharacter(character); + for (const character of value.normalize("NFC")) folded += foldCharacter(character); return folded; } diff --git a/tests/control_plane/test_canonical_lease_scopes.py b/tests/control_plane/test_canonical_lease_scopes.py index dd869aa01e..ffb8e1523e 100644 --- a/tests/control_plane/test_canonical_lease_scopes.py +++ b/tests/control_plane/test_canonical_lease_scopes.py @@ -13,6 +13,8 @@ REPO = Path(__file__).resolve().parents[2] ALIASES = ["src//main.ts", "src/./main.ts"] +NFC_SCOPE = "src/caf\u00e9.ts" +NFD_SCOPE = "src/cafe\u0301.ts" @pytest.fixture(params=["file", "sqlite"]) @@ -114,6 +116,58 @@ def test_case_aliases_remain_exclusive_write_scopes(scope_goal): assert current() == before +def test_unicode_normalization_aliases_remain_exclusive_write_scopes(scope_goal): + cli, current = scope_goal + acquired = cli("task-lease", "acquire", "--todo-id", "todo_scope_a", "--owner", "agent-a", + "--idempotency-key", "normalization-a", "--expected-version", "0", "--write-scope", NFD_SCOPE) + assert acquired["lease"]["write_scopes"] == [NFD_SCOPE] + before = current() + rejected = cli("task-lease", "acquire", "--todo-id", "todo_scope_b", "--owner", "agent-b", + "--idempotency-key", "normalization-b", "--expected-version", "0", "--write-scope", NFC_SCOPE, + expected_exit=1) + assert rejected["error_code"] == "write_scope_conflict" + assert current() == before + + +def test_unicode_normalization_aliases_conflict_at_atomic_todo_claim(scope_goal): + cli, current = scope_goal + repository = "git:github.com/example/project" + + def add_todo(label, scope): + return cli("todo", "add", "--role", "agent", "--priority", "P1", + "--text", f"Normalization scope {label}", "--task-repository", repository, + "--required-write-scope", scope, + "--operation-id", f"normalization-add-{label}")["todo_id"] + + first = add_todo("a", NFD_SCOPE) + claimed = cli("todo", "claim", "--todo-id", first, "--claimed-by", "agent-a", "--agent-id", "agent-a", + "--claim-operation-id", "normalization-claim-a", + "--task-lease-idempotency-key", "normalization-claim-a", + "--task-lease-expected-version", "0") + assert claimed["ok"] + assert cli("task-lease", "inspect", "--todo-id", first)["lease"]["write_scopes"] == [NFD_SCOPE] + + second = add_todo("b", NFC_SCOPE) + before = current() + rejected = cli("todo", "claim", "--todo-id", second, "--claimed-by", "agent-b", "--agent-id", "agent-b", + "--claim-operation-id", "normalization-claim-b", + "--task-lease-idempotency-key", "normalization-claim-b", + "--task-lease-expected-version", "0", expected_exit=1) + assert rejected["error_code"] == "write_scope_conflict" + assert current() == before + + +def test_unicode_normalization_distinct_paths_do_not_conflict(scope_goal): + cli, _ = scope_goal + cli("task-lease", "acquire", "--todo-id", "todo_scope_a", "--owner", "agent-a", + "--idempotency-key", "normalization-distinct-a", "--expected-version", "0", + "--write-scope", "src/cafe.ts") + allowed = cli("task-lease", "acquire", "--todo-id", "todo_scope_b", "--owner", "agent-b", + "--idempotency-key", "normalization-distinct-b", "--expected-version", "0", + "--write-scope", NFC_SCOPE) + assert allowed["acquired"] + + @pytest.mark.parametrize("first_scope,second_scope", [ ("src/**/*.ts", "src/main.ts"), ("src/main.ts", "src/**/*.ts"), diff --git a/tests/control_plane_ts/task_lease_workspace.test.ts b/tests/control_plane_ts/task_lease_workspace.test.ts index c08905499a..e02b4af866 100644 --- a/tests/control_plane_ts/task_lease_workspace.test.ts +++ b/tests/control_plane_ts/task_lease_workspace.test.ts @@ -4,7 +4,7 @@ import {execFileSync} from "node:child_process"; import {mkdtemp, rm, mkdir, writeFile, symlink, realpath} from "node:fs/promises"; import {platform, tmpdir} from "node:os"; import {join} from "node:path"; -import {canonicalWriteScope, comparisonWriteScopes, decideTaskLeaseAcquire, evaluateTaskLeaseAcquireDecision, evaluateTaskLeaseWriteScopesOverlap} from "../../loopx/control_plane/work_items/task_lease_acquire_decision.ts"; +import {canonicalWriteScope, comparisonWriteScopes, decideTaskLeaseAcquire, evaluateQuotaSchedulingWriteScopesOverlap, evaluateTaskLeaseAcquireDecision, evaluateTaskLeaseWriteScopesOverlap} from "../../loopx/control_plane/work_items/task_lease_acquire_decision.ts"; import {normalizeWriteScopes} from "../../loopx/control_plane/work_items/task_lease_acquire.ts"; import {canonicalTaskLeaseAcquireFacts} from "../../loopx/control_plane/coordination/task_lease_state.ts"; import {indexCoordinationProjection} from "../../loopx/control_plane/coordination/coordination_projection.ts"; @@ -357,6 +357,26 @@ test("scope comparison folds case aliases without rewriting retained or request assert.equal(evaluateTaskLeaseWriteScopesOverlap({left: [left], right: [right]}).overlap, false, `${left} excludes ${right}`); } }); +test("scope comparison folds Unicode normalization aliases without rewriting retained spelling", () => { + const nfc = "src/caf\u00e9.ts"; + const nfd = "src/cafe\u0301.ts"; + for (const [left, right] of [ + [nfc, nfd], + [nfd, nfc], + ["src/cafe\u0301/*.ts", "src/caf\u00e9/file.ts"], + ["src/?.ts", "src/e\u0301.ts"], + ["src/[\u00e9]x", "src/e\u0301x"], + ]) { + assert.equal(evaluateTaskLeaseWriteScopesOverlap({left: [left], right: [right]}).overlap, true, `${left} overlaps ${right}`); + } + assert.equal(evaluateTaskLeaseWriteScopesOverlap({ + left: ["src/cafe.ts"], + right: [nfc], + }).overlap, false); + assert.deepEqual(normalizeWriteScopes([nfd, nfc]), [nfd, nfc]); + assert.deepEqual(comparisonWriteScopes([nfd]), [nfd]); + assert.equal(evaluateQuotaSchedulingWriteScopesOverlap({left: [nfd], right: [nfc]}).overlap, true); +}); test("glob matching consumes code points, not UTF-16 code units", () => { assert.equal(evaluateTaskLeaseWriteScopesOverlap({ left: ["src/?.ts"],