@unson/brainbase-mcp 0.48.3 → 0.49.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/dist/entity-indexer.d.ts +291 -0
- package/dist/entity-indexer.js +288 -0
- package/dist/judgment-frame.d.ts +24 -0
- package/dist/judgment-frame.js +45 -3
- package/dist/judgment-history.d.ts +1 -1
- package/dist/judgment-history.js +1 -0
- package/dist/worker-judgment-emit-cli.d.ts +18 -0
- package/dist/worker-judgment-emit-cli.js +98 -0
- package/dist/worker-judgment-emitter.d.ts +136 -0
- package/dist/worker-judgment-emitter.js +457 -0
- package/package.json +6 -2
- package/ui/brainbase-tokens.css +10 -0
- package/ui/graph-registry-view.js +2 -2
- package/ui/inbox.css +1 -1
- package/ui/inbox.js +4 -4
- package/ui/local-web-shell.css +1 -1
- package/ui/people-and-companies-view.css +0 -11
- package/ui/project-workspace.js +3 -2
- package/ui/workspace-kit.css +26 -6
- package/ui/workspace-kit.js +119 -5
- package/ui/world/world-canvas-ui.css +2 -2
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { closeSync, existsSync, linkSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { checkJudgmentPublicFrame, judgmentFramePublicFrameDigest, containsJudgmentFrameCredential, publicFrameTextHasControlCharacters } from './judgment-frame.js';
|
|
5
|
+
import { canonicalJson } from './judgment-history.js';
|
|
6
|
+
/**
|
|
7
|
+
* RQ-119 Story S1: the common emission port that turns a Worker turn into the
|
|
8
|
+
* shared judgment receipt contract, derives its idempotency keys, and writes it
|
|
9
|
+
* to a local outbox before any delivery. Delivery is an injected transport; the
|
|
10
|
+
* OSS default is local-only (nothing is sent). Tool-specific adapters (S2/S3),
|
|
11
|
+
* the organization transport and receiving endpoint (S4), and retry/visibility
|
|
12
|
+
* (S5) are intentionally out of this module.
|
|
13
|
+
*/
|
|
14
|
+
export const WORKER_JUDGMENT_RECEIPT_SCHEMA = 'brainbase-worker-judgment-receipt-v1';
|
|
15
|
+
export const WORKER_JUDGMENT_OUTBOX_SCHEMA = 'brainbase-worker-judgment-outbox-v1';
|
|
16
|
+
export const WORKER_JUDGMENT_SKIP_SCHEMA = 'brainbase-worker-judgment-skip-v1';
|
|
17
|
+
/** The presence of a `<resolution_id>.json.sent` marker is the authoritative "delivered" signal; the outbox body is never rewritten on success. */
|
|
18
|
+
export const WORKER_JUDGMENT_SENT_SCHEMA = 'brainbase-worker-judgment-sent-v1';
|
|
19
|
+
/** Only Worker tools emit through this port. `opencode` is the shared enum value for DeepSeek via opencode. */
|
|
20
|
+
export const WORKER_JUDGMENT_ENTRYPOINTS = Object.freeze(['claude_code', 'opencode']);
|
|
21
|
+
export class WorkerJudgmentEmitterError extends Error {
|
|
22
|
+
code;
|
|
23
|
+
constructor(code, message) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.name = 'WorkerJudgmentEmitterError';
|
|
26
|
+
this.code = code;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Raised when the same resolution id is already stored with a different receipt (§6 conflict). We never overwrite. */
|
|
30
|
+
export class WorkerJudgmentConflictError extends WorkerJudgmentEmitterError {
|
|
31
|
+
resolution_id;
|
|
32
|
+
constructor(resolutionId) {
|
|
33
|
+
super('worker_judgment_receipt_conflict', `A different receipt is already stored for resolution ${resolutionId}`);
|
|
34
|
+
this.name = 'WorkerJudgmentConflictError';
|
|
35
|
+
this.resolution_id = resolutionId;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const MAX_META_TEXT = 200;
|
|
39
|
+
const DEFAULT_MAX_FUTURE_SKEW_MS = 24 * 60 * 60 * 1000;
|
|
40
|
+
function sha256Hex(value) {
|
|
41
|
+
return createHash('sha256').update(value, 'utf8').digest('hex');
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Accept only an explicit calendar date-time that carries a timezone (`Z` or a
|
|
45
|
+
* numeric offset). A zone-less value like `2026-10-01T00:00:00` would otherwise
|
|
46
|
+
* be read in the host timezone, so the §8 boundary and the receipt_digest would
|
|
47
|
+
* differ between a UTC runner and an Asia/Tokyo laptop. Calendar fields are
|
|
48
|
+
* checked against a UTC round-trip so a rolled-over date (`2026-02-30`,
|
|
49
|
+
* hour `25`) is rejected rather than silently carried forward. The accepted
|
|
50
|
+
* value is returned normalized to a UTC ISO string; it is never rewritten
|
|
51
|
+
* beyond that canonicalization.
|
|
52
|
+
*/
|
|
53
|
+
const ISO_DATETIME_WITH_TZ = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
54
|
+
function normalizeIso(value) {
|
|
55
|
+
if (typeof value !== 'string')
|
|
56
|
+
return null;
|
|
57
|
+
const trimmed = value.trim();
|
|
58
|
+
const match = ISO_DATETIME_WITH_TZ.exec(trimmed);
|
|
59
|
+
if (!match)
|
|
60
|
+
return null;
|
|
61
|
+
const [, y, mo, d, h, mi, s] = match;
|
|
62
|
+
const year = Number(y);
|
|
63
|
+
const month = Number(mo);
|
|
64
|
+
const day = Number(d);
|
|
65
|
+
const hour = Number(h);
|
|
66
|
+
const minute = Number(mi);
|
|
67
|
+
const second = Number(s);
|
|
68
|
+
const check = new Date(Date.UTC(year, month - 1, day, hour, minute, second));
|
|
69
|
+
if (check.getUTCFullYear() !== year || check.getUTCMonth() !== month - 1 || check.getUTCDate() !== day
|
|
70
|
+
|| check.getUTCHours() !== hour || check.getUTCMinutes() !== minute || check.getUTCSeconds() !== second) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
const ms = Date.parse(trimmed);
|
|
74
|
+
if (!Number.isFinite(ms))
|
|
75
|
+
return null;
|
|
76
|
+
return new Date(ms).toISOString();
|
|
77
|
+
}
|
|
78
|
+
function requireId(value, field) {
|
|
79
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
80
|
+
throw new WorkerJudgmentEmitterError('worker_judgment_input_invalid', `${field} is required`);
|
|
81
|
+
}
|
|
82
|
+
return value;
|
|
83
|
+
}
|
|
84
|
+
/** `null` = not provided; a string = accepted; `undefined` = provided but unusable (rejected). */
|
|
85
|
+
function normalizeMetaField(value) {
|
|
86
|
+
if (value === undefined || value === null)
|
|
87
|
+
return null;
|
|
88
|
+
if (typeof value !== 'string' || !value.trim() || value.length > MAX_META_TEXT
|
|
89
|
+
|| publicFrameTextHasControlCharacters(value) || containsJudgmentFrameCredential(value)) {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* `project_code` is stored in the receipt and handed to any injected transport,
|
|
96
|
+
* so it gets the same secret/control-character/length screen as meta. An absent,
|
|
97
|
+
* null, or blank string is "not recorded" (`null`); a value of the wrong type
|
|
98
|
+
* (e.g. a number smuggled in from a CLI JSON payload), or a non-empty string
|
|
99
|
+
* that fails the screen, is rejected (`undefined`) instead of being silently
|
|
100
|
+
* dropped to `null` and losing the project link.
|
|
101
|
+
*/
|
|
102
|
+
function normalizeProjectCode(value) {
|
|
103
|
+
if (value === undefined || value === null)
|
|
104
|
+
return null;
|
|
105
|
+
if (typeof value !== 'string')
|
|
106
|
+
return undefined;
|
|
107
|
+
if (!value.trim())
|
|
108
|
+
return null;
|
|
109
|
+
if (value.length > MAX_META_TEXT || publicFrameTextHasControlCharacters(value) || containsJudgmentFrameCredential(value)) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Build the deterministic receipt from a turn, or decide not to send it.
|
|
116
|
+
* Pure: the same input and `effectiveAfter` always yield the same receipt and
|
|
117
|
+
* the same `resolution_id`. `now` only bounds how far into the future a
|
|
118
|
+
* `resolved_at` may sit; it never enters the receipt.
|
|
119
|
+
*/
|
|
120
|
+
export function buildWorkerJudgmentReceipt(input, config) {
|
|
121
|
+
if (!WORKER_JUDGMENT_ENTRYPOINTS.includes(input.entrypoint)) {
|
|
122
|
+
throw new WorkerJudgmentEmitterError('worker_judgment_entrypoint_invalid', `entrypoint must be one of ${WORKER_JUDGMENT_ENTRYPOINTS.join(', ')}`);
|
|
123
|
+
}
|
|
124
|
+
const sessionId = requireId(input.session_id, 'session_id');
|
|
125
|
+
const turnId = requireId(input.turn_id, 'turn_id');
|
|
126
|
+
const boundary = normalizeIso(config.effectiveAfter);
|
|
127
|
+
if (!boundary) {
|
|
128
|
+
throw new WorkerJudgmentEmitterError('worker_judgment_effective_after_invalid', 'effectiveAfter must be an ISO time');
|
|
129
|
+
}
|
|
130
|
+
const frameCheck = checkJudgmentPublicFrame(input.public_frame);
|
|
131
|
+
if (!frameCheck.ok) {
|
|
132
|
+
const reason_code = frameCheck.code === 'frame_public_absent'
|
|
133
|
+
? 'worker_public_frame_absent'
|
|
134
|
+
: 'frame_public_invalid';
|
|
135
|
+
return { outcome: 'skipped', reason_code, detail: frameCheck.detail };
|
|
136
|
+
}
|
|
137
|
+
const model = normalizeMetaField(input.meta?.model);
|
|
138
|
+
const agentMemberId = normalizeMetaField(input.meta?.agent_member_id);
|
|
139
|
+
if (model === undefined || agentMemberId === undefined) {
|
|
140
|
+
return { outcome: 'skipped', reason_code: 'worker_meta_invalid', detail: 'meta contains an invalid or credential-like value' };
|
|
141
|
+
}
|
|
142
|
+
const projectCode = normalizeProjectCode(input.project_code);
|
|
143
|
+
if (projectCode === undefined) {
|
|
144
|
+
return { outcome: 'skipped', reason_code: 'worker_project_code_invalid', detail: 'project_code is invalid or credential-like' };
|
|
145
|
+
}
|
|
146
|
+
if (input.resolved_at === undefined || input.resolved_at === null
|
|
147
|
+
|| (typeof input.resolved_at === 'string' && !input.resolved_at.trim())) {
|
|
148
|
+
return { outcome: 'skipped', reason_code: 'worker_resolved_at_missing', detail: 'resolved_at was not provided' };
|
|
149
|
+
}
|
|
150
|
+
const resolvedAt = normalizeIso(input.resolved_at);
|
|
151
|
+
if (!resolvedAt) {
|
|
152
|
+
return { outcome: 'skipped', reason_code: 'worker_resolved_at_invalid', detail: 'resolved_at is not a valid ISO time' };
|
|
153
|
+
}
|
|
154
|
+
const now = (config.now ?? (() => new Date()))();
|
|
155
|
+
const skew = config.maxFutureSkewMs ?? DEFAULT_MAX_FUTURE_SKEW_MS;
|
|
156
|
+
if (Date.parse(resolvedAt) > now.getTime() + skew) {
|
|
157
|
+
return { outcome: 'skipped', reason_code: 'worker_resolved_at_invalid', detail: 'resolved_at is too far in the future' };
|
|
158
|
+
}
|
|
159
|
+
if (Date.parse(resolvedAt) < Date.parse(boundary)) {
|
|
160
|
+
return { outcome: 'skipped', reason_code: 'worker_resolved_at_before_boundary', detail: 'resolved_at is before the activation boundary' };
|
|
161
|
+
}
|
|
162
|
+
const contentDigest = judgmentFramePublicFrameDigest(frameCheck.frame);
|
|
163
|
+
const resolutionId = sha256Hex(canonicalJson({
|
|
164
|
+
content_digest: contentDigest,
|
|
165
|
+
entrypoint: input.entrypoint,
|
|
166
|
+
session_id: sessionId,
|
|
167
|
+
turn_id: turnId
|
|
168
|
+
}));
|
|
169
|
+
const core = {
|
|
170
|
+
schema_version: WORKER_JUDGMENT_RECEIPT_SCHEMA,
|
|
171
|
+
resolution_id: resolutionId,
|
|
172
|
+
entrypoint: input.entrypoint,
|
|
173
|
+
turn_ref: `${sha256Hex(sessionId)}/${sha256Hex(turnId)}`,
|
|
174
|
+
project_code: projectCode,
|
|
175
|
+
resolved_at: resolvedAt,
|
|
176
|
+
public_frame: frameCheck.frame,
|
|
177
|
+
content_digest: contentDigest,
|
|
178
|
+
meta: { model, agent_member_id: agentMemberId }
|
|
179
|
+
};
|
|
180
|
+
const receiptDigest = sha256Hex(canonicalJson(core));
|
|
181
|
+
return { outcome: 'receipt', receipt: { ...core, receipt_digest: receiptDigest } };
|
|
182
|
+
}
|
|
183
|
+
/** Resolve the per-turn outbox directory. Session and turn are hashed so neither raw id reaches the path. */
|
|
184
|
+
export function resolveWorkerJudgmentTurnDir(journalRoot, sessionId, turnId) {
|
|
185
|
+
return join(journalRoot, sha256Hex(requireId(sessionId, 'session_id')), sha256Hex(requireId(turnId, 'turn_id')));
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* A per-process counter so two writes that share a pid and an injected (fixed)
|
|
189
|
+
* clock still get distinct temp names. Without it, two concurrent writers under a
|
|
190
|
+
* test clock would collide on the temp path.
|
|
191
|
+
*/
|
|
192
|
+
let outboxTempSequence = 0;
|
|
193
|
+
function nextTempPath(target, now) {
|
|
194
|
+
outboxTempSequence += 1;
|
|
195
|
+
return `${target}.${process.pid}.${now().getTime()}.${outboxTempSequence}.tmp`;
|
|
196
|
+
}
|
|
197
|
+
function atomicWriteReplace(target, value, now) {
|
|
198
|
+
const temporary = nextTempPath(target, now);
|
|
199
|
+
writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
|
200
|
+
renameSync(temporary, target);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Publish a receipt without replacing one another writer created. The temp file
|
|
204
|
+
* is complete before the hard link makes it visible, so a reader sees either the
|
|
205
|
+
* old file or the complete new file — the same discipline as the judgment event
|
|
206
|
+
* outbox.
|
|
207
|
+
*/
|
|
208
|
+
function atomicPublishNoReplace(target, value, now) {
|
|
209
|
+
const temporary = nextTempPath(target, now);
|
|
210
|
+
writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
|
|
211
|
+
try {
|
|
212
|
+
linkSync(temporary, target);
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
try {
|
|
216
|
+
unlinkSync(temporary);
|
|
217
|
+
}
|
|
218
|
+
catch { /* publication may have failed */ }
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function isErrno(error, code) {
|
|
222
|
+
return Boolean(error && typeof error === 'object' && 'code' in error && error.code === code);
|
|
223
|
+
}
|
|
224
|
+
const DELIVERY_STATUSES = ['pending', 'saved', 'already_saved', 'blocked_credential', 'failed'];
|
|
225
|
+
/** A delivery state a later re-emit must never silently undo. */
|
|
226
|
+
function isSuccessStatus(status) {
|
|
227
|
+
return status === 'saved' || status === 'already_saved';
|
|
228
|
+
}
|
|
229
|
+
/** Raised when a stored outbox file exists but is corrupt. We never re-send or overwrite over corruption (§ fail loudly). */
|
|
230
|
+
export class WorkerJudgmentOutboxCorruptError extends WorkerJudgmentEmitterError {
|
|
231
|
+
constructor(path, detail) {
|
|
232
|
+
super('worker_judgment_outbox_corrupt', `Stored outbox ${path} ${detail}`);
|
|
233
|
+
this.name = 'WorkerJudgmentOutboxCorruptError';
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/** Recompute the receipt_digest from the receipt body so a tampered or truncated body cannot ride on a stale digest string. */
|
|
237
|
+
function recomputeReceiptDigest(receipt) {
|
|
238
|
+
const core = { ...receipt };
|
|
239
|
+
delete core.receipt_digest;
|
|
240
|
+
return sha256Hex(canonicalJson(core));
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Read the digest and the recorded delivery state of a receipt already on disk.
|
|
244
|
+
* A re-emit must compare the digest (§6 conflict) and must not undo delivery
|
|
245
|
+
* progress, so the stored status is read too. The stored digest string is not
|
|
246
|
+
* trusted on its own: the receipt_digest is recomputed from the receipt body,
|
|
247
|
+
* the outbox schema and the delivery status are verified, and any mismatch is a
|
|
248
|
+
* loud `WorkerJudgmentOutboxCorruptError` rather than a silent re-send or
|
|
249
|
+
* overwrite. Returns `null` only when the file is genuinely absent.
|
|
250
|
+
*/
|
|
251
|
+
function readStoredOutboxState(path) {
|
|
252
|
+
let raw;
|
|
253
|
+
try {
|
|
254
|
+
raw = readFileSync(path, 'utf8');
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
if (isErrno(error, 'ENOENT'))
|
|
258
|
+
return null;
|
|
259
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'could not be read');
|
|
260
|
+
}
|
|
261
|
+
let parsed;
|
|
262
|
+
try {
|
|
263
|
+
parsed = JSON.parse(raw);
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'is not valid JSON');
|
|
267
|
+
}
|
|
268
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
269
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'is not an object');
|
|
270
|
+
}
|
|
271
|
+
if (parsed.schema_version !== WORKER_JUDGMENT_OUTBOX_SCHEMA) {
|
|
272
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'has an unexpected schema_version');
|
|
273
|
+
}
|
|
274
|
+
const receipt = parsed.receipt;
|
|
275
|
+
if (!receipt || typeof receipt !== 'object' || Array.isArray(receipt)) {
|
|
276
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'is missing its receipt');
|
|
277
|
+
}
|
|
278
|
+
const digest = receipt.receipt_digest;
|
|
279
|
+
if (typeof digest !== 'string' || !digest) {
|
|
280
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'is missing its receipt_digest');
|
|
281
|
+
}
|
|
282
|
+
let recomputed;
|
|
283
|
+
try {
|
|
284
|
+
recomputed = recomputeReceiptDigest(receipt);
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'has a receipt body that cannot be canonicalized');
|
|
288
|
+
}
|
|
289
|
+
if (recomputed !== digest) {
|
|
290
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'has a receipt_digest that does not match its body');
|
|
291
|
+
}
|
|
292
|
+
const delivery = parsed.delivery;
|
|
293
|
+
if (!delivery || typeof delivery !== 'object' || Array.isArray(delivery)) {
|
|
294
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'is missing its delivery state');
|
|
295
|
+
}
|
|
296
|
+
const rawStatus = delivery.status;
|
|
297
|
+
if (!DELIVERY_STATUSES.includes(rawStatus)) {
|
|
298
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'has an invalid delivery status');
|
|
299
|
+
}
|
|
300
|
+
const status = rawStatus;
|
|
301
|
+
const rawError = delivery.last_error_code;
|
|
302
|
+
if (rawError !== undefined && typeof rawError !== 'string') {
|
|
303
|
+
throw new WorkerJudgmentOutboxCorruptError(path, 'has an invalid last_error_code');
|
|
304
|
+
}
|
|
305
|
+
return { receipt_digest: digest, status, ...(typeof rawError === 'string' ? { last_error_code: rawError } : {}) };
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Record a successful delivery as a separate marker file created exclusively
|
|
309
|
+
* (`wx`). The outbox body is never rewritten on success, so a slow concurrent
|
|
310
|
+
* failure cannot clobber a recorded success: the marker's presence — not a body
|
|
311
|
+
* status — is the authoritative "delivered" signal, and a marker, once created,
|
|
312
|
+
* is never removed. Creating a marker that already exists is treated as success:
|
|
313
|
+
* another writer, or an earlier crash, already delivered it. The marker carries
|
|
314
|
+
* no judgment body, only a schema tag and the time it was recorded, and its
|
|
315
|
+
* content is never read: presence alone means saved, so a crash between creating
|
|
316
|
+
* and writing the marker still reads as delivered.
|
|
317
|
+
*/
|
|
318
|
+
function recordDeliverySuccess(markerPath, now) {
|
|
319
|
+
let fd;
|
|
320
|
+
try {
|
|
321
|
+
fd = openSync(markerPath, 'wx', 0o600);
|
|
322
|
+
}
|
|
323
|
+
catch (error) {
|
|
324
|
+
if (isErrno(error, 'EEXIST'))
|
|
325
|
+
return;
|
|
326
|
+
throw error;
|
|
327
|
+
}
|
|
328
|
+
try {
|
|
329
|
+
writeFileSync(fd, `${JSON.stringify({ schema_version: WORKER_JUDGMENT_SENT_SCHEMA, recorded_at: now().toISOString() })}\n`);
|
|
330
|
+
}
|
|
331
|
+
finally {
|
|
332
|
+
closeSync(fd);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
/** True when a success marker is present. Presence is the only signal; the content is never read. */
|
|
336
|
+
function deliverySucceeded(markerPath) {
|
|
337
|
+
return existsSync(markerPath);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Turn a Worker turn into a receipt and write it to the local outbox, then —
|
|
341
|
+
* only if a transport is injected — attempt a single delivery and record its
|
|
342
|
+
* status. With no transport the receipt stays `pending` locally. Re-emitting
|
|
343
|
+
* the identical turn is idempotent; a different receipt under the same
|
|
344
|
+
* resolution id is a loud conflict. Nothing here writes the raw session/turn
|
|
345
|
+
* ids, the prompt, the response, or any credential.
|
|
346
|
+
*/
|
|
347
|
+
export async function emitWorkerJudgment(input, options) {
|
|
348
|
+
if (!options.journalRoot)
|
|
349
|
+
throw new WorkerJudgmentEmitterError('worker_judgment_journal_root_required', 'journalRoot is required');
|
|
350
|
+
const now = options.now ?? (() => new Date());
|
|
351
|
+
const built = buildWorkerJudgmentReceipt(input, {
|
|
352
|
+
effectiveAfter: options.effectiveAfter,
|
|
353
|
+
now,
|
|
354
|
+
maxFutureSkewMs: options.maxFutureSkewMs
|
|
355
|
+
});
|
|
356
|
+
const dir = resolveWorkerJudgmentTurnDir(options.journalRoot, input.session_id, input.turn_id);
|
|
357
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
358
|
+
if (built.outcome === 'skipped') {
|
|
359
|
+
const target = join(dir, 'skip.json');
|
|
360
|
+
atomicWriteReplace(target, {
|
|
361
|
+
schema_version: WORKER_JUDGMENT_SKIP_SCHEMA,
|
|
362
|
+
entrypoint: input.entrypoint,
|
|
363
|
+
turn_ref: `${sha256Hex(input.session_id)}/${sha256Hex(input.turn_id)}`,
|
|
364
|
+
reason_code: built.reason_code,
|
|
365
|
+
recorded_at: now().toISOString()
|
|
366
|
+
}, now);
|
|
367
|
+
return { outcome: 'skipped', reason_code: built.reason_code, path: target };
|
|
368
|
+
}
|
|
369
|
+
const receipt = built.receipt;
|
|
370
|
+
const target = join(dir, `${receipt.resolution_id}.json`);
|
|
371
|
+
const markerPath = `${target}.sent`;
|
|
372
|
+
let deduped = false;
|
|
373
|
+
let stored = null;
|
|
374
|
+
try {
|
|
375
|
+
atomicPublishNoReplace(target, {
|
|
376
|
+
schema_version: WORKER_JUDGMENT_OUTBOX_SCHEMA,
|
|
377
|
+
receipt,
|
|
378
|
+
delivery: { status: 'pending', attempt: 1, queued_at: now().toISOString() }
|
|
379
|
+
}, now);
|
|
380
|
+
}
|
|
381
|
+
catch (error) {
|
|
382
|
+
if (!isErrno(error, 'EEXIST'))
|
|
383
|
+
throw error;
|
|
384
|
+
stored = readStoredOutboxState(target);
|
|
385
|
+
if (!stored || stored.receipt_digest !== receipt.receipt_digest)
|
|
386
|
+
throw new WorkerJudgmentConflictError(receipt.resolution_id);
|
|
387
|
+
deduped = true;
|
|
388
|
+
}
|
|
389
|
+
// A delivered receipt is marked by a `.sent` file, never by the outbox body:
|
|
390
|
+
// if the marker is present the receipt was already saved and must not be
|
|
391
|
+
// re-sent or downgraded, whatever the body or a later transport says.
|
|
392
|
+
if (deliverySucceeded(markerPath)) {
|
|
393
|
+
return { outcome: 'emitted', status: 'saved', receipt, path: target, deduped };
|
|
394
|
+
}
|
|
395
|
+
// Legacy success: an outbox written before the `.sent` marker existed recorded
|
|
396
|
+
// delivery only in its body. A marker-less `saved` / `already_saved` body is
|
|
397
|
+
// still an authoritative success, so honor it exactly like a marker — never
|
|
398
|
+
// re-send through the transport, and never let a later failure downgrade it.
|
|
399
|
+
if (stored && isSuccessStatus(stored.status)) {
|
|
400
|
+
return {
|
|
401
|
+
outcome: 'emitted',
|
|
402
|
+
status: stored.status,
|
|
403
|
+
receipt,
|
|
404
|
+
path: target,
|
|
405
|
+
deduped,
|
|
406
|
+
...(stored.last_error_code ? { last_error_code: stored.last_error_code } : {})
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
if (!options.transport) {
|
|
410
|
+
// No transport: the receipt keeps whatever the body already recorded
|
|
411
|
+
// (`pending` on a first write, or a prior `failed`), never a fresh success.
|
|
412
|
+
const status = stored ? stored.status : 'pending';
|
|
413
|
+
return {
|
|
414
|
+
outcome: 'emitted',
|
|
415
|
+
status,
|
|
416
|
+
receipt,
|
|
417
|
+
path: target,
|
|
418
|
+
deduped,
|
|
419
|
+
...(stored?.last_error_code ? { last_error_code: stored.last_error_code } : {})
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
let status = 'pending';
|
|
423
|
+
let lastErrorCode;
|
|
424
|
+
try {
|
|
425
|
+
const result = await options.transport.send(receipt);
|
|
426
|
+
status = result.status;
|
|
427
|
+
if (result.status === 'blocked_credential' || result.status === 'failed')
|
|
428
|
+
lastErrorCode = result.reason_code;
|
|
429
|
+
}
|
|
430
|
+
catch (error) {
|
|
431
|
+
status = 'failed';
|
|
432
|
+
lastErrorCode = error instanceof WorkerJudgmentEmitterError ? error.code : 'worker_judgment_transport_error';
|
|
433
|
+
}
|
|
434
|
+
if (isSuccessStatus(status)) {
|
|
435
|
+
// Success is recorded only by exclusively creating the marker; the body is
|
|
436
|
+
// left as published. An already-present marker means a concurrent writer (or
|
|
437
|
+
// an earlier crash mid-delivery) already recorded success, which is fine.
|
|
438
|
+
recordDeliverySuccess(markerPath, now);
|
|
439
|
+
return { outcome: 'emitted', status, receipt, path: target, deduped };
|
|
440
|
+
}
|
|
441
|
+
// Delivery failed. If a concurrent re-emit already recorded success, keep that
|
|
442
|
+
// success and neither record the failure in the body nor touch the marker.
|
|
443
|
+
if (deliverySucceeded(markerPath)) {
|
|
444
|
+
return { outcome: 'emitted', status: 'saved', receipt, path: target, deduped };
|
|
445
|
+
}
|
|
446
|
+
atomicWriteReplace(target, {
|
|
447
|
+
schema_version: WORKER_JUDGMENT_OUTBOX_SCHEMA,
|
|
448
|
+
receipt,
|
|
449
|
+
delivery: {
|
|
450
|
+
status,
|
|
451
|
+
attempt: 1,
|
|
452
|
+
queued_at: now().toISOString(),
|
|
453
|
+
...(lastErrorCode ? { last_error_code: lastErrorCode } : {})
|
|
454
|
+
}
|
|
455
|
+
}, now);
|
|
456
|
+
return { outcome: 'emitted', status, receipt, path: target, deduped, ...(lastErrorCode ? { last_error_code: lastErrorCode } : {}) };
|
|
457
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unson/brainbase-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.49.0",
|
|
4
4
|
"description": "Local-first Brainbase judgment OS that lets supported AI agents reuse a user's approved goals, priorities, decisions, and judgment criteria.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,6 +15,10 @@
|
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"import": "./dist/index.js"
|
|
17
17
|
},
|
|
18
|
+
"./entity-indexer": {
|
|
19
|
+
"types": "./dist/entity-indexer.d.ts",
|
|
20
|
+
"import": "./dist/entity-indexer.js"
|
|
21
|
+
},
|
|
18
22
|
"./entity-index-cache": {
|
|
19
23
|
"types": "./dist/entity-index-cache.d.ts",
|
|
20
24
|
"import": "./dist/entity-index-cache.js"
|
|
@@ -624,5 +628,5 @@
|
|
|
624
628
|
"url": "https://github.com/Unson-LLC/brainbase/issues"
|
|
625
629
|
},
|
|
626
630
|
"homepage": "https://github.com/Unson-LLC/brainbase#readme",
|
|
627
|
-
"gitHead": "
|
|
631
|
+
"gitHead": "d1817b3c50345b7d482e71f9e00c736e38f3756e"
|
|
628
632
|
}
|
package/ui/brainbase-tokens.css
CHANGED
|
@@ -11,6 +11,14 @@
|
|
|
11
11
|
* Status colors retain their semantic meaning; hosts can still override tokens.
|
|
12
12
|
*/
|
|
13
13
|
:root {
|
|
14
|
+
/* Shared motion curves and dismissal durations. */
|
|
15
|
+
--bb-ease-out: cubic-bezier(0.23, 1, 0.32, 1);
|
|
16
|
+
--bb-ease-in-out: cubic-bezier(0.77, 0, 0.175, 1); /* On-screen movement/position swaps only, not enter/exit. */
|
|
17
|
+
--bb-ease-drawer: cubic-bezier(0.32, 0.72, 0, 1);
|
|
18
|
+
--bb-ease-drawer-in: ease-out;
|
|
19
|
+
--bb-duration-drawer-in: 180ms;
|
|
20
|
+
--bb-duration-drawer-out: 150ms;
|
|
21
|
+
--bb-duration-toast-out: 200ms;
|
|
14
22
|
--bb-color-ink: #202939;
|
|
15
23
|
--bb-color-muted: #6b7280;
|
|
16
24
|
/* Small secondary text on tinted surfaces needs a slightly darker mix. */
|
|
@@ -111,6 +119,8 @@
|
|
|
111
119
|
--bb-tab-line-height: 20px;
|
|
112
120
|
--bb-tab-font-weight: 500;
|
|
113
121
|
--bb-tab-indicator-size: 2px;
|
|
122
|
+
--bb-tab-indicator-duration: 260ms;
|
|
123
|
+
--bb-tab-indicator-ease: cubic-bezier(.2, .8, .2, 1);
|
|
114
124
|
--bb-tab-count-size: 20px;
|
|
115
125
|
--bb-tab-count-font-size: 12px;
|
|
116
126
|
/* Opt-in search band: dimensions and interaction surfaces. */
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { workspaceTabStrip } from './workspace-kit.js';
|
|
2
2
|
/*
|
|
3
3
|
* 「情報と関係」: what the local Graph holds, how records are connected, and
|
|
4
4
|
* where to correct a mistake so the next MCP use sees the fix.
|
|
@@ -469,7 +469,7 @@ export function createGraphRegistryView({
|
|
|
469
469
|
.map(({ id }) => id)
|
|
470
470
|
.filter((id) => id === selected || counts.get(id)?.count !== 0);
|
|
471
471
|
const tabs = makeElement(doc, 'div', { className: 'bb-gr-tabs', attrs: { role: 'tablist', 'aria-label': '種類' } });
|
|
472
|
-
|
|
472
|
+
workspaceTabStrip(tabs, { memoryKey: 'graph-registry-kinds' });
|
|
473
473
|
for (const id of ['', ...ids]) {
|
|
474
474
|
const item = counts.get(id);
|
|
475
475
|
const count = Number.isInteger(item?.count) ? String(item.count)
|
package/ui/inbox.css
CHANGED
|
@@ -154,7 +154,7 @@ a.bb-inbox-title:focus-visible {
|
|
|
154
154
|
border-radius: 999px;
|
|
155
155
|
padding: 0 8px;
|
|
156
156
|
background: var(--bb-color-danger-soft);
|
|
157
|
-
color: var(--bb-color-
|
|
157
|
+
color: var(--bb-color-inline-error);
|
|
158
158
|
font-weight: var(--bb-font-weight-medium);
|
|
159
159
|
}
|
|
160
160
|
|
package/ui/inbox.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { workspaceTabStrip } from './workspace-kit.js';
|
|
2
2
|
/*
|
|
3
3
|
* 受信箱 (story-inbox-stage1-v1, docs/specs/inbox-stage1.md).
|
|
4
4
|
*
|
|
@@ -647,7 +647,7 @@ export function createInboxUI({
|
|
|
647
647
|
|
|
648
648
|
function renderTabs(waitingCount) {
|
|
649
649
|
const bar = el(doc, 'div', { className: 'bb-inbox-tabs', attrs: { role: 'tablist', 'aria-label': '受信箱' } });
|
|
650
|
-
|
|
650
|
+
workspaceTabStrip(bar, { memoryKey: 'inbox-tabs' });
|
|
651
651
|
for (const [id, label] of [['waiting', '対応待ち'], ['notices', 'お知らせ']]) {
|
|
652
652
|
const selected = state.route.tab === id;
|
|
653
653
|
const tab = el(doc, 'button', {
|
|
@@ -898,7 +898,7 @@ export function createInboxUI({
|
|
|
898
898
|
if (!notices || notices.phase === 'not_collected') {
|
|
899
899
|
view.append(el(doc, 'p', { className: 'bb-inbox-uncollected', text: 'お知らせはまだ集めていません。AIに任せた仕事の終わり・議事録ができたこと・申請の結果は、これから集めます。' }));
|
|
900
900
|
} else if (notices.phase === 'unavailable') {
|
|
901
|
-
view.append(workspaceErrorCard(doc, { title: 'お知らせを読めませんでした', message: `理由: ${reasonText(notices.reason)}。0
|
|
901
|
+
view.append(workspaceErrorCard(doc, { title: 'お知らせを読めませんでした', message: `理由: ${reasonText(notices.reason)}。0件ではありません。`, open: true }));
|
|
902
902
|
} else if (notices.items.length === 0) {
|
|
903
903
|
view.append(el(doc, 'p', { className: 'bb-inbox-empty', text: 'お知らせはありません。' }));
|
|
904
904
|
}
|
|
@@ -947,7 +947,7 @@ export function createInboxUI({
|
|
|
947
947
|
if (!isToday && state.route.tab === 'notices') {
|
|
948
948
|
view.append(renderNotices());
|
|
949
949
|
} else if (state.phase === 'error' && !shown) {
|
|
950
|
-
view.append(workspaceErrorCard(doc, { title: '受信箱を読めませんでした', message: `理由: ${reasonText(state.reason)}。0件ではありません。`, onRetry: () => void load() }));
|
|
950
|
+
view.append(workspaceErrorCard(doc, { title: '受信箱を読めませんでした', message: `理由: ${reasonText(state.reason)}。0件ではありません。`, onRetry: () => void load(), open: true }));
|
|
951
951
|
} else if (shown) {
|
|
952
952
|
if (isToday) view.append(state.route.view === 'waiting' ? renderTodayWaiting(shown) : renderSet(shown, state.route.view));
|
|
953
953
|
else view.append(state.route.view === 'waiting' ? renderWaiting(shown) : renderSet(shown, state.route.view));
|
package/ui/local-web-shell.css
CHANGED
|
@@ -272,17 +272,6 @@
|
|
|
272
272
|
gap: 8px;
|
|
273
273
|
}
|
|
274
274
|
|
|
275
|
-
.bb-pc-action-row .bb-ws-button.is-primary {
|
|
276
|
-
border-color: var(--bb-color-action);
|
|
277
|
-
background: var(--bb-color-action);
|
|
278
|
-
color: var(--bb-color-action-text);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
.bb-pc-action-row .bb-ws-button.is-primary:hover:not(:disabled) {
|
|
282
|
-
border-color: var(--bb-color-action-hover);
|
|
283
|
-
background: var(--bb-color-action-hover);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
275
|
.bb-pc-action-row .bb-ws-button[data-adds]::before {
|
|
287
276
|
content: '+';
|
|
288
277
|
}
|
package/ui/project-workspace.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { workspaceTabStrip } from './workspace-kit.js';
|
|
2
2
|
/*
|
|
3
3
|
* Shared project knowledge workspace.
|
|
4
4
|
*
|
|
@@ -1254,7 +1254,7 @@ export function createProjectKnowledgeWorkspace({
|
|
|
1254
1254
|
});
|
|
1255
1255
|
const body = makeElement(doc, 'div', { className: 'bb-pkw-body' });
|
|
1256
1256
|
const tabs = makeElement(doc, 'div', { className: 'bb-pkw-tabs', attrs: { role: 'tablist', 'aria-label': 'プロジェクト情報の表示' } });
|
|
1257
|
-
|
|
1257
|
+
const tabStrip = workspaceTabStrip(tabs);
|
|
1258
1258
|
const panels = makeElement(doc, 'div', { className: 'bb-pkw-panels' });
|
|
1259
1259
|
root.append(makeElement(doc, 'div', { className: 'bb-pkw-kicker', text: 'プロジェクトの記録' }));
|
|
1260
1260
|
const header = makeElement(doc, 'header', { className: 'bb-pkw-header' });
|
|
@@ -1562,6 +1562,7 @@ export function createProjectKnowledgeWorkspace({
|
|
|
1562
1562
|
if (typeof onTabChange === 'function') onTabChange(activeTab);
|
|
1563
1563
|
teardownGraph();
|
|
1564
1564
|
tabs.replaceChildren(tabButton('概要', 'overview'), tabButton('情報を探す', 'graph'));
|
|
1565
|
+
tabStrip.update();
|
|
1565
1566
|
panels.replaceChildren();
|
|
1566
1567
|
const actions = { openOverview, openWorkList, openWorkDetail, openRecordDetail, openSectionList, openGraphEntity: selectAndExplore,
|
|
1567
1568
|
retryParticipants: typeof onRetryProject === 'function' ? () => onRetryProject(workspaceProjectId) : null,
|