tam 1.0.14

A tasks manager cli tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/**
 * Pure core for the `dsh-tam` DSH plugin.
 *
 * No DSH imports here: this module owns the tam CLI runner and the tool specs,
 * so it can be unit-tested with plain Node before the plugin is loaded by DSH.
 */

import { execFile } from 'node:child_process';
import { createRequire } from 'node:module';
import { existsSync, realpathSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { promisify } from 'node:util';

const execFileAsync = promisify(execFile);

const MAX_BUFFER = 10 * 1024 * 1024;

/** Business error returned by the tam CLI (a JSON envelope on stderr). */
export class TamError extends Error {
  /**
   * @param {string} message
   * @param {{code?: string, exitCode?: number|null, details?: unknown}} [meta]
   */
  constructor(message, meta = {}) {
    super(message);
    this.name = 'TamError';
    this.code = meta.code ?? 'internal';
    this.exitCode = meta.exitCode ?? null;
    this.details = meta.details;
  }
}

/** Prefer the checkout's freshly built binary for local development. */
export function defaultTamCommand() {
  const exe = process.platform === 'win32' ? 'tam.exe' : 'tam';
  // `dsh plugin add` links this package; resolve symlinks so the checkout's
  // freshly built binary is found in local development.
  const here = dirname(realpathSync(fileURLToPath(import.meta.url)));
  for (const relative of [
    `../../target/debug/${exe}`,
    `../../target/release/${exe}`,
  ]) {
    const candidate = join(here, relative);
    if (existsSync(candidate)) return candidate;
  }
  return 'tam';
}

const require = createRequire(import.meta.url);

/**
 * If the plugin was installed from npm, cargo-dist's `@wst7/tam` package is an
 * optional dependency. Its bin is a launcher that downloads the platform binary
 * on first use; run it through Node so the plugin does not depend on a `tam`
 * shim being on PATH.
 */
function npmTamLauncher() {
  try {
    const launcher = require.resolve('@wst7/tam/run-tam.js');
    // DSH runs inside Electron; ELECTRON_RUN_AS_NODE makes its process.execPath
    // behave as Node for the cargo-dist launcher. Harmless under plain Node.
    return {
      command: process.execPath,
      prefixArgs: [launcher],
      env: { ELECTRON_RUN_AS_NODE: '1' },
    };
  } catch {
    return null;
  }
}

/** Resolve the command used to run tam, in priority order. */
export function resolveTamCommand(config = {}) {
  if (config.command) return { command: config.command, prefixArgs: [] };
  if (process.env.TAM_COMMAND) {
    return { command: process.env.TAM_COMMAND, prefixArgs: [] };
  }
  const npm = npmTamLauncher();
  if (npm) return npm;
  const local = defaultTamCommand();
  return { command: local, prefixArgs: [] };
}

function parseEnvelope(text) {
  const trimmed = text.trim();
  if (!trimmed) return null;
  // stderr may contain a logger line before the envelope; try each line.
  const lines = trimmed.split(/\r?\n/);
  for (let index = lines.length - 1; index >= 0; index -= 1) {
    const line = lines[index].trim();
    if (!line.startsWith('{')) continue;
    try {
      return JSON.parse(line);
    } catch {
      // keep looking
    }
  }
  return null;
}

/**
 * Create a runner bound to one tam binary and optional default context.
 *
 * @param {{
 *   command?: string,
 *   project?: string,
 *   agent?: string,
 *   home?: string,
 *   cwd?: string,
 * }} [config]
 */
export function createTamClient(config = {}) {
  const { command, prefixArgs, env: commandEnv } = resolveTamCommand(config);
  const defaultProject = config.project || process.env.TAM_PROJECT || '';
  const defaultAgent = config.agent || process.env.TAM_AGENT || 'dsh';
  const home = config.home;

  /**
   * Run one tam command in JSON mode and return its `data` payload.
   *
   * @param {string[]} args
   * @param {{project?: string, agent?: string, signal?: AbortSignal}} [options]
   */
  async function run(args, options = {}) {
    const project = options.project || defaultProject;
    const agent = options.agent || defaultAgent;
    const argv = ['--json'];
    if (project) argv.push('--project', project);
    if (agent) argv.push('--agent', agent);
    argv.push(...args);

    const env = { ...process.env, ...commandEnv };
    if (home) {
      env.HOME = home;
      env.USERPROFILE = home;
    }

    try {
      const { stdout } = await execFileAsync(command, [...prefixArgs, ...argv], {
        cwd: config.cwd,
        env,
        maxBuffer: MAX_BUFFER,
        signal: options.signal,
      });
      const envelope = parseEnvelope(stdout);
      if (!envelope) {
        throw new TamError(`tam produced no JSON output for \`${args.join(' ')}\``, {
          code: 'internal',
          exitCode: 1,
        });
      }
      if (envelope.ok !== true) {
        throw new TamError(
          `tam ${args.join(' ')} failed: ${envelope.error?.message ?? 'unknown error'}`,
          {
            code: envelope.error?.code ?? 'internal',
            exitCode: 1,
            details: envelope.error?.details,
          },
        );
      }
      return envelope.data;
    } catch (error) {
      if (error instanceof TamError) throw error;
      if (error?.name === 'AbortError') throw error;

      const exitCode = typeof error?.code === 'number' ? error.code : null;
      const stderr = error?.stderr?.toString?.() ?? '';
      const envelope = parseEnvelope(stderr);
      if (envelope?.error) {
        throw new TamError(
          `tam ${args.join(' ')} failed (exit ${exitCode ?? '?'}, ${envelope.error.code}): ${envelope.error.message}`,
          {
            code: envelope.error.code,
            exitCode,
            details: envelope.error.details,
          },
        );
      }
      throw new TamError(
        `failed to run tam (${command}): ${error?.message ?? String(error)}. ` +
          'Install a tam build with the agent JSON contract (tam >= 1.0.14), or set ' +
          'config.command / TAM_COMMAND to a compatible binary.' +
          (stderr ? `\n${stderr.trim()}` : ''),
        { code: 'internal', exitCode },
      );
    }
  }

  return { run, command, defaultProject, defaultAgent };
}

// ---------------------------------------------------------------------------
// Tool specs
// ---------------------------------------------------------------------------

const objectSchema = { type: 'object', additionalProperties: true };
const arraySchema = {
  type: 'array',
  items: { type: 'object', additionalProperties: true },
};

const renderJson = (_args, value) => [
  { type: 'text', text: JSON.stringify(value, null, 2) },
];

/** Compile the small authoring DSL used below into a raw JSON Schema object. */
export function compileParameters(parameters) {
  const properties = {};
  const required = [];
  for (const [name, node] of Object.entries(parameters)) {
    const { required: isRequired, ...schema } = node;
    properties[name] = schema;
    if (isRequired) required.push(name);
  }
  return {
    type: 'object',
    properties,
    ...(required.length > 0 ? { required } : {}),
    additionalProperties: false,
  };
}

/** Minimal raw-schema validation for tools registered without `defineTool`. */
export function validateArguments(spec, args) {
  if (args === undefined || args === null) args = {};
  if (typeof args !== 'object' || Array.isArray(args)) {
    throw new TamError('tool arguments must be an object', { code: 'invalid' });
  }
  const schema = spec.parametersJsonSchema;
  const known = new Set(Object.keys(schema.properties ?? {}));
  for (const name of Object.keys(args)) {
    if (!known.has(name)) {
      throw new TamError(`unknown argument '${name}'`, { code: 'invalid' });
    }
  }
  for (const name of schema.required ?? []) {
    if (args[name] === undefined) {
      throw new TamError(`missing required argument '${name}'`, { code: 'invalid' });
    }
  }
  for (const [name, node] of Object.entries(schema.properties ?? {})) {
    const value = args[name];
    if (value === undefined) continue;
    if (node.enum && !node.enum.includes(value)) {
      throw new TamError(
        `argument '${name}' must be one of ${node.enum.join(', ')}`,
        { code: 'invalid' },
      );
    }
    switch (node.type) {
      case 'string':
        if (typeof value !== 'string') throw new TamError(`argument '${name}' must be a string`, { code: 'invalid' });
        break;
      case 'number':
      case 'integer':
        if (typeof value !== 'number' || !Number.isFinite(value)) {
          throw new TamError(`argument '${name}' must be a number`, { code: 'invalid' });
        }
        break;
      case 'boolean':
        if (typeof value !== 'boolean') throw new TamError(`argument '${name}' must be a boolean`, { code: 'invalid' });
        break;
      case 'array':
        if (!Array.isArray(value)) throw new TamError(`argument '${name}' must be an array`, { code: 'invalid' });
        if (node.items?.type === 'string' && value.some((item) => typeof item !== 'string')) {
          throw new TamError(`argument '${name}' must contain only strings`, { code: 'invalid' });
        }
        break;
      default:
        break;
    }
  }
  return args;
}

const PROJECT_PARAM = {
  type: 'string',
  description:
    'tam project name. Required when multiple projects exist; otherwise defaults to plugin config/TAM_PROJECT.',
};
const AGENT_PARAM = {
  type: 'string',
  description:
    'Agent identity for leases/events. Defaults to plugin config/TAM_AGENT/dsh; pass explicitly for multi-agent work.',
};

function taskId(args) {
  if (typeof args.id !== 'number' || !Number.isInteger(args.id) || args.id < 1) {
    throw new TamError('id must be a positive integer', { code: 'invalid' });
  }
  return String(args.id);
}

function pushFlag(argv, flag, value) {
  if (value === undefined || value === null || value === '') return;
  argv.push(flag, String(value));
}

function pushTags(argv, tags) {
  for (const tag of tags ?? []) {
    argv.push('--tag', String(tag));
  }
}

/**
 * One model-facing tool. `execute(args, exec, client)` returns the tam `data`
 * value; throwing makes the DSH tool result an error.
 */
function spec(name, description, parameters, outputSchema, execute) {
  return {
    name,
    description,
    parameters,
    parametersJsonSchema: compileParameters(parameters),
    outputSchema,
    render: renderJson,
    execute,
  };
}

export const toolSpecs = [
  spec(
    'tam_status',
    'Show task counts, leases, unassigned work, and next claimable tasks for a tam project. Pass project in multi-project setups.',
    { project: PROJECT_PARAM, agent: AGENT_PARAM },
    objectSchema,
    (args, exec, client) =>
      client.run(['status'], {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      }),
  ),
  spec(
    'tam_project_list',
    'List all active tam projects. Use this to discover project names before creating/listing tasks.',
    {},
    arraySchema,
    (_args, exec, client) => client.run(['project', 'list'], { signal: exec.signal }),
  ),
  spec(
    'tam_project_add',
    'Create a tam project (or restore a soft-deleted one).',
    {
      name: { type: 'string', required: true, description: 'New project name' },
    },
    objectSchema,
    (args, exec, client) =>
      client.run(['project', 'add', String(args.name)], { signal: exec.signal }),
  ),
  spec(
    'tam_task_list',
    'List tasks in a tam project with optional status/assignee/priority/tag/search/limit filters. Pass project in multi-project setups.',
    {
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
      status: {
        type: 'string',
        enum: ['todo', 'in-progress', 'done'],
        description: 'Only tasks with this status',
      },
      assigned: {
        type: 'string',
        description: '`me`, `unassigned`, or an agent id',
      },
      priority: { type: 'number', description: 'Only this priority (1 highest .. 5 lowest)' },
      tags: {
        type: 'array',
        items: { type: 'string' },
        description: 'Only tasks carrying all of these tags',
      },
      search: { type: 'string', description: 'Case-insensitive title/description substring' },
      limit: { type: 'number', description: 'Max tasks (default 100, max 1000)' },
    },
    objectSchema,
    (args, exec, client) => {
      const argv = ['task', 'list'];
      pushFlag(argv, '--status', args.status);
      pushFlag(argv, '--assigned', args.assigned);
      pushFlag(argv, '--priority', args.priority);
      pushTags(argv, args.tags);
      pushFlag(argv, '--search', args.search);
      pushFlag(argv, '--limit', args.limit);
      return client.run(argv, {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      });
    },
  ),
  spec(
    'tam_task_show',
    'Show one tam task with its audit events.',
    {
      id: { type: 'number', required: true, description: 'Task id' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
    },
    objectSchema,
    (args, exec, client) =>
      client.run(['task', 'show', taskId(args)], {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      }),
  ),
  spec(
    'tam_task_create',
    'Create a tam task. Use idempotency_key for safe retries; the CLI returns deduplicated:true when the key already exists.',
    {
      title: { type: 'string', required: true, description: 'Task title' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
      description: { type: 'string', description: 'Task description/context' },
      priority: { type: 'number', description: '1 highest .. 5 lowest (default 3)' },
      tags: {
        type: 'array',
        items: { type: 'string' },
        description: 'Tags (normalized lowercase)',
      },
      idempotency_key: {
        type: 'string',
        description: 'Project-scoped retry key; duplicate calls return the same task',
      },
    },
    objectSchema,
    (args, exec, client) => {
      const argv = ['task', 'add', String(args.title)];
      pushFlag(argv, '--description', args.description);
      pushFlag(argv, '--priority', args.priority);
      pushTags(argv, args.tags);
      pushFlag(argv, '--idempotency-key', args.idempotency_key);
      return client.run(argv, {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      });
    },
  ),
  spec(
    'tam_task_update',
    'Partially update a tam task (title/description/status/priority/tags). Only the lease holder or a human caller may update a leased task.',
    {
      id: { type: 'number', required: true, description: 'Task id' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
      title: { type: 'string' },
      description: { type: 'string' },
      status: {
        type: 'string',
        enum: ['todo', 'in-progress', 'done'],
        description: 'New status; todo/done clear the lease',
      },
      priority: { type: 'number', description: '1 highest .. 5 lowest' },
      tags: {
        type: 'array',
        items: { type: 'string' },
        description: 'Replace tags; pass [] to clear',
      },
    },
    objectSchema,
    (args, exec, client) => {
      const argv = ['task', 'update', taskId(args)];
      pushFlag(argv, '--title', args.title);
      pushFlag(argv, '--description', args.description);
      pushFlag(argv, '--status', args.status);
      pushFlag(argv, '--priority', args.priority);
      if (args.tags !== undefined) {
        argv.push('--tags', (args.tags ?? []).map(String).join(','));
      }
      return client.run(argv, {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      });
    },
  ),
  spec(
    'tam_task_claim',
    'Atomically claim a tam task with a lease. Another agent holding a valid lease causes a conflict (exit 4).',
    {
      id: { type: 'number', required: true, description: 'Task id' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
      lease: { type: 'number', description: 'Lease seconds (default 900, 5..86400)' },
      force: { type: 'boolean', description: 'Preempt another agent lease (recorded in events)' },
    },
    objectSchema,
    (args, exec, client) => {
      const argv = ['task', 'claim', taskId(args)];
      pushFlag(argv, '--lease', args.lease);
      if (args.force) argv.push('--force');
      return client.run(argv, {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      });
    },
  ),
  spec(
    'tam_task_heartbeat',
    'Renew the lease of a task held by the caller. Only the holder can heartbeat.',
    {
      id: { type: 'number', required: true, description: 'Task id' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
      lease: { type: 'number', description: 'New lease seconds (default 900)' },
    },
    objectSchema,
    (args, exec, client) => {
      const argv = ['task', 'heartbeat', taskId(args)];
      pushFlag(argv, '--lease', args.lease);
      return client.run(argv, {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      });
    },
  ),
  spec(
    'tam_task_release',
    'Release a task lease and return an in-progress task to todo.',
    {
      id: { type: 'number', required: true, description: 'Task id' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
      force: { type: 'boolean', description: 'Release even when another agent holds the lease' },
    },
    objectSchema,
    (args, exec, client) => {
      const argv = ['task', 'release', taskId(args)];
      if (args.force) argv.push('--force');
      return client.run(argv, {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      });
    },
  ),
  spec(
    'tam_task_complete',
    'Mark a task done, clear its lease, and append a completed event.',
    {
      id: { type: 'number', required: true, description: 'Task id' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
    },
    objectSchema,
    (args, exec, client) =>
      client.run(['task', 'done', taskId(args)], {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      }),
  ),
  spec(
    'tam_task_next',
    'Return the next claimable task (priority ascending, created ascending). Set claim=true to atomically claim it.',
    {
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
      claim: { type: 'boolean', description: 'Atomically claim the returned task' },
      lease: { type: 'number', description: 'Lease seconds when claiming (default 900)' },
    },
    objectSchema,
    (args, exec, client) => {
      const argv = ['task', 'next'];
      if (args.claim) argv.push('--claim');
      pushFlag(argv, '--lease', args.lease);
      return client.run(argv, {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      });
    },
  ),
  spec(
    'tam_task_events',
    'Show the audit event log for one task.',
    {
      id: { type: 'number', required: true, description: 'Task id' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
    },
    objectSchema,
    (args, exec, client) =>
      client.run(['task', 'events', taskId(args)], {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      }),
  ),
  spec(
    'tam_task_delete',
    'Soft-delete a task and append a deleted event.',
    {
      id: { type: 'number', required: true, description: 'Task id' },
      project: PROJECT_PARAM,
      agent: AGENT_PARAM,
    },
    objectSchema,
    (args, exec, client) =>
      client.run(['task', 'del', taskId(args)], {
        project: args.project,
        agent: args.agent,
        signal: exec.signal,
      }),
  ),
];