@unson/brainbase-mcp 0.49.0 → 0.50.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/contracts/brainbase-public-contract.v1.json +288 -0
- package/dist/meeting-source-connection.d.ts +33 -1
- package/dist/meeting-source-connection.js +124 -1
- package/dist/meeting-source-reader.d.ts +9 -0
- package/dist/meeting-source-reader.js +10 -3
- package/dist/ownership-survey.d.ts +28 -0
- package/dist/ownership-survey.js +60 -0
- package/dist/queue-task-tools.d.ts +114 -0
- package/dist/queue-task-tools.js +263 -0
- package/dist/testing/queue-task-reference-backend.d.ts +43 -0
- package/dist/testing/queue-task-reference-backend.js +157 -0
- package/dist/testing/queue-task-tools-contract.d.ts +33 -0
- package/dist/testing/queue-task-tools-contract.js +487 -0
- package/package.json +21 -5
- package/ui/area-nav.css +23 -13
- package/ui/area-nav.js +15 -1
- package/ui/judgment-history.js +0 -15
- package/ui/meeting-minutes.js +1 -1
- package/ui/world/world-canvas-ui.js +25 -5
- package/ui/world/world-view.css +58 -0
- package/ui/world/world-view.js +43 -14
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
2
|
+
import type { Tool, ServerRequest, ServerNotification } from '@modelcontextprotocol/sdk/types.js';
|
|
3
|
+
import type { LocalMcpRuntimeTool } from './mcp-runtime.js';
|
|
4
|
+
export declare const QUEUE_TASK_TOOLS_CONTRACT_VERSION: "queue_task_tools.v1";
|
|
5
|
+
export declare const QUEUE_TASK_TOOL_NAMES: readonly ["list_my_tasks", "get_task", "claim_task", "claim_next", "renew_claim", "attach_evidence", "update_status"];
|
|
6
|
+
export type QueueTaskToolName = (typeof QUEUE_TASK_TOOL_NAMES)[number];
|
|
7
|
+
export type QueueTaskStatus = 'pending' | 'in_progress' | 'waiting' | 'completed' | 'cancelled';
|
|
8
|
+
export type QueueTargetStatus = 'started' | 'waiting_for_review' | 'blocked' | 'done';
|
|
9
|
+
export type QueueEvidenceKind = 'pull_request' | 'ci' | 'production_readback';
|
|
10
|
+
export interface QueueAgentContext {
|
|
11
|
+
readonly kind: 'agent';
|
|
12
|
+
readonly agentId: string;
|
|
13
|
+
readonly credential: unknown;
|
|
14
|
+
}
|
|
15
|
+
export type QueueAgentResolver = (extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => QueueAgentContext | null | Promise<QueueAgentContext | null>;
|
|
16
|
+
type ResolvedKey = {
|
|
17
|
+
readonly idempotency_key: string;
|
|
18
|
+
};
|
|
19
|
+
export interface QueueListInput {
|
|
20
|
+
statuses?: QueueTaskStatus[];
|
|
21
|
+
limit?: number;
|
|
22
|
+
cursor?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface QueueGetInput {
|
|
25
|
+
task_id: string;
|
|
26
|
+
}
|
|
27
|
+
export type QueueClaimInput = ({
|
|
28
|
+
mode: 'next';
|
|
29
|
+
} | {
|
|
30
|
+
mode: 'by_id';
|
|
31
|
+
task_id: string;
|
|
32
|
+
}) & ResolvedKey;
|
|
33
|
+
export interface QueueRenewInput extends ResolvedKey {
|
|
34
|
+
task_id: string;
|
|
35
|
+
}
|
|
36
|
+
export interface QueueEvidenceInput extends ResolvedKey {
|
|
37
|
+
task_id: string;
|
|
38
|
+
expected_version: number;
|
|
39
|
+
kind: QueueEvidenceKind;
|
|
40
|
+
ref: Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
export interface QueueStatusInput extends ResolvedKey {
|
|
43
|
+
task_id: string;
|
|
44
|
+
expected_version: number;
|
|
45
|
+
to_status: QueueTargetStatus;
|
|
46
|
+
waiting_on?: string;
|
|
47
|
+
}
|
|
48
|
+
export interface QueueTask {
|
|
49
|
+
id: string;
|
|
50
|
+
version: number;
|
|
51
|
+
status: QueueTaskStatus;
|
|
52
|
+
[field: string]: unknown;
|
|
53
|
+
}
|
|
54
|
+
export interface QueueTaskPage {
|
|
55
|
+
items: QueueTask[];
|
|
56
|
+
next_cursor: string | null;
|
|
57
|
+
[field: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
export interface QueueTaskRead {
|
|
60
|
+
task: QueueTask;
|
|
61
|
+
[field: string]: unknown;
|
|
62
|
+
}
|
|
63
|
+
export type QueueClaimResult = {
|
|
64
|
+
status: 'claimed';
|
|
65
|
+
task: QueueTask;
|
|
66
|
+
claimed_until: string;
|
|
67
|
+
replayed?: boolean;
|
|
68
|
+
renew_interval_minutes?: number;
|
|
69
|
+
[field: string]: unknown;
|
|
70
|
+
} | {
|
|
71
|
+
status: 'no_claimable_task';
|
|
72
|
+
[field: string]: unknown;
|
|
73
|
+
};
|
|
74
|
+
export interface QueueRenewResult {
|
|
75
|
+
task: QueueTask;
|
|
76
|
+
claimed_until: string;
|
|
77
|
+
[field: string]: unknown;
|
|
78
|
+
}
|
|
79
|
+
export type QueueWriteOutcome = 'not_sent' | 'unknown';
|
|
80
|
+
export interface QueueTaskError {
|
|
81
|
+
code: string;
|
|
82
|
+
message: string;
|
|
83
|
+
http_status?: number;
|
|
84
|
+
details?: Record<string, unknown>;
|
|
85
|
+
}
|
|
86
|
+
export type QueueTaskResult<T> = {
|
|
87
|
+
status: 'ok';
|
|
88
|
+
queue: T;
|
|
89
|
+
} | {
|
|
90
|
+
status: 'error';
|
|
91
|
+
error: QueueTaskError;
|
|
92
|
+
} | {
|
|
93
|
+
status: 'unavailable';
|
|
94
|
+
error: QueueTaskError;
|
|
95
|
+
};
|
|
96
|
+
export interface QueueTaskBackend {
|
|
97
|
+
list(context: QueueAgentContext, input: QueueListInput): Promise<QueueTaskResult<QueueTaskPage>>;
|
|
98
|
+
get(context: QueueAgentContext, input: QueueGetInput): Promise<QueueTaskResult<QueueTaskRead>>;
|
|
99
|
+
claim(context: QueueAgentContext, input: QueueClaimInput): Promise<QueueTaskResult<QueueClaimResult>>;
|
|
100
|
+
renew(context: QueueAgentContext, input: QueueRenewInput): Promise<QueueTaskResult<QueueRenewResult>>;
|
|
101
|
+
evidence(context: QueueAgentContext, input: QueueEvidenceInput): Promise<QueueTaskResult<QueueTaskRead>>;
|
|
102
|
+
status(context: QueueAgentContext, input: QueueStatusInput): Promise<QueueTaskResult<QueueTaskRead>>;
|
|
103
|
+
}
|
|
104
|
+
export interface CreateQueueTaskToolsOptions {
|
|
105
|
+
backend?: QueueTaskBackend;
|
|
106
|
+
resolveAgent?: QueueAgentResolver;
|
|
107
|
+
generateKey?: () => string;
|
|
108
|
+
}
|
|
109
|
+
export declare class QueueTaskToolsConfigError extends Error {
|
|
110
|
+
readonly code = "queue_task_tools_config_invalid";
|
|
111
|
+
}
|
|
112
|
+
export declare const queueTaskToolDefinitions: readonly Tool[];
|
|
113
|
+
export declare function createQueueTaskTools(options: CreateQueueTaskToolsOptions): readonly LocalMcpRuntimeTool[];
|
|
114
|
+
export {};
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
export const QUEUE_TASK_TOOLS_CONTRACT_VERSION = 'queue_task_tools.v1';
|
|
4
|
+
export const QUEUE_TASK_TOOL_NAMES = [
|
|
5
|
+
'list_my_tasks',
|
|
6
|
+
'get_task',
|
|
7
|
+
'claim_task',
|
|
8
|
+
'claim_next',
|
|
9
|
+
'renew_claim',
|
|
10
|
+
'attach_evidence',
|
|
11
|
+
'update_status',
|
|
12
|
+
]; // AC-1 / AC-9 はこの順序で assert する
|
|
13
|
+
export class QueueTaskToolsConfigError extends Error {
|
|
14
|
+
code = 'queue_task_tools_config_invalid';
|
|
15
|
+
}
|
|
16
|
+
const statuses = ['pending', 'in_progress', 'waiting', 'completed', 'cancelled'];
|
|
17
|
+
const targets = ['started', 'waiting_for_review', 'blocked', 'done'];
|
|
18
|
+
const kinds = ['pull_request', 'ci', 'production_readback'];
|
|
19
|
+
const text = (max) => z.string().refine((s) => !!s.trim() && [...s].length <= max);
|
|
20
|
+
const key = z
|
|
21
|
+
.string()
|
|
22
|
+
.refine((s) => !!s.trim() && !/^(api:|workflow:)/.test(s))
|
|
23
|
+
.optional();
|
|
24
|
+
const task_id = text(256);
|
|
25
|
+
const expected_version = z.number().int().min(1);
|
|
26
|
+
const str = { type: 'string' };
|
|
27
|
+
const id = { ...str, minLength: 1, maxLength: 256 };
|
|
28
|
+
const version = { type: 'integer', minimum: 1 };
|
|
29
|
+
const toolConfigs = {
|
|
30
|
+
list_my_tasks: {
|
|
31
|
+
schema: z.strictObject({
|
|
32
|
+
statuses: z.array(z.enum(statuses)).optional(),
|
|
33
|
+
limit: z.number().int().min(1).max(50).optional(),
|
|
34
|
+
cursor: z.string().optional(),
|
|
35
|
+
}),
|
|
36
|
+
properties: {
|
|
37
|
+
statuses: { type: 'array', items: { type: 'string', enum: statuses } },
|
|
38
|
+
limit: { type: 'integer', minimum: 1, maximum: 50 },
|
|
39
|
+
cursor: str,
|
|
40
|
+
},
|
|
41
|
+
required: [],
|
|
42
|
+
write: false,
|
|
43
|
+
description: 'List tasks visible to the authenticated agent.',
|
|
44
|
+
execute: (backend, context, input) => backend.list(context, input),
|
|
45
|
+
},
|
|
46
|
+
get_task: {
|
|
47
|
+
schema: z.strictObject({ task_id }),
|
|
48
|
+
properties: { task_id: id },
|
|
49
|
+
required: ['task_id'],
|
|
50
|
+
write: false,
|
|
51
|
+
description: 'Read a task visible to the authenticated agent.',
|
|
52
|
+
execute: (backend, context, input) => backend.get(context, input),
|
|
53
|
+
},
|
|
54
|
+
claim_task: {
|
|
55
|
+
schema: z.strictObject({ task_id, idempotency_key: key }),
|
|
56
|
+
properties: { task_id: id, idempotency_key: str },
|
|
57
|
+
required: ['task_id'],
|
|
58
|
+
write: true,
|
|
59
|
+
description: 'Claim a task by id. Supply and reuse an explicit idempotency_key when retrying the same operation.',
|
|
60
|
+
execute: (backend, context, input) => backend.claim(context, { ...input, mode: 'by_id' }),
|
|
61
|
+
},
|
|
62
|
+
claim_next: {
|
|
63
|
+
schema: z.strictObject({ idempotency_key: key }),
|
|
64
|
+
properties: { idempotency_key: str },
|
|
65
|
+
required: [],
|
|
66
|
+
write: true,
|
|
67
|
+
description: 'Claim the next available task. Supply and reuse an explicit idempotency_key when retrying the same operation.',
|
|
68
|
+
execute: (backend, context, input) => backend.claim(context, { ...input, mode: 'next' }),
|
|
69
|
+
},
|
|
70
|
+
renew_claim: {
|
|
71
|
+
schema: z.strictObject({ task_id, idempotency_key: key }),
|
|
72
|
+
properties: { task_id: id, idempotency_key: str },
|
|
73
|
+
required: ['task_id'],
|
|
74
|
+
write: true,
|
|
75
|
+
description: 'Renew an existing claim. Supply and reuse an explicit idempotency_key when retrying the same operation.',
|
|
76
|
+
execute: (backend, context, input) => backend.renew(context, input),
|
|
77
|
+
},
|
|
78
|
+
attach_evidence: {
|
|
79
|
+
schema: z.strictObject({
|
|
80
|
+
task_id,
|
|
81
|
+
expected_version,
|
|
82
|
+
kind: z.enum(kinds),
|
|
83
|
+
ref: z.record(z.string(), z.unknown()),
|
|
84
|
+
idempotency_key: key,
|
|
85
|
+
}),
|
|
86
|
+
properties: {
|
|
87
|
+
task_id: id,
|
|
88
|
+
expected_version: version,
|
|
89
|
+
kind: { type: 'string', enum: kinds },
|
|
90
|
+
ref: { type: 'object' },
|
|
91
|
+
idempotency_key: str,
|
|
92
|
+
},
|
|
93
|
+
required: ['task_id', 'expected_version', 'kind', 'ref'],
|
|
94
|
+
write: true,
|
|
95
|
+
description: 'Attach evidence to a claimed task. Supply and reuse an explicit idempotency_key when retrying the same operation.',
|
|
96
|
+
execute: (backend, context, input) => backend.evidence(context, input),
|
|
97
|
+
},
|
|
98
|
+
update_status: {
|
|
99
|
+
schema: z
|
|
100
|
+
.strictObject({
|
|
101
|
+
task_id,
|
|
102
|
+
expected_version,
|
|
103
|
+
to_status: z.enum(targets),
|
|
104
|
+
waiting_on: text(2000).optional(),
|
|
105
|
+
idempotency_key: key,
|
|
106
|
+
})
|
|
107
|
+
.refine((x) => (x.to_status === 'blocked') === (x.waiting_on !== undefined)),
|
|
108
|
+
properties: {
|
|
109
|
+
task_id: id,
|
|
110
|
+
expected_version: version,
|
|
111
|
+
to_status: { type: 'string', enum: targets },
|
|
112
|
+
waiting_on: { ...str, minLength: 1, maxLength: 2000 },
|
|
113
|
+
idempotency_key: str,
|
|
114
|
+
},
|
|
115
|
+
required: ['task_id', 'expected_version', 'to_status'],
|
|
116
|
+
write: true,
|
|
117
|
+
description: 'Update task status; close with to_status done. Supply and reuse an explicit idempotency_key when retrying the same operation.',
|
|
118
|
+
execute: (backend, context, input) => backend.status(context, input),
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
export const queueTaskToolDefinitions = QUEUE_TASK_TOOL_NAMES.map((name) => {
|
|
122
|
+
const config = toolConfigs[name];
|
|
123
|
+
return {
|
|
124
|
+
name,
|
|
125
|
+
description: config.description,
|
|
126
|
+
inputSchema: {
|
|
127
|
+
type: 'object',
|
|
128
|
+
properties: config.properties,
|
|
129
|
+
required: config.required,
|
|
130
|
+
additionalProperties: false,
|
|
131
|
+
},
|
|
132
|
+
annotations: {
|
|
133
|
+
readOnlyHint: !config.write,
|
|
134
|
+
destructiveHint: config.write,
|
|
135
|
+
idempotentHint: !config.write,
|
|
136
|
+
openWorldHint: true,
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
const object = (v) => !!v && typeof v === 'object' && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
|
|
141
|
+
const nonempty = (v) => typeof v === 'string' && !!v.trim();
|
|
142
|
+
const task = (v) => object(v) &&
|
|
143
|
+
nonempty(v.id) &&
|
|
144
|
+
Number.isInteger(v.version) &&
|
|
145
|
+
v.version >= 1 &&
|
|
146
|
+
statuses.includes(v.status);
|
|
147
|
+
// Public proposed minimum shape; actual API responses remain unverified.
|
|
148
|
+
function isJson(value, ancestors = new Set()) {
|
|
149
|
+
if (value === null || typeof value === 'string' || typeof value === 'boolean')
|
|
150
|
+
return true;
|
|
151
|
+
if (typeof value === 'number')
|
|
152
|
+
return Number.isFinite(value);
|
|
153
|
+
if ((!Array.isArray(value) && !object(value)) || ancestors.has(value))
|
|
154
|
+
return false;
|
|
155
|
+
ancestors.add(value);
|
|
156
|
+
const result = Object.values(value).every((child) => isJson(child, ancestors));
|
|
157
|
+
ancestors.delete(value);
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
function valid(v, name) {
|
|
161
|
+
if (!object(v) || !isJson(v))
|
|
162
|
+
return false;
|
|
163
|
+
if (v.status === 'error' || v.status === 'unavailable')
|
|
164
|
+
return (object(v.error) &&
|
|
165
|
+
nonempty(v.error.code) &&
|
|
166
|
+
typeof v.error.message === 'string' &&
|
|
167
|
+
(v.error.http_status === undefined || Number.isInteger(v.error.http_status)) &&
|
|
168
|
+
(v.error.details === undefined || object(v.error.details)) &&
|
|
169
|
+
(v.status !== 'unavailable' ||
|
|
170
|
+
['task_api_unavailable', 'task_auth_unavailable'].includes(v.error.code)) &&
|
|
171
|
+
!(toolConfigs[name].write &&
|
|
172
|
+
v.status === 'unavailable' &&
|
|
173
|
+
!['not_sent', 'unknown'].includes(v.error.details?.outcome)));
|
|
174
|
+
if (v.status !== 'ok' || !object(v.queue))
|
|
175
|
+
return false;
|
|
176
|
+
const q = v.queue;
|
|
177
|
+
if (name === 'list_my_tasks')
|
|
178
|
+
return (Array.isArray(q.items) &&
|
|
179
|
+
q.items.every(task) &&
|
|
180
|
+
(q.next_cursor === null || typeof q.next_cursor === 'string'));
|
|
181
|
+
if (name === 'claim_task' || name === 'claim_next')
|
|
182
|
+
return (q.status === 'no_claimable_task' ||
|
|
183
|
+
(q.status === 'claimed' &&
|
|
184
|
+
task(q.task) &&
|
|
185
|
+
nonempty(q.claimed_until) &&
|
|
186
|
+
(q.replayed === undefined || typeof q.replayed === 'boolean') &&
|
|
187
|
+
(q.renew_interval_minutes === undefined ||
|
|
188
|
+
(typeof q.renew_interval_minutes === 'number' &&
|
|
189
|
+
Number.isFinite(q.renew_interval_minutes) &&
|
|
190
|
+
q.renew_interval_minutes > 0))));
|
|
191
|
+
return task(q.task) && (name !== 'renew_claim' || nonempty(q.claimed_until));
|
|
192
|
+
}
|
|
193
|
+
function failure(status, code, write = false, outcome = 'unknown') {
|
|
194
|
+
return { status, error: { code, message: code, ...(write ? { details: { outcome } } : {}) } };
|
|
195
|
+
}
|
|
196
|
+
function encode(result) {
|
|
197
|
+
return {
|
|
198
|
+
content: [{ type: 'text', text: JSON.stringify(result) }],
|
|
199
|
+
...(result.status === 'ok' ? {} : { isError: true }),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
export function createQueueTaskTools(options) {
|
|
203
|
+
const backend = options.backend;
|
|
204
|
+
if (!backend)
|
|
205
|
+
return [];
|
|
206
|
+
if (typeof options.resolveAgent !== 'function')
|
|
207
|
+
throw new QueueTaskToolsConfigError('Agent resolver required');
|
|
208
|
+
return queueTaskToolDefinitions.map((definition) => {
|
|
209
|
+
const name = definition.name;
|
|
210
|
+
const config = toolConfigs[name];
|
|
211
|
+
return {
|
|
212
|
+
definition: structuredClone(definition),
|
|
213
|
+
handler: async (args, extra) => {
|
|
214
|
+
const parsed = config.schema.safeParse(args);
|
|
215
|
+
if (!parsed.success)
|
|
216
|
+
return encode(failure('error', 'task_input_invalid'));
|
|
217
|
+
let context;
|
|
218
|
+
try {
|
|
219
|
+
context = await options.resolveAgent(extra);
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
context = null;
|
|
223
|
+
}
|
|
224
|
+
if (!context ||
|
|
225
|
+
context.kind !== 'agent' ||
|
|
226
|
+
!nonempty(context.agentId) ||
|
|
227
|
+
context.credential === null ||
|
|
228
|
+
context.credential === undefined ||
|
|
229
|
+
(typeof context.credential === 'string' && !context.credential.trim()))
|
|
230
|
+
return encode(failure('unavailable', 'task_auth_unavailable', config.write, 'not_sent'));
|
|
231
|
+
const input = { ...parsed.data };
|
|
232
|
+
if (config.write && input.idempotency_key === undefined) {
|
|
233
|
+
try {
|
|
234
|
+
const opaque = (options.generateKey ?? randomUUID)();
|
|
235
|
+
if (typeof opaque !== 'string' || !/^[A-Za-z0-9_-]+$/.test(opaque))
|
|
236
|
+
throw new Error();
|
|
237
|
+
input.idempotency_key = `mcp:${opaque}`;
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return encode(failure('error', 'task_contract_error', true, 'not_sent'));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
let result;
|
|
244
|
+
try {
|
|
245
|
+
result = await config.execute(backend, context, input);
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
return encode(failure('unavailable', 'task_api_unavailable', config.write));
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
if (valid(result, name) &&
|
|
252
|
+
!(typeof context.credential === 'string' &&
|
|
253
|
+
JSON.stringify(result).includes(JSON.stringify(context.credential).slice(1, -1))))
|
|
254
|
+
return encode(result);
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
/* malformed/non-JSON result */
|
|
258
|
+
}
|
|
259
|
+
return encode(failure('error', 'task_contract_error', config.write));
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
});
|
|
263
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { QueueTaskBackend, QueueTask } from '../queue-task-tools.js';
|
|
2
|
+
export declare const QUEUE_REFERENCE_CODES: Record<"agent_scope_denied" | "task_not_found" | "claim_not_held" | "version_conflict" | "completion_gate_unmet" | "task_already_completed" | "idempotency_conflict", string>;
|
|
3
|
+
export interface QueueReferenceState {
|
|
4
|
+
tasks: (QueueTask & {
|
|
5
|
+
assignee: string;
|
|
6
|
+
title: string;
|
|
7
|
+
})[];
|
|
8
|
+
assignable: Record<string, string[]>;
|
|
9
|
+
claims: Record<string, {
|
|
10
|
+
agent: string;
|
|
11
|
+
claimed_until: string;
|
|
12
|
+
}>;
|
|
13
|
+
evidence: {
|
|
14
|
+
task_id: string;
|
|
15
|
+
kind: string;
|
|
16
|
+
ref: Record<string, unknown>;
|
|
17
|
+
}[];
|
|
18
|
+
completions: string[];
|
|
19
|
+
audit: string[];
|
|
20
|
+
calls: {
|
|
21
|
+
operation: string;
|
|
22
|
+
agentId: string;
|
|
23
|
+
credentialMatchesAgent: boolean;
|
|
24
|
+
input: Record<string, any>;
|
|
25
|
+
}[];
|
|
26
|
+
operations: Record<string, {
|
|
27
|
+
fingerprint: string;
|
|
28
|
+
result: any;
|
|
29
|
+
task_id?: string;
|
|
30
|
+
}>;
|
|
31
|
+
scripted: {
|
|
32
|
+
at: number;
|
|
33
|
+
type: 'result' | 'throw' | 'reject' | 'malformed';
|
|
34
|
+
result?: unknown;
|
|
35
|
+
}[];
|
|
36
|
+
}
|
|
37
|
+
export declare function createQueueReferenceSeed(): QueueReferenceState;
|
|
38
|
+
export declare function createQueueReferenceBackend(initial: QueueReferenceState, options?: {
|
|
39
|
+
expectedAgentCredentials?: Record<string, string>;
|
|
40
|
+
onChange?: (state: QueueReferenceState) => void;
|
|
41
|
+
}): QueueTaskBackend & {
|
|
42
|
+
snapshot(): QueueReferenceState;
|
|
43
|
+
};
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
export const QUEUE_REFERENCE_CODES = Object.fromEntries([
|
|
2
|
+
'agent_scope_denied',
|
|
3
|
+
'task_not_found',
|
|
4
|
+
'claim_not_held',
|
|
5
|
+
'version_conflict',
|
|
6
|
+
'completion_gate_unmet',
|
|
7
|
+
'task_already_completed',
|
|
8
|
+
'idempotency_conflict',
|
|
9
|
+
].map((x) => [x, x]));
|
|
10
|
+
export function createQueueReferenceSeed() {
|
|
11
|
+
return {
|
|
12
|
+
tasks: [1, 2, 3, 4].map((n) => ({
|
|
13
|
+
id: `t${n}`,
|
|
14
|
+
version: 1,
|
|
15
|
+
status: 'pending',
|
|
16
|
+
assignee: n === 4 ? 'B' : 'A',
|
|
17
|
+
title: `synthetic-task-${n}`,
|
|
18
|
+
})),
|
|
19
|
+
assignable: {},
|
|
20
|
+
claims: {},
|
|
21
|
+
evidence: [],
|
|
22
|
+
completions: [],
|
|
23
|
+
audit: [],
|
|
24
|
+
calls: [],
|
|
25
|
+
operations: {},
|
|
26
|
+
scripted: [],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const canonical = (v) => JSON.stringify(v && typeof v === 'object'
|
|
30
|
+
? Array.isArray(v)
|
|
31
|
+
? v.map((x) => JSON.parse(canonical(x)))
|
|
32
|
+
: Object.fromEntries(Object.keys(v)
|
|
33
|
+
.sort()
|
|
34
|
+
.map((k) => [k, JSON.parse(canonical(v[k]))]))
|
|
35
|
+
: v);
|
|
36
|
+
export function createQueueReferenceBackend(initial, options = {}) {
|
|
37
|
+
const state = structuredClone(initial);
|
|
38
|
+
const snapshot = () => structuredClone(state);
|
|
39
|
+
const notify = () => options.onChange?.(snapshot());
|
|
40
|
+
const error = (code) => ({ status: 'error', error: { code, message: code } });
|
|
41
|
+
const ok = (queue) => ({ status: 'ok', queue: structuredClone(queue) });
|
|
42
|
+
const visible = (t, a) => t.assignee === a || state.assignable[t.id]?.includes(a);
|
|
43
|
+
async function execute(operation, context, input) {
|
|
44
|
+
const agent = context.agentId;
|
|
45
|
+
state.calls.push({
|
|
46
|
+
operation,
|
|
47
|
+
agentId: agent,
|
|
48
|
+
credentialMatchesAgent: Object.hasOwn(options.expectedAgentCredentials ?? {}, agent) &&
|
|
49
|
+
context.credential === options.expectedAgentCredentials?.[agent],
|
|
50
|
+
input: structuredClone(input),
|
|
51
|
+
});
|
|
52
|
+
try {
|
|
53
|
+
const script = state.scripted.find((x) => x.at === state.calls.length);
|
|
54
|
+
if (script) {
|
|
55
|
+
if (script.type === 'throw' || script.type === 'reject')
|
|
56
|
+
throw new Error('exception-sentinel');
|
|
57
|
+
return structuredClone(script.result);
|
|
58
|
+
}
|
|
59
|
+
if (operation === 'list') {
|
|
60
|
+
const items = state.tasks.filter((t) => visible(t, agent) && (!input.statuses || input.statuses.includes(t.status)));
|
|
61
|
+
const offset = input.cursor ? Number(input.cursor.slice('page/opaque:'.length)) : 0;
|
|
62
|
+
const limit = input.limit ?? 50;
|
|
63
|
+
return ok({
|
|
64
|
+
items: items.slice(offset, offset + limit),
|
|
65
|
+
next_cursor: offset + limit < items.length ? `page/opaque:${offset + limit}` : null,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const namespace = JSON.stringify([agent, input.idempotency_key]);
|
|
69
|
+
const fingerprint = canonical({ operation, input });
|
|
70
|
+
const prior = input.idempotency_key ? state.operations[namespace] : undefined;
|
|
71
|
+
if (prior && prior.fingerprint !== fingerprint)
|
|
72
|
+
return error(QUEUE_REFERENCE_CODES.idempotency_conflict);
|
|
73
|
+
let taskId = input.task_id ?? prior?.task_id;
|
|
74
|
+
if (operation === 'claim' && input.mode === 'next' && !taskId)
|
|
75
|
+
taskId = state.tasks.find((t) => visible(t, agent) && !state.claims[t.id] && t.status !== 'completed')?.id;
|
|
76
|
+
if (operation === 'claim' && !taskId)
|
|
77
|
+
return ok({ status: 'no_claimable_task' });
|
|
78
|
+
const t = state.tasks.find((t) => t.id === taskId);
|
|
79
|
+
if (!t)
|
|
80
|
+
return error(QUEUE_REFERENCE_CODES.task_not_found);
|
|
81
|
+
if (!visible(t, agent))
|
|
82
|
+
return error(QUEUE_REFERENCE_CODES.agent_scope_denied);
|
|
83
|
+
if (operation === 'get')
|
|
84
|
+
return ok({ task: t });
|
|
85
|
+
const claim = state.claims[t.id];
|
|
86
|
+
if (operation !== 'claim' &&
|
|
87
|
+
(!claim ||
|
|
88
|
+
claim.agent !== agent ||
|
|
89
|
+
Date.parse(claim.claimed_until) <= Date.parse('2026-01-01T00:00:00Z')))
|
|
90
|
+
return error(QUEUE_REFERENCE_CODES.claim_not_held);
|
|
91
|
+
if (operation === 'claim' && claim && claim.agent !== agent)
|
|
92
|
+
return error(QUEUE_REFERENCE_CODES.claim_not_held);
|
|
93
|
+
if (prior) {
|
|
94
|
+
if (operation === 'claim' &&
|
|
95
|
+
(!claim || Date.parse(claim.claimed_until) <= Date.parse('2026-01-01T00:00:00Z')))
|
|
96
|
+
return error(QUEUE_REFERENCE_CODES.claim_not_held);
|
|
97
|
+
const result = structuredClone(prior.result);
|
|
98
|
+
if (operation === 'claim')
|
|
99
|
+
result.queue.replayed = true;
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
let result;
|
|
103
|
+
if (operation === 'claim') {
|
|
104
|
+
if (t.status === 'completed')
|
|
105
|
+
return error(QUEUE_REFERENCE_CODES.task_already_completed);
|
|
106
|
+
state.claims[t.id] = { agent, claimed_until: '2026-01-01T00:30:00.000Z' };
|
|
107
|
+
result = ok({
|
|
108
|
+
status: 'claimed',
|
|
109
|
+
task: t,
|
|
110
|
+
claimed_until: state.claims[t.id].claimed_until,
|
|
111
|
+
replayed: false,
|
|
112
|
+
renew_interval_minutes: 10,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
else if (operation === 'renew') {
|
|
116
|
+
claim.claimed_until = new Date(Date.parse(claim.claimed_until) + 600000).toISOString();
|
|
117
|
+
result = ok({ task: t, claimed_until: claim.claimed_until });
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
if (t.status === 'completed')
|
|
121
|
+
return error(QUEUE_REFERENCE_CODES.task_already_completed);
|
|
122
|
+
if (input.expected_version !== t.version)
|
|
123
|
+
return error(QUEUE_REFERENCE_CODES.version_conflict);
|
|
124
|
+
if (operation === 'evidence')
|
|
125
|
+
state.evidence.push({ task_id: t.id, kind: input.kind, ref: structuredClone(input.ref) });
|
|
126
|
+
else {
|
|
127
|
+
if (input.to_status === 'done' && !state.evidence.some((e) => e.task_id === t.id))
|
|
128
|
+
return error(QUEUE_REFERENCE_CODES.completion_gate_unmet);
|
|
129
|
+
t.status =
|
|
130
|
+
input.to_status === 'done'
|
|
131
|
+
? 'completed'
|
|
132
|
+
: input.to_status === 'started'
|
|
133
|
+
? 'in_progress'
|
|
134
|
+
: 'waiting';
|
|
135
|
+
if (t.status === 'completed') {
|
|
136
|
+
state.completions.push(t.id);
|
|
137
|
+
state.audit.push(t.id);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
t.version++;
|
|
141
|
+
result = ok({ task: t });
|
|
142
|
+
}
|
|
143
|
+
state.operations[namespace] = { fingerprint, result: structuredClone(result), task_id: t.id };
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
finally {
|
|
147
|
+
notify();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
snapshot,
|
|
152
|
+
...Object.fromEntries(['list', 'get', 'claim', 'renew', 'evidence', 'status'].map((op) => [
|
|
153
|
+
op,
|
|
154
|
+
(context, input) => execute(op, context, input),
|
|
155
|
+
])),
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Tool, CallToolResult } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import { type QueueReferenceState } from './queue-task-reference-backend.js';
|
|
3
|
+
export declare const QUEUE_TASK_CONTRACT_SCENARIO_NAMES: readonly ["AC-2 reads only visible tasks and forwards opaque cursors one page per call", "AC-2 reads an approved task by id", "AC-3 binds every call and replay to the authenticated agent", "AC-4 claims next and renews without duplicate claims", "AC-4 claims an approved task by id without duplicates", "AC-5 attaches evidence once for the same operation", "AC-6 completes through queue status without bypassing gates", "AC-7 validates inputs before calling the queue backend", "AC-8 preserves structured failures without leaking credentials", "AC-9 advertises conservative queue tool annotations"];
|
|
4
|
+
export interface QueueTaskContractAssert {
|
|
5
|
+
equal(actual: unknown, expected: unknown, message?: string): void;
|
|
6
|
+
deepEqual(actual: unknown, expected: unknown, message?: string): void;
|
|
7
|
+
ok(value: unknown, message?: string): void;
|
|
8
|
+
}
|
|
9
|
+
export interface QueueTaskContractConnectOptions {
|
|
10
|
+
agent: {
|
|
11
|
+
agentId: string;
|
|
12
|
+
credential: string;
|
|
13
|
+
} | null;
|
|
14
|
+
sharedServiceToken?: string;
|
|
15
|
+
state: QueueReferenceState;
|
|
16
|
+
generatedKeys?: string[];
|
|
17
|
+
}
|
|
18
|
+
export interface QueueTaskContractConnection {
|
|
19
|
+
listTools(): Promise<Tool[]>;
|
|
20
|
+
callTool(name: string, args: Record<string, unknown>): Promise<CallToolResult>;
|
|
21
|
+
snapshot(): Promise<QueueReferenceState>;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export interface QueueTaskContractAdapter {
|
|
25
|
+
readonly transport: string;
|
|
26
|
+
connect(options: QueueTaskContractConnectOptions): Promise<QueueTaskContractConnection>;
|
|
27
|
+
}
|
|
28
|
+
export interface QueueTaskContractTranscript {
|
|
29
|
+
scenario: string;
|
|
30
|
+
steps: unknown[];
|
|
31
|
+
}
|
|
32
|
+
export { createQueueReferenceBackend, createQueueReferenceSeed, QUEUE_REFERENCE_CODES, type QueueReferenceState, } from './queue-task-reference-backend.js';
|
|
33
|
+
export declare function runQueueTaskContractScenario(name: (typeof QUEUE_TASK_CONTRACT_SCENARIO_NAMES)[number], adapter: QueueTaskContractAdapter, assert: QueueTaskContractAssert): Promise<QueueTaskContractTranscript>;
|