@tansr/protocol 0.2.0 → 0.3.0

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.
package/README.md CHANGED
@@ -67,6 +67,7 @@ toHex(digest as Uint8Array);
67
67
  - `contract-v0` 自 W0 末冻结,任何形的变更走 RFC(`doc/rfc/`),修订号 `contract-v0.x` 与同名 git tag 对应;事件包络 `v` 维持 `v0`。
68
68
  - 包版本遵「**0.x 期 minor = breaking**」(doc/89 §3.3):导出集任何增 / 删 / 签名变至少 minor,patch 不得携新导出;发版门 ④ 以 `exports.snapshot.json` + `exports.subpaths.snapshot.json` 与上一 tag 比对强制执行。
69
69
  - 预发布 `X.Y.Z-rc.N` 走 dist-tag `next`,`-beta.N` 走 `beta`,正式版 `latest`;变更记录见 [`CHANGELOG.md`](./CHANGELOG.md),每版发布回执落仓内 `doc/release/protocol-<version>.md`,无回执无 tag(tag 形 `@tansr/protocol@<version>`)。
70
+ - 发行链(仓内):`pnpm build` 串本包 `tsc -p tsconfig.build.json`;`pnpm release:pack-protocol` 在独立合成目录真 `tsc` 后 `npm pack` 出 `dist/release/tansr-protocol-<version>.tgz`(白名单 `package.json` / `LICENSE` / `README.md` / `dist/**`,入口 `.` 与 `./canonical` 经 `publishConfig` 镜像到 dist,仅 `zod` 一个外部依赖、零 `@tansr/*`),只打包不发布;正式发布恒走 `scripts/release/publish.mjs`(`PUBLISH_ORDER` 首位 protocol → sdk → serve → cli → api-client),逐包过六步门。首次公开发行 **0.2.0**(2026-10-02,`latest`),回执 `doc/release/protocol-0.2.0.md`。
70
71
  - canonical 规则与错误码表的规范文本:[RFC-UAPI-1 统一信封与 canonical 单模块](../../doc/rfc/RFC-UAPI-1-统一信封与canonical单模块.md) §4;统一 API 合同的结构 schema:`doc/rfc/unified-v1.schema.json`。
71
72
 
72
73
  ## 许可
@@ -11,145 +11,171 @@
11
11
  import { CANONICAL_KEY_PATTERN, normalizeOptions, utf8Length, wellFormed } from './common.js';
12
12
  import { CanonicalError, childPointer } from './errors.js';
13
13
  const encoder = new TextEncoder();
