Skip to content

An open-source, faster Devin alternative

TypeScript

computer: SP_SESSION_ID in every session's shell environment

Igor Zalutski via Computere813d60bb5a2
diff --git a/computer/src/runner/harness/codex/config.ts b/computer/src/runner/harness/codex/config.ts
index afd67843..2dcb5986 100644
--- a/computer/src/runner/harness/codex/config.ts
+++ b/computer/src/runner/harness/codex/config.ts
@@ -34,8 +34,8 @@ export const PROVIDER = 'calvin';
* PASS_ENV that a command needs (and the locale and terminal, when set); then the
* git identity runner/main.ts sets; then what bin/ reads: gh's and opencomputer's
* real binaries, the token socket's directory (git-credential-slack-presence), and
- * the files socket (calvin-send-file). SP_SESSION_KEY and the git author come with
- * the session's secrets (BASH_ENV), as for Claude.
+ * the files socket (calvin-send-file). SP_SESSION_KEY, SP_SESSION_ID and the git
+ * author come with the session's secrets (BASH_ENV), as for Claude.
*/
export const CHILD_ENV = [
'PATH', 'HOME', 'USER', 'SHELL', 'LANG', 'LC_ALL', 'LC_CTYPE', 'TERM', 'TZ', 'TMPDIR',
diff --git a/computer/src/runner/main.ts b/computer/src/runner/main.ts
index 191ba351..280eaac2 100644
--- a/computer/src/runner/main.ts
+++ b/computer/src/runner/main.ts
@@ -40,7 +40,7 @@ import { Session, type SessionHooks, prompts } from './session.js';
import { mayAnswer } from './prompts.js';
import { fetchAttachments, interruptDownloads } from './attachments.js';
import { type Linked, linkedFiles, withLinked } from '../links.js';
-import { SessionSecrets, redactingClient } from './session-secrets.js';
+import { SessionSecrets, redactingClient, sessionVars } from './session-secrets.js';
import { PromptGuard } from './prompt-guard.js';
import { codexBin } from '../runner-env.js';
import { fakeSlack } from '../dev/fake-slack.js';
@@ -98,7 +98,6 @@ const ROOT = path.resolve(import.meta.dirname, '..', '..');
const secrets = new SessionSecrets(secretsFor(HOST_RUN, path.basename(spec.socket, '.sock')));
// Commits are authored by the initiator (sessionVars) and committed by the app identity.
-const sessionVars = (b: Bound) => ({ GIT_AUTHOR_NAME: b.owner.gitName, GIT_AUTHOR_EMAIL: b.owner.gitEmail, SP_SESSION_KEY: b.key });
Object.assign(env, {
GIT_COMMITTER_NAME: env.SP_COMMITTER_NAME ?? 'Computer',
GIT_COMMITTER_EMAIL: env.SP_COMMITTER_EMAIL ?? '[email protected]',
diff --git a/computer/src/runner/session-secrets.test.ts b/computer/src/runner/session-secrets.test.ts
index e6891eb3..edf94777 100644
--- a/computer/src/runner/session-secrets.test.ts
+++ b/computer/src/runner/session-secrets.test.ts
@@ -2,7 +2,7 @@ import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
-import { SessionSecrets } from './session-secrets.js';
+import { SessionSecrets, sessionVars } from './session-secrets.js';
import { ReminderFilter } from './stream.js';
const dirs: string[] = [];
@@ -54,6 +54,21 @@ describe('SessionSecrets', () => {
delete process.env.SP_TEST_KEY;
});
+ it('gives every session SP_SESSION_ID: its id, else its key', () => {
+ const owner = { gitName: 'Ada', gitEmail: '[email protected]' };
+ expect(sessionVars({ key: 'C1:1.2', sessionId: 'cs_abc', owner })).toEqual({
+ GIT_AUTHOR_NAME: 'Ada',
+ GIT_AUTHOR_EMAIL: '[email protected]',
+ SP_SESSION_KEY: 'C1:1.2',
+ SP_SESSION_ID: 'cs_abc',
+ });
+ expect(sessionVars({ key: 'C1:1.2', owner }).SP_SESSION_ID).toBe('C1:1.2');
+ const s = secrets();
+ s.base(sessionVars({ key: 'C1:1.2', sessionId: 'cs_abc', owner }));
+ expect(readFileSync(path.join(path.dirname(s.envFile), 'values.sh'), 'utf8')).toContain(`export SP_SESSION_ID='cs_abc'`);
+ for (const k of Object.keys(sessionVars({ key: 'k', owner }))) delete process.env[k];
+ });
+
it('holds back a streamed value split across deltas until it can tell', () => {
const s = secrets();
s.apply([{ name: 'TOKEN', value: 'ghp_abcdefghij', scope: 'user' }]);
diff --git a/computer/src/runner/session-secrets.ts b/computer/src/runner/session-secrets.ts
index b9d3bd2e..f3ae4e63 100644
--- a/computer/src/runner/session-secrets.ts
+++ b/computer/src/runner/session-secrets.ts
@@ -13,9 +13,10 @@
// The variables are also set in the runner's own environment, for processes
// started later without the env file (a new query, MCP servers).
//
-// `values.sh` also carries the session's own variables (`base`): a spare
-// runner's Claude Code (work 069) started before the session was known, so
-// its Bash tool gets SP_SESSION_KEY and the git author only from here.
+// `values.sh` also carries the session's own variables (`base`, sessionVars):
+// a spare runner's Claude Code (work 069) started before the session was
+// known, so its Bash tool gets SP_SESSION_KEY, SP_SESSION_ID and the git
+// author only from here. Codex commands source the same file (BASH_ENV).
//
// Out of Slack: every known value is replaced with `[secret:NAME]` in what the
// runner posts. Variables (`!var`, `variable: true`) reach the environment the
@@ -38,6 +39,16 @@ const MIN_REDACT = 6;
/** Lines of a multi-line value (a PEM key) at least this long are redacted on their own too. */
const MIN_LINE = 16;
+/**
+ * The session's own variables, for `base`: commits authored by its initiator
+ * (the app identity commits them), its key, and SP_SESSION_ID, its public id
+ * (the key while it has none, as host/api-host.ts names it), which the knowledge
+ * repository's hooks/knowledge/prepare-commit-msg puts in a `Session:` trailer.
+ */
+export function sessionVars(s: { key: string; sessionId?: string; owner: { gitName: string; gitEmail: string } }): Record<string, string> {
+ return { GIT_AUTHOR_NAME: s.owner.gitName, GIT_AUTHOR_EMAIL: s.owner.gitEmail, SP_SESSION_KEY: s.key, SP_SESSION_ID: s.sessionId ?? s.key };
+}
+
const quote = (v: string) => `'${v.replaceAll("'", `'\\''`)}'`;
export class SessionSecrets {