Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: preserve initialized archive group ownership (#13485)
[._.]/e298f9862536
diff --git a/docs/features/branch-reconciliation.md b/docs/features/branch-reconciliation.mdindex 345d5f408a..0962f07864 100644--- a/docs/features/branch-reconciliation.md+++ b/docs/features/branch-reconciliation.md@@ -195,6 +195,14 @@ when the OS replaces it twice. These retained original bytes are recoveryevidence; the backend never installs them onto the copy. Missing attributes,changes to any other attribute, and unrecorded additions still refuse preservation.No provenance or quarantine attribute is removed or rewritten by this backend.+Ownership is checked during preview and again before copying. New copies retain+permitted UID/GID values without changing source metadata or caller credentials.+On Darwin, a uniform directory tree may retain a nonmember group through a+private empty directory created in a same-volume temporary parent with that+inherited group, then exclusively moved into the archive before copying.+Mixed unsupported ownership fails with `ownership-preservation-unavailable`+before creating an archive; use an executor already authorized to represent the+original ownership, never normalize the source to make verification pass.Normal retirement and rollback require exact original and quarantined metadata. Every laterarchive verification or move is bound to the exact manifest hash journaled afterpreservation. If that journal write is interrupted before any move, statusdiff --git a/scripts/lib/initialized_checkout_archive.py b/scripts/lib/initialized_checkout_archive.pyindex 2f4c43c7e0..5126819bee 100644--- a/scripts/lib/initialized_checkout_archive.py+++ b/scripts/lib/initialized_checkout_archive.py@@ -18,6 +18,7 @@ import shutilimport statimport subprocessimport sys+import tempfileimport uuidMAX_FILES = 200000@@ -406,9 +407,55 @@ def scan(request):return work, admin, common+def preflight_copy_ownership(rows):+ """Copies must represent original ownership without changing the caller."""+ uid = os.geteuid()+ groups = {os.getegid(), *os.getgroups()}+ unavailable = next((row for row in rows if uid != 0 and+ (row['uid'] != uid or row['gid'] not in groups)), None)+ if unavailable is None:+ return None+ # Darwin inherits a new directory's group from its parent, even when that+ # group is not in getgroups(). A private empty carrier created by this user+ # can retain that group across an exclusive same-volume rename. Permit only+ # uniform directory trees; mixed unsupported groups still refuse preflight.+ anchor = Path('/private/tmp')+ if (rows[0]['kind'] == 'directory' and all(row['uid'] == uid and row['gid'] == unavailable['gid'] for row in rows)+ and anchor.is_dir() and not anchor.is_symlink()+ and anchor.stat().st_gid == unavailable['gid']+ and anchor.stat().st_dev == rows[0]['filesystemIdentity']['dev']+ and os.access(anchor, os.W_OK | os.X_OK)):+ return anchor+ fail(f"ownership-preservation-unavailable:uid={unavailable['uid']}:gid={unavailable['gid']}; "+ 'use a preservation executor already authorized for the original ownership; '+ 'do not change source ownership')++def copy_tree(source, destination):if destination.exists() or destination.is_symlink():raise RuntimeError('Destination exists; never overwrite recovery evidence')+ paths = [source, *source.rglob('*')] if source.is_dir() else [source]+ rows = []+ for path in paths:+ info = path.lstat()+ rows.append({'uid': info.st_uid, 'gid': info.st_gid,+ 'kind': 'directory' if stat.S_ISDIR(info.st_mode) else 'other',+ 'filesystemIdentity': {'dev': info.st_dev}})+ anchor = preflight_copy_ownership(rows)+ if anchor:+ carrier = Path(tempfile.mkdtemp(prefix='portos-owned-archive-root-', dir=anchor))+ try:+ if carrier.stat().st_gid != rows[0]['gid'] or carrier.stat().st_uid != os.geteuid():+ fail('ownership-inheritance-changed')+ exclusive_rename(carrier, destination)+ except BaseException:+ # This is our still-empty private carrier, never source or archive.+ carrier.rmdir()+ raise+ copy_tree_contents(source, destination)+++def copy_tree_contents(source, destination):env = {k: v for k, v in os.environ.items() if not k.startswith('DITTO')}env['DITTOABORT'] = '1'command(['/usr/bin/ditto', '--rsrc', '--extattr', '--acl', '--qtn',@@ -444,9 +491,18 @@ def copy_tree(source, destination):for original in [source, *source.rglob('*')] if source.is_dir() else [source]:target = destination if original == source else destination / original.relative_to(source)original_stat = original.lstat()- # ditto creates symlinks with the current timestamp. Restore the link's- # own timestamps without resolving even a dangling dependency target.- if stat.S_ISLNK(original_stat.st_mode):+ copied_stat = target.lstat()+ if (original_stat.st_uid, original_stat.st_gid) != (copied_stat.st_uid, copied_stat.st_gid):+ # ditto cannot retain a nondefault group as an unprivileged caller.+ # Only NEW copies are changed, without following even dangling links.+ # chown may clear set-id bits; restore the original mode afterward.+ os.chown(target, original_stat.st_uid, original_stat.st_gid, follow_symlinks=False)+ if stat.S_IMODE(target.lstat().st_mode) != stat.S_IMODE(original_stat.st_mode):+ os.chmod(target, stat.S_IMODE(original_stat.st_mode), follow_symlinks=False)+ # ditto creates symlinks with the current timestamp and leaves a+ # pre-created root directory's timestamp changed by its child writes.+ # Restore only NEW-copy metadata; never resolve a dependency link.+ if stat.S_ISLNK(original_stat.st_mode) or stat.S_ISDIR(original_stat.st_mode):os.utime(target, ns=(original_stat.st_atime_ns, original_stat.st_mtime_ns),follow_symlinks=False)expected_flags = original_stat.st_flags@@ -512,6 +568,9 @@ def inspect(request, full=False):fail('unregistered-private-module')sources = {'working': manifest(work), 'administration': manifest(admin, True),'common': {name: manifest(common / name, True) for name in COMMON_PARTS if (common / name).exists()}}+ if request.get('requireOwnership') is True:+ for rows in [sources['working'], sources['administration'], *sources['common'].values()]:+ preflight_copy_ownership(rows)facts = {'identity': {'working': identity(work), 'administration': identity(admin), 'common': identity(common)},'privateGitDir': str(admin), 'commonGitDir': str(common), 'modules': modules, 'parent': parent,'repoPath': request['repoPath'], 'release': release}@@ -610,6 +669,8 @@ def preserve(request):fail('quarantine-cross-device')rows = before['sources']['working'] + before['sources']['administration']rows += [r for section in before['sources']['common'].values() for r in section]+ for section in [before['sources']['working'], before['sources']['administration'], *before['sources']['common'].values()]:+ preflight_copy_ownership(section)size = sum(r.get('bytes', 0) for r in rows)if size > MAX_BYTES or shutil.disk_usage(root).free < size * 2 + 1024 ** 3:fail('insufficient-preservation-space')diff --git a/scripts/lib/initialized_checkout_archive_test.py b/scripts/lib/initialized_checkout_archive_test.pyindex cac1f0b711..bf64373c41 100644--- a/scripts/lib/initialized_checkout_archive_test.py+++ b/scripts/lib/initialized_checkout_archive_test.py@@ -128,6 +128,69 @@ class NativeArchive(unittest.TestCase):(admission / 'writer.lock').write_text('owned service bookkeeping')self.assertEqual(archive.inspect(self.request), before)+ def test_preservation_retains_permitted_nondefault_group_and_rejects_tampering(self):+ groups = set(os.getgroups()) - {os.getegid()}+ if not groups:+ self.skipTest('A permitted nondefault group is required')+ gid = min(groups)+ (self.work / 'group-directory').mkdir()+ (self.work / 'group-directory' / 'file').write_text('preserve group bytes\n')+ (self.work / 'group-link').symlink_to('group-directory/file')+ paths = [self.work, self.work / 'group-directory', self.work / 'group-directory' / 'file', self.work / 'group-link']+ for path in paths:+ os.chown(path, -1, gid, follow_symlinks=False)+ self.preserve()+ saved = json.loads((self.txn / 'source.json').read_text())+ for prefix in ('archive', 'restore'):+ for original in paths:+ copied = self.txn / prefix / 'working' / original.relative_to(self.work)+ self.assertEqual(copied.lstat().st_gid, gid)+ self.assertEqual(copied.lstat().st_uid, original.lstat().st_uid)+ self.assertEqual(archive.manifest(self.work), saved['sources']['working'])+ archive.verify({**self.request, 'mode': 'standalone'})+ os.chown(self.txn / 'archive' / 'working' / 'group-directory' / 'file', -1, os.getegid())+ with self.assertRaises(RuntimeError):+ archive.verify({**self.request, 'mode': 'standalone'})++ def test_unrepresentable_ownership_refuses_review_and_copy_before_archive_creation(self):+ before = archive.inspect(self.request)+ self.request['expected'] = before+ with patch.object(archive.os, 'getgroups', return_value=[]), patch.object(archive.os, 'getegid', return_value=-1):+ with self.assertRaisesRegex(RuntimeError, 'ownership-preservation-unavailable'):+ archive.inspect({**self.request, 'requireOwnership': True})+ with self.assertRaisesRegex(RuntimeError, 'ownership-preservation-unavailable'):+ archive.preserve(self.request)+ self.assertEqual(archive.inspect(self.request), before)+ for name in ('archive', 'restore', 'quarantine', 'source.json', 'manifest.json'):+ self.assertFalse((self.txn / name).exists())++ def test_uniform_inherited_group_preserves_without_membership_or_chown_authority(self):+ group = Path('/private/tmp').stat().st_gid+ if group in {os.getegid(), *os.getgroups()} or os.geteuid() == 0:+ self.skipTest('Requires the Darwin inherited temporary group outside the caller groups')+ work = Path(tempfile.mkdtemp(prefix='initialized-inherited-group-', dir='/private/tmp'))+ try:+ self.git(self.repo, 'worktree', 'add', '-b', 'feature/inherited', str(work))+ self.git(work, '-c', 'protocol.file.allow=always', 'submodule', 'update', '--init')+ (work / 'ignored-evidence').write_text('recoverable bytes\n')+ (work / 'link').symlink_to('ignored-evidence')+ os.link(work / 'ignored-evidence', work / 'hardlink')+ self.request['release'] = {'path': str(work), 'head': self.git(work, 'rev-parse', 'HEAD'),+ 'branch': 'feature/inherited', 'locked': False, 'lockReason': None}+ before = archive.manifest(work)+ self.assertTrue(all(row['gid'] == group for row in before))+ # Any attempt to repair this unsupported group with chown fails;+ # preservation must instead keep ordinary inherited ownership.+ with patch.object(archive.os, 'chown', side_effect=PermissionError('not authorized')):+ self.preserve()+ for prefix in ('archive', 'restore'):+ copied = archive.manifest(self.txn / prefix / 'working')+ self.assertTrue(all(row['gid'] == group for row in copied))+ self.assertEqual(archive.manifest(work), before)+ self.assertTrue(archive.verify({**self.request, 'mode': 'standalone'})['standalone'])+ finally:+ shutil.rmtree(work)+def test_unbound_cancellation_accepts_shared_updates_without_touching_originals_or_evidence(self):before = archive.inspect(self.request, full=True)self.request['expected'] = {k: v for k, v in before.items() if k != 'sources'}diff --git a/scripts/pregate-attest.test.js b/scripts/pregate-attest.test.jsindex 8e93cb8a80..0cc32602a5 100644--- a/scripts/pregate-attest.test.js+++ b/scripts/pregate-attest.test.js@@ -296,10 +296,14 @@ describe('runAttest', () => {git('-C', cwd, 'commit', '-m', text);return git('-C', cwd, 'rev-parse', 'HEAD');};- commit('initial');+ const initial = commit('initial');git('-C', cwd, 'push', '-u', 'origin', 'main');const published = commit('published');git('-C', cwd, 'push', remote, 'main');+ // Git for Windows may recognize this path as the configured origin and+ // update its tracking ref. Model stale tracking explicitly on every host;+ // publication must still be proved from the advertised remote, not refs.+ git('-C', cwd, 'update-ref', 'refs/remotes/origin/main', initial);expect(git('-C', cwd, 'branch', '-r', '--contains', published)).toBe('');const before = git('-C', cwd, 'show-ref');const calls = [];diff --git a/server/services/initializedCheckoutRecovery.js b/server/services/initializedCheckoutRecovery.jsindex d0cdc90ca8..47e4ee12e9 100644--- a/server/services/initializedCheckoutRecovery.js+++ b/server/services/initializedCheckoutRecovery.js@@ -203,7 +203,7 @@ export function _createInitializedCheckoutRecovery(deps = {}) {const makeReview = async ({ appId, release, prUrl }) => {const ctx = await context(appId, release);const policy = await ensureOwner(ctx, release);- const snapshot = await native({ command: 'inspect', repoPath: ctx.repoPath, release });+ const snapshot = await native({ command: 'inspect', repoPath: ctx.repoPath, release, requireOwnership: true });const facts = await forge(ctx, release, prUrl);const base = await comparison(ctx, release, prUrl, snapshot, policy, facts);const checkoutIdentity = await readRetirementCheckoutIdentity(release.path);diff --git a/server/services/initializedCheckoutRecovery.test.js b/server/services/initializedCheckoutRecovery.test.jsindex f9060498c5..c86309166e 100644--- a/server/services/initializedCheckoutRecovery.test.js+++ b/server/services/initializedCheckoutRecovery.test.js@@ -94,6 +94,7 @@ beforeEach(async () => {if (input.command === 'lock') return { assertHeld: () => {}, release: async () => {} };if (input.command === 'inspect') {if (fixture.inspectRefusal) throw new Error(fixture.inspectRefusal);+ if (fixture.ownershipRefusal && input.requireOwnership) throw new Error(fixture.ownershipRefusal);return { ...fixture.snapshot };}if (input.command === 'verify-original-placement') {@@ -200,6 +201,15 @@ const reviewed = async () => {};describe('initialized checkout transaction', () => {+ it('refuses unrepresentable ownership during preview before reserving or archiving', async () => {+ fixture.ownershipRefusal = 'ownership-preservation-unavailable:uid=501:gid=0';+ expect(await fixture.service.review({ appId: 'app', release, prUrl })).toEqual({+ ok: false, reason: fixture.ownershipRefusal,+ });+ expect(fixture.cut).toBeNull();+ expect(await readdir(root)).toEqual([]);+ });+it('uses actual Windows filesystem capabilities for mocked workflows without suppressing file fsync', async () => {persisted.hostType = 'Windows_NT'; persisted.syntheticModes = true; persisted.rejectDirectoryOpen = true;const restorePlatform = pinPlatform('darwin');