@kapybara/zerosub 1.2.0 → 1.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kapybara/zerosub",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "ZeroSub: use several Claude and ChatGPT (Codex) subscriptions side by side in Paseo. Add accounts in-app, pick one per agent, and switch automatically when an account hits its limit.",
5
5
  "keywords": [
6
6
  "paseo",
package/server/adapter.ts CHANGED
@@ -1,8 +1,13 @@
1
- import type { AgentTimelineItem } from "@getpaseo/protocol/agent-types";
2
- import type { PluginTurnOutcome } from "@getpaseo/plugin/server";
1
+ import type { PluginLifecycleEvents, PluginTurnOutcome } from "@getpaseo/plugin/server";
3
2
  import type { Family, LoginMethod, LoginStep, Usage } from "../shared/model";
4
3
  import type { RedeemOutcome } from "../shared/rpc";
5
4
 
5
+ /**
6
+ * One timeline entry. Taken from the SDK rather than `@getpaseo/protocol`: Paseo only supplies
7
+ * `@getpaseo/plugin` to plugins, and an npm install has no devDependencies to resolve it from.
8
+ */
9
+ export type AgentTimelineItem = PluginLifecycleEvents["agent.turn_ended"]["timeline"][number];
10
+
6
11
  /** A provider's answer to a redeem request. */
7
12
  export interface RedeemReply {
8
13
  outcome: RedeemOutcome;
@@ -35,6 +40,14 @@ export interface LimitHit {
35
40
  message: string;
36
41
  }
37
42
 
43
+ /** A finished turn, as Paseo reports it. */
44
+ export interface TurnEvent {
45
+ outcome: PluginTurnOutcome;
46
+ timeline: readonly AgentTimelineItem[];
47
+ /** Paseo's id for the turn; `null` when the turn failed before it started. */
48
+ turnId?: string | null;
49
+ }
50
+
38
51
  export interface LoginProgress {
39
52
  step: LoginStep;
40
53
  /** Link that finishes by itself when opened on the daemon machine (localhost callback), or the device-code page. */
@@ -88,9 +101,9 @@ export interface FamilyAdapter {
88
101
  login(home: string | null, method: LoginMethod): Promise<LoginHandle>;
89
102
  logout(home: string): Promise<void>;
90
103
  /** Looks for a subscription usage-limit failure in a finished turn. */
91
- detectLimit(event: { outcome: PluginTurnOutcome; timeline: readonly AgentTimelineItem[] }): LimitHit | null;
104
+ detectLimit(event: TurnEvent): LimitHit | null;
92
105
  /** Looks for a turn that failed because the account's login is no longer valid. */
93
- detectSignOut(event: { outcome: PluginTurnOutcome; timeline: readonly AgentTimelineItem[] }): string | null;
106
+ detectSignOut(event: TurnEvent): string | null;
94
107
  }
95
108
 
96
109
  /** The environment for a process ZeroSub spawns against `patch`. */
package/server/codex.ts CHANGED
@@ -57,7 +57,7 @@ class AppServer {
57
57
  try {
58
58
  await server.request(
59
59
  "initialize",
60
- { clientInfo: { name: CLIENT_NAME, title: "Paseo ZeroSub", version: "1.2.0" }, capabilities: { experimentalApi: false } },
60
+ { clientInfo: { name: CLIENT_NAME, title: "Paseo ZeroSub", version: "1.2.1" }, capabilities: { experimentalApi: false } },
61
61
  60_000,
62
62
  );
63
63
  server.send({ method: "initialized" });
package/server/handoff.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import type { AgentTimelineItem } from "@getpaseo/protocol/agent-types";
3
2
  import type { PluginHookContext } from "@getpaseo/plugin/server";
3
+ import type { AgentTimelineItem } from "./adapter";
4
4
 
5
5
  type PaseoApi = PluginHookContext["paseo"];
6
6
 
package/server/limits.ts CHANGED
@@ -1,8 +1,4 @@
1
- import type { AgentTimelineItem } from "@getpaseo/protocol/agent-types";
2
- import type { PluginTurnOutcome } from "@getpaseo/plugin/server";
3
- import type { LimitHit, LimitKind } from "./adapter";
4
-
5
- type TurnEvent = { outcome: PluginTurnOutcome; timeline: readonly AgentTimelineItem[] };
1
+ import type { LimitHit, LimitKind, TurnEvent } from "./adapter";
6
2
 
7
3
  /**
8
4
  * Claude Code's exact wording for hard limits (2.1.x templates: `You've hit your ${limit}${suffix}`),
@@ -56,14 +52,17 @@ function clean(text: string): string {
56
52
  /**
57
53
  * Where a CLI-generated notice can appear in a finished turn: the failure message, error items,
58
54
  * Paseo's `[System Error]` rows, and — for Claude, which reports limits as a synthetic reply — the
59
- * final assistant messages. Ordinary replies earlier in the turn are never inspected.
55
+ * final assistant messages. Ordinary replies earlier in the turn are never inspected. A turn that
56
+ * failed before it started has only its failure: Paseo never recorded its message, so the timeline
57
+ * still ends with the previous turn.
60
58
  */
61
59
  function notices(event: TurnEvent, includeFinalReplies: boolean): string[] {
62
60
  const found: string[] = [];
63
61
  if (event.outcome.kind === "failed") found.push(event.outcome.error.message);
62
+ const timeline = event.turnId === null ? [] : event.timeline;
64
63
  let replies = 0;
65
- for (let index = event.timeline.length - 1; index >= 0; index -= 1) {
66
- const item = event.timeline[index];
64
+ for (let index = timeline.length - 1; index >= 0; index -= 1) {
65
+ const item = timeline[index];
67
66
  if (!item) continue;
68
67
  if (item.type === "user_message") break;
69
68
  if (item.type === "error") found.push(item.message);
@@ -115,13 +114,15 @@ const MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "
115
114
 
116
115
  /**
117
116
  * "· resets 3pm (Asia/Kolkata)", "· resets 3:30pm (America/New_York)",
118
- * "· resets Sep 25, 3pm (Europe/Berlin)", or the legacy "|1790150000" epoch.
117
+ * "· resets Sep 25, 3pm (Europe/Berlin)" ("Sep 25 at 3pm" where the CLI's ICU is newer), or the
118
+ * legacy "|1790150000" epoch. A reset in the next calendar year also carries the year
119
+ * ("Jan 2, 2027"), which rolling the date forward already gives.
119
120
  */
120
121
  export function parseClaudeReset(text: string, now: Date): string | null {
121
122
  const epoch = text.match(/usage limit reached\|(\d{9,})/i);
122
123
  if (epoch?.[1]) return new Date(Number(epoch[1]) * 1000).toISOString();
123
124
  const match = text.match(
124
- /resets\s+(?:([A-Za-z]{3})[a-z]*\.?\s+(\d{1,2}),?\s+)?(\d{1,2})(?::(\d{2}))?\s*([ap])\.?m\.?(?:\s*\(([^)]+)\))?/i,
125
+ /resets\s+(?:([A-Za-z]{3})[a-z]*\.?\s+(\d{1,2})(?:,?\s+\d{4})?,?\s+(?:at\s+)?)?(\d{1,2})(?::(\d{2}))?\s*([ap])\.?m\.?(?:\s*\(([^)]+)\))?/i,
125
126
  );
126
127
  if (!match) return null;
127
128
  const [, monthName, dayText, hourText, minuteText, meridiem, zone] = match;
package/server/reopen.ts CHANGED
@@ -22,9 +22,10 @@ export class Reopener {
22
22
  return this.cli.path;
23
23
  }
24
24
 
25
- async reopen(agentId: string): Promise<{ ok: true } | { ok: false; error: string }> {
25
+ /** `timedOut`: the command was stopped before the daemon answered, so the reload may still finish. */
26
+ async reopen(agentId: string): Promise<{ ok: true } | { ok: false; error: string; timedOut: boolean }> {
26
27
  const cli = await this.locate();
27
- if (!cli) return { ok: false, error: "The paseo command-line tool was not found on the daemon machine." };
28
+ if (!cli) return { ok: false, error: "The paseo command-line tool was not found on the daemon machine.", timedOut: false };
28
29
  return new Promise((resolve) => {
29
30
  execFile(
30
31
  cli,
@@ -33,7 +34,7 @@ export class Reopener {
33
34
  (error, _stdout, stderr) => {
34
35
  if (!error) return resolve({ ok: true });
35
36
  const detail = `${stderr || error.message}`.trim().split("\n").slice(-3).join(" ");
36
- resolve({ ok: false, error: explainFailure(detail) });
37
+ resolve({ ok: false, error: explainFailure(detail), timedOut: error.killed === true });
37
38
  },
38
39
  );
39
40
  });
package/server/service.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { randomBytes } from "node:crypto";
2
2
  import { readdir } from "node:fs/promises";
3
3
  import { join } from "node:path";
4
- import type { AgentTimelineItem } from "@getpaseo/protocol/agent-types";
5
4
  import type {
6
5
  PluginHookContext,
7
6
  PluginLifecycleEvents,
@@ -22,6 +21,7 @@ import type { RedeemResult, ReopenSummary } from "../shared/rpc";
22
21
  import { SWITCH_ROW_KIND, SWITCH_ROW_VERSION, type SwitchRow } from "../shared/timeline";
23
22
  import {
24
23
  applyEnv,
24
+ type AgentTimelineItem,
25
25
  type FamilyAdapter,
26
26
  type Identity,
27
27
  type LimitHit,
@@ -1049,9 +1049,20 @@ export class Service {
1049
1049
  }
1050
1050
  return "closed";
1051
1051
  }
1052
+ const before = (await this.store.read()).sessions[agentId];
1052
1053
  const result = await this.reopener.reopen(agentId);
1053
1054
  if (!result.ok) {
1054
1055
  console.warn(`[ZeroSub] could not reopen ${agentId}: ${result.error}`);
1056
+ // Paseo asks for the new session's environment before it closes the old one, so a failed reload
1057
+ // has still recorded a session that never opened. One that ran out of time may yet open.
1058
+ if (!result.timedOut) {
1059
+ await this.store.update((draft) => {
1060
+ const recorded = draft.sessions[agentId];
1061
+ if (!recorded || recorded.openedAt === before?.openedAt) return;
1062
+ if (before) draft.sessions[agentId] = before;
1063
+ else delete draft.sessions[agentId];
1064
+ });
1065
+ }
1055
1066
  return "failed";
1056
1067
  }
1057
1068
  return "reopened";