@gajae-code/coding-agent 0.22.1 → 0.22.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.
Files changed (75) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/dist/types/cli/doctor/probe-types.d.ts +1 -1
  3. package/dist/types/cli/doctor/session-owners-probe.d.ts +7 -0
  4. package/dist/types/cli/doctor/session-owners.d.ts +16 -0
  5. package/dist/types/cli/github-release.d.ts +16 -0
  6. package/dist/types/cli/session-owners-cli.d.ts +13 -0
  7. package/dist/types/cli/update-cli.d.ts +2 -0
  8. package/dist/types/commands/sdk.d.ts +23 -14
  9. package/dist/types/config/settings-schema.d.ts +6 -0
  10. package/dist/types/exec/bash-shell-guardian.d.ts +61 -0
  11. package/dist/types/export/html/html-export-destination.d.ts +16 -0
  12. package/dist/types/sdk/bus/telegram-daemon-contract.d.ts +1 -1
  13. package/dist/types/sdk/host/control/runtime-gate.d.ts +7 -0
  14. package/dist/types/session/internal/graph-successor-proof.d.ts +33 -0
  15. package/dist/types/session/internal/managed-session-storage.d.ts +6 -1
  16. package/dist/types/session/internal/session-claim-set.d.ts +69 -0
  17. package/dist/types/session/operator-error-message.d.ts +29 -0
  18. package/dist/types/session/session-manager.d.ts +57 -3
  19. package/dist/types/session/session-owners-layout.d.ts +24 -0
  20. package/dist/types/session/session-ownership-mode.d.ts +45 -0
  21. package/dist/types/session/session-ownership.d.ts +195 -0
  22. package/dist/types/session/session-storage.d.ts +36 -0
  23. package/dist/types/slash-commands/helpers/parse.d.ts +1 -0
  24. package/package.json +12 -8
  25. package/scripts/verify-gjc-sdk-canonicalization.ts +2 -0
  26. package/src/cli/doctor/probe-types.ts +1 -1
  27. package/src/cli/doctor/probe.ts +9 -5
  28. package/src/cli/doctor/runner.ts +6 -1
  29. package/src/cli/doctor/session-owners-probe.ts +69 -0
  30. package/src/cli/doctor/session-owners.ts +106 -0
  31. package/src/cli/github-release.ts +100 -0
  32. package/src/cli/session-owners-cli.ts +118 -0
  33. package/src/cli/stats-cli.ts +19 -1
  34. package/src/cli/update-cli.ts +50 -3
  35. package/src/cli-ordinary.ts +1 -1
  36. package/src/commands/sdk.ts +36 -33
  37. package/src/commands/session.ts +10 -2
  38. package/src/config/settings-schema.ts +18 -0
  39. package/src/config/settings.ts +127 -13
  40. package/src/exec/bash-shell-guardian.ts +181 -12
  41. package/src/exec/isolated-shell.ts +13 -3
  42. package/src/export/html/html-export-destination.ts +47 -2
  43. package/src/export/html/index.ts +161 -13
  44. package/src/internal-urls/docs-index.generated.ts +4 -4
  45. package/src/main.ts +20 -14
  46. package/src/modes/acp/acp-agent.ts +6 -0
  47. package/src/modes/action-registry.ts +4 -1
  48. package/src/modes/controllers/command-controller.ts +12 -5
  49. package/src/modes/controllers/extension-ui-controller.ts +6 -22
  50. package/src/modes/controllers/input-controller.ts +3 -2
  51. package/src/modes/controllers/selector-controller.ts +14 -1
  52. package/src/modes/interactive-mode.ts +6 -3
  53. package/src/modes/print-mode.ts +2 -1
  54. package/src/modes/runtime-init.ts +2 -17
  55. package/src/modes/utils/ui-helpers.ts +5 -3
  56. package/src/sdk/bus/index.ts +22 -0
  57. package/src/sdk/bus/telegram-daemon-contract.ts +1 -1
  58. package/src/sdk/host/control/dispatch.ts +5 -1
  59. package/src/sdk/host/control/runtime-gate.ts +22 -0
  60. package/src/sdk/host/host.ts +4 -1
  61. package/src/sdk/session-readonly/metadata.ts +24 -18
  62. package/src/sdk/session.ts +2 -0
  63. package/src/session/agent-session.ts +101 -22
  64. package/src/session/internal/graph-successor-proof.ts +108 -0
  65. package/src/session/internal/managed-session-storage.ts +8 -1
  66. package/src/session/internal/session-claim-set.ts +242 -0
  67. package/src/session/operator-error-message.ts +79 -0
  68. package/src/session/session-manager.ts +524 -108
  69. package/src/session/session-owners-layout.ts +52 -0
  70. package/src/session/session-ownership-mode.ts +176 -0
  71. package/src/session/session-ownership.ts +851 -0
  72. package/src/session/session-storage.ts +60 -16
  73. package/src/setup/credential-import.ts +2 -1
  74. package/src/slash-commands/builtin-registry.ts +2 -0
  75. package/src/slash-commands/helpers/parse.ts +3 -1
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Where session claims live and how their files are named (design §2.2). A leaf module with no
3
+ * native or storage imports, so `gjc doctor`'s worker can count sidecars without loading the
4
+ * ownership core, and both read one definition.
5
+ */
6
+ import * as fs from "node:fs";
7
+ import * as path from "node:path";
8
+
9
+ /** The owners directory, relative to the trusted home. */
10
+ export const SESSION_OWNERS_RELATIVE_DIR = path.join(".gjc", "runtime", "session-owners");
11
+ export const SESSION_OWNER_SIDECAR_SUFFIX = ".session-owner";
12
+ export const SESSION_OWNER_RECORD_SUFFIX = ".owner.json";
13
+ /** A sidecar or record file name's key, as `sessionOwnerKey` produces it. */
14
+ export const SESSION_OWNER_KEY_NAME = /^(?:h-[0-9a-f]{32}|(?!h-)[a-z0-9_-]{1,128})$/;
15
+
16
+ export type SessionOwnerEntryKind =
17
+ | { readonly kind: "sidecar"; readonly key: string }
18
+ | { readonly kind: "record"; readonly key: string }
19
+ | { readonly kind: "unrecognized" };
20
+
21
+ /** Classify one directory entry. Only a regular file with a well-formed key is a sidecar or record. */
22
+ export function classifySessionOwnerEntry(name: string, isFile: boolean): SessionOwnerEntryKind {
23
+ if (isFile) {
24
+ for (const [suffix, kind] of [
25
+ [SESSION_OWNER_SIDECAR_SUFFIX, "sidecar"],
26
+ [SESSION_OWNER_RECORD_SUFFIX, "record"],
27
+ ] as const) {
28
+ if (!name.endsWith(suffix)) continue;
29
+ const key = name.slice(0, -suffix.length);
30
+ if (SESSION_OWNER_KEY_NAME.test(key)) return { kind, key };
31
+ }
32
+ }
33
+ return { kind: "unrecognized" };
34
+ }
35
+
36
+ export interface SessionOwnerEntryCounts {
37
+ readonly sidecars: number;
38
+ readonly records: number;
39
+ readonly unrecognized: number;
40
+ }
41
+
42
+ /** Count a directory's entries from `readdir` file types alone: no lock, no stat per entry. */
43
+ export function countSessionOwnerEntries(directory: string): SessionOwnerEntryCounts {
44
+ const counts = { sidecars: 0, records: 0, unrecognized: 0 };
45
+ for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
46
+ const { kind } = classifySessionOwnerEntry(entry.name, entry.isFile());
47
+ if (kind === "sidecar") counts.sidecars += 1;
48
+ else if (kind === "record") counts.records += 1;
49
+ else counts.unrecognized += 1;
50
+ }
51
+ return counts;
52
+ }
@@ -0,0 +1,176 @@
1
+ /**
2
+ * Session ownership mode and test seams (design §2.3, §7.3).
3
+ *
4
+ * - `off`: no claims and no lock calls; the process is invisible to other processes' claims.
5
+ * - `observe`: claims and the write-path detector run, and nothing is refused.
6
+ * - `enforce`: refusals as each later step enables them.
7
+ *
8
+ * The mode comes from the user setting `session.ownership.mode`, which is global-only, so a project
9
+ * cannot set it. The environment override `GJC_SESSION_OWNERSHIP` is read only when the operator
10
+ * set it (not a project `.env`) and can only lower the mode.
11
+ *
12
+ * A leaf module: Settings imports it to supply the setting, so it imports nothing from config.
13
+ */
14
+ import { logger } from "@gajae-code/utils";
15
+ import { type ProjectEnvSnapshot, projectEnvSnapshot } from "@gajae-code/utils/env-file";
16
+ import { trustedCoordinatorEnv } from "../gjc-runtime/sidecar-bootstrap-env";
17
+
18
+ export const SESSION_OWNERSHIP_MODES = ["off", "observe", "enforce"] as const;
19
+ export type SessionOwnershipMode = (typeof SESSION_OWNERSHIP_MODES)[number];
20
+ export const DEFAULT_SESSION_OWNERSHIP_MODE: SessionOwnershipMode = "off";
21
+ export const SESSION_OWNERSHIP_ENV = "GJC_SESSION_OWNERSHIP";
22
+
23
+ /** Test-only seams shared by the ownership modules. Code, not environment, so a project `.env` cannot reach them. */
24
+ export interface SessionOwnershipTestHookSlot {
25
+ /** Trusted home for this process's claims and probes. The bun test preload sets it for every test process. */
26
+ homeOverride?: string;
27
+ /** Replaces the resolved mode. */
28
+ mode?: SessionOwnershipMode;
29
+ /**
30
+ * The strict seam (design §2.5): a write the detector flags throws `SessionWriteWithoutClaimError`
31
+ * instead of only being logged and counted. Entry-path and control-matrix tests turn it on.
32
+ */
33
+ strictWriteDetection?: boolean;
34
+ /**
35
+ * Where ownership counters are written. While `homeOverride` is set (every test process), counts
36
+ * go here or nowhere, so a test never writes the operator's stats database.
37
+ */
38
+ statsDbPath?: string;
39
+ }
40
+
41
+ /**
42
+ * One slot per process, under a versioned `globalThis` symbol, so two copies of the ownership
43
+ * modules share it and the test preload can fill it without importing them.
44
+ */
45
+ const TEST_HOOKS_KEY = Symbol.for("gajae-code.session-ownership.test-hooks.v1");
46
+
47
+ export function sessionOwnershipTestHookSlot(): SessionOwnershipTestHookSlot {
48
+ const store = globalThis as unknown as Record<symbol, SessionOwnershipTestHookSlot | undefined>;
49
+ let slot = store[TEST_HOOKS_KEY];
50
+ if (!slot) {
51
+ slot = {};
52
+ store[TEST_HOOKS_KEY] = slot;
53
+ }
54
+ return slot;
55
+ }
56
+
57
+ const RANK: Record<SessionOwnershipMode, number> = { off: 0, observe: 1, enforce: 2 };
58
+
59
+ function parseMode(value: unknown): SessionOwnershipMode | undefined {
60
+ return typeof value === "string" && (SESSION_OWNERSHIP_MODES as readonly string[]).includes(value)
61
+ ? (value as SessionOwnershipMode)
62
+ : undefined;
63
+ }
64
+
65
+ /**
66
+ * The effective mode: the setting (an invalid value reads as the default), lowered by `override`
67
+ * when that is a valid, lower mode. `override` never raises the mode.
68
+ */
69
+ export function resolveSessionOwnershipMode(setting: unknown, override: string | undefined): SessionOwnershipMode {
70
+ const configured = parseMode(setting) ?? DEFAULT_SESSION_OWNERSHIP_MODE;
71
+ if (configured === "off") return "off";
72
+ const lowered = parseMode(override?.trim().toLowerCase());
73
+ return lowered !== undefined && RANK[lowered] < RANK[configured] ? lowered : configured;
74
+ }
75
+
76
+ /**
77
+ * `GJC_SESSION_OWNERSHIP` as the operator set it. A value that matches the project dotenv
78
+ * snapshot was loaded by Bun from the repository and is ignored, as is any value when that
79
+ * snapshot could not be read in full.
80
+ */
81
+ export function trustedSessionOwnershipOverride(
82
+ env: NodeJS.ProcessEnv,
83
+ snapshot: ProjectEnvSnapshot,
84
+ ): string | undefined {
85
+ if (snapshot.incomplete) return undefined;
86
+ return trustedCoordinatorEnv(SESSION_OWNERSHIP_ENV, env, snapshot);
87
+ }
88
+
89
+ /**
90
+ * Bun loads the project `.env` from the launch directory, so provenance is checked there. A launch
91
+ * directory that is already gone leaves no snapshot to check against, and the override is ignored.
92
+ */
93
+ const LAUNCH_CWD = ((): string | undefined => {
94
+ try {
95
+ return process.cwd();
96
+ } catch {
97
+ return undefined;
98
+ }
99
+ })();
100
+
101
+ /** The override as read once for this process: the trusted value, or why the variable was ignored. */
102
+ interface OverrideRead {
103
+ readonly value: string | undefined;
104
+ readonly ignored?: "project_env" | "unreadable";
105
+ }
106
+
107
+ function readOverride(): OverrideRead {
108
+ const raw = process.env[SESSION_OWNERSHIP_ENV];
109
+ if (raw === undefined || raw.trim() === "") return { value: undefined };
110
+ if (LAUNCH_CWD === undefined) return { value: undefined, ignored: "unreadable" };
111
+ try {
112
+ const value = trustedSessionOwnershipOverride(process.env, projectEnvSnapshot(LAUNCH_CWD));
113
+ return value === undefined ? { value, ignored: "project_env" } : { value };
114
+ } catch {
115
+ return { value: undefined, ignored: "unreadable" };
116
+ }
117
+ }
118
+
119
+ let cachedOverride: OverrideRead | undefined;
120
+ let reportedOverride = false;
121
+
122
+ /** An ignored override fails quietly for the user, so say why once, at debug level. */
123
+ function reportIgnoredOverride(read: OverrideRead, configured: SessionOwnershipMode, effective: SessionOwnershipMode) {
124
+ if (reportedOverride) return;
125
+ let reason: string | undefined;
126
+ if (read.ignored === "project_env") reason = "it was set by the project .env, not the operator";
127
+ else if (read.ignored === "unreadable") reason = "the project .env could not be checked";
128
+ else if (read.value !== undefined) {
129
+ const requested = parseMode(read.value.trim().toLowerCase());
130
+ if (requested === undefined) reason = "its value is not off, observe or enforce";
131
+ else if (effective === configured && requested !== configured) reason = "it can only lower the configured mode";
132
+ }
133
+ if (reason === undefined) return;
134
+ reportedOverride = true;
135
+ logger.debug(`${SESSION_OWNERSHIP_ENV} ignored: ${reason}`, { configured });
136
+ }
137
+
138
+ /** Test-only: forget the cached override, so a test can change the environment. */
139
+ export function resetSessionOwnershipModeForTests(): void {
140
+ cachedOverride = undefined;
141
+ reportedOverride = false;
142
+ }
143
+
144
+ /** Test-only: whether this process has read `GJC_SESSION_OWNERSHIP` since the last reset. */
145
+ export function sessionOwnershipOverrideWasRead(): boolean {
146
+ return cachedOverride !== undefined;
147
+ }
148
+
149
+ let settingSource: (() => unknown) | undefined;
150
+
151
+ /** Settings supplies the global-only `session.ownership.mode` value. Without a source the default applies. */
152
+ export function setSessionOwnershipSettingSource(source: (() => unknown) | undefined): void {
153
+ settingSource = source;
154
+ }
155
+
156
+ /**
157
+ * The mode for this process now. With the setting `off` it returns before reading the environment,
158
+ * so `off` does no work beyond reading the setting.
159
+ */
160
+ export function sessionOwnershipMode(): SessionOwnershipMode {
161
+ const forced = sessionOwnershipTestHookSlot().mode;
162
+ if (forced !== undefined) return forced;
163
+ let setting: unknown;
164
+ try {
165
+ setting = settingSource?.();
166
+ } catch {
167
+ setting = undefined;
168
+ }
169
+ const configured = resolveSessionOwnershipMode(setting, undefined);
170
+ // `off` is the kill switch: it returns before the environment is read at all.
171
+ if (configured === "off") return "off";
172
+ cachedOverride ??= readOverride();
173
+ const effective = resolveSessionOwnershipMode(setting, cachedOverride.value);
174
+ reportIgnoredOverride(cachedOverride, configured, effective);
175
+ return effective;
176
+ }