Files
plugins/packages/chat/lib
yetone b38ad66551 page-chat 0.1.8: the service takes its port once the old host lets go, instead of giving up after 6s (ferock on Discord)
ferock on Discord, after page-chat updated itself to 0.1.7: typing a
system prompt said "保存失败:Failed to fetch", and every message after
failed the same way. The system prompt wasn't the cause. Nothing was
listening on the page's port any more.

magpie swaps plugin hosts when a plugin is updated, installed or set
(Restart, or another magpie's change to plugins.json). An old host that
is still finishing a call (a reply, a tool call) keeps running, up to 10
minutes, and its page service keeps 3477. The new host's service tried
for 6s, threw "3477 一直被占用", and never tried again. When the old
host left, nobody had the port. The page's frame stayed on
127.0.0.1:3477, and every fetch failed until magpie was restarted.

startServer now tries for those 6s (bindTries x 500ms) without holding
up the host. Then it goes on trying once a second in the background
until the port is free, and stops when the service is closed. The page's
next request gets 403 with the new token, and the page reloads itself
as before. index.mjs logs "listening" when the port is bound.

Run (mote):
- Reproduced with real magpie serve (magpie origin/main 36af588e2) in a
  sandbox HOME, page-chat on port 3592. A plugin tool that takes 40s was
  in flight while page-chat 0.1.6 was auto-updated to 0.1.7 by
  KeepUpdated, or while another plugin was added. 0.1.7's service logged
  "could not start: 3592 一直被占用", and every save to /api/settings got
  000 (no listener) after the old host left, even with a fresh token.
  With this change the same run logs "还被占着" and then "listening".
  A save gets 403 (new token), and the page's reload saves 200.
- Without a busy host, an update came back on its own (403, reload). So
  did saves of Chinese, quoted, multi-line and 50k-char system prompts
  and a chat on 0.1.7. None of them failed.
- bun test chat.test.mjs: 31 pass. The two new tests are red on the old
  lib ("58585 一直被占用,服务起不来"), checked by hand.
- bun scripts/check.mjs ok.

Not tried: magpie's desktop window (WebKit). The page there was not
driven; its 403 reload path is unchanged.
2026-11-01 12:25:02 +08:00
..