@useshifu/coding-harness 0.3.28 → 0.3.29

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/README.md CHANGED
@@ -44,6 +44,8 @@ To disconnect this device, run `npx --yes @useshifu/coding-harness disconnect --
44
44
 
45
45
  ## Operate
46
46
 
47
+ Version `0.3.29` verifies native v5 sync across Codex, Claude Code, OpenCode 1 and OpenCode 2. Codex now loads a private allowlisted configuration and home instead of an ignored profile, with executable tools disabled. OpenCode 2 uses its standalone CLI, native provider/agent configuration, typed session database and isolated account storage. Native CI tests exercise original-source discovery, extraction, redaction, frozen retries and matching receipt checkpoints against local synthetic endpoints, without live credentials or paid models.
48
+
47
49
  Version `0.3.28` adds one isolated, pre-queue correction attempt for invalid v5 preparation, with field-specific reference and privacy diagnostics. Both attempts share the 180-second deadline. Privacy rules, trusted source binding, accepted checkpoints and exact queued retries remain unchanged.
48
50
 
49
51
  Version `0.3.27` adds a concrete topic-and-relationship retrieval playbook. Execution is the default for project work, first-person decision history and technical feedback; Growth requires explicit personal-development intent. The caller omits optional lexical query filters by default, accounts for Pi's internal expansion attempts, and preserves source authorship separately from contribution credit. Server retrieval reserves candidates for reformulation, ranks Execution without attribution metadata, and reports observed source groups without presenting repeated copies as independent confirmations.
@@ -51,7 +53,7 @@ Version `0.3.27` adds a concrete topic-and-relationship retrieval playbook. Exec
51
53
 
52
54
  Version `0.3.26` adds branded onboarding, independent growth-feedback and decision-alignment preferences, and an initial-history choice. `sessions --initial-history` uses the saved selection date when a backfill resumes instead of shifting a rolling days window. Publish this package version before deploying app setup prompts that reference it.
53
55
 
54
- Version `0.3.25` moves scheduled and pending preparation to the source-grounded v5 path across Codex, Claude Code and OpenCode. It caches each redacted payload before upload, preserves exact retries and queued legacy payloads, and leaves unfinished or failed ranges pending. The matching native CLI must support the required isolation flags; OpenCode requires 1.18.30 or newer.
56
+ Version `0.3.25` moves scheduled and pending preparation to the source-grounded v5 path across Codex, Claude Code and OpenCode. It caches each redacted payload before upload, preserves exact retries and queued legacy payloads, and leaves unfinished or failed ranges pending. The matching native CLI must support the required isolation flags; OpenCode 1 requires 1.18.30 or newer. OpenCode 2 requires harness 0.3.29 or newer and `sqlite3` for local source/account access.
55
57
 
56
58
  Version `0.3.23` introduced the context guidance below. It retrieves context at substantive task starts and new topics, tracks current topic coverage, and surfaces Shifu-attributed support or discrepancies alongside proposals and actions. Current user instructions take precedence over historical context; adopted choices and rationale remain available to existing session sync.
57
59
 
@@ -86,6 +88,10 @@ To upgrade an existing installation, run `npx --yes @useshifu/coding-harness@lat
86
88
 
87
89
  Scheduled runs and `pending` prepare version-five payloads using the matching Codex, Claude Code, or OpenCode CLI and its existing account and model configuration. The selected raw source is processed by that native model provider in an isolated temporary invocation; it is never uploaded to Shifu or appended to the original conversation. Tools, project instructions, plugins, and MCP access are disabled during preparation, and temporary preparation files are removed afterwards. No separate extraction API key is required. Unattended native preparation currently requires macOS or Linux.
88
90
 
91
+ Native sync gates pin Codex 0.163.0, Claude Code 2.1.301, OpenCode 1.18.30 and OpenCode 2.0.31. Other releases are checked for the required capabilities rather than assumed compatible. Codex file-backed authentication is shared without copying its session history; an explicit keyring-only store remains pending with an authentication remedy because its keyring namespace is tied to the original home. OpenCode 2 copies only native credentials into private temporary storage and conditionally preserves rotated OAuth values without overwriting concurrent account changes. No accepted history is replayed by this upgrade.
92
+
93
+ OpenCode 2 account finalization has a separate 10-second recovery budget after model preparation ends. If native account write-back fails, an owner-only credential recovery artifact is retained outside session discovery and replayed safely before the next preparation; source storage is removed. If credentials cannot be read or the recovery artifact cannot be saved, private preparation storage is retained locally for assisted account recovery instead of deleting potentially rotated credentials. Neither recovery path uploads source or credentials. Legacy OAuth accounts must first be migrated by opening OpenCode 2 normally; reconnecting Shifu is unnecessary.
94
+
89
95
  Each preparation invocation handles at most four ranges, each covering up to 12 original turns, 128 source messages, and 256 KiB. Oversized automatic ranges are shortened before preparation rather than silently truncating text. A native preparation has a 180-second deadline. Unfinished trailing turns stay pending regardless of age; partial assistant text and intermediate tool calls do not count as final reports. An unsupported native CLI, missing required isolation flags, unavailable account, timeout, or invalid model output leaves the range pending. Update or repair the matching CLI and retry; the runner does not fall back to the old keyword projection.
90
96
 
91
97
  Before queueing, invalid structured preparation receives at most one isolated correction attempt using only privacy-safe validator diagnostics and the same source packet, within the original 180-second deadline. Reference errors identify the evidence position and decision or verification field without exposing source text. Native invocation failures are not retried through this correction path. If correction still fails, the range remains pending.
@@ -120,7 +126,7 @@ The AI Profile page shows detected categories, model and thinking labels, and Je
120
126
 