14
+ const ASCII_ONLY = /^[\x00-\x7f]*$/;
15
+ /** 已通过 `CANONICAL_KEY_PATTERN` 的短键 → `"key":`;纯函数记忆,满即清空。 */
16
+ const quotedKeys = new Map();
17
+ const QUOTED_KEYS_MAX = 4096, QUOTED_KEY_MAX_LENGTH = 64;
14
18
  class Writer {
15
19
  maxBytes;
16
20
  maxDepth;
17
21
  maxNodes;
18
22
  isProxy;
19
- parts = [];
23
+ text = '';
20
24
  used = 0;
21
25
  nodes = 0;
22
26
  ancestors = new Set();
27
+ /** 从根到当前值的键 / 下标;只在报错时拼成 JSON Pointer。 */
28
+ keys = [];
23
29
  constructor({ maxBytes, maxDepth, maxNodes, isProxy }) {
24
30
  this.maxBytes = maxBytes;
25
31
  this.maxDepth = maxDepth;
26
32
  this.maxNodes = maxNodes;
27
33
  this.isProxy = isProxy;
28
34
  }
29
- fail(code, message, path) {
35
+ /** `up`:报错位置在当前值之上几层(元素之间的逗号属于容器,`up = 1`)。 */
36
+ fail(code, message, up = 0) {
37
+ let path = '';
38
+ for (let index = 0; index < this.keys.length - up; index++)
39
+ path = childPointer(path, this.keys[index]);
30
40
  throw new CanonicalError(code, message, { path });
31
41
  }
32
42
  /** 结构符 / 数字 / 字面量 / ASCII 键:字节数 = 码元数。 */
33
- ascii(part, path) {
43
+ ascii(part, up = 0) {
34
44
  this.used += part.length;
35
45
  if (this.used > this.maxBytes)
36
- this.fail('bytes_exceeded', `encoded bytes exceed maxBytes ${this.maxBytes}`, path);
37
- this.parts.push(part);
46
+ this.fail('bytes_exceeded', `encoded bytes exceed maxBytes ${this.maxBytes}`, up);
47
+ this.text += part;
38
48
  }
39
49
  /** 已加引号的字符串:先以码元数快速否决(UTF-8 字节 ≥ 码元数),再精确累计。 */
40
- quoted(part, path) {
50
+ quoted(part) {
41
51
  if (part.length > this.maxBytes - this.used)
42
- this.fail('bytes_exceeded', `encoded bytes exceed maxBytes ${this.maxBytes}`, path);
52
+ this.fail('bytes_exceeded', `encoded bytes exceed maxBytes ${this.maxBytes}`);
43
53
  this.used += utf8Length(part);
44
54
  if (this.used > this.maxBytes)
45
- this.fail('bytes_exceeded', `encoded bytes exceed maxBytes ${this.maxBytes}`, path);
46
- this.parts.push(part);
55
+ this.fail('bytes_exceeded', `encoded bytes exceed maxBytes ${this.maxBytes}`);
56
+ this.text += part;
47
57
  }
48
- number(value, path) {
58
+ number(value) {
49
59
  if (!Number.isInteger(value))
50
- this.fail('invalid_number', 'number must be an integer (no fraction, NaN or Infinity)', path);
60
+ this.fail('invalid_number', 'number must be an integer (no fraction, NaN or Infinity)');
51
61
  if (!Number.isSafeInteger(value))
52
- this.fail('unsafe_integer', 'integer exceeds 2^53-1', path);
62
+ this.fail('unsafe_integer', 'integer exceeds 2^53-1');
53
63
  if (value < 0 || Object.is(value, -0))
54
- this.fail('invalid_number', 'number must be nonnegative (no -0)', path);
55
- this.ascii(String(value), path);
64
+ this.fail('invalid_number', 'number must be nonnegative (no -0)');
65
+ this.ascii(String(value));
56
66
  }
57
- string(value, path) {
67
+ string(value) {
68
+ // 纯 ASCII 串无代理项,转义后仍是 ASCII:字节数 = 码元数,与 quoted 两道检查同一结论、同一路径。
69
+ if (ASCII_ONLY.test(value)) {
70
+ this.ascii(JSON.stringify(value));
71
+ return;
72
+ }
58
73
  if (!wellFormed(value))
59
- this.fail('lone_surrogate', 'string contains an unpaired surrogate', path);
60
- this.quoted(JSON.stringify(value), path);
74
+ this.fail('lone_surrogate', 'string contains an unpaired surrogate');
75
+ this.quoted(JSON.stringify(value));
61
76
  }
62
- dataDescriptor(target, key, path) {
77
+ dataDescriptor(target, key) {
63
78
  const descriptor = Object.getOwnPropertyDescriptor(target, key);
64
79
  if (descriptor === undefined || !Object.hasOwn(descriptor, 'value') || !descriptor.enumerable) {
65
- this.fail('invalid_property', 'properties must be enumerable own data properties (no accessor / hole)', path);
80
+ this.fail('invalid_property', 'properties must be enumerable own data properties (no accessor / hole)');
66
81
  }
67
82
  return descriptor.value;
68
83
  }
69
- array(value, depth, path) {
84
+ array(value, depth) {
70
85
  // `length` 是不可枚举的自有数据属性:直接读描述符的值,不经访问器。
71
86
  const length = Object.getOwnPropertyDescriptor(value, 'length')?.value;
72
87
  if (typeof length !== 'number' || !Number.isSafeInteger(length))
73
- this.fail('invalid_property', 'array length is not an integer', path);
88
+ this.fail('invalid_property', 'array length is not an integer');
74
89
  // 每个元素至少一个节点:越界必然发生,提前否决,避免先遍历再失败。
75
90
  if (this.nodes + length > this.maxNodes)
76
- this.fail('nodes_exceeded', `node count exceeds maxNodes ${this.maxNodes}`, path);
91
+ this.fail('nodes_exceeded', `node count exceeds maxNodes ${this.maxNodes}`);
77
92
  const keys = Reflect.ownKeys(value);
78
93
  if (keys.length !== length + 1)
79
- this.fail('invalid_property', 'array must be dense without extra own properties', path);
80
- this.ascii('[', path);
94
+ this.fail('invalid_property', 'array must be dense without extra own properties');
95
+ this.ascii('[');
81
96
  for (let index = 0; index < length; index++) {
82
- const item = childPointer(path, index);
83
- const element = this.dataDescriptor(value, String(index), item);
97
+ this.keys.push(index);
98
+ const element = this.dataDescriptor(value, String(index));
84
99
  if (index > 0)
85
- this.ascii(',', path);
86
- this.write(element, depth + 1, item);
100
+ this.ascii(',', 1);
101
+ this.write(element, depth + 1);
102
+ this.keys.pop();
87
103
  }
88
- this.ascii(']', path);
104
+ this.ascii(']');
89
105
  }
90
- object(value, depth, path) {
106
+ object(value, depth) {
91
107
  const prototype = Object.getPrototypeOf(value);
92
108
  if (prototype !== Object.prototype && prototype !== null)
93
- this.fail('unsupported_value', 'objects must be plain (Object.prototype or null prototype)', path);
109
+ this.fail('unsupported_value', 'objects must be plain (Object.prototype or null prototype)');
94
110
  const ownKeys = Reflect.ownKeys(value);
95
111
  for (const key of ownKeys)
96
112
  if (typeof key !== 'string')
97
- this.fail('invalid_property', 'symbol keys are not representable', path);
113
+ this.fail('invalid_property', 'symbol keys are not representable');
98
114
  const keys = ownKeys.sort();
99
- this.ascii('{', path);
115
+ this.ascii('{');
100
116
  for (let index = 0; index < keys.length; index++) {
101
117
  const key = keys[index];
102
- const member = childPointer(path, key);
103
- const element = this.dataDescriptor(value, key, member);
104
- if (!CANONICAL_KEY_PATTERN.test(key))
105
- this.fail('invalid_key', 'keys must be nonempty printable ASCII (0x21-0x7e)', member);
118
+ this.keys.push(key);
119
+ const element = this.dataDescriptor(value, key);
120
+ let quoted = quotedKeys.get(key);
121
+ if (quoted === undefined) {
122
+ if (!CANONICAL_KEY_PATTERN.test(key))
123
+ this.fail('invalid_key', 'keys must be nonempty printable ASCII (0x21-0x7e)');
124
+ quoted = JSON.stringify(key) + ':';
125
+ if (key.length <= QUOTED_KEY_MAX_LENGTH) {
126
+ if (quotedKeys.size >= QUOTED_KEYS_MAX)
127
+ quotedKeys.clear();
128
+ quotedKeys.set(key, quoted);
129
+ }
130
+ }
106
131
  if (index > 0)
107
- this.ascii(',', path);
108
- this.ascii(JSON.stringify(key) + ':', member);
109
- this.write(element, depth + 1, member);
132
+ this.ascii(',', 1);
133
+ this.ascii(quoted);
134
+ this.write(element, depth + 1);
135
+ this.keys.pop();
110
136
  }
111
- this.ascii('}', path);
137
+ this.ascii('}');
112
138
  }
113
- write(value, depth, path) {
139
+ write(value, depth) {
114
140
  if (depth > this.maxDepth)
115
- this.fail('depth_exceeded', `nesting depth exceeds maxDepth ${this.maxDepth}`, path);
141
+ this.fail('depth_exceeded', `nesting depth exceeds maxDepth ${this.maxDepth}`);
116
142
  if (++this.nodes > this.maxNodes)
117
- this.fail('nodes_exceeded', `node count exceeds maxNodes ${this.maxNodes}`, path);
143
+ this.fail('nodes_exceeded', `node count exceeds maxNodes ${this.maxNodes}`);
118
144
  if (value === null) {
119
- this.ascii('null', path);
145
+ this.ascii('null');
120
146
  return;
121
147
  }
122
148
  switch (typeof value) {
123
149
  case 'string':
124
- this.string(value, path);
150
+ this.string(value);
125
151
  return;
126
152
  case 'boolean':
127
- this.ascii(value ? 'true' : 'false', path);
153
+ this.ascii(value ? 'true' : 'false');
128
154
  return;
129
155
  case 'number':
130
- this.number(value, path);
156
+ this.number(value);
131
157
  return;
132
158
  case 'object': break;
133
- default: this.fail('unsupported_value', `${typeof value} is not representable`, path);
159
+ default: this.fail('unsupported_value', `${typeof value} is not representable`);
134
160
  }
135
161
  const target = value;
136
162
  if (this.isProxy !== null && this.isProxy(target))
137
- this.fail('unsupported_value', 'Proxy objects are not representable', path);
163
+ this.fail('unsupported_value', 'Proxy objects are not representable');
138
164
  if (this.ancestors.has(target))
139
- this.fail('cyclic_reference', 'value contains itself', path);
165
+ this.fail('cyclic_reference', 'value contains itself');
140
166
  this.ancestors.add(target);
141
167
  if (Array.isArray(target))
142
- this.array(target, depth, path);
168
+ this.array(target, depth);
143
169
  else
144
- this.object(target, depth, path);
170
+ this.object(target, depth);
145
171
  this.ancestors.delete(target);
146
172
  }
147
173
  }
148
174
  /** canonical 文本(UTF-16 字符串形态;与 `encodeCanonical` 同字节,供宿主内部比较免去编码 / 解码往返)。 */
149
175
  export function encodeCanonicalText(value, options) {
150
176
  const writer = new Writer(normalizeOptions(options, 'encode'));
151
- writer.write(value, 0, '');
152
- return writer.parts.join('');
177
+ writer.write(value, 0);
178
+ return writer.text;
153
179
  }
154
180
  /** canonical 字节(UTF-8 Uint8Array)。`options.maxBytes` 必填;`maxDepth` / `maxNodes` 只可收紧。 */
155
181
  export function encodeCanonical(value, options) {
package/dist/index.d.ts CHANGED
@@ -502,6 +502,11 @@
502
502
  * 配套严格校验 schema;旧 maxChars 加权语义不变。API 按 tts-input-limit-v1 做新旧投影,
503
503
  * SDK/kernel/serve 和示例同源消费;计费量、事件包络和 agent wire revision 不因此改变。
504
504
  * API/管理端会签及用户明确批准后实施,tag 待主线收编,不提前发布。
505
+ * - contract-v0.30(STO-01,2026-10-28 用户会签文件日志冷归档格式5最小增量):
506
+ * SessionLogFileManifest 独立描述 session-log-file-v2 的存储身份、提交锚、恢复头及分页材料目录;
507
+ * ColdStorageManifest 为新共同读取入口。SegmentManifest 继续仅表示格式1—4,不改变旧写者;
508
+ * 最高支持版本5与原段合同最高版本4分离。原 owner/lease 不成为恢复授权,wire 包络保持 v0。
509
+ * 新 schema/golden 与出口同步冻结;本笔不发布,后续发布须符合0.x minor门。
505
510
  */
506
511
  /** contract-v0.28(MEDIA-03,用户授权 2026-09-14):tool_result.artifact 原值保全,
507
512
  * 新产物受限 v1 分类;含扩展的存储格式提升 v2,旧 writer 拒绝写入。
@@ -520,6 +525,8 @@ export * from './metering.js';
520
525
  export * from './identity.js';
521
526
  export * from './daemon.js';
522
527
  export * from './storage.js';
528
+ export * from './persistence-custody.generated.js';
529
+ export * from './persistence-custody.js';
523
530
  export * from './media.js';
524
531
  export * from './tool-result-artifact.js';
525
532
  export * from './redact-patterns.js';
package/dist/index.js CHANGED
@@ -502,6 +502,11 @@
502
502
  * 配套严格校验 schema;旧 maxChars 加权语义不变。API 按 tts-input-limit-v1 做新旧投影,
503
503
  * SDK/kernel/serve 和示例同源消费;计费量、事件包络和 agent wire revision 不因此改变。
504
504
  * API/管理端会签及用户明确批准后实施,tag 待主线收编,不提前发布。
505
+ * - contract-v0.30(STO-01,2026-10-28 用户会签文件日志冷归档格式5最小增量):
506
+ * SessionLogFileManifest 独立描述 session-log-file-v2 的存储身份、提交锚、恢复头及分页材料目录;
507
+ * ColdStorageManifest 为新共同读取入口。SegmentManifest 继续仅表示格式1—4,不改变旧写者;
508
+ * 最高支持版本5与原段合同最高版本4分离。原 owner/lease 不成为恢复授权,wire 包络保持 v0。
509
+ * 新 schema/golden 与出口同步冻结;本笔不发布,后续发布须符合0.x minor门。
505
510
  */
506
511
  /** contract-v0.28(MEDIA-03,用户授权 2026-09-14):tool_result.artifact 原值保全,
507
512
  * 新产物受限 v1 分类;含扩展的存储格式提升 v2,旧 writer 拒绝写入。
@@ -520,6 +525,9 @@ export * from './metering.js';
520
525
  export * from './identity.js';
521
526
  export * from './daemon.js';
522
527
  export * from './storage.js';
528
+ // PST-SERVE-01 已会签:可选托管交接合同及跨语言摘要字节;原业务存储合同不变。
529
+ export * from './persistence-custody.generated.js';
530
+ export * from './persistence-custody.js';
523
531
  // 媒体能力内核化 K-1(doc/128 §3.1;加法模块不占号):四媒体产物形单源 + 授权模型描述子 +
524
532
  // parseMediaArtifact 三端形状判别参考实现
525
533
  export * from './media.js';
@@ -0,0 +1,13 @@
1
+ import type { PersistenceCustodyIdentity, PersistenceCustodyRecord, PersistenceCustodyRootRecord, PersistenceCustodyLegacyRootRecord, PersistenceCustodyRootProof, PersistenceCustodyPrepareRequest } from './persistence-custody.generated.js';
2
+ /** 原业务根与控制摘要固定一个快照;源/目的托管位置不参与业务身份。 */
3
+ export declare function persistenceCustodySnapshotBytes(identity: PersistenceCustodyIdentity, root: PersistenceCustodyRootRecord | PersistenceCustodyLegacyRootRecord): Uint8Array;
4
+ /** 空目录初值。目录非空,第一项必须为与 fixedRoot 相符的原根描述。 */
5
+ export declare function persistenceCustodyInventorySeedBytes(identity: PersistenceCustodyIdentity, originFormat: PersistenceCustodyRootProof['originFormat'], fixedRoot: string): Uint8Array;
6
+ /** 每项链式摘要;index 为全目录原序号,从 "0" 开始,与分页大小无关。 */
7
+ export declare function persistenceCustodyRecordBytes(index: string, previousDigest: string, record: PersistenceCustodyRecord): Uint8Array;
8
+ /** 管理意图摘要排除传输 envelope 与 operationId,本键重试不得换意图。 */
9
+ export declare function persistenceCustodyPrepareIntentBytes(request: PersistenceCustodyPrepareRequest): Uint8Array;
10
+ /** 原 PST 请求本身定义 intentSha256;原字节不受托管迁移改写。 */
11
+ export declare function persistenceCustodyWriterIntentBytes(originalRequest: unknown): Uint8Array;
12
+ /** 写者认领 operationId = SHA256(本函数返回的 canonical 字节)。 */
13
+ export declare function persistenceCustodyWriterOperationBytes(originalRequest: unknown): Uint8Array;