halfcode-compiler.xnl 0.2.2 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +14 -0
  2. package/dist/application-assembly.js +1 -1
  3. package/dist/{index-96qHPZj2.d.ts → index-D7UtYNm7.d.ts} +1 -1
  4. package/dist/{index-DdRHFKSQ.d.ts → index-DihSfO7X.d.ts} +33 -2
  5. package/dist/{index-D2mpux6L.d.ts → index-DjvNDidT.d.ts} +1 -1
  6. package/dist/index.d.ts +5 -4
  7. package/dist/index.js +14 -4
  8. package/dist/kind-definition.d.ts +1 -1
  9. package/dist/{resource-core-Byhqyadq.d.ts → resource-core-UAvFx3m-.d.ts} +1 -1
  10. package/dist/resource-core.d.ts +3 -3
  11. package/dist/resource-core.js +4 -4
  12. package/dist/resource-projection.d.ts +1 -1
  13. package/dist/skill-capsule.d.ts +1 -1
  14. package/dist/skill-capsule.js +1 -1
  15. package/dist/{src-DRIws6tY.js → src-6nezdi57.js} +1 -1
  16. package/dist/{src-DCRih45n.js → src-Bj7BZokJ.js} +237 -36
  17. package/dist/{src-D1Bmq7Lo.js → src-ClihwpeP.js} +1 -1
  18. package/dist/system-skills/resource-dsl/KindDefinitions/SkillCapsule/manifest.xnl +21 -0
  19. package/dist/system-skills/resource-dsl/KindDefinitions/WikiPage/manifest.xnl +10 -0
  20. package/dist/system-skills/resource-dsl/SkillCapsules/System/SKILL.metadata.yaml +2 -0
  21. package/dist/system-skills/resource-dsl/SkillCapsules/System/SKILL.template.ejs +20 -0
  22. package/dist/system-skills/resource-dsl/SkillCapsules/System/manifest.xnl +18 -0
  23. package/dist/system-skills/resource-dsl/SkillCapsules/System/resource-mappings.xnl +10 -0
  24. package/dist/system-skills/resource-dsl/WikiPages/documents/manifest.xnl +6 -0
  25. package/dist/system-skills/resource-dsl/WikiPages/files/manifest.xnl +6 -0
  26. package/dist/system-skills/resource-dsl/WikiPages/index/manifest.xnl +6 -0
  27. package/dist/system-skills/resource-dsl/WikiPages/language/manifest.xnl +6 -0
  28. package/dist/system-skills/resource-dsl/WikiPages/migration/manifest.xnl +6 -0
  29. package/dist/system-skills/resource-dsl/WikiPages/projections/manifest.xnl +6 -0
  30. package/dist/system-skills/resource-dsl/WikiPages/skill-capsules/manifest.xnl +6 -0
  31. package/dist/system-skills/resource-dsl/documents.md +119 -0
  32. package/dist/system-skills/resource-dsl/examples/function.xnl +12 -0
  33. package/dist/system-skills/resource-dsl/examples/index.md +17 -0
  34. package/dist/system-skills/resource-dsl/examples/kind-definition.xnl +12 -0
  35. package/dist/system-skills/resource-dsl/examples/resource-mappings.xnl +21 -0
  36. package/dist/system-skills/resource-dsl/examples/resource-package.xnl +19 -0
  37. package/dist/system-skills/resource-dsl/examples/skill-capsule-authoring.xnl +11 -0
  38. package/dist/system-skills/resource-dsl/examples/skill-capsule-devops.xnl +12 -0
  39. package/dist/system-skills/resource-dsl/examples/skill-capsule-kind-definition.xnl +27 -0
  40. package/dist/system-skills/resource-dsl/examples/skill-capsule-resource-dsl.xnl +8 -0
  41. package/dist/system-skills/resource-dsl/examples/skill-capsule-run.xnl +8 -0
  42. package/dist/system-skills/resource-dsl/files.md +47 -0
  43. package/dist/system-skills/resource-dsl/index.md +59 -0
  44. package/dist/system-skills/resource-dsl/language.md +64 -0
  45. package/dist/system-skills/resource-dsl/manifest.xnl +10 -0
  46. package/dist/system-skills/resource-dsl/migration.md +40 -0
  47. package/dist/system-skills/resource-dsl/projections.md +142 -0
  48. package/dist/system-skills/resource-dsl/skill-capsules.md +94 -0
  49. package/package.json +1 -1
