@1agents/acp-service 0.8.0 → 0.8.1
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/README.md +5 -0
- package/dist/bin/acp-service.js +0 -0
- package/dist/src/a2a/executor.js +34 -9
- package/dist/src/a2a/index.d.ts +1 -0
- package/dist/src/a2a/index.js +1 -0
- package/dist/src/a2a/lease.d.ts +13 -0
- package/dist/src/a2a/lease.js +149 -0
- package/dist/src/a2a/request-id.d.ts +2 -0
- package/dist/src/a2a/request-id.js +6 -0
- package/dist/src/a2a/server.js +13 -1
- package/dist/src/a2a/store.d.ts +4 -6
- package/dist/src/a2a/store.js +14 -114
- package/dist/src/a2a/types.d.ts +9 -0
- package/dist/src/acp-connection.d.ts +2 -1
- package/dist/src/acp-connection.js +52 -9
- package/dist/src/bridge.js +38 -39
- package/package.json +3 -2
- package/vendor/dsh/README.md +12 -2
- package/vendor/dsh/a2a-executor.d.ts +7 -1
- package/vendor/dsh/a2a-executor.js +56 -15
- package/vendor/dsh/a2a.d.ts +2 -1
- package/vendor/dsh/a2a.js +9 -5
- package/vendor/dsh/adapter.d.ts +3 -1
- package/vendor/dsh/adapter.js +144 -16
- package/vendor/dsh/diagnostics.d.ts +42 -0
- package/vendor/dsh/diagnostics.js +176 -0
- package/vendor/dsh/index.d.ts +1 -0
- package/vendor/dsh/index.js +16 -4
- package/vendor/runtime/dist/{cli-DUHyAADv.js → cli-Bdzplbpj.js} +4 -4
- package/vendor/runtime/dist/{cli-DUHyAADv.js.map → cli-Bdzplbpj.js.map} +1 -1
- package/vendor/runtime/dist/cli.d.ts +1 -1
- package/vendor/runtime/dist/cli.js +8 -8
- package/vendor/runtime/dist/{client-DjkWknf7.d.ts → client-BXOiB0ND.d.ts} +2 -2
- package/vendor/runtime/dist/{client-DjkWknf7.d.ts.map → client-BXOiB0ND.d.ts.map} +1 -1
- package/vendor/runtime/dist/{flows-CiJSCy4D.js → flows-BKlAuzM8.js} +4 -4
- package/vendor/runtime/dist/{flows-CiJSCy4D.js.map → flows-BKlAuzM8.js.map} +1 -1
- package/vendor/runtime/dist/flows.d.ts +2 -2
- package/vendor/runtime/dist/flows.js +1 -1
- package/vendor/runtime/dist/{invocation-options-BlIySxIw.js → invocation-options-DMy77sEU.js} +4 -4
- package/vendor/runtime/dist/{invocation-options-BlIySxIw.js.map → invocation-options-DMy77sEU.js.map} +1 -1
- package/vendor/runtime/dist/{ipc-BlljKR2b.js → ipc-CLv2iL89.js} +260 -2
- package/vendor/runtime/dist/ipc-CLv2iL89.js.map +1 -0
- package/vendor/runtime/dist/{output-BzXQKArM.js → output-I-t-v55D.js} +3 -3
- package/vendor/runtime/dist/{output-BzXQKArM.js.map → output-I-t-v55D.js.map} +1 -1
- package/vendor/runtime/dist/{queue-owner-runtime-DSFdsS_y.js → queue-owner-runtime-RN3uSid0.js} +2 -2
- package/vendor/runtime/dist/{queue-owner-runtime-DSFdsS_y.js.map → queue-owner-runtime-RN3uSid0.js.map} +1 -1
- package/vendor/runtime/dist/runtime.d.ts +25 -15
- package/vendor/runtime/dist/runtime.d.ts.map +1 -1
- package/vendor/runtime/dist/runtime.js +57 -37
- package/vendor/runtime/dist/runtime.js.map +1 -1
- package/vendor/runtime/dist/{session-options-ORCchZGk.d.ts → session-options-DNnDfiUt.d.ts} +18 -2
- package/vendor/runtime/dist/session-options-DNnDfiUt.d.ts.map +1 -0
- package/vendor/runtime/dist/{session-watch-BiBTBMz4.js → session-watch-CmHbH0jS.js} +4 -4
- package/vendor/runtime/dist/{session-watch-BiBTBMz4.js.map → session-watch-CmHbH0jS.js.map} +1 -1
- package/vendor/runtime/dist/{watch-iYiP-lOo.js → watch-BE-kKa7o.js} +2 -2
- package/vendor/runtime/dist/{watch-iYiP-lOo.js.map → watch-BE-kKa7o.js.map} +1 -1
- package/vendor/runtime/skills/acpx/SKILL.md +1 -0
- package/vendor/runtime/dist/ipc-BlljKR2b.js.map +0 -1
- package/vendor/runtime/dist/session-options-ORCchZGk.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -340,3 +340,8 @@ To wake a main-agent host, configure `notificationTargets` as a map of trusted t
|
|
|
340
340
|
## 0.8.0 — DreamMate dependency transition
|
|
341
341
|
|
|
342
342
|
The public service now targets dreammate-node ^0.15.0 and dreammate-network ^0.5.0. The runtime and DSH plugin remain embedded in this one public package. Update consuming DSH profiles and service installations from the lockfile; do not substitute the older standalone dsh-acp release. This change does not alter stored agent sessions or configured credentials.
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
## 0.8.1 — preserve deployed diagnostics and cancellation fixes
|
|
346
|
+
|
|
347
|
+
Includes the already deployed c055869 branch: structured error provenance, confirmed/unconfirmed cancellation handling, journal ownership, and persisted current-turn diagnostics. This avoids regressing the ERP pilot during the DreamMate 0.15 dependency migration.
|
package/dist/bin/acp-service.js
CHANGED
|
File without changes
|
package/dist/src/a2a/executor.js
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
/** Official A2A executor with durable DSH context bindings and independent turns. */
|
|
2
|
-
import {
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
3
|
import { Role, Task, TaskState, Message } from '@a2a-js/sdk';
|
|
4
4
|
import { ContentTypeNotSupportedError, RequestMalformedError, UnsupportedOperationError } from '@a2a-js/sdk/errors';
|
|
5
5
|
import { AgentEvent, resolveUserScope } from '@a2a-js/sdk/server';
|
|
6
|
+
import { a2aRequestId } from './request-id.js';
|
|
7
|
+
import { toSafeError } from '@1agents/acp-service/runtime';
|
|
8
|
+
const FAILURE_MESSAGES = {
|
|
9
|
+
'missing-final-answer': 'DSH completed the task without a final answer; ACP final-answer metadata contained no final output',
|
|
10
|
+
'max-tokens': 'DSH reached the model output token limit',
|
|
11
|
+
forked: 'DSH task turn was closed by a session fork',
|
|
12
|
+
'unsupported-ending': 'DSH returned an unsupported turn ending',
|
|
13
|
+
discarded: 'DSH discarded the queued task before execution',
|
|
14
|
+
disposed: 'DSH session was disposed before the task finished',
|
|
15
|
+
aborted: 'DSH task execution was canceled',
|
|
16
|
+
interrupted: 'DSH task execution was interrupted',
|
|
17
|
+
};
|
|
6
18
|
/** Validate application metadata before the SDK starts an execution. */
|
|
7
19
|
export function parseA2AInput(request, options) {
|
|
8
20
|
const message = request.message;
|
|
@@ -61,11 +73,17 @@ export class A2ASessionExecutor {
|
|
|
61
73
|
const done = new Promise(resolve => { finish = resolve; });
|
|
62
74
|
this.active.set(context.taskId, { controller, done });
|
|
63
75
|
let published = false;
|
|
64
|
-
|
|
76
|
+
let target;
|
|
77
|
+
let returnedError;
|
|
78
|
+
let returnedFailureMessage;
|
|
79
|
+
// DSH deduplicates prompts by rpcId across the whole session. A Task can
|
|
80
|
+
// have several input-required turns, each with its own message identity.
|
|
81
|
+
const requestId = a2aRequestId(context.taskId, context.userMessage.messageId);
|
|
82
|
+
const status = (state, text, error) => {
|
|
65
83
|
bus.publish(AgentEvent.statusUpdate({ taskId: context.taskId, contextId: context.contextId,
|
|
66
84
|
status: { state, timestamp: new Date().toISOString(), message: text ? Message.fromJSON({
|
|
67
85
|
messageId: randomUUID(), role: 'ROLE_AGENT', contextId: context.contextId, taskId: context.taskId, parts: [{ text }],
|
|
68
|
-
}) : undefined }, metadata: undefined }));
|
|
86
|
+
}) : undefined }, metadata: error ? { '1agents': { ...target, error } } : undefined }));
|
|
69
87
|
};
|
|
70
88
|
try {
|
|
71
89
|
const { prompt, input } = parseA2AInput(context.request, this.options);
|
|
@@ -74,7 +92,6 @@ export class A2ASessionExecutor {
|
|
|
74
92
|
let unlock;
|
|
75
93
|
const lock = new Promise(resolve => { unlock = resolve; });
|
|
76
94
|
this.preparing.set(key, lock);
|
|
77
|
-
let target;
|
|
78
95
|
try {
|
|
79
96
|
await previous;
|
|
80
97
|
controller.signal.throwIfAborted();
|
|
@@ -96,10 +113,11 @@ export class A2ASessionExecutor {
|
|
|
96
113
|
published = true;
|
|
97
114
|
controller.signal.throwIfAborted();
|
|
98
115
|
status(TaskState.TASK_STATE_WORKING);
|
|
99
|
-
// DSH deduplicates prompts by rpcId across the whole session. A Task can
|
|
100
|
-
// have several input-required turns, each with its own message identity.
|
|
101
|
-
const requestId = `a2a-${createHash('sha256').update(JSON.stringify([context.taskId, context.userMessage.messageId])).digest('hex')}`;
|
|
102
116
|
const result = await this.backend.run(target, prompt, { requestId, signal: controller.signal });
|
|
117
|
+
returnedError = result.outcome === 'failed' && result.error !== undefined
|
|
118
|
+
? toSafeError(result.error, { origin: 'a2a', operationId: requestId }) : undefined;
|
|
119
|
+
returnedFailureMessage = result.outcome === 'failed' && typeof result.failureReason === 'string' && Object.hasOwn(FAILURE_MESSAGES, result.failureReason)
|
|
120
|
+
? FAILURE_MESSAGES[result.failureReason] : undefined;
|
|
103
121
|
controller.signal.throwIfAborted();
|
|
104
122
|
if (result.text)
|
|
105
123
|
bus.publish(AgentEvent.artifactUpdate({ taskId: context.taskId, contextId: context.contextId,
|
|
@@ -108,13 +126,20 @@ export class A2ASessionExecutor {
|
|
|
108
126
|
append: false, lastChunk: true, metadata: undefined }));
|
|
109
127
|
if (result.outcome === 'input-required')
|
|
110
128
|
this.awaitingInput.set(context.taskId, context.contextId);
|
|
111
|
-
|
|
129
|
+
const error = result.outcome === 'failed'
|
|
130
|
+
? toSafeError(returnedError, { origin: 'a2a', operationId: requestId }) : undefined;
|
|
131
|
+
status(result.outcome === 'completed' ? TaskState.TASK_STATE_COMPLETED : result.outcome === 'input-required' ? TaskState.TASK_STATE_INPUT_REQUIRED : TaskState.TASK_STATE_FAILED, returnedFailureMessage ?? error?.message ?? result.detail, error);
|
|
112
132
|
}
|
|
113
133
|
catch (error) {
|
|
114
134
|
if (!published)
|
|
115
135
|
bus.publish(AgentEvent.task(Task.fromJSON({ id: context.taskId, contextId: context.contextId,
|
|
116
136
|
status: { state: 'TASK_STATE_SUBMITTED', timestamp: new Date().toISOString() } })));
|
|
117
|
-
|
|
137
|
+
const projected = controller.signal.aborted && returnedError ? returnedError : toSafeError(error, { origin: 'a2a', operationId: requestId });
|
|
138
|
+
const cancellationOnly = controller.signal.aborted && !returnedError && error === controller.signal.reason;
|
|
139
|
+
const safe = toSafeError(cancellationOnly ? new DOMException('A2A task canceled', 'AbortError') : projected, {
|
|
140
|
+
origin: 'a2a', operationId: requestId, outcomeUnknown: projected.outcomeUnknown,
|
|
141
|
+
});
|
|
142
|
+
status(controller.signal.aborted ? TaskState.TASK_STATE_CANCELED : TaskState.TASK_STATE_FAILED, returnedFailureMessage ?? safe.message, safe);
|
|
118
143
|
}
|
|
119
144
|
finally {
|
|
120
145
|
this.active.delete(context.taskId);
|
package/dist/src/a2a/index.d.ts
CHANGED
|
@@ -7,3 +7,4 @@ export { RemoteAgentGateway, serveA2AGateway, REMOTE_AGENT_METHODS } from './gat
|
|
|
7
7
|
export type { A2AGatewayConfig, RemoteAgentNotice } from './gateway.js';
|
|
8
8
|
export { A2AAutomation } from './automation.js';
|
|
9
9
|
export type { A2AAutomationConfig, AutomationRun, AutomationRunStatus } from './automation.js';
|
|
10
|
+
export { DirectoryLease } from './lease.js';
|
package/dist/src/a2a/index.js
CHANGED
|
@@ -3,3 +3,4 @@ export { createA2AServer } from './server.js';
|
|
|
3
3
|
export { FileA2AStore } from './store.js';
|
|
4
4
|
export { RemoteAgentGateway, serveA2AGateway, REMOTE_AGENT_METHODS } from './gateway.js';
|
|
5
5
|
export { A2AAutomation } from './automation.js';
|
|
6
|
+
export { DirectoryLease } from './lease.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Reject a live owner; reclaim only uniquely named markers whose process is demonstrably absent. */
|
|
2
|
+
export declare class DirectoryLease {
|
|
3
|
+
private readonly directory;
|
|
4
|
+
private readonly lock;
|
|
5
|
+
private closed;
|
|
6
|
+
constructor(directory: string);
|
|
7
|
+
close(): void;
|
|
8
|
+
private acquireLock;
|
|
9
|
+
private ownerName;
|
|
10
|
+
private ownerPath;
|
|
11
|
+
private removeEmptyLockDirectory;
|
|
12
|
+
private readJson;
|
|
13
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/** Single-writer directory lease shared by A2A records and plugin diagnostics. */
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, rmdirSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { join, resolve } from 'node:path';
|
|
5
|
+
function record(value, label) {
|
|
6
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
7
|
+
throw new Error(`Invalid ${label}`);
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
/** Reject a live owner; reclaim only uniquely named markers whose process is demonstrably absent. */
|
|
11
|
+
export class DirectoryLease {
|
|
12
|
+
directory;
|
|
13
|
+
lock = { pid: process.pid, token: randomUUID() };
|
|
14
|
+
closed = false;
|
|
15
|
+
constructor(directory) {
|
|
16
|
+
this.directory = resolve(directory);
|
|
17
|
+
mkdirSync(this.directory, { recursive: true, mode: 0o700 });
|
|
18
|
+
const stat = lstatSync(this.directory);
|
|
19
|
+
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
20
|
+
throw new Error('Storage directory must be a real directory');
|
|
21
|
+
this.acquireLock();
|
|
22
|
+
}
|
|
23
|
+
close() {
|
|
24
|
+
if (this.closed)
|
|
25
|
+
return;
|
|
26
|
+
this.closed = true;
|
|
27
|
+
// The owner filename is unique to this instance. Never unlink a shared
|
|
28
|
+
// lock path: another process may have acquired it since our last check.
|
|
29
|
+
const path = this.ownerPath();
|
|
30
|
+
try {
|
|
31
|
+
const lock = this.readJson(path);
|
|
32
|
+
if (lock.token === this.lock.token && lock.pid === this.lock.pid)
|
|
33
|
+
unlinkSync(path);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
if (error.code !== 'ENOENT')
|
|
37
|
+
throw error;
|
|
38
|
+
}
|
|
39
|
+
this.removeEmptyLockDirectory();
|
|
40
|
+
}
|
|
41
|
+
acquireLock() {
|
|
42
|
+
const path = join(this.directory, '.lock');
|
|
43
|
+
for (let attempt = 0; attempt < 32; attempt++) {
|
|
44
|
+
try {
|
|
45
|
+
mkdirSync(path, { mode: 0o700 });
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error.code !== 'EEXIST')
|
|
49
|
+
throw error;
|
|
50
|
+
try {
|
|
51
|
+
const stat = lstatSync(path);
|
|
52
|
+
if (!stat.isDirectory() || stat.isSymbolicLink()) {
|
|
53
|
+
throw new Error('Legacy or invalid A2A .lock file; verify no instance is running before removing it');
|
|
54
|
+
}
|
|
55
|
+
const names = readdirSync(path);
|
|
56
|
+
for (const name of names) {
|
|
57
|
+
const lock = record(this.readJson(join(path, name)), 'storage lock');
|
|
58
|
+
if (!Number.isInteger(lock.pid) || lock.pid <= 0 ||
|
|
59
|
+
typeof lock.token !== 'string' || !/^[a-zA-Z0-9_-]{1,128}$/.test(lock.token) ||
|
|
60
|
+
name !== `owner-${lock.pid}-${lock.token}.json`) {
|
|
61
|
+
throw new Error('Invalid A2A storage lock; inspect the storage directory before removing it');
|
|
62
|
+
}
|
|
63
|
+
let dead = false;
|
|
64
|
+
try {
|
|
65
|
+
process.kill(lock.pid, 0);
|
|
66
|
+
}
|
|
67
|
+
catch (cause) {
|
|
68
|
+
dead = cause.code === 'ESRCH';
|
|
69
|
+
}
|
|
70
|
+
if (!dead)
|
|
71
|
+
throw new Error('A2A storage directory is already in use by another instance');
|
|
72
|
+
}
|
|
73
|
+
// Only remove the unique filenames observed above. A new live owner
|
|
74
|
+
// can never share them, so a delayed contender cannot delete its lock.
|
|
75
|
+
for (const name of names) {
|
|
76
|
+
try {
|
|
77
|
+
unlinkSync(join(path, name));
|
|
78
|
+
}
|
|
79
|
+
catch (cause) {
|
|
80
|
+
if (cause.code !== 'ENOENT')
|
|
81
|
+
throw cause;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this.removeEmptyLockDirectory();
|
|
85
|
+
}
|
|
86
|
+
catch (cause) {
|
|
87
|
+
if (cause.code !== 'ENOENT')
|
|
88
|
+
throw cause;
|
|
89
|
+
}
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
const fd = openSync(this.ownerPath(), 'wx', 0o600);
|
|
94
|
+
try {
|
|
95
|
+
writeFileSync(fd, JSON.stringify(this.lock));
|
|
96
|
+
fsyncSync(fd);
|
|
97
|
+
}
|
|
98
|
+
finally {
|
|
99
|
+
closeSync(fd);
|
|
100
|
+
}
|
|
101
|
+
// A competing recovery can replace an empty directory between mkdir
|
|
102
|
+
// and marker creation. If both contenders wrote into that replacement,
|
|
103
|
+
// only the sole remaining owner may return a successfully held lock.
|
|
104
|
+
const names = readdirSync(path);
|
|
105
|
+
if (names.length !== 1 || names[0] !== this.ownerName()) {
|
|
106
|
+
throw new Error('A2A storage directory is already in use by another instance');
|
|
107
|
+
}
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
try {
|
|
112
|
+
unlinkSync(this.ownerPath());
|
|
113
|
+
}
|
|
114
|
+
catch (cause) {
|
|
115
|
+
if (cause.code !== 'ENOENT')
|
|
116
|
+
throw cause;
|
|
117
|
+
}
|
|
118
|
+
this.removeEmptyLockDirectory();
|
|
119
|
+
if (error.code !== 'ENOENT')
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
throw new Error('Could not acquire A2A storage lock');
|
|
124
|
+
}
|
|
125
|
+
ownerName() { return `owner-${this.lock.pid}-${this.lock.token}.json`; }
|
|
126
|
+
ownerPath() { return join(this.directory, '.lock', this.ownerName()); }
|
|
127
|
+
removeEmptyLockDirectory() {
|
|
128
|
+
try {
|
|
129
|
+
rmdirSync(join(this.directory, '.lock'));
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
// A live marker makes rmdir fail; never recursively remove a lock directory.
|
|
133
|
+
if (!['ENOENT', 'ENOTEMPTY', 'EEXIST'].includes(error.code ?? ''))
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
readJson(path) {
|
|
138
|
+
const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
139
|
+
try {
|
|
140
|
+
const stat = fstatSync(fd);
|
|
141
|
+
if (!stat.isFile() || stat.size > 32 * 1024 * 1024)
|
|
142
|
+
throw new Error('Invalid A2A storage file');
|
|
143
|
+
return JSON.parse(readFileSync(fd, 'utf8'));
|
|
144
|
+
}
|
|
145
|
+
finally {
|
|
146
|
+
closeSync(fd);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Stable execution identity shared by live admission and read-only restart reconciliation. */
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
/** Bind one A2A task's user message, independent of transport connections. */
|
|
4
|
+
export function a2aRequestId(taskId, messageId) {
|
|
5
|
+
return `a2a-${createHash('sha256').update(JSON.stringify([taskId, messageId])).digest('hex')}`;
|
|
6
|
+
}
|
package/dist/src/a2a/server.js
CHANGED
|
@@ -4,6 +4,9 @@ import { AgentCard, SSE_HEADERS, TaskState, formatSSEEvent } from '@a2a-js/sdk';
|
|
|
4
4
|
import { PushNotificationNotSupportedError, RequestMalformedError, UnsupportedOperationError } from '@a2a-js/sdk/errors';
|
|
5
5
|
import { DefaultExecutionEventBusManager, DefaultRequestHandler, JsonRpcTransportHandler, ServerCallContext, V1PushNotificationSerializer, resolveUserScope, validateVersion } from '@a2a-js/sdk/server';
|
|
6
6
|
import { A2ASessionExecutor, parseA2AInput } from './executor.js';
|
|
7
|
+
import { Role } from '@a2a-js/sdk';
|
|
8
|
+
import { toSafeError } from '@1agents/acp-service/runtime';
|
|
9
|
+
import { a2aRequestId } from './request-id.js';
|
|
7
10
|
import { FileA2AStore } from './store.js';
|
|
8
11
|
function allowedCallback(config, origins) {
|
|
9
12
|
if (!origins.size)
|
|
@@ -191,7 +194,16 @@ export async function createA2AServer(options) {
|
|
|
191
194
|
skills: [{ id: 'dsh-session', name: 'DSH session task', description: 'Create or resume a session using metadata.1agents.cwd, workspace and agentPreset; retain contextId to continue collaboration.', tags: ['dsh', 'project', 'role', 'async'] }] });
|
|
192
195
|
const store = new FileA2AStore(options.stateDirectory);
|
|
193
196
|
try {
|
|
194
|
-
await store.recoverInterrupted()
|
|
197
|
+
await store.recoverInterrupted(async (task, context) => {
|
|
198
|
+
const message = task.history.findLast(message => message.role === Role.ROLE_USER);
|
|
199
|
+
const target = store.loadTarget(task.contextId, context);
|
|
200
|
+
if (!message || !target)
|
|
201
|
+
return;
|
|
202
|
+
const requestId = a2aRequestId(task.id, message.messageId);
|
|
203
|
+
const recovered = await options.backend.recover?.(target, { requestId });
|
|
204
|
+
return toSafeError(recovered, { origin: 'a2a', operationId: requestId,
|
|
205
|
+
outcomeUnknown: recovered === undefined });
|
|
206
|
+
});
|
|
195
207
|
}
|
|
196
208
|
catch (error) {
|
|
197
209
|
store.close();
|
package/dist/src/a2a/store.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { Task } from '@a2a-js/sdk';
|
|
1
2
|
import { ServerCallContext, type PushNotificationStore, type TaskStore } from '@a2a-js/sdk/server';
|
|
3
|
+
import { type SafeError } from '@1agents/acp-service/runtime';
|
|
2
4
|
import type { A2ATarget } from './types.js';
|
|
3
5
|
/**
|
|
4
6
|
* A single-process, durable store shared by A2A transports and DSH hosts.
|
|
@@ -9,7 +11,7 @@ export declare class FileA2AStore {
|
|
|
9
11
|
readonly taskStore: TaskStore;
|
|
10
12
|
readonly pushStore: PushNotificationStore;
|
|
11
13
|
private readonly directory;
|
|
12
|
-
private readonly
|
|
14
|
+
private readonly lease;
|
|
13
15
|
private readonly memory;
|
|
14
16
|
private readonly tasks;
|
|
15
17
|
private readonly pushes;
|
|
@@ -20,14 +22,10 @@ export declare class FileA2AStore {
|
|
|
20
22
|
loadTarget(contextId: string, context: ServerCallContext): A2ATarget | undefined;
|
|
21
23
|
saveTarget(contextId: string, target: A2ATarget, context: ServerCallContext): void;
|
|
22
24
|
/** Persist failure for interrupted executions. A restart never reruns work. */
|
|
23
|
-
recoverInterrupted(): Promise<void>;
|
|
25
|
+
recoverInterrupted(recover?: (task: Task, context: ServerCallContext) => Promise<SafeError | undefined>): Promise<void>;
|
|
24
26
|
close(): void;
|
|
25
27
|
private loadPush;
|
|
26
28
|
private assertOpen;
|
|
27
|
-
private acquireLock;
|
|
28
|
-
private ownerName;
|
|
29
|
-
private ownerPath;
|
|
30
|
-
private removeEmptyLockDirectory;
|
|
31
29
|
private readJson;
|
|
32
30
|
private readEnvelope;
|
|
33
31
|
private writeEnvelope;
|
package/dist/src/a2a/store.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
-
import { chmodSync, closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync,
|
|
2
|
+
import { chmodSync, closeSync, constants, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
3
3
|
import { isAbsolute, join, resolve } from 'node:path';
|
|
4
4
|
import { Message, Role, Task, TaskPushNotificationConfig, TaskState } from '@a2a-js/sdk';
|
|
5
5
|
import { InMemoryTaskStore, resolveUserScope, ServerCallContext, } from '@a2a-js/sdk/server';
|
|
6
6
|
import { RequestMalformedError } from '@a2a-js/sdk/errors';
|
|
7
|
+
import { toSafeError } from '@1agents/acp-service/runtime';
|
|
8
|
+
import { DirectoryLease } from './lease.js';
|
|
7
9
|
const terminalStates = new Set([
|
|
8
10
|
TaskState.TASK_STATE_COMPLETED, TaskState.TASK_STATE_FAILED,
|
|
9
11
|
TaskState.TASK_STATE_CANCELED, TaskState.TASK_STATE_REJECTED,
|
|
@@ -128,7 +130,7 @@ export class FileA2AStore {
|
|
|
128
130
|
taskStore;
|
|
129
131
|
pushStore;
|
|
130
132
|
directory;
|
|
131
|
-
|
|
133
|
+
lease;
|
|
132
134
|
memory = new InMemoryTaskStore();
|
|
133
135
|
tasks = new Map();
|
|
134
136
|
pushes = new Map();
|
|
@@ -142,7 +144,7 @@ export class FileA2AStore {
|
|
|
142
144
|
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
143
145
|
throw new Error('A2A storage directory must be a real directory');
|
|
144
146
|
chmodSync(this.directory, 0o700);
|
|
145
|
-
this.
|
|
147
|
+
this.lease = new DirectoryLease(this.directory);
|
|
146
148
|
try {
|
|
147
149
|
const loads = [];
|
|
148
150
|
for (const name of readdirSync(this.directory)) {
|
|
@@ -271,19 +273,25 @@ export class FileA2AStore {
|
|
|
271
273
|
this.targets.set(key(scope, contextId), normalized);
|
|
272
274
|
}
|
|
273
275
|
/** Persist failure for interrupted executions. A restart never reruns work. */
|
|
274
|
-
async recoverInterrupted() {
|
|
276
|
+
async recoverInterrupted(recover) {
|
|
275
277
|
this.assertOpen();
|
|
276
278
|
await this.ready;
|
|
277
279
|
for (const { scope, task } of [...this.tasks.values()]) {
|
|
278
280
|
if (task.status && terminalStates.has(task.status.state))
|
|
279
281
|
continue;
|
|
282
|
+
const recovered = await recover?.(task, contextOf(scope));
|
|
283
|
+
const previous = task.metadata?.['1agents'];
|
|
284
|
+
const metadata = recovered ? { ...task.metadata, '1agents': {
|
|
285
|
+
...(previous && typeof previous === 'object' && !Array.isArray(previous) ? previous : {}),
|
|
286
|
+
error: toSafeError(recovered, { origin: 'a2a' }),
|
|
287
|
+
} } : task.metadata;
|
|
280
288
|
const message = Message.fromJSON({
|
|
281
289
|
messageId: randomUUID(), contextId: task.contextId, taskId: task.id,
|
|
282
290
|
role: 'ROLE_AGENT',
|
|
283
291
|
parts: [{ text: 'Execution was interrupted by an A2A service restart and was not restarted.' }],
|
|
284
292
|
});
|
|
285
293
|
await this.taskStore.save({
|
|
286
|
-
...task,
|
|
294
|
+
...task, metadata,
|
|
287
295
|
status: { state: TaskState.TASK_STATE_FAILED, timestamp: new Date().toISOString(), message },
|
|
288
296
|
history: [...task.history, message],
|
|
289
297
|
}, contextOf(scope));
|
|
@@ -293,19 +301,7 @@ export class FileA2AStore {
|
|
|
293
301
|
if (this.closed)
|
|
294
302
|
return;
|
|
295
303
|
this.closed = true;
|
|
296
|
-
|
|
297
|
-
// lock path: another process may have acquired it since our last check.
|
|
298
|
-
const path = this.ownerPath();
|
|
299
|
-
try {
|
|
300
|
-
const lock = this.readJson(path);
|
|
301
|
-
if (lock.token === this.lock.token && lock.pid === this.lock.pid)
|
|
302
|
-
unlinkSync(path);
|
|
303
|
-
}
|
|
304
|
-
catch (error) {
|
|
305
|
-
if (error.code !== 'ENOENT')
|
|
306
|
-
throw error;
|
|
307
|
-
}
|
|
308
|
-
this.removeEmptyLockDirectory();
|
|
304
|
+
this.lease.close();
|
|
309
305
|
}
|
|
310
306
|
loadPush(taskId, context) {
|
|
311
307
|
this.assertOpen();
|
|
@@ -316,102 +312,6 @@ export class FileA2AStore {
|
|
|
316
312
|
if (this.closed)
|
|
317
313
|
throw new Error('A2A store is closed');
|
|
318
314
|
}
|
|
319
|
-
acquireLock() {
|
|
320
|
-
const path = join(this.directory, '.lock');
|
|
321
|
-
for (let attempt = 0; attempt < 32; attempt++) {
|
|
322
|
-
try {
|
|
323
|
-
mkdirSync(path, { mode: 0o700 });
|
|
324
|
-
}
|
|
325
|
-
catch (error) {
|
|
326
|
-
if (error.code !== 'EEXIST')
|
|
327
|
-
throw error;
|
|
328
|
-
try {
|
|
329
|
-
const stat = lstatSync(path);
|
|
330
|
-
if (!stat.isDirectory() || stat.isSymbolicLink()) {
|
|
331
|
-
throw new Error('Legacy or invalid A2A .lock file; verify no instance is running before removing it');
|
|
332
|
-
}
|
|
333
|
-
const names = readdirSync(path);
|
|
334
|
-
for (const name of names) {
|
|
335
|
-
const lock = record(this.readJson(join(path, name)), 'storage lock');
|
|
336
|
-
if (!Number.isInteger(lock.pid) || lock.pid <= 0 ||
|
|
337
|
-
typeof lock.token !== 'string' || !/^[a-zA-Z0-9_-]{1,128}$/.test(lock.token) ||
|
|
338
|
-
name !== `owner-${lock.pid}-${lock.token}.json`) {
|
|
339
|
-
throw new Error('Invalid A2A storage lock; inspect the storage directory before removing it');
|
|
340
|
-
}
|
|
341
|
-
let dead = false;
|
|
342
|
-
try {
|
|
343
|
-
process.kill(lock.pid, 0);
|
|
344
|
-
}
|
|
345
|
-
catch (cause) {
|
|
346
|
-
dead = cause.code === 'ESRCH';
|
|
347
|
-
}
|
|
348
|
-
if (!dead)
|
|
349
|
-
throw new Error('A2A storage directory is already in use by another instance');
|
|
350
|
-
}
|
|
351
|
-
// Only remove the unique filenames observed above. A new live owner
|
|
352
|
-
// can never share them, so a delayed contender cannot delete its lock.
|
|
353
|
-
for (const name of names) {
|
|
354
|
-
try {
|
|
355
|
-
unlinkSync(join(path, name));
|
|
356
|
-
}
|
|
357
|
-
catch (cause) {
|
|
358
|
-
if (cause.code !== 'ENOENT')
|
|
359
|
-
throw cause;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
this.removeEmptyLockDirectory();
|
|
363
|
-
}
|
|
364
|
-
catch (cause) {
|
|
365
|
-
if (cause.code !== 'ENOENT')
|
|
366
|
-
throw cause;
|
|
367
|
-
}
|
|
368
|
-
continue;
|
|
369
|
-
}
|
|
370
|
-
try {
|
|
371
|
-
const fd = openSync(this.ownerPath(), 'wx', 0o600);
|
|
372
|
-
try {
|
|
373
|
-
writeFileSync(fd, JSON.stringify(this.lock));
|
|
374
|
-
fsyncSync(fd);
|
|
375
|
-
}
|
|
376
|
-
finally {
|
|
377
|
-
closeSync(fd);
|
|
378
|
-
}
|
|
379
|
-
// A competing recovery can replace an empty directory between mkdir
|
|
380
|
-
// and marker creation. If both contenders wrote into that replacement,
|
|
381
|
-
// only the sole remaining owner may return a successfully held lock.
|
|
382
|
-
const names = readdirSync(path);
|
|
383
|
-
if (names.length !== 1 || names[0] !== this.ownerName()) {
|
|
384
|
-
throw new Error('A2A storage directory is already in use by another instance');
|
|
385
|
-
}
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
catch (error) {
|
|
389
|
-
try {
|
|
390
|
-
unlinkSync(this.ownerPath());
|
|
391
|
-
}
|
|
392
|
-
catch (cause) {
|
|
393
|
-
if (cause.code !== 'ENOENT')
|
|
394
|
-
throw cause;
|
|
395
|
-
}
|
|
396
|
-
this.removeEmptyLockDirectory();
|
|
397
|
-
if (error.code !== 'ENOENT')
|
|
398
|
-
throw error;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
throw new Error('Could not acquire A2A storage lock');
|
|
402
|
-
}
|
|
403
|
-
ownerName() { return `owner-${this.lock.pid}-${this.lock.token}.json`; }
|
|
404
|
-
ownerPath() { return join(this.directory, '.lock', this.ownerName()); }
|
|
405
|
-
removeEmptyLockDirectory() {
|
|
406
|
-
try {
|
|
407
|
-
rmdirSync(join(this.directory, '.lock'));
|
|
408
|
-
}
|
|
409
|
-
catch (error) {
|
|
410
|
-
// A live marker makes rmdir fail; never recursively remove a lock directory.
|
|
411
|
-
if (!['ENOENT', 'ENOTEMPTY', 'EEXIST'].includes(error.code ?? ''))
|
|
412
|
-
throw error;
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
315
|
readJson(path) {
|
|
416
316
|
const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
417
317
|
try {
|
package/dist/src/a2a/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/** Execution interfaces shared by DSH and future standalone A2A hosts. */
|
|
2
|
+
import type { SafeError } from '@1agents/acp-service/runtime';
|
|
2
3
|
export interface A2ATarget {
|
|
3
4
|
sessionId: string;
|
|
4
5
|
cwd: string;
|
|
@@ -15,9 +16,17 @@ export interface A2ARunResult {
|
|
|
15
16
|
text: string;
|
|
16
17
|
outcome: 'completed' | 'failed' | 'input-required';
|
|
17
18
|
detail?: string;
|
|
19
|
+
/** Bounded, safe failure context; execution state is independent of recovery policy. */
|
|
20
|
+
error?: SafeError;
|
|
21
|
+
/** Known host endings select a fixed explanation without forwarding raw detail. */
|
|
22
|
+
failureReason?: 'missing-final-answer' | 'max-tokens' | 'forked' | 'unsupported-ending' | 'discarded' | 'disposed' | 'aborted' | 'interrupted';
|
|
18
23
|
}
|
|
19
24
|
/** Implementations own session admission, execution and precise cancellation. */
|
|
20
25
|
export interface A2ABackend {
|
|
26
|
+
/** Optional read-only reconciliation; never resumes or repeats execution. */
|
|
27
|
+
recover?(target: A2ATarget, options: {
|
|
28
|
+
requestId: string;
|
|
29
|
+
}): Promise<SafeError | undefined>;
|
|
21
30
|
prepare(input: A2APrepareInput): Promise<A2ATarget>;
|
|
22
31
|
run(target: A2ATarget, prompt: string, options: {
|
|
23
32
|
requestId: string;
|
|
@@ -15,9 +15,10 @@ export declare function webSocketStream(ws: WebSocket): Stream;
|
|
|
15
15
|
/** A runtime subscriber owned by exactly one live ACP connection. */
|
|
16
16
|
export declare class RuntimePeer {
|
|
17
17
|
private readonly receive;
|
|
18
|
+
private readonly trustedRuntimeErrors;
|
|
18
19
|
readyState: number;
|
|
19
20
|
_acpxReqHost?: string;
|
|
20
|
-
constructor(receive: (event: RecordValue) => void);
|
|
21
|
+
constructor(receive: (event: RecordValue) => void, trustedRuntimeErrors?: boolean);
|
|
21
22
|
send(raw: string): void;
|
|
22
23
|
command(backend: RuntimeBackend, payload: RecordValue): Promise<RecordValue[]>;
|
|
23
24
|
}
|