evolcore 0.0.41-test.1 → 0.0.41
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/CHANGELOG.md +49 -36
- package/dist/agents/baseagent.js +68 -6
- package/dist/agents/codex-runner.js +62 -43
- package/dist/aun/aid/agentmd.js +98 -11
- package/dist/aun/aid/client.js +1 -1
- package/dist/aun/aid/control-aid.js +26 -2
- package/dist/aun/aid/identity.js +91 -0
- package/dist/aun/aid/index.js +1 -1
- package/dist/aun/aid/managed-operation.js +1 -1
- package/dist/aun/aid/ownership-operation.js +23 -22
- package/dist/aun/menu/client.js +27 -0
- package/dist/aun/msg/p2p.js +3 -8
- package/dist/aun/service-proxy.js +17 -0
- package/dist/channels/aun.js +321 -34
- package/dist/cli/agent-command.js +5 -2
- package/dist/cli/agent-create.js +991 -233
- package/dist/cli/agent.js +49 -10
- package/dist/cli/aid-bind-command.js +48 -19
- package/dist/cli/aun-commands.js +44 -16
- package/dist/cli/daemon-commands.js +1 -0
- package/dist/cli/index.js +24 -1
- package/dist/cli/init-channel.js +1 -1
- package/dist/cli/init.js +75 -1
- package/dist/cli/managed-command-guard.js +13 -1
- package/dist/cli/provider-command.js +57 -0
- package/dist/cli/restart-monitor.js +4 -2
- package/dist/cli/stats.js +10 -3
- package/dist/cli/task-context.js +19 -0
- package/dist/config/boot-log.js +3 -2
- package/dist/config/config-field-policy.js +12 -7
- package/dist/config/config-manager.js +138 -6
- package/dist/config/owner-config.js +2 -2
- package/dist/config/peer-role-resolver.js +2 -1
- package/dist/config/provider-ipc-service.js +132 -0
- package/dist/config/provider-menu-request.js +58 -0
- package/dist/config/providers-management.js +78 -5
- package/dist/config/resolved-config-op.js +8 -1
- package/dist/config/schema-migration.js +322 -8
- package/dist/config-store.js +10 -1
- package/dist/contracts/ecweb-service-protocol.js +2 -1
- package/dist/core/agent-application-service.js +60 -11
- package/dist/core/agent-create-status.js +203 -0
- package/dist/core/agent-creation-claim.js +177 -0
- package/dist/core/agent-ipc-registration.js +39 -18
- package/dist/core/agent-runtime.js +193 -23
- package/dist/core/auth/agent-delegation.js +4 -2
- package/dist/core/auth/auth-gateway.js +66 -1
- package/dist/core/auth/authorization-audit.js +41 -5
- package/dist/core/auth/delegation-authorization.js +2 -1
- package/dist/core/auth/operation-authorizer.js +28 -6
- package/dist/core/auth/operation-catalog.js +14 -1
- package/dist/core/channel-loader.js +2 -0
- package/dist/core/command/agent-control.js +257 -14
- package/dist/core/command/cli-intent-parser.js +6 -2
- package/dist/core/command/command-handler.js +64 -55
- package/dist/core/command/memory-slash.js +1 -1
- package/dist/core/command/menu-catalog.js +5 -0
- package/dist/core/command/menu-handler.js +64 -18
- package/dist/core/command/menu-protocol.js +15 -1
- package/dist/core/command/provider-menu.js +46 -0
- package/dist/core/command/slash-handler.js +2 -1
- package/dist/core/daemon-control.js +18 -13
- package/dist/core/event-catalog.js +10 -0
- package/dist/core/evolagent-registry.js +3 -1
- package/dist/core/evolagent.js +8 -6
- package/dist/core/file-integrity/store.js +11 -0
- package/dist/core/handoff/ipc-registration.js +3 -0
- package/dist/core/initialization/service.js +491 -0
- package/dist/core/ipc-registration.js +34 -22
- package/dist/core/memory/consolidation-ops-records.js +95 -0
- package/dist/core/memory/consolidation-trigger.js +19 -2
- package/dist/core/memory/consolidation.js +19 -8
- package/dist/core/memory/memory-admin.js +1 -1
- package/dist/core/memory/relation-sweep.js +11 -6
- package/dist/core/message/aun-ipc-sender.js +73 -67
- package/dist/core/message/im-renderer.js +18 -0
- package/dist/core/message/message-bridge.js +35 -3
- package/dist/core/message/response-engine.js +32 -28
- package/dist/core/model/catalog-service.js +1 -1
- package/dist/core/model/config-scope.js +5 -7
- package/dist/core/ownership/owner-resolver.js +755 -143
- package/dist/core/permission/approval-gateway.js +103 -403
- package/dist/core/permission/ec-command-parser.js +32 -4
- package/dist/core/permission/index.js +1 -0
- package/dist/core/permission/permission-result.js +78 -0
- package/dist/core/permission/tool-policy.js +7 -43
- package/dist/core/system-session/memory.js +37 -7
- package/dist/directory/owner-directory-cache.js +2 -2
- package/dist/ecweb/init-menu.js +32 -0
- package/dist/index.js +17 -5
- package/dist/ipc.js +85 -25
- package/dist/paths.js +3 -0
- package/dist/proxy/manager.js +6 -7
- package/dist/trigger/authorization.js +27 -17
- package/dist/trigger/feedback.js +2 -3
- package/dist/trigger/ipc-service.js +105 -78
- package/dist/trigger/local-files.js +2 -1
- package/dist/trigger/manager.js +5 -3
- package/dist/trigger/scheduler.js +88 -6
- package/dist/utils/atomic-write.js +28 -0
- package/dist/utils/error-utils.js +24 -0
- package/dist/utils/logger.js +68 -1
- package/dist/utils/welcome.js +1 -1
- package/kits/docs/INDEX.md +1 -0
- package/kits/docs/aun/SDK.md +4 -1
- package/kits/docs/evolcore/INDEX.md +1 -0
- package/kits/docs/evolcore/agent.md +8 -1
- package/kits/docs/evolcore/aid.md +67 -17
- package/kits/docs/evolcore/config.md +11 -1
- package/kits/docs/evolcore/group.md +1 -1
- package/kits/docs/evolcore/provider.md +50 -0
- package/kits/docs/identity/AID_PROFILE_SPEC.md +52 -17
- package/kits/rules/01-overview.md +3 -0
- package/kits/rules/04-relation.md +1 -1
- package/kits/schemas/_meta.json +2 -2
- package/kits/schemas/agent-config.schema.16.json +3 -3
- package/kits/schemas/relation-config.schema.11.json +17 -0
- package/kits/skills/.switches.json +8 -0
- package/kits/skills/ec-agent-create/SKILL.md +3 -0
- package/kits/skills/ec-identity/SKILL.md +2 -2
- package/kits/skills/ec-skill/scripts/skill.mjs +6 -0
- package/kits/skills/ec-trigger/SKILL.md +5 -13
- package/kits/skills/thread-ops/SKILL.md +5 -0
- package/kits/templates/system-fragments/venue.md +1 -1
- package/package.json +3 -3
- package/dist/core/memory/forget-log.js +0 -96
- package/dist/core/message/create-status.js +0 -96
package/dist/channels/aun.js
CHANGED
|
@@ -29,7 +29,7 @@ import { appendAidLifecycle } from '../aun/aid/identity.js';
|
|
|
29
29
|
import { getAidStore, loadClient, SLOT } from '../aun/aid/store.js';
|
|
30
30
|
import { MAX_AUN_ATTACHMENT_SIZE, uploadBufferAndBuildPayload, uploadFileAndBuildPayload } from '../aun/msg/upload.js';
|
|
31
31
|
import { loadAgent } from '../config-store.js';
|
|
32
|
-
import { configuredOwnerList } from '../config/owner-config.js';
|
|
32
|
+
import { configuredOwnerList, firstConfiguredOwner } from '../config/owner-config.js';
|
|
33
33
|
import { resolveAgentLifecycle } from '../config/lifecycle.js';
|
|
34
34
|
import { resolveEffective } from '../config/config-manager.js';
|
|
35
35
|
import { isManagementRole } from '../config/builtin-roles.js';
|
|
@@ -57,6 +57,8 @@ import { sentReceipt, suppressedReceipt } from '../core/message/send-receipt.js'
|
|
|
57
57
|
export const AUN_HANDOFF_MARKER_FIELD = '_evolcore_handoff_id';
|
|
58
58
|
const AUN_INTERACTION_CARD_TTL_MS = 24 * 60 * 60 * 1000;
|
|
59
59
|
const AUN_INBOUND_DEDUP_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
60
|
+
const OWNER_DIRECTORY_RETRY_BASE_MS = 1_000;
|
|
61
|
+
const OWNER_DIRECTORY_RETRY_MAX_MS = 60_000;
|
|
60
62
|
const IMAGE_MIME_TYPE = /^image\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/i;
|
|
61
63
|
const AUN_ATTACHMENT_DOWNLOAD_ATTEMPTS = 3;
|
|
62
64
|
const AUN_ATTACHMENT_RETRY_DELAYS_MS = [250, 750];
|
|
@@ -695,6 +697,8 @@ function errorCodeForLog(error) {
|
|
|
695
697
|
}
|
|
696
698
|
export class AUNChannel {
|
|
697
699
|
config;
|
|
700
|
+
// Keep each target's attempt across channel recreation; reset only on process restart.
|
|
701
|
+
static configuredOwnerBindAttempts = new Set();
|
|
698
702
|
managedMenuClient = new ManagedMenuClient(params => {
|
|
699
703
|
if (!this.connected || !this.client)
|
|
700
704
|
return Promise.reject(new Error('AUN channel is not connected'));
|
|
@@ -706,7 +710,17 @@ export class AUNChannel {
|
|
|
706
710
|
return this.managedMenuClient.request(options);
|
|
707
711
|
}
|
|
708
712
|
client = null;
|
|
713
|
+
configuredOwnerBind;
|
|
714
|
+
configuredOwnerBindEpoch = 0;
|
|
715
|
+
configuredOwnerBindRecheck = false;
|
|
709
716
|
ownerDirectoryCache;
|
|
717
|
+
ownerDirectoryWatch;
|
|
718
|
+
ownerDirectoryFlight;
|
|
719
|
+
ownerDirectoryRecheck = false;
|
|
720
|
+
ownerDirectoryRetryTimer;
|
|
721
|
+
ownerDirectoryRetryAttempts = 0;
|
|
722
|
+
ownerDirectoryObserved;
|
|
723
|
+
ownerDirectoryResolvedSignature;
|
|
710
724
|
store = null;
|
|
711
725
|
/** 实际连接的网关 URL(来自 authenticate() 返回值 / connection.state 事件),替代旧 (client as any)._gatewayUrl。 */
|
|
712
726
|
gatewayUrl = '';
|
|
@@ -1501,25 +1515,205 @@ export class AUNChannel {
|
|
|
1501
1515
|
logger.warn(`${this.logPrefix()} topic.renamed failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
1502
1516
|
});
|
|
1503
1517
|
}
|
|
1504
|
-
|
|
1518
|
+
stopOwnerDirectoryRefresh() {
|
|
1519
|
+
this.ownerDirectoryWatch?.stop();
|
|
1520
|
+
this.ownerDirectoryWatch = undefined;
|
|
1521
|
+
if (this.ownerDirectoryRetryTimer)
|
|
1522
|
+
clearTimeout(this.ownerDirectoryRetryTimer);
|
|
1523
|
+
this.ownerDirectoryRetryTimer = undefined;
|
|
1524
|
+
this.ownerDirectoryRetryAttempts = 0;
|
|
1525
|
+
this.ownerDirectoryFlight = undefined;
|
|
1526
|
+
this.ownerDirectoryRecheck = false;
|
|
1527
|
+
this.ownerDirectoryObserved = undefined;
|
|
1528
|
+
this.ownerDirectoryResolvedSignature = undefined;
|
|
1529
|
+
this.ownerDirectoryCache?.authorization.dispose();
|
|
1530
|
+
this.ownerDirectoryCache = undefined;
|
|
1531
|
+
}
|
|
1532
|
+
/** One watch and retry chain per live connection; never opens another SDK client. */
|
|
1533
|
+
startOwnerDirectoryRefresh() {
|
|
1534
|
+
const client = this.client;
|
|
1535
|
+
if (!client || !this.connected || this.intentionalDisconnect || this.config.pureIdentity)
|
|
1536
|
+
return;
|
|
1537
|
+
if (this.ownerDirectoryWatch?.client === client)
|
|
1538
|
+
return;
|
|
1539
|
+
this.stopOwnerDirectoryRefresh();
|
|
1505
1540
|
const selfAid = this.config.aid.replace(/^@/, '');
|
|
1541
|
+
this.ownerDirectoryWatch = {
|
|
1542
|
+
client,
|
|
1543
|
+
stop: ownerResolver.onUpdate(subjectAid => {
|
|
1544
|
+
if (subjectAid !== selfAid || this.ownerDirectoryWatch?.client !== client || !this.connected)
|
|
1545
|
+
return;
|
|
1546
|
+
const view = ownerResolver.snapshot(selfAid);
|
|
1547
|
+
const previous = this.ownerDirectoryObserved;
|
|
1548
|
+
this.ownerDirectoryObserved = view;
|
|
1549
|
+
if (view.refreshFailure || view.consensusStatus === 'invalid' || view.consensusStatus === 'unavailable') {
|
|
1550
|
+
if (view.refreshFailure || view.consensus?.status === view.consensusStatus) {
|
|
1551
|
+
if (this.ownerDirectoryFlight) {
|
|
1552
|
+
this.scheduleOwnerDirectoryRetry(view);
|
|
1553
|
+
return;
|
|
1554
|
+
}
|
|
1555
|
+
void this.getOwnerDirectoryRuntime(undefined, false).catch(error => {
|
|
1556
|
+
logger.warn(`${this.logPrefix()} Owner directory state processing failed: ${errorCodeForLog(error) || 'UNKNOWN'}`);
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
else {
|
|
1560
|
+
// Material invalidation needs a new check, not a warning about an unchecked snapshot.
|
|
1561
|
+
this.queueOwnerDirectoryRefresh();
|
|
1562
|
+
}
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
this.clearOwnerDirectoryRetry();
|
|
1566
|
+
if (!previous || previous.refreshFailure || previous.consensusStatus !== view.consensusStatus
|
|
1567
|
+
|| previous.ownerAid !== view.ownerAid
|
|
1568
|
+
|| previous.consensus?.relation_version !== view.consensus?.relation_version) {
|
|
1569
|
+
this.queueOwnerDirectoryRefresh();
|
|
1570
|
+
}
|
|
1571
|
+
}),
|
|
1572
|
+
};
|
|
1573
|
+
this.queueOwnerDirectoryRefresh();
|
|
1574
|
+
}
|
|
1575
|
+
clearOwnerDirectoryRetry() {
|
|
1576
|
+
if (this.ownerDirectoryRetryTimer)
|
|
1577
|
+
clearTimeout(this.ownerDirectoryRetryTimer);
|
|
1578
|
+
this.ownerDirectoryRetryTimer = undefined;
|
|
1579
|
+
this.ownerDirectoryRetryAttempts = 0;
|
|
1580
|
+
}
|
|
1581
|
+
scheduleOwnerDirectoryRetry(ownership) {
|
|
1582
|
+
const retryable = ownership.refreshFailure
|
|
1583
|
+
? ownership.refreshFailure.kind === 'transient'
|
|
1584
|
+
: ownership.consensusStatus === 'unavailable' && ownership.consensus?.retryable === true;
|
|
1585
|
+
if (!retryable) {
|
|
1586
|
+
this.clearOwnerDirectoryRetry();
|
|
1587
|
+
return;
|
|
1588
|
+
}
|
|
1506
1589
|
const client = this.client;
|
|
1507
|
-
|
|
1590
|
+
if (!client || this.ownerDirectoryWatch?.client !== client || !this.connected
|
|
1591
|
+
|| this.intentionalDisconnect || this.ownerDirectoryRetryTimer)
|
|
1592
|
+
return;
|
|
1593
|
+
const delay = Math.min(OWNER_DIRECTORY_RETRY_BASE_MS * 2 ** Math.min(this.ownerDirectoryRetryAttempts++, 6), OWNER_DIRECTORY_RETRY_MAX_MS);
|
|
1594
|
+
logger.debug(`${this.logPrefix()} Owner 核验暂时失败,${delay}ms 后重试:selfAid=${ownership.subjectAid}`);
|
|
1595
|
+
this.ownerDirectoryRetryTimer = setTimeout(() => {
|
|
1596
|
+
this.ownerDirectoryRetryTimer = undefined;
|
|
1597
|
+
if (this.ownerDirectoryWatch?.client === client && this.connected)
|
|
1598
|
+
this.queueOwnerDirectoryRefresh();
|
|
1599
|
+
}, delay);
|
|
1600
|
+
this.ownerDirectoryRetryTimer.unref();
|
|
1601
|
+
}
|
|
1602
|
+
queueOwnerDirectoryRefresh() {
|
|
1603
|
+
const watch = this.ownerDirectoryWatch;
|
|
1604
|
+
if (!watch || !this.connected)
|
|
1605
|
+
return;
|
|
1606
|
+
if (this.ownerDirectoryFlight) {
|
|
1607
|
+
this.ownerDirectoryRecheck = true;
|
|
1608
|
+
return;
|
|
1609
|
+
}
|
|
1610
|
+
this.ownerDirectoryResolvedSignature = undefined;
|
|
1611
|
+
const promise = Promise.resolve().then(async () => {
|
|
1612
|
+
if (this.ownerDirectoryWatch !== watch || !this.connected)
|
|
1613
|
+
return;
|
|
1614
|
+
await this.passiveOwnerDirectoryRefresh(watch);
|
|
1615
|
+
}).catch(error => {
|
|
1616
|
+
logger.warn(`${this.logPrefix()} Owner directory passive refresh failed: ${errorCodeForLog(error) || 'UNKNOWN'}`);
|
|
1617
|
+
}).finally(() => {
|
|
1618
|
+
if (this.ownerDirectoryFlight !== promise)
|
|
1619
|
+
return;
|
|
1620
|
+
const resolvedSignature = this.ownerDirectoryResolvedSignature;
|
|
1621
|
+
const recheck = this.ownerDirectoryRecheck;
|
|
1622
|
+
this.ownerDirectoryFlight = undefined;
|
|
1623
|
+
this.ownerDirectoryResolvedSignature = undefined;
|
|
1624
|
+
this.ownerDirectoryRecheck = false;
|
|
1625
|
+
if (recheck && this.ownerDirectoryWatch === watch && this.connected) {
|
|
1626
|
+
const current = ownerResolver.snapshot(this.config.aid.replace(/^@/, ''));
|
|
1627
|
+
// The result of this flight already covers its own consensus notification.
|
|
1628
|
+
if (this.ownerDirectoryViewSignature(current) !== resolvedSignature)
|
|
1629
|
+
this.queueOwnerDirectoryRefresh();
|
|
1630
|
+
}
|
|
1631
|
+
});
|
|
1632
|
+
this.ownerDirectoryFlight = promise;
|
|
1633
|
+
}
|
|
1634
|
+
ownerDirectoryViewSignature(view) {
|
|
1635
|
+
return JSON.stringify([
|
|
1636
|
+
view.revision,
|
|
1637
|
+
view.consensusStatus,
|
|
1638
|
+
view.ownerAid,
|
|
1639
|
+
view.consensus?.relation_version,
|
|
1640
|
+
view.refreshFailure?.attemptId,
|
|
1641
|
+
]);
|
|
1642
|
+
}
|
|
1643
|
+
ownerDirectoryFailureReason(ownership) {
|
|
1644
|
+
const code = (ownership.refreshFailure?.code ?? ownership.consensus?.code ?? '').toUpperCase();
|
|
1645
|
+
const reason = ownership.refreshFailure?.reason ?? ownership.consensus?.reason ?? '';
|
|
1646
|
+
if (/TIMEOUT|ETIMEDOUT/.test(code) || /timed out|timeout/i.test(reason))
|
|
1647
|
+
return '核验超时';
|
|
1648
|
+
if (/projection|relation_version|NS relation changed|digest does not match/i.test(reason))
|
|
1649
|
+
return '关系信息不一致或发生变化';
|
|
1650
|
+
if (/CLIENT_DISCONNECTED|CONNECTION|NETWORK|ECONN|ENET|EHOST|EAI_AGAIN|ENOTFOUND/.test(code)
|
|
1651
|
+
|| /gateway|network|connection|socket hang up|not connected|fetch failed|temporarily unavailable/i.test(reason)) {
|
|
1652
|
+
return '连接或网络暂不可用';
|
|
1653
|
+
}
|
|
1654
|
+
if (ownership.consensusStatus === 'invalid')
|
|
1655
|
+
return '关系材料校验未通过';
|
|
1656
|
+
return '未取得可用核验结果';
|
|
1657
|
+
}
|
|
1658
|
+
async getOwnerDirectoryRuntime(expectedOwnerAid, verifyOwnership = true, expectedWatch) {
|
|
1659
|
+
const selfAid = this.config.aid.replace(/^@/, '');
|
|
1660
|
+
const client = this.client;
|
|
1661
|
+
logger.debug(`${this.logPrefix()} getOwnerDirectoryRuntime called: selfAid=${selfAid}, connected=${this.connected}, pureIdentity=${!!this.config.pureIdentity}`);
|
|
1508
1662
|
if (!client || !this.connected || this.config.pureIdentity) {
|
|
1509
|
-
logger.debug(`${this.logPrefix()} getOwnerDirectoryRuntime early return: client=${!!client}, connected=${this.connected}, pureIdentity=${this.config.pureIdentity}`);
|
|
1663
|
+
logger.debug(`${this.logPrefix()} getOwnerDirectoryRuntime early return: client=${!!client}, connected=${this.connected}, pureIdentity=${!!this.config.pureIdentity}`);
|
|
1664
|
+
return;
|
|
1665
|
+
}
|
|
1666
|
+
const ownership = verifyOwnership
|
|
1667
|
+
? await ownerResolver.refresh(selfAid, {
|
|
1668
|
+
force: !!expectedOwnerAid, allowTransient: !expectedOwnerAid, source: 'directory.runtime',
|
|
1669
|
+
})
|
|
1670
|
+
: ownerResolver.snapshot(selfAid);
|
|
1671
|
+
if (this.client !== client || !this.connected
|
|
1672
|
+
|| (expectedWatch && (this.ownerDirectoryWatch !== expectedWatch || expectedWatch.client !== client))) {
|
|
1673
|
+
logger.debug(`${this.logPrefix()} 跳过 Owner 目录刷新:连接已切换或断开,selfAid=${selfAid} clientChanged=${this.client !== client} connected=${this.connected}`);
|
|
1510
1674
|
return;
|
|
1511
1675
|
}
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1676
|
+
if (expectedWatch && this.ownerDirectoryFlight) {
|
|
1677
|
+
this.ownerDirectoryResolvedSignature = this.ownerDirectoryViewSignature(ownership);
|
|
1678
|
+
}
|
|
1515
1679
|
const ownerAid = ownerResolver.owner(selfAid) ?? ownerResolver.knownOwner(selfAid);
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
||
|
|
1519
|
-
|
|
1680
|
+
const expectedContext = expectedOwnerAid ? ` expectedOwnerAid=${expectedOwnerAid}` : '';
|
|
1681
|
+
const queryFailed = !!ownership.refreshFailure
|
|
1682
|
+
|| ownership.consensusStatus === 'invalid'
|
|
1683
|
+
|| (ownership.consensusStatus === 'unavailable' && ownership.consensus?.status === 'unavailable');
|
|
1684
|
+
if (queryFailed) {
|
|
1685
|
+
const code = ownership.refreshFailure?.code ?? ownership.consensus?.code ?? ownership.consensusStatus;
|
|
1686
|
+
const reason = this.ownerDirectoryFailureReason(ownership);
|
|
1687
|
+
const message = ownership.consensusStatus === 'invalid' ? 'Owner 关系核验未通过'
|
|
1688
|
+
: ownership.refreshFailure?.kind === 'permanent' ? 'Owner 关系核验失败' : 'Owner 关系核验暂不可用';
|
|
1689
|
+
logger.warn(`${this.logPrefix()} ${message}:selfAid=${selfAid} status=${ownership.consensusStatus} code=${code} 原因=${reason}${expectedContext}`);
|
|
1690
|
+
this.scheduleOwnerDirectoryRetry(ownership);
|
|
1691
|
+
}
|
|
1692
|
+
else if (ownership.consensusStatus === 'unavailable') {
|
|
1693
|
+
logger.debug(`${this.logPrefix()} 等待 Owner 关系核验完成,暂缓名录刷新:selfAid=${selfAid}${expectedContext}`);
|
|
1694
|
+
return;
|
|
1695
|
+
}
|
|
1696
|
+
else {
|
|
1697
|
+
this.clearOwnerDirectoryRetry();
|
|
1698
|
+
}
|
|
1699
|
+
if (!ownerAid) {
|
|
1700
|
+
if (!queryFailed) {
|
|
1701
|
+
const reason = ownership.consensusStatus === 'single_sided' ? '绑定待确认'
|
|
1702
|
+
: ownership.consensusStatus === 'unbound' ? '尚未绑定 Owner'
|
|
1703
|
+
: ownership.consensusStatus === 'released' ? 'Owner 关系已释放'
|
|
1704
|
+
: '未解析到 Owner';
|
|
1705
|
+
logger.info(`${this.logPrefix()} 跳过 Owner 目录刷新:${reason},selfAid=${selfAid} status=${ownership.consensusStatus}${expectedContext}`);
|
|
1706
|
+
}
|
|
1707
|
+
return;
|
|
1708
|
+
}
|
|
1709
|
+
if (expectedOwnerAid && (ownership.consensusStatus !== 'consensus'
|
|
1710
|
+
|| ownership.refreshFailure || ownership.ownerAid !== expectedOwnerAid)) {
|
|
1711
|
+
logger.info(`${this.logPrefix()} 跳过 Owner 目录刷新:Owner 与预期不符,selfAid=${selfAid} ownerAid=${ownerAid} expectedOwnerAid=${expectedOwnerAid}`);
|
|
1520
1712
|
return;
|
|
1521
1713
|
}
|
|
1714
|
+
logger.debug(`${this.logPrefix()} Owner 目录运行时就绪:selfAid=${selfAid} ownerAid=${ownerAid} status=${ownership.consensusStatus}${expectedContext}`);
|
|
1522
1715
|
if (this.ownerDirectoryCache?.client !== client || this.ownerDirectoryCache.ownerAid !== ownerAid) {
|
|
1716
|
+
this.ownerDirectoryCache?.authorization.dispose();
|
|
1523
1717
|
const directory = path.join(this.agentDir, 'directory');
|
|
1524
1718
|
const cache = new OwnerDirectoryCache({
|
|
1525
1719
|
selfAid,
|
|
@@ -1554,9 +1748,9 @@ export class AUNChannel {
|
|
|
1554
1748
|
await runtime.cache.refreshIfNeeded();
|
|
1555
1749
|
}
|
|
1556
1750
|
}
|
|
1557
|
-
/**
|
|
1558
|
-
async passiveOwnerDirectoryRefresh() {
|
|
1559
|
-
const runtime = await this.getOwnerDirectoryRuntime();
|
|
1751
|
+
/** Verify Ownership first; refresh existing grants without initiating an authorization request. */
|
|
1752
|
+
async passiveOwnerDirectoryRefresh(expectedWatch) {
|
|
1753
|
+
const runtime = await this.getOwnerDirectoryRuntime(undefined, true, expectedWatch);
|
|
1560
1754
|
if (!runtime)
|
|
1561
1755
|
return;
|
|
1562
1756
|
await runtime.cache.refreshIfNeeded();
|
|
@@ -1623,12 +1817,6 @@ export class AUNChannel {
|
|
|
1623
1817
|
}
|
|
1624
1818
|
async handleOwnerDirectoryGrantFromOwner(senderAid, grant) {
|
|
1625
1819
|
const selfAid = this.config.aid.replace(/^@/, '');
|
|
1626
|
-
await ownerResolver.refresh(selfAid, { force: true });
|
|
1627
|
-
const ownerAid = ownerResolver.owner(selfAid) ?? ownerResolver.knownOwner(selfAid);
|
|
1628
|
-
if (!ownerAid || senderAid !== ownerAid) {
|
|
1629
|
-
logger.warn(`${this.logPrefix()} owner.directory.grant rejected: sender ${senderAid} is not owner ${ownerAid}`);
|
|
1630
|
-
return;
|
|
1631
|
-
}
|
|
1632
1820
|
const parsed = parseOwnerDirectoryGrantAbilityResponse(grant);
|
|
1633
1821
|
if (!parsed) {
|
|
1634
1822
|
logger.warn(`${this.logPrefix()} owner.directory.grant: invalid grant payload (does not match evol-directory-access-v1 contract)`);
|
|
@@ -1976,6 +2164,8 @@ export class AUNChannel {
|
|
|
1976
2164
|
}
|
|
1977
2165
|
}
|
|
1978
2166
|
async _initClientInner() {
|
|
2167
|
+
this.configuredOwnerBindEpoch++;
|
|
2168
|
+
this.stopOwnerDirectoryRefresh();
|
|
1979
2169
|
this.observerGroupMembers.clear();
|
|
1980
2170
|
for (const groupId of new Set([
|
|
1981
2171
|
...this.observerGroupMembershipEpoch.keys(),
|
|
@@ -2276,15 +2466,14 @@ export class AUNChannel {
|
|
|
2276
2466
|
this.connected = true;
|
|
2277
2467
|
this.connectedAt = Date.now();
|
|
2278
2468
|
this.setAidStatus('connected', { lastConnectedAt: Date.now(), lastError: undefined, gatewayUrl: this.gatewayUrl });
|
|
2279
|
-
|
|
2280
|
-
logger.warn(`${this.logPrefix()} Owner directory passive refresh failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
2281
|
-
});
|
|
2469
|
+
this.reconcileConfiguredOwnerBinding();
|
|
2282
2470
|
if (!this.config.pureIdentity) {
|
|
2283
2471
|
void Promise.resolve(this.groupInviteConnectedHandler?.()).catch(error => {
|
|
2284
2472
|
logger.warn(`${this.logPrefix()} Group invitation recovery failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
2285
2473
|
});
|
|
2286
2474
|
}
|
|
2287
2475
|
logger.info(`${this.logPrefix()} Connected as ${this._aid}`);
|
|
2476
|
+
this.startOwnerDirectoryRefresh();
|
|
2288
2477
|
appendAidEvent({ ts: Date.now(), iso: new Date().toISOString(), event: 'connected', aid: this.config.aid, gateway: this.gatewayUrl });
|
|
2289
2478
|
appendAidLifecycle({ ts: Date.now(), iso: new Date().toISOString(), event: 'connected', aid: this.config.aid, gateway: this.gatewayUrl });
|
|
2290
2479
|
}
|
|
@@ -2305,7 +2494,7 @@ export class AUNChannel {
|
|
|
2305
2494
|
logger.warn(`${this.logPrefix()} agent config not found for ${aidName}, skipping welcome message`);
|
|
2306
2495
|
return false;
|
|
2307
2496
|
}
|
|
2308
|
-
await ownerResolver.refresh(aidName);
|
|
2497
|
+
await ownerResolver.refresh(aidName, { source: 'bootstrap.welcome' });
|
|
2309
2498
|
const owners = ownerResolver.owners(aidName);
|
|
2310
2499
|
if (owners.length === 0) {
|
|
2311
2500
|
logger.info(`${this.logPrefix()} No owner configured, skipping post-bootstrap welcome message`);
|
|
@@ -2396,15 +2585,19 @@ export class AUNChannel {
|
|
|
2396
2585
|
* already-active agent from manufacturing another welcome message.
|
|
2397
2586
|
*/
|
|
2398
2587
|
async reconcilePostBootstrapWelcome() {
|
|
2399
|
-
await this.repairBootstrapRoutes();
|
|
2400
2588
|
const configuredAid = this.config.aid;
|
|
2401
2589
|
const aid = configuredAid.startsWith('@') ? configuredAid.slice(1) : configuredAid;
|
|
2590
|
+
const pendingEntries = findPostBootstrapWelcomeOutbox(aid);
|
|
2591
|
+
if (pendingEntries.length === 0)
|
|
2592
|
+
return postBootstrapWelcomeStatus(aid, ownerResolver.owners(aid));
|
|
2593
|
+
await this.repairBootstrapRoutes();
|
|
2594
|
+
// Route repair may replace the persisted entry object (including its
|
|
2595
|
+
// delivery target), so drain the freshly reloaded entry instead of the
|
|
2596
|
+
// stale pre-repair snapshot.
|
|
2402
2597
|
const entries = findPostBootstrapWelcomeOutbox(aid);
|
|
2403
2598
|
const agentConfig = loadAgent(aid);
|
|
2404
|
-
await ownerResolver.refresh(aid);
|
|
2599
|
+
await ownerResolver.refresh(aid, { source: 'bootstrap.welcome' });
|
|
2405
2600
|
const owners = ownerResolver.owners(aid);
|
|
2406
|
-
if (entries.length === 0)
|
|
2407
|
-
return postBootstrapWelcomeStatus(aid, owners);
|
|
2408
2601
|
if (!agentConfig || resolveAgentLifecycle(agentConfig) !== 'active') {
|
|
2409
2602
|
logger.info(`${this.logPrefix()} Post-bootstrap welcome prepared; waiting for lifecycle=active`);
|
|
2410
2603
|
return postBootstrapWelcomeStatus(aid, owners);
|
|
@@ -3844,6 +4037,7 @@ export class AUNChannel {
|
|
|
3844
4037
|
return;
|
|
3845
4038
|
this.lastConnectionStateSignature = signature;
|
|
3846
4039
|
if (this.intentionalDisconnect && state === 'disconnected') {
|
|
4040
|
+
this.stopOwnerDirectoryRefresh();
|
|
3847
4041
|
this.connected = false;
|
|
3848
4042
|
this.connectedAt = 0;
|
|
3849
4043
|
return;
|
|
@@ -3860,13 +4054,18 @@ export class AUNChannel {
|
|
|
3860
4054
|
this.setAidStatus('connected', { lastConnectedAt: Date.now(), lastError: undefined, gatewayUrl: this.gatewayUrl });
|
|
3861
4055
|
this.trace('IN', source, data);
|
|
3862
4056
|
logger.info(`${this.logPrefix()} Connected`);
|
|
4057
|
+
this.reconcileConfiguredOwnerBinding();
|
|
4058
|
+
this.startOwnerDirectoryRefresh();
|
|
3863
4059
|
// 不在这里清 flapCount —— 短命连接一上来就会触发本分支,
|
|
3864
4060
|
// 必须等 disconnected 时根据 lifetime 决定是否清零
|
|
3865
4061
|
this.drainOutbox();
|
|
3866
4062
|
}
|
|
3867
4063
|
else if (state === 'disconnected' || state === 'reconnecting') {
|
|
4064
|
+
this.stopOwnerDirectoryRefresh();
|
|
3868
4065
|
const wasConnected = this.connected;
|
|
3869
4066
|
this.connected = false;
|
|
4067
|
+
if (wasConnected)
|
|
4068
|
+
this.configuredOwnerBindEpoch++;
|
|
3870
4069
|
this.clearQueuedObserverSends(state);
|
|
3871
4070
|
// The nine-state SDK emits standby -> retry_backoff -> reconnecting for
|
|
3872
4071
|
// one reconnect cycle. Count the connected-to-down edge once instead of
|
|
@@ -3919,6 +4118,8 @@ export class AUNChannel {
|
|
|
3919
4118
|
}
|
|
3920
4119
|
}
|
|
3921
4120
|
else if (state === 'terminal_failed') {
|
|
4121
|
+
this.stopOwnerDirectoryRefresh();
|
|
4122
|
+
this.configuredOwnerBindEpoch++;
|
|
3922
4123
|
this.connected = false;
|
|
3923
4124
|
this.connectedAt = 0;
|
|
3924
4125
|
this.trace('IN', source, data);
|
|
@@ -3971,6 +4172,81 @@ export class AUNChannel {
|
|
|
3971
4172
|
}
|
|
3972
4173
|
}
|
|
3973
4174
|
}
|
|
4175
|
+
configuredOwnerAid() {
|
|
4176
|
+
const config = loadAgent(this.config.aid);
|
|
4177
|
+
if (!config)
|
|
4178
|
+
return undefined;
|
|
4179
|
+
const lifecycle = resolveAgentLifecycle(config);
|
|
4180
|
+
if (lifecycle === null || lifecycle === 'active')
|
|
4181
|
+
return undefined;
|
|
4182
|
+
return firstConfiguredOwner(config);
|
|
4183
|
+
}
|
|
4184
|
+
reconcileConfiguredOwnerBinding() {
|
|
4185
|
+
const client = this.client;
|
|
4186
|
+
const subjectAid = this.config.aid;
|
|
4187
|
+
const epoch = this.configuredOwnerBindEpoch;
|
|
4188
|
+
if (!client || !this.connected || this.intentionalDisconnect || this.config.pureIdentity)
|
|
4189
|
+
return;
|
|
4190
|
+
if (this.configuredOwnerBind) {
|
|
4191
|
+
if (this.configuredOwnerBind.client !== client || this.configuredOwnerBind.epoch !== epoch) {
|
|
4192
|
+
this.configuredOwnerBindRecheck = true;
|
|
4193
|
+
}
|
|
4194
|
+
return;
|
|
4195
|
+
}
|
|
4196
|
+
// Queue the work after installing the guard; config failures must not fail the channel connection.
|
|
4197
|
+
const promise = Promise.resolve()
|
|
4198
|
+
.then(() => this.submitConfiguredOwnerBinding(subjectAid, client, epoch))
|
|
4199
|
+
.catch(error => {
|
|
4200
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
4201
|
+
logger.warn(`${this.logPrefix()} Configured Owner binding invitation failed: ${reason}`);
|
|
4202
|
+
});
|
|
4203
|
+
const tracked = promise.finally(() => {
|
|
4204
|
+
if (this.configuredOwnerBind?.promise !== tracked)
|
|
4205
|
+
return;
|
|
4206
|
+
this.configuredOwnerBind = undefined;
|
|
4207
|
+
const recheck = this.configuredOwnerBindRecheck;
|
|
4208
|
+
this.configuredOwnerBindRecheck = false;
|
|
4209
|
+
if (recheck)
|
|
4210
|
+
this.reconcileConfiguredOwnerBinding();
|
|
4211
|
+
});
|
|
4212
|
+
this.configuredOwnerBind = { client, epoch, promise: tracked };
|
|
4213
|
+
}
|
|
4214
|
+
isCurrentConfiguredOwnerBind(client, epoch) {
|
|
4215
|
+
return this.client === client && this.configuredOwnerBindEpoch === epoch
|
|
4216
|
+
&& this.connected && !this.intentionalDisconnect;
|
|
4217
|
+
}
|
|
4218
|
+
async submitConfiguredOwnerBinding(subjectAid, client, epoch) {
|
|
4219
|
+
if (!this.isCurrentConfiguredOwnerBind(client, epoch))
|
|
4220
|
+
return;
|
|
4221
|
+
const configuredOwnerAid = this.configuredOwnerAid();
|
|
4222
|
+
if (!configuredOwnerAid
|
|
4223
|
+
|| AUNChannel.configuredOwnerBindAttempts.has(JSON.stringify([subjectAid, configuredOwnerAid]))
|
|
4224
|
+
|| ownerResolver.hasRejectedOwnerBinding(subjectAid, configuredOwnerAid))
|
|
4225
|
+
return;
|
|
4226
|
+
ownerResolver.setConnected(subjectAid, client, true);
|
|
4227
|
+
const current = await ownerResolver.refresh(subjectAid, { force: true, source: 'channel.binding' });
|
|
4228
|
+
if (!this.isCurrentConfiguredOwnerBind(client, epoch))
|
|
4229
|
+
return;
|
|
4230
|
+
// The config may change while the fresh consensus query is in flight.
|
|
4231
|
+
const ownerAid = this.configuredOwnerAid();
|
|
4232
|
+
if (!ownerAid || ownerResolver.hasRejectedOwnerBinding(subjectAid, ownerAid))
|
|
4233
|
+
return;
|
|
4234
|
+
if (current.task?.operation === 'bind' && current.task.owner_aid === ownerAid
|
|
4235
|
+
&& current.task.status === 'rejected') {
|
|
4236
|
+
return;
|
|
4237
|
+
}
|
|
4238
|
+
if (current.consensusStatus === 'consensus')
|
|
4239
|
+
return;
|
|
4240
|
+
if (!['unbound', 'released', 'single_sided'].includes(current.consensusStatus))
|
|
4241
|
+
return;
|
|
4242
|
+
const attemptKey = JSON.stringify([subjectAid, ownerAid]);
|
|
4243
|
+
if (AUNChannel.configuredOwnerBindAttempts.has(attemptKey))
|
|
4244
|
+
return;
|
|
4245
|
+
// Claim before awaiting submission: an error or lost receipt must not cause automatic resends.
|
|
4246
|
+
AUNChannel.configuredOwnerBindAttempts.add(attemptKey);
|
|
4247
|
+
await ownerResolver.bind(subjectAid, ownerAid, () => this.isCurrentConfiguredOwnerBind(client, epoch) && this.configuredOwnerAid() === ownerAid
|
|
4248
|
+
&& !ownerResolver.hasRejectedOwnerBinding(subjectAid, ownerAid));
|
|
4249
|
+
}
|
|
3974
4250
|
/** 判断 terminal_failed 的 reason 是否属于"被踢"类 */
|
|
3975
4251
|
isKickReason(reason) {
|
|
3976
4252
|
if (!reason)
|
|
@@ -4045,6 +4321,7 @@ export class AUNChannel {
|
|
|
4045
4321
|
this.reconnectTimer = null;
|
|
4046
4322
|
}
|
|
4047
4323
|
// Force close SDK client,中断它内部的重连循环
|
|
4324
|
+
this.stopOwnerDirectoryRefresh();
|
|
4048
4325
|
if (this.client) {
|
|
4049
4326
|
ownerResolver.detachClient(this.config.aid, this.client);
|
|
4050
4327
|
this.trace('OUT', 'client.close', { reason: `takeover_${reason}` });
|
|
@@ -4070,6 +4347,7 @@ export class AUNChannel {
|
|
|
4070
4347
|
// initClient 完成后再次校验 generation,防止 initClient 期间被新的 takeover 取代
|
|
4071
4348
|
if (gen !== this.reconnectGeneration) {
|
|
4072
4349
|
logger.info(`${this.logPrefix()} TS-layer reconnect (${reason}) succeeded but generation stale, closing stale client`);
|
|
4350
|
+
this.stopOwnerDirectoryRefresh();
|
|
4073
4351
|
if (this.client) {
|
|
4074
4352
|
ownerResolver.detachClient(this.config.aid, this.client);
|
|
4075
4353
|
this.client.close().catch(() => { });
|
|
@@ -6428,7 +6706,12 @@ export class AUNChannel {
|
|
|
6428
6706
|
async drainOutbox() {
|
|
6429
6707
|
if (!this.connected || !this.client)
|
|
6430
6708
|
return;
|
|
6431
|
-
|
|
6709
|
+
const bootstrapPending = [
|
|
6710
|
+
...outbox.findByDedupePrefix(this.config.aid, bootstrapInitialMessageOperationPrefix(this.config.aid)),
|
|
6711
|
+
...findPostBootstrapWelcomeOutbox(this.config.aid),
|
|
6712
|
+
].some(entry => !entry.terminal && !entry.deliveryResult);
|
|
6713
|
+
if (bootstrapPending)
|
|
6714
|
+
await this.repairBootstrapRoutes(false);
|
|
6432
6715
|
const drainQueue = async (queue) => {
|
|
6433
6716
|
if (!outbox.hasPending(this.config.aid, queue))
|
|
6434
6717
|
return;
|
|
@@ -6471,14 +6754,16 @@ export class AUNChannel {
|
|
|
6471
6754
|
await drainQueue('activity');
|
|
6472
6755
|
}
|
|
6473
6756
|
/** Repair bootstrap outbox entries to the configured personal Owner route. */
|
|
6474
|
-
async repairBootstrapRoutes() {
|
|
6757
|
+
async repairBootstrapRoutes(includeTerminal = true) {
|
|
6475
6758
|
const aid = this.config.aid.replace(/^@/, '');
|
|
6476
|
-
await ownerResolver.refresh(aid);
|
|
6477
|
-
const owners = ownerResolver.owners(aid);
|
|
6478
6759
|
const entries = [
|
|
6479
|
-
...outbox.findByDedupePrefix(aid, bootstrapInitialMessageOperationPrefix(aid), { includeTerminal
|
|
6480
|
-
...findPostBootstrapWelcomeOutbox(aid, { includeTerminal
|
|
6760
|
+
...outbox.findByDedupePrefix(aid, bootstrapInitialMessageOperationPrefix(aid), { includeTerminal }),
|
|
6761
|
+
...findPostBootstrapWelcomeOutbox(aid, { includeTerminal }),
|
|
6481
6762
|
].filter((entry) => !!entry?.channelId);
|
|
6763
|
+
if (entries.length === 0)
|
|
6764
|
+
return;
|
|
6765
|
+
await ownerResolver.refresh(aid, { source: includeTerminal ? 'bootstrap.welcome' : 'bootstrap.outbox' });
|
|
6766
|
+
const owners = ownerResolver.owners(aid);
|
|
6482
6767
|
for (const entry of entries) {
|
|
6483
6768
|
const isInitial = entry.dedupeKey?.startsWith(bootstrapInitialMessageOperationPrefix(aid));
|
|
6484
6769
|
const isOwnerSpecific = entry.dedupeKey?.startsWith(`bootstrap-complete:v2:${aid}:`);
|
|
@@ -6646,6 +6931,8 @@ export class AUNChannel {
|
|
|
6646
6931
|
await this.sendStructuredOrThrow(channelId, payloadObj, context);
|
|
6647
6932
|
}
|
|
6648
6933
|
async disconnect() {
|
|
6934
|
+
this.configuredOwnerBindEpoch++;
|
|
6935
|
+
this.stopOwnerDirectoryRefresh();
|
|
6649
6936
|
if (this.client)
|
|
6650
6937
|
ownerResolver.detachClient(this.config.aid, this.client);
|
|
6651
6938
|
this.managedMenuClient.close();
|
|
@@ -183,7 +183,7 @@ Options:
|
|
|
183
183
|
}
|
|
184
184
|
if ((sub === 'new' && createMode === 'initialize') || sub === 'initialize') {
|
|
185
185
|
if (wantsHelp(args)) {
|
|
186
|
-
console.log(`${MAIN_CLI_BIN} agent initialize <aid> --resume [--wait 0..300] [--format json]\n
|
|
186
|
+
console.log(`${MAIN_CLI_BIN} agent initialize <aid> --resume [--client-request-id <uuid>] [--wait 0..300] [--format json]\n恢复一次性初始化;一步创建恢复必须提供原 clientRequestId;默认等待 60 秒。`);
|
|
187
187
|
return;
|
|
188
188
|
}
|
|
189
189
|
try {
|
|
@@ -393,7 +393,10 @@ Options:
|
|
|
393
393
|
});
|
|
394
394
|
const validationError = validateAgentCreatePlan(plan);
|
|
395
395
|
if (validationError) {
|
|
396
|
-
const error = {
|
|
396
|
+
const error = {
|
|
397
|
+
ok: false, error: validationError,
|
|
398
|
+
...(validationError.startsWith('CONTROL_AID_RESERVED:') ? { code: 'CONTROL_AID_RESERVED' } : {}),
|
|
399
|
+
};
|
|
397
400
|
if (formatJson)
|
|
398
401
|
console.log(JSON.stringify(error));
|
|
399
402
|
else
|