121
127
  The connection endpoint returns `recommendedPackageVersion`, pinned to a published release. Before `sync`, `sync-files`, `approve`, or `scheduled-sync`, the runner compares its embedded release version with that property. An older runner installs the exact recommended release through npm and resumes the original command with the updated runner. Equal or newer runners continue unchanged. Explicitly recommended major releases follow the same comparison rule. Missing or invalid recommendations, connection failures, and npm failures do not block sync. The connection, pending work, checkpoints, and schedule are preserved.
122
128
 
123
- This update mechanism first ships in `0.3.10`. Runners from earlier releases need a one-time installation of a release containing it (`npx --yes @useshifu/coding-harness@0.3.28 install --harness codex`). Raise the server recommendation only after the target release is published and its backend requirements are deployed.
129
+ This update mechanism first ships in `0.3.10`. Runners from earlier releases need a one-time installation of a release containing it (`npx --yes @useshifu/coding-harness@0.3.29 install --harness codex`). Raise the server recommendation only after the target release is published and its backend requirements are deployed.
124
130
 
125
131
  ## Force sync local work
126
132
 
@@ -155,7 +161,7 @@ Setup preserves other instruction text and MCP settings. Package installation re
155
161
  The context branch adds a remote MCP connection using the same saved Codex key:
156
162
 
157
163
  ```sh
158
- npx --yes @useshifu/coding-harness@0.3.28 mcp-setup --harness codex --mcp-url https://YOUR-BRANCH-HOST/mcp
164
+ npx --yes @useshifu/coding-harness@0.3.29 mcp-setup --harness codex --mcp-url https://YOUR-BRANCH-HOST/mcp
159
165
  ```
160
166
 
161
167
  For development, install dependencies and run from the checkout root:
@@ -186,7 +192,7 @@ Official configuration reference: https://learn.chatgpt.com/docs/config-file/con
186
192
  OpenCode uses the same `/mcp` endpoint and three tools, authenticated with its own saved OpenCode connection key:
187
193
 
188
194
  ```sh
189
- npx --yes @useshifu/coding-harness@0.3.28 mcp-setup --harness opencode --mcp-url https://YOUR-SHIFU-HOST/mcp
195
+ npx --yes @useshifu/coding-harness@0.3.29 mcp-setup --harness opencode --mcp-url https://YOUR-SHIFU-HOST/mcp
190
196
  ```
191
197
 
192
198
  For checkout usage, install dependencies with `npm ci --prefix packages/shifu-coding-harness --ignore-scripts` from the checkout root, then run the checkout CLI with the same arguments. Setup preserves existing sync settings and other MCP servers, checks authenticated initialization and tool discovery, and installs the `shifu-context` usage instructions. A missing OpenCode connection prompts for its key in the terminal. A Codex key cannot authorize an OpenCode connection.
@@ -206,7 +212,7 @@ For the optional native integration gate, set `SHIFU_TEST_OPENCODE_BIN` to a tem
206
212
  Claude Code connects to the same `/mcp` endpoint and three tools with its own saved connection key:
207
213
 
208
214
  ```sh
209
- npx --yes @useshifu/coding-harness@0.3.28 mcp-setup --harness claude_code --mcp-url https://YOUR-SHIFU-HOST/mcp
215
+ npx --yes @useshifu/coding-harness@0.3.29 mcp-setup --harness claude_code --mcp-url https://YOUR-SHIFU-HOST/mcp
210
216
  ```
211
217
 
212
218
  For checkout usage, install dependencies as above and run the checkout CLI with these arguments. Setup checks the existing Claude Code key, authenticated initialization and tool discovery, and installs the `shifu-context` skill. It preserves the sync endpoint, schedule, checkpoints and other MCP servers. A missing connection prompts for a Claude Code key in the terminal; do not substitute a Codex or OpenCode key.
@@ -1,7 +1,6 @@
1
1
  const fs = require("node:fs");
2
2
  const os = require("node:os");
3
3
  const path = require("node:path");
4
- const { randomUUID } = require("node:crypto");
5
4
  const { spawn } = require("node:child_process");
6
5
  const toml = require("smol-toml");
7
6
  const jsonc = require("jsonc-parser");
@@ -105,29 +104,39 @@ async function requireFlags(command, prefix, flags, options) {
105
104
  }
106
105
  }
107
106
 
