@rivus/gateway 0.21.0 → 0.21.2

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.
@@ -2250,7 +2250,7 @@ function parseLockOwner(value) {
2250
2250
  //#endregion
2251
2251
  //#region src/platform/project/setup/release-descriptor.ts
2252
2252
  const RIVUS_RELEASE_DESCRIPTOR = Object.freeze({
2253
- version: "0.21.0",
2253
+ version: "0.21.2",
2254
2254
  dependencies: Object.freeze({
2255
2255
  "@earendil-works/pi-coding-agent": "0.84.4",
2256
2256
  "@larksuiteoapi/node-sdk": "1.74.0",
@@ -4085,6 +4085,7 @@ function createLaunchdServiceManager(context) {
4085
4085
  if (result.exitCode !== 0) {
4086
4086
  if (/Could not find service ["']?[^\n]+ in domain/u.test(result.stderr)) return {
4087
4087
  loaded: false,
4088
+ stopping: false,
4088
4089
  running: false,
4089
4090
  command: service.command
4090
4091
  };
@@ -4092,6 +4093,8 @@ function createLaunchdServiceManager(context) {
4092
4093
  }
4093
4094
  const state = /^\s*state = ([^\r\n]+)$/mu.exec(result.stdout)?.[1]?.trim();
4094
4095
  const pidText = /^\s*pid = (\d+)\s*$/mu.exec(result.stdout)?.[1];
4096
+ const pid = pidText ? Number(pidText) : void 0;
4097
+ const stopping = state === "SIGTERMed" && Boolean(pid && pid > 0);
4095
4098
  const stoppedStates = [
4096
4099
  "waiting",
4097
4100
  "not running",
@@ -4099,12 +4102,12 @@ function createLaunchdServiceManager(context) {
4099
4102
  ];
4100
4103
  const pendingStates = [
4101
4104
  "running",
4105
+ "xpcproxy",
4102
4106
  "spawn scheduled",
4103
4107
  "spawning",
4104
4108
  "throttled"
4105
4109
  ];
4106
- if (!state || ![...stoppedStates, ...pendingStates].includes(state)) throw new Error("launchctl print returned an unrecognized service state.");
4107
- const pid = pidText ? Number(pidText) : void 0;
4110
+ if (!state || !stopping && ![...stoppedStates, ...pendingStates].includes(state)) throw new Error("launchctl print returned an unrecognized service state.");
4108
4111
  const running = Boolean(pid && pid > 0) || pendingStates.includes(state);
4109
4112
  const block = /^\s*arguments = \{\r?\n([\s\S]*?)^\s*\}\s*$/mu.exec(result.stdout)?.[1];
4110
4113
  let command = service.command;
@@ -4114,6 +4117,7 @@ function createLaunchdServiceManager(context) {
4114
4117
  }
4115
4118
  return {
4116
4119
  loaded: true,
4120
+ stopping,
4117
4121
  running,
4118
4122
  ...pid ? { pid } : {},
4119
4123
  command
@@ -4140,12 +4144,13 @@ function createLaunchdServiceManager(context) {
4140
4144
  },
4141
4145
  load,
4142
4146
  async inspect(identity) {
4143
- const { loaded: _loaded, ...status } = await query(identity);
4147
+ const { loaded: _loaded, stopping: _stopping, ...status } = await query(identity);
4144
4148
  return status;
4145
4149
  },
4146
4150
  async stop(identity) {
4147
- if ((await query(identity)).loaded) await requireServiceTool(context, "/bin/launchctl", ["bootout", `${domain}/${identity.id}`]);
4148
- await waitForServiceStop(context, () => query(identity));
4151
+ const observed = await query(identity);
4152
+ if (observed.loaded && !observed.stopping) await requireServiceTool(context, "/bin/launchctl", ["bootout", `${domain}/${identity.id}`]);
4153
+ await waitForServiceStop(context, async () => ({ running: (await query(identity)).loaded }));
4149
4154
  },
4150
4155
  async switch(identity, command, backupDirectory) {
4151
4156
  const service = await load(identity);
@@ -4257,7 +4262,7 @@ function createSystemdServiceManager(context) {
4257
4262
  bus,
4258
4263
  managerPath,
4259
4264
  `${bus}.Manager`,
4260
- "GetUnit",
4265
+ "LoadUnit",
4261
4266
  "s",
4262
4267
  id
4263
4268
  ], unitPathSchema);
@@ -5140,19 +5145,33 @@ finally { image.free(); }
5140
5145
  `;
5141
5146
  //#endregion
5142
5147
  //#region src/adapters/deployment/upgrade/home-backup.ts
5148
+ const isMissingFile = Schema.is(Schema.Struct({ code: Schema.Literal("ENOENT") }));
5143
5149
  async function backUpUpgradeHome(input) {
5144
5150
  await mkdir(input.directory, {
5145
5151
  recursive: true,
5146
5152
  mode: 448
5147
5153
  });
5148
- const archive = join(input.directory, "home.tar");
5149
- const temporary = `${archive}.partial`;
5154
+ const archive = {
5155
+ path: join(input.directory, "home.tar.gz"),
5156
+ format: "gzip"
5157
+ };
5158
+ const temporary = {
5159
+ ...archive,
5160
+ path: `${archive.path}.partial`
5161
+ };
5150
5162
  try {
5151
- if ((await input.run({
5163
+ await rm(join(input.directory, "home.tar.partial"), { force: true });
5164
+ const existing = await retainedArchive(input.directory);
5165
+ if (existing) {
5166
+ await verifyArchive(existing, input);
5167
+ return existing.path;
5168
+ }
5169
+ const created = await input.run({
5152
5170
  command: "/usr/bin/tar",
5153
5171
  args: [
5172
+ "--use-compress-program=/usr/bin/gzip -1",
5154
5173
  "-cpf",
5155
- temporary,
5174
+ temporary.path,
5156
5175
  "--exclude=./.rivus-upgrade",
5157
5176
  "-C",
5158
5177
  input.home,
@@ -5163,29 +5182,74 @@ async function backUpUpgradeHome(input) {
5163
5182
  timeoutMs: 18e5,
5164
5183
  ...input.signal ? { signal: input.signal } : {},
5165
5184
  discardStdout: true
5166
- })).exitCode !== 0) throw new Error("Home backup failed; the service remains stopped and the previous release is retained");
5167
- await chmod(temporary, 384);
5168
- await verifyArchive(temporary, input);
5169
- await rename(temporary, archive);
5170
- return archive;
5185
+ });
5186
+ if (created.exitCode !== 0) {
5187
+ await rm(temporary.path, { force: true });
5188
+ await throwArchiveFailure("create", archive.path, created);
5189
+ }
5190
+ await chmod(temporary.path, 384);
5191
+ await verifyArchive(temporary, input, true);
5192
+ await rename(temporary.path, archive.path);
5193
+ return archive.path;
5171
5194
  } finally {
5172
- await rm(temporary, { force: true });
5195
+ await rm(temporary.path, { force: true });
5173
5196
  }
5174
5197
  }
5175
5198
  async function verifyUpgradeBackup(input) {
5176
- await verifyArchive(join(input.directory, "home.tar"), input);
5199
+ const archive = await retainedArchive(input.directory);
5200
+ if (!archive) throw new Error("Home backup is missing");
5201
+ await verifyArchive(archive, input);
5202
+ }
5203
+ async function retainedArchive(directory) {
5204
+ const archives = [];
5205
+ for (const [name, format] of [["home.tar.gz", "gzip"], ["home.tar", "tar"]]) {
5206
+ const path = join(directory, name);
5207
+ if (await lstat(path).catch((error) => {
5208
+ if (!isMissingFile(error)) throw error;
5209
+ })) archives.push({
5210
+ path,
5211
+ format
5212
+ });
5213
+ }
5214
+ if (archives.length > 1) throw new Error("Both home.tar and home.tar.gz exist; preserve both backups and resolve the ambiguity before resuming");
5215
+ return archives[0];
5177
5216
  }
5178
- async function verifyArchive(path, input) {
5179
- const metadata = await stat(path);
5180
- if (!metadata.isFile() || metadata.size === 0) throw new Error("Home backup is missing or empty");
5181
- if ((await input.run({
5217
+ async function verifyArchive(archive, input, removeFailedPartial = false) {
5218
+ const metadata = await lstat(archive.path);
5219
+ if (!metadata.isFile() || metadata.size === 0) throw new Error("Home backup is missing, empty or not a regular file");
5220
+ const listed = await input.run({
5182
5221
  command: "/usr/bin/tar",
5183
- args: ["-tf", path],
5222
+ args: [
5223
+ ...archive.format === "gzip" ? ["--use-compress-program=/usr/bin/gzip -d"] : [],
5224
+ "-tf",
5225
+ archive.path
5226
+ ],
5184
5227
  cwd: input.home,
5185
5228
  env: input.env,
5186
5229
  timeoutMs: 18e5,
5187
5230
  discardStdout: true
5188
- })).exitCode !== 0) throw new Error("Home backup could not be read; refusing to switch the service");
5231
+ });
5232
+ if (listed.exitCode !== 0) {
5233
+ if (removeFailedPartial) await rm(archive.path, { force: true });
5234
+ await throwArchiveFailure("verify", archive.path, listed);
5235
+ }
5236
+ }
5237
+ async function throwArchiveFailure(phase, archive, result) {
5238
+ const diagnostics = join(dirname(archive), `tar-${phase}-failure-${randomUUID()}.json`);
5239
+ const summary = phase === "create" ? "Home backup failed; the service remains stopped and the previous release is retained" : "Home backup could not be read; refusing to switch the service";
5240
+ try {
5241
+ await writeAtomicTextFile(diagnostics, `${JSON.stringify({
5242
+ archive,
5243
+ exitCode: result.exitCode,
5244
+ stderr: result.stderr
5245
+ }, null, 2)}\n`, {
5246
+ mode: 384,
5247
+ durable: true
5248
+ });
5249
+ } catch {
5250
+ throw new Error(`${summary} (tar exit ${result.exitCode}); private diagnostics could not be saved to ${diagnostics}`);
5251
+ }
5252
+ throw new Error(`${summary} (tar exit ${result.exitCode}); private diagnostics: ${diagnostics}`);
5189
5253
  }
5190
5254
  //#endregion
5191
5255
  //#region src/bootstrap/upgrade/upgrade-home.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rivus/gateway",
3
- "version": "0.21.0",
3
+ "version": "0.21.2",
4
4
  "description": "Message gateway, channels, host binding, automation, and daemon assembly for Rivus.",
5
5
  "type": "module",
6
6
  "license": "GPL-3.0-only",
@@ -57,8 +57,8 @@
57
57
  "@agentclientprotocol/sdk": "1.5.1",
58
58
  "@larksuiteoapi/node-sdk": "1.74.0",
59
59
  "@resvg/resvg-js": "2.6.2",
60
- "@rivus/platform": "0.21.0",
61
- "@rivus/runtime": "0.21.0",
60
+ "@rivus/platform": "0.21.2",
61
+ "@rivus/runtime": "0.21.2",
62
62
  "axios": "1.20.0",
63
63
  "effect": "^4.0.0",
64
64
  "get-east-asian-width": "1.6.0",