@@ -0,0 +1,142 @@
1
+ # Resource Projections and Dependency Authority
2
+
3
+ ## Authority map
4
+
5
+ 资源 authoring、组合与运行闭包是三个不同层次,不能互相替代:
6
+
7
+ | Fact | Authority owner | Halfcode projection |
8
+ |---|---|---|
9
+ | package、Catalog、KindDefinition、resource descriptor | XNL ResourcePackage | `LoadedResourceTree extends ResourceTree` |
10
+ | layer precedence | 调用方提供的有序 `ResourceLayerInput[]` | `EffectiveResourceRegistry.layers` |
11
+ | suppression | 调用方显式提供的 `ResourceTombstone` | effective/tombstone/shadow provenance |
12
+ | Kind-specific dependency semantics | 对应 Kind owner 或 consumer | `ResourceDependencyEdge[]` |
13
+ | XNL authority bytes | canonical loader 的同一次 raw-byte read | `LoadedResourceTree.contentIdentities` |
14
+ | Kind-specific material bytes | Kind owner 提供的显式 digest contribution | effective `ResourceContentIdentity` |
15
+ | roots 与 dependency closure | 调用方的显式 roots 加 typed edges | `ResourceDependencySnapshot` |
16
+
17
+ `ResourceTree` 是单个已验证 package 的规范化结果。`EffectiveResourceRegistry` 只是多个 tree 的确定性只读投影;它会 deep-clone/freeze record、metadata、node/value 与 KindDefinition facts,避免调用方之后修改输入而改变既有投影,但不会原地冻结或改写旧 `ResourceTree`。`ResourceDependencySnapshot` 又是某组 roots 在一个 content-sensitive registry revision 上的冻结闭包。两种投影都可由 authority facts 重建,不得反写任一 XNL package,也不得成为第二份 authoring 真源。
18
+
19
+ Snapshot builder 只接受同一 resource-core runtime 中由 `composeLayeredResourceRegistry()` 返回的 canonical immutable registry。手工构造、浅拷贝或改写 revision 字段的结构相似对象会 fail closed;需要跨进程恢复时,应从 XNL packages、ordered layers 与 tombstones 重新组合。Snapshot 会复制并冻结自己的 origin facts,不共享可变调用方引用。
20
+
21
+ ## Ordered named layers
22
+
23
+ layer 只包含一个不透明、稳定的 `id` 和一个已验证 `ResourceTree`。数组从低 precedence 到高 precedence 排列,后一个 layer 优先。Halfcode 不保留特殊 layer 名称,不根据名称重排,也不拥有任何产品安装目录或机器路径策略;实际 roots 由 consumer 在调用 resource-core 之前绑定。
24
+
25
+ 组合规则是:
26
+
27
+ 1. 同一 `resourceId`、同一 kind 的高层资源成为 effective;低层 origin 留在 `shadowed`。
28
+ 2. 同一 identity 跨 kind,或同一 resource kind 的规范化 KindDefinition contract 不兼容,组合失败并返回稳定 diagnostics。
29
+ 3. 高层没有某个资源只表示“未提供”,绝不表示删除。
30
+ 4. 删除语义必须通过 `ResourceTombstone { resourceId, expectedKind?, reason? }` 显式声明;tombstone origin 与历史保留在 projection 中。
31
+ 5. `byKind` 只包含 effective resources;完整 effective、shadow 与 tombstone 证据由 `byId` 保存。
32
+ 6. `documentUri` 必须是与安全相对 `logicalPath` 一致的 canonical `vfs://@/` URI;非 loader 来源的绝对机器路径或 traversal provenance 会 fail closed。
33
+
34
+ ```ts
35
+ import {
36
+ composeLayeredResourceRegistry,
37
+ type ResourceLayerInput,
38
+ } from "halfcode-compiler.xnl/resource-core"
39
+
40
+ declare const lowerPackage: ResourceLayerInput["tree"]
41
+ declare const higherPackage: ResourceLayerInput["tree"]
42
+
43
+ const registry = composeLayeredResourceRegistry({
44
+ layers: [
45
+ { id: "base", tree: lowerPackage },
46
+ {
47
+ id: "override",
48
+ tree: higherPackage,
49
+ tombstones: [{ resourceId: "demo.retired", expectedKind: "Function" }],
50
+ },
51
+ ],
52
+ })
53
+ ```
54
+
55
+ 示例中的名字仅用于展示调用顺序,不携带预定义语义。consumer 可以选择任何稳定 id;改变数组顺序才会改变 precedence。
56
+
57
+ ## Explicit content identity
58
+
59
+ canonical loader 对每个 XNL authority 文件只读取一次 raw bytes:同一次读取同时用于 fatal UTF-8 decode、XNL parse 和 SHA-256。成功加载后返回 additive `LoadedResourceTree`;其 `contentIdentities` 精确覆盖 `registry.byKind` 的业务 resources,不包含 package manifest 或 KindDefinition。multi-root document 的 roots 共享同一文件级 `authorityDigest`,但各自的 `resourceId` 和 `contentDigest` 独立。
60
+
61
+ `ResourceTree` 仍是兼容父接口,手工 structural tree 仍可参与 generic layer composition。只有 canonical loader 返回并由当前 runtime 登记的 `LoadedResourceTree` 才能进入 `resolveEffectiveResourceContentIdentities()`。projector exact 核对 registry 的 layer id、顺序、package 与 effective origin,从每个 effective layer 选择 loader identity;shadowed non-effective resource 与 tombstone 不进入结果。Kind owner 再把 prompt、schema、instruction 等 material 作为带稳定 `key`、`digest` 和可选 `sourceUri` 的 contribution 显式提供。
62
+
63
+ `createResourceContentIdentity()` 按 UTF-16 code-unit key 顺序 canonicalize contributions,并拒绝任何重复 key;即使 digest 相同,重复或不同 provenance 也不是可静默折叠的合法事实。snapshot builder 会对全部 effective resources 重新规范化 authority/contribution facts、重算 `contentDigest` 并核对 claim;它不会只验证 roots 可达的 identity,也不会信任调用方构造的对象。`sourceUri` 是 provenance,不允许机器绝对路径进入 content revision;调用方应使用 Resource DSL 定义的逻辑 URI。
64
+
65
+ ```ts
66
+ import {
67
+ composeLayeredResourceRegistry,
68
+ loadResourceTree,
69
+ resolveEffectiveResourceContentIdentities,
70
+ } from "halfcode-compiler.xnl/resource-core"
71
+
72
+ declare const lowerPackageRoot: string
73
+ declare const higherPackageRoot: string
74
+ declare const contributions: Parameters<typeof resolveEffectiveResourceContentIdentities>[0]["contributions"]
75
+
76
+ const lowerPackage = await loadResourceTree({ rootDir: lowerPackageRoot })
77
+ const higherPackage = await loadResourceTree({ rootDir: higherPackageRoot })
78
+ const registry = composeLayeredResourceRegistry({
79
+ layers: [
80
+ { id: "lower", tree: lowerPackage },
81
+ { id: "higher", tree: higherPackage },
82
+ ],
83
+ })
84
+
85
+ const identities = resolveEffectiveResourceContentIdentities({
86
+ registry,
87
+ layers: [
88
+ { id: "lower", tree: lowerPackage },
89
+ { id: "higher", tree: higherPackage },
90
+ ],
91
+ contributions,
92
+ })
93
+ ```
94
+
95
+ 示例中的 `lowerPackageRoot`、`higherPackageRoot` 与 `contributions` 由 consumer 在调用边界绑定。Halfcode 不定义机器安装目录或 layer precedence policy,也不会重复读取 XNL authority。projector 的结果可直接传给 snapshot builder;snapshot builder 仍不执行隐藏 IO。
96
+
97
+ ## Typed dependency closure
98
+
99
+ 依赖的字段语义属于 Kind owner。它负责把自己的 descriptor contract 转成 `ResourceDependencyEdge { fromResourceId, toResourceId, relation, declaredBy }`。resource-core 只校验 endpoint、从显式 roots 沿这些边遍历、拒绝可达 cycle,并生成稳定排序的 snapshot。
100
+
101
+ ```ts
102
+ import {
103
+ buildResourceDependencySnapshot,
104
+ type EffectiveResourceRegistry,
105
+ type ResourceDependencyEdge,
106
+ type ResourceContentIdentity,
107
+ } from "halfcode-compiler.xnl/resource-core"
108
+
109
+ declare const registry: EffectiveResourceRegistry
110
+ declare const workflowIdentity: ResourceContentIdentity
111
+ declare const schemaIdentity: ResourceContentIdentity
112
+
113
+ const edges: readonly ResourceDependencyEdge[] = [{
114
+ fromResourceId: workflowIdentity.resourceId,
115
+ toResourceId: schemaIdentity.resourceId,
116
+ relation: "uses-schema",
117
+ declaredBy: "WorkflowKindDefinition",
118
+ }]
119
+
120
+ const snapshot = buildResourceDependencySnapshot({
121
+ registry,
122
+ roots: [workflowIdentity.resourceId],
123
+ edges,
124
+ contentIdentities: new Map([
125
+ [workflowIdentity.resourceId, workflowIdentity],
126
+ [schemaIdentity.resourceId, schemaIdentity],
127
+ ]),
128
+ })
129
+ ```
130
+
131
+ 普通文本、instruction 或 `ResourceNode` property 即使看起来像资源 URI,只要 Kind owner 没有提供 typed edge,就不会进入 closure。resource-core 不遍历任意字符串、不匹配自然语言关键词,也不猜测引用字段。
132
+
133
+ ## Deterministic revisions
134
+
135
+ - content revision 覆盖 resource identity、authority digest 与按 key 排序的 material digests。
136
+ - `compositionRevision` 覆盖显式 layer 顺序、effective identity/presence、shadow/tombstone facts、选定 KindDefinition 的 authority `resourceId`/兼容 contract 与 canonical `documentUri/logicalPath` provenance;`EffectiveResourceRegistry.revision` 是它的精确兼容别名。ResourceRecord descriptor/node/metadata bytes 不进入该摘要,而由显式 content identity 负责。不同 definition identity 不会自行把相同 normalized Kind contract 变成冲突。
137
+ - snapshot 的 `registryRevision` 覆盖 `compositionRevision` 与全部 effective resource 的已验证 content revisions,包括当前 roots 不可达的资源。
138
+ - `snapshotRevision` 覆盖显式 root 顺序、可达资源 content revisions、参与闭包的 typed edges 与 `registryRevision`。
139
+ - revisions 使用带算法前缀的 SHA-256 形式;逻辑 provenance 在 projection 中保留,机器绝对路径不得进入摘要。
140
+ - canonical object key、resource、kind、edge 与 contribution 排序固定使用 ECMAScript UTF-16 code-unit 顺序,不依赖 locale、ICU、文件枚举或 Map 插入顺序。
141
+
142
+ 缺失 endpoint、缺失 content identity、identity-kind/KindDefinition conflict、矛盾 digest 或 dependency cycle 都会 fail closed;调用方不会收到看似完整的 partial snapshot。
@@ -0,0 +1,94 @@
1
+ # SkillCapsule Dependencies And Distribution
2
+
3
+ ## Identity authority
4
+
5
+ 新的 `SkillCapsule` descriptor 由 XNL 根节点独占 FQN、consumer-facing `name`、`apiVersion` 与 `version`。`SKILL.metadata.yaml` 只保存 title/description 等呈现字段,不重复 name/version;assembly 会把 canonical XNL name/version 投影给模板。若 descriptor 已声明 `name` 而 YAML 仍重复该字段,assembly 在 compiler 写入前以 `SKILL_CAPSULE_NAME_DUPLICATE` 失败。历史 descriptor 未声明 XNL name 时,既有 YAML name 与 FQN 尾段 fallback 继续兼容;新资源不得采用该兼容路径。
6
+
7
+ XNL `version` 也是 capsule identity 与 dependency binding 的唯一 version authority。历史 YAML 若仍带 `version`,必须与 XNL 严格相等,否则 assembly 以 `SKILL_CAPSULE_VERSION_CONFLICT` 失败。
8
+
9
+ ```xnl
10
+ <SkillCapsule #example.resource_lifecycle.skill.authoring
11
+ apiVersion="halfcode.resources/v1"
12
+ version="1.0.0" {
13
+ name = "example-resource-authoring"
14
+ description = "Generic resource authoring operations."
15
+ } (
16
+ <SkillMetadata { href = "vfs://./SKILL.metadata.yaml" format = "yaml" }>
17
+ <Template { href = "vfs://./SKILL.template.ejs" format = "ejs" }>
18
+ )>
19
+ ```
20
+
21
+ 对应 YAML 只保存呈现字段:
22
+
23
+ ```yaml
24
+ title: Example Resource Authoring
25
+ description: Generic resource authoring operations.
26
+ ```
27
+
28
+ ## `SkillDependencies` 与 `Includes`
29
+
30
+ 两种关系不可互换,也不做名称或文本推断:
31
+
32
+ - `SkillDependencies/SkillDependency` 声明同一个 `ApplicationAssembly.byFqn` resource universe 中的 sibling `SkillCapsule`。`ref` 必须是 `resource://<FQN>`,目标 kind 必须是 `SkillCapsule`,`version` 是必填的精确 binding;当前版本不解释 semver range。
33
+ - `Includes/Include` 选择要编入当前 capsule 的 Function、PromptFragment 等普通资源。即便一个 Include 写了 `kind = "SkillCapsule"`,planner 也不会把它提升为 sibling dependency。
34
+
35
+ ```xnl
36
+ <SkillDependencies [
37
+ <SkillDependency {
38
+ ref = "resource://example.resource_lifecycle.skill.resource_dsl"
39
+ version = "1.0.0"
40
+ }>
41
+ ]>
42
+ <Includes [
43
+ <Include {
44
+ kind = "PromptFragment"
45
+ ref = "resource://example.resource_lifecycle.prompt.authoring_prelude"
46
+ }>
47
+ ]>
48
+ ```
49
+
50
+ 缺失目标、错误 kind、重复 dependency、精确版本不匹配和 cycle 都会 fail closed。依赖解析只读取既有 application assembly,不创建第二份 catalog 或 scanner。
51
+
52
+ ## 四 Skill canonical topology
53
+
54
+ 规范示例使用与具体产品无关的四个 Skill:
55
+
56
+ ```text
57
+ resource DSL -> authoring -> devops/root
58
+ run -> devops/root
59
+ ```
60
+
61
+ - `devops/root` 显式依赖 `authoring` 与 `run`;
62
+ - `authoring` 显式依赖共享 `resource DSL`;
63
+ - `run` 与 `resource DSL` 没有 sibling dependency。
64
+
65
+ 完整 descriptor 分别位于 [skill-capsule-devops.xnl](examples/skill-capsule-devops.xnl)、[skill-capsule-authoring.xnl](examples/skill-capsule-authoring.xnl)、[skill-capsule-run.xnl](examples/skill-capsule-run.xnl) 和 [skill-capsule-resource-dsl.xnl](examples/skill-capsule-resource-dsl.xnl)。可加载、可规划的 bundle fixture 位于 `apps/demo-resource-workflow-authoring/resources-xnl/SkillCapsules/Topology*`。所有 consumer name 与 version 都由 XNL 声明,dependency 绑定 `1.0.0`,YAML 不重复 identity 字段。
66
+
67
+ ## Deterministic plan 与 atomic apply
68
+
69
+ `planSkillCapsuleDistribution` 与 `applySkillCapsuleDistributionPlan` 是两个独立阶段:
70
+
71
+ 1. planner 从显式 roots 解析传递闭包,按固定 code-unit 顺序生成 dependency-first topology;
72
+ 2. planner 读取所有 generated/mapped bytes,并将 canonical bytes 保存为不可变、可序列化的 `contentBase64` authority;兼容字段 `content` 每次读取只返回新的 defensive copy;
73
+ 3. planner 对所有会进入 SKILL/reference/callable bytes 的资源集合按 FQN 使用固定 UTF-16 code-unit comparator 排序,并对完整 closure 做重复 output name、相同 target 与文件/目录 prefix collision preflight;
74
+ 4. plan 保存 versioned content/capsule/closure digest,且不含绝对路径、时间戳或函数对象;plan 成功或失败都不写目标 root;
75
+ 5. applier 先 fail-closed 校验非空 roots、root 可达的完整闭包、canonical base64 与全部 digest,再在 caller 提供的 `outputRoot` 同父目录 stage、readback,以 backup/rename/rollback 成组切换。
76
+
77
+ 每个 planned capsule 还必须包含固定路径 `references/.halfcode/provenance.json`。该文件由 compiler 生成,不由模板维护;它记录 `halfcode.skill-provenance/v1` format、`halfcode.skill-distribution/v1` generator、SkillCapsule XNL 的 FQN/apiVersion/version,以及除自身以外全部 payload files 的 canonical path/content digest。manifest 自身作为普通 planned file 进入 capsule/closure digest,因此不需要自引用摘要。applier 会在任何目标写入前从 plan 重建并 exact 校验这些 facts。
78
+
79
+ ```ts
80
+ const plan = await planSkillCapsuleDistribution({
81
+ assembly,
82
+ rootSkillFqns: ["example.resource_lifecycle.skill.devops"],
83
+ })
84
+
85
+ await applySkillCapsuleDistributionPlan(plan, { outputRoot })
86
+ ```
87
+
88
+ Halfcode 只拥有通用 resource dependency、plan 与 atomic apply contract;目标 root、安装位置、生命周期阶段和升级策略都由 caller/host 决定。compiler 不推导这些 policy,也不把它们写进资源 authority。
89
+
90
+ ## Consumer Resource DSL Skill
91
+
92
+ `docs/resource-dsl/manifest.xnl` 将本目录的 canonical 文档登记为 ResourcePackage/WikiPage resources,并声明 `Halfcode.ResourceDsl.Skill.System`。compiler 从这些原始文档生成 `sys-halfcode-resource-dsl@1.0.0` 的 references;`SKILL.md` 只提供渐进加载顺序,不复制规范正文,也不包含仓库维护命令或 host 安装操作。仓库维护者继续使用 `skills/framework-resource-dsl`,两者职责不可互换。
93
+
94
+ 既有 `compileSkillCapsule`、`compileResourceSkillCapsule` 与 `SkillCapsulePlan` 保持兼容。新 planner/applier 和类型从 package root 及 `halfcode-compiler.xnl/skill-capsule` additive 导出。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "halfcode-compiler.xnl",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Compile XNL-described applications into runtime-ready skill capsules.",
5
5
  "type": "module",
6
6
  "private": false,