@code-yeongyu/senpi-codemode 2026.11.12 → 2026.11.13

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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [2026.11.13] - 2026-11-13
4
+
5
+ ### Breaking Changes
6
+
7
+ ### Added
8
+
9
+ ### Changed
10
+
11
+ ### Fixed
12
+
13
+ - The permission state a kernel reports for cross-session kernel tools, and the kernels a session revocation reaches, are shared by every loaded copy of the extension ([#6422](https://github.com/code-yeongyu/senpi/pull/6422)).
14
+
15
+ ### Removed
16
+
3
17
  ## [2026.11.12] - 2026-11-12
4
18
 
5
19
  ### Breaking Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@code-yeongyu/senpi-codemode",
3
- "version": "2026.11.12",
3
+ "version": "2026.11.13",
4
4
  "description": "Source-only senpi extension package for codemode evaluation tools",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -33,15 +33,16 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@babel/parser": "8.0.4",
36
- "@earendil-works/pi-ai": "npm:@code-yeongyu/senpi-ai@2026.11.12",
36
+ "@earendil-works/pi-ai": "npm:@code-yeongyu/senpi-ai@2026.11.13",
37
+ "@earendil-works/pi-realm-registry": "npm:@code-yeongyu/senpi-realm-registry@2026.11.13",
37
38
  "quickjs-wasi": "3.6.2",
38
39
  "typebox": "1.3.34"
39
40
  },
40
41
  "peerDependencies": {
41
- "@code-yeongyu/senpi": "2026.11.12"
42
+ "@code-yeongyu/senpi": "2026.11.13"
42
43
  },
43
44
  "devDependencies": {
44
- "@code-yeongyu/senpi": "2026.11.12"
45
+ "@code-yeongyu/senpi": "2026.11.13"
45
46
  },
46
47
  "keywords": [
47
48
  "senpi",
@@ -1,46 +1,58 @@
1
+ import {
2
+ cellAuthority,
3
+ isPermissionSessionHandle,
4
+ type PermissionSessionHandle,
5
+ } from "../shared/cell-authority-registry.ts";
6
+
1
7
  interface SessionKernel {
2
8
  revoke(): void;
3
9
  retire(): Promise<void>;
4
10
  }
5
11
 
12
+ /**
13
+ * One session's permission state as a kernel bound to it sees it. The state is shared by every codemode copy in the
14
+ * realm (omo#12965): a session one copy marked dead, or is revoking, reads so in every copy.
15
+ */
6
16
  export interface PermissionSession {
7
17
  dead: boolean;
8
- pending?: Promise<void>;
9
- readonly kernels: Set<SessionKernel>;
18
+ readonly pending: Promise<void> | undefined;
19
+ }
20
+
21
+ /** The revocation in flight, `undefined` for none, `"unreadable"` for a value another copy shaped differently. */
22
+ function pendingOf(handle: PermissionSessionHandle): Promise<void> | undefined | "unreadable" {
23
+ const pending: unknown = handle.pending();
24
+ return pending === undefined || pending instanceof Promise ? pending : "unreadable";
10
25
  }
11
26
 
12
- const sessions = new Map<string, PermissionSession>();
27
+ /**
28
+ * A session whose handle this copy cannot read is dead: its kernels refuse instead of running unrevocable. The view
29
+ * has one shape for every handle, so nothing a holder assigns can make an unreadable one read live.
30
+ */
31
+ function sessionView(handle: unknown): PermissionSession {
32
+ const readable = isPermissionSessionHandle(handle) ? handle : undefined;
33
+ return {
34
+ get dead() {
35
+ return readable === undefined || readable.dead() !== false || pendingOf(readable) === "unreadable";
36
+ },
37
+ set dead(value: boolean) {
38
+ if (value) readable?.markDead();
39
+ },
40
+ get pending() {
41
+ const pending = readable === undefined ? undefined : pendingOf(readable);
42
+ return pending === "unreadable" ? undefined : pending;
43
+ },
44
+ };
45
+ }
13
46
 
14
47
  export function registerPermissionKernel(sessionId: string, kernel: SessionKernel): PermissionSession {
15
- let session = sessions.get(sessionId);
16
- if (!session) {
17
- session = { dead: false, kernels: new Set() };
18
- sessions.set(sessionId, session);
19
- }
20
- session.kernels.add(kernel);
21
- return session;
48
+ return sessionView(cellAuthority.registerKernel(sessionId, kernel));
22
49
  }
23
50
 
24
51
  export function unregisterPermissionKernel(sessionId: string, kernel: SessionKernel): void {
25
- const session = sessions.get(sessionId);
26
- if (!session) return;
27
- session.kernels.delete(kernel);
28
- if (!session.dead && !session.pending && session.kernels.size === 0) sessions.delete(sessionId);
52
+ cellAuthority.unregisterKernel(sessionId, kernel);
29
53
  }
30
54
 
31
- /** All words go DEAD synchronously, before retirement or another admission can run. */
55
+ /** All words go DEAD synchronously, before retirement or another admission can run, in every copy's kernels. */
32
56
  export function revokeSessionKernels(sessionId: string): Promise<void> {
33
- const session = sessions.get(sessionId);
34
- if (!session) return Promise.resolve();
35
- if (session.pending) return session.pending;
36
- const kernels = [...session.kernels];
37
- for (const kernel of kernels) kernel.revoke();
38
- const pending = Promise.all(kernels.map((kernel) => kernel.retire())).then(() => {});
39
- session.pending = pending;
40
- void pending
41
- .finally(() => {
42
- if (session.pending === pending) session.pending = undefined;
43
- })
44
- .catch(() => {});
45
- return pending;
57
+ return cellAuthority.revoke(sessionId);
46
58
  }
@@ -0,0 +1,143 @@
1
+ import { hasFunctions, realmRegistry } from "@earendil-works/pi-realm-registry";
2
+
3
+ /**
4
+ * Cross-session cell authority of every codemode copy in this realm (omo#12965, design amendment 3.2-A1).
5
+ *
6
+ * A realm can hold more than one copy of this extension: Node evaluates an extension again on every load, and a module
7
+ * generation is replaced while sessions live. Two tables are looked up by session id across sessions: what each
8
+ * session's kernels report as their cell permission state (read when another session invokes a kernel tool), and the
9
+ * kernels a session's revocation must reach. With a table per copy, a copy answers for a session another copy
10
+ * registered as if it had no state. So both tables live in one realm registry, in the closure of the copy that created
11
+ * it, and every copy calls through these members.
12
+ *
13
+ * Nothing crosses copies through class identity. A kernel is an identity key the registry also calls through
14
+ * `revoke()` and `retire()`; one without both is refused at registration. A snapshot and a session handle are read
15
+ * structurally by the copy that receives them, and an unreadable one refuses (`invocation-authority.ts`,
16
+ * `permission-sessions.ts`). The mutable session record never leaves the closure.
17
+ */
18
+ interface CellAuthority {
19
+ registerInvocation(sessionId: string, read: () => unknown): () => void;
20
+ /** The current snapshot of every reader registered for `sessionId`, in registration order. */
21
+ invocations(sessionId: string): unknown[];
22
+ registerKernel(sessionId: string, kernel: object): unknown;
23
+ unregisterKernel(sessionId: string, kernel: object): void;
24
+ /** All words go DEAD synchronously, before retirement or another admission can run. */
25
+ revoke(sessionId: string): Promise<void>;
26
+ }
27
+
28
+ interface SessionKernel {
29
+ revoke(): void;
30
+ retire(): Promise<void>;
31
+ }
32
+
33
+ /** One session's permission state as other copies reach it: functions only, the record stays in the closure. */
34
+ export interface PermissionSessionHandle {
35
+ dead(): unknown;
36
+ markDead(): void;
37
+ pending(): unknown;
38
+ }
39
+
40
+ interface SessionRecord {
41
+ dead: boolean;
42
+ pending?: Promise<void>;
43
+ readonly kernels: Set<SessionKernel>;
44
+ readonly handle: PermissionSessionHandle;
45
+ }
46
+
47
+ function isSessionKernel(value: object): value is SessionKernel {
48
+ return hasFunctions(value, ["revoke", "retire"]);
49
+ }
50
+
51
+ function createCellAuthority(): CellAuthority {
52
+ const readers = new Map<string, Set<() => unknown>>();
53
+ const sessions = new Map<string, SessionRecord>();
54
+ const sessionFor = (sessionId: string): SessionRecord => {
55
+ const existing = sessions.get(sessionId);
56
+ if (existing) return existing;
57
+ const session: SessionRecord = {
58
+ dead: false,
59
+ kernels: new Set(),
60
+ handle: Object.freeze({
61
+ dead: () => session.dead,
62
+ markDead: () => {
63
+ session.dead = true;
64
+ },
65
+ pending: () => session.pending,
66
+ }),
67
+ };
68
+ sessions.set(sessionId, session);
69
+ return session;
70
+ };
71
+ return {
72
+ registerInvocation: (sessionId, read) => {
73
+ let entries = readers.get(sessionId);
74
+ if (!entries) {
75
+ entries = new Set();
76
+ readers.set(sessionId, entries);
77
+ }
78
+ const registered = entries;
79
+ registered.add(read);
80
+ return () => {
81
+ registered.delete(read);
82
+ if (registered.size === 0 && readers.get(sessionId) === registered) readers.delete(sessionId);
83
+ };
84
+ },
85
+ invocations: (sessionId) =>
86
+ [...(readers.get(sessionId) ?? [])].map((read) => (typeof read === "function" ? read() : undefined)),
87
+ registerKernel: (sessionId, kernel) => {
88
+ if (!isSessionKernel(kernel)) throw new Error("Permission authority unavailable");
89
+ const session = sessionFor(sessionId);
90
+ session.kernels.add(kernel);
91
+ return session.handle;
92
+ },
93
+ unregisterKernel: (sessionId, kernel) => {
94
+ const session = sessions.get(sessionId);
95
+ if (!session || !isSessionKernel(kernel)) return;
96
+ session.kernels.delete(kernel);
97
+ if (!session.dead && !session.pending && session.kernels.size === 0) sessions.delete(sessionId);
98
+ },
99
+ revoke: (sessionId) => {
100
+ const session = sessions.get(sessionId);
101
+ if (!session) return Promise.resolve();
102
+ if (session.pending) return session.pending;
103
+ const kernels = [...session.kernels];
104
+ // Every kernel is revoked and asked to retire whatever the others do: one that throws must not shield the
105
+ // kernels registered after it. A kernel that could not be revoked or retired leaves the session dead, the
106
+ // rule an abandoned retirement already follows.
107
+ const failures: unknown[] = [];
108
+ for (const kernel of kernels) {
109
+ try {
110
+ kernel.revoke();
111
+ } catch (error) {
112
+ failures.push(error);
113
+ }
114
+ }
115
+ if (failures.length > 0) session.dead = true;
116
+ const retirements = kernels.map(async (kernel) => kernel.retire());
117
+ const pending = Promise.allSettled(retirements).then((results) => {
118
+ for (const result of results) if (result.status === "rejected") failures.push(result.reason);
119
+ if (failures.length === 0) return;
120
+ session.dead = true;
121
+ throw failures.length === 1 ? failures[0] : new AggregateError(failures, "Permission revocation failed");
122
+ });
123
+ session.pending = pending;
124
+ void pending
125
+ .finally(() => {
126
+ if (session.pending === pending) session.pending = undefined;
127
+ })
128
+ .catch(() => {});
129
+ return pending;
130
+ },
131
+ };
132
+ }
133
+
134
+ export const cellAuthority = realmRegistry(
135
+ "@code-yeongyu/senpi/codemode-cell-authority/v1",
136
+ createCellAuthority,
137
+ (value): value is CellAuthority =>
138
+ hasFunctions(value, ["registerInvocation", "invocations", "registerKernel", "unregisterKernel", "revoke"]),
139
+ );
140
+
141
+ export function isPermissionSessionHandle(value: unknown): value is PermissionSessionHandle {
142
+ return hasFunctions(value, ["dead", "markDead", "pending"]);
143
+ }
@@ -1,5 +1,6 @@
1
1
  import type { CellPermissionState } from "../js/kernel-contract.ts";
2
2
  import type { KernelToolsInvokeOptions } from "../js/kernel-tools-types.ts";
3
+ import { cellAuthority } from "./cell-authority-registry.ts";
3
4
 
4
5
  export interface InvocationSession {
5
6
  readonly cwd: string;
@@ -12,19 +13,55 @@ export interface InvocationPermissions {
12
13
  readonly resolveSession?: (sessionId: string) => InvocationSession;
13
14
  }
14
15
 
15
- const sessions = new Map<string, Set<() => InvocationSession>>();
16
-
16
+ /** Registers what this session's kernel reports, for every codemode copy in the realm (omo#12965). */
17
17
  export function registerInvocationSession(sessionId: string, read: () => InvocationSession): () => void {
18
- let entries = sessions.get(sessionId);
19
- if (!entries) {
20
- entries = new Set();
21
- sessions.set(sessionId, entries);
22
- }
23
- entries.add(read);
24
- return () => {
25
- entries.delete(read);
26
- if (entries.size === 0) sessions.delete(sessionId);
27
- };
18
+ return cellAuthority.registerInvocation(sessionId, read);
19
+ }
20
+
21
+ function isCellRule(value: unknown): boolean {
22
+ return (
23
+ typeof value === "object" &&
24
+ value !== null &&
25
+ ["permission", "pattern", "layer"].every((name) => typeof Reflect.get(value, name) === "string")
26
+ );
27
+ }
28
+
29
+ /** A snapshot another copy's kernel may have produced: every field present with its type. */
30
+ function isInvocationSession(value: unknown): value is InvocationSession {
31
+ if (typeof value !== "object" || value === null || typeof Reflect.get(value, "cwd") !== "string") return false;
32
+ const state: unknown = Reflect.get(value, "state");
33
+ if (typeof state !== "object" || state === null) return false;
34
+ const rules: unknown = Reflect.get(state, "rules");
35
+ return (
36
+ typeof Reflect.get(state, "epoch") === "number" &&
37
+ typeof Reflect.get(state, "active") === "boolean" &&
38
+ Array.isArray(rules) &&
39
+ rules.every(isCellRule)
40
+ );
41
+ }
42
+
43
+ function crossSessionRefusal(enforcingSessionId: string, rule?: CellPermissionState["rules"][number]): Error {
44
+ return Object.assign(
45
+ new Error(
46
+ rule
47
+ ? `Cross-session kernel tool refused while session ${enforcingSessionId} enforces deny rules, including ${rule.permission} ${rule.pattern} (${rule.layer})`
48
+ : "Cross-session kernel tool refused: permission authority unavailable",
49
+ ),
50
+ { code: "cell_permission_denied", reason: "cross_session_authority", enforcingSessionId, rule },
51
+ );
52
+ }
53
+
54
+ /**
55
+ * What the invoker's kernels report, from whichever copy registered them. A snapshot this copy cannot read refuses:
56
+ * an invoker whose deny state is not visible is never treated as having none.
57
+ */
58
+ function registeredInvocationSessions(invokerId: string): InvocationSession[] {
59
+ const snapshots: unknown = cellAuthority.invocations(invokerId);
60
+ if (!Array.isArray(snapshots)) throw crossSessionRefusal(invokerId);
61
+ return snapshots.map((snapshot: unknown) => {
62
+ if (!isInvocationSession(snapshot)) throw crossSessionRefusal(invokerId);
63
+ return snapshot;
64
+ });
28
65
  }
29
66
 
30
67
  export function invocationSession(
@@ -35,25 +72,17 @@ export function invocationSession(
35
72
  ): InvocationSession & { readonly sessionId: string } {
36
73
  const invokerId = options && !("aborted" in options) ? options.invoker?.sessionId : undefined;
37
74
  if (invokerId === undefined || invokerId === ownerId) return { sessionId: ownerId, ...owner };
38
- const registered = [...(sessions.get(invokerId) ?? [])].map((read) => read());
75
+ // Read on every cross-session call, before the hook, as one copy always has: an unreadable snapshot refuses even
76
+ // when the hook answers. The hook answers only with a session: `undefined` or `null` is no answer.
77
+ const registered = registeredInvocationSessions(invokerId);
39
78
  const invoker = resolveSession?.(invokerId) ?? registered.find((session) => session.state.active) ?? registered[0];
40
79
  for (const [enforcingSessionId, state] of [
41
80
  [ownerId, owner.state],
42
81
  [invokerId, invoker?.state],
43
82
  ] as const) {
44
- if (!state?.active) continue;
45
- const rule = state.rules[0];
46
- throw Object.assign(
47
- new Error(
48
- rule
49
- ? `Cross-session kernel tool refused while session ${enforcingSessionId} enforces deny rules, including ${rule.permission} ${rule.pattern} (${rule.layer})`
50
- : "Cross-session kernel tool refused: permission authority unavailable",
51
- ),
52
- { code: "cell_permission_denied", reason: "cross_session_authority", enforcingSessionId, rule },
53
- );
83
+ if (state?.active) throw crossSessionRefusal(enforcingSessionId, state.rules[0]);
54
84
  }
55
- return {
56
- sessionId: invokerId,
57
- ...(invoker ?? { cwd: owner.cwd, state: { epoch: 0, active: false, rules: [] } }),
58
- };
85
+ return invoker
86
+ ? { sessionId: invokerId, cwd: invoker.cwd, state: invoker.state }
87
+ : { sessionId: invokerId, cwd: owner.cwd, state: { epoch: 0, active: false, rules: [] } };
59
88
  }