@juspay/neurolink 10.1.2 → 10.2.0

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.
@@ -700,6 +700,40 @@ export type ProxyStats = {
700
700
  totalQuotaRateLimits: number;
701
701
  accounts: Record<string, AccountStats>;
702
702
  };
703
+ /** Durability and reconciliation state for the proxy usage counters. */
704
+ export type ProxyStatsPersistenceStatus = {
705
+ enabled: boolean;
706
+ filePath: string | null;
707
+ revision: number;
708
+ pendingMutations: number;
709
+ inFlightMutations: number;
710
+ unpersistedMutations: number;
711
+ lastFlushedAt: number | null;
712
+ lastReconciledAt: number | null;
713
+ lastRecoveryAt: number | null;
714
+ lastError: string | null;
715
+ };
716
+ /** Versioned on-disk snapshot shared by overlapping proxy workers. */
717
+ export type PersistedProxyStatsSnapshot = {
718
+ schemaVersion: 1;
719
+ revision: number;
720
+ updatedAt: number;
721
+ stats: ProxyStats;
722
+ };
723
+ /** Ownership metadata for the proxy statistics cross-process lock. */
724
+ export type ProxyStatsLockOwner = {
725
+ token: string;
726
+ pid: number;
727
+ acquiredAt: number;
728
+ };
729
+ /** Construction options for an isolated proxy statistics store. */
730
+ export type ProxyUsageStatsStoreOptions = {
731
+ filePath?: string;
732
+ flushIntervalMs?: number;
733
+ lockTimeoutMs?: number;
734
+ staleLockMs?: number;
735
+ now?: () => number;
736
+ };
703
737
  export type RefreshableAccount = {
704
738
  token: string;
705
739
  refreshToken?: string;
@@ -874,6 +908,8 @@ export type ProxyPaths = {
874
908
  quotaFile: string;
875
909
  /** account-cooldowns.json — restart-safe account cooldown state */
876
910
  cooldownFile: string;
911
+ /** proxy-usage-stats.json — restart- and handoff-safe usage counters */
912
+ statsFile?: string;
877
913
  /** Whether this is a dev-mode isolated instance */
878
914
  isDev: boolean;
879
915
  };
@@ -1876,6 +1912,7 @@ export type ProxyStartApp = {
1876
1912
  };
1877
1913
  /** Stats shape consumed by the proxy status printer. */
1878
1914
  export type StatusStats = {
1915
+ startedAt?: number;
1879
1916
  totalAttempts?: number;
1880
1917
  totalAttemptErrors?: number;
1881
1918
  totalRequests: number;
@@ -1896,7 +1933,9 @@ export type StatusStats = {
1896
1933
  transientRateLimits?: number;
1897
1934
  quotaRateLimits?: number;
1898
1935
  cooling: boolean;
1936
+ status?: "active" | "cooling" | "disabled" | "excluded" | "removed";
1899
1937
  }[];
1938
+ persistence?: ProxyStatsPersistenceStatus;
1900
1939
  };
1901
1940
  /** Sub-action of the `proxy telemetry` CLI command. */
1902
1941
  export type ProxyTelemetryAction = "setup" | "start" | "stop" | "status" | "logs" | "import-dashboard";
@@ -14,3 +14,4 @@
14
14
  */
15
15
  import type { ProxyPaths } from "../types/index.js";
16
16
  export declare function resolveProxyPaths(dev: boolean): ProxyPaths;
17
+ export declare function resolveProxyUsageStatsPath(paths: ProxyPaths): string;
@@ -22,6 +22,7 @@ export function resolveProxyPaths(dev) {
22
22
  logsDir: join(base, "logs"),
23
23
  quotaFile: join(base, "account-quotas.json"),
24
24
  cooldownFile: join(base, "account-cooldowns.json"),
25
+ statsFile: join(base, "proxy-usage-stats.json"),
25
26
  isDev: true,
26
27
  };
27
28
  }
@@ -31,6 +32,10 @@ export function resolveProxyPaths(dev) {
31
32
  logsDir: join(base, "logs"),
32
33
  quotaFile: join(base, "account-quotas.json"),
33
34
  cooldownFile: join(base, "account-cooldowns.json"),
35
+ statsFile: join(base, "proxy-usage-stats.json"),
34
36
  isDev: false,
35
37
  };
