@agentchatme/agent-core 0.4.1 → 0.5.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/dist/index.js CHANGED
@@ -1,13 +1,12 @@
1
1
  import {
2
+ AGENT_LOCK_DIR_ENV,
3
+ DAEMON_LOCK_FILE,
2
4
  DEFAULT_API_BASE,
3
- DEFAULT_RUN_BUDGET_MS,
4
- DEFAULT_RUN_PROFILE,
5
5
  FAILED_CYCLE_OUTCOMES,
6
6
  HEARTBEAT_FILE,
7
- INTEGRATION_ENV,
8
- INTEGRATION_ENV_LEGACY,
9
- INTEGRATION_ENV_LEGACY_REMOVAL,
10
7
  INTEGRATION_STATUS_FILE,
8
+ LEASE_RENEW_MS,
9
+ LEASE_STALE_MS,
11
10
  LEGACY_STATUS_FILE,
12
11
  MANUAL,
13
12
  MANUAL_FETCH_TIMEOUT_MS,
@@ -17,17 +16,21 @@ import {
17
16
  MANUAL_SKILL_NAME,
18
17
  MANUAL_URL,
19
18
  REPEATED_FAILED_CYCLES,
20
- RUN_BUDGET_ENV,
21
- RUN_PROFILES,
22
- RUN_PROFILE_ENV,
23
19
  SOCKET_STATES,
20
+ STANDBY_MARKER_FRESH_MS,
24
21
  TEXTS_CACHE_FILE,
22
+ acquireAgentLock,
25
23
  acquireLeaderLock,
24
+ acquireLease,
25
+ agentLockDir,
26
+ agentLockPath,
27
+ agentLockRefusal,
26
28
  alwaysOnHealth,
27
29
  alwaysOnOptedOut,
28
30
  alwaysOnState,
29
31
  alwaysOnWanted,
30
32
  beat,
33
+ bootedAt,
31
34
  checkManual,
32
35
  clearAlwaysOnInstalledVersion,
33
36
  clearAlwaysOnOptOut,
@@ -35,6 +38,7 @@ import {
35
38
  clearCredentials,
36
39
  clearIntegrationStatus,
37
40
  clearPending,
41
+ clearStandby,
38
42
  credentialsPath,
39
43
  describeManualSync,
40
44
  envApiKey,
@@ -44,13 +48,13 @@ import {
44
48
  fetchServedTexts,
45
49
  formatIntegrationStatus,
46
50
  fromKitResult,
51
+ holderLive,
52
+ identityLineOf,
47
53
  idle,
48
- integrationDisabled,
49
- integrationDisabledBy,
50
54
  integrationStatusAge,
51
55
  integrationStatusPath,
52
- isRunProfile,
53
56
  isRunnerFactory,
57
+ leaseHolder,
54
58
  loadTexts,
55
59
  manualFrontMatter,
56
60
  manualPointer,
@@ -58,40 +62,63 @@ import {
58
62
  markAlwaysOnInstalledVersion,
59
63
  markAlwaysOnOptOut,
60
64
  markAlwaysOnWanted,
65
+ markStandby,
66
+ normalizeHandle,
67
+ parseLease,
61
68
  pendingPath,
69
+ pidAlive,
62
70
  readAlwaysOnInstalledVersion,
63
71
  readCachedTexts,
64
72
  readCredentials,
65
73
  readIntegrationStatus,
66
74
  readManualVersion,
67
75
  readPending,
76
+ resetLeaseObservations,
68
77
  resolveIdentity,
69
- resolveRunBudgetMs,
70
- resolveRunProfile,
78
+ standbyMarkerPath,
79
+ standbyWaiting,
71
80
  statePath,
72
81
  syncManual,
73
82
  textsCachePath,
74
83
  writeCredentials,
75
84
  writeIntegrationStatus,
76
85
  writePending
77
- } from "./chunk-7LAAAXUO.js";
86
+ } from "./chunk-NAAHIK7Z.js";
78
87
  import {
79
88
  HOSTED_MCP_PATH,
89
+ PROBE_KILL_WAIT_MS,
80
90
  VERSION,
81
91
  hostedMcpUrl,
82
- probeHostedMcp
83
- } from "./chunk-XDWBTX5J.js";
92
+ probeHostedMcp,
93
+ runProbe
94
+ } from "./chunk-UE7DKW3C.js";
84
95
  import {
96
+ DEFAULT_RUN_BUDGET_MS,
97
+ DEFAULT_RUN_PROFILE,
98
+ INTEGRATION_ENV,
99
+ INTEGRATION_ENV_LEGACY,
100
+ INTEGRATION_ENV_LEGACY_REMOVAL,
101
+ PEER_RUN_WARNING,
102
+ RUN_BUDGET_ENV,
103
+ RUN_PROFILES,
104
+ RUN_PROFILE_ENV,
85
105
  atomicCopyFile,
86
106
  atomicWriteFile,
87
107
  clientHeadersOf,
88
108
  clientIdentityOf,
89
109
  clientNameOf,
110
+ integrationDisabled,
111
+ integrationDisabledBy,
112
+ isRunProfile,
90
113
  log,
91
114
  readJsonFile,
115
+ resolveRunBudgetMs,
116
+ resolveRunProfile,
117
+ runProfileWarning,
92
118
  spawnCommand,
93
- spawnCommandSync
94
- } from "./chunk-QDM4EFI7.js";
119
+ spawnCommandSync,
120
+ workdirExposesHome
121
+ } from "./chunk-AFDD53DD.js";
95
122
 
96
123
  // src/integration/tray.ts
97
124
  import { INBOX_TRAY_MAX_LIMIT } from "agentchatme";
@@ -137,45 +164,36 @@ function clearOfferDeclined(home) {
137
164
  writeState(home, state);
138
165
  }
139
166
 
140
- // src/skill/identity-line.ts
141
- import { integration as kit } from "agentchatme";
142
- function identityLineOf(texts) {
143
- return texts.identity_line;
144
- }
145
- function normalizeHandle(handle) {
146
- return kit.anchor.normalizeHandle(handle);
147
- }
148
-
149
167
  // src/anchor/block.ts
150
168
  import * as fs from "fs";
151
169
  import * as path from "path";
152
- import { integration as kit2 } from "agentchatme";
153
- var ANCHOR_START = kit2.anchor.START_MARKER;
154
- var ANCHOR_END = kit2.anchor.END_MARKER;
170
+ import { integration as kit } from "agentchatme";
171
+ var ANCHOR_START = kit.anchor.START_MARKER;
172
+ var ANCHOR_END = kit.anchor.END_MARKER;
155
173
  var MARKER_LINES = /* @__PURE__ */ new Set([
156
- kit2.anchor.START_MARKER,
157
- kit2.anchor.END_MARKER,
158
- kit2.anchor.LEGACY_START_MARKER,
159
- kit2.anchor.LEGACY_END_MARKER
174
+ kit.anchor.START_MARKER,
175
+ kit.anchor.END_MARKER,
176
+ kit.anchor.LEGACY_START_MARKER,
177
+ kit.anchor.LEGACY_END_MARKER
160
178
  ]);
161
179
  function renderAnchorBlock(identityLine, handle) {
162
180
  if (typeof identityLine !== "string" || identityLine.trim().length === 0) throw new Error("renderAnchorBlock: the identity line is empty");
163
- if (typeof handle !== "string" || kit2.anchor.normalizeHandle(handle).length === 0) throw new Error("renderAnchorBlock: the handle is empty");
181
+ if (typeof handle !== "string" || kit.anchor.normalizeHandle(handle).length === 0) throw new Error("renderAnchorBlock: the handle is empty");
164
182
  if (identityLine.split(/\r?\n/).some((line) => MARKER_LINES.has(line.trim()))) {
165
183
  throw new Error("renderAnchorBlock: the identity line holds an AgentChat marker line");
166
184
  }
167
- return kit2.anchor.render({ identity_line: identityLine, identity_line_template: "" }, handle);
185
+ return kit.anchor.render({ identity_line: identityLine, identity_line_template: "" }, handle);
168
186
  }
169
187
  function writeAnchor(filePath, block, expectHandle) {
170
188
  if (expectHandle !== void 0) {
171
- const bare = kit2.anchor.normalizeHandle(expectHandle);
172
- const claimed = kit2.anchor.find(block)?.handle ?? null;
189
+ const bare = kit.anchor.normalizeHandle(expectHandle);
190
+ const claimed = kit.anchor.find(block)?.handle ?? null;
173
191
  if (claimed !== bare) {
174
192
  throw new Error(`writeAnchor: the block for ${filePath} does not name @${bare} \u2014 nothing was written`);
175
193
  }
176
194
  }
177
195
  const existing = readInstructionFile(filePath) ?? { bom: "", text: "" };
178
- const next = untilStable(existing.text, (text) => kit2.anchor.replace(text, block));
196
+ const next = untilStable(existing.text, (text) => kit.anchor.replace(text, block));
179
197
  if (next !== existing.text) writeInstructionFile(filePath, existing.bom + next);
180
198
  if (expectHandle !== void 0 && readInstructionFile(filePath)?.text !== next) {
181
199
  throw new Error(`writeAnchor: ${filePath} did not read back as written \u2014 remove the agentchat block manually and re-run`);
@@ -194,14 +212,14 @@ function untilStable(text, step) {
194
212
  function removeAnchorAt(filePath) {
195
213
  const existing = readInstructionFile(filePath);
196
214
  if (existing === null) return "noop";
197
- const next = untilStable(existing.text, (text) => kit2.anchor.remove(text));
215
+ const next = untilStable(existing.text, (text) => kit.anchor.remove(text));
198
216
  if (next === existing.text) return "noop";
199
217
  writeInstructionFile(filePath, existing.bom + next);
200
218
  return "removed";
201
219
  }
202
220
  function hasAnchorAt(filePath) {
203
221
  const file = readInstructionFile(filePath);
204
- return file !== null && kit2.anchor.find(file.text) !== null;
222
+ return file !== null && kit.anchor.find(file.text) !== null;
205
223
  }
206
224
  function anchorIsCurrentAt(filePath, identityLine, handle) {
207
225
  const file = readInstructionFile(filePath);
@@ -212,21 +230,21 @@ function anchorIsCurrentAt(filePath, identityLine, handle) {
212
230
  } catch {
213
231
  return false;
214
232
  }
215
- if (kit2.anchor.find(file.text) === null) return false;
216
- return kit2.anchor.replace(file.text, expected) === file.text;
233
+ if (kit.anchor.find(file.text) === null) return false;
234
+ return kit.anchor.replace(file.text, expected) === file.text;
217
235
  }
218
236
  function readAnchorHandleAt(filePath, template) {
219
237
  const file = readInstructionFile(filePath);
220
238
  return file === null ? null : readAnchorHandleFrom(file.text, template);
221
239
  }
222
240
  function readAnchorHandleFrom(text, template) {
223
- return kit2.anchor.find(text.replace(/^\uFEFF/, ""), { template })?.handle ?? null;
241
+ return kit.anchor.find(text.replace(/^\uFEFF/, ""), { template })?.handle ?? null;
224
242
  }
225
243
  function upsertAnchorBlock(existing, block) {
226
- return kit2.anchor.replace(existing, block);
244
+ return kit.anchor.replace(existing, block);
227
245
  }
228
246
  function stripAnchorBlock(existing) {
229
- return kit2.anchor.remove(existing);
247
+ return kit.anchor.remove(existing);
230
248
  }
231
249
  var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
232
250
  function readInstructionFile(filePath) {
@@ -1028,6 +1046,29 @@ function createIdentityCommands(profile) {
1028
1046
  if (anchorFile !== null) checks.push(await identityLineCheck(anchorFile, creds, apiBase, texts, opts.fix === true));
1029
1047
  }
1030
1048
  const integration = readIntegrationStatus(home);
1049
+ const runsFailing = integration?.runs_failing ?? null;
1050
+ const gaveUp = integration?.unservable ?? null;
1051
+ if (gaveUp !== null) {
1052
+ checks.push({
1053
+ name: "runs",
1054
+ verdict: "FAIL",
1055
+ detail: gaveUp.reason === "key_refused" ? `this home gave the agent up: the server refuses its key (${gaveUp.detail}); it looks again at ${gaveUp.retry_at} (every 5 min) and takes the agent back whenever no other listener holds it, but only a key the server accepts can serve \u2014 sign in again here (login/recover; that clears this at once), or log out if another home serves the agent` : `this home gave the agent up: ${gaveUp.detail} (since ${gaveUp.since}); its runtime is checked again at ${gaveUp.retry_at} (every 60 s) and a check that passes clears this; it takes the agent back whenever no other listener holds it`
1056
+ });
1057
+ } else if (runsFailing !== null) {
1058
+ checks.push({
1059
+ name: "runs",
1060
+ verdict: "FAIL",
1061
+ detail: `runs failing: ${runsFailing.reason} \u2014 since ${runsFailing.since}, ${runsFailing.runs_missed} run(s) missed (checked ${runsFailing.checked_at}); fix that, and the next run that succeeds clears this`
1062
+ });
1063
+ }
1064
+ const standby = integration?.standby ?? null;
1065
+ if (standby !== null) {
1066
+ checks.push({
1067
+ name: "single-listener",
1068
+ verdict: "WARN",
1069
+ detail: `another listener serves this agent: ${standby.home ?? "another identity home"}${standby.pid !== null ? ` (pid ${standby.pid})` : ""}; this one stands by (no socket, no run) so no message is answered twice`
1070
+ });
1071
+ }
1031
1072
  const cycles = integration?.recent_cycles ?? [];
1032
1073
  if (cycles.length > 0) {
1033
1074
  const failing = failedCyclesInARow(integration);
@@ -1551,9 +1592,11 @@ function killLauncher(label, mode) {
1551
1592
  return run(mode === "win32" ? "powershell" : "powershell.exe", ["-NoProfile", "-Command", ps]);
1552
1593
  }
1553
1594
  function lockedDaemonPid(home) {
1595
+ const file = path2.join(home, DAEMON_LOCK_FILE);
1554
1596
  try {
1555
- const pid = Number.parseInt(fs2.readFileSync(path2.join(home, "daemon.lock"), "utf-8").trim(), 10);
1556
- return Number.isInteger(pid) && pid > 0 ? pid : null;
1597
+ const holder = parseLease(fs2.readFileSync(file, "utf-8"));
1598
+ if (holder === null || !Number.isInteger(holder.pid) || holder.pid <= 0) return null;
1599
+ return holderLive(holder, { mtimeMs: fs2.statSync(file).mtimeMs }) ? holder.pid : null;
1557
1600
  } catch {
1558
1601
  return null;
1559
1602
  }
@@ -1710,8 +1753,10 @@ function serviceInstalled(opts) {
1710
1753
  return unitInstalled(serviceLabel(opts.label));
1711
1754
  }
1712
1755
  export {
1756
+ AGENT_LOCK_DIR_ENV,
1713
1757
  ANCHOR_END,
1714
1758
  ANCHOR_START,
1759
+ DAEMON_LOCK_FILE,
1715
1760
  DEFAULT_API_BASE,
1716
1761
  DEFAULT_RUN_BUDGET_MS,
1717
1762
  DEFAULT_RUN_PROFILE,
@@ -1722,6 +1767,8 @@ export {
1722
1767
  INTEGRATION_ENV_LEGACY,
1723
1768
  INTEGRATION_ENV_LEGACY_REMOVAL,
1724
1769
  INTEGRATION_STATUS_FILE,
1770
+ LEASE_RENEW_MS,
1771
+ LEASE_STALE_MS,
1725
1772
  LEGACY_STATUS_FILE,
1726
1773
  MANUAL,
1727
1774
  MANUAL_FETCH_TIMEOUT_MS,
@@ -1730,14 +1777,22 @@ export {
1730
1777
  MANUAL_MAX_REDIRECTS,
1731
1778
  MANUAL_SKILL_NAME,
1732
1779
  MANUAL_URL,
1780
+ PEER_RUN_WARNING,
1781
+ PROBE_KILL_WAIT_MS,
1733
1782
  REPEATED_FAILED_CYCLES,
1734
1783
  RUN_BUDGET_ENV,
1735
1784
  RUN_PROFILES,
1736
1785
  RUN_PROFILE_ENV,
1737
1786
  SOCKET_STATES,
1787
+ STANDBY_MARKER_FRESH_MS,
1738
1788
  TEXTS_CACHE_FILE,
1739
1789
  VERSION,
1790
+ acquireAgentLock,
1740
1791
  acquireLeaderLock,
1792
+ acquireLease,
1793
+ agentLockDir,
1794
+ agentLockPath,
1795
+ agentLockRefusal,
1741
1796
  alwaysOnHealth,
1742
1797
  alwaysOnOptedOut,
1743
1798
  alwaysOnState,
@@ -1747,6 +1802,7 @@ export {
1747
1802
  atomicCopyFile,
1748
1803
  atomicWriteFile,
1749
1804
  beat,
1805
+ bootedAt,
1750
1806
  checkManual,
1751
1807
  clearAlwaysOnInstalledVersion,
1752
1808
  clearAlwaysOnOptOut,
@@ -1755,6 +1811,7 @@ export {
1755
1811
  clearIntegrationStatus,
1756
1812
  clearOfferDeclined,
1757
1813
  clearPending,
1814
+ clearStandby,
1758
1815
  clientHeadersOf,
1759
1816
  clientIdentityOf,
1760
1817
  clientNameOf,
@@ -1768,6 +1825,7 @@ export {
1768
1825
  formatIntegrationStatus,
1769
1826
  fromKitResult,
1770
1827
  hasAnchorAt,
1828
+ holderLive,
1771
1829
  hostedMcpUrl,
1772
1830
  identityLineOf,
1773
1831
  idle,
@@ -1779,6 +1837,7 @@ export {
1779
1837
  isRunProfile,
1780
1838
  isRunnerFactory,
1781
1839
  launchdPlist,
1840
+ leaseHolder,
1782
1841
  loadTexts,
1783
1842
  log,
1784
1843
  manualFrontMatter,
@@ -1787,10 +1846,13 @@ export {
1787
1846
  markAlwaysOnInstalledVersion,
1788
1847
  markAlwaysOnOptOut,
1789
1848
  markAlwaysOnWanted,
1849
+ markStandby,
1790
1850
  normalizeHandle,
1791
1851
  offerDeclined,
1852
+ parseLease,
1792
1853
  parseServiceHome,
1793
1854
  pendingPath,
1855
+ pidAlive,
1794
1856
  planForTest,
1795
1857
  probeHostedMcp,
1796
1858
  readAlwaysOnInstalledVersion,
@@ -1808,9 +1870,12 @@ export {
1808
1870
  renderAnchorBlock,
1809
1871
  renderDeclinedBlock,
1810
1872
  renderUnregisteredBlock,
1873
+ resetLeaseObservations,
1811
1874
  resolveIdentity,
1812
1875
  resolveRunBudgetMs,
1813
1876
  resolveRunProfile,
1877
+ runProbe,
1878
+ runProfileWarning,
1814
1879
  serviceDefinitionCurrent,
1815
1880
  serviceInstalled,
1816
1881
  serviceLabelFor,
@@ -1819,6 +1884,8 @@ export {
1819
1884
  setServiceHostOsForTests,
1820
1885
  spawnCommand,
1821
1886
  spawnCommandSync,
1887
+ standbyMarkerPath,
1888
+ standbyWaiting,
1822
1889
  statePath,
1823
1890
  stripAnchorBlock,
1824
1891
  syncManual,
@@ -1828,6 +1895,7 @@ export {
1828
1895
  traySummary,
1829
1896
  uninstallService,
1830
1897
  upsertAnchorBlock,
1898
+ workdirExposesHome,
1831
1899
  writeAnchor,
1832
1900
  writeCredentials,
1833
1901
  writeIntegrationStatus,