-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatlas_absorb.hexa
More file actions
190 lines (174 loc) · 8.38 KB
/
atlas_absorb.hexa
File metadata and controls
190 lines (174 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// ══════════════════════════════════════════════════════════════
// nexus/n6/atlas_absorb.hexa — atlas auto-absorption module
// Centralized unlock-append-relock cycle for chflags-locked atlas.n6
//
// @resolver-bypass(reason="darwin-native-chflags-+-local-fs-absorption")
// (raw 82 marker — auto-bypass for darwin-native chflags(2) syscalls;
// per-file unlock/lock requires direct chflags rather than os_level_lock.hexa
// which operates on a fixed SSOT target set, ignoring per-path arguments.)
//
// Procedure:
// 1. unlock atlas.n6 directly: chflags nouchg <target_atlas>
// (replaces earlier os_level_lock.hexa indirection — that tool's
// cmd_unlock() ignores per-file arg and operates on _required_targets()
// only, which silently no-op'd absorption against locked file.)
// 2. append shard contents with separator markers
// 3. (optional) archive absorbed shards to _absorbed/<ts>/
// 4. relock atlas.n6 directly: chflags uchg <target_atlas>
//
// Idempotent: shards already absorbed (same basename in atlas.n6) are skipped.
//
// Invocation (from nexus CLI or directly):
// HEXA_RESOLVER_NO_REROUTE=1 hexa run nexus/n6/atlas_absorb.hexa <target_root> [--glob <pat>] [--archive] [--dry-run]
// target_root: absolute path to repo root (e.g., /Users/ghost/core/nexus)
//
// Layout auto-detection (2026-05-08):
// <target_root>/n6/atlas.n6 → uses subdir "n6/" (nexus-native, primary SSOT)
// <target_root>/atlas/atlas.n6 → uses subdir "atlas/" (legacy canon-style)
// When both exist, n6/ wins (nexus self-absorb).
//
// User directive 2026-04-28: "모듈을 별도 만들어서 해주고 + 모듈은 lock"
// 2026-04-28 fix: replace os_level_lock.hexa per-file unlock (silent no-op)
// with direct chflags nouchg/uchg — discovered after iter-70 absorption no-op.
// File chflags uchg-locked after authoring.
// ══════════════════════════════════════════════════════════════
let _args = args()
fn _shell_quote(s: str) -> str {
return "'" + s + "'"
}
fn _has_arg(name: str) -> bool {
let mut i = 0
while i < _args.len() {
if _args[i] == name { return true }
i = i + 1
}
return false
}
fn _flag_value(name: str, default_val: str) -> str {
let mut i = 0
while i < _args.len() - 1 {
if _args[i] == name { return _args[i + 1] }
i = i + 1
}
return default_val
}
fn _detect_subdir(root: str) -> str {
// 2026-05-08: nexus-native uses n6/, legacy canon uses atlas/. Prefer n6/.
let has_n6 = exec("test -f " + _shell_quote(root) + "/n6/atlas.n6 && echo 1 || echo 0").trim()
if has_n6 == "1" { return "n6" }
let has_atlas = exec("test -f " + _shell_quote(root) + "/atlas/atlas.n6 && echo 1 || echo 0").trim()
if has_atlas == "1" { return "atlas" }
return ""
}
fn run_absorb() -> i64 {
if _args.len() < 3 {
println("[atlas_absorb] usage: hexa run atlas_absorb.hexa <target_root> [--glob <pat>] [--archive] [--dry-run]")
println(" target_root: absolute path to repo root containing n6/atlas.n6 (or legacy atlas/atlas.n6)")
println(" --glob <pat>: shard glob (default: <subdir>/atlas.append.*.n6)")
println(" --archive: move absorbed shards to <subdir>/_absorbed/<timestamp>/")
println(" --dry-run: preview without modifying atlas.n6")
return 2
}
// iter-71 fix: position-independent target_root scan (hetzner remote shifts args)
// 2026-05-08: scan accepts both n6/ and atlas/ subdir layouts.
let mut target_root = ""
let mut _ti = 2
while _ti < _args.len() {
let cand = _args[_ti]
if !cand.starts_with("--") {
let is_hexa = cand.ends_with(".hexa")
if !is_hexa {
let sd = _detect_subdir(cand)
if sd.len() > 0 {
target_root = cand
_ti = _args.len() // break
}
}
}
_ti = _ti + 1
}
if target_root.len() == 0 {
// legacy fallback
target_root = if _args.len() >= 3 { _args[2] } else { "" }
}
let subdir = _detect_subdir(target_root)
if subdir.len() == 0 {
println("[atlas_absorb] ERROR: neither n6/atlas.n6 nor atlas/atlas.n6 found under " + target_root)
return 3
}
let glob = _flag_value("--glob", subdir + "/atlas.append.*.n6")
let archive = _has_arg("--archive")
let dry_run = _has_arg("--dry-run")
let target_atlas = target_root + "/" + subdir + "/atlas.n6"
// Verify target exists (sanity — should be guaranteed by _detect_subdir)
let exists = exec("test -f " + _shell_quote(target_atlas) + " && echo 1 || echo 0").trim()
if exists != "1" {
println("[atlas_absorb] ERROR: atlas.n6 not found at " + target_atlas)
return 3
}
// Discover shards
let shard_list_raw = exec("cd " + _shell_quote(target_root) + " && ls " + glob + " 2>/dev/null | sort")
if shard_list_raw.len() == 0 {
println("[atlas_absorb] no shards matched glob='" + glob + "' under " + target_root)
return 0
}
let shard_count = exec("echo " + _shell_quote(shard_list_raw) + " | wc -l").trim()
println("[atlas_absorb] target=" + target_atlas)
println(" shards: " + shard_count + " files matched")
if dry_run {
println(" --dry-run: preview only (no atlas.n6 modification)")
print(shard_list_raw)
return 0
}
// Idempotency check
let pre_check = exec("grep -c '^# .. atlas.append' " + _shell_quote(target_atlas) + " 2>/dev/null || echo 0").trim()
println(" atlas.n6 pre-existing absorbed-shard markers: " + pre_check)
// Unlock — direct chflags (raw 82 darwin-native bypass).
// Earlier indirection via os_level_lock.hexa ignored per-file arg
// (it operates on _required_targets() SSOT only) → silent no-op.
//
// Cross-OS rationale (raw 91 C3 honesty):
// chflags is darwin/BSD-only. On Linux (hetzner) the binary doesn't
// exist → `sh: 1: chflags: not found` noise. Linux uses chattr +i
// for immutability, but on hetzner this module is currently NOT
// chattr-locked, so the unlock/relock is a no-op there. We detect
// `uname -s` and skip with an explicit message rather than emit a
// silent error (which violates the no-silent-failure principle).
let os_kind = exec("uname -s").trim()
if os_kind == "Darwin" {
println(" unlocking atlas.n6 (chflags nouchg)...")
let unlock_out = exec("chflags nouchg " + _shell_quote(target_atlas) + " 2>&1")
print(unlock_out)
} else {
println(" unlocking atlas.n6 (Linux: chflags-equivalent skipped — file not immutable on this OS)")
}
// Append separator + each shard
let pre_cmd = "echo " + _shell_quote(absorb_marker) + " >> " + _shell_quote(target_atlas)
exec(pre_cmd)
let absorb_cmd = "cd " + _shell_quote(target_root) + " && for f in $(ls " + glob + " 2>/dev/null | sort); do echo; echo \"# .. $(basename $f) ..\"; cat \"$f\"; done >> " + _shell_quote(target_atlas)
let absorb_out = exec(absorb_cmd + " 2>&1")
print(absorb_out)
let post_lines = exec("wc -l " + _shell_quote(target_atlas) + " | awk '{print $1}'").trim()
println(" atlas.n6 post-absorb line count: " + post_lines)
// Optional archive
if archive {
let ts = exec("date +%Y%m%d-%H%M%S").trim()
let archive_dir = target_root + "/" + subdir + "/_absorbed/" + ts
exec("mkdir -p " + _shell_quote(archive_dir))
exec("cd " + _shell_quote(target_root) + " && mv " + glob + " " + _shell_quote(archive_dir) + "/ 2>&1")
println(" shards archived to: " + archive_dir)
}
// Relock — direct chflags (raw 82 darwin-native bypass).
// OS detection mirrors the unlock step above (see cross-OS rationale).
if os_kind == "Darwin" {
println(" re-locking atlas.n6 (chflags uchg)...")
let lock_out = exec("chflags uchg " + _shell_quote(target_atlas) + " 2>&1")
print(lock_out)
} else {
println(" re-locking atlas.n6 (Linux: chflags-equivalent skipped — file not immutable on this OS)")
}
println("[atlas_absorb] complete (" + shard_count + " shards absorbed into " + target_atlas + ")")
return 0
}
let rc = run_absorb()
exit(rc)