fix(editorial): migrate exact CI ownership and fence failed research runs
Deploy Canina / deploy (push) Successful in 5m1s

This commit is contained in:
devops authored and devops committed 2026-10-24 10:28:30 +00:00
1 parent 01b6f1d3d7
commit f4add07780
10 files changed
+311 -11

No files matched your search

@@ -115,15 +115,7 @@ import { WorkerTransportModule, tracerReport } from './worker-transport.module';
},
{ id: 'independent-disposable-reviewer', role: 'SUPER_ADMIN' },
);
t = await workflow.transition(
t.id,
{
toState: 'READY',
expectedVersion: t.version,
idempotencyKey: randomUUID(),
},
{ id: 'pmo', role: 'Admin' },
);
// Gate decision now atomically queues READY; no second queue transition.
}
return t;
}
@@ -139,6 +131,34 @@ import { WorkerTransportModule, tracerReport } from './worker-transport.module';
b: { taskId: t.id, runId: l.runId, leaseToken: l.leaseToken },
};
}
it('stops a held run through authenticated failure API, fences late writes and preserves unknown reservation', async () => {
const {t,l,b}=await lease();
const d={...b,expectedVersion: t.version+1,idempotencyKey:'stop:'+l.runId,reason:'Disposable subprocess failure'};
expect((await request('editorial-failure',{...d,leaseToken:randomUUID()})).status).toBe(403);
expect((await request('editorial-failure',d,other)).status).toBe(403);
expect((await request('editorial-failure',{...d,actualCents:0})).status).toBe(400);
const stopped=await Promise.all([request('editorial-failure',d),request('editorial-failure',d)]);
expect(stopped.map(x=>x.status)).toEqual([201,201]);
expect(stopped[0].body).toMatchObject({state:'FAILED',retainedReservationCents:20});
expect((await request('heartbeat',b)).status).toBe(403);
expect((await request('editorial-result',{...b,brief:brief()})).status).toBe(403);
expect((await request('editorial-claim',{taskId:t.id})).status).toBe(403);
expect(await db.orchestrationArtifact.count({where:{runId:l.runId}})).toBe(0);
expect(await db.orchestrationAttestation.count({where:{runId:l.runId}})).toBe(0);
expect((await db.orchestrationBudgetLedger.findMany({where:{taskId:t.id}})).map(x=>[x.kind,x.amountCents])).toEqual([['RESERVE',20]]);
});
it('recovers an expired run only as independent reviewer and fences original token', async () => {
const {t,l,b}=await lease();
const d={runId:l.runId,expectedVersion:t.version+1,idempotencyKey:'stale-stop:'+l.runId,reason:'Disposable ended process'};
await expect(workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'})).rejects.toThrow();
await db.prepareWorkflowOutbox.update({where:{taskId:t.id},data:{leaseUntil:new Date(Date.now()-1000)}});
const results=await Promise.all([workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'}),workflow.editorialFailure(t.id,d,{id:'reviewer',role:'SUPER_ADMIN'})]);
expect(results.map(x=>x.state)).toEqual(['FAILED','FAILED']);
expect((await request('heartbeat',b)).status).toBe(403);
expect((await request('editorial-result',{...b,brief:brief()})).status).toBe(403);
expect(await db.orchestrationEvent.count({where:{taskId:t.id,eventType:'EDITORIAL_RUN_STOPPED'}})).toBe(1);
expect(await db.orchestrationBudgetLedger.count({where:{taskId:t.id}})).toBe(1);
});
it('projects only owned exact proposals and refuses pending dispatch', async () => {
const t = await proposal(false);
const p = await request('editorial-proposals/' + t.id);
@@ -32,6 +32,7 @@ import {
WorkflowIdentity,
} from "../orchestration/prepare-workflow.service";
import { EditorialFailureDto } from '../orchestration/dto/orchestration.dto';
import { EditorialProposalBridge, EditorialProposalDto, isEditorialProposal } from "./editorial-bridge";
import { EditorialExecutionService, EditorialClaimDto, EditorialQueueDto, EditorialResultDto } from './editorial-execution';
import { admittedTechTask, TECH_PILOT_KIND, techArtifact, validateTechSource } from "./tech-pilot-validator";
@@ -70,6 +71,10 @@ class LeaseDto extends ClaimDto {
@IsUUID() runId!: string;
@IsUUID() leaseToken!: string;
}
class EditorialHeldFailureDto extends EditorialFailureDto {
@IsUUID() taskId!: string;
@IsUUID() leaseToken!: string;
}
class CompleteDto extends LeaseDto {
@IsString() @MaxLength(2048) report!: string;
}
@@ -306,6 +311,11 @@ class WorkerTransportService {
throw new ConflictException("Artifact binding mismatch");
return JSON.parse(bytes);
}
async editorialFailure(a: WorkflowIdentity, dto: EditorialHeldFailureDto) {
const { taskId, ...failure } = dto;
const t = await this.workflow.editorialFailure(taskId, failure, a);
return { taskId: t.id, state: t.state, version: t.version, costStatus: 'unknown-upper-bound-reserved', retainedReservationCents: 20 };
}
stage(a: WorkflowIdentity, dto: StageEvidenceDto) {
return this.workflow.stageEvidence(a, dto);
}
@@ -330,6 +340,9 @@ class WorkerTransportController {
@Post('editorial-claim') editorialClaim(@Req() req: any, @Body() dto: EditorialClaimDto) {
return this.execution.claim(req.workerIdentity, dto);
}
@Post('editorial-failure') editorialFailure(@Req() req: any, @Body() dto: EditorialHeldFailureDto) {
return this.service.editorialFailure(req.workerIdentity, dto);
}
@Post('editorial-result') editorialResult(@Req() req: any, @Body() dto: EditorialResultDto) {
return this.execution.result(req.workerIdentity, dto);
}
@@ -8,6 +8,8 @@ import {
IsNotEmpty,
IsOptional,
IsString,
IsUUID,
MaxLength,
Max,
Min,
} from 'class-validator';
@@ -39,6 +41,12 @@ export class GateDecisionDto {
@IsOptional() @IsString() commitSha?: string;
@IsOptional() @IsString() environment?: string;
}
export class EditorialFailureDto {
@IsUUID() runId!: string;
@IsInt() @Min(1) expectedVersion!: number;
@IsString() @IsNotEmpty() @MaxLength(120) idempotencyKey!: string;
@IsString() @IsNotEmpty() @MaxLength(500) reason!: string;
}
export class RecordBudgetDto {
@Type(() => Number) @IsInt() @Min(1) amountCents!: number;
@IsString() @IsNotEmpty() idempotencyKey!: string;
@@ -0,0 +1,30 @@
import { PrepareWorkflowService } from './prepare-workflow.service';
import { randomUUID } from 'crypto';
describe('editorial failure recovery fencing',()=>{
const actor={id:'owner',role:'SUPER_ADMIN'},worker={id:'pmo',role:'WORKFLOW_WORKER'};
let db:any,core:PrepareWorkflowService,t:any,o:any,r:any,events:any[];
beforeEach(()=>{
t={id:randomUUID(),key:'editorial-v1:'+'a'.repeat(64),title:'Editorial research: fixture',department:'CONTENT',operationMode:'PREPARE',budgetLimitCents:20,state:'RUNNING',version:6,metadata:{createdBy:'pmo',workflowScope:'STAGE_PREPARE',environment:'staging',commitSha:'a'.repeat(40),scope:'editorial-research-to-brief-v1\nTopic: fixture\nResearch and evidence-backed brief only. No publication, release, production changes or execution authorization.',acceptanceCriteria:['evidence']}};
r={id:randomUUID(),taskId:t.id,status:'RUNNING',commitSha:t.metadata.commitSha,environment:'staging'};
o={id:randomUUID(),taskId:t.id,status:'PENDING',runId:r.id,leaseOwner:'pmo',leaseToken:randomUUID(),leaseUntil:new Date(Date.now()-1000),reservationId:'reserve'};events=[];
const tx:any={$queryRaw:async()=>[],orchestrationTask:{findUnique:async()=>t,update:async({data}:any)=>(Object.assign(t,{state:data.state,version:t.version+1}),{...t})},prepareWorkflowOutbox:{findUnique:async()=>o,update:async({data}:any)=>Object.assign(o,data)},orchestrationRun:{findUnique:async()=>r,update:async({data}:any)=>Object.assign(r,data)},orchestrationEvent:{findUnique:async({where}:any)=>events.find(e=>e.idempotencyKey===where.idempotencyKey),findFirst:async()=>({}),create:async({data}:any)=>events.push(data)},orchestrationBudgetLedger:{findUnique:async()=>({id:'reserve',taskId:t.id,kind:'RESERVE',actorId:'pmo',amountCents:20,idempotencyKey:'prepare-reserve:'+r.id}),aggregate:async()=>({_sum:{amountCents:20}}),findFirst:async()=>null},orchestrationArtifact:{count:async()=>0}};
let tail=Promise.resolve();db={$transaction:(fn:any)=>{const p=tail.then(()=>fn(tx));tail=p.catch(()=>{});return p;}};core=new PrepareWorkflowService(db,{workerIds:['pmo'],stageCiIds:[]});
});
const dto=()=>({runId:r.id,expectedVersion:6,idempotencyKey:'recover:'+r.id,reason:'local process ended; cost unknown'});
it('terminalizes expired run without QA/cost writes and replays concurrently',async()=>{
const d=dto();const results=await Promise.all([core.editorialFailure(t.id,d,actor),core.editorialFailure(t.id,d,actor)]);
expect(results.map(x=>x.state)).toEqual(['FAILED','FAILED']);expect(t.version).toBe(7);expect(r.status).toBe('FAILED');expect(o.status).toBe('DONE');expect(o.leaseToken).toBeNull();expect(o.reservationId).toBe('reserve');expect(events).toHaveLength(1);expect(events[0].payload.retainedReservationCents).toBe(20);
});
it('rejects wrong run, stale version, unprivileged actor, active recovery and wrong held token',async()=>{
for(const [d,a] of [[{...dto(),runId:randomUUID()},actor],[{...dto(),expectedVersion:5},actor],[dto(),{id:'other',role:'Admin'}],[dto(),worker]] as any)await expect(core.editorialFailure(t.id,d,a)).rejects.toThrow();
o.leaseUntil=new Date(Date.now()+30000);
await expect(core.editorialFailure(t.id,dto(),actor)).rejects.toThrow();
await expect(core.editorialFailure(t.id,{...dto(),leaseToken:randomUUID()},worker)).rejects.toThrow();
await core.editorialFailure(t.id,{...dto(),leaseToken:o.leaseToken},worker);expect(t.state).toBe('FAILED');
});
it('rejects cross-task run binding and changed idempotency payload',async()=>{
r.taskId=randomUUID();await expect(core.editorialFailure(t.id,dto(),actor)).rejects.toThrow();r.taskId=t.id;
const d=dto();await core.editorialFailure(t.id,d,actor);await expect(core.editorialFailure(t.id,{...d,reason:'changed'},actor)).rejects.toThrow();
});
});
@@ -25,6 +25,7 @@ import {
CreateOrchestrationTaskDto,
TransitionTaskDto,
GateDecisionDto,
EditorialFailureDto,
} from './dto/orchestration.dto';
const workflowValidation = new ValidationPipe({
transform: true,
@@ -215,6 +216,14 @@ export class OrchestrationController {
req,
);
}
@Post('tasks/:id/editorial-failure') async editorialFailure(
@Param('id') id: string,
@Body(workflowValidation) dto: EditorialFailureDto,
@Req() req: any,
) {
if (!this.workflow) return this.deny();
return this.safe(await this.workflow.editorialFailure(id, dto, this.actor(req)), req);
}
@Post('tasks/:id/budget') budget() {
return this.deny();
}
@@ -517,6 +517,52 @@ export class PrepareWorkflowService {
);
});
}
/** Stop only; never turns unknown spend into free retry capacity. */
async editorialFailure(id: string, dto: any, a: WorkflowIdentity) {
const held = dto.leaseToken !== undefined;
if (held) {
this.worker(a);
if (a.id !== 'pmo') throw new ForbiddenException('Canonical PMO required');
} else if (!this.reviewer(a)) throw new ForbiddenException('Independent recovery reviewer required');
if (typeof dto.reason !== 'string' || !dto.reason.trim() || dto.reason.length > 500)
throw new BadRequestException('Bounded failure reason required');
return this.db.$transaction(async (tx: any) => {
await this.allowed(tx);
const t = await this.task(tx, id);
const { admittedEditorialTask } = require('../orchestration-worker/editorial-execution');
if (!admittedEditorialTask(t)) throw new ForbiddenException('Exact editorial proposal required');
const proposed = await tx.orchestrationEvent.findFirst({ where: { taskId: id, sequence: 1, eventType: 'PREPARE_PROPOSED', actorId: 'pmo', toState: 'DRAFT' } });
const submitted = await tx.orchestrationEvent.findFirst({ where: { taskId: id, sequence: 2, eventType: 'PREPARE_TRANSITION', actorId: 'pmo', fromState: 'DRAFT', toState: 'GATE1_PENDING', idempotencyKey: t.key + ':submit' } });
if (!proposed || !submitted) throw new ForbiddenException('Editorial provenance required');
if (!held && t.metadata.createdBy === a.id) throw new ForbiddenException('Independent recovery reviewer required');
const fingerprint = JSON.stringify({ id, dto, a });
if (await this.replay(tx, dto.idempotencyKey, fingerprint)) return t;
this.version(t, dto);
const o = await tx.prepareWorkflowOutbox.findUnique({ where: { taskId: id } });
const r = await tx.orchestrationRun.findUnique({ where: { id: dto.runId } });
if (t.state !== 'RUNNING' || !o || o.status !== 'PENDING' || o.runId !== dto.runId ||
o.leaseOwner !== 'pmo' || !o.leaseUntil || !r || r.taskId !== id || r.status !== 'RUNNING' ||
r.environment !== 'staging' || r.commitSha !== t.metadata.commitSha ||
(held ? (o.leaseOwner !== a.id || o.leaseToken !== dto.leaseToken || o.leaseUntil <= new Date()) : o.leaseUntil > new Date()))
throw new ForbiddenException('Matching held lease or expired run required');
const reserve = await tx.orchestrationBudgetLedger.findUnique({ where: { id: o.reservationId } });
const ledger = await tx.orchestrationBudgetLedger.aggregate({ where: { taskId: id }, _sum: { amountCents: true } });
if (!reserve || reserve.taskId !== id || reserve.actorId !== 'pmo' || reserve.kind !== 'RESERVE' || reserve.amountCents !== 20 ||
reserve.idempotencyKey !== `prepare-reserve:${r.id}` || ledger._sum.amountCents !== 20 ||
await tx.orchestrationBudgetLedger.findFirst({ where: { taskId: id, kind: 'RECONCILE' } }) ||
await tx.orchestrationArtifact.count({ where: { runId: r.id } }))
throw new ForbiddenException('Unreconciled artifact-free editorial reservation required');
await tx.orchestrationRun.update({ where: { id: r.id }, data: { status: 'FAILED', retryClass: 'DEPENDENCY', finishedAt: new Date() } });
await tx.prepareWorkflowOutbox.update({ where: { id: o.id }, data: { status: 'DONE', leaseToken: null, leaseUntil: null } });
const updated = await tx.orchestrationTask.update({ where: { id }, data: { state: 'FAILED', version: { increment: 1 } } });
await tx.orchestrationEvent.create({ data: {
taskId: id, sequence: updated.version, actorId: a.id, eventType: 'EDITORIAL_RUN_STOPPED',
fromState: 'RUNNING', toState: 'FAILED', idempotencyKey: dto.idempotencyKey,
payload: { fingerprint, runId: r.id, reason: dto.reason, recoveryMode: held ? 'HELD_LEASE' : 'EXPIRED_RUN', costStatus: 'unknown-upper-bound-reserved', retainedReservationCents: 20 },
} });
return updated;
});
}
async stageEvidence(a: WorkflowIdentity, dto: any) {
this.identity(a, ['STAGE_CI']);
if (!this.config.stageCiIds.includes(a.id))
+41
View File
@@ -0,0 +1,41 @@
# Reviewed Stage editorial recovery
No live change was made by this patch. CI owns installation, parent owns authenticated recovery and any separately approved research. Do not use `complete`, DB writes, forged tokens or root runner invocation.
## Ownership
Read-only reconnaissance: `canina-hermes` Docker Config.User=root; `/proc/self/uid_map` maps container 0 to host 100000 for 65536 IDs; passwd hermes is 10000:10000 (host 110000:110000). Migration runs *inside* the container namespace, never with guessed host IDs. Existing package, dedicated PMO token (0400), locks and competitor editorial-runner cache (0700) are root-owned. Profile roots and normal caches are already hermes-owned and must not be recursively changed.
`provision-editorial-runtime.py` runs the tracked `editorial-ownership.py` through the existing clean-SHA serialized Stage CI gate before installing as hermes. `scan()` preflights every member, symlink ancestor, UID/GID, mode and hardlink; exact package/payload/installed skills, dedicated credential, editorial state names and profile `cache/editorial-runner` only. Read-only scan against the existing container accepted 96 paths. General profile files, provider credentials, other caches and config are excluded. Unknown entries fail closed. Modes and contents never change. Root journal `/opt/data/.editorial-ownership-v1.json` is 0600, records original IDs/modes/inodes; a second migration is a no-op. A partial failure rolls back. Explicit `rollback(home,journal)` refuses inode/mode/path-set conflicts; use only before an installer upgrade replaces files. CI must remain serialized and runner/client inactive during migration. After installation, normal installer rollback and ownership rollback are separate operations; do not blindly restore old journal after file replacement.
## Stop original RUNNING v6
After deploying/reviewing this SHA through CI, authenticated independent SUPER_ADMIN or configured owner-reviewer Admin uses the ordinary admin JWT (not a worker token). Read:
`GET https://stageapi.canina.ir/api/admin/orchestration/tasks/565ace1f-8cfc-4447-b978-66c36e7b767b`
Take the actual RUNNING run ID from `runs` (do not guess). Confirm version=6 and original process has ended. Invoke:
```
POST https://stageapi.canina.ir/api/admin/orchestration/tasks/565ace1f-8cfc-4447-b978-66c36e7b767b/editorial-failure
Authorization: Bearer <existing independent admin session JWT>
Content-Type: application/json
{"runId":"<actual original RUNNING run UUID>","expectedVersion":6,"idempotencyKey":"editorial-recovery:565ace1f:v6","reason":"Original supervised runner process ended before artifact ingestion; cache access failure; actual spend unknown"}
```
Requires exact editorial provenance, matching current run, expired lease, expected task version and unreconciled owned 20 reservation; rejects cross-run/cross-task/active lease. This does not require an expired execution approval to become valid again merely to stop work. Existing kill policy still applies: FREEZE_NEW_WORK permits stopping; STOP_AGENTS/DISABLE_MUTATIONS/EMERGENCY_HALT block as for other core operations. Owner controls switches through existing policy, not this endpoint. Identical replay is safe; different payload/key collision is rejected. Locks serialize heartbeat/result/claim/failure. Success is task FAILED v7, run FAILED, outbox DONE, token cleared, no artifact/QA/reconciliation, original unknown20 retained.
Held lease variant: canonical PMO worker may `POST /api/internal/prepare-worker/editorial-failure` with the same fields plus `taskId` and valid unexpired `leaseToken`; no worker can perform stale recovery. Never include actualCents or QA.
## Explicit retry and budget boundary
There is intentionally **no retry of this exhausted 20-cent task**: preserving unknown20 and reserving another20 under a20 task cap is mathematically impossible. Reusing the original reservation as fresh spend would falsely manufacture free capacity. A repeat claim on FAILED is denied before any ledger write; duplicate failure never creates another reservation.
After predecessor is confirmed FAILED/fenced, parent can explicitly submit a **new** editorial proposal through existing authenticated `POST /api/internal/prepare-worker/editorial-proposals`, with a fresh requestId, the reviewed deployment commit SHA, same bounded topic/criteria and budgetLimitCents=20 (see editorial-execution.md contract). Obtain a new independent Gate1 via existing `POST /api/admin/orchestration/tasks/<newTaskId>/gates/1/decide` with APPROVED, its current expectedVersion and a new idempotencyKey. This atomically queues READY under current policy. Do not renew or mutate the FAILED predecessor. Only then execute the reviewed hermes-identity runner on the *new task ID*. Original unknown20 remains reserved; the new explicit approval authorizes up to20 additional, so cumulative upper bound is40. Parent must make that extra-budget authorization explicit; if the existing authorization is a cumulative20 ceiling, retry remains blocked until actual spend is measured through a separately reviewed reconciliation path. This patch does not bypass that ceiling or invent cost0.
## Verification
TDD: missing failure method was observed red (3 tests), then green. Migration tests verify twice, exact ownership/modes, outside sentinel, symlink/public secret/unknown path rejection and injected partial-failure rollback. Real TCP/PostgreSQL worker test races duplicate failure, rejects wrong token/actor/extra cost field, denies heartbeat/result/reclaim after stop and verifies only original RESERVE20, no artifacts or attestations. Unit tests cover stale version, active stale recovery, wrong run/task, independent role and idempotency payload mismatch.
Disposable test database `editorial_recovery_isolated` only. Full migration chain has pre-existing `blogs` missing relation; isolated tests apply the four scoped orchestration migrations to a dedicated DB instead. Shared generated Prisma client was stale; tests/build use private generated .prisma mount, not shared mutation. Build initially failed from remapped fixture ownership and stale Prisma; private owned fixture fixed both. No Stage/Production migrations or writes, deploy, push, scheduler, runner or live research executed.
+77
View File
@@ -0,0 +1,77 @@
#!/usr/bin/env python3
"""CI-only exact editorial ownership migration; run inside container UID namespace.
No profiles, general caches, provider credentials or mode changes are permitted.
Caller must serialize with deployment; never run alongside an editorial runner.
"""
import json
import os
from pathlib import Path
import stat
PROFILES=('pmo','canina-competitor-worker','canina-seo-worker','canina-content-worker','canina-vet-review-worker','canina-qa-worker')
PACKAGE={'bundle.json','installer.py','client.py','runner.py','payload/editorial-v4/status.json'} | {f'payload/profiles/{p}/skills/canina-editorial-v4/{f}' for p in PROFILES for f in ('SKILL.md','references/policy.md','references/role.json')}
LEAVES={'editorial-v4/owner-proposal.json','editorial-v4/status.json','editorial-v4/deployment.json','editorial-v4/credentials/editorial-pmo-token','editorial-v4/.installer/lock','editorial-v4/.installer/transaction.json'} | {'editorial-v4/runtime-package/'+f for f in PACKAGE} | {f'profiles/{p}/skills/canina-editorial-v4/{f}' for p in PROFILES for f in ('SKILL.md','references/policy.md','references/role.json')}
# State names are taken from the reviewed clients, not arbitrary directory trees.
STATE_ROOTS={'editorial-v4/runner-state','editorial-v4/client-state'} | {f'profiles/{p}/cache/editorial-runner' for p in PROFILES}
DIRS={str(p) for f in LEAVES for p in Path(f).parents if str(p)!='.' and (str(p).startswith('editorial-v4') or '/skills/canina-editorial-v4' in str(p))} | STATE_ROOTS
def allowed(relative,isdir):
if relative in DIRS if isdir else relative in LEAVES:return True
# Runner-created state is limited to immediate regular files, never subtrees.
p=Path(relative)
return not isdir and str(p.parent) in STATE_ROOTS and p.name in ('lock','proposal.json')
def scan(home,uid,gid):
if not home.is_absolute() or home.is_symlink():raise ValueError('Canonical home required')
result=[]
roots=[home/'editorial-v4']+[home/f'profiles/{p}/skills/canina-editorial-v4' for p in PROFILES]+[home/r for r in STATE_ROOTS if r.startswith('profiles/')]
for root in roots:
for ancestor in root.parents:
if ancestor.is_symlink():raise ValueError('Symlink ancestor')
if not root.exists() and not root.is_symlink():continue
for p in [root,*sorted(root.rglob('*'))]:
s=p.lstat();r=str(p.relative_to(home));directory=stat.S_ISDIR(s.st_mode)
if not (directory or stat.S_ISREG(s.st_mode)) or not allowed(r,directory) or (not directory and s.st_nlink!=1):raise ValueError('Unreviewed editorial path')
mode=stat.S_IMODE(s.st_mode)
if (s.st_uid,s.st_gid) not in ((0,0),(uid,gid)) or mode & 0o7022:raise ValueError('Unexpected owner or writable mode')
if '/credentials/' in r and mode!=0o400:raise ValueError('Private credential required')
result.append({'path':r,'uid':s.st_uid,'gid':s.st_gid,'mode':mode,'dev':s.st_dev,'ino':s.st_ino})
return result
def check(home,row):
p=home/row['path'];s=p.lstat()
if (s.st_dev,s.st_ino,stat.S_IMODE(s.st_mode))!=(row['dev'],row['ino'],row['mode']):raise ValueError('Ownership migration conflict')
return p
def rollback(home,journal):
s=journal.lstat()
if not stat.S_ISREG(s.st_mode) or s.st_uid!=0 or stat.S_IMODE(s.st_mode)!=0o600 or s.st_nlink!=1:raise ValueError('Private journal required')
state=json.loads(journal.read_text())
if state['home']!=str(home):raise ValueError('Journal home mismatch')
rows=state['before']
# Full preflight before restoring; never follow changed paths.
current=scan(home,state['uid'],state['gid'])
if {r['path'] for r in current}!={r['path'] for r in rows}:raise ValueError('Rollback path conflict')
for row in rows:check(home,row)
for row in reversed(rows):os.chown(check(home,row),row['uid'],row['gid'],follow_symlinks=False)
journal.unlink()
def migrate(home,uid,gid,journal):
if os.getuid()!=0 or (uid,gid)!=(10000,10000):raise ValueError('Container root and canonical hermes identity required')
rows=scan(home,uid,gid)
if journal.exists():
s=journal.lstat()
if not stat.S_ISREG(s.st_mode) or s.st_uid!=0 or stat.S_IMODE(s.st_mode)!=0o600 or s.st_nlink!=1:raise ValueError('Private journal required')
old=json.loads(journal.read_text())
if old['home']!=str(home) or old['uid']!=uid or old['gid']!=gid:raise ValueError('Journal mismatch')
if all((r['uid'],r['gid'])==(uid,gid) for r in rows):return
raise ValueError('Incomplete migration: rollback first')
state={'home':str(home),'uid':uid,'gid':gid,'before':rows}
fd=os.open(journal,os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'w') as f:json.dump(state,f);f.flush();os.fsync(f.fileno())
try:
for row in rows:os.chown(check(home,row),uid,gid,follow_symlinks=False)
except BaseException:
rollback(home,journal)
raise
+6 -2
View File
@@ -108,8 +108,12 @@ def provision(sha, uid, gid):
home=Path(env.get('HERMES_HOME',''));safe(home)
if not any(home==Path(v['Destination']) or Path(v['Destination']) in home.parents for v in h['Mounts'] if v['Type'] in ('bind','volume')):raise ValueError('Persistent home required')
# Match the supervised Hermes/shim identity, NOT Docker Config.User (root).
# Existing root-owned installations fail closed; owner-reviewed CI migration
# is required rather than recursive chown of profiles or credentials.
# Preflight the exact reviewed migration in the container UID namespace.
identity=json.loads(docker('exec','canina-hermes','python3','-c',"import pwd,json; p=pwd.getpwnam('hermes'); print(json.dumps([p.pw_uid,p.pw_gid]))"))
if identity != [10000,10000]:raise ValueError('Canonical Hermes identity drift')
migration=Path(__file__).with_name('editorial-ownership.py').read_text()
migrate_code="import json,sys; p=json.load(sys.stdin); ns={'__name__':'ci_ownership'}; exec(compile(p['source'],'<ci-ownership>','exec'),ns); h=ns['Path'](p['home']); ns['migrate'](h,10000,10000,h/'.editorial-ownership-v1.json')"
docker('exec','--user','0:0','-i','canina-hermes','python3','-B','-c',migrate_code,data=json.dumps({'source':migration,'home':str(home)}))
source=Path(__file__).read_text()
payload=package(Path(__file__).resolve().parents[1]/'infra/hermes-editorial',sha)
code="import json,sys; p=json.load(sys.stdin); ns={'__name__':'editorial_stream'}; exec(compile(p['source'],'<ci-editorial>','exec'),ns); print(json.dumps(ns['install'](ns['Path'](p['home']),p['payload'])))"
+52
View File
@@ -0,0 +1,52 @@
import importlib.util
import os
from pathlib import Path
import tempfile
import unittest
spec=importlib.util.spec_from_file_location('migration',Path(__file__).with_name('editorial-ownership.py'))
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m)
@unittest.skipUnless(os.getuid()==0,'root fixture required')
class OwnershipTests(unittest.TestCase):
def test_migrate_twice_preserves_private_modes_and_rollback(self):
with tempfile.TemporaryDirectory() as t:
h=Path(t); d=h/'editorial-v4/credentials';d.mkdir(parents=True);d.chmod(0o700)
secret=d/'editorial-pmo-token';secret.write_text('fixture');secret.chmod(0o400)
c=h/'profiles/canina-competitor-worker/cache/editorial-runner';c.mkdir(parents=True);c.chmod(0o700)
outside=h/'config.yaml';outside.write_text('sentinel')
before={str(p.relative_to(h)):(p.stat().st_uid,p.stat().st_gid,p.stat().st_mode&0o777) for p in [d,d.parent,secret,c]}
journal=h/'ownership.json'
m.migrate(h,10000,10000,journal)
m.migrate(h,10000,10000,journal)
for p in [d,d.parent,secret,c]:self.assertEqual(p.stat().st_uid,10000)
self.assertEqual(secret.stat().st_mode&0o777,0o400)
self.assertEqual(d.stat().st_mode&0o777,0o700)
self.assertEqual(outside.stat().st_uid,0)
m.rollback(h,journal)
for p,expected in before.items():
s=(h/p).stat();self.assertEqual((s.st_uid,s.st_gid,s.st_mode&0o777),expected)
def test_preflight_refuses_unexpected_symlinks_world_readable_secret_without_changes(self):
for bad in ['symlink','public','unknown']:
with tempfile.TemporaryDirectory() as t:
h=Path(t);d=h/'editorial-v4/credentials';d.mkdir(parents=True);d.chmod(0o700)
p=d/'editorial-pmo-token';p.write_text('fixture');p.chmod(0o400)
if bad=='symlink':p.unlink();p.symlink_to(h/'elsewhere')
if bad=='public':p.chmod(0o644)
if bad=='unknown':(d/'other-token').write_text('fixture')
with self.assertRaises(ValueError):m.migrate(h,10000,10000,h/'journal')
self.assertEqual(d.stat().st_uid,0)
def test_partial_failure_rolls_back(self):
with tempfile.TemporaryDirectory() as t:
h=Path(t);d=h/'editorial-v4';d.mkdir();p=d/'deployment.json';p.write_text('{}');p.chmod(0o400)
from unittest.mock import patch
real=os.chown;calls=[]
def fail(path,uid,gid,**kw):
calls.append(path)
if len(calls)==2:raise OSError('fixture interruption')
return real(path,uid,gid,**kw)
with patch.object(m.os,'chown',side_effect=fail):
with self.assertRaises(OSError):m.migrate(h,10000,10000,h/'journal')
self.assertEqual(d.stat().st_uid,0);self.assertEqual(p.stat().st_uid,0)