108
- async function codexInvocation(directory, env, cleanup, deadlineAt) {
107
+ async function codexInvocation(directory, env, deadlineAt) {
109
108
  const command = resolveNativeBinary("codex", env);
110
- await requireFlags(command, ["exec"], ["--ephemeral", "--ignore-user-config", "--ignore-rules", "--output-schema", "--profile"], { cwd: directory, env, deadlineAt });
109
+ await requireFlags(command, ["exec"], ["--ephemeral", "--ignore-rules", "--output-schema"], { cwd: directory, env, deadlineAt });
111
110
  const home = env.CODEX_HOME || path.join(os.homedir(), ".codex");
112
111
  let original = optionalConfig(path.join(home, "config.toml"), toml.parse);
113
112
  if (typeof original.profile === "string" && /^[A-Za-z0-9_-]+$/.test(original.profile)) {
114
113
  original = { ...original, ...optionalConfig(path.join(home, `${original.profile}.config.toml`), toml.parse) };
115
114
  }
116
- const config = pick(original, ["model", "model_provider", "model_providers", "model_reasoning_effort", "model_reasoning_summary", "model_verbosity", "service_tier"]);
115
+ const config = pick(original, ["model", "model_provider", "model_providers", "model_reasoning_effort", "model_reasoning_summary", "model_verbosity", "service_tier", "cli_auth_credentials_store"]);
117
116
  Object.assign(config, {
118
117
  approval_policy: "never", sandbox_mode: "read-only", web_search: "disabled", project_doc_max_bytes: 0,
119
- mcp_servers: {}, apps: { _default: { enabled: false } }, tools: { view_image: false }, sqlite_home: directory,
120
- features: { shell_tool: false, shell_snapshot: false, hooks: false, apps: false, plugins: false,
118
+ mcp_servers: {}, apps: { _default: { enabled: false } },
119
+ tools: { experimental_request_user_input: { enabled: false }, update_plan: { enabled: false } }, sqlite_home: directory,
120
+ features: { shell_tool: false, view_image: false, goals: false, shell_snapshot: false, hooks: false, apps: false, plugins: false,
121
121
  browser_use: false, browser_use_external: false, computer_use: false, multi_agent: false, memories: false,
122
122
  skill_mcp_dependency_install: false },
123
123
  });
124
- const profile = `shifu-preparation-${randomUUID()}`;
125
- const profileFile = path.join(home, `${profile}.config.toml`);
126
- privateFile(profileFile, toml.stringify(config));
127
- cleanup.push(profileFile);
124
+ const isolatedHome = path.join(directory, "codex");
125
+ fs.mkdirSync(isolatedHome, { mode: 0o700 });
126
+ const authFile = path.join(home, "auth.json");
127
+ if (original.cli_auth_credentials_store === "keyring") {
128
+ throw new Error("Native preparation requires file-backed Codex authentication for isolated account reuse. Configure Codex file-backed authentication and retry; no source was sent.");
129
+ }
130
+ try {
131
+ if (!fs.statSync(authFile).isFile()) throw new Error("Invalid native account storage");
132
+ fs.symlinkSync(authFile, path.join(isolatedHome, "auth.json"));
133
+ } catch (error) {
134
+ if (error.code !== "ENOENT") throw new Error("Native preparation could not reuse the existing Codex account. Repair native authentication and retry.");
135
+ }
136
+ privateFile(path.join(isolatedHome, "config.toml"), toml.stringify(config));
128
137
  const schema = path.join(directory, "schema.json");
129
138
  privateFile(schema, JSON.stringify(PREPARATION_SCHEMA));
130
- return { command, env: { ...env, CODEX_HOME: home }, args: ["exec", "--ignore-user-config", "--ignore-rules", "--profile", profile,
139
+ return { command, env: { ...env, CODEX_HOME: isolatedHome, HOME: directory, PWD: directory }, args: ["exec", "--ignore-rules",
131
140
  "--ephemeral", "--json", "--skip-git-repo-check", "--sandbox", "read-only", "--cd", directory, "--output-schema", schema, "-"] };
132
141
  }
133
142
 
@@ -168,11 +177,12 @@ function readOpenCodeConfig(directory) {
168
177
 
169
178
  async function opencodeInvocation(directory, env, deadlineAt) {
170
179
  const command = resolveNativeBinary("opencode", env);
171
- await requireFlags(command, ["run"], ["--pure", "--format", "--agent", "--dir"], { cwd: directory, env, deadlineAt });
172
- const version = (await runNativeCommand(command, ["--version"], { cwd: directory, env, deadlineAt, timeoutMs: 10_000 })).trim().match(/^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/);
173
- if (!version || Number(version[1]) < 1 || (Number(version[1]) === 1 && (Number(version[2]) < 18 || (Number(version[2]) === 18 && Number(version[3]) < 30)))) {
180
+ const version = (await runNativeCommand(command, ["--version"], { cwd: directory, env, deadlineAt, timeoutMs: 10_000 })).trim().match(/^(?:opencode\s+)?v?(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/);
181
+ if (!version || ![1, 2].includes(Number(version[1])) || (Number(version[1]) === 1 && (Number(version[2]) < 18 || (Number(version[2]) === 18 && Number(version[3]) < 30)))) {
174
182
  throw new Error("Native preparation requires OpenCode 1.18.30 or newer for isolated sessions. Update the native CLI and retry; the source remains pending.");
175
183
  }
184
+ const v2 = Number(version[1]) === 2;
185
+ await requireFlags(command, ["run"], v2 ? ["--standalone", "--format", "--agent"] : ["--pure", "--format", "--agent", "--dir"], { cwd: directory, env, deadlineAt });
176
186
  const originalConfig = path.join(env.XDG_CONFIG_HOME || path.join(os.homedir(), ".config"), "opencode");
177
187
  const originalData = path.join(env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share"), "opencode");
178
188
  let original = readOpenCodeConfig(originalConfig);
@@ -184,11 +194,21 @@ async function opencodeInvocation(directory, env, deadlineAt) {
184
194
  try { original = mergeConfig(original, parseOpenCodeConfig(env.OPENCODE_CONFIG_CONTENT)); }
185
195
  catch { throw new Error("Native preparation could not read the existing inline provider configuration. Repair that configuration and retry."); }
186
196
  }
187
- const config = pick(original, ["model", "small_model", "provider", "enabled_providers", "disabled_providers"]);
188
- const selectedAgent = original.agent?.[original.default_agent || "build"];
197
+ const config = pick(original, ["model", "small_model", "provider", "providers", "enabled_providers", "disabled_providers"]);
198
+ const selectedAgent = (v2 ? original.agents?.[original.default_agent || "build"] : undefined) || original.agent?.[original.default_agent || "build"];
189
199
  if (selectedAgent?.model) config.model = selectedAgent.model;
200
+ const prompt = `${POLICY}\nReturn raw JSON only, without Markdown or code fences. Return JSON matching this schema exactly. Decisions and verification must be arrays of strings, never objects.\n${JSON.stringify(PREPARATION_SCHEMA)}`;
190
201
  Object.assign(config, { snapshot: false, share: "disabled", autoupdate: false, permission: "deny", instructions: [], mcp: {},
191
- default_agent: "shifu-preparation", agent: { "shifu-preparation": { ...pick(selectedAgent || {}, ["model", "variant", "temperature", "top_p"]), mode: "primary", prompt: `${POLICY}\nReturn raw JSON only, without Markdown or code fences. Return JSON matching this schema exactly. Decisions and verification must be arrays of strings, never objects.\n${JSON.stringify(PREPARATION_SCHEMA)}`, permission: "deny", tools: { "*": false }, steps: 1 } } });
202
+ default_agent: "shifu-preparation", agent: { "shifu-preparation": { ...pick(selectedAgent || {}, ["model", "variant", "temperature", "top_p"]), mode: "primary", prompt, permission: "deny", tools: { "*": false }, steps: 1 } } });
203
+ if (v2) {
204
+ delete config.agent;
205
+ Object.assign(config, { snapshots: false, update: "disable", plugins: [], skills: [], mcp: { servers: {} },
206
+ permissions: [{ action: "*", resource: "*", effect: "deny" }],
207
+ policies: [...(original.policies || []).filter((policy) => policy.action === "provider.use"),
208
+ { action: "tool.use", resource: "*", effect: "deny" }, { action: "integration.use", resource: "*", effect: "deny" }],
209
+ agents: { "shifu-preparation": { ...pick(selectedAgent || {}, ["model", "request"]), mode: "primary", system: prompt,
210
+ permissions: [{ action: "*", resource: "*", effect: "deny" }], steps: 1 } } });
211
+ }
192
212
  const configDirectory = path.join(directory, "config", "opencode");
193
213
  const dataDirectory = path.join(directory, "data", "opencode");
194
214
  const stateDirectory = path.join(directory, "state", "opencode");
@@ -198,7 +218,7 @@ async function opencodeInvocation(directory, env, deadlineAt) {
198
218
  try {
199
219
  const authFile = path.join(originalData, "auth.json");
200
220
  if (!fs.statSync(authFile).isFile()) throw new Error("Invalid native account storage");
201
- fs.symlinkSync(authFile, path.join(dataDirectory, "auth.json"));
221
+ if (!v2) fs.symlinkSync(authFile, path.join(dataDirectory, "auth.json"));
202
222
  }
203
223
  catch (error) { if (error.code !== "ENOENT") throw new Error("Native preparation could not read the existing OpenCode account. Repair it and retry."); }
204
224
  // The native default uses recent models when no configured model is selected.
@@ -209,12 +229,17 @@ async function opencodeInvocation(directory, env, deadlineAt) {
209
229
  .map((entry) => ({ providerID: entry.providerID, modelID: entry.modelID }));
210
230
  privateFile(path.join(stateDirectory, "model.json"), JSON.stringify({ recent }));
211
231
  }
212
- const isolatedEnv = { ...env, XDG_CONFIG_HOME: path.join(directory, "config"), XDG_DATA_HOME: path.join(directory, "data"),
232
+ const isolatedEnv = { ...env, PWD: directory, XDG_CONFIG_HOME: path.join(directory, "config"), XDG_DATA_HOME: path.join(directory, "data"),
213
233
  XDG_STATE_HOME: path.join(directory, "state"), XDG_CACHE_HOME: path.join(directory, "cache"), OPENCODE_CONFIG_DIR: configDirectory,
214
234
  OPENCODE_DB: ":memory:", OPENCODE_TEST_HOME: directory, OPENCODE_DISABLE_PROJECT_CONFIG: "1", OPENCODE_DISABLE_CLAUDE_CODE: "1",
215
235
  OPENCODE_DISABLE_EXTERNAL_SKILLS: "1", OPENCODE_DISABLE_AUTOUPDATE: "1", OPENCODE_AUTO_SHARE: "0" };
216
236
  for (const key of ["OPENCODE_CONFIG", "OPENCODE_CONFIG_CONTENT", "OPENCODE_PERMISSION", "OPENCODE_ATTACH"]) delete isolatedEnv[key];
217
- return { command, env: isolatedEnv, args: ["run", "--pure", "--format", "json", "--agent", "shifu-preparation", "--title", "Shifu extraction preparation", "--dir", directory] };
237
+ if (v2) isolatedEnv.OPENCODE_DB = path.join(dataDirectory, "preparation.db");
238
+ const finalize = v2 ? await require("./opencode-v2-account.js").prepareOpenCodeAccount({
239
+ command, originalEnv: env, isolatedEnv, directory, deadlineAt, runNativeCommand,
240
+ legacyAuthFile: path.join(originalData, "auth.json"),
241
+ }) : undefined;
242
+ return { command, env: isolatedEnv, finalize, args: ["run", v2 ? "--standalone" : "--pure", "--format", "json", "--agent", "shifu-preparation", "--title", "Shifu extraction preparation", ...v2 ? [] : ["--dir", directory]] };
218
243
  }
219
244
 
220
245
  function parseOutput(harness, text) {
@@ -246,12 +271,12 @@ async function prepareNative(harness, packet, options = {}) {
246
271
  const startedAt = Date.now();
247
272
  const timeoutMs = Math.min(options.timeoutMs || TIMEOUT_MS, TIMEOUT_MS);
248
273
  const deadlineAt = startedAt + timeoutMs;
249
- const directory = fs.mkdtempSync(path.join(os.tmpdir(), "shifu-native-preparation-"));
274
+ const directory = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "shifu-native-preparation-")));
250
275
  fs.chmodSync(directory, 0o700);
251
- const cleanup = [];
276
+ let invocation;
252
277
  try {
253
278
  const env = { ...process.env };
254
- const invocation = harness === "codex" ? await codexInvocation(directory, env, cleanup, deadlineAt)
279
+ invocation = harness === "codex" ? await codexInvocation(directory, env, deadlineAt)
255
280
  : harness === "claude_code" ? await claudeInvocation(directory, env, deadlineAt) : await opencodeInvocation(directory, env, deadlineAt);
256
281
  const input = preparationPrompt(packet, options.validationFeedback);
257
282
  if (Buffer.byteLength(input) > MAX_OUTPUT_BYTES) throw new Error("Native preparation source packet exceeds the input limit.");
@@ -260,8 +285,14 @@ async function prepareNative(harness, packet, options = {}) {
260
285
  const output = await runNativeCommand(invocation.command, invocation.args, { cwd: directory, env: invocation.env, input, timeoutMs: remainingMs, deadlineAt });
261
286
  return parseOutput(harness, output);
262
287
  } finally {
263
- try { for (const file of cleanup) fs.rmSync(file, { force: true }); }
264
- finally { fs.rmSync(directory, { recursive: true, force: true }); }
288
+ let preservePreparation = false;
289
+ try { await invocation?.finalize?.(); }
290
+ catch (error) {
291
+ preservePreparation = error.preservePreparation === true;
292
+ throw error;
293
+ } finally {
294
+ if (!preservePreparation) fs.rmSync(directory, { recursive: true, force: true });
295
+ }
265
296
  }
266
297
  }
267
298
 
@@ -0,0 +1,186 @@
1
+ const fs = require("node:fs");
2
+ const os = require("node:os");
3
+ const path = require("node:path");
4
+ const { createHash, randomUUID } = require("node:crypto");
5
+ const { pathToFileURL } = require("node:url");
6
+
7
+ const FINALIZATION_TIMEOUT_MS = 10_000;
8
+ const CREDENTIAL_COLUMNS = "id, integration_id, label, value, active, time_created";
9
+ const ACCOUNT_ERROR = "Native preparation could not isolate or preserve the OpenCode account. Check sqlite3 and the native account, then retry; the source remains pending.";
10
+ const LEGACY_OAUTH_ERROR = "Native preparation requires migrated OpenCode 2 OAuth accounts. Open OpenCode 2 once to migrate its native account, then retry; do not reconnect Shifu. The source remains pending.";
11
+
12
+ async function readCredentials(database, options) {
13
+ if (!fs.existsSync(database)) return [];
14
+ const tables = await options.runNativeCommand("sqlite3", ["-readonly", "-json", database], {
15
+ ...options, input: "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'credential';\n",
16
+ });
17
+ if (!JSON.parse(tables || "[]").length) return [];
18
+ const output = await options.runNativeCommand("sqlite3", ["-readonly", "-json", database], {
19
+ ...options, input: `SELECT ${CREDENTIAL_COLUMNS} FROM credential ORDER BY active DESC, time_created DESC, id DESC;\n`,
20
+ });
21
+ const rows = JSON.parse(output || "[]");
22
+ for (const row of rows) {
23
+ if (typeof row.id !== "string" || typeof row.label !== "string" || typeof row.value !== "string") throw new Error(ACCOUNT_ERROR);
24
+ row.parsedValue = JSON.parse(row.value);
25
+ }
26
+ return rows;
27
+ }
28
+
29
+ function exportedCredentials(rows) {
30
+ const selected = new Set();
31
+ return rows.filter((row) => typeof row.integration_id === "string").map((row) => {
32
+ // Native v2 selects the newest credential when legacy rows lack an active flag.
33
+ const active = !selected.has(row.integration_id);
34
+ selected.add(row.integration_id);
35
+ return { id: row.id, integrationID: row.integration_id, label: row.label, value: row.parsedValue, active };
36
+ });
37
+ }
38
+
39
+ function sqlParameter(name, value) {
40
+ // Hex-encoded bindings cannot introduce SQL or sqlite shell metacommands.
41
+ return `.parameter set @${name} X'${Buffer.from(value, "utf8").toString("hex")}'\n`;
42
+ }
43
+
44
+ function requirePrivateDestination(directory, destination) {
45
+ const relative = path.relative(directory, destination);
46
+ if (!relative || relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) throw new Error(ACCOUNT_ERROR);
47
+ }
48
+
49
+ function copyLegacyAccount({ legacyAuthFile, isolatedEnv, directory }) {
50
+ if (!legacyAuthFile || !fs.existsSync(legacyAuthFile)) return;
51
+ const text = fs.readFileSync(legacyAuthFile, "utf8");
52
+ const account = JSON.parse(text);
53
+ if (!account || typeof account !== "object" || Array.isArray(account)) throw new Error(ACCOUNT_ERROR);
54
+ // V2 refreshes SQLite credentials, not legacy auth.json; copying OAuth would lose rotated tokens.
55
+ if (Object.values(account).some((value) => value?.type === "oauth")) throw new Error(LEGACY_OAUTH_ERROR);
56
+ const destination = path.join(isolatedEnv.XDG_DATA_HOME, "opencode", "auth.json");
57
+ requirePrivateDestination(directory, destination);
58
+ fs.mkdirSync(path.dirname(destination), { recursive: true, mode: 0o700 });
59
+ fs.writeFileSync(destination, text, { mode: 0o600, flag: "wx" });
60
+ }
61
+
62
+ function credentialRecoveryDirectory(env) {
63
+ const configRoot = env.SHIFU_CONFIG_ROOT || path.join(env.XDG_CONFIG_HOME || path.join(env.HOME || os.homedir(), ".config"), "shifu", "coding-harness");
64
+ return path.join(configRoot, "credential-recovery");
65
+ }
66
+
67
+ function requirePrivateRecoveryDirectory(directory) {
68
+ const stat = fs.lstatSync(directory);
69
+ if (!stat.isDirectory() || (stat.mode & 0o077) || (process.getuid && stat.uid !== process.getuid())) throw new Error(ACCOUNT_ERROR);
70
+ }
71
+
72
+ function recoveryPrefix(database) {
73
+ return createHash("sha256").update(database).digest("hex");
74
+ }
75
+
76
+ function credentialRecoveryError(file) {
77
+ const error = new Error("OpenCode account refresh could not be saved to its native account. A private credential-only recovery file remains in Shifu's credential-recovery directory. Close concurrent OpenCode processes and retry sync to restore it automatically; do not reconnect Shifu. The source remains pending.");
78
+ error.code = "SHIFU_NATIVE_ACCOUNT_RECOVERY";
79
+ error.recoveryFile = file;
80
+ return error;
81
+ }
82
+
83
+ function retainCredentialRecovery(database, credentials, env) {
84
+ const recoveryDirectory = credentialRecoveryDirectory(env);
85
+ fs.mkdirSync(recoveryDirectory, { recursive: true, mode: 0o700 });
86
+ requirePrivateRecoveryDirectory(recoveryDirectory);
87
+ const file = path.join(recoveryDirectory, `${recoveryPrefix(database)}-${Date.now()}-${randomUUID()}.credential-recovery`);
88
+ fs.writeFileSync(file, JSON.stringify({ version: 1, harness: "opencode", database, credentials }), { mode: 0o600, flag: "wx" });
89
+ return credentialRecoveryError(file);
90
+ }
91
+
92
+ async function writeCredentialRefreshes(database, credentials, options) {
93
+ const updates = credentials.map((credential) => sqlParameter("id", credential.id) + sqlParameter("integration", credential.integrationID) +
94
+ sqlParameter("previous", credential.previousValue) + sqlParameter("updated", credential.value) +
95
+ "UPDATE credential SET value = CAST(@updated AS TEXT) WHERE id = CAST(@id AS TEXT) AND integration_id = CAST(@integration AS TEXT) AND value = CAST(@previous AS TEXT);\n");
96
+ // Compare-and-swap leaves concurrent logins, refreshes, deletions and account selection untouched.
97
+ await options.runNativeCommand("sqlite3", [`${pathToFileURL(database).href}?mode=rw`], {
98
+ ...options, input: `.bail on\n.timeout 1000\n.parameter init\nBEGIN IMMEDIATE;\n${updates.join("")}COMMIT;\n`,
99
+ });
100
+ }
101
+
102
+ async function recoverCredentialRefreshes(database, options) {
103
+ const directory = credentialRecoveryDirectory(options.env);
104
+ if (!fs.existsSync(directory)) return;
105
+ requirePrivateRecoveryDirectory(directory);
106
+ const prefix = recoveryPrefix(database);
107
+ const pattern = new RegExp(`^${prefix}-[0-9]+-[a-f0-9-]+\\.credential-recovery$`);
108
+ const files = fs.readdirSync(directory).filter((name) => pattern.test(name)).sort();
109
+ // Recovery is bounded separately from source discovery and never reads unrelated files.
110
+ if (files.length > 32) throw credentialRecoveryError(path.join(directory, files[0]));
111
+ for (const name of files) {
112
+ const file = path.join(directory, name);
113
+ try {
114
+ const stat = fs.lstatSync(file);
115
+ if (!stat.isFile() || (stat.mode & 0o077) || (process.getuid && stat.uid !== process.getuid()) || stat.size > 256 * 1024) throw new Error(ACCOUNT_ERROR);
116
+ const recovery = JSON.parse(fs.readFileSync(file, "utf8"));
117
+ if (recovery.version !== 1 || recovery.harness !== "opencode" || recovery.database !== database ||
118
+ !Array.isArray(recovery.credentials) || !recovery.credentials.length || recovery.credentials.length > 128 ||
119
+ !recovery.credentials.every((credential) => typeof credential.id === "string" && typeof credential.integrationID === "string" &&
120
+ typeof credential.previousValue === "string" && typeof credential.value === "string" &&
121
+ JSON.parse(credential.previousValue)?.type === "oauth" && JSON.parse(credential.value)?.type === "oauth")) throw new Error(ACCOUNT_ERROR);
122
+ await writeCredentialRefreshes(database, recovery.credentials, options);
123
+ fs.unlinkSync(file);
124
+ } catch {
125
+ throw credentialRecoveryError(file);
126
+ }
127
+ }
128
+ }
129
+
130
+ async function preserveRefreshes(originalDatabase, snapshots, options, originalEnv) {
131
+ const isolatedDatabase = options.env.OPENCODE_DB;
132
+ const current = await readCredentials(isolatedDatabase, options);
133
+ const credentials = [];
134
+ for (const row of current) {
135
+ const original = snapshots.find((snapshot) => snapshot.id === row.id);
136
+ if (!original || original.parsedValue.type !== "oauth" || row.parsedValue.type !== "oauth" ||
137
+ original.integration_id !== row.integration_id || JSON.stringify(original.parsedValue) === JSON.stringify(row.parsedValue)) continue;
138
+ credentials.push({ id: row.id, integrationID: original.integration_id, previousValue: original.value, value: row.value });
139
+ }
140
+ if (!credentials.length) return;
141
+ try {
142
+ await writeCredentialRefreshes(originalDatabase, credentials, options);
143
+ } catch {
144
+ throw retainCredentialRecovery(originalDatabase, credentials, originalEnv);
145
+ }
146
+ }
147
+
148
+ /** Copy only native v2 credentials into the isolated database, then preserve token refreshes. */
149
+ async function prepareOpenCodeAccount({ command, originalEnv, isolatedEnv, directory, deadlineAt, runNativeCommand, legacyAuthFile }) {
150
+ const originalOptions = { cwd: directory, env: originalEnv, deadlineAt, runNativeCommand };
151
+ const isolatedOptions = { cwd: directory, env: isolatedEnv, deadlineAt, runNativeCommand };
152
+ try {
153
+ requirePrivateDestination(directory, isolatedEnv.OPENCODE_DB);
154
+ const database = (await runNativeCommand(command, ["debug", "paths", "db"], originalOptions)).trim();
155
+ if (!path.isAbsolute(database) || /[\r\n]/.test(database)) throw new Error(ACCOUNT_ERROR);
156
+ await recoverCredentialRefreshes(database, originalOptions);
157
+ const snapshots = await readCredentials(database, originalOptions);
158
+ const credentials = exportedCredentials(snapshots);
159
+ if (!credentials.length) {
160
+ copyLegacyAccount({ legacyAuthFile, isolatedEnv, directory });
161
+ return async () => {};
162
+ }
163
+ await runNativeCommand(command, ["auth", "import", "--standalone"], {
164
+ ...isolatedOptions, input: JSON.stringify(credentials),
165
+ });
166
+ if (fs.existsSync(isolatedEnv.OPENCODE_DB)) fs.chmodSync(isolatedEnv.OPENCODE_DB, 0o600);
167
+ if (!snapshots.some((snapshot) => snapshot.parsedValue.type === "oauth")) return async () => {};
168
+ return async () => {
169
+ try {
170
+ const finalizationOptions = { ...isolatedOptions, deadlineAt: Date.now() + FINALIZATION_TIMEOUT_MS };
171
+ await preserveRefreshes(database, snapshots, finalizationOptions, originalEnv);
172
+ } catch (error) {
173
+ if (error.code === "SHIFU_NATIVE_ACCOUNT_RECOVERY") throw error;
174
+ const recoveryError = new Error("OpenCode account recovery could not be saved safely. Private preparation files were retained locally to prevent losing refreshed native credentials. Close concurrent OpenCode processes and ask your assistant to recover the native account before retrying; do not reconnect Shifu. The source remains pending.");
175
+ recoveryError.preservePreparation = true;
176
+ recoveryError.recoveryDirectory = directory;
177
+ throw recoveryError;
178
+ }
179
+ };
180
+ } catch (error) {
181
+ if (error.message === LEGACY_OAUTH_ERROR || error.code === "SHIFU_NATIVE_ACCOUNT_RECOVERY") throw error;
182
+ throw new Error(ACCOUNT_ERROR);
183
+ }
184
+ }
185
+
186
+ module.exports = { prepareOpenCodeAccount };
@@ -46,7 +46,7 @@ const MAX_TURNS_PER_SEGMENT = 12;
46
46
  const MAX_TITLE_LENGTH = 160;
47
47
  const MAX_WORK_DETAIL_LENGTH = 600;
48
48
  const CONFIG_VERSION = 2;
49
- const RUNNER_VERSION = "0.3.28";
49
+ const RUNNER_VERSION = "0.3.29";
50
50
  const REDACTION_VERSION = 5;
51
51
  const LEGACY_REDACTION_VERSION = 4;
52
52
  const MAX_PREPARATIONS_PER_RUN = 4;
@@ -109,13 +109,9 @@ function claudeSessionsRoot() {
109
109
  function opencodeQuery(query) {
110
110
  if (process.env.SHIFU_OPENCODE_DB && !process.env.SHIFU_OPENCODE_BIN) {
111
111
  try {
112
- const sqliteOutput = execFileSync("sqlite3", ["-json", process.env.SHIFU_OPENCODE_DB, query], {
113
- encoding: "utf8",
114
- stdio: ["ignore", "pipe", "pipe"],
115
- maxBuffer: 64 * 1024 * 1024,
116
- });
117
- return JSON.parse(sqliteOutput);
118
- } catch {
112
+ return openCodeDatabaseQuery(process.env.SHIFU_OPENCODE_DB, query);
113
+ } catch (error) {
114
+ if (error.code === "SHIFU_SQLITE_UNAVAILABLE") throw error;
119
115
  throw new Error("Could not read OpenCode session metadata. Ensure the opencode command is installed and its local database is available.");
120
116
  }
121
117
  }
@@ -137,16 +133,13 @@ function opencodeQuery(query) {
137
133
  try { fs.closeSync(fd); } catch {}
138
134
  fd = undefined;
139
135
  }
140
- const dbPath = process.env.SHIFU_OPENCODE_DB || path.join(process.env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share"), "opencode", "opencode.db");
136
+ const dbPath = process.env.SHIFU_OPENCODE_DB || openCodeDatabasePath();
141
137
  if (fs.existsSync(dbPath)) {
142
138
  try {
143
- const sqliteOutput = execFileSync("sqlite3", ["-json", dbPath, query], {
144
- encoding: "utf8",
145
- stdio: ["ignore", "pipe", "pipe"],
146
- maxBuffer: 64 * 1024 * 1024,
147
- });
148
- return JSON.parse(sqliteOutput);
149
- } catch {}
139
+ return openCodeDatabaseQuery(dbPath, query);
140
+ } catch (error) {
141
+ if (error.code === "SHIFU_SQLITE_UNAVAILABLE") throw error;
142
+ }
150
143
  }
151
144
  throw new Error("Could not read OpenCode session metadata. Ensure the opencode command is installed and its local database is available.");
152
145
  } finally {
@@ -157,6 +150,55 @@ function opencodeQuery(query) {
157
150
  }
158
151
  }
159
152
 
153
+ function openCodeDatabasePath() {
154
+ try {
155
+ const { resolveNativeBinary, nativeEnvironment } = require("./native-preparation.js");
156
+ const location = execFileSync(resolveNativeBinary("opencode"), ["debug", "paths", "db"], {
157
+ env: nativeEnvironment(), encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 10_000, maxBuffer: 1024 * 1024,
158
+ }).trim();
159
+ if (path.isAbsolute(location) && !/[\r\n\0]/.test(location)) return location;
160
+ } catch {}
161
+ return path.join(process.env.XDG_DATA_HOME || path.join(os.homedir(), ".local", "share"), "opencode", "opencode.db");
162
+ }
163
+
164
+ function openCodeDatabaseQuery(dbPath, query) {
165
+ function read(sql) {
166
+ let output;
167
+ try {
168
+ output = execFileSync("sqlite3", ["-readonly", "-json", "-cmd", ".timeout 5000", dbPath, sql], {
169
+ encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout: 10_000, maxBuffer: 64 * 1024 * 1024,
170
+ });
171
+ } catch (error) {
172
+ if (error.code !== "ENOENT") throw error;
173
+ const unavailable = new Error("Reading the local OpenCode database requires sqlite3. Install sqlite3 and retry; no work was synced.");
174
+ unavailable.code = "SHIFU_SQLITE_UNAVAILABLE";
175
+ throw unavailable;
176
+ }
177
+ return output.trim() ? JSON.parse(output) : [];
178
+ }
179
+ const tables = read("SELECT name FROM sqlite_master WHERE type = 'table' AND name IN ('session_v2', 'session_message')");
180
+ if (tables.length !== 2) return read(query);
181
+ // V2 stores typed messages, ordered by sequence, instead of separate parts.
182
+ // Read-only CTEs retain the existing adapter contract without changing its database.
183
+ const projection = `WITH
184
+ session AS (SELECT id, time_created, time_updated FROM session_v2),
185
+ message AS (
186
+ SELECT id, session_id, seq, time_created, json_object(
187
+ 'role', type, 'finish', CASE WHEN json_extract(data, '$.time.completed') IS NOT NULL THEN json_extract(data, '$.finish') END,
188
+ 'error', json_extract(data, '$.error'), 'modelID', json_extract(data, '$.model.id'), 'storageVersion', 2
189
+ ) AS data FROM session_message WHERE type IN ('user', 'assistant', 'idle')
190
+ ),
191
+ part AS (
192
+ SELECT id || '_user' AS id, id AS message_id, json_object('type', 'text', 'text', json_extract(data, '$.text')) AS data
193
+ FROM session_message WHERE type = 'user'
194
+ UNION ALL
195
+ SELECT m.id || '_' || printf('%08d', p.key), m.id, p.value
196
+ FROM session_message m, json_each(m.data, '$.content') p WHERE m.type = 'assistant'
197
+ ) `;
198
+ return read(projection + query.replaceAll("ORDER BY s.time_updated, m.time_created", "ORDER BY s.time_updated, m.seq")
199
+ .replaceAll("ORDER BY m.time_created, p.id", "ORDER BY m.seq, p.id"));
200
+ }
201
+
160
202
  function opencodeSessions(rows) {
161
203
  const sessions = new Map();
162
204
  for (const row of rows) {
@@ -589,7 +631,7 @@ function reviewSourcePacket(harness, sessionRef, fromTurn, toTurn, scheduled = f
589
631
 
590
632
  function sourceRecords(harness, sessionRef) {
591
633
  if (harness === "opencode") {
592
- return opencodeQuery(`SELECT m.id AS messageId, m.time_created AS createdAt, json_extract(m.data, '$.role') AS role, json_extract(m.data, '$.finish') AS finish, json_extract(m.data, '$.error') AS error, p.data AS part FROM message m LEFT JOIN part p ON p.message_id = m.id WHERE m.session_id = ${JSON.stringify(sessionRef)} ORDER BY m.time_created, p.id`);
634
+ return opencodeQuery(`SELECT m.id AS messageId, m.time_created AS createdAt, json_extract(m.data, '$.role') AS role, json_extract(m.data, '$.finish') AS finish, json_extract(m.data, '$.error') AS error, json_extract(m.data, '$.storageVersion') AS storageVersion, p.data AS part FROM message m LEFT JOIN part p ON p.message_id = m.id WHERE m.session_id = ${JSON.stringify(sessionRef)} ORDER BY m.time_created, p.id`);
593
635
  }
594
636
  const file = sessionFile(harness, sessionRef);
595
637
  try { return fs.readFileSync(file, "utf8").split("\n").filter(Boolean).map((line) => JSON.parse(line)); }
@@ -1231,7 +1273,7 @@ function copyRunner() {
1231
1273
  const installedDependency = path.join(configRoot(), "node_modules", name);
1232
1274
  if (!sameFile(dependency, installedDependency)) copyDirectory(dependency, installedDependency);
1233
1275
  }
1234
- for (const name of ["source-review.js", "v5-payload.js", "native-preparation.js", "context-preferences.js", "setup-options.js", "initial-history.js", "onboarding-ui.js"]) {
1276
+ for (const name of ["source-review.js", "v5-payload.js", "native-preparation.js", "opencode-v2-account.js", "context-preferences.js", "setup-options.js", "initial-history.js", "onboarding-ui.js"]) {
1235
1277
  const source = path.join(__dirname, name);
1236
1278
  const destination = path.join(configRoot(), name);
1237
1279
  if (!sameFile(source, destination)) {
@@ -191,7 +191,8 @@ function completedSourceTurn(selection, records) {
191
191
  if (message.role === "user") {
192
192
  turnCount += 1;
193
193
  closed = false;
194
- } else if (message.role === "assistant") closed = message.finish === "stop" && message.error == null;
194
+ } else if (message.role === "assistant") closed = message.storageVersion !== 2 && message.finish === "stop" && message.error == null;
195
+ else if (message.storageVersion === 2 && message.role === "idle") closed = true;
195
196
  }
196
197
  } else throw new Error("Unsupported original-source harness.");
197
198
  return Math.max(0, turnCount - (closed ? 0 : 1));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@useshifu/coding-harness",
3
- "version": "0.3.28",
3
+ "version": "0.3.29",
4
4
  "description": "Scheduled, redacted coding-harness activity sync for Shifu",
5
5
  "bin": {
6
6
  "coding-harness": "bin/shifu-harness.js",
@@ -12,7 +12,7 @@ Before first connection or MCP personalization, ask for any choices that are not
12
12
  When asked to connect Shifu context, run:
13
13
 
14
14
  ```sh
15
- npx --yes @useshifu/coding-harness@0.3.28 mcp-setup --harness codex
15
+ npx --yes @useshifu/coding-harness@0.3.29 mcp-setup --harness codex
16
16
  ```
17
17
 
18
18
  MCP setup verifies the connection and tools, saves the choices and installs instructions; it does not sync sessions or upload the selected history. History selection governs eligible work for subsequent sync. Do not start sync as part of this setup unless the user separately asks for it. If a saved connection exists, reuse its key and do not run `connect` again.