@agentchatme/agent-core 0.4.0 → 0.4.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/dist/index.js CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  HEARTBEAT_FILE,
7
7
  INTEGRATION_ENV,
8
8
  INTEGRATION_ENV_LEGACY,
9
+ INTEGRATION_ENV_LEGACY_REMOVAL,
9
10
  INTEGRATION_STATUS_FILE,
10
11
  LEGACY_STATUS_FILE,
11
12
  MANUAL,
@@ -73,12 +74,13 @@ import {
73
74
  writeCredentials,
74
75
  writeIntegrationStatus,
75
76
  writePending
76
- } from "./chunk-W3LYBKIV.js";
77
+ } from "./chunk-7LAAAXUO.js";
77
78
  import {
78
79
  HOSTED_MCP_PATH,
80
+ VERSION,
79
81
  hostedMcpUrl,
80
82
  probeHostedMcp
81
- } from "./chunk-HOY2FERJ.js";
83
+ } from "./chunk-XDWBTX5J.js";
82
84
  import {
83
85
  atomicCopyFile,
84
86
  atomicWriteFile,
@@ -89,10 +91,7 @@ import {
89
91
  readJsonFile,
90
92
  spawnCommand,
91
93
  spawnCommandSync
92
- } from "./chunk-NCSF3CCV.js";
93
-
94
- // src/version.ts
95
- var VERSION = "0.4.0";
94
+ } from "./chunk-QDM4EFI7.js";
96
95
 
97
96
  // src/integration/tray.ts
98
97
  import { INBOX_TRAY_MAX_LIMIT } from "agentchatme";
@@ -139,114 +138,169 @@ function clearOfferDeclined(home) {
139
138
  }
140
139
 
141
140
  // src/skill/identity-line.ts
141
+ import { integration as kit } from "agentchatme";
142
142
  function identityLineOf(texts) {
143
143
  return texts.identity_line;
144
144
  }
145
145
  function normalizeHandle(handle) {
146
- return handle.trim().replace(/^@/, "").toLowerCase();
146
+ return kit.anchor.normalizeHandle(handle);
147
147
  }
148
148
 
149
149
  // src/anchor/block.ts
150
150
  import * as fs from "fs";
151
151
  import * as path from "path";
152
- var ANCHOR_START = "<!-- agentchat:start -->";
153
- var ANCHOR_END = "<!-- agentchat:end -->";
154
- var LEGACY_ANCHOR_START = "<!-- agentchat-skill:start -->";
155
- var LEGACY_ANCHOR_END = "<!-- agentchat-skill:end -->";
156
- function renderAnchorBlock(identityLine) {
157
- const line = identityLine.trim();
158
- if (line.length === 0) throw new Error("renderAnchorBlock: the identity line is empty");
159
- return [ANCHOR_START, line, ANCHOR_END].join("\n");
152
+ import { integration as kit2 } from "agentchatme";
153
+ var ANCHOR_START = kit2.anchor.START_MARKER;
154
+ var ANCHOR_END = kit2.anchor.END_MARKER;
155
+ 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
160
+ ]);
161
+ function renderAnchorBlock(identityLine, handle) {
162
+ 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");
164
+ if (identityLine.split(/\r?\n/).some((line) => MARKER_LINES.has(line.trim()))) {
165
+ throw new Error("renderAnchorBlock: the identity line holds an AgentChat marker line");
166
+ }
167
+ return kit2.anchor.render({ identity_line: identityLine, identity_line_template: "" }, handle);
160
168
  }
