Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion docs/reference/canonical-lease-renew.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 声明中的等价字符串不会要求用户先重录声明。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] NFC 折叠漏掉已有 glob 的范围重叠。同一保留的分解字符路径,在主干原实现中同时匹配采用两个字符通配符或字符类加字面量的范围。准确头和当前主干整合预演中,这两种范围的租约重叠都从 true 变为 false;正反顺序一致。调度重叠和写入范围覆盖仍为 true。 在现有比较所有者中保留原 glob 命中,然后再补充 NFC 别名匹配。继续用原拼写入库,并为已有使用两个通配符和字符类的声明增加主干/新头的对照。

return folded;
}

Expand Down
54 changes: 54 additions & 0 deletions tests/control_plane/test_canonical_lease_scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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"),
Expand Down
22 changes: 21 additions & 1 deletion tests/control_plane_ts/task_lease_workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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"],
Expand Down
Loading