@danceiny/gotry 0.2.0-rc.32 → 0.2.0-rc.33
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 +2 -2
- package/README.zh-CN.md +2 -2
- package/dist/capabilities/session/bridge-ledger.js +168 -67
- package/dist/capabilities/session/extension-bridge.js +140 -46
- package/dist/capabilities/session/gui-flight.js +281 -69
- package/dist/capabilities/session/gui-hotel.js +74 -17
- package/dist/capabilities/session-search.js +6 -5
- package/dist/scripts/booking-copilot-dsh-planner-proof-tests.js +129 -2
- package/dist/scripts/booking-copilot-dsh-readiness-proof.js +53 -5
- package/dist/scripts/booking-copilot-gateway-chat-adapter-tests.js +233 -0
- package/dist/scripts/booking-copilot-planner-live-probe.js +1 -0
- package/dist/scripts/booking-copilot-startup-proof-tests.js +1 -1
- package/dist/scripts/bridge-ledger-tests.js +20 -18
- package/dist/scripts/fixtures/tenant-runaway-worker-fixture.mjs +73 -0
- package/dist/scripts/mutation-check.js +153 -0
- package/dist/scripts/nightly-evidence.js +4 -111
- package/dist/scripts/session-tests.js +42 -8
- package/dist/scripts/tenant-bridge-org-tests.js +1264 -0
- package/dist/scripts/tenant-capacity-probe.js +263 -0
- package/dist/scripts/tenant-compute-isolation-tests.js +707 -0
- package/dist/scripts/tenant-egress-credentials-tests.js +822 -0
- package/dist/scripts/tenant-executor-binding-tests.js +203 -0
- package/dist/scripts/tenant-prompt-partition-tests.js +265 -0
- package/dist/scripts/tenant-seam-adversarial-tests.js +957 -0
- package/dist/scripts/tenant-seam-quota-tests.js +383 -0
- package/dist/scripts/tenant-seam-test-kit.js +292 -0
- package/dist/scripts/tenant-state-copies-tests.js +607 -0
- package/dist/scripts/tenant-state-drill-tests.js +298 -0
- package/dist/scripts/tenant-state-drill-worker.js +164 -0
- package/dist/scripts/tenant-state-durable-conformance.js +101 -0
- package/dist/scripts/tenant-state-handoff-tests.js +854 -0
- package/dist/scripts/tenant-state-lease-conformance.js +142 -0
- package/dist/scripts/tenant-state-lease-worker.js +28 -0
- package/dist/scripts/tenant-state-router-tests.js +464 -0
- package/dist/scripts/tenant-state-test-kit.js +92 -0
- package/dist/scripts/tenant-state-tests.js +564 -0
- package/dist/src/backend/modules/booking-executor.js +61 -6
- package/dist/src/backend/modules/session-search.js +136 -39
- package/dist/src/booking-surface/dsh-llm-gateway-chat.js +434 -0
- package/dist/src/booking-surface/dsh-planner.js +150 -19
- package/dist/src/booking-surface/index.js +1 -0
- package/dist/src/booking-surface/managed-dsh-run-port.js +121 -7
- package/dist/src/booking-surface/server.js +193 -80
- package/dist/src/booking-surface/startup.js +153 -0
- package/dist/src/booking-surface/tenancy.js +188 -0
- package/dist/src/index.js +31 -1
- package/dist/src/llm-price-table.js +148 -0
- package/dist/src/tenant-seam/index.js +8 -0
- package/dist/src/tenant-seam/llm-broker.js +508 -0
- package/dist/src/tenant-seam/process-governor.js +281 -0
- package/dist/src/tenant-seam/quotas.js +360 -0
- package/dist/src/tenant-seam/seam.js +92 -0
- package/dist/src/tenant-state/durable-store.js +158 -0
- package/dist/src/tenant-state/epoch-copies.js +422 -0
- package/dist/src/tenant-state/errors.js +108 -0
- package/dist/src/tenant-state/ids.js +101 -0
- package/dist/src/tenant-state/index.js +11 -0
- package/dist/src/tenant-state/lease-store.js +203 -0
- package/dist/src/tenant-state/tenant-lease.js +150 -0
- package/dist/src/tenant-state/tenant-router.js +650 -0
- package/extension/manifest.json +2 -2
- package/package.json +4 -1
- package/ts/capabilities/session/bridge-ledger.ts +192 -78
- package/ts/capabilities/session/extension-bridge.ts +176 -65
- package/ts/capabilities/session/gui-flight.ts +318 -88
- package/ts/capabilities/session/gui-hotel.ts +72 -18
- package/ts/capabilities/session-search.ts +17 -5
- package/ts/src/booking-surface/dsh-llm-gateway-chat.js +434 -0
- package/ts/src/booking-surface/dsh-planner.ts +169 -19
- package/ts/src/booking-surface/index.ts +1 -0
- package/ts/src/booking-surface/managed-dsh-run-port.ts +150 -7
- package/ts/src/booking-surface/server.ts +215 -84
- package/ts/src/booking-surface/startup.ts +155 -0
- package/ts/src/booking-surface/tenancy.ts +242 -0
- package/ts/src/index.ts +28 -2
- package/ts/src/llm-price-table.ts +180 -0
- package/ts/src/tenant-seam/index.ts +9 -0
- package/ts/src/tenant-seam/llm-broker.ts +520 -0
- package/ts/src/tenant-seam/process-governor.ts +343 -0
- package/ts/src/tenant-seam/quotas.ts +433 -0
- package/ts/src/tenant-seam/seam.ts +142 -0
- package/ts/src/tenant-state/durable-store.ts +171 -0
- package/ts/src/tenant-state/epoch-copies.ts +491 -0
- package/ts/src/tenant-state/errors.ts +128 -0
- package/ts/src/tenant-state/ids.ts +108 -0
- package/ts/src/tenant-state/index.ts +12 -0
- package/ts/src/tenant-state/lease-store.ts +266 -0
- package/ts/src/tenant-state/tenant-lease.ts +175 -0
- package/ts/src/tenant-state/tenant-router.ts +743 -0
package/README.md
CHANGED
|
@@ -153,7 +153,7 @@ Trust is structural, not promised:
|
|
|
153
153
|
## Project Status and Roadmap
|
|
154
154
|
Current boundary: deterministic planning and evidence-bound read capabilities are available; booking and real-user acceptance remain outside the shipped claim. See the [architecture](docs/architecture.md) and [roadmap](docs/roadmap.md).
|
|
155
155
|
|
|
156
|
-
**v0.2.0-rc.
|
|
156
|
+
**v0.2.0-rc.32** on npm (`latest`). Pre-1.0: the core loop works end to end; evaluation is still at deterministic contracts and validators, with no external scores or uplift claims. External W2A events are contract-only and inert; no real sensor bridge or consumer is active yet.
|
|
157
157
|
|
|
158
158
|
**Working today**
|
|
159
159
|
|
|
@@ -209,7 +209,7 @@ Documents ship as bilingual pairs (`x.md` English + `x.zh-CN.md` 中文); diverg
|
|
|
209
209
|
|
|
210
210
|
**Built with**: DeepSeek Harness 0.2.0-rc.2 (root-pinned) · Cordis · Z3 (WASM) · loopx (pipx) · hotelbyte-cli · Agent-Reach v1.5.0 · OpenFlights · TypeScript
|
|
211
211
|
|
|
212
|
-
**Version baseline: `v0.2.0-rc.
|
|
212
|
+
**Version baseline: `v0.2.0-rc.32` (npm `latest`).** Verification gates: `scripts/run-all-tests.sh`; release flow: `scripts/publish-npm.sh`.
|
|
213
213
|
|
|
214
214
|
<a href="https://www.star-history.com/?repos=danceiny%2Fgotry&type=date&legend=top-left">
|
|
215
215
|
<picture>
|
package/README.zh-CN.md
CHANGED
|
@@ -155,7 +155,7 @@ npx @danceiny/gotry "我想从深圳休整两天,预算 3000" # headless 一
|
|
|
155
155
|
## 状态与路线图
|
|
156
156
|
当前边界:确定性规划与有证据边界的只读能力已经可用;预订与真实用户验收仍不属于当前交付声明。见[架构权威文档](docs/architecture.zh-CN.md)与[路线图](docs/roadmap.zh-CN.md)。
|
|
157
157
|
|
|
158
|
-
npm `latest`:**v0.2.0-rc.
|
|
158
|
+
npm `latest`:**v0.2.0-rc.32**。未到 1.0:核心链路已端到端可用;评测仍停留在确定性合同与校验器阶段,无外部分数、无 uplift 声明。外部 W2A 事件仍是合同层、默认 inert;尚无真实 sensor 桥或 consumer 激活。
|
|
159
159
|
|
|
160
160
|
**今天可用**
|
|
161
161
|
|
|
@@ -211,7 +211,7 @@ AI agent:[`AGENTS.md`](AGENTS.zh-CN.md) 是绑定契约——入场先清扫
|
|
|
211
211
|
|
|
212
212
|
**Built with**: DeepSeek Harness 0.2.0-rc.2 (root-pinned) · Cordis · Z3 (WASM) · loopx (pipx) · hotelbyte-cli · Agent-Reach v1.5.0 · OpenFlights · TypeScript
|
|
213
213
|
|
|
214
|
-
**版本基线:`v0.2.0-rc.
|
|
214
|
+
**版本基线:`v0.2.0-rc.32`(npm `latest`)。** 验证闸:`scripts/run-all-tests.sh`;发布流程:`scripts/publish-npm.sh`。
|
|
215
215
|
|
|
216
216
|
<a href="https://www.star-history.com/?repos=danceiny%2Fgotry&type=date&legend=top-left">
|
|
217
217
|
<picture>
|
|
@@ -16,6 +16,14 @@
|
|
|
16
16
|
* (活动上下文,非权威事实)经 POST /v1/session/bridge/events 上行落账;
|
|
17
17
|
* idem_key 部分唯一索引(WHERE NOT NULL)让重试幂等:同 key 重发不重复落行。
|
|
18
18
|
*
|
|
19
|
+
* AaaS A2(#674,威胁 S-5):组织维度。作业/客户端/节律/事件各行都属于恰好一个组织
|
|
20
|
+
* (`organization` 列,缺省 `local` = 今天的单车道);客户端主键 (organization, client_id)、
|
|
21
|
+
* 节律主键 (organization, site)、事件幂等唯一索引 (organization, idem_key);
|
|
22
|
+
* 按作业 id 的更新与查询一律再带组织条件。组织永远是调用方(队列按自己的条目、
|
|
23
|
+
* 接缝解出的调用者)传入的参数,**绝不来自回包体或请求头**;每个方法先用
|
|
24
|
+
* assertOrganizationId 校验,非法组织在碰库之前就被拒。旧库(无组织列)在打开时原地迁移,
|
|
25
|
+
* 既有行归 `local`,迁移是单向的(迁移后旧版本二进制的节律/客户端写会失败)。
|
|
26
|
+
*
|
|
19
27
|
* 红线继承:本账本只存**控制状态**(job 元数据/回包/节律/客户端能力清单/事件流水),
|
|
20
28
|
* 协议面不存在 cookie 值字段(extension-tests §38 红线断言),账本亦零凭据落库。
|
|
21
29
|
*
|
|
@@ -25,6 +33,7 @@
|
|
|
25
33
|
import Database from 'better-sqlite3';
|
|
26
34
|
import { mkdirSync } from 'node:fs';
|
|
27
35
|
import { dirname, isAbsolute, join } from 'node:path';
|
|
36
|
+
import { assertOrganizationId } from '../../src/tenant-state/ids.js';
|
|
28
37
|
/** 站点节律冷却(毫秒):与 capabilities/session-search.ts 的 MIN_INTERVAL_MS 同口径 30s;导出仅测试/文档锚点 */
|
|
29
38
|
export const BRIDGE_SITE_COOLDOWN_MS = 30_000;
|
|
30
39
|
/** stateRoot 可相对可绝对(与 state-ledger.stateDirOf 同款解析) */
|
|
@@ -35,9 +44,27 @@ function stateDirOf(stateRoot) {
|
|
|
35
44
|
export function bridgeLedgerDbPath(stateRoot) {
|
|
36
45
|
return join(stateDirOf(stateRoot), 'bridge.db');
|
|
37
46
|
}
|
|
38
|
-
|
|
47
|
+
// 主键含组织的两张表:建表与「旧库重建」共用同一份列定义,两处不会漂移
|
|
48
|
+
const CLIENTS_COLUMNS = `(
|
|
49
|
+
organization TEXT NOT NULL DEFAULT 'local',
|
|
50
|
+
client_id TEXT NOT NULL,
|
|
51
|
+
origin TEXT NOT NULL,
|
|
52
|
+
capabilities_json TEXT NOT NULL DEFAULT '[]',
|
|
53
|
+
last_seen_at INTEGER NOT NULL,
|
|
54
|
+
login_sites_json TEXT NOT NULL DEFAULT '[]',
|
|
55
|
+
PRIMARY KEY (organization, client_id)
|
|
56
|
+
)`;
|
|
57
|
+
const PACING_COLUMNS = `(
|
|
58
|
+
organization TEXT NOT NULL DEFAULT 'local',
|
|
59
|
+
site TEXT NOT NULL,
|
|
60
|
+
last_hit_at INTEGER NOT NULL,
|
|
61
|
+
cooldown_until INTEGER NOT NULL DEFAULT 0,
|
|
62
|
+
PRIMARY KEY (organization, site)
|
|
63
|
+
)`;
|
|
64
|
+
const TABLES = `
|
|
39
65
|
CREATE TABLE IF NOT EXISTS bridge_jobs (
|
|
40
66
|
id TEXT PRIMARY KEY,
|
|
67
|
+
organization TEXT NOT NULL DEFAULT 'local',
|
|
41
68
|
kind TEXT NOT NULL,
|
|
42
69
|
site TEXT NOT NULL,
|
|
43
70
|
payload_json TEXT NOT NULL,
|
|
@@ -51,24 +78,14 @@ CREATE TABLE IF NOT EXISTS bridge_jobs (
|
|
|
51
78
|
created_at INTEGER NOT NULL,
|
|
52
79
|
updated_at INTEGER NOT NULL
|
|
53
80
|
);
|
|
54
|
-
CREATE INDEX IF NOT EXISTS bridge_jobs_status ON bridge_jobs(status);
|
|
55
81
|
|
|
56
|
-
CREATE TABLE IF NOT EXISTS bridge_clients
|
|
57
|
-
client_id TEXT PRIMARY KEY,
|
|
58
|
-
origin TEXT NOT NULL,
|
|
59
|
-
capabilities_json TEXT NOT NULL DEFAULT '[]',
|
|
60
|
-
last_seen_at INTEGER NOT NULL,
|
|
61
|
-
login_sites_json TEXT NOT NULL DEFAULT '[]'
|
|
62
|
-
);
|
|
82
|
+
CREATE TABLE IF NOT EXISTS bridge_clients ${CLIENTS_COLUMNS};
|
|
63
83
|
|
|
64
|
-
CREATE TABLE IF NOT EXISTS bridge_pacing
|
|
65
|
-
site TEXT PRIMARY KEY,
|
|
66
|
-
last_hit_at INTEGER NOT NULL,
|
|
67
|
-
cooldown_until INTEGER NOT NULL DEFAULT 0
|
|
68
|
-
);
|
|
84
|
+
CREATE TABLE IF NOT EXISTS bridge_pacing ${PACING_COLUMNS};
|
|
69
85
|
|
|
70
86
|
CREATE TABLE IF NOT EXISTS bridge_events (
|
|
71
87
|
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
88
|
+
organization TEXT NOT NULL DEFAULT 'local',
|
|
72
89
|
ts INTEGER NOT NULL,
|
|
73
90
|
client_id TEXT NOT NULL,
|
|
74
91
|
kind TEXT NOT NULL,
|
|
@@ -77,13 +94,62 @@ CREATE TABLE IF NOT EXISTS bridge_events (
|
|
|
77
94
|
payload_json TEXT NOT NULL,
|
|
78
95
|
idem_key TEXT
|
|
79
96
|
);
|
|
80
|
-
CREATE UNIQUE INDEX IF NOT EXISTS bridge_events_idem_key
|
|
81
|
-
ON bridge_events(idem_key) WHERE idem_key IS NOT NULL;
|
|
82
97
|
`;
|
|
98
|
+
// 索引在迁移之后建:旧库的 bridge_events 在迁移前还没有 organization 列
|
|
99
|
+
const INDEXES = `
|
|
100
|
+
CREATE INDEX IF NOT EXISTS bridge_jobs_status ON bridge_jobs(status);
|
|
101
|
+
CREATE UNIQUE INDEX IF NOT EXISTS bridge_events_org_idem_key
|
|
102
|
+
ON bridge_events(organization, idem_key) WHERE idem_key IS NOT NULL;
|
|
103
|
+
`;
|
|
104
|
+
const ORGANIZATION_TABLES = ['bridge_jobs', 'bridge_clients', 'bridge_pacing', 'bridge_events'];
|
|
105
|
+
function hasOrganizationColumn(db, table) {
|
|
106
|
+
return db.pragma(`table_info(${table})`).some((column) => column.name === 'organization');
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 旧库(组织维度之前)原地迁移:jobs/events 加列,clients/pacing 因主键含组织而重建;
|
|
110
|
+
* 既有行一律归 `local`。整段在一个 IMMEDIATE 事务里,拿到写锁后重新判定哪些表还需要迁移
|
|
111
|
+
* (并发打开同一个库的另一连接可能已迁完),所以幂等且无半迁移状态。
|
|
112
|
+
*/
|
|
113
|
+
function migrateOrganizationDimension(db) {
|
|
114
|
+
const pending = () => ORGANIZATION_TABLES.filter((table) => !hasOrganizationColumn(db, table));
|
|
115
|
+
if (pending().length > 0)
|
|
116
|
+
migrateTables(db, pending);
|
|
117
|
+
// The single-column unique index on idem_key of the legacy schema would make two organizations' identical keys refuse each
|
|
118
|
+
// other (the per-organization one replaces it). An older binary recreates it every time it opens the file, so a file that
|
|
119
|
+
// went new, old, new has nothing left to migrate and still holds it. Drop it on every open (a no-op when absent).
|
|
120
|
+
if (hasIndex(db, 'bridge_events_idem_key'))
|
|
121
|
+
db.transaction(() => { db.exec('DROP INDEX IF EXISTS bridge_events_idem_key'); }).immediate();
|
|
122
|
+
}
|
|
123
|
+
function hasIndex(db, name) {
|
|
124
|
+
return db.prepare("SELECT 1 FROM sqlite_master WHERE type='index' AND name=?").get(name) !== undefined;
|
|
125
|
+
}
|
|
126
|
+
function migrateTables(db, pending) {
|
|
127
|
+
db.transaction(() => {
|
|
128
|
+
for (const table of pending()) {
|
|
129
|
+
if (table === 'bridge_jobs' || table === 'bridge_events') {
|
|
130
|
+
db.exec(`ALTER TABLE ${table} ADD COLUMN organization TEXT NOT NULL DEFAULT 'local'`);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
db.exec(`ALTER TABLE ${table} RENAME TO ${table}__legacy`);
|
|
134
|
+
if (table === 'bridge_clients') {
|
|
135
|
+
db.exec(`CREATE TABLE bridge_clients ${CLIENTS_COLUMNS}`);
|
|
136
|
+
db.exec(`INSERT INTO bridge_clients (organization, client_id, origin, capabilities_json, last_seen_at, login_sites_json)
|
|
137
|
+
SELECT 'local', client_id, origin, capabilities_json, last_seen_at, login_sites_json FROM bridge_clients__legacy`);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
db.exec(`CREATE TABLE bridge_pacing ${PACING_COLUMNS}`);
|
|
141
|
+
db.exec(`INSERT INTO bridge_pacing (organization, site, last_hit_at, cooldown_until)
|
|
142
|
+
SELECT 'local', site, last_hit_at, cooldown_until FROM bridge_pacing__legacy`);
|
|
143
|
+
}
|
|
144
|
+
db.exec(`DROP TABLE ${table}__legacy`);
|
|
145
|
+
}
|
|
146
|
+
}).immediate();
|
|
147
|
+
}
|
|
83
148
|
/**
|
|
84
149
|
* SQLite 落地的桥作业账本(实现 extension-bridge.BridgeJobStore 契约)。
|
|
85
150
|
* 所有写方法单语句/单事务同步提交;调用方(extension-bridge)负责失败兜底
|
|
86
151
|
* (write-before-submit 的 INSERT 失败 = 提交失败,fail-closed 不入队)。
|
|
152
|
+
* 组织是每个方法的第一个参数且必填:漏传是编译错误,不是静默落进 `local`。
|
|
87
153
|
*/
|
|
88
154
|
export class BridgeLedgerStore {
|
|
89
155
|
dbPath;
|
|
@@ -95,62 +161,79 @@ export class BridgeLedgerStore {
|
|
|
95
161
|
mkdirSync(dirname(dbPath), { recursive: true });
|
|
96
162
|
this.db = new Database(dbPath);
|
|
97
163
|
this.db.pragma('journal_mode = WAL');
|
|
164
|
+
// Opening a legacy file needs the write lock once (migration, index changes). A rolling deploy can have the old process
|
|
165
|
+
// writing through it, so the open waits longer than a normal write does and then drops back.
|
|
166
|
+
this.db.pragma('busy_timeout = 30000');
|
|
167
|
+
this.db.exec(TABLES);
|
|
168
|
+
migrateOrganizationDimension(this.db);
|
|
169
|
+
this.db.exec(INDEXES);
|
|
98
170
|
this.db.pragma('busy_timeout = 5000');
|
|
99
|
-
this.db.exec(SCHEMA);
|
|
100
171
|
}
|
|
101
172
|
/** write-before-submit:入队前先落 queued(提交失败 = 调用方不得派发) */
|
|
102
|
-
insertQueuedJob(job, timeoutAt) {
|
|
173
|
+
insertQueuedJob(organization, job, timeoutAt) {
|
|
174
|
+
const org = assertOrganizationId(organization);
|
|
103
175
|
const ts = this.now();
|
|
104
|
-
this.db.prepare(`INSERT INTO bridge_jobs (id, kind, site, payload_json, status, timeout_at, attempts, created_at, updated_at)
|
|
105
|
-
VALUES (@id, @kind, @site, @payload, 'queued', @timeoutAt, 0, @ts, @ts)`).run({ id: job.jobId, kind: job.kind, site: job.site, payload: JSON.stringify(job), timeoutAt, ts });
|
|
176
|
+
this.db.prepare(`INSERT INTO bridge_jobs (id, organization, kind, site, payload_json, status, timeout_at, attempts, created_at, updated_at)
|
|
177
|
+
VALUES (@id, @organization, @kind, @site, @payload, 'queued', @timeoutAt, 0, @ts, @ts)`).run({ id: job.jobId, organization: org, kind: job.kind, site: job.site, payload: JSON.stringify(job), timeoutAt, ts });
|
|
106
178
|
}
|
|
107
179
|
/** 领取(/jobs 派发与 dispatchToParked 两处):claimed + 领取者 + attempts 递增 */
|
|
108
|
-
markClaimed(jobId, claim) {
|
|
180
|
+
markClaimed(organization, jobId, claim) {
|
|
181
|
+
const org = assertOrganizationId(organization);
|
|
109
182
|
const ts = this.now();
|
|
110
183
|
this.db.prepare(`UPDATE bridge_jobs
|
|
111
184
|
SET status='claimed', claimed_by_client=@client, claimed_origin=@origin, claimed_at=@ts, attempts=attempts+1, updated_at=@ts
|
|
112
|
-
WHERE id=@id`).run({ id: jobId, client: claim.clientId ?? null, origin: claim.origin, ts });
|
|
185
|
+
WHERE id=@id AND organization=@organization`).run({ id: jobId, organization: org, client: claim.clientId ?? null, origin: claim.origin, ts });
|
|
113
186
|
}
|
|
114
|
-
markResolved(jobId, result) {
|
|
115
|
-
|
|
187
|
+
markResolved(organization, jobId, result) {
|
|
188
|
+
const org = assertOrganizationId(organization);
|
|
189
|
+
this.db.prepare(`UPDATE bridge_jobs SET status='resolved', result_json=@result, updated_at=@ts WHERE id=@id AND organization=@organization`).run({ id: jobId, organization: org, result: JSON.stringify(result), ts: this.now() });
|
|
116
190
|
}
|
|
117
|
-
markUnresolved(jobId, note) {
|
|
191
|
+
markUnresolved(organization, jobId, note) {
|
|
192
|
+
const org = assertOrganizationId(organization);
|
|
118
193
|
const result = note ? JSON.stringify({ ok: false, reason: note.reason }) : null;
|
|
119
|
-
this.db.prepare(`UPDATE bridge_jobs SET status='unresolved', result_json=@result, updated_at=@ts WHERE id=@id`).run({ id: jobId, result, ts: this.now() });
|
|
194
|
+
this.db.prepare(`UPDATE bridge_jobs SET status='unresolved', result_json=@result, updated_at=@ts WHERE id=@id AND organization=@organization`).run({ id: jobId, organization: org, result, ts: this.now() });
|
|
120
195
|
}
|
|
121
|
-
markVoid(jobId) {
|
|
122
|
-
|
|
196
|
+
markVoid(organization, jobId) {
|
|
197
|
+
const org = assertOrganizationId(organization);
|
|
198
|
+
this.db.prepare(`UPDATE bridge_jobs SET status='void', updated_at=@ts WHERE id=@id AND organization=@organization`).run({ id: jobId, organization: org, ts: this.now() });
|
|
123
199
|
}
|
|
124
200
|
/**
|
|
125
201
|
* 孤儿回包落库(对账面):未知 jobId 的回包内容以 status='unresolved' 保留。
|
|
126
|
-
* - 无行(纯未知/探测回包):插 kind 取回包自报(缺省 'orphan'),site='unknown'
|
|
202
|
+
* - 无行(纯未知/探测回包):插 kind 取回包自报(缺省 'orphan'),site='unknown',归调用方组织;
|
|
127
203
|
* - 已 resolved:迟到回包不覆盖已决结果(幂等,首次裁决为准);
|
|
128
|
-
* - unresolved/void:补 result_json
|
|
204
|
+
* - unresolved/void:补 result_json——作废/超时后扩展其实跑完了,留证供对账;
|
|
205
|
+
* - 该 jobId 已属于**另一个组织**:拒绝(抛错,零写入)。队列在调用前就按组织拦下并答
|
|
206
|
+
* 与未知作业完全一样的 404,这里是第二道防线。
|
|
129
207
|
*/
|
|
130
|
-
recordOrphanResult(jobId, result) {
|
|
131
|
-
const
|
|
208
|
+
recordOrphanResult(organization, jobId, result) {
|
|
209
|
+
const org = assertOrganizationId(organization);
|
|
210
|
+
const existing = this.db.prepare(`SELECT organization, status FROM bridge_jobs WHERE id=@id`).get({ id: jobId });
|
|
132
211
|
const ts = this.now();
|
|
133
212
|
const resultJson = JSON.stringify(result);
|
|
134
213
|
if (!existing) {
|
|
135
|
-
this.db.prepare(`INSERT INTO bridge_jobs (id, kind, site, payload_json, status, result_json, attempts, created_at, updated_at)
|
|
136
|
-
VALUES (@id, @kind, 'unknown', '{}', 'unresolved', @result, 0, @ts, @ts)`).run({ id: jobId, kind: result.kind ?? 'orphan', result: resultJson, ts });
|
|
214
|
+
this.db.prepare(`INSERT INTO bridge_jobs (id, organization, kind, site, payload_json, status, result_json, attempts, created_at, updated_at)
|
|
215
|
+
VALUES (@id, @organization, @kind, 'unknown', '{}', 'unresolved', @result, 0, @ts, @ts)`).run({ id: jobId, organization: org, kind: result.kind ?? 'orphan', result: resultJson, ts });
|
|
137
216
|
return;
|
|
138
217
|
}
|
|
218
|
+
if (existing.organization !== org)
|
|
219
|
+
throw new Error('bridge-job-organization-mismatch');
|
|
139
220
|
if (existing.status === 'resolved')
|
|
140
221
|
return;
|
|
141
|
-
this.db.prepare(`UPDATE bridge_jobs SET status='unresolved', result_json=@result, updated_at=@ts WHERE id=@id`).run({ id: jobId, result: resultJson, ts });
|
|
222
|
+
this.db.prepare(`UPDATE bridge_jobs SET status='unresolved', result_json=@result, updated_at=@ts WHERE id=@id AND organization=@organization`).run({ id: jobId, organization: org, result: resultJson, ts });
|
|
142
223
|
}
|
|
143
224
|
/** bridge_clients upsert:/jobs 轮询体(clientId + capabilities)到达即刷新,login_sites 保序合并 */
|
|
144
|
-
upsertClient(clientId, origin, capabilities) {
|
|
225
|
+
upsertClient(organization, clientId, origin, capabilities) {
|
|
226
|
+
const org = assertOrganizationId(organization);
|
|
145
227
|
const ts = this.now();
|
|
146
|
-
this.db.prepare(`INSERT INTO bridge_clients (client_id, origin, capabilities_json, last_seen_at, login_sites_json)
|
|
147
|
-
VALUES (@clientId, @origin, @caps, @ts, '[]')
|
|
148
|
-
ON CONFLICT(client_id) DO UPDATE SET
|
|
149
|
-
origin=excluded.origin, capabilities_json=excluded.capabilities_json, last_seen_at=excluded.last_seen_at`).run({ clientId, origin, caps: JSON.stringify([...capabilities]), ts });
|
|
228
|
+
this.db.prepare(`INSERT INTO bridge_clients (organization, client_id, origin, capabilities_json, last_seen_at, login_sites_json)
|
|
229
|
+
VALUES (@organization, @clientId, @origin, @caps, @ts, '[]')
|
|
230
|
+
ON CONFLICT(organization, client_id) DO UPDATE SET
|
|
231
|
+
origin=excluded.origin, capabilities_json=excluded.capabilities_json, last_seen_at=excluded.last_seen_at`).run({ organization: org, clientId, origin, caps: JSON.stringify([...capabilities]), ts });
|
|
150
232
|
}
|
|
151
233
|
/** cookie-names 命中票据名(名字级)→ 该客户端在该站点持有登录态 */
|
|
152
|
-
recordClientLoginSite(clientId, site) {
|
|
153
|
-
const
|
|
234
|
+
recordClientLoginSite(organization, clientId, site) {
|
|
235
|
+
const org = assertOrganizationId(organization);
|
|
236
|
+
const row = this.db.prepare(`SELECT login_sites_json FROM bridge_clients WHERE organization=@organization AND client_id=@clientId`).get({ organization: org, clientId });
|
|
154
237
|
if (!row)
|
|
155
238
|
return;
|
|
156
239
|
let sites;
|
|
@@ -162,57 +245,75 @@ export class BridgeLedgerStore {
|
|
|
162
245
|
}
|
|
163
246
|
const merged = new Set(Array.isArray(sites) ? sites.filter((s) => typeof s === 'string') : []);
|
|
164
247
|
merged.add(site);
|
|
165
|
-
this.db.prepare(`UPDATE bridge_clients SET login_sites_json=@sites WHERE client_id=@clientId`).run({ clientId, sites: JSON.stringify([...merged]) });
|
|
248
|
+
this.db.prepare(`UPDATE bridge_clients SET login_sites_json=@sites WHERE organization=@organization AND client_id=@clientId`).run({ organization: org, clientId, sites: JSON.stringify([...merged]) });
|
|
166
249
|
}
|
|
167
|
-
/** 站点导航类作业提交/领取:记命中时刻并把冷却推到 now+30s(不回退既有冷却) */
|
|
168
|
-
noteSiteHit(site) {
|
|
250
|
+
/** 站点导航类作业提交/领取:记命中时刻并把冷却推到 now+30s(不回退既有冷却);节律按 组织+站点 */
|
|
251
|
+
noteSiteHit(organization, site) {
|
|
252
|
+
const org = assertOrganizationId(organization);
|
|
169
253
|
const ts = this.now();
|
|
170
|
-
this.db.prepare(`INSERT INTO bridge_pacing (site, last_hit_at, cooldown_until) VALUES (@site, @ts, @until)
|
|
171
|
-
ON CONFLICT(site) DO UPDATE SET last_hit_at=excluded.last_hit_at, cooldown_until=MAX(cooldown_until, excluded.cooldown_until)`).run({ site, ts, until: ts + BRIDGE_SITE_COOLDOWN_MS });
|
|
254
|
+
this.db.prepare(`INSERT INTO bridge_pacing (organization, site, last_hit_at, cooldown_until) VALUES (@organization, @site, @ts, @until)
|
|
255
|
+
ON CONFLICT(organization, site) DO UPDATE SET last_hit_at=excluded.last_hit_at, cooldown_until=MAX(cooldown_until, excluded.cooldown_until)`).run({ organization: org, site, ts, until: ts + BRIDGE_SITE_COOLDOWN_MS });
|
|
172
256
|
}
|
|
173
257
|
/** 超时结算冷却:claimed 超时的导航作业——站点可能已被打,只推冷却,不虚构 last_hit_at */
|
|
174
|
-
applyCooldown(site) {
|
|
258
|
+
applyCooldown(organization, site) {
|
|
259
|
+
const org = assertOrganizationId(organization);
|
|
175
260
|
const ts = this.now();
|
|
176
|
-
this.db.prepare(`INSERT INTO bridge_pacing (site, last_hit_at, cooldown_until) VALUES (@site, @ts, @until)
|
|
177
|
-
ON CONFLICT(site) DO UPDATE SET cooldown_until=MAX(cooldown_until, excluded.cooldown_until)`).run({ site, ts, until: ts + BRIDGE_SITE_COOLDOWN_MS });
|
|
261
|
+
this.db.prepare(`INSERT INTO bridge_pacing (organization, site, last_hit_at, cooldown_until) VALUES (@organization, @site, @ts, @until)
|
|
262
|
+
ON CONFLICT(organization, site) DO UPDATE SET cooldown_until=MAX(cooldown_until, excluded.cooldown_until)`).run({ organization: org, site, ts, until: ts + BRIDGE_SITE_COOLDOWN_MS });
|
|
178
263
|
}
|
|
179
|
-
pacingOf(site) {
|
|
180
|
-
const
|
|
264
|
+
pacingOf(organization, site) {
|
|
265
|
+
const org = assertOrganizationId(organization);
|
|
266
|
+
const row = this.db.prepare(`SELECT last_hit_at, cooldown_until FROM bridge_pacing WHERE organization=@organization AND site=@site`).get({ organization: org, site });
|
|
181
267
|
return row ? { lastHitAt: row.last_hit_at, cooldownUntil: row.cooldown_until } : null;
|
|
182
268
|
}
|
|
183
269
|
/**
|
|
184
270
|
* 事件上行落账(批次 B):seq 由 AUTOINCREMENT 分配,ts 取服务端落账时刻
|
|
185
271
|
* (clientTs 是扩展侧时钟,仅参与形状校验不入列——服务端时钟才是账本唯一时间轴)。
|
|
186
|
-
* 幂等:idem_key
|
|
187
|
-
* idem_key 为 NULL 的行不参与去重,多行共存
|
|
188
|
-
*
|
|
272
|
+
* 幂等:idem_key 非空且**同组织**已存在 → 不重复落行,返回 false(由调用方计入 rejected);
|
|
273
|
+
* 不同组织可以复用同一个 idem_key,互不去重。idem_key 为 NULL 的行不参与去重,多行共存
|
|
274
|
+
* (部分唯一索引)。同进程同步 API,check-then-insert 对本连接原子。
|
|
189
275
|
*/
|
|
190
|
-
insertEvent(ev) {
|
|
276
|
+
insertEvent(organization, ev) {
|
|
277
|
+
const org = assertOrganizationId(organization);
|
|
191
278
|
if (ev.idem_key !== null) {
|
|
192
|
-
const dup = this.db.prepare(`SELECT 1 FROM bridge_events WHERE idem_key
|
|
279
|
+
const dup = this.db.prepare(`SELECT 1 FROM bridge_events WHERE organization=@organization AND idem_key=@idem`).get({ organization: org, idem: ev.idem_key });
|
|
193
280
|
if (dup)
|
|
194
281
|
return false;
|
|
195
282
|
}
|
|
196
|
-
this.db.prepare(`INSERT INTO bridge_events (ts, client_id, kind, site, subject, payload_json, idem_key)
|
|
197
|
-
VALUES (@ts, @clientId, @kind, @site, @subject, @payload, @idem)`).run({ ts: this.now(), clientId: ev.client_id, kind: ev.kind, site: ev.site, subject: ev.subject, payload: ev.payload_json, idem: ev.idem_key });
|
|
283
|
+
this.db.prepare(`INSERT INTO bridge_events (organization, ts, client_id, kind, site, subject, payload_json, idem_key)
|
|
284
|
+
VALUES (@organization, @ts, @clientId, @kind, @site, @subject, @payload, @idem)`).run({ organization: org, ts: this.now(), clientId: ev.client_id, kind: ev.kind, site: ev.site, subject: ev.subject, payload: ev.payload_json, idem: ev.idem_key });
|
|
198
285
|
return true;
|
|
199
286
|
}
|
|
200
|
-
/** 事件流水读面(测试/对账)
|
|
201
|
-
listEvents() {
|
|
202
|
-
|
|
287
|
+
/** 事件流水读面(测试/对账):某组织的行,按 seq 升序 */
|
|
288
|
+
listEvents(organization) {
|
|
289
|
+
const org = assertOrganizationId(organization);
|
|
290
|
+
return this.db.prepare(`SELECT seq, organization, ts, client_id, kind, site, subject, payload_json, idem_key FROM bridge_events WHERE organization=@organization ORDER BY seq`).all({ organization: org });
|
|
203
291
|
}
|
|
292
|
+
/** 全部组织的未终态作业(开机恢复按行还原所属组织) */
|
|
204
293
|
listPendingJobs() {
|
|
205
|
-
return this.db.prepare(`SELECT id, kind, site, payload_json, status, claimed_by_client, claimed_origin, timeout_at, attempts
|
|
294
|
+
return this.db.prepare(`SELECT id, organization, kind, site, payload_json, status, claimed_by_client, claimed_origin, timeout_at, attempts
|
|
206
295
|
FROM bridge_jobs WHERE status IN ('queued','claimed')`).all();
|
|
207
296
|
}
|
|
297
|
+
/** 按 jobId 读行(任何组织;测试/对账口径,行里带 organization)。调用方有组织时用 getJobForOrganization */
|
|
208
298
|
getJob(jobId) {
|
|
209
299
|
return this.db.prepare(`SELECT * FROM bridge_jobs WHERE id=?`).get(jobId) ?? null;
|
|
210
300
|
}
|
|
211
|
-
|
|
212
|
-
|
|
301
|
+
getJobForOrganization(organization, jobId) {
|
|
302
|
+
const org = assertOrganizationId(organization);
|
|
303
|
+
return this.db.prepare(`SELECT * FROM bridge_jobs WHERE id=@id AND organization=@organization`).get({ id: jobId, organization: org }) ?? null;
|
|
304
|
+
}
|
|
305
|
+
/** 该 jobId 属于哪个组织(账本里没有这行 = null);队列用它认出「别的组织的作业」 */
|
|
306
|
+
organizationOfJob(jobId) {
|
|
307
|
+
const row = this.db.prepare(`SELECT organization FROM bridge_jobs WHERE id=?`).get(jobId);
|
|
308
|
+
return row ? row.organization : null;
|
|
309
|
+
}
|
|
310
|
+
getClient(organization, clientId) {
|
|
311
|
+
const org = assertOrganizationId(organization);
|
|
312
|
+
return this.db.prepare(`SELECT * FROM bridge_clients WHERE organization=@organization AND client_id=@clientId`).get({ organization: org, clientId }) ?? null;
|
|
213
313
|
}
|
|
214
|
-
listPacing() {
|
|
215
|
-
|
|
314
|
+
listPacing(organization) {
|
|
315
|
+
const org = assertOrganizationId(organization);
|
|
316
|
+
return this.db.prepare(`SELECT organization, site, last_hit_at, cooldown_until FROM bridge_pacing WHERE organization=@organization ORDER BY site`).all({ organization: org });
|
|
216
317
|
}
|
|
217
318
|
close() {
|
|
218
319
|
this.db.close();
|