161
169
  function writeAnchor(filePath, block, expectHandle) {
162
- fs.mkdirSync(path.dirname(filePath), { recursive: true });
163
- const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, "utf-8") : "";
164
- fs.writeFileSync(filePath, upsertAnchorBlock(existing, block), "utf-8");
165
170
  if (expectHandle !== void 0) {
166
- const verify = fs.readFileSync(filePath, "utf-8");
167
- if (!verify.includes(`@${expectHandle}`)) {
168
- throw new Error(
169
- `writeAnchor: handle @${expectHandle} did not land in ${filePath} \u2014 remove the agentchat block manually and re-run.`
170
- );
171
+ const bare = kit2.anchor.normalizeHandle(expectHandle);
172
+ const claimed = kit2.anchor.find(block)?.handle ?? null;
173
+ if (claimed !== bare) {
174
+ throw new Error(`writeAnchor: the block for ${filePath} does not name @${bare} \u2014 nothing was written`);
171
175
  }
172
176
  }
177
+ const existing = readInstructionFile(filePath) ?? { bom: "", text: "" };
178
+ const next = untilStable(existing.text, (text) => kit2.anchor.replace(text, block));
179
+ if (next !== existing.text) writeInstructionFile(filePath, existing.bom + next);
180
+ if (expectHandle !== void 0 && readInstructionFile(filePath)?.text !== next) {
181
+ throw new Error(`writeAnchor: ${filePath} did not read back as written \u2014 remove the agentchat block manually and re-run`);
182
+ }
173
183
  return "written";
174
184
  }
185
+ function untilStable(text, step) {
186
+ let current = text;
187
+ for (let i = 0; i < 8; i++) {
188
+ const next = step(current);
189
+ if (next === current) return current;
190
+ current = next;
191
+ }
192
+ return current;
193
+ }
175
194
  function removeAnchorAt(filePath) {
176
- if (!fs.existsSync(filePath)) return "noop";
177
- const existing = fs.readFileSync(filePath, "utf-8");
178
- const next = stripAnchorBlock(existing);
179
- if (next === existing) return "noop";
180
- fs.writeFileSync(filePath, next, "utf-8");
195
+ const existing = readInstructionFile(filePath);
196
+ if (existing === null) return "noop";
197
+ const next = untilStable(existing.text, (text) => kit2.anchor.remove(text));
198
+ if (next === existing.text) return "noop";
199
+ writeInstructionFile(filePath, existing.bom + next);
181
200
  return "removed";
182
201
  }