36
38
  }
39
+ export function resolveProxyUsageStatsPath(paths) {
40
+ return paths.statsFile ?? join(paths.stateDir, "proxy-usage-stats.json");
41
+ }
@@ -1,13 +1,67 @@
1
1
  /**
2
- * Proxy Usage Statistics
3
- * Tracks per-account request counts, token usage, and error rates.
4
- * In-memory only resets on proxy restart.
2
+ * Proxy usage statistics with restart- and handoff-safe persistence.
3
+ *
4
+ * Request handlers update memory synchronously. Small deltas are merged into a
5
+ * shared snapshot asynchronously, under a cross-process lock, so overlapping
6
+ * rolling workers cannot overwrite each other's counters.
5
7
  */
6
- import type { AccountStats, ProxyStats } from "../types/index.js";
8
+ import type { AccountStats, ProxyStats, ProxyStatsPersistenceStatus, ProxyUsageStatsStoreOptions } from "../types/index.js";
9
+ export declare class ProxyUsageStatsStore {
10
+ private readonly now;
11
+ private readonly flushIntervalMs;
12
+ private readonly lockTimeoutMs;
13
+ private readonly staleLockMs;
14
+ private filePath?;
15
+ private stats;
16
+ private pending;
17
+ private pendingMutations;
18
+ private inFlightMutations;
19
+ private revision;
20
+ private flushTimer;
21
+ private readonly flushMutex;
22
+ private lastFlushedAt?;
23
+ private lastReconciledAt?;
24
+ private lastRecoveryAt?;
25
+ private lastError?;
26
+ constructor(options?: ProxyUsageStatsStoreOptions);
27
+ initialize(filePath?: string): Promise<void>;
28
+ recordAttempt(accountLabel: string, accountType: string): void;
29
+ recordFinalSuccess(accountLabel?: string, accountType?: string): void;
30
+ recordAttemptError(accountLabel: string, accountType: string, status: number, rateLimitKind?: "transient" | "quota"): void;
31
+ recordFinalError(_status: number, accountLabel?: string, accountType?: string): void;
32
+ getStats(): ProxyStats;
33
+ getAccountStats(label: string): AccountStats | undefined;
34
+ getPersistenceStatus(): ProxyStatsPersistenceStatus;
35
+ flush(): Promise<void>;
36
+ reconcile(): Promise<ProxyStats>;
37
+ resetMemory(): void;
38
+ resetForTests(): Promise<void>;
39
+ private markMutation;
40
+ private applyAttempt;
41
+ private applyFinalSuccess;
42
+ private applyAttemptError;
43
+ private applyFinalError;
44
+ private ensureAccount;
45
+ private scheduleFlush;
46
+ private cancelFlushTimer;
47
+ private readSnapshot;
48
+ private applySnapshot;
49
+ private recoverCorruptSnapshot;
50
+ private quarantineCorruptSnapshot;
51
+ private pruneCorruptSnapshots;
52
+ private describeError;
53
+ }
54
+ export declare function initUsageStats(filePath: string): Promise<void>;
7
55
  export declare function recordAttempt(accountLabel: string, accountType: string): void;
8
56
  export declare function recordFinalSuccess(accountLabel?: string, accountType?: string): void;
9
57
  export declare function recordAttemptError(accountLabel: string, accountType: string, status: number, rateLimitKind?: "transient" | "quota"): void;
10
58
  export declare function recordFinalError(_status: number, accountLabel?: string, accountType?: string): void;
11
59
  export declare function getStats(): ProxyStats;
60
+ export declare function getReconciledStats(): Promise<ProxyStats>;
12
61
  export declare function getAccountStats(label: string): AccountStats | undefined;
62
+ export declare function getUsageStatsPersistenceStatus(): ProxyStatsPersistenceStatus;
63
+ export declare function flushUsageStats(): Promise<void>;
64
+ /** Reset process-local counters while intentionally preserving durable state. */
13
65
  export declare function resetStats(): void;
66
+ /** Disconnect persistence and clear singleton state between isolated tests. */
67
+ export declare function resetUsageStatsForTests(): Promise<void>;