djournal 0.5.1 → 0.7.0
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/.agents/adapters/contract.md +4 -3
- package/.agents/adapters/shared/journal-hook.js +81 -6
- package/.agents/skills/init-work/SKILL.md +12 -9
- package/.agents/skills/switch/SKILL.md +11 -8
- package/.claude/settings.json +3 -3
- package/.codex/hooks.json +3 -3
- package/README.md +14 -4
- package/bin/journal.js +55 -2
- package/docs/architecture.md +10 -3
- package/docs/djournal-in-practice.md +14 -0
- package/docs/installation.md +51 -6
- package/docs/read-only-team-journal.md +150 -0
- package/docs/remote-sync.md +49 -10
- package/docs/visibility-and-sharing.md +12 -4
- package/lib/installer/index.js +353 -10
- package/lib/update-checker.js +177 -0
- package/package.json +5 -2
package/lib/installer/index.js
CHANGED
|
@@ -14,6 +14,13 @@ const {
|
|
|
14
14
|
removeManagedBlock,
|
|
15
15
|
upsertManagedBlock,
|
|
16
16
|
} = require("./merge.js");
|
|
17
|
+
const {
|
|
18
|
+
cacheIsStale,
|
|
19
|
+
deriveUpdateState,
|
|
20
|
+
passiveChecksEnabled,
|
|
21
|
+
readCache: readUpdateCache,
|
|
22
|
+
refreshCache: refreshUpdateCache,
|
|
23
|
+
} = require("../update-checker.js");
|
|
17
24
|
|
|
18
25
|
const SCHEMA_VERSION = 1;
|
|
19
26
|
const SUPPORTED_HARNESSES = ["codex", "claude-code", "pi"];
|
|
@@ -167,6 +174,10 @@ function defaultProjectConfig(target, store) {
|
|
|
167
174
|
default: "local_only",
|
|
168
175
|
sharedWorkItems: {},
|
|
169
176
|
},
|
|
177
|
+
updates: {
|
|
178
|
+
enabled: true,
|
|
179
|
+
intervalHours: 24,
|
|
180
|
+
},
|
|
170
181
|
};
|
|
171
182
|
}
|
|
172
183
|
|
|
@@ -255,6 +266,10 @@ function packageVersion(sourceRoot) {
|
|
|
255
266
|
return parseJson(fs.readFileSync(path.join(sourceRoot, "package.json")), "package.json").version;
|
|
256
267
|
}
|
|
257
268
|
|
|
269
|
+
function packageSourceRoot(options = {}) {
|
|
270
|
+
return path.resolve(options.sourceRoot || path.join(__dirname, "../.."));
|
|
271
|
+
}
|
|
272
|
+
|
|
258
273
|
function commandExists(command, env = process.env) {
|
|
259
274
|
const pathValue = env.PATH || "";
|
|
260
275
|
const extensions = process.platform === "win32"
|
|
@@ -414,7 +429,7 @@ function markdownRecord(target, relative, body, oldRecord, harness) {
|
|
|
414
429
|
|
|
415
430
|
function journalAccessFragment(harness, store) {
|
|
416
431
|
if (harness !== "claude-code") return {};
|
|
417
|
-
const commands = ["status", "doctor", "config", "share", "sync"];
|
|
432
|
+
const commands = ["status", "doctor", "config", "share", "sync", "update"];
|
|
418
433
|
return {
|
|
419
434
|
permissions: {
|
|
420
435
|
additionalDirectories: [store.root],
|
|
@@ -753,6 +768,278 @@ function projectJournalRoot(projectionRoot) {
|
|
|
753
768
|
return path.join(projectionRoot, ".journal");
|
|
754
769
|
}
|
|
755
770
|
|
|
771
|
+
function syncBaselineWorkRoot(context, slug) {
|
|
772
|
+
return path.join(context.root, ".sync", "base", "work", slug);
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
function readBuffer(file) {
|
|
776
|
+
return readOptional(file);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
function buffersEqual(left, right) {
|
|
780
|
+
if (left === null || right === null) return left === right;
|
|
781
|
+
return left.equals(right);
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
function durableWorkSlugs(journalRoot) {
|
|
785
|
+
const workRoot = path.join(journalRoot, "work");
|
|
786
|
+
if (!fs.existsSync(workRoot)) return [];
|
|
787
|
+
return fs.readdirSync(workRoot, { withFileTypes: true })
|
|
788
|
+
.filter((entry) => entry.isDirectory() && !entry.isSymbolicLink())
|
|
789
|
+
.map((entry) => entry.name)
|
|
790
|
+
.filter((slug) => !slug.includes("/") && !slug.includes("\\") &&
|
|
791
|
+
fs.existsSync(path.join(workRoot, slug, "work.md")))
|
|
792
|
+
.sort();
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function mergeJournalText(local, base, remote, options = {}) {
|
|
796
|
+
if ([local, base, remote].some((value) => value.includes(0))) {
|
|
797
|
+
return { ok: false, message: "binary content cannot be merged automatically" };
|
|
798
|
+
}
|
|
799
|
+
const lines = (value) => value.toString("utf8").match(/[^\n]*\n|[^\n]+$/g) || [];
|
|
800
|
+
const localLines = lines(local);
|
|
801
|
+
const baseLines = lines(base);
|
|
802
|
+
const remoteLines = lines(remote);
|
|
803
|
+
if (localLines.length === baseLines.length && remoteLines.length === baseLines.length) {
|
|
804
|
+
const merged = [];
|
|
805
|
+
for (let index = 0; index < baseLines.length; index += 1) {
|
|
806
|
+
if (localLines[index] === remoteLines[index]) merged.push(localLines[index]);
|
|
807
|
+
else if (localLines[index] === baseLines[index]) merged.push(remoteLines[index]);
|
|
808
|
+
else if (remoteLines[index] === baseLines[index]) merged.push(localLines[index]);
|
|
809
|
+
else return { ok: false, message: "overlapping edits" };
|
|
810
|
+
}
|
|
811
|
+
return { ok: true, content: Buffer.from(merged.join("")) };
|
|
812
|
+
}
|
|
813
|
+
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "djournal-merge-"));
|
|
814
|
+
const localFile = path.join(temporary, "local");
|
|
815
|
+
const baseFile = path.join(temporary, "base");
|
|
816
|
+
const remoteFile = path.join(temporary, "remote");
|
|
817
|
+
try {
|
|
818
|
+
fs.writeFileSync(localFile, local);
|
|
819
|
+
fs.writeFileSync(baseFile, base);
|
|
820
|
+
fs.writeFileSync(remoteFile, remote);
|
|
821
|
+
const result = (options.mergeRunner || spawnSync)("git", ["merge-file", "-p", "--", localFile, baseFile, remoteFile], {
|
|
822
|
+
encoding: null,
|
|
823
|
+
timeout: options.timeout || 30000,
|
|
824
|
+
});
|
|
825
|
+
if (result.error) return { ok: false, message: result.error.message };
|
|
826
|
+
if (result.status !== 0) {
|
|
827
|
+
return { ok: false, message: result.status === 1 ? "overlapping edits" : "git merge-file failed" };
|
|
828
|
+
}
|
|
829
|
+
return { ok: true, content: Buffer.from(result.stdout || "") };
|
|
830
|
+
} finally {
|
|
831
|
+
fs.rmSync(temporary, { recursive: true, force: true });
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function reconcileWork(context, projectionJournal, slug, options = {}) {
|
|
836
|
+
const canonicalRoot = path.join(context.journalRoot, "work", slug);
|
|
837
|
+
const remoteRoot = path.join(projectionJournal, "work", slug);
|
|
838
|
+
const baselineRoot = syncBaselineWorkRoot(context, slug);
|
|
839
|
+
const comparisonBaseRoot = fs.existsSync(baselineRoot)
|
|
840
|
+
? baselineRoot
|
|
841
|
+
: path.join(options.fallbackBaselineJournal || projectionJournal, "work", slug);
|
|
842
|
+
const relativeFiles = [...new Set([
|
|
843
|
+
...listFiles(canonicalRoot),
|
|
844
|
+
...listFiles(remoteRoot),
|
|
845
|
+
...listFiles(comparisonBaseRoot),
|
|
846
|
+
])].sort();
|
|
847
|
+
const changes = [];
|
|
848
|
+
const files = [];
|
|
849
|
+
const conflicts = [];
|
|
850
|
+
|
|
851
|
+
for (const relative of relativeFiles) {
|
|
852
|
+
const canonicalFile = path.join(canonicalRoot, relative);
|
|
853
|
+
const remoteFile = path.join(remoteRoot, relative);
|
|
854
|
+
const baselineFile = path.join(comparisonBaseRoot, relative);
|
|
855
|
+
const local = readBuffer(canonicalFile);
|
|
856
|
+
const remote = readBuffer(remoteFile);
|
|
857
|
+
const base = readBuffer(baselineFile);
|
|
858
|
+
const logicalPath = `.journal/work/${slug}/${relative}`;
|
|
859
|
+
|
|
860
|
+
if (buffersEqual(local, remote)) {
|
|
861
|
+
files.push({ path: logicalPath, status: "unchanged" });
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
if (base !== null && remote === null) {
|
|
865
|
+
if (local === null) {
|
|
866
|
+
files.push({ path: logicalPath, status: "unchanged" });
|
|
867
|
+
} else {
|
|
868
|
+
conflicts.push(`${logicalPath}: remote deletion requires manual review`);
|
|
869
|
+
}
|
|
870
|
+
continue;
|
|
871
|
+
}
|
|
872
|
+
if (local === null && remote !== null) {
|
|
873
|
+
changes.push({ file: canonicalFile, content: remote });
|
|
874
|
+
files.push({ path: logicalPath, status: "imported" });
|
|
875
|
+
continue;
|
|
876
|
+
}
|
|
877
|
+
if (remote === null) {
|
|
878
|
+
files.push({ path: logicalPath, status: "local" });
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
if (base !== null && buffersEqual(local, base)) {
|
|
882
|
+
changes.push({ file: canonicalFile, content: remote });
|
|
883
|
+
files.push({ path: logicalPath, status: "imported" });
|
|
884
|
+
continue;
|
|
885
|
+
}
|
|
886
|
+
if (base !== null && buffersEqual(remote, base)) {
|
|
887
|
+
files.push({ path: logicalPath, status: "local" });
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
890
|
+
if (base === null) {
|
|
891
|
+
conflicts.push(`${logicalPath}: divergent content has no synchronization baseline`);
|
|
892
|
+
continue;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
const merged = mergeJournalText(local, base, remote, options);
|
|
896
|
+
if (!merged.ok) {
|
|
897
|
+
conflicts.push(`${logicalPath}: ${merged.message}`);
|
|
898
|
+
continue;
|
|
899
|
+
}
|
|
900
|
+
changes.push({ file: canonicalFile, content: merged.content });
|
|
901
|
+
files.push({ path: logicalPath, status: "merged" });
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
return { slug, canonicalRoot, remoteRoot, baselineRoot, changes, files, conflicts };
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
function applyCanonicalChanges(reconciliations) {
|
|
908
|
+
const written = [];
|
|
909
|
+
try {
|
|
910
|
+
for (const reconciliation of reconciliations) {
|
|
911
|
+
for (const change of reconciliation.changes) {
|
|
912
|
+
const previous = readOptional(change.file);
|
|
913
|
+
fs.mkdirSync(path.dirname(change.file), { recursive: true });
|
|
914
|
+
atomicWrite(change.file, change.content);
|
|
915
|
+
written.push({ file: change.file, previous });
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
} catch (error) {
|
|
919
|
+
for (const item of written.reverse()) {
|
|
920
|
+
if (item.previous === null) fs.rmSync(item.file, { force: true });
|
|
921
|
+
else atomicWrite(item.file, item.previous);
|
|
922
|
+
}
|
|
923
|
+
throw error;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
function replaceBaseline(source, destination) {
|
|
928
|
+
const parent = path.dirname(destination);
|
|
929
|
+
fs.mkdirSync(parent, { recursive: true });
|
|
930
|
+
const staged = fs.mkdtempSync(path.join(parent, ".baseline-"));
|
|
931
|
+
const backup = `${destination}.previous-${process.pid}-${Date.now()}`;
|
|
932
|
+
try {
|
|
933
|
+
copyDirectoryContents(source, staged);
|
|
934
|
+
if (fs.existsSync(destination)) fs.renameSync(destination, backup);
|
|
935
|
+
fs.renameSync(staged, destination);
|
|
936
|
+
if (fs.existsSync(backup)) fs.rmSync(backup, { recursive: true, force: true });
|
|
937
|
+
} catch (error) {
|
|
938
|
+
if (fs.existsSync(staged)) fs.rmSync(staged, { recursive: true, force: true });
|
|
939
|
+
if (fs.existsSync(backup) && !fs.existsSync(destination)) fs.renameSync(backup, destination);
|
|
940
|
+
throw error;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
function pullWithContext(context, config, options = {}) {
|
|
945
|
+
const syncOptions = config.sync && typeof config.sync === "object" ? config.sync : {};
|
|
946
|
+
const target = context.target;
|
|
947
|
+
if (syncOptions.enabled !== true) {
|
|
948
|
+
return { action: "pull", target, skipped: true, reason: "sync is not enabled" };
|
|
949
|
+
}
|
|
950
|
+
if (!["colocated", "standalone"].includes(syncOptions.mode)) {
|
|
951
|
+
return { action: "pull", target, skipped: true, reason: "sync mode is not supported" };
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
const projectionRoot = projectionRootFor(target, config);
|
|
955
|
+
const projectionJournal = projectJournalRoot(projectionRoot);
|
|
956
|
+
if (!fs.existsSync(projectionJournal)) {
|
|
957
|
+
return { action: "pull", target, skipped: true, reason: "journal projection does not exist" };
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
let pulled = false;
|
|
961
|
+
let gitRoot;
|
|
962
|
+
let fallbackBaselineJournal = projectionJournal;
|
|
963
|
+
let projectionSnapshot;
|
|
964
|
+
if (syncOptions.mode === "standalone") {
|
|
965
|
+
gitRoot = gitRootFor(projectionJournal, options);
|
|
966
|
+
const pathspec = journalPathspec(gitRoot, path.join(projectionJournal, "work"));
|
|
967
|
+
const unmerged = runGit(gitRoot, ["diff", "--name-only", "--diff-filter=U", "--", pathspec], options);
|
|
968
|
+
if (unmerged) throw new InstallerError(`unresolved journal conflicts:\n${unmerged}`, "SYNC_CONFLICT");
|
|
969
|
+
const dirty = runGit(gitRoot, ["status", "--porcelain", "--untracked-files=all", "--", pathspec], options);
|
|
970
|
+
if (dirty) throw new InstallerError(`journal projection has uncommitted changes:\n${dirty}`, "SYNC_CONFLICT");
|
|
971
|
+
if (!options.dryRun) {
|
|
972
|
+
projectionSnapshot = fs.mkdtempSync(path.join(os.tmpdir(), "djournal-pull-base-"));
|
|
973
|
+
copyDirectoryContents(projectionJournal, projectionSnapshot);
|
|
974
|
+
fallbackBaselineJournal = projectionSnapshot;
|
|
975
|
+
try {
|
|
976
|
+
runGit(gitRoot, ["pull", "--ff-only"], options);
|
|
977
|
+
pulled = true;
|
|
978
|
+
} catch (error) {
|
|
979
|
+
fs.rmSync(projectionSnapshot, { recursive: true, force: true });
|
|
980
|
+
throw error;
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
try {
|
|
986
|
+
const available = durableWorkSlugs(projectionJournal);
|
|
987
|
+
const slugs = options.work ? available.filter((slug) => slug === options.work) : available;
|
|
988
|
+
if (!slugs.length) {
|
|
989
|
+
return {
|
|
990
|
+
action: "pull",
|
|
991
|
+
target,
|
|
992
|
+
mode: syncOptions.mode,
|
|
993
|
+
work: options.work,
|
|
994
|
+
skipped: true,
|
|
995
|
+
reason: options.work ? "work item is not present in the journal projection" : "no projected work items found",
|
|
996
|
+
pulled,
|
|
997
|
+
};
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
const reconcileOptions = { ...options, fallbackBaselineJournal };
|
|
1001
|
+
const reconciliations = slugs.map((slug) => reconcileWork(context, projectionJournal, slug, reconcileOptions));
|
|
1002
|
+
const conflicts = reconciliations.flatMap((item) => item.conflicts);
|
|
1003
|
+
const files = reconciliations.flatMap((item) => item.files);
|
|
1004
|
+
if (conflicts.length || options.dryRun) {
|
|
1005
|
+
return {
|
|
1006
|
+
action: "pull",
|
|
1007
|
+
target,
|
|
1008
|
+
mode: syncOptions.mode,
|
|
1009
|
+
workItems: slugs,
|
|
1010
|
+
files,
|
|
1011
|
+
conflicts,
|
|
1012
|
+
pulled,
|
|
1013
|
+
dryRun: !!options.dryRun,
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
applyCanonicalChanges(reconciliations);
|
|
1018
|
+
for (const reconciliation of reconciliations) {
|
|
1019
|
+
replaceBaseline(reconciliation.remoteRoot, reconciliation.baselineRoot);
|
|
1020
|
+
}
|
|
1021
|
+
return {
|
|
1022
|
+
action: "pull",
|
|
1023
|
+
target,
|
|
1024
|
+
mode: syncOptions.mode,
|
|
1025
|
+
workItems: slugs,
|
|
1026
|
+
files,
|
|
1027
|
+
conflicts: [],
|
|
1028
|
+
pulled,
|
|
1029
|
+
dryRun: false,
|
|
1030
|
+
};
|
|
1031
|
+
} finally {
|
|
1032
|
+
if (projectionSnapshot) fs.rmSync(projectionSnapshot, { recursive: true, force: true });
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
function pull(options) {
|
|
1037
|
+
const target = checkTarget(options.target || process.cwd());
|
|
1038
|
+
const context = projectContext(target, options);
|
|
1039
|
+
const config = readProjectConfig(context);
|
|
1040
|
+
return pullWithContext(context, config, options);
|
|
1041
|
+
}
|
|
1042
|
+
|
|
756
1043
|
function projectSharedWork(context, config, work, options = {}) {
|
|
757
1044
|
const targetRoot = projectionRootFor(context.target, config);
|
|
758
1045
|
const destinationJournal = projectJournalRoot(targetRoot);
|
|
@@ -799,23 +1086,28 @@ function sync(options) {
|
|
|
799
1086
|
if (!["colocated", "standalone"].includes(syncOptions.mode)) {
|
|
800
1087
|
return { action: "sync", target, skipped: true, reason: "sync mode is not standalone" };
|
|
801
1088
|
}
|
|
1089
|
+
const pulled = pullWithContext(context, config, options);
|
|
1090
|
+
if (pulled.conflicts?.length) {
|
|
1091
|
+
throw new InstallerError(`unresolved journal conflicts:\n${pulled.conflicts.join("\n")}`, "SYNC_CONFLICT");
|
|
1092
|
+
}
|
|
802
1093
|
const work = readActiveWork(context, options.work);
|
|
803
1094
|
if (!isWorkShared(config, work.slug)) {
|
|
804
1095
|
return { action: "sync", target, work: work.slug, skipped: true, reason: "work is not shared" };
|
|
805
1096
|
}
|
|
806
1097
|
const projection = projectSharedWork(context, config, work, options);
|
|
807
1098
|
if (syncOptions.mode === "colocated") {
|
|
808
|
-
|
|
1099
|
+
if (!options.dryRun) replaceBaseline(
|
|
1100
|
+
path.join(projection.journalRoot, "work", work.slug),
|
|
1101
|
+
syncBaselineWorkRoot(context, work.slug),
|
|
1102
|
+
);
|
|
1103
|
+
return { action: "sync", target, work: work.slug, projectionRoot: projection.projectionRoot, files: projection.files, pulled, dryRun: !!options.dryRun };
|
|
809
1104
|
}
|
|
810
1105
|
const gitRoot = gitRootFor(projection.journalRoot, options);
|
|
811
|
-
const pathspec = journalPathspec(gitRoot, projection.journalRoot);
|
|
1106
|
+
const pathspec = journalPathspec(gitRoot, path.join(projection.journalRoot, "work"));
|
|
812
1107
|
const unmerged = runGit(gitRoot, ["diff", "--name-only", "--diff-filter=U", "--", pathspec], options);
|
|
813
1108
|
if (unmerged) throw new InstallerError(`unresolved journal conflicts:\n${unmerged}`, "SYNC_CONFLICT");
|
|
814
|
-
if (options.dryRun) return { action: "sync", target, work: work.slug, projectionRoot: projection.projectionRoot, gitRoot, pathspec, files: projection.files, dryRun: true };
|
|
1109
|
+
if (options.dryRun) return { action: "sync", target, work: work.slug, projectionRoot: projection.projectionRoot, gitRoot, pathspec, files: projection.files, pulled, dryRun: true };
|
|
815
1110
|
|
|
816
|
-
runGit(gitRoot, ["pull", "--ff-only"], options);
|
|
817
|
-
const afterPullUnmerged = runGit(gitRoot, ["diff", "--name-only", "--diff-filter=U", "--", pathspec], options);
|
|
818
|
-
if (afterPullUnmerged) throw new InstallerError(`unresolved journal conflicts:\n${afterPullUnmerged}`, "SYNC_CONFLICT");
|
|
819
1111
|
runGit(gitRoot, ["add", "--", pathspec], options);
|
|
820
1112
|
let committed = true;
|
|
821
1113
|
try {
|
|
@@ -826,7 +1118,11 @@ function sync(options) {
|
|
|
826
1118
|
runGit(gitRoot, ["commit", "-m", `chore(journal): sync ${work.slug}`], options);
|
|
827
1119
|
}
|
|
828
1120
|
runGit(gitRoot, ["push"], options);
|
|
829
|
-
|
|
1121
|
+
replaceBaseline(
|
|
1122
|
+
path.join(projection.journalRoot, "work", work.slug),
|
|
1123
|
+
syncBaselineWorkRoot(context, work.slug),
|
|
1124
|
+
);
|
|
1125
|
+
return { action: "sync", target, work: work.slug, projectionRoot: projection.projectionRoot, gitRoot, pathspec, files: projection.files, pulled, committed, pushed: true, auto: !!options.auto };
|
|
830
1126
|
}
|
|
831
1127
|
|
|
832
1128
|
function doctor(options) {
|
|
@@ -1007,9 +1303,54 @@ function recordStatus(target, record) {
|
|
|
1007
1303
|
function status(options) {
|
|
1008
1304
|
const target = checkTarget(options.target || process.cwd());
|
|
1009
1305
|
const manifest = loadManifest(target);
|
|
1010
|
-
|
|
1306
|
+
const config = readProjectConfig(projectContext(target, options));
|
|
1307
|
+
const cache = readUpdateCache(options);
|
|
1308
|
+
const updateState = deriveUpdateState({
|
|
1309
|
+
cliVersion: packageVersion(packageSourceRoot(options)),
|
|
1310
|
+
projectVersion: manifest?.toolVersion,
|
|
1311
|
+
cache,
|
|
1312
|
+
});
|
|
1313
|
+
const updateChecksEnabled = passiveChecksEnabled(config, options.env || process.env, { allowTest: options.allowTest });
|
|
1314
|
+
if (!manifest) return { installed: false, target, ...updateState, updateChecksEnabled };
|
|
1011
1315
|
const files = manifest.files.map((record) => ({ path: record.path, mode: record.mode, status: recordStatus(target, record) }));
|
|
1012
|
-
return {
|
|
1316
|
+
return {
|
|
1317
|
+
installed: true,
|
|
1318
|
+
target,
|
|
1319
|
+
toolVersion: manifest.toolVersion,
|
|
1320
|
+
...updateState,
|
|
1321
|
+
updateChecksEnabled,
|
|
1322
|
+
harnesses: manifest.harnesses,
|
|
1323
|
+
files,
|
|
1324
|
+
clean: files.every((file) => file.status === "clean"),
|
|
1325
|
+
};
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
async function checkUpdates(options) {
|
|
1329
|
+
const target = checkTarget(options.target || process.cwd());
|
|
1330
|
+
const manifest = loadManifest(target);
|
|
1331
|
+
const config = readProjectConfig(projectContext(target, options));
|
|
1332
|
+
const cliVersion = packageVersion(packageSourceRoot(options));
|
|
1333
|
+
const enabled = passiveChecksEnabled(config, options.env || process.env, { allowTest: options.allowTest });
|
|
1334
|
+
let cache = readUpdateCache(options);
|
|
1335
|
+
let refreshed = false;
|
|
1336
|
+
if (!options.passive || options.refreshCache) {
|
|
1337
|
+
try {
|
|
1338
|
+
cache = await refreshUpdateCache({ ...options, cliVersion, packageName: "djournal" });
|
|
1339
|
+
refreshed = true;
|
|
1340
|
+
} catch (error) {
|
|
1341
|
+
throw new InstallerError(error.message, error.code || "UPDATE_CHECK_FAILED");
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
const stale = cacheIsStale(cache, { config, now: options.now });
|
|
1345
|
+
return {
|
|
1346
|
+
action: "update-check",
|
|
1347
|
+
target,
|
|
1348
|
+
enabled,
|
|
1349
|
+
stale,
|
|
1350
|
+
refreshNeeded: Boolean(options.passive && enabled && stale),
|
|
1351
|
+
refreshed,
|
|
1352
|
+
...deriveUpdateState({ cliVersion, projectVersion: manifest?.toolVersion, cache }),
|
|
1353
|
+
};
|
|
1013
1354
|
}
|
|
1014
1355
|
|
|
1015
1356
|
module.exports = {
|
|
@@ -1018,12 +1359,14 @@ module.exports = {
|
|
|
1018
1359
|
PROJECT_MARKER_PATH,
|
|
1019
1360
|
SUPPORTED_HARNESSES,
|
|
1020
1361
|
applyTransaction,
|
|
1362
|
+
checkUpdates,
|
|
1021
1363
|
configure,
|
|
1022
1364
|
detectHarnesses,
|
|
1023
1365
|
doctor,
|
|
1024
1366
|
djournalHome,
|
|
1025
1367
|
install,
|
|
1026
1368
|
loadManifest,
|
|
1369
|
+
pull,
|
|
1027
1370
|
projectContext,
|
|
1028
1371
|
recoverTransaction,
|
|
1029
1372
|
selectHarnesses,
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("node:crypto");
|
|
4
|
+
const fs = require("node:fs");
|
|
5
|
+
const os = require("node:os");
|
|
6
|
+
const path = require("node:path");
|
|
7
|
+
const semver = require("semver");
|
|
8
|
+
|
|
9
|
+
const CACHE_FILENAME = "update-check.json";
|
|
10
|
+
const DEFAULT_INTERVAL_HOURS = 24;
|
|
11
|
+
const DEFAULT_TIMEOUT_MS = 3000;
|
|
12
|
+
const MAX_RESPONSE_BYTES = 1024 * 1024;
|
|
13
|
+
const REGISTRY_ACCEPT = "application/vnd.npm.install-v1+json";
|
|
14
|
+
|
|
15
|
+
class UpdateCheckError extends Error {
|
|
16
|
+
constructor(message, code = "UPDATE_CHECK_FAILED") {
|
|
17
|
+
super(message);
|
|
18
|
+
this.name = "UpdateCheckError";
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function djournalHome(options = {}) {
|
|
24
|
+
return path.resolve(options.djournalHome || process.env.DJOURNAL_HOME || path.join(os.homedir(), ".djournal"));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function cacheFile(options = {}) {
|
|
28
|
+
return path.resolve(options.cacheFile || path.join(djournalHome(options), CACHE_FILENAME));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function validVersion(value) {
|
|
32
|
+
return typeof value === "string" ? semver.valid(value) : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function normalizeCache(value) {
|
|
36
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
37
|
+
const latestVersion = validVersion(value.latestVersion);
|
|
38
|
+
const checkedAt = typeof value.checkedAt === "string" && Number.isFinite(Date.parse(value.checkedAt))
|
|
39
|
+
? new Date(value.checkedAt).toISOString()
|
|
40
|
+
: null;
|
|
41
|
+
const distTag = typeof value.distTag === "string" && value.distTag ? value.distTag : null;
|
|
42
|
+
if (!latestVersion || !checkedAt || !distTag) return null;
|
|
43
|
+
return { checkedAt, latestVersion, distTag };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function readCache(options = {}) {
|
|
47
|
+
try {
|
|
48
|
+
const file = cacheFile(options);
|
|
49
|
+
const stat = fs.statSync(file);
|
|
50
|
+
if (!stat.isFile() || stat.size > MAX_RESPONSE_BYTES) return null;
|
|
51
|
+
return normalizeCache(JSON.parse(fs.readFileSync(file, "utf8")));
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function writeCache(value, options = {}) {
|
|
58
|
+
const normalized = normalizeCache(value);
|
|
59
|
+
if (!normalized) throw new UpdateCheckError("update cache is invalid", "INVALID_UPDATE_CACHE");
|
|
60
|
+
const file = cacheFile(options);
|
|
61
|
+
const directory = path.dirname(file);
|
|
62
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
63
|
+
const temporary = path.join(directory, `.${path.basename(file)}.${process.pid}.${crypto.randomBytes(4).toString("hex")}.tmp`);
|
|
64
|
+
try {
|
|
65
|
+
fs.writeFileSync(temporary, `${JSON.stringify(normalized, null, 2)}\n`, { mode: 0o600 });
|
|
66
|
+
fs.renameSync(temporary, file);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
try { fs.unlinkSync(temporary); } catch {}
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
return normalized;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function intervalHours(config = {}) {
|
|
75
|
+
const value = config?.updates?.intervalHours;
|
|
76
|
+
return Number.isFinite(value) && value > 0 ? value : DEFAULT_INTERVAL_HOURS;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function cacheIsStale(cache, options = {}) {
|
|
80
|
+
if (!cache) return true;
|
|
81
|
+
const now = options.now instanceof Date ? options.now.getTime() : Number(options.now ?? Date.now());
|
|
82
|
+
const checkedAt = Date.parse(cache.checkedAt);
|
|
83
|
+
const intervalMs = intervalHours(options.config) * 60 * 60 * 1000;
|
|
84
|
+
return !Number.isFinite(now) || !Number.isFinite(checkedAt) || now - checkedAt >= intervalMs;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function environmentFlag(value) {
|
|
88
|
+
return typeof value === "string" ? !["", "0", "false"].includes(value.toLowerCase()) : Boolean(value);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function passiveChecksEnabled(config = {}, env = process.env, options = {}) {
|
|
92
|
+
if (config?.updates?.enabled === false) return false;
|
|
93
|
+
if (environmentFlag(env.NO_UPDATE_NOTIFIER) || environmentFlag(env.CI)) return false;
|
|
94
|
+
if (!options.allowTest && (env.NODE_ENV === "test" || environmentFlag(env.NODE_TEST_CONTEXT))) return false;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function deriveUpdateState(options = {}) {
|
|
99
|
+
const cliVersion = validVersion(options.cliVersion);
|
|
100
|
+
const projectVersion = validVersion(options.projectVersion);
|
|
101
|
+
const cache = normalizeCache(options.cache);
|
|
102
|
+
const latestVersion = cache?.latestVersion || null;
|
|
103
|
+
return {
|
|
104
|
+
cliVersion,
|
|
105
|
+
projectVersion,
|
|
106
|
+
latestVersion,
|
|
107
|
+
checkedAt: cache?.checkedAt || null,
|
|
108
|
+
distTag: cache?.distTag || options.distTag || "latest",
|
|
109
|
+
updateAvailable: Boolean(cliVersion && latestVersion && semver.gt(latestVersion, cliVersion)),
|
|
110
|
+
projectUpdateAvailable: Boolean(cliVersion && projectVersion && semver.gt(cliVersion, projectVersion)),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function fetchLatest(options = {}) {
|
|
115
|
+
const fetchImpl = options.fetchImpl || globalThis.fetch;
|
|
116
|
+
if (typeof fetchImpl !== "function") throw new UpdateCheckError("fetch is unavailable", "UPDATE_FETCH_UNAVAILABLE");
|
|
117
|
+
const packageName = options.packageName || "djournal";
|
|
118
|
+
const distTag = options.distTag || "latest";
|
|
119
|
+
const timeoutMs = options.timeoutMs || DEFAULT_TIMEOUT_MS;
|
|
120
|
+
const signal = options.signal || AbortSignal.timeout(timeoutMs);
|
|
121
|
+
let response;
|
|
122
|
+
try {
|
|
123
|
+
response = await fetchImpl(`https://registry.npmjs.org/${encodeURIComponent(packageName)}`, {
|
|
124
|
+
headers: {
|
|
125
|
+
accept: REGISTRY_ACCEPT,
|
|
126
|
+
"user-agent": `${packageName}/${options.cliVersion || "unknown"}`,
|
|
127
|
+
},
|
|
128
|
+
signal,
|
|
129
|
+
});
|
|
130
|
+
} catch (error) {
|
|
131
|
+
throw new UpdateCheckError(`npm registry request failed: ${error.message}`, "UPDATE_NETWORK_FAILED");
|
|
132
|
+
}
|
|
133
|
+
if (!response || response.ok !== true) {
|
|
134
|
+
throw new UpdateCheckError(`npm registry returned HTTP ${response?.status || "unknown"}`, "UPDATE_HTTP_FAILED");
|
|
135
|
+
}
|
|
136
|
+
const text = await response.text();
|
|
137
|
+
if (Buffer.byteLength(text) > MAX_RESPONSE_BYTES) {
|
|
138
|
+
throw new UpdateCheckError("npm registry response is too large", "UPDATE_RESPONSE_INVALID");
|
|
139
|
+
}
|
|
140
|
+
let metadata;
|
|
141
|
+
try { metadata = JSON.parse(text); } catch {
|
|
142
|
+
throw new UpdateCheckError("npm registry response is not valid JSON", "UPDATE_RESPONSE_INVALID");
|
|
143
|
+
}
|
|
144
|
+
const latestVersion = validVersion(metadata?.["dist-tags"]?.[distTag]);
|
|
145
|
+
if (!latestVersion) {
|
|
146
|
+
throw new UpdateCheckError(`npm registry response has no valid ${distTag} version`, "UPDATE_RESPONSE_INVALID");
|
|
147
|
+
}
|
|
148
|
+
return { latestVersion, distTag };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function refreshCache(options = {}) {
|
|
152
|
+
const latest = await fetchLatest(options);
|
|
153
|
+
const now = options.now instanceof Date ? options.now : new Date(options.now ?? Date.now());
|
|
154
|
+
if (!Number.isFinite(now.getTime())) throw new UpdateCheckError("current time is invalid", "UPDATE_TIME_INVALID");
|
|
155
|
+
return writeCache({ ...latest, checkedAt: now.toISOString() }, options);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
module.exports = {
|
|
159
|
+
CACHE_FILENAME,
|
|
160
|
+
DEFAULT_INTERVAL_HOURS,
|
|
161
|
+
DEFAULT_TIMEOUT_MS,
|
|
162
|
+
MAX_RESPONSE_BYTES,
|
|
163
|
+
REGISTRY_ACCEPT,
|
|
164
|
+
UpdateCheckError,
|
|
165
|
+
cacheFile,
|
|
166
|
+
cacheIsStale,
|
|
167
|
+
deriveUpdateState,
|
|
168
|
+
djournalHome,
|
|
169
|
+
fetchLatest,
|
|
170
|
+
intervalHours,
|
|
171
|
+
normalizeCache,
|
|
172
|
+
passiveChecksEnabled,
|
|
173
|
+
readCache,
|
|
174
|
+
refreshCache,
|
|
175
|
+
validVersion,
|
|
176
|
+
writeCache,
|
|
177
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "djournal",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Durable project memory for coding agents, stored as linked Markdown",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"bin": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"check:pr-title": "node scripts/check-pr-title.js",
|
|
27
27
|
"link:global": "npm link",
|
|
28
28
|
"release": "semantic-release",
|
|
29
|
-
"test": "node tests/journal-hook.test.js && node tests/pi-adapter.test.js && node tests/installer/installer.test.js && node tests/release-policy.test.js && node tests/skill-instructions.test.js && npm run check:package",
|
|
29
|
+
"test": "node tests/update-checker.test.js && node tests/journal-hook.test.js && node tests/pi-adapter.test.js && node tests/installer/installer.test.js && node tests/release-policy.test.js && node tests/skill-instructions.test.js && npm run check:package",
|
|
30
30
|
"prepublishOnly": "npm test"
|
|
31
31
|
},
|
|
32
32
|
"repository": {
|
|
@@ -54,5 +54,8 @@
|
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"semantic-release": "25.0.3"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"semver": "7.8.5"
|
|
57
60
|
}
|
|
58
61
|
}
|