An agent proposes a document ID. The application decides which tenant may be searched. Keeping those responsibilities separate is the property this exercise tests.

The example runs with Node.js 22 or newer and keeps all state in memory. It uses no model, database, or network. All records are synthetic; run it locally in an environment you control.

1. Separate trusted job state from proposed arguments

Save the program as tenant-retrieval.mjs. The jobId argument represents a value supplied by the trusted dispatcher after authentication, not a choice exposed to the model. The model-controlled input contains only a document ID.

Two tenants deliberately have a document named shared. The lookup must resolve the ID within the job’s tenant rather than finding a global document first.

import assert from 'node:assert/strict';

const jobs = new Map([
  ['job-a', { tenant: 'tenant-a', active: true }],
  ['job-b', { tenant: 'tenant-b', active: true }],
]);
const documents = [
  { id: 'shared', tenant: 'tenant-a', text: 'Tenant A fixture' },
  { id: 'shared', tenant: 'tenant-b', text: 'Tenant B fixture' },
  { id: 'b-only', tenant: 'tenant-b', text: 'Private B fixture' },
];

function retrieve(jobId, input) {
  const job = jobs.get(jobId);
  assert(job?.active, 'task unavailable');
  assert(input && typeof input === 'object' && !Array.isArray(input), 'invalid arguments');
  assert.deepEqual(Object.keys(input), ['documentId']);
  assert(typeof input.documentId === 'string' && /^[a-z0-9-]{1,40}$/.test(input.documentId), 'invalid document ID');
  const document = documents.find((item) => item.id === input.documentId && item.tenant === job.tenant);
  assert(document, 'document unavailable');
  return { id: document.id, text: document.text };
}

assert.deepEqual(retrieve('job-a', { documentId: 'shared' }), { id: 'shared', text: 'Tenant A fixture' });
assert.equal(retrieve('job-b', { documentId: 'shared' }).text, 'Tenant B fixture');
for (const documentId of ['b-only', 'missing']) {
  assert.throws(() => retrieve('job-a', { documentId }), (error) => {
    assert.equal(error.message, 'document unavailable');
    assert(!error.message.includes('Private B'));
    return true;
  });
}
assert.throws(() => retrieve('job-a', { documentId: 'b-only', tenant: 'tenant-b' }));
assert.throws(() => retrieve('job-a', { documentId: '../invalid' }), /invalid document ID/);
jobs.get('job-a').active = false;
assert.throws(() => retrieve('job-a', { documentId: 'shared' }), /task unavailable/);
assert.throws(() => retrieve('unknown', { documentId: 'shared' }), /task unavailable/);
assert.equal(retrieve('job-b', { documentId: 'shared' }).text, 'Tenant B fixture');
console.log('PASS: tenant-scoped reads succeed; cross-tenant, forged-scope, and revoked-task reads fail.');

2. Run the exercise

node tenant-retrieval.mjs

Expect one PASS: line. The permitted reads return different text for the same ID, proving that tenant scope participates in the lookup. Revoking job A does not disable the independent job B.

The error for an inaccessible document matches the error for a nonexistent one. This reduces information disclosed by this function’s messages. It does not claim to remove timing differences or every possible existence side channel.

3. Check for a meaningful regression

In a temporary copy, remove && item.tenant === job.tenant from the lookup. Run it again. The assertion for job B must fail because the global search finds tenant A’s shared document first.

A test that used globally unique IDs and only checked tenant A’s happy path could miss that regression. Restore the scope condition after the experiment.

4. Translate the property into your application

In a database-backed implementation, constrain the query itself by the verified scope and keep current document permissions in the decision. Do not fetch confidential text into model context and filter it afterward.

This in-memory example omits authentication, document-level permissions inside a tenant, caches, and concurrent permission changes. It also cannot erase text already returned before revocation. The retrieval isolation guide describes those additional boundaries.

Keep the test alongside the retrieval code when adapting it. Its useful claim is specific: a model-supplied document identifier cannot change the tenant selected by the trusted job.