183
202
  function hasAnchorAt(filePath) {
184
- if (!fs.existsSync(filePath)) return false;
185
- return findBlock(fs.readFileSync(filePath, "utf-8"), ANCHOR_START, ANCHOR_END) !== null;
186
- }
187
- function anchorIsCurrentAt(filePath, identityLine) {
188
- if (!fs.existsSync(filePath)) return false;
189
- const text = fs.readFileSync(filePath, "utf-8");
190
- const block = findBlock(text, ANCHOR_START, ANCHOR_END);
191
- if (block === null) return false;
192
- return text.slice(block.from, block.to) === renderAnchorBlock(identityLine);
193
- }
194
- var IDENTITY_HANDLE_RE = /I am @([^\s]+) on AgentChat/;
195
- var LEGACY_HANDLE_RE = /\*\*@([^*\s]+)\*\*/;
196
- function readAnchorHandleAt(filePath) {
197
- if (!fs.existsSync(filePath)) return null;
198
- return readAnchorHandleFrom(fs.readFileSync(filePath, "utf-8"));
199
- }
200
- function readAnchorHandleFrom(text) {
201
- const block = findBlock(text, ANCHOR_START, ANCHOR_END);
202
- if (block === null) return null;
203
- const inside = text.slice(block.from, block.to);
204
- const match = IDENTITY_HANDLE_RE.exec(inside) ?? LEGACY_HANDLE_RE.exec(inside);
205
- return match?.[1] ?? null;
206
- }
207
- function lineAnchoredIndex(text, marker, fromIndex = 0) {
208
- let idx = text.indexOf(marker, fromIndex);
209
- while (idx >= 0) {
210
- const lineStart = text.lastIndexOf("\n", idx - 1) + 1;
211
- const lineEndRaw = text.indexOf("\n", idx);
212
- const lineEnd = lineEndRaw === -1 ? text.length : lineEndRaw;
213
- if (text.slice(lineStart, lineEnd).trim() === marker) {
214
- return { start: lineStart, end: lineEnd };
215
- }
216
- idx = text.indexOf(marker, idx + marker.length);
203
+ const file = readInstructionFile(filePath);
204
+ return file !== null && kit2.anchor.find(file.text) !== null;
205
+ }
206
+ function anchorIsCurrentAt(filePath, identityLine, handle) {
207
+ const file = readInstructionFile(filePath);
208
+ if (file === null) return false;
209
+ let expected;
210
+ try {
211
+ expected = renderAnchorBlock(identityLine, handle);
212
+ } catch {
213
+ return false;
217
214
  }
218
- return null;
215
+ if (kit2.anchor.find(file.text) === null) return false;
216
+ return kit2.anchor.replace(file.text, expected) === file.text;
217
+ }
218
+ function readAnchorHandleAt(filePath, template) {
219
+ const file = readInstructionFile(filePath);
220
+ return file === null ? null : readAnchorHandleFrom(file.text, template);
219
221
  }
220
- function findBlock(text, startMarker, endMarker) {
221
- const start = lineAnchoredIndex(text, startMarker);
222
- if (start === null) return null;
223
- const end = lineAnchoredIndex(text, endMarker, start.end);
224
- if (end === null) return null;
225
- return { from: start.start, to: end.end };
222
+ function readAnchorHandleFrom(text, template) {
223
+ return kit2.anchor.find(text.replace(/^\uFEFF/, ""), { template })?.handle ?? null;
226
224
  }
227
225
  function upsertAnchorBlock(existing, block) {
228
- const cleaned = stripAnchorBlock(existing);
229
- const trimmed = cleaned.replace(/\n+$/, "");
230
- if (trimmed.length === 0) return block + "\n";
231
- return trimmed + "\n\n" + block + "\n";
226
+ return kit2.anchor.replace(existing, block);
232
227
  }
233
228
  function stripAnchorBlock(existing) {
234
- const afterUnified = stripAllBlocks(existing, ANCHOR_START, ANCHOR_END);
235
- return stripAllBlocks(afterUnified, LEGACY_ANCHOR_START, LEGACY_ANCHOR_END);
236
- }
237
- function stripAllBlocks(existing, start, end) {
238
- let text = existing;
239
- for (let i = 0; i < 1e4; i++) {
240
- const block = findBlock(text, start, end);
241
- if (block === null) return text;
242
- const before = text.slice(0, block.from).replace(/\n+$/, "");
243
- const after = text.slice(block.to).replace(/^\n+/, "");
244
- if (before.length === 0 && after.length === 0) return "";
245
- if (before.length === 0) text = after.endsWith("\n") ? after : after + "\n";
246
- else if (after.length === 0) text = before + "\n";
247
- else text = before + "\n\n" + after + (after.endsWith("\n") ? "" : "\n");
229
+ return kit2.anchor.remove(existing);
230
+ }
231
+ var UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
232
+ function readInstructionFile(filePath) {
233
+ let bytes;
234
+ try {
235
+ bytes = fs.readFileSync(filePath);
236
+ } catch (err) {
237
+ if (err.code === "ENOENT") return null;
238
+ throw err;
239
+ }
240
+ let decoded;
241
+ try {
242
+ decoded = UTF8.decode(bytes);
243
+ } catch {
244
+ throw new Error(`${filePath} is not UTF-8 text; the AgentChat block is not read or written there (save the file as UTF-8 and run the command again)`);
245
+ }
246
+ return decoded.startsWith("\uFEFF") ? { bom: "\uFEFF", text: decoded.slice(1) } : { bom: "", text: decoded };
247
+ }
248
+ function resolveWriteTarget(filePath) {
249
+ let current = filePath;
250
+ for (let hops = 0; hops < 40; hops++) {
251
+ let stat;
252
+ try {
253
+ stat = fs.lstatSync(current);
254
+ } catch (err) {
255
+ if (err.code === "ENOENT") return current;
256
+ throw err;
257
+ }
258
+ if (!stat.isSymbolicLink()) return current;
259
+ const link = fs.readlinkSync(current);
260
+ const joined = path.isAbsolute(link) ? link : `${fs.realpathSync.native(path.dirname(current))}${path.sep}${link}`;
261
+ current = path.join(fs.realpathSync.native(path.dirname(joined)), path.basename(joined));
262
+ }
263
+ throw Object.assign(new Error(`${filePath}: too many symbolic links`), { code: "ELOOP" });
264
+ }
265
+ var IN_PLACE_FALLBACK_CODES = /* @__PURE__ */ new Set(["EACCES", "EPERM", "EROFS", "EBUSY", "EXDEV"]);
266
+ function stillWritable(file) {
267
+ try {
268
+ fs.accessSync(file, fs.constants.W_OK);
269
+ return true;
270
+ } catch {
271
+ return false;
272
+ }
273
+ }
274
+ function writeInstructionFile(filePath, data) {
275
+ const target = resolveWriteTarget(filePath);
276
+ let existing = null;
277
+ try {
278
+ existing = fs.statSync(target);
279
+ } catch (err) {
280
+ if (err.code !== "ENOENT") throw err;
281
+ }
282
+ if (existing !== null) {
283
+ if (!existing.isFile()) throw new Error(`${filePath} is not a regular file`);
284
+ fs.accessSync(target, fs.constants.W_OK);
285
+ } else {
286
+ fs.mkdirSync(path.dirname(target), { recursive: true });
287
+ }
288
+ try {
289
+ atomicWriteFile(target, data, existing === null ? void 0 : existing.mode & 4095);
290
+ } catch (err) {
291
+ const code = err.code;
292
+ if (existing !== null && code !== void 0 && IN_PLACE_FALLBACK_CODES.has(code) && stillWritable(target)) {
293
+ fs.writeFileSync(target, data, "utf-8");
294
+ return;
295
+ }
296
+ throw err;
297
+ }
298
+ if (existing !== null && typeof process.getuid === "function" && process.getuid() === 0 && (existing.uid !== 0 || existing.gid !== 0)) {
299
+ try {
300
+ fs.chownSync(target, existing.uid, existing.gid);
301
+ } catch {
302
+ }
248
303
  }
249
- return text;
250
304
  }
251
305
 
252
306
  // src/anchor/offer.ts
@@ -326,6 +380,13 @@ var HANDLE_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
326
380
  function validHandle(handle) {
327
381
  return handle.length >= 3 && handle.length <= 30 && HANDLE_PATTERN.test(handle);
328
382
  }
383
+ var NO_TEXTS = Object.freeze({ identityLine: null, identityLineTemplate: null, manual: null });
384
+ function errorText(err) {
385
+ return err instanceof Error ? err.message : String(err);
386
+ }
387
+ function contextOf(texts) {
388
+ return { identityLine: identityLineOf(texts), identityLineTemplate: texts.identity_line_template, manual: texts.manual };
389
+ }
329
390
  async function prompt(question) {
330
391
  const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
331
392
  try {
@@ -401,7 +462,7 @@ function createIdentityCommands(profile) {
401
462
  const apiClient = (apiKey, apiBase, budget = "quick") => budget === "quick" ? new AgentChatClient({ apiKey, baseUrl: apiBase, clientIdentity, timeoutMs: 5e3, retry: { maxRetries: 0, baseDelayMs: 0, maxDelayMs: 0 } }) : new AgentChatClient({ apiKey, baseUrl: apiBase, clientIdentity, timeoutMs: 15e3 });
402
463
  async function served(apiKey, apiBase) {
403
464
  const loaded = await loadTexts({ home: profile.home(), runtime: profile.client.runtime, client: apiClient(apiKey, apiBase), timeoutMs: 5e3 });
404
- return loaded === null ? { identityLine: null, manual: null } : { identityLine: identityLineOf(loaded.texts), manual: loaded.texts.manual };
465
+ return loaded === null ? NO_TEXTS : contextOf(loaded.texts);
405
466
  }
406
467
  async function printExtraStatus(context) {
407
468
  if (profile.extraStatus === void 0) return;
@@ -426,14 +487,14 @@ function createIdentityCommands(profile) {
426
487
  if (profile.anchorFile === void 0) return [];
427
488
  const file = profile.anchorFile();
428
489
  const label = anchorLabelOf(profile);
429
- if (profile.isWired !== void 0 && !profile.isWired() && !hasAnchorAt(file)) return [];
430
- if (identityLine === null) {
431
- return [
432
- ` ${label}: identity line not written \u2014 GET /v1/texts could not be reached and nothing is cached; run \`${invocation()} doctor --fix\` once online`
433
- ];
434
- }
435
490
  try {
436
- writeAnchor(file, profile.renderAnchor(identityLine), handle);
491
+ if (profile.isWired !== void 0 && !profile.isWired() && !hasAnchorAt(file)) return [];
492
+ if (identityLine === null) {
493
+ return [
494
+ ` ${label}: identity line not written \u2014 GET /v1/texts could not be reached and nothing is cached; run \`${invocation()} doctor --fix\` once online`
495
+ ];
496
+ }
497
+ writeAnchor(file, profile.renderAnchor(identityLine, handle), handle);
437
498
  return [` ${label}: @${handle} \u2192 ${file}`];
438
499
  } catch (err) {
439
500
  return [` ${label}: FAILED \u2014 ${String(err)}`];
@@ -718,6 +779,16 @@ function createIdentityCommands(profile) {
718
779
  return 1;
719
780
  }
720
781
  }
782
+ function anchorView(file, identityLine, handle) {
783
+ if (file === null) return { present: null, current: null, error: null };
784
+ try {
785
+ const present = hasAnchorAt(file);
786
+ const current = identityLine === null || handle === null ? null : anchorIsCurrentAt(file, identityLine, handle);
787
+ return { present, current, error: null };
788
+ } catch (err) {
789
+ return { present: null, current: null, error: errorText(err) };
790
+ }
791
+ }
721
792
  async function runStatus(opts) {
722
793
  const home = profile.home();
723
794
  const anchorFile = profile.anchorFile?.() ?? null;
@@ -732,9 +803,7 @@ function createIdentityCommands(profile) {
732
803
  host: profile.id,
733
804
  pending: pending !== null,
734
805
  pending_kind: pending?.kind ?? null,
735
- integration,
736
- // The previous key, kept one release for scripts that read it.
737
- doorbell: integration
806
+ integration
738
807
  })
739
808
  );
740
809
  } else if (pending?.kind === "recover") {
@@ -760,6 +829,7 @@ function createIdentityCommands(profile) {
760
829
  const refused = status === 401 || status === 403;
761
830
  const detail = apiErr(err);
762
831
  if (opts.json) {
832
+ const offline = anchorView(anchorFile, null, null);
763
833
  console.log(
764
834
  JSON.stringify({
765
835
  configured: true,
@@ -769,9 +839,9 @@ function createIdentityCommands(profile) {
769
839
  key_source: identity.source,
770
840
  api_base: identity.apiBase,
771
841
  home,
772
- anchor: anchorFile === null ? null : hasAnchorAt(anchorFile),
773
- integration,
774
- doorbell: integration
842
+ anchor: offline.present,
843
+ anchor_error: offline.error,
844
+ integration
775
845
  })
776
846
  );
777
847
  } else {
@@ -787,14 +857,14 @@ function createIdentityCommands(profile) {
787
857
  ].join("\n")
788
858
  );
789
859
  const cached = readCachedTexts(home, profile.client.runtime);
790
- await printExtraStatus(cached === null ? { identityLine: null, manual: null } : { identityLine: identityLineOf(cached.texts), manual: cached.texts.manual });
860
+ await printExtraStatus(cached === null ? NO_TEXTS : contextOf(cached.texts));
791
861
  }
792
862
  return 1;
793
863
  }
794
864
  const tray = await traySummary(client).catch(() => null);
795
865
  const texts = await served(identity.apiKey, identity.apiBase);
796
866
  const identityLine = texts.identityLine;
797
- const anchorCurrent = identityLine === null || anchorFile === null ? null : anchorIsCurrentAt(anchorFile, identityLine);
867
+ const anchor = anchorView(anchorFile, identityLine, me.handle);
798
868
  if (opts.json) {
799
869
  console.log(
800
870
  JSON.stringify({
@@ -806,11 +876,12 @@ function createIdentityCommands(profile) {
806
876
  key_source: identity.source,
807
877
  api_base: identity.apiBase,
808
878
  home,
809
- anchor: anchorFile === null ? null : hasAnchorAt(anchorFile),
810
- /** null: the served texts were unavailable (or there is no instruction file), so currency could not be checked. */
811
- anchor_current: anchorCurrent,
812
- integration,
813
- doorbell: integration
879
+ anchor: anchor.present,
880
+ /** null: the served texts were unavailable (or there is no instruction file, or it cannot be read), so currency could not be checked. */
881
+ anchor_current: anchor.current,
882
+ /** Why the instruction file could not be read, or null. */
883
+ anchor_error: anchor.error,
884
+ integration
814
885
  })
815
886
  );
816
887
  } else {
@@ -822,7 +893,7 @@ function createIdentityCommands(profile) {
822
893
  `Key source: ${identity.source} (${identity.source === "file" ? credentialsPath(home) : "AGENTCHAT_API_KEY"})`,
823
894
  `API: ${identity.apiBase}`,
824
895
  ...anchorFile === null ? [] : [
825
- `Identity line: ${hasAnchorAt(anchorFile) ? anchorCurrent === null ? "present (served texts unavailable \u2014 not checked)" : anchorCurrent ? "current" : "present (older text \u2014 `doctor --fix` refreshes it)" : "missing"} (${anchorFile})`
896
+ `Identity line: ${anchor.error !== null ? `unreadable \u2014 ${anchor.error}` : anchor.present ? anchor.current === null ? "present (served texts unavailable \u2014 not checked)" : anchor.current ? "current" : "present (older text \u2014 `doctor --fix` refreshes it)" : "missing"} (${anchorFile})`
826
897
  ],
827
898
  ...formatIntegrationStatus(integration)
828
899
  ].join("\n")
@@ -841,9 +912,16 @@ function createIdentityCommands(profile) {
841
912
  any = true;
842
913
  reports.push(" credentials deleted");
843
914
  }
844
- if (anchorFile !== null && removeAnchorAt(anchorFile) === "removed") {
845
- any = true;
846
- reports.push(` ${anchorLabelOf(profile)} identity line removed`);
915
+ if (anchorFile !== null) {
916
+ try {
917
+ if (removeAnchorAt(anchorFile) === "removed") {
918
+ any = true;
919
+ reports.push(` ${anchorLabelOf(profile)} identity line removed`);
920
+ }
921
+ } catch (err) {
922
+ any = true;
923
+ reports.push(` could not remove the identity line from ${anchorFile}: ${errorText(err)} \u2014 remove the agentchat block by hand`);
924
+ }
847
925
  }
848
926
  if (profile.unplace !== void 0) {
849
927
  try {
@@ -866,9 +944,20 @@ function createIdentityCommands(profile) {
866
944
  );
867
945
  return 0;
868
946
  }
869
- async function identityLineCheck(anchorFile, creds, apiBase, identityLine, fix) {
870
- const claimed = readAnchorHandleAt(anchorFile);
871
- const current = identityLine === null ? null : anchorIsCurrentAt(anchorFile, identityLine);
947
+ function readIdentityLine(anchorFile, handle, texts) {
948
+ const claimed = readAnchorHandleAt(anchorFile, texts.identityLineTemplate);
949
+ const current = texts.identityLine === null ? null : anchorIsCurrentAt(anchorFile, texts.identityLine, handle);
950
+ return { claimed, current };
951
+ }
952
+ async function identityLineCheck(anchorFile, creds, apiBase, texts, fix) {
953
+ let claimed;
954
+ let current;
955
+ try {
956
+ ;
957
+ ({ claimed, current } = readIdentityLine(anchorFile, creds.handle, texts));
958
+ } catch (err) {
959
+ return { name: "identity-line", verdict: "FAIL", detail: `cannot read ${anchorFile}: ${errorText(err)}` };
960
+ }
872
961
  if (claimed === creds.handle && current === true) {
873
962
  return { name: "identity-line", verdict: "PASS", detail: `@${claimed} in ${anchorFile}` };
874
963
  }
@@ -879,11 +968,16 @@ function createIdentityCommands(profile) {
879
968
  if (fix) {
880
969
  const report = await placeIdentity(creds.handle, creds.api_key, apiBase);
881
970
  const failed = report.some((l) => l.includes("FAILED") || l.includes("not written"));
882
- return {
883
- name: "identity-line",
884
- verdict: failed ? "FAIL" : "PASS",
885
- detail: failed ? `could not repair: ${report.join("; ")}` : `repaired \u2192 @${creds.handle}`
886
- };
971
+ if (failed) return { name: "identity-line", verdict: "FAIL", detail: `could not repair: ${report.join("; ")}` };
972
+ try {
973
+ const after = readIdentityLine(anchorFile, creds.handle, texts);
974
+ if (after.claimed === creds.handle && after.current !== false) {
975
+ return { name: "identity-line", verdict: "PASS", detail: `repaired \u2192 @${creds.handle}` };
976
+ }
977
+ return { name: "identity-line", verdict: "FAIL", detail: `repair did not take: ${anchorFile} still ${after.claimed === null ? "has no identity line" : after.claimed !== creds.handle ? `says @${after.claimed}` : "differs from the served block"}${report.length > 0 ? ` (${report.join("; ").trim()})` : ""}` };
978
+ } catch (err) {
979
+ return { name: "identity-line", verdict: "FAIL", detail: `cannot read ${anchorFile} after the repair: ${errorText(err)}` };
980
+ }
887
981
  }
888
982
  return { name: "identity-line", verdict: "WARN", detail: `${why} \u2014 repair with \`${invocation()} doctor --fix\`` };
889
983
  }
@@ -891,8 +985,7 @@ function createIdentityCommands(profile) {
891
985
  const home = profile.home();
892
986
  const anchorFile = profile.anchorFile?.() ?? null;
893
987
  const checks = [];
894
- let identityLine = null;
895
- let manual = null;
988
+ let texts = NO_TEXTS;
896
989
  checks.push({ name: "node", verdict: "PASS", detail: process.version });
897
990
  checks.push({ name: "home", verdict: "PASS", detail: home });
898
991
  const creds = readCredentials(home);
@@ -928,11 +1021,11 @@ function createIdentityCommands(profile) {
928
1021
  }
929
1022
  }
930
1023
  const apiBase = identity?.apiBase ?? creds.api_base ?? DEFAULT_API_BASE;
931
- ({ identityLine, manual } = await served(creds.api_key, apiBase));
1024
+ texts = await served(creds.api_key, apiBase);
932
1025
  checks.push(
933
- identityLine === null ? { name: "texts", verdict: "WARN", detail: "GET /v1/texts could not be reached and nothing is cached \u2014 the identity line cannot be checked or placed until it can" } : { name: "texts", verdict: "PASS", detail: "served texts in hand" }
1026
+ texts.identityLine === null ? { name: "texts", verdict: "WARN", detail: "GET /v1/texts could not be reached and nothing is cached \u2014 the identity line cannot be checked or placed until it can" } : { name: "texts", verdict: "PASS", detail: "served texts in hand" }
934
1027
  );
935
- if (anchorFile !== null) checks.push(await identityLineCheck(anchorFile, creds, apiBase, identityLine, opts.fix === true));
1028
+ if (anchorFile !== null) checks.push(await identityLineCheck(anchorFile, creds, apiBase, texts, opts.fix === true));
936
1029
  }
937
1030
  const integration = readIntegrationStatus(home);
938
1031
  const cycles = integration?.recent_cycles ?? [];
@@ -947,7 +1040,13 @@ function createIdentityCommands(profile) {
947
1040
  } : { name: "wake-cycles", verdict: "PASS", detail: `last cycle ${last.outcome} at ${last.at} (${cycles.length} recent)` }
948
1041
  );
949
1042
  }
950
- if (profile.extraDoctorChecks !== void 0) checks.push(...await profile.extraDoctorChecks({ ...opts, identityLine, manual }));
1043
+ if (profile.extraDoctorChecks !== void 0) {
1044
+ try {
1045
+ checks.push(...await profile.extraDoctorChecks({ ...opts, ...texts }));
1046
+ } catch (err) {
1047
+ checks.push({ name: "host", verdict: "FAIL", detail: `the ${LABEL} checks failed: ${errorText(err)}` });
1048
+ }
1049
+ }
951
1050
  console.log(checks.map((c) => `${c.verdict.padEnd(4)} ${c.name}: ${c.detail}`).join("\n"));
952
1051
  return checks.some((c) => c.verdict === "FAIL") ? 1 : 0;
953
1052
  }
@@ -1621,6 +1720,7 @@ export {
1621
1720
  HOSTED_MCP_PATH,
1622
1721
  INTEGRATION_ENV,
1623
1722
  INTEGRATION_ENV_LEGACY,
1723
+ INTEGRATION_ENV_LEGACY_REMOVAL,
1624
1724
  INTEGRATION_STATUS_FILE,
1625
1725
  LEGACY_STATUS_FILE,
1626
1726
  MANUAL,