Hot reload preserves IRC session (nick, channels, voice). (0.39.0)
This commit is contained in:
parent
8657304b96
commit
738323ebaf
3 changed files with 43 additions and 51 deletions
|
|
@ -1,3 +1,11 @@
|
|||
2026.07.21 - Hot reload preserves IRC session (nick, channels, voice). (0.39.0)
|
||||
|
||||
- `reload_do()` now accepts an `exec_new` parameter: when 0 (`!reload`/`!restart`), the child inherits the socket fd and SSL context directly and continues the event loop without reconnecting; when 1 (`!rebuild`/`!code`), the old fork+exec path is used.
|
||||
- Removed `irc_part_all()` from the reload path — the bot no longer parts channels on reload, so voice mode (+v) and channel presence are preserved.
|
||||
- Reset `pending_ai_pid`/`pending_ai_id` in the fork child to avoid stale state from orphaned AI processes.
|
||||
- `!reload` and `!restart` now keep the bot's IRC session alive (no nick collision, no rejoin, no lost voice). `!rebuild`/`!code` still do a clean QUIT+exec since the binary changed.
|
||||
- Version bumped to 0.39.0.
|
||||
|
||||
2026.07.21 - Strip opencode header from !ai responses. (0.38.3)
|
||||
|
||||
- Added `strip_opencode_header()`: removes the opencode header line (e.g. `> plan · big-pickle`) and any trailing blank lines from AI responses before sending to IRC.
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ A single-file IRC bot in C. Features a key-value store backed by SQLite3, weathe
|
|||
- **AI Queries**: Ask questions via `!ai` or by addressing the bot; powered by `opencode`
|
||||
- **Code Changes**: AI-powered code editing with auto-recompile and hot reload
|
||||
- **Chat Greetings**: Responds to `hi`, `hello`, `hey`, `bye`, and more
|
||||
- **Hot Reload**: Reload or restart without disconnecting from IRC
|
||||
- **Hot Reload**: Reload or restart without leaving channels, losing your nick, or dropping voice mode
|
||||
- **TLS**: Secure connections via OpenSSL
|
||||
- **Rate Limiting**: Per-nick protection against spam
|
||||
- **Reconnection**: Auto-reconnect with exponential backoff
|
||||
|
|
@ -248,8 +248,8 @@ The calculator uses `bc -l` (POSIX arbitrary-precision calculator with the math
|
|||
|
||||
| Command | Description |
|
||||
|-----------------------|-------------------------------------------------------------------------------------|
|
||||
| `!reload` | Hot reload (save TLS state, fork new process) |
|
||||
| `!restart` | Restart the bot |
|
||||
| `!reload` | Hot reload (preserve IRC session, fork-in-place) |
|
||||
| `!restart` | Restart the bot (preserves session, same as !reload) |
|
||||
| `!rebuild [pull]` | Rebuild and restart (ops only, in channels; `pull` runs Git pull before rebuilding) |
|
||||
| `!code <instruction>` | Make AI-powered code changes |
|
||||
|
||||
|
|
@ -272,7 +272,7 @@ Data is stored in `zynk.db` (SQLite3) in the working directory. The bot creates
|
|||
|
||||
## Architecture
|
||||
|
||||
`zynk` uses a single `poll()` loop with a 100 ms timeout. AI queries fork a child process that runs the `opencode` CLI; the parent picks up the result on the next poll iteration. SQLite3 is loaded at runtime via `dlopen` to avoid a compile-time dependency. The bot supports hot reload: `!reload` and `!restart` save TLS session state, fork a new process with the inherited socket, and resume without dropping from IRC.
|
||||
`zynk` uses a single `poll()` loop with a 100 ms timeout. AI queries fork a child process that runs the `opencode` CLI; the parent picks up the result on the next poll iteration. SQLite3 is loaded at runtime via `dlopen` to avoid a compile-time dependency. The bot supports hot reload: `!reload` and `!restart` fork without exec, so the child inherits the live socket and SSL session — the bot stays connected, keeps its nick, and remains voiced in channels. `!rebuild`/`!code` do a clean QUIT+exec since the binary changed on disk.
|
||||
|
||||
## License
|
||||
|
||||
|
|
|
|||
78
zynk.c
78
zynk.c
|
|
@ -28,7 +28,7 @@ int asprintf(char **, const char *, ...);
|
|||
|
||||
/* ---- constants ---- */
|
||||
|
||||
#define VERSION "0.38.3"
|
||||
#define VERSION "0.39.0"
|
||||
#define DEFAULT_SERVER "irc.libera.chat"
|
||||
#define OPENCODE_BIN "/usr/bin/opencode"
|
||||
#define CLR_RESET "\033[0m"
|
||||
|
|
@ -1194,40 +1194,20 @@ fail:
|
|||
}
|
||||
|
||||
/*
|
||||
* irc_part_all - Send IRC PART messages to all joined channels.
|
||||
*
|
||||
* Iterates the global chans[] array and sends a PART command for each
|
||||
* non-empty channel name. Used during hot reload so the bot cleanly
|
||||
* leaves channels before exec'ing a new instance.
|
||||
*
|
||||
* Parameters:
|
||||
* s - The IRC session.
|
||||
*
|
||||
* Returns: void.
|
||||
*/
|
||||
static void irc_part_all(Session *s) {
|
||||
for (int i = 0; i < nchans; i++) {
|
||||
if (chans[i].name[0])
|
||||
net_send(s, "PART %s\r\n", chans[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* reload_do - Execute a hot reload: save state, part channels, fork+exec.
|
||||
* reload_do - Execute a hot reload: save state, fork-in-place or fork+exec.
|
||||
*
|
||||
* Orchestrates the full hot reload sequence: saves TLS session state via
|
||||
* reload_save(), waits briefly, parts all channels, sends QUIT, then
|
||||
* fork()s and exec's the same binary. The child inherits the socket fd
|
||||
* via ZYNK_RESTORE_FD environment variable and the parent exits. Requires
|
||||
* an active TLS connection to preserve the session across the restart.
|
||||
* reload_save(), then either forks-in-place (exec_new=0: child inherits the
|
||||
* live socket and SSL session, continues the event loop) or fork+execs a
|
||||
* new binary (exec_new=1: sends QUIT, child exec's the same binary).
|
||||
*
|
||||
* Parameters:
|
||||
* s - The IRC session.
|
||||
* reply_target - Channel or nick to send status messages to.
|
||||
*
|
||||
* Returns: void (parent exits, child continues as new instance).
|
||||
* Returns: void (parent exits, child continues).
|
||||
*/
|
||||
static void reload_do(Session *s, const char *reply_target) {
|
||||
static void reload_do(Session *s, const char *reply_target, int exec_new) {
|
||||
if (!s->ssl || s->fd < 0) {
|
||||
irc_msg(s, reply_target, "Cannot reload: not connected with TLS");
|
||||
return;
|
||||
|
|
@ -1236,15 +1216,16 @@ static void reload_do(Session *s, const char *reply_target) {
|
|||
irc_msg(s, reply_target, "Failed to save session state");
|
||||
return;
|
||||
}
|
||||
irc_msg(s, reply_target, "Reloading...");
|
||||
irc_msg(s, reply_target, exec_new ? "Reloading..." : "Reloading (session preserved)...");
|
||||
struct timespec ts_short = { .tv_sec = 0, .tv_nsec = 100000000 };
|
||||
nanosleep(&ts_short, NULL);
|
||||
irc_part_all(s);
|
||||
int old_fl = fcntl(s->fd, F_GETFL, 0);
|
||||
fcntl(s->fd, F_SETFL, old_fl & ~O_NONBLOCK);
|
||||
net_send(s, "QUIT :restarting\r\n");
|
||||
struct timespec ts_quit = { .tv_sec = 0, .tv_nsec = 500000000 };
|
||||
nanosleep(&ts_quit, NULL);
|
||||
if (exec_new) {
|
||||
int old_fl = fcntl(s->fd, F_GETFL, 0);
|
||||
fcntl(s->fd, F_SETFL, old_fl & ~O_NONBLOCK);
|
||||
net_send(s, "QUIT :restarting\r\n");
|
||||
struct timespec ts_quit = { .tv_sec = 0, .tv_nsec = 500000000 };
|
||||
nanosleep(&ts_quit, NULL);
|
||||
}
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
unlink(ZYNK_RELOAD_FILE);
|
||||
|
|
@ -1252,16 +1233,19 @@ static void reload_do(Session *s, const char *reply_target) {
|
|||
return;
|
||||
}
|
||||
if (pid == 0) {
|
||||
close(s->fd);
|
||||
char self[4096];
|
||||
ssize_t n = readlink("/proc/self/exe", self, sizeof self - 1);
|
||||
if (n > 0) {
|
||||
self[n] = 0;
|
||||
char *deleted = strstr(self, " (deleted)");
|
||||
if (deleted) *deleted = 0;
|
||||
execl(self, "zynk", NULL);
|
||||
if (exec_new) {
|
||||
close(s->fd);
|
||||
char self[4096];
|
||||
ssize_t n = readlink("/proc/self/exe", self, sizeof self - 1);
|
||||
if (n > 0) {
|
||||
self[n] = 0;
|
||||
char *deleted = strstr(self, " (deleted)");
|
||||
if (deleted) *deleted = 0;
|
||||
execl(self, "zynk", NULL);
|
||||
}
|
||||
_exit(1);
|
||||
}
|
||||
_exit(1);
|
||||
return;
|
||||
}
|
||||
_exit(0);
|
||||
}
|
||||
|
|
@ -1938,7 +1922,7 @@ static int try_compile_and_restart(Session *s, const char *target) {
|
|||
log_stamp(); fprintf(stderr, CLR_GREEN "Compilation successful, restarting..." CLR_RESET "\n");
|
||||
FILE *cf = fopen("/tmp/zynk_code_restart", "w");
|
||||
if (cf) { fprintf(cf, "%s\n", target); fclose(cf); }
|
||||
reload_do(s, target);
|
||||
reload_do(s, target, 1);
|
||||
return 1;
|
||||
} else {
|
||||
char errbuf[512];
|
||||
|
|
@ -2651,7 +2635,7 @@ static int cmd_reload(Session *s, const char *msg, const char *reply_target, con
|
|||
return 1;
|
||||
}
|
||||
if (rate_limit_check(src_nick, reply_target) < 0) return 1;
|
||||
reload_do(s, reply_target);
|
||||
reload_do(s, reply_target, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -2671,7 +2655,7 @@ static int cmd_restart(Session *s, const char *msg, const char *reply_target, co
|
|||
}
|
||||
if (rate_limit_check(src_nick, reply_target) < 0) return 1;
|
||||
irc_msg(s, reply_target, "Restarting...");
|
||||
reload_do(s, reply_target);
|
||||
reload_do(s, reply_target, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -3318,7 +3302,7 @@ static int cmd_rebuild(Session *s, const char *msg, const char *reply_target, co
|
|||
log_stamp(); fprintf(stderr, CLR_GREEN "REBUILD successful, restarting..." CLR_RESET "\n");
|
||||
FILE *cf = fopen("/tmp/zynk_code_restart", "w");
|
||||
if (cf) { fprintf(cf, "%s\n", reply_target); fclose(cf); }
|
||||
reload_do(s, reply_target);
|
||||
reload_do(s, reply_target, 1);
|
||||
} else {
|
||||
FILE *f = fopen("/tmp/zynk_rebuild_err", "r");
|
||||
if (f) { size_t n = fread(errbuf, 1, sizeof errbuf - 1, f); errbuf[n] = 0; fclose(f); char *nl = strchr(errbuf, '\n'); if (nl) *nl = 0; }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue