{"record":{"id":"f3dbc7639cdd94dd","repo":"affaan-m/ECC","slug":"capsule-exists","errorCode":"capsule.exists","errorMessage":"capsule already exists at ${resolved}","messagePattern":"capsule already exists at (.+?)","errorType":"error_code","errorClass":"CapsuleError","httpStatus":null,"severity":"error","filePath":"scripts/lib/eval-harness/capsule.js","lineNumber":141,"sourceCode":"\nclass Capsule {\n  /**\n   * @param {string} dir capsule root (created if missing)\n   * @param {object} meta { run_id, capsule_id, harness_version, task_family }\n   */\n  constructor(dir, meta, options = {}) {\n    this.dir = path.resolve(dir);\n    this.meta = meta;\n    this.clock = options.clock || null;\n    this.journalPath = path.join(this.dir, JOURNAL_FILE);\n    this.lastHash = envelope.GENESIS_HASH;\n    this.nextSeq = 0;\n  }\n\n  static create(dir, options = {}) {\n    const resolved = path.resolve(dir);\n    if (fs.existsSync(path.join(resolved, META_FILE))) {\n      throw new CapsuleError('capsule.exists', `capsule already exists at ${resolved}`);\n    }\n    const meta = {\n      schema: envelope.SCHEMA_VERSION,\n      run_id: options.run_id === undefined ? newId('run') : options.run_id,\n      capsule_id: options.capsule_id === undefined ? newId('capsule') : options.capsule_id,\n      harness_version: options.harness_version === undefined ? 'unknown' : options.harness_version,\n      task_family: options.task_family === undefined ? 'unspecified' : options.task_family,\n      created_at: nowIso(options.clock),\n    };\n    const failure = metadataFailure(meta);\n    if (failure) throw new CapsuleError(failure.code, failure.reason);\n    fs.mkdirSync(resolved, { recursive: true });\n    fs.writeFileSync(path.join(resolved, META_FILE), canonicalJson(meta) + '\\n', 'utf8');\n    fs.writeFileSync(path.join(resolved, JOURNAL_FILE), '', 'utf8');\n    return new Capsule(resolved, meta, options);\n  }\n\n  static open(dir, options = {}) {","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/scripts/lib/eval-harness/capsule.js#L123-L159","documentation":"Capsule.create refuses to initialize a capsule in a directory that already contains a meta file (capsule.exists). Capsule metadata is intended to be written once; re-running create over the same directory would silently reset sequence numbers and hashes, corrupting the hash-linked journal chain. The resolved path is included in the message.","triggerScenarios":"Calling Capsule.create(dir) where dir already contains META_FILE; re-running a setup script twice; pointing create at an existing capsule directory instead of Capsule.open(dir); a previous run partially succeeded and left the meta file in place.","commonSituations":"Re-running an eval pipeline without cleaning its output dir; idempotency scripts that call create on every start; mixing 'create new capsule' and 'resume existing capsule' logic in one code path; copying a capsule directory and re-initializing it.","solutions":["Use Capsule.open(dir) instead of create when the capsule may already exist","Check fs.existsSync(path.join(dir, META_FILE)) (or catch capsule.exists) and branch: open if present, create if not","Point create at a fresh, unique directory (e.g. run-id-based path) per run","If the old capsule is truly disposable and you intend to discard it, explicitly delete the directory first, then create"],"exampleFix":"// before\nconst capsule = Capsule.create('./out');\n// after\nlet capsule;\ntry {\n  capsule = Capsule.open('./out');\n} catch (e) {\n  if (e.code === 'capsule.file_not_found' || e.code === 'capsule.directory_not_found') {\n    capsule = Capsule.create('./out');\n  } else throw e;\n}","handlingStrategy":"validation","validationCode":"const path = require('path');\nconst fs = require('fs');\nfunction canCreateCapsule(dir) {\n  const resolved = path.resolve(dir);\n  return !fs.existsSync(path.join(resolved, 'meta.json'));\n}","typeGuard":null,"tryCatchPattern":"let capsule;\ntry {\n  capsule = Capsule.create(dir);\n} catch (e) {\n  if (e.code === 'capsule.exists') capsule = Capsule.open(dir);\n  else throw e;\n}","preventionTips":["Use Capsule.open when a capsule may already exist","Give each run a fresh, uniquely named directory (timestamp/run-id based)","Make init scripts idempotent: check-before-create or catch capsule.exists","Clean up abandoned capsule dirs explicitly before re-initializing"],"tags":["filesystem","initialization","idempotency"],"backgroundTag":"file-already-exists","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}