derived-artifact-storage-unit.test.mjs
sha256:0d530f9ef27b8b75547d1db7701a74bc77b77aa8f3d7fa3a8672cf2af36e63bb
reconcile: import GitHub-direct RBAC/OAuth/companion and ho…
Human
minor
⚠ breaking
4 hours ago
| 1 | /** |
| 2 | * Tier 1 — UNIT: Phase 6 derived-artifact storage layer. |
| 3 | * |
| 4 | * Covers (§10 Unit obligations): |
| 5 | * - Provenance schema validation (every required field; reject missing/malformed; |
| 6 | * model_version-or-runtime_version rule) |
| 7 | * - Tier resolver (D6.1) incl. fail-closed unknown→privacy-max |
| 8 | * - ClientEncryptor: isAvailable=false → no host-readable write |
| 9 | * - Routing table: each artifact×tier → correct terminal state |
| 10 | * - enrichesDelegatedPartition computation (owner≠actor) |
| 11 | * - Re-enrichment eligibility (D6.7) |
| 12 | */ |
| 13 | |
| 14 | import { describe, it } from 'node:test'; |
| 15 | import assert from 'node:assert/strict'; |
| 16 | |
| 17 | import { |
| 18 | validateProvenance, |
| 19 | buildConvenienceProvenance, |
| 20 | PROVENANCE_REJECT_REASONS, |
| 21 | PROVENANCE_SCHEMA_VERSION, |
| 22 | ARTIFACT_TYPES, |
| 23 | PRIVACY_TIERS, |
| 24 | PROVENANCE_SOURCES, |
| 25 | } from '../lib/companion-provenance-validator.mjs'; |
| 26 | |
| 27 | import { |
| 28 | resolveTier, |
| 29 | TERMINAL_STATES, |
| 30 | TIER_RESOLVE_REASONS, |
| 31 | } from '../lib/companion-tier-resolver.mjs'; |
| 32 | |
| 33 | import { |
| 34 | UNAVAILABLE_CLIENT_ENCRYPTOR, |
| 35 | createClientEncryptor, |
| 36 | ENCRYPTOR_REASONS, |
| 37 | ENCRYPTOR_SCOPES, |
| 38 | } from '../lib/companion-client-encryptor.mjs'; |
| 39 | |
| 40 | import { |
| 41 | createDerivedArtifactWriter, |
| 42 | WRITER_REASONS, |
| 43 | } from '../lib/companion-artifact-writer.mjs'; |
| 44 | |
| 45 | // ── Helpers ─────────────────────────────────────────────────────────────────── |
| 46 | |
| 47 | function validProvenance(overrides = {}) { |
| 48 | return { |
| 49 | generated_by: 'user-123', |
| 50 | source: 'companion', |
| 51 | model: 'llama-3', |
| 52 | model_version: '3.1', |
| 53 | runtime_version: '0.9.0', |
| 54 | lane: 'local', |
| 55 | privacy_tier: 'convenience', |
| 56 | source_note_path: 'projects/note.md', |
| 57 | source_event_id: 'mem_abc123', |
| 58 | created_at: new Date().toISOString(), |
| 59 | artifact_type: 'ai_summary', |
| 60 | schema_version: PROVENANCE_SCHEMA_VERSION, |
| 61 | ...overrides, |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | function makeWriter(overrides = {}) { |
| 66 | return createDerivedArtifactWriter({ |
| 67 | writeNoteFn: () => {}, |
| 68 | vaultPath: '/vault', |
| 69 | ...overrides, |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | // ── Provenance schema validation ────────────────────────────────────────────── |
| 74 | |
| 75 | describe('validateProvenance — required field presence', () => { |
| 76 | it('accepts a fully valid provenance record', () => { |
| 77 | assert.deepEqual(validateProvenance(validProvenance()), { ok: true }); |
| 78 | }); |
| 79 | |
| 80 | it('rejects null provenance', () => { |
| 81 | const r = validateProvenance(null); |
| 82 | assert.equal(r.ok, false); |
| 83 | assert.equal(r.reason, PROVENANCE_REJECT_REASONS.MISSING_FIELD); |
| 84 | }); |
| 85 | |
| 86 | it('rejects array provenance', () => { |
| 87 | const r = validateProvenance([]); |
| 88 | assert.equal(r.ok, false); |
| 89 | }); |
| 90 | |
| 91 | for (const field of [ |
| 92 | 'generated_by', 'model', 'lane', 'privacy_tier', |
| 93 | 'source_note_path', 'source_event_id', 'created_at', 'artifact_type', 'schema_version', |
| 94 | ]) { |
| 95 | it(`rejects missing field: ${field}`, () => { |
| 96 | const p = validProvenance(); |
| 97 | delete p[field]; |
| 98 | const r = validateProvenance(p); |
| 99 | assert.equal(r.ok, false, `Expected rejection for missing ${field}`); |
| 100 | }); |
| 101 | } |
| 102 | |
| 103 | it('rejects empty generated_by', () => { |
| 104 | const r = validateProvenance(validProvenance({ generated_by: ' ' })); |
| 105 | assert.equal(r.ok, false); |
| 106 | assert.equal(r.field, 'generated_by'); |
| 107 | }); |
| 108 | |
| 109 | it('rejects empty model', () => { |
| 110 | const r = validateProvenance(validProvenance({ model: '' })); |
| 111 | assert.equal(r.ok, false); |
| 112 | }); |
| 113 | |
| 114 | it('rejects invalid source enum', () => { |
| 115 | const r = validateProvenance(validProvenance({ source: 'unknown_source' })); |
| 116 | assert.equal(r.ok, false); |
| 117 | assert.equal(r.reason, PROVENANCE_REJECT_REASONS.MALFORMED_FIELD); |
| 118 | assert.equal(r.field, 'source'); |
| 119 | }); |
| 120 | |
| 121 | it('accepts all valid source values', () => { |
| 122 | for (const source of PROVENANCE_SOURCES) { |
| 123 | const p = validProvenance({ source }); |
| 124 | // privacy_max+managed is a contradiction — skip that combo in this test |
| 125 | if (p.privacy_tier === 'privacy_max' && source === 'managed') continue; |
| 126 | assert.deepEqual(validateProvenance(p), { ok: true }, `source=${source} should be valid`); |
| 127 | } |
| 128 | }); |
| 129 | |
| 130 | it('rejects invalid lane', () => { |
| 131 | const r = validateProvenance(validProvenance({ lane: 'not-a-lane' })); |
| 132 | assert.equal(r.ok, false); |
| 133 | assert.equal(r.field, 'lane'); |
| 134 | }); |
| 135 | |
| 136 | it('rejects invalid privacy_tier', () => { |
| 137 | const r = validateProvenance(validProvenance({ privacy_tier: 'secret' })); |
| 138 | assert.equal(r.ok, false); |
| 139 | assert.equal(r.field, 'privacy_tier'); |
| 140 | }); |
| 141 | |
| 142 | it('rejects non-integer schema_version', () => { |
| 143 | const r = validateProvenance(validProvenance({ schema_version: 1.5 })); |
| 144 | assert.equal(r.ok, false); |
| 145 | assert.equal(r.field, 'schema_version'); |
| 146 | }); |
| 147 | |
| 148 | it('rejects schema_version < 1', () => { |
| 149 | const r = validateProvenance(validProvenance({ schema_version: 0 })); |
| 150 | assert.equal(r.ok, false); |
| 151 | }); |
| 152 | |
| 153 | it('rejects invalid created_at', () => { |
| 154 | const r = validateProvenance(validProvenance({ created_at: 'not-a-date' })); |
| 155 | assert.equal(r.ok, false); |
| 156 | assert.equal(r.field, 'created_at'); |
| 157 | }); |
| 158 | |
| 159 | it('rejects empty created_at', () => { |
| 160 | const r = validateProvenance(validProvenance({ created_at: '' })); |
| 161 | assert.equal(r.ok, false); |
| 162 | }); |
| 163 | |
| 164 | it('rejects invalid artifact_type', () => { |
| 165 | const r = validateProvenance(validProvenance({ artifact_type: 'video' })); |
| 166 | assert.equal(r.ok, false); |
| 167 | assert.equal(r.field, 'artifact_type'); |
| 168 | }); |
| 169 | |
| 170 | it('accepts all valid artifact_type values', () => { |
| 171 | for (const artifact_type of ARTIFACT_TYPES) { |
| 172 | assert.deepEqual( |
| 173 | validateProvenance(validProvenance({ artifact_type })), |
| 174 | { ok: true }, |
| 175 | `artifact_type=${artifact_type}`, |
| 176 | ); |
| 177 | } |
| 178 | }); |
| 179 | }); |
| 180 | |
| 181 | describe('validateProvenance — model_version|runtime_version rule (D6.2.1)', () => { |
| 182 | it('accepts only model_version', () => { |
| 183 | assert.deepEqual( |
| 184 | validateProvenance(validProvenance({ model_version: '3.1', runtime_version: null })), |
| 185 | { ok: true }, |
| 186 | ); |
| 187 | }); |
| 188 | |
| 189 | it('accepts only runtime_version', () => { |
| 190 | assert.deepEqual( |
| 191 | validateProvenance(validProvenance({ model_version: null, runtime_version: '0.9.0' })), |
| 192 | { ok: true }, |
| 193 | ); |
| 194 | }); |
| 195 | |
| 196 | it('rejects both absent', () => { |
| 197 | const r = validateProvenance(validProvenance({ model_version: null, runtime_version: null })); |
| 198 | assert.equal(r.ok, false); |
| 199 | assert.equal(r.reason, PROVENANCE_REJECT_REASONS.BOTH_VERSIONS_ABSENT); |
| 200 | }); |
| 201 | |
| 202 | it('rejects both empty strings', () => { |
| 203 | const r = validateProvenance(validProvenance({ model_version: '', runtime_version: '' })); |
| 204 | assert.equal(r.ok, false); |
| 205 | assert.equal(r.reason, PROVENANCE_REJECT_REASONS.BOTH_VERSIONS_ABSENT); |
| 206 | }); |
| 207 | |
| 208 | it('rejects missing runtime_version key entirely', () => { |
| 209 | const p = validProvenance(); |
| 210 | delete p.runtime_version; |
| 211 | const r = validateProvenance(p); |
| 212 | assert.equal(r.ok, false); |
| 213 | assert.equal(r.field, 'runtime_version'); |
| 214 | }); |
| 215 | }); |
| 216 | |
| 217 | describe('validateProvenance — source_event_id variants', () => { |
| 218 | it('accepts a single string id', () => { |
| 219 | assert.deepEqual(validateProvenance(validProvenance({ source_event_id: 'mem_001' })), { ok: true }); |
| 220 | }); |
| 221 | |
| 222 | it('accepts a non-empty string array', () => { |
| 223 | assert.deepEqual( |
| 224 | validateProvenance(validProvenance({ source_event_id: ['mem_001', 'mem_002'] })), |
| 225 | { ok: true }, |
| 226 | ); |
| 227 | }); |
| 228 | |
| 229 | it('rejects empty string', () => { |
| 230 | const r = validateProvenance(validProvenance({ source_event_id: '' })); |
| 231 | assert.equal(r.ok, false); |
| 232 | assert.equal(r.field, 'source_event_id'); |
| 233 | }); |
| 234 | |
| 235 | it('rejects empty array', () => { |
| 236 | const r = validateProvenance(validProvenance({ source_event_id: [] })); |
| 237 | assert.equal(r.ok, false); |
| 238 | }); |
| 239 | |
| 240 | it('rejects array with empty string element', () => { |
| 241 | const r = validateProvenance(validProvenance({ source_event_id: ['mem_001', ''] })); |
| 242 | assert.equal(r.ok, false); |
| 243 | }); |
| 244 | |
| 245 | it('rejects number type', () => { |
| 246 | const r = validateProvenance(validProvenance({ source_event_id: 123 })); |
| 247 | assert.equal(r.ok, false); |
| 248 | }); |
| 249 | }); |
| 250 | |
| 251 | describe('validateProvenance — privacy_max + managed contradiction (D6.2)', () => { |
| 252 | it('rejects privacy_max + source=managed', () => { |
| 253 | const r = validateProvenance(validProvenance({ privacy_tier: 'privacy_max', source: 'managed' })); |
| 254 | assert.equal(r.ok, false); |
| 255 | assert.equal(r.reason, PROVENANCE_REJECT_REASONS.PRIVACY_MAX_MANAGED_CONTRADICTION); |
| 256 | }); |
| 257 | |
| 258 | it('accepts privacy_max + source=companion', () => { |
| 259 | assert.deepEqual( |
| 260 | validateProvenance(validProvenance({ privacy_tier: 'privacy_max', source: 'companion' })), |
| 261 | { ok: true }, |
| 262 | ); |
| 263 | }); |
| 264 | |
| 265 | it('accepts convenience + source=managed', () => { |
| 266 | assert.deepEqual( |
| 267 | validateProvenance(validProvenance({ privacy_tier: 'convenience', source: 'managed' })), |
| 268 | { ok: true }, |
| 269 | ); |
| 270 | }); |
| 271 | }); |
| 272 | |
| 273 | describe('validateProvenance — hasSensitiveKeys scan (D6.2.3)', () => { |
| 274 | it('rejects provenance with a token field', () => { |
| 275 | const p = validProvenance({ token: 'secret-value' }); |
| 276 | const r = validateProvenance(p); |
| 277 | assert.equal(r.ok, false); |
| 278 | assert.equal(r.reason, PROVENANCE_REJECT_REASONS.SENSITIVE_DATA); |
| 279 | }); |
| 280 | |
| 281 | it('rejects provenance with a nested secret field', () => { |
| 282 | const p = validProvenance({ meta: { api_key: 'sk-123' } }); |
| 283 | const r = validateProvenance(p); |
| 284 | assert.equal(r.ok, false); |
| 285 | assert.equal(r.reason, PROVENANCE_REJECT_REASONS.SENSITIVE_DATA); |
| 286 | }); |
| 287 | |
| 288 | it('rejects artifact payload with a token field', () => { |
| 289 | const p = validProvenance(); |
| 290 | const artifact = { summary: 'ok', secret: 'val' }; |
| 291 | const r = validateProvenance(p, artifact); |
| 292 | assert.equal(r.ok, false); |
| 293 | assert.equal(r.reason, PROVENANCE_REJECT_REASONS.SENSITIVE_DATA); |
| 294 | }); |
| 295 | |
| 296 | it('accepts an artifact with no sensitive keys', () => { |
| 297 | const p = validProvenance(); |
| 298 | const artifact = { summary: 'This is a fine summary.' }; |
| 299 | assert.deepEqual(validateProvenance(p, artifact), { ok: true }); |
| 300 | }); |
| 301 | }); |
| 302 | |
| 303 | // ── Tier resolver ───────────────────────────────────────────────────────────── |
| 304 | |
| 305 | describe('resolveTier — D6.1 routing table', () => { |
| 306 | it('maps convenience × any artifact_type → host_readable', () => { |
| 307 | for (const at of ARTIFACT_TYPES) { |
| 308 | const r = resolveTier(at, 'convenience'); |
| 309 | assert.equal(r.ok, true); |
| 310 | assert.equal(r.terminalState, TERMINAL_STATES.HOST_READABLE, `artifact_type=${at}`); |
| 311 | } |
| 312 | }); |
| 313 | |
| 314 | it('rejects privacy_max without vault registry (D6.1.1 fail-closed)', () => { |
| 315 | for (const at of ARTIFACT_TYPES) { |
| 316 | const r = resolveTier(at, 'privacy_max', { vaultRegistryAvailable: false }); |
| 317 | assert.equal(r.ok, false); |
| 318 | assert.equal(r.reason, TIER_RESOLVE_REASONS.PRIVACY_MAX_NO_REGISTRY, `artifact_type=${at}`); |
| 319 | } |
| 320 | }); |
| 321 | |
| 322 | it('resolves privacy_max → client_encrypted when registry is available', () => { |
| 323 | for (const at of ARTIFACT_TYPES) { |
| 324 | const r = resolveTier(at, 'privacy_max', { vaultRegistryAvailable: true }); |
| 325 | assert.equal(r.ok, true); |
| 326 | assert.equal(r.terminalState, TERMINAL_STATES.CLIENT_ENCRYPTED, `artifact_type=${at}`); |
| 327 | } |
| 328 | }); |
| 329 | |
| 330 | it('rejects unknown tier → fail-closed (not convenience)', () => { |
| 331 | const r = resolveTier('ai_summary', 'unknown_tier'); |
| 332 | assert.equal(r.ok, false); |
| 333 | assert.equal(r.reason, TIER_RESOLVE_REASONS.UNKNOWN_TIER); |
| 334 | }); |
| 335 | |
| 336 | it('rejects null tier', () => { |
| 337 | const r = resolveTier('ai_summary', null); |
| 338 | assert.equal(r.ok, false); |
| 339 | }); |
| 340 | |
| 341 | it('rejects invalid artifact_type', () => { |
| 342 | const r = resolveTier('video', 'convenience'); |
| 343 | assert.equal(r.ok, false); |
| 344 | assert.equal(r.reason, TIER_RESOLVE_REASONS.INVALID_ARTIFACT_TYPE); |
| 345 | }); |
| 346 | }); |
| 347 | |
| 348 | // ── ClientEncryptor ─────────────────────────────────────────────────────────── |
| 349 | |
| 350 | describe('UNAVAILABLE_CLIENT_ENCRYPTOR — default fail-closed', () => { |
| 351 | it('isAvailable always returns false', () => { |
| 352 | assert.equal(UNAVAILABLE_CLIENT_ENCRYPTOR.isAvailable('privacy_max', 'vault'), false); |
| 353 | assert.equal(UNAVAILABLE_CLIENT_ENCRYPTOR.isAvailable('convenience', 'memory'), false); |
| 354 | assert.equal(UNAVAILABLE_CLIENT_ENCRYPTOR.isAvailable('anything', 'anything'), false); |
| 355 | }); |
| 356 | |
| 357 | it('encrypt always throws UNAVAILABLE (no plaintext fallback)', () => { |
| 358 | assert.throws( |
| 359 | () => UNAVAILABLE_CLIENT_ENCRYPTOR.encrypt(new Uint8Array([1, 2, 3]), { scope: 'vault' }), |
| 360 | { message: ENCRYPTOR_REASONS.UNAVAILABLE }, |
| 361 | ); |
| 362 | }); |
| 363 | |
| 364 | it('has no decrypt method (D6.4.1)', () => { |
| 365 | assert.equal('decrypt' in UNAVAILABLE_CLIENT_ENCRYPTOR, false); |
| 366 | }); |
| 367 | }); |
| 368 | |
| 369 | describe('createClientEncryptor — wrapper contract enforcement', () => { |
| 370 | it('throws on missing isAvailable', () => { |
| 371 | assert.throws(() => createClientEncryptor({ encrypt: () => {} }), TypeError); |
| 372 | }); |
| 373 | |
| 374 | it('throws on missing encrypt', () => { |
| 375 | assert.throws(() => createClientEncryptor({ isAvailable: () => true }), TypeError); |
| 376 | }); |
| 377 | |
| 378 | it('wraps a valid implementation', () => { |
| 379 | const impl = { |
| 380 | isAvailable: () => true, |
| 381 | encrypt: (_bytes, _opts) => ({ |
| 382 | ciphertext: new Uint8Array([9, 8, 7]), |
| 383 | wrappedDekRef: 'dek-ref-001', |
| 384 | alg: 'AES-GCM-256', |
| 385 | }), |
| 386 | }; |
| 387 | const enc = createClientEncryptor(impl); |
| 388 | assert.equal(enc.isAvailable('privacy_max', ENCRYPTOR_SCOPES.VAULT), true); |
| 389 | const result = enc.encrypt(new Uint8Array([1]), { scope: ENCRYPTOR_SCOPES.VAULT }); |
| 390 | assert.ok(result.ciphertext instanceof Uint8Array); |
| 391 | assert.equal(result.wrappedDekRef, 'dek-ref-001'); |
| 392 | assert.equal(result.alg, 'AES-GCM-256'); |
| 393 | }); |
| 394 | |
| 395 | it('converts isAvailable exception to false (fail-closed)', () => { |
| 396 | const impl = { |
| 397 | isAvailable: () => { throw new Error('boom'); }, |
| 398 | encrypt: () => {}, |
| 399 | }; |
| 400 | const enc = createClientEncryptor(impl); |
| 401 | assert.equal(enc.isAvailable('privacy_max', 'vault'), false); |
| 402 | }); |
| 403 | |
| 404 | it('throws ENCRYPT_FAILED when impl returns malformed result', () => { |
| 405 | const impl = { |
| 406 | isAvailable: () => true, |
| 407 | encrypt: () => ({ ciphertext: 'not-uint8array', wrappedDekRef: 'x', alg: 'y' }), |
| 408 | }; |
| 409 | const enc = createClientEncryptor(impl); |
| 410 | assert.throws( |
| 411 | () => enc.encrypt(new Uint8Array([1]), { scope: 'vault' }), |
| 412 | { message: ENCRYPTOR_REASONS.ENCRYPT_FAILED }, |
| 413 | ); |
| 414 | }); |
| 415 | |
| 416 | it('does not expose a decrypt method (D6.4.1)', () => { |
| 417 | const impl = { |
| 418 | isAvailable: () => false, |
| 419 | encrypt: () => {}, |
| 420 | decrypt: () => 'plaintext', |
| 421 | }; |
| 422 | const enc = createClientEncryptor(impl); |
| 423 | assert.equal('decrypt' in enc, false); |
| 424 | }); |
| 425 | }); |
| 426 | |
| 427 | // ── DerivedArtifactWriter — construction ────────────────────────────────────── |
| 428 | |
| 429 | describe('createDerivedArtifactWriter — construction guards', () => { |
| 430 | it('throws if writeNoteFn is missing', () => { |
| 431 | assert.throws( |
| 432 | () => createDerivedArtifactWriter({ vaultPath: '/v' }), |
| 433 | TypeError, |
| 434 | ); |
| 435 | }); |
| 436 | |
| 437 | it('throws if vaultPath is empty', () => { |
| 438 | assert.throws( |
| 439 | () => createDerivedArtifactWriter({ writeNoteFn: () => {}, vaultPath: '' }), |
| 440 | TypeError, |
| 441 | ); |
| 442 | }); |
| 443 | |
| 444 | it('returns a frozen writer object', () => { |
| 445 | const w = makeWriter(); |
| 446 | assert.ok(Object.isFrozen(w)); |
| 447 | }); |
| 448 | |
| 449 | it('exposes write, deleteArtifacts, checkReEnrichmentEligibility', () => { |
| 450 | const w = makeWriter(); |
| 451 | assert.equal(typeof w.write, 'function'); |
| 452 | assert.equal(typeof w.deleteArtifacts, 'function'); |
| 453 | assert.equal(typeof w.checkReEnrichmentEligibility, 'function'); |
| 454 | }); |
| 455 | }); |
| 456 | |
| 457 | // ── DerivedArtifactWriter — write gate: self-partition only (D6.3.6) ───────── |
| 458 | |
| 459 | describe('writer.write — cross-partition blocked (D6.3.6)', () => { |
| 460 | it('rejects enrichesDelegatedPartition=true before tenancy gate', async () => { |
| 461 | const w = makeWriter(); |
| 462 | const r = await w.write( |
| 463 | { summary: 'hi' }, |
| 464 | validProvenance(), |
| 465 | { lane: 'local', containsPrivateData: false, isDelegate: true, |
| 466 | delegatedManagedAllowed: false, enrichesDelegatedPartition: true, |
| 467 | delegatedEnrichmentAllowed: false }, |
| 468 | ); |
| 469 | assert.equal(r.ok, false); |
| 470 | assert.equal(r.reason, WRITER_REASONS.SELF_PARTITION_ONLY); |
| 471 | }); |
| 472 | }); |
| 473 | |
| 474 | // ── DerivedArtifactWriter — write gate: provenance validation ───────────────── |
| 475 | |
| 476 | describe('writer.write — provenance invalid → no write', () => { |
| 477 | it('returns PROVENANCE_INVALID for missing generated_by', async () => { |
| 478 | const w = makeWriter(); |
| 479 | const p = validProvenance(); |
| 480 | delete p.generated_by; |
| 481 | const r = await w.write({ summary: 'x' }, p, { lane: 'local', containsPrivateData: false }); |
| 482 | assert.equal(r.ok, false); |
| 483 | assert.equal(r.reason, WRITER_REASONS.PROVENANCE_INVALID); |
| 484 | }); |
| 485 | }); |
| 486 | |
| 487 | // ── DerivedArtifactWriter — write gate: tier unresolvable ──────────────────── |
| 488 | |
| 489 | describe('writer.write — unknown tier fails closed', () => { |
| 490 | it('returns TIER_UNRESOLVABLE for unknown privacy_tier', async () => { |
| 491 | const w = makeWriter(); |
| 492 | const p = validProvenance({ privacy_tier: 'unknownTier' }); |
| 493 | // Bypass provenance validator by patching after validation — just test tier resolve: |
| 494 | // Instead test with model_version present to avoid OTHER rejections first. |
| 495 | // Actually privacy_tier is validated by validateProvenance first → PROVENANCE_INVALID. |
| 496 | // So unknown tier first hits provenance validator → MALFORMED_FIELD → PROVENANCE_INVALID. |
| 497 | const r = await w.write({ summary: 'x' }, p, { lane: 'local', containsPrivateData: false }); |
| 498 | assert.equal(r.ok, false); |
| 499 | // Either PROVENANCE_INVALID (validator catches it) or TIER_UNRESOLVABLE is correct |
| 500 | assert.ok( |
| 501 | r.reason === WRITER_REASONS.PROVENANCE_INVALID || r.reason === WRITER_REASONS.TIER_UNRESOLVABLE, |
| 502 | ); |
| 503 | }); |
| 504 | }); |
| 505 | |
| 506 | // ── DerivedArtifactWriter — write gate: consent denied ─────────────────────── |
| 507 | |
| 508 | describe('writer.write — consent denied → no write', () => { |
| 509 | it('denies managed lane delegate without delegatedManagedAllowed', async () => { |
| 510 | const writes = []; |
| 511 | const w = createDerivedArtifactWriter({ |
| 512 | writeNoteFn: (_, _p, opts) => writes.push(opts), |
| 513 | vaultPath: '/v', |
| 514 | }); |
| 515 | const r = await w.write( |
| 516 | { summary: 'x' }, |
| 517 | validProvenance({ lane: 'direct_provider', source: 'managed' }), |
| 518 | { lane: 'direct_provider', containsPrivateData: false, |
| 519 | isDelegate: true, delegatedManagedAllowed: false }, |
| 520 | ); |
| 521 | assert.equal(r.ok, false); |
| 522 | assert.equal(r.reason, WRITER_REASONS.CONSENT_DENIED); |
| 523 | assert.equal(writes.length, 0); |
| 524 | }); |
| 525 | }); |
| 526 | |
| 527 | // ── DerivedArtifactWriter — write success: host_readable ───────────────────── |
| 528 | |
| 529 | describe('writer.write — convenience tier → host_readable store', () => { |
| 530 | it('calls writeNoteFn with ai_summary + provenance sidecar', async () => { |
| 531 | const writes = []; |
| 532 | const w = createDerivedArtifactWriter({ |
| 533 | writeNoteFn: (_vp, notePath, opts) => writes.push({ notePath, opts }), |
| 534 | vaultPath: '/vault', |
| 535 | }); |
| 536 | const prov = validProvenance({ |
| 537 | artifact_type: 'ai_summary', |
| 538 | source_note_path: 'projects/note.md', |
| 539 | }); |
| 540 | const r = await w.write({ summary: 'A good summary.' }, prov, { |
| 541 | lane: 'local', containsPrivateData: false, isDelegate: false, |
| 542 | delegatedManagedAllowed: false, |
| 543 | }); |
| 544 | assert.equal(r.ok, true); |
| 545 | assert.equal(r.terminalState, TERMINAL_STATES.HOST_READABLE); |
| 546 | assert.equal(writes.length, 1); |
| 547 | assert.equal(writes[0].notePath, 'projects/note.md'); |
| 548 | assert.equal(writes[0].opts.frontmatter.ai_summary, 'A good summary.'); |
| 549 | assert.ok(writes[0].opts.frontmatter.ai_summary_provenance); |
| 550 | }); |
| 551 | }); |
| 552 | |
| 553 | // ── DerivedArtifactWriter — write: encryption unavailable fails closed ──────── |
| 554 | |
| 555 | describe('writer.write — privacy_max + no encryptor → ENCRYPTION_UNAVAILABLE', () => { |
| 556 | it('never writes host-readable when encryptor is absent', async () => { |
| 557 | const writes = []; |
| 558 | const w = createDerivedArtifactWriter({ |
| 559 | writeNoteFn: (_vp, _np, opts) => writes.push(opts), |
| 560 | vaultPath: '/vault', |
| 561 | vaultRegistryAvailable: true, // registry present — tier resolves to client_encrypted |
| 562 | }); |
| 563 | const prov = validProvenance({ privacy_tier: 'privacy_max', source: 'companion' }); |
| 564 | const r = await w.write({ summary: 'secret' }, prov, { |
| 565 | lane: 'local', containsPrivateData: true, isDelegate: false, |
| 566 | delegatedManagedAllowed: false, |
| 567 | }); |
| 568 | assert.equal(r.ok, false); |
| 569 | assert.equal(r.reason, WRITER_REASONS.ENCRYPTION_UNAVAILABLE); |
| 570 | // No write must have happened |
| 571 | assert.equal(writes.length, 0); |
| 572 | }); |
| 573 | }); |
| 574 | |
| 575 | // ── DerivedArtifactWriter — re-enrichment eligibility (D6.7) ───────────────── |
| 576 | |
| 577 | describe('checkReEnrichmentEligibility — D6.7', () => { |
| 578 | it('eligible when model_version differs', () => { |
| 579 | const w = makeWriter(); |
| 580 | const r = w.checkReEnrichmentEligibility( |
| 581 | { model_version: 'v1', runtime_version: null }, |
| 582 | { model_version: 'v2', runtime_version: null }, |
| 583 | ); |
| 584 | assert.equal(r.eligible, true); |
| 585 | assert.equal(r.reason, 're_enrichment_model_version_newer'); |
| 586 | }); |
| 587 | |
| 588 | it('eligible when runtime_version differs', () => { |
| 589 | const w = makeWriter(); |
| 590 | const r = w.checkReEnrichmentEligibility( |
| 591 | { model_version: null, runtime_version: '1.0' }, |
| 592 | { model_version: null, runtime_version: '2.0' }, |
| 593 | ); |
| 594 | assert.equal(r.eligible, true); |
| 595 | assert.equal(r.reason, 're_enrichment_runtime_version_newer'); |
| 596 | }); |
| 597 | |
| 598 | it('not eligible when versions match', () => { |
| 599 | const w = makeWriter(); |
| 600 | const r = w.checkReEnrichmentEligibility( |
| 601 | { model_version: 'v1', runtime_version: '1.0' }, |
| 602 | { model_version: 'v1', runtime_version: '1.0' }, |
| 603 | ); |
| 604 | assert.equal(r.eligible, false); |
| 605 | assert.equal(r.reason, 're_enrichment_version_current'); |
| 606 | }); |
| 607 | |
| 608 | it('eligible when stored version absent (fail-closed, D6.7 §fail-closed)', () => { |
| 609 | const w = makeWriter(); |
| 610 | const r = w.checkReEnrichmentEligibility( |
| 611 | { model_version: null, runtime_version: null }, |
| 612 | { model_version: 'v1', runtime_version: null }, |
| 613 | ); |
| 614 | assert.equal(r.eligible, true); |
| 615 | }); |
| 616 | |
| 617 | it('eligible when storedProvenance is null (fail-closed)', () => { |
| 618 | const w = makeWriter(); |
| 619 | const r = w.checkReEnrichmentEligibility(null, { model_version: 'v1', runtime_version: null }); |
| 620 | assert.equal(r.eligible, true); |
| 621 | }); |
| 622 | |
| 623 | it('eligible when currentVersions is null (unknown current = safe recompute)', () => { |
| 624 | const w = makeWriter(); |
| 625 | const r = w.checkReEnrichmentEligibility({ model_version: 'v1', runtime_version: null }, null); |
| 626 | assert.equal(r.eligible, true); |
| 627 | }); |
| 628 | }); |
| 629 | |
| 630 | // ── buildConvenienceProvenance helper ───────────────────────────────────────── |
| 631 | |
| 632 | describe('buildConvenienceProvenance', () => { |
| 633 | it('produces a record that passes validateProvenance', () => { |
| 634 | const p = buildConvenienceProvenance({ |
| 635 | generatedBy: 'system', |
| 636 | source: 'companion', |
| 637 | model: 'llama-3', |
| 638 | modelVersion: '3.1', |
| 639 | runtimeVersion: '0.9', |
| 640 | lane: 'local', |
| 641 | artifactType: 'ai_summary', |
| 642 | sourceNotePath: 'notes/test.md', |
| 643 | sourceEventId: 'mem_001', |
| 644 | }); |
| 645 | assert.deepEqual(validateProvenance(p), { ok: true }); |
| 646 | assert.equal(p.privacy_tier, 'convenience'); |
| 647 | assert.equal(p.schema_version, PROVENANCE_SCHEMA_VERSION); |
| 648 | }); |
| 649 | |
| 650 | it('sets runtime_version to null when omitted (only model_version concrete)', () => { |
| 651 | const p = buildConvenienceProvenance({ |
| 652 | generatedBy: 'sys', |
| 653 | source: 'companion', |
| 654 | model: 'm', |
| 655 | modelVersion: '1.0', |
| 656 | lane: 'local', |
| 657 | artifactType: 'insight', |
| 658 | sourceEventId: ['mem_001'], |
| 659 | }); |
| 660 | assert.equal(p.runtime_version, null); |
| 661 | assert.deepEqual(validateProvenance(p), { ok: true }); |
| 662 | }); |
| 663 | }); |
File History
1 commit
sha256:0d530f9ef27b8b75547d1db7701a74bc77b77aa8f3d7fa3a8672cf2af36e63bb
reconcile: import GitHub-direct RBAC/OAuth/companion and ho…
Human
minor
⚠
4